@@ -8,16 +8,17 @@ use libc::c_int;
88use rustc_codegen_ssa:: target_features:: {
99 supported_target_features, tied_target_features, RUSTC_SPECIFIC_FEATURES ,
1010} ;
11+ use rustc_codegen_ssa:: traits:: PrintBackendInfo ;
1112use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
1213use rustc_data_structures:: small_c_str:: SmallCStr ;
1314use rustc_fs_util:: path_to_c_string;
1415use rustc_middle:: bug;
15- use rustc_session:: config:: PrintRequest ;
16+ use rustc_session:: config:: { PrintKind , PrintRequest } ;
1617use rustc_session:: Session ;
1718use rustc_span:: symbol:: Symbol ;
1819use rustc_target:: spec:: { MergeFunctions , PanicStrategy } ;
19- use std:: ffi:: { CStr , CString } ;
2020
21+ use std:: ffi:: { c_char, c_void, CStr , CString } ;
2122use std:: path:: Path ;
2223use std:: ptr;
2324use std:: slice;
@@ -110,6 +111,10 @@ unsafe fn configure_llvm(sess: &Session) {
110111 // Use non-zero `import-instr-limit` multiplier for cold callsites.
111112 add ( "-import-cold-multiplier=0.1" , false ) ;
112113
114+ if sess. print_llvm_stats ( ) {
115+ add ( "-stats" , false ) ;
116+ }
117+
113118 for arg in sess_args {
114119 add ( & ( * arg) , true ) ;
115120 }
@@ -350,7 +355,7 @@ fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
350355 ret
351356}
352357
353- fn print_target_features ( sess : & Session , tm : & llvm:: TargetMachine ) {
358+ fn print_target_features ( out : & mut dyn PrintBackendInfo , sess : & Session , tm : & llvm:: TargetMachine ) {
354359 let mut llvm_target_features = llvm_target_features ( tm) ;
355360 let mut known_llvm_target_features = FxHashSet :: < & ' static str > :: default ( ) ;
356361 let mut rustc_target_features = supported_target_features ( sess)
@@ -383,36 +388,48 @@ fn print_target_features(sess: &Session, tm: &llvm::TargetMachine) {
383388 . max ( )
384389 . unwrap_or ( 0 ) ;
385390
386- println ! ( "Features supported by rustc for this target:" ) ;
391+ writeln ! ( out , "Features supported by rustc for this target:" ) ;
387392 for ( feature, desc) in & rustc_target_features {
388- println ! ( " {1:0$} - {2}." , max_feature_len, feature, desc) ;
393+ writeln ! ( out , " {1:0$} - {2}." , max_feature_len, feature, desc) ;
389394 }
390- println ! ( "\n Code-generation features supported by LLVM for this target:" ) ;
395+ writeln ! ( out , "\n Code-generation features supported by LLVM for this target:" ) ;
391396 for ( feature, desc) in & llvm_target_features {
392- println ! ( " {1:0$} - {2}." , max_feature_len, feature, desc) ;
397+ writeln ! ( out , " {1:0$} - {2}." , max_feature_len, feature, desc) ;
393398 }
394399 if llvm_target_features. is_empty ( ) {
395- println ! ( " Target features listing is not supported by this LLVM version." ) ;
400+ writeln ! ( out , " Target features listing is not supported by this LLVM version." ) ;
396401 }
397- println ! ( "\n Use +feature to enable a feature, or -feature to disable it." ) ;
398- println ! ( "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n " ) ;
399- println ! ( "Code-generation features cannot be used in cfg or #[target_feature]," ) ;
400- println ! ( "and may be renamed or removed in a future version of LLVM or rustc.\n " ) ;
402+ writeln ! ( out , "\n Use +feature to enable a feature, or -feature to disable it." ) ;
403+ writeln ! ( out , "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n " ) ;
404+ writeln ! ( out , "Code-generation features cannot be used in cfg or #[target_feature]," ) ;
405+ writeln ! ( out , "and may be renamed or removed in a future version of LLVM or rustc.\n " ) ;
401406}
402407
403- pub ( crate ) fn print ( req : PrintRequest , sess : & Session ) {
408+ pub ( crate ) fn print ( req : & PrintRequest , mut out : & mut dyn PrintBackendInfo , sess : & Session ) {
404409 require_inited ( ) ;
405410 let tm = create_informational_target_machine ( sess) ;
406- match req {
407- PrintRequest :: TargetCPUs => {
411+ match req. kind {
412+ PrintKind :: TargetCPUs => {
408413 // SAFETY generate a C compatible string from a byte slice to pass
409414 // the target CPU name into LLVM, the lifetime of the reference is
410415 // at least as long as the C function
411416 let cpu_cstring = CString :: new ( handle_native ( sess. target . cpu . as_ref ( ) ) )
412417 . unwrap_or_else ( |e| bug ! ( "failed to convert to cstring: {}" , e) ) ;
413- unsafe { llvm:: LLVMRustPrintTargetCPUs ( tm, cpu_cstring. as_ptr ( ) ) } ;
418+ unsafe extern "C" fn callback ( out : * mut c_void , string : * const c_char , len : usize ) {
419+ let out = & mut * ( out as * mut & mut dyn PrintBackendInfo ) ;
420+ let bytes = slice:: from_raw_parts ( string as * const u8 , len) ;
421+ write ! ( out, "{}" , String :: from_utf8_lossy( bytes) ) ;
422+ }
423+ unsafe {
424+ llvm:: LLVMRustPrintTargetCPUs (
425+ tm,
426+ cpu_cstring. as_ptr ( ) ,
427+ callback,
428+ & mut out as * mut & mut dyn PrintBackendInfo as * mut c_void ,
429+ ) ;
430+ }
414431 }
415- PrintRequest :: TargetFeatures => print_target_features ( sess, tm) ,
432+ PrintKind :: TargetFeatures => print_target_features ( out , sess, tm) ,
416433 _ => bug ! ( "rustc_codegen_llvm can't handle print request: {:?}" , req) ,
417434 }
418435}
0 commit comments