@@ -73,6 +73,7 @@ mod type_of;
73
73
74
74
use std:: any:: Any ;
75
75
use std:: sync:: Arc ;
76
+ use std:: sync:: Mutex ;
76
77
#[ cfg( not( feature="master" ) ) ]
77
78
use std:: sync:: atomic:: AtomicBool ;
78
79
#[ cfg( not( feature="master" ) ) ]
@@ -135,9 +136,24 @@ impl TargetInfo {
135
136
}
136
137
}
137
138
139
+ #[ derive( Clone , Debug ) ]
140
+ pub struct LockedTargetInfo {
141
+ info : Arc < Mutex < TargetInfo > > ,
142
+ }
143
+
144
+ impl LockedTargetInfo {
145
+ fn cpu_supports ( & self , feature : & str ) -> bool {
146
+ self . info . lock ( ) . expect ( "lock" ) . cpu_supports ( feature)
147
+ }
148
+
149
+ fn supports_128bit_int ( & self ) -> bool {
150
+ self . info . lock ( ) . expect ( "lock" ) . supports_128bit_int ( )
151
+ }
152
+ }
153
+
138
154
#[ derive( Clone ) ]
139
155
pub struct GccCodegenBackend {
140
- target_info : Arc < TargetInfo > ,
156
+ target_info : LockedTargetInfo ,
141
157
}
142
158
143
159
impl CodegenBackend for GccCodegenBackend {
@@ -146,6 +162,17 @@ impl CodegenBackend for GccCodegenBackend {
146
162
}
147
163
148
164
fn init ( & self , sess : & Session ) {
165
+ #[ cfg( feature="master" ) ]
166
+ {
167
+ let target_cpu = target_cpu ( sess) ;
168
+
169
+ // Get the second TargetInfo with the correct CPU features by setting the arch.
170
+ let context = Context :: default ( ) ;
171
+ context. add_command_line_option ( & format ! ( "-march={}" , target_cpu) ) ;
172
+
173
+ * self . target_info . info . lock ( ) . expect ( "lock" ) = context. get_target_info ( ) ;
174
+ }
175
+
149
176
#[ cfg( feature="master" ) ]
150
177
gccjit:: set_global_personality_function_name ( b"rust_eh_personality\0 " ) ;
151
178
if sess. lto ( ) == Lto :: Thin {
@@ -161,7 +188,7 @@ impl CodegenBackend for GccCodegenBackend {
161
188
let _int128_ty = check_context. new_c_type ( CType :: UInt128t ) ;
162
189
// NOTE: we cannot just call compile() as this would require other files than libgccjit.so.
163
190
check_context. compile_to_file ( gccjit:: OutputKind :: Assembler , temp_file. to_str ( ) . expect ( "path to str" ) ) ;
164
- self . target_info . supports_128bit_integers . store ( check_context. get_last_error ( ) == Ok ( None ) , Ordering :: SeqCst ) ;
191
+ self . target_info . info . lock ( ) . expect ( "lock" ) . supports_128bit_integers . store ( check_context. get_last_error ( ) == Ok ( None ) , Ordering :: SeqCst ) ;
165
192
}
166
193
}
167
194
@@ -217,7 +244,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
217
244
}
218
245
219
246
fn compile_codegen_unit ( & self , tcx : TyCtxt < ' _ > , cgu_name : Symbol ) -> ( ModuleCodegen < Self :: Module > , u64 ) {
220
- base:: compile_codegen_unit ( tcx, cgu_name, Arc :: clone ( & self . target_info ) )
247
+ base:: compile_codegen_unit ( tcx, cgu_name, self . target_info . clone ( ) )
221
248
}
222
249
223
250
fn target_machine_factory ( & self , _sess : & Session , _opt_level : OptLevel , _features : & [ String ] ) -> TargetMachineFactoryFn < Self > {
@@ -306,23 +333,18 @@ impl WriteBackendMethods for GccCodegenBackend {
306
333
#[ no_mangle]
307
334
pub fn __rustc_codegen_backend ( ) -> Box < dyn CodegenBackend > {
308
335
#[ cfg( feature="master" ) ]
309
- let target_info = {
310
- // Get the native arch and check whether the target supports 128-bit integers.
311
- let context = Context :: default ( ) ;
312
- let arch = context. get_target_info ( ) . arch ( ) . unwrap ( ) ;
313
-
314
- // Get the second TargetInfo with the correct CPU features by setting the arch.
336
+ let info = {
337
+ // Check whether the target supports 128-bit integers.
315
338
let context = Context :: default ( ) ;
316
- context. add_command_line_option ( & format ! ( "-march={}" , arch. to_str( ) . unwrap( ) ) ) ;
317
- Arc :: new ( context. get_target_info ( ) )
339
+ Arc :: new ( Mutex :: new ( context. get_target_info ( ) ) )
318
340
} ;
319
341
#[ cfg( not( feature="master" ) ) ]
320
- let target_info = Arc :: new ( TargetInfo {
342
+ let info = Arc :: new ( Mutex :: new ( TargetInfo {
321
343
supports_128bit_integers : AtomicBool :: new ( false ) ,
322
- } ) ;
344
+ } ) ) ;
323
345
324
346
Box :: new ( GccCodegenBackend {
325
- target_info,
347
+ target_info : LockedTargetInfo { info } ,
326
348
} )
327
349
}
328
350
@@ -356,7 +378,7 @@ pub fn target_cpu(sess: &Session) -> &str {
356
378
}
357
379
}
358
380
359
- pub fn target_features ( sess : & Session , allow_unstable : bool , target_info : & Arc < TargetInfo > ) -> Vec < Symbol > {
381
+ pub fn target_features ( sess : & Session , allow_unstable : bool , target_info : & LockedTargetInfo ) -> Vec < Symbol > {
360
382
supported_target_features ( sess)
361
383
. iter ( )
362
384
. filter_map (
0 commit comments