@@ -8,16 +8,17 @@ use libc::c_int;
8
8
use rustc_codegen_ssa:: target_features:: {
9
9
supported_target_features, tied_target_features, RUSTC_SPECIFIC_FEATURES ,
10
10
} ;
11
+ use rustc_codegen_ssa:: traits:: PrintBackendInfo ;
11
12
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
12
13
use rustc_data_structures:: small_c_str:: SmallCStr ;
13
14
use rustc_fs_util:: path_to_c_string;
14
15
use rustc_middle:: bug;
15
- use rustc_session:: config:: PrintRequest ;
16
+ use rustc_session:: config:: { PrintKind , PrintRequest } ;
16
17
use rustc_session:: Session ;
17
18
use rustc_span:: symbol:: Symbol ;
18
19
use rustc_target:: spec:: { MergeFunctions , PanicStrategy } ;
19
- use std:: ffi:: { CStr , CString } ;
20
20
21
+ use std:: ffi:: { c_char, c_void, CStr , CString } ;
21
22
use std:: path:: Path ;
22
23
use std:: ptr;
23
24
use std:: slice;
@@ -110,6 +111,10 @@ unsafe fn configure_llvm(sess: &Session) {
110
111
// Use non-zero `import-instr-limit` multiplier for cold callsites.
111
112
add ( "-import-cold-multiplier=0.1" , false ) ;
112
113
114
+ if sess. print_llvm_stats ( ) {
115
+ add ( "-stats" , false ) ;
116
+ }
117
+
113
118
for arg in sess_args {
114
119
add ( & ( * arg) , true ) ;
115
120
}
@@ -350,7 +355,7 @@ fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
350
355
ret
351
356
}
352
357
353
- fn print_target_features ( sess : & Session , tm : & llvm:: TargetMachine ) {
358
+ fn print_target_features ( out : & mut dyn PrintBackendInfo , sess : & Session , tm : & llvm:: TargetMachine ) {
354
359
let mut llvm_target_features = llvm_target_features ( tm) ;
355
360
let mut known_llvm_target_features = FxHashSet :: < & ' static str > :: default ( ) ;
356
361
let mut rustc_target_features = supported_target_features ( sess)
@@ -383,36 +388,48 @@ fn print_target_features(sess: &Session, tm: &llvm::TargetMachine) {
383
388
. max ( )
384
389
. unwrap_or ( 0 ) ;
385
390
386
- println ! ( "Features supported by rustc for this target:" ) ;
391
+ writeln ! ( out , "Features supported by rustc for this target:" ) ;
387
392
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) ;
389
394
}
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:" ) ;
391
396
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) ;
393
398
}
394
399
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." ) ;
396
401
}
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 " ) ;
401
406
}
402
407
403
- pub ( crate ) fn print ( req : PrintRequest , sess : & Session ) {
408
+ pub ( crate ) fn print ( req : & PrintRequest , mut out : & mut dyn PrintBackendInfo , sess : & Session ) {
404
409
require_inited ( ) ;
405
410
let tm = create_informational_target_machine ( sess) ;
406
- match req {
407
- PrintRequest :: TargetCPUs => {
411
+ match req. kind {
412
+ PrintKind :: TargetCPUs => {
408
413
// SAFETY generate a C compatible string from a byte slice to pass
409
414
// the target CPU name into LLVM, the lifetime of the reference is
410
415
// at least as long as the C function
411
416
let cpu_cstring = CString :: new ( handle_native ( sess. target . cpu . as_ref ( ) ) )
412
417
. 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
+ }
414
431
}
415
- PrintRequest :: TargetFeatures => print_target_features ( sess, tm) ,
432
+ PrintKind :: TargetFeatures => print_target_features ( out , sess, tm) ,
416
433
_ => bug ! ( "rustc_codegen_llvm can't handle print request: {:?}" , req) ,
417
434
}
418
435
}
0 commit comments