@@ -35,6 +35,7 @@ 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;
38
39
39
40
// This prevents duplicating functions and statics that are already part of the host rustc process.
40
41
#[ allow( unused_extern_crates) ]
@@ -66,7 +67,9 @@ use std::any::Any;
66
67
use std:: sync:: Arc ;
67
68
68
69
use crate :: errors:: LTONotSupported ;
69
- use gccjit:: { Context , OptimizationLevel , TargetInfo } ;
70
+ use gccjit:: { Context , OptimizationLevel } ;
71
+ #[ cfg( feature="master" ) ]
72
+ use gccjit:: TargetInfo ;
70
73
use rustc_ast:: expand:: allocator:: AllocatorKind ;
71
74
use rustc_codegen_ssa:: { CodegenResults , CompiledModule , ModuleCodegen } ;
72
75
use rustc_codegen_ssa:: base:: codegen_crate;
@@ -259,17 +262,54 @@ impl WriteBackendMethods for GccCodegenBackend {
259
262
}
260
263
}
261
264
265
+ #[ cfg( not( feature="master" ) ) ]
266
+ #[ derive( Debug ) ]
267
+ pub struct TargetInfo {
268
+ supports_128bit_integers : bool ,
269
+ }
270
+
271
+ #[ cfg( not( feature="master" ) ) ]
272
+ impl TargetInfo {
273
+ fn cpu_supports ( & self , _feature : & str ) -> bool {
274
+ false
275
+ }
276
+
277
+ fn supports_128bit_int ( & self ) -> bool {
278
+ self . supports_128bit_integers
279
+ }
280
+ }
281
+
262
282
/// This is the entrypoint for a hot plugged rustc_codegen_gccjit
263
283
#[ no_mangle]
264
284
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 ( ) ) ;
285
+ #[ cfg( feature="master" ) ]
286
+ let target_info = {
287
+ // Get the native arch and check whether the target supports 128-bit integers.
288
+ let context = Context :: default ( ) ;
289
+ let arch = context. get_target_info ( ) . arch ( ) . unwrap ( ) ;
290
+
291
+ // Get the second TargetInfo with the correct CPU features by setting the arch.
292
+ let context = Context :: default ( ) ;
293
+ context. add_driver_option ( & format ! ( "-march={}" , arch. to_str( ) . unwrap( ) ) ) ;
294
+ Arc :: new ( context. get_target_info ( ) )
295
+ } ;
296
+
297
+ #[ cfg( not( feature="master" ) ) ]
298
+ let target_info = {
299
+ use gccjit:: CType ;
300
+ use tempfile:: TempDir ;
301
+
302
+ let temp_dir = TempDir :: new ( ) . expect ( "cannot create temporary directory" ) ;
303
+ let temp_file = temp_dir. into_path ( ) . join ( "result.asm" ) ;
304
+ let check_context = Context :: default ( ) ;
305
+ check_context. set_print_errors_to_stderr ( false ) ;
306
+ let _int128_ty = check_context. new_c_type ( CType :: UInt128t ) ;
307
+ // NOTE: we cannot just call compile() as this would require other files than libgccjit.so.
308
+ check_context. compile_to_file ( gccjit:: OutputKind :: Assembler , temp_file. to_str ( ) . expect ( "path to str" ) ) ;
309
+ Arc :: new ( TargetInfo {
310
+ supports_128bit_integers : check_context. get_last_error ( ) == Ok ( None ) ,
311
+ } )
312
+ } ;
273
313
274
314
Box :: new ( GccCodegenBackend {
275
315
target_info,
@@ -314,15 +354,8 @@ pub fn target_features(sess: &Session, allow_unstable: bool, target_info: &Arc<T
314
354
if sess. is_nightly_build ( ) || allow_unstable || gate. is_none ( ) { Some ( feature) } else { None }
315
355
} ,
316
356
)
317
- . filter ( |_feature| {
318
- #[ cfg( feature="master" ) ]
319
- {
320
- target_info. cpu_supports ( _feature)
321
- }
322
- #[ cfg( not( feature="master" ) ) ]
323
- {
324
- false
325
- }
357
+ . filter ( |feature| {
358
+ target_info. cpu_supports ( feature)
326
359
/*
327
360
adx, aes, avx, avx2, avx512bf16, avx512bitalg, avx512bw, avx512cd, avx512dq, avx512er, avx512f, avx512ifma,
328
361
avx512pf, avx512vbmi, avx512vbmi2, avx512vl, avx512vnni, avx512vp2intersect, avx512vpopcntdq,
0 commit comments