@@ -35,7 +35,6 @@ extern crate rustc_middle;
3535extern crate rustc_session;
3636extern crate rustc_span;
3737extern crate rustc_target;
38- extern crate tempfile;
3938
4039// This prevents duplicating functions and statics that are already part of the host rustc process.
4140#[ allow( unused_extern_crates) ]
@@ -64,10 +63,10 @@ mod type_;
6463mod type_of;
6564
6665use std:: any:: Any ;
67- use std:: sync:: { Arc , Mutex } ;
66+ use std:: sync:: Arc ;
6867
6968use crate :: errors:: LTONotSupported ;
70- use gccjit:: { Context , OptimizationLevel , CType } ;
69+ use gccjit:: { Context , OptimizationLevel , TargetInfo } ;
7170use rustc_ast:: expand:: allocator:: AllocatorKind ;
7271use rustc_codegen_ssa:: { CodegenResults , CompiledModule , ModuleCodegen } ;
7372use rustc_codegen_ssa:: base:: codegen_crate;
@@ -86,7 +85,6 @@ use rustc_session::config::{Lto, OptLevel, OutputFilenames};
8685use rustc_session:: Session ;
8786use rustc_span:: Symbol ;
8887use rustc_span:: fatal_error:: FatalError ;
89- use tempfile:: TempDir ;
9088
9189fluent_messages ! { "../messages.ftl" }
9290
@@ -102,7 +100,7 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
102100
103101#[ derive( Clone ) ]
104102pub struct GccCodegenBackend {
105- supports_128bit_integers : Arc < Mutex < bool > > ,
103+ target_info : Arc < TargetInfo > ,
106104}
107105
108106impl CodegenBackend for GccCodegenBackend {
@@ -116,15 +114,6 @@ impl CodegenBackend for GccCodegenBackend {
116114 if sess. lto ( ) != Lto :: No {
117115 sess. emit_warning ( LTONotSupported { } ) ;
118116 }
119-
120- let temp_dir = TempDir :: new ( ) . expect ( "cannot create temporary directory" ) ;
121- let temp_file = temp_dir. into_path ( ) . join ( "result.asm" ) ;
122- let check_context = Context :: default ( ) ;
123- check_context. set_print_errors_to_stderr ( false ) ;
124- let _int128_ty = check_context. new_c_type ( CType :: UInt128t ) ;
125- // NOTE: we cannot just call compile() as this would require other files than libgccjit.so.
126- check_context. compile_to_file ( gccjit:: OutputKind :: Assembler , temp_file. to_str ( ) . expect ( "path to str" ) ) ;
127- * self . supports_128bit_integers . lock ( ) . expect ( "lock" ) = check_context. get_last_error ( ) == Ok ( None ) ;
128117 }
129118
130119 fn provide ( & self , providers : & mut Providers ) {
@@ -160,7 +149,7 @@ impl CodegenBackend for GccCodegenBackend {
160149 }
161150
162151 fn target_features ( & self , sess : & Session , allow_unstable : bool ) -> Vec < Symbol > {
163- target_features ( sess, allow_unstable)
152+ target_features ( sess, allow_unstable, & self . target_info )
164153 }
165154}
166155
@@ -174,7 +163,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
174163 }
175164
176165 fn compile_codegen_unit ( & self , tcx : TyCtxt < ' _ > , cgu_name : Symbol ) -> ( ModuleCodegen < Self :: Module > , u64 ) {
177- base:: compile_codegen_unit ( tcx, cgu_name, * self . supports_128bit_integers . lock ( ) . expect ( "lock" ) )
166+ base:: compile_codegen_unit ( tcx, cgu_name, Arc :: clone ( & self . target_info ) )
178167 }
179168
180169 fn target_machine_factory ( & self , _sess : & Session , _opt_level : OptLevel , _features : & [ String ] ) -> TargetMachineFactoryFn < Self > {
@@ -273,8 +262,17 @@ impl WriteBackendMethods for GccCodegenBackend {
273262/// This is the entrypoint for a hot plugged rustc_codegen_gccjit
274263#[ no_mangle]
275264pub fn __rustc_codegen_backend ( ) -> Box < dyn CodegenBackend > {
265+ // Get the native arch and check whether the target supports 128-bit integers.
266+ let context = Context :: default ( ) ;
267+ let arch = context. get_target_info ( ) . arch ( ) . unwrap ( ) ;
268+
269+ // Get the second TargetInfo with the correct CPU features by setting the arch.
270+ let context = Context :: default ( ) ;
271+ context. add_driver_option ( & format ! ( "-march={}" , arch. to_str( ) . unwrap( ) ) ) ;
272+ let target_info = Arc :: new ( context. get_target_info ( ) ) ;
273+
276274 Box :: new ( GccCodegenBackend {
277- supports_128bit_integers : Arc :: new ( Mutex :: new ( false ) ) ,
275+ target_info ,
278276 } )
279277}
280278
@@ -308,7 +306,7 @@ pub fn target_cpu(sess: &Session) -> &str {
308306 }
309307}
310308
311- pub fn target_features ( sess : & Session , allow_unstable : bool ) -> Vec < Symbol > {
309+ pub fn target_features ( sess : & Session , allow_unstable : bool , target_info : & Arc < TargetInfo > ) -> Vec < Symbol > {
312310 supported_target_features ( sess)
313311 . iter ( )
314312 . filter_map (
@@ -317,14 +315,9 @@ pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
317315 } ,
318316 )
319317 . filter ( |_feature| {
320- // TODO(antoyo): implement a way to get enabled feature in libgccjit.
321- // Probably using the equivalent of __builtin_cpu_supports.
322- // TODO(antoyo): maybe use whatever outputs the following command:
323- // gcc -march=native -Q --help=target
324318 #[ cfg( feature="master" ) ]
325319 {
326- // NOTE: the CPU in the CI doesn't support sse4a, so disable it to make the stdarch tests pass in the CI.
327- ( _feature. contains ( "sse" ) || _feature. contains ( "avx" ) ) && !_feature. contains ( "avx512" ) && !_feature. contains ( "sse4a" )
320+ target_info. cpu_supports ( _feature)
328321 }
329322 #[ cfg( not( feature="master" ) ) ]
330323 {
@@ -336,7 +329,6 @@ pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
336329 bmi1, bmi2, cmpxchg16b, ermsb, f16c, fma, fxsr, gfni, lzcnt, movbe, pclmulqdq, popcnt, rdrand, rdseed, rtm,
337330 sha, sse, sse2, sse3, sse4.1, sse4.2, sse4a, ssse3, tbm, vaes, vpclmulqdq, xsave, xsavec, xsaveopt, xsaves
338331 */
339- //false
340332 } )
341333 . map ( |feature| Symbol :: intern ( feature) )
342334 . collect ( )
0 commit comments