1- use std:: path:: Path ;
1+ use std:: path:: { Path , PathBuf } ;
22use std:: process:: Command ;
33
44/// Uses the `llvm-bolt` binary to instrument the binary/library at the given `path` with BOLT.
55/// When the instrumented artifact is executed, it will generate BOLT profiles into
66/// `/tmp/prof.fdata.<pid>.fdata`.
7- pub fn instrument_with_bolt_inplace ( path : & Path ) {
7+ /// Returns a path to the instrumented artifact, created in a temporary directory.
8+ pub fn instrument_with_bolt ( path : & Path ) -> PathBuf {
89 let dir = std:: env:: temp_dir ( ) ;
9- let instrumented_path = dir. join ( "instrumented.so" ) ;
10+ let instrumented_path = dir. join ( path . file_name ( ) . unwrap ( ) ) ;
1011
1112 let status = Command :: new ( "llvm-bolt" )
1213 . arg ( "-instrument" )
@@ -21,19 +22,19 @@ pub fn instrument_with_bolt_inplace(path: &Path) {
2122 if !status. success ( ) {
2223 panic ! ( "Could not instrument {} with BOLT, exit code {:?}" , path. display( ) , status. code( ) ) ;
2324 }
24-
25- std:: fs:: copy ( & instrumented_path, path) . expect ( "Cannot copy instrumented artifact" ) ;
26- std:: fs:: remove_file ( instrumented_path) . expect ( "Cannot delete instrumented artifact" ) ;
25+ instrumented_path
2726}
2827
2928/// Uses the `llvm-bolt` binary to optimize the binary/library at the given `path` with BOLT,
3029/// using merged profiles from `profile_path`.
3130///
3231/// The recorded profiles have to be merged using the `merge-fdata` tool from LLVM and the merged
3332/// profile path should be then passed to this function.
34- pub fn optimize_library_with_bolt_inplace ( path : & Path , profile_path : & Path ) {
33+ ///
34+ /// Returns a path to the optimized artifact, created in a temporary directory.
35+ pub fn optimize_with_bolt ( path : & Path , profile_path : & Path ) -> PathBuf {
3536 let dir = std:: env:: temp_dir ( ) ;
36- let optimized_path = dir. join ( "optimized.so" ) ;
37+ let optimized_path = dir. join ( path . file_name ( ) . unwrap ( ) ) ;
3738
3839 let status = Command :: new ( "llvm-bolt" )
3940 . arg ( & path)
@@ -65,7 +66,5 @@ pub fn optimize_library_with_bolt_inplace(path: &Path, profile_path: &Path) {
6566 if !status. success ( ) {
6667 panic ! ( "Could not optimize {} with BOLT, exit code {:?}" , path. display( ) , status. code( ) ) ;
6768 }
68-
69- std:: fs:: copy ( & optimized_path, path) . expect ( "Cannot copy optimized artifact" ) ;
70- std:: fs:: remove_file ( optimized_path) . expect ( "Cannot delete optimized artifact" ) ;
69+ optimized_path
7170}
0 commit comments