@@ -64,9 +64,15 @@ mod type_of;
6464
6565use std:: any:: Any ;
6666use std:: sync:: Arc ;
67+ #[ cfg( not( feature="master" ) ) ]
68+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
6769
6870use crate :: errors:: LTONotSupported ;
69- use gccjit:: { Context , OptimizationLevel , TargetInfo } ;
71+ use gccjit:: { Context , OptimizationLevel } ;
72+ #[ cfg( feature="master" ) ]
73+ use gccjit:: TargetInfo ;
74+ #[ cfg( not( feature="master" ) ) ]
75+ use gccjit:: CType ;
7076use rustc_ast:: expand:: allocator:: AllocatorKind ;
7177use rustc_codegen_ssa:: { CodegenResults , CompiledModule , ModuleCodegen } ;
7278use rustc_codegen_ssa:: base:: codegen_crate;
@@ -85,6 +91,8 @@ use rustc_session::config::{Lto, OptLevel, OutputFilenames};
8591use rustc_session:: Session ;
8692use rustc_span:: Symbol ;
8793use rustc_span:: fatal_error:: FatalError ;
94+ #[ cfg( not( feature="master" ) ) ]
95+ use tempfile:: TempDir ;
8896
8997fluent_messages ! { "../messages.ftl" }
9098
@@ -98,6 +106,23 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
98106 }
99107}
100108
109+ #[ cfg( not( feature="master" ) ) ]
110+ #[ derive( Debug ) ]
111+ pub struct TargetInfo {
112+ supports_128bit_integers : AtomicBool ,
113+ }
114+
115+ #[ cfg( not( feature="master" ) ) ]
116+ impl TargetInfo {
117+ fn cpu_supports ( & self , _feature : & str ) -> bool {
118+ false
119+ }
120+
121+ fn supports_128bit_int ( & self ) -> bool {
122+ self . supports_128bit_integers . load ( Ordering :: SeqCst )
123+ }
124+ }
125+
101126#[ derive( Clone ) ]
102127pub struct GccCodegenBackend {
103128 target_info : Arc < TargetInfo > ,
@@ -114,6 +139,18 @@ impl CodegenBackend for GccCodegenBackend {
114139 if sess. lto ( ) != Lto :: No {
115140 sess. emit_warning ( LTONotSupported { } ) ;
116141 }
142+
143+ #[ cfg( not( feature="master" ) ) ]
144+ {
145+ let temp_dir = TempDir :: new ( ) . expect ( "cannot create temporary directory" ) ;
146+ let temp_file = temp_dir. into_path ( ) . join ( "result.asm" ) ;
147+ let check_context = Context :: default ( ) ;
148+ check_context. set_print_errors_to_stderr ( false ) ;
149+ let _int128_ty = check_context. new_c_type ( CType :: UInt128t ) ;
150+ // NOTE: we cannot just call compile() as this would require other files than libgccjit.so.
151+ check_context. compile_to_file ( gccjit:: OutputKind :: Assembler , temp_file. to_str ( ) . expect ( "path to str" ) ) ;
152+ self . target_info . supports_128bit_integers . store ( check_context. get_last_error ( ) == Ok ( None ) , Ordering :: SeqCst ) ;
153+ }
117154 }
118155
119156 fn provide ( & self , providers : & mut Providers ) {
@@ -266,14 +303,21 @@ impl WriteBackendMethods for GccCodegenBackend {
266303/// This is the entrypoint for a hot plugged rustc_codegen_gccjit
267304#[ no_mangle]
268305pub fn __rustc_codegen_backend ( ) -> Box < dyn CodegenBackend > {
269- // Get the native arch and check whether the target supports 128-bit integers.
270- let context = Context :: default ( ) ;
271- let arch = context. get_target_info ( ) . arch ( ) . unwrap ( ) ;
272-
273- // Get the second TargetInfo with the correct CPU features by setting the arch.
274- let context = Context :: default ( ) ;
275- context. add_driver_option ( & format ! ( "-march={}" , arch. to_str( ) . unwrap( ) ) ) ;
276- let target_info = Arc :: new ( context. get_target_info ( ) ) ;
306+ #[ cfg( feature="master" ) ]
307+ let target_info = {
308+ // Get the native arch and check whether the target supports 128-bit integers.
309+ let context = Context :: default ( ) ;
310+ let arch = context. get_target_info ( ) . arch ( ) . unwrap ( ) ;
311+
312+ // Get the second TargetInfo with the correct CPU features by setting the arch.
313+ let context = Context :: default ( ) ;
314+ context. add_driver_option ( & format ! ( "-march={}" , arch. to_str( ) . unwrap( ) ) ) ;
315+ Arc :: new ( context. get_target_info ( ) )
316+ } ;
317+ #[ cfg( not( feature="master" ) ) ]
318+ let target_info = Arc :: new ( TargetInfo {
319+ supports_128bit_integers : AtomicBool :: new ( false ) ,
320+ } ) ;
277321
278322 Box :: new ( GccCodegenBackend {
279323 target_info,
@@ -319,14 +363,7 @@ pub fn target_features(sess: &Session, allow_unstable: bool, target_info: &Arc<T
319363 } ,
320364 )
321365 . filter ( |_feature| {
322- #[ cfg( feature="master" ) ]
323- {
324- target_info. cpu_supports ( _feature)
325- }
326- #[ cfg( not( feature="master" ) ) ]
327- {
328- false
329- }
366+ target_info. cpu_supports ( _feature)
330367 /*
331368 adx, aes, avx, avx2, avx512bf16, avx512bitalg, avx512bw, avx512cd, avx512dq, avx512er, avx512f, avx512ifma,
332369 avx512pf, avx512vbmi, avx512vbmi2, avx512vl, avx512vnni, avx512vp2intersect, avx512vpopcntdq,
0 commit comments