@@ -35,7 +35,6 @@ extern crate rustc_middle;
35
35
extern crate rustc_session;
36
36
extern crate rustc_span;
37
37
extern crate rustc_target;
38
- extern crate tempfile;
39
38
40
39
// This prevents duplicating functions and statics that are already part of the host rustc process.
41
40
#[ allow( unused_extern_crates) ]
@@ -64,10 +63,10 @@ mod type_;
64
63
mod type_of;
65
64
66
65
use std:: any:: Any ;
67
- use std:: sync:: { Arc , Mutex } ;
66
+ use std:: sync:: Arc ;
68
67
69
68
use crate :: errors:: LTONotSupported ;
70
- use gccjit:: { Context , OptimizationLevel , CType } ;
69
+ use gccjit:: { Context , OptimizationLevel , TargetInfo } ;
71
70
use rustc_ast:: expand:: allocator:: AllocatorKind ;
72
71
use rustc_codegen_ssa:: { CodegenResults , CompiledModule , ModuleCodegen } ;
73
72
use rustc_codegen_ssa:: base:: codegen_crate;
@@ -86,7 +85,6 @@ use rustc_session::config::{Lto, OptLevel, OutputFilenames};
86
85
use rustc_session:: Session ;
87
86
use rustc_span:: Symbol ;
88
87
use rustc_span:: fatal_error:: FatalError ;
89
- use tempfile:: TempDir ;
90
88
91
89
fluent_messages ! { "../messages.ftl" }
92
90
@@ -102,7 +100,7 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
102
100
103
101
#[ derive( Clone ) ]
104
102
pub struct GccCodegenBackend {
105
- supports_128bit_integers : Arc < Mutex < bool > > ,
103
+ target_info : Arc < TargetInfo > ,
106
104
}
107
105
108
106
impl CodegenBackend for GccCodegenBackend {
@@ -116,15 +114,6 @@ impl CodegenBackend for GccCodegenBackend {
116
114
if sess. lto ( ) != Lto :: No {
117
115
sess. emit_warning ( LTONotSupported { } ) ;
118
116
}
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 ) ;
128
117
}
129
118
130
119
fn provide ( & self , providers : & mut Providers ) {
@@ -160,7 +149,7 @@ impl CodegenBackend for GccCodegenBackend {
160
149
}
161
150
162
151
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 )
164
153
}
165
154
}
166
155
@@ -174,7 +163,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
174
163
}
175
164
176
165
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 ) )
178
167
}
179
168
180
169
fn target_machine_factory ( & self , _sess : & Session , _opt_level : OptLevel , _features : & [ String ] ) -> TargetMachineFactoryFn < Self > {
@@ -273,8 +262,17 @@ impl WriteBackendMethods for GccCodegenBackend {
273
262
/// This is the entrypoint for a hot plugged rustc_codegen_gccjit
274
263
#[ no_mangle]
275
264
pub 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
+
276
274
Box :: new ( GccCodegenBackend {
277
- supports_128bit_integers : Arc :: new ( Mutex :: new ( false ) ) ,
275
+ target_info ,
278
276
} )
279
277
}
280
278
@@ -308,7 +306,7 @@ pub fn target_cpu(sess: &Session) -> &str {
308
306
}
309
307
}
310
308
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 > {
312
310
supported_target_features ( sess)
313
311
. iter ( )
314
312
. filter_map (
@@ -317,14 +315,9 @@ pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
317
315
} ,
318
316
)
319
317
. 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
324
318
#[ cfg( feature="master" ) ]
325
319
{
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)
328
321
}
329
322
#[ cfg( not( feature="master" ) ) ]
330
323
{
@@ -336,7 +329,6 @@ pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
336
329
bmi1, bmi2, cmpxchg16b, ermsb, f16c, fma, fxsr, gfni, lzcnt, movbe, pclmulqdq, popcnt, rdrand, rdseed, rtm,
337
330
sha, sse, sse2, sse3, sse4.1, sse4.2, sse4a, ssse3, tbm, vaes, vpclmulqdq, xsave, xsavec, xsaveopt, xsaves
338
331
*/
339
- //false
340
332
} )
341
333
. map ( |feature| Symbol :: intern ( feature) )
342
334
. collect ( )
0 commit comments