@@ -32,7 +32,6 @@ extern crate rustc_target;
3232extern crate rustc_driver;
3333
3434use std:: any:: Any ;
35- use std:: cell:: OnceCell ;
3635use std:: env;
3736use std:: sync:: Arc ;
3837
@@ -44,7 +43,7 @@ use rustc_data_structures::unord::UnordSet;
4443use rustc_log:: tracing:: info;
4544use rustc_middle:: dep_graph:: WorkProductMap ;
4645use rustc_session:: config:: { NATIVE_CPU , OutputFilenames } ;
47- use rustc_session:: { IncrCompSession , Session } ;
46+ use rustc_session:: { CodegenBackendInit , EarlySession , IncrCompSession , Session } ;
4847use rustc_span:: { Symbol , sym} ;
4948use rustc_target:: spec:: { Arch , CfgAbi , Env , Os } ;
5049
@@ -119,43 +118,49 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
119118}
120119
121120pub struct CraneliftCodegenBackend {
122- pub config : OnceCell < BackendConfig > ,
121+ // Set by `init` if not already set. (E.g. by cg_clif.)
122+ pub config : Option < BackendConfig > ,
123123}
124124
125125impl CodegenBackend for CraneliftCodegenBackend {
126126 fn name ( & self ) -> & ' static str {
127127 "cranelift"
128128 }
129129
130- fn init ( & self , sess : & Session ) {
131- use rustc_session:: config:: { InstrumentCoverage , Lto } ;
132- match sess . lto ( ) {
133- Lto :: No | Lto :: ThinLocal => { }
134- Lto :: Thin | Lto :: Fat => {
135- sess. dcx ( ) . fatal ( "LTO is not supported by rustc_codegen_cranelift" ) ;
130+ fn init ( & mut self , sess : & EarlySession ) -> CodegenBackendInit {
131+ use rustc_session:: config:: { InstrumentCoverage , LtoCli } ;
132+
133+ match ( sess . target . requires_lto , sess . early_lto ( ) ) {
134+ ( true , _ ) | ( false , LtoCli :: Yes | LtoCli :: Fat | LtoCli :: NoParam | LtoCli :: Thin ) => {
135+ sess. dcx ( ) . fatal ( "LTO is not supported by rustc_codegen_cranelift" )
136136 }
137+ ( false , LtoCli :: Unspecified | LtoCli :: No ) => { }
137138 }
138139
139140 if sess. opts . cg . instrument_coverage ( ) != InstrumentCoverage :: No {
140141 sess. dcx ( )
141142 . fatal ( "`-Cinstrument-coverage` is LLVM specific and not supported by Cranelift" ) ;
142143 }
143144
144- let config = self . config . get_or_init ( || {
145+ // Set `config` if not already set.
146+ let config = self . config . get_or_insert_with ( || {
145147 BackendConfig :: from_opts ( & sess. opts . cg . llvm_args )
146148 . unwrap_or_else ( |err| sess. dcx ( ) . fatal ( err) )
147149 } ) ;
148150
149151 if config. jit_mode && !sess. opts . output_types . should_codegen ( ) {
150152 sess. dcx ( ) . fatal ( "JIT mode doesn't work with `cargo check`" ) ;
151153 }
152- }
153154
154- fn thin_lto_supported ( & self ) -> bool {
155- false
155+ CodegenBackendInit {
156+ global_backend_features : vec ! [ ] ,
157+ replaced_intrinsics : vec ! [ ] ,
158+ fallback_intrinsics : vec ! [ sym:: type_id_eq] ,
159+ thin_lto_supported : false ,
160+ }
156161 }
157162
158- fn target_config ( & self , sess : & Session ) -> TargetConfig {
163+ fn target_config ( & self , sess : & EarlySession ) -> TargetConfig {
159164 // FIXME return the actually used target features. this is necessary for #[cfg(target_feature)]
160165 let target_features = match sess. target . arch {
161166 Arch :: X86_64 if sess. target . os != Os :: None => {
@@ -216,7 +221,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
216221
217222 fn codegen_crate ( & self , tcx : TyCtxt < ' _ > ) -> Box < dyn Any > {
218223 info ! ( "codegen crate {}" , tcx. crate_name( LOCAL_CRATE ) ) ;
219- let config = self . config . get ( ) . unwrap ( ) ;
224+ let config = self . config . as_ref ( ) . unwrap ( ) ;
220225 if config. jit_mode {
221226 #[ cfg( feature = "jit" ) ]
222227 driver:: jit:: run_jit ( tcx, self . target_cpu ( tcx. sess ) , config. jit_args . clone ( ) ) ;
@@ -241,10 +246,6 @@ impl CodegenBackend for CraneliftCodegenBackend {
241246 . unwrap ( )
242247 . join ( sess, incr_comp_session, crate_info)
243248 }
244-
245- fn fallback_intrinsics ( & self ) -> Vec < Symbol > {
246- vec ! [ sym:: type_id_eq]
247- }
248249}
249250
250251/// Determine if the Cranelift ir verifier should run.
@@ -376,5 +377,5 @@ fn build_isa(sess: &Session, jit: bool) -> Arc<dyn TargetIsa + 'static> {
376377/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
377378#[ unsafe( no_mangle) ]
378379pub fn __rustc_codegen_backend ( ) -> Box < dyn CodegenBackend > {
379- Box :: new ( CraneliftCodegenBackend { config : OnceCell :: new ( ) } )
380+ Box :: new ( CraneliftCodegenBackend { config : None } )
380381}
0 commit comments