Skip to content

Commit 8515e12

Browse files
committed
Remove global_backend_features query
It's now possible to get the backend features (a `Vec<String>`) when the codegen backend is started, pass it back through `CodegenBackendInit`, and just store it in the `Session`. This removes the need for the query. Also: - `WriteBackendMethods::target_machine_factory` no longer needs the `target_features` parameter, because it's now available through the `sess` parameter. - `CodegenContext` no longer needs the `backend_features` field because we can use `sess.global_backend_features` instead. - `CodegenBackend::provide` is now a no-op for all the in-tree backends. I haven't removed it because out-of-tree backends still rely on it.
1 parent 81210c9 commit 8515e12

15 files changed

Lines changed: 52 additions & 70 deletions

File tree

‎compiler/rustc_codegen_cranelift/src/driver/aot.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ impl WriteBackendMethods for AotDriver {
319319
&self,
320320
_sess: &Session,
321321
_opt_level: OptLevel,
322-
_target_features: &[String],
323322
) -> TargetMachineFactoryFn<Self> {
324323
Arc::new(|_, _| ())
325324
}

‎compiler/rustc_codegen_cranelift/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
153153
}
154154

155155
CodegenBackendInit {
156+
global_backend_features: vec![],
156157
replaced_intrinsics: vec![],
157158
fallback_intrinsics: vec![sym::type_id_eq],
158159
thin_lto_supported: false,

‎compiler/rustc_codegen_gcc/src/attributes.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn from_fn_attrs<'gcc, 'tcx>(
133133

134134
// FIXME(antoyo): cg_llvm adds global features to each function so that LTO keep them.
135135
// Check if GCC requires the same.
136-
let mut global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str());
136+
let mut global_features = cx.tcx.sess.global_backend_features.iter().map(|s| s.as_str());
137137
function_features.extend(&mut global_features);
138138
let target_features = function_features
139139
.iter()

‎compiler/rustc_codegen_gcc/src/lib.rs‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ use rustc_data_structures::sync::IntoDynSyncSend;
9191
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
9292
use rustc_middle::dep_graph::{WorkProduct, WorkProductMap};
9393
use rustc_middle::ty::TyCtxt;
94-
use rustc_middle::util::Providers;
9594
use rustc_session::config::{OptLevel, OutputFilenames};
9695
use rustc_session::{CodegenBackendInit, EarlySession, IncrCompSession, Session};
9796
use rustc_span::{Symbol, sym};
@@ -188,6 +187,8 @@ impl CodegenBackend for GccCodegenBackend {
188187
.join("libgccjit.so")
189188
}
190189

190+
let global_backend_features = gcc_util::global_gcc_features(sess);
191+
191192
// We use all_paths() instead of only path() in case the path specified by --sysroot is
192193
// invalid.
193194
// This is the case for instance in Rust for Linux where they specify --sysroot=/dev/null.
@@ -250,17 +251,13 @@ impl CodegenBackend for GccCodegenBackend {
250251
}
251252

252253
CodegenBackendInit {
254+
global_backend_features,
253255
replaced_intrinsics: vec![],
254256
fallback_intrinsics: vec![sym::type_id_eq],
255257
thin_lto_supported: false,
256258
}
257259
}
258260

259-
fn provide(&self, providers: &mut Providers) {
260-
providers.queries.global_backend_features =
261-
|tcx, ()| gcc_util::global_gcc_features(tcx.sess)
262-
}
263-
264261
fn target_cpu(&self, sess: &Session) -> String {
265262
target_cpu(sess).to_owned()
266263
}
@@ -397,7 +394,6 @@ impl WriteBackendMethods for GccCodegenBackend {
397394
&self,
398395
_sess: &Session,
399396
_opt_level: OptLevel,
400-
_features: &[String],
401397
) -> TargetMachineFactoryFn<Self> {
402398
// FIXME(antoyo): set opt level.
403399
Arc::new(|_, _| ())

‎compiler/rustc_codegen_llvm/src/asm.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl<'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
502502
}
503503

504504
// Globally-enabled features that are already in the backend format.
505-
let global_features = self.tcx.global_backend_features(()).iter().map(String::as_str);
505+
let global_features = self.tcx.sess.global_backend_features.iter().map(String::as_str);
506506

507507
// Features enabled on a particular instance, in the rust format.
508508
// These need to be translated to the LLVM format.

‎compiler/rustc_codegen_llvm/src/attributes.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub(crate) fn sanitize_attrs<'ll, 'tcx>(
174174
}
175175
if enabled.contains(SanitizerSet::MEMTAG) {
176176
// Check to make sure the mte target feature is actually enabled.
177-
let features = tcx.global_backend_features(());
177+
let features = &tcx.sess.global_backend_features;
178178
let mte_feature =
179179
features.iter().map(|s| &s[..]).rfind(|n| ["+mte", "-mte"].contains(&&n[..]));
180180
if let None | Some("-mte") = mte_feature {
@@ -441,7 +441,7 @@ pub(crate) fn target_features_attr<'ll, 'tcx>(
441441
tcx: TyCtxt<'tcx>,
442442
function_features: Vec<String>,
443443
) -> Option<&'ll Attribute> {
444-
let global_features = tcx.global_backend_features(()).iter().map(String::as_str);
444+
let global_features = tcx.sess.global_backend_features.iter().map(String::as_str);
445445
let function_features = function_features.iter().map(String::as_str);
446446
let target_features =
447447
global_features.chain(function_features).intersperse(",").collect::<String>();

‎compiler/rustc_codegen_llvm/src/back/write.rs‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,7 @@ fn write_output_file<'ll>(
102102

103103
pub(crate) fn create_informational_target_machine(sess: &Session) -> OwnedTargetMachine {
104104
let config = TargetMachineFactoryConfig { split_dwarf_file: None, output_obj_file: None };
105-
// Can't use query system here quite yet because this function is invoked before the query
106-
// system/tcx is set up.
107-
let features = llvm_util::global_llvm_features(sess, /* for_cfg */ false);
108-
109-
target_machine_factory(sess, config::OptLevel::No, &features)(sess.dcx(), config)
105+
target_machine_factory(sess, config::OptLevel::No)(sess.dcx(), config)
110106
}
111107

112108
pub(crate) fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> OwnedTargetMachine {
@@ -124,11 +120,7 @@ pub(crate) fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> OwnedTar
124120
Some(tcx.output_filenames(()).temp_path_for_cgu(OutputType::Object, mod_name));
125121
let config = TargetMachineFactoryConfig { split_dwarf_file, output_obj_file };
126122

127-
target_machine_factory(
128-
tcx.sess,
129-
tcx.backend_optimization_level(()),
130-
tcx.global_backend_features(()),
131-
)(tcx.dcx(), config)
123+
target_machine_factory(tcx.sess, tcx.backend_optimization_level(()))(tcx.dcx(), config)
132124
}
133125

134126
fn to_llvm_opt_settings(cfg: config::OptLevel) -> (llvm::CodeGenOptLevel, llvm::CodeGenOptSize) {
@@ -190,7 +182,6 @@ fn to_llvm_float_abi(float_abi: Option<FloatAbi>) -> llvm::FloatAbi {
190182
pub(crate) fn target_machine_factory(
191183
sess: &Session,
192184
optlvl: config::OptLevel,
193-
target_features: &[String],
194185
) -> TargetMachineFactoryFn<LlvmCodegenBackend> {
195186
// Self-profile timer for creating a _factory_.
196187
let _prof_timer = sess.prof.generic_activity("target_machine_factory");
@@ -211,7 +202,7 @@ pub(crate) fn target_machine_factory(
211202

212203
let triple = SmallCStr::new(&versioned_llvm_target(sess));
213204
let cpu = SmallCStr::new(llvm_util::target_cpu(sess));
214-
let features = CString::new(target_features.join(",")).unwrap();
205+
let features = CString::new(sess.global_backend_features.join(",")).unwrap();
215206
let abi = SmallCStr::new(sess.target.llvm_abiname.desc());
216207
let trap_unreachable =
217208
sess.opts.unstable_opts.trap_unreachable.unwrap_or(sess.target.trap_unreachable);

‎compiler/rustc_codegen_llvm/src/lib.rs‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use rustc_errors::{DiagCtxt, DiagCtxtHandle};
3737
use rustc_metadata::EncodedMetadata;
3838
use rustc_middle::dep_graph::{WorkProduct, WorkProductMap};
3939
use rustc_middle::ty::TyCtxt;
40-
use rustc_middle::util::Providers;
4140
use rustc_session::config::{OptLevel, OutputFilenames, PrintKind, PrintRequest};
4241
use rustc_session::{CodegenBackendInit, EarlySession, IncrCompSession, Session};
4342
use rustc_span::{Symbol, sym};
@@ -131,9 +130,8 @@ impl WriteBackendMethods for LlvmCodegenBackend {
131130
&self,
132131
sess: &Session,
133132
optlvl: OptLevel,
134-
target_features: &[String],
135133
) -> TargetMachineFactoryFn<Self> {
136-
back::write::target_machine_factory(sess, optlvl, target_features)
134+
back::write::target_machine_factory(sess, optlvl)
137135
}
138136
fn optimize_and_codegen_fat_lto(
139137
sess: &Session,
@@ -223,6 +221,9 @@ impl CodegenBackend for LlvmCodegenBackend {
223221
fn init(&mut self, sess: &EarlySession) -> CodegenBackendInit {
224222
llvm_util::init(sess); // Make sure llvm is inited
225223

224+
let global_backend_features =
225+
llvm_util::global_llvm_features(sess, /* for_cfg */ false);
226+
226227
// autodiff is based on Enzyme, a library which we might not have available, when it was
227228
// neither build, nor downloaded via rustup. If autodiff is used, but not available we emit
228229
// an early error here and abort compilation.
@@ -289,12 +290,12 @@ impl CodegenBackend for LlvmCodegenBackend {
289290
// cranelift/GCC even if they have dedicated implementations.
290291
let fallback_intrinsics = vec![sym::type_id_eq];
291292

292-
CodegenBackendInit { replaced_intrinsics, fallback_intrinsics, thin_lto_supported: true }
293-
}
294-
295-
fn provide(&self, providers: &mut Providers) {
296-
providers.queries.global_backend_features =
297-
|tcx, ()| llvm_util::global_llvm_features(tcx.sess, /* for_cfg */ false)
293+
CodegenBackendInit {
294+
global_backend_features,
295+
replaced_intrinsics,
296+
fallback_intrinsics,
297+
thin_lto_supported: true,
298+
}
298299
}
299300

300301
fn print(&self, req: &PrintRequest, out: &mut String, sess: &Session) {

‎compiler/rustc_codegen_ssa/src/back/write.rs‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ pub struct CodegenContext {
335335
pub output_filenames: Arc<OutputFilenames>,
336336
pub module_config: Arc<ModuleConfig>,
337337
pub opt_level: OptLevel,
338-
pub backend_features: Vec<String>,
339338
pub msvc_imps_needed: bool,
340339
pub is_pe_coff: bool,
341340
pub target_can_use_split_dwarf: bool,
@@ -1272,8 +1271,7 @@ fn start_executing_work<B: WriteBackendMethods>(
12721271
});
12731272

12741273
let opt_level = tcx.backend_optimization_level(());
1275-
let backend_features = tcx.global_backend_features(()).clone();
1276-
let tm_factory = backend.target_machine_factory(tcx.sess, opt_level, &backend_features);
1274+
let tm_factory = backend.target_machine_factory(tcx.sess, opt_level);
12771275

12781276
let remark_dir = if let Some(ref dir) = sess.opts.unstable_opts.remark_dir {
12791277
let result = fs::create_dir_all(dir).and_then(|_| dir.canonicalize());
@@ -1303,7 +1301,6 @@ fn start_executing_work<B: WriteBackendMethods>(
13031301
output_filenames: Arc::clone(tcx.output_filenames(())),
13041302
module_config: regular_config,
13051303
opt_level,
1306-
backend_features,
13071304
msvc_imps_needed: msvc_imps_needed(tcx),
13081305
is_pe_coff: tcx.sess.target.is_like_windows,
13091306
target_can_use_split_dwarf: tcx.sess.target_can_use_split_dwarf(),
@@ -2164,11 +2161,7 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
21642161
compiled_modules
21652162
}
21662163
MaybeLtoModules::FatLto { cgcx, needs_fat_lto } => {
2167-
let tm_factory = self.backend.target_machine_factory(
2168-
sess,
2169-
cgcx.opt_level,
2170-
&cgcx.backend_features,
2171-
);
2164+
let tm_factory = self.backend.target_machine_factory(sess, cgcx.opt_level);
21722165

21732166
CompiledModules {
21742167
modules: vec![do_fat_lto(
@@ -2184,11 +2177,7 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
21842177
}
21852178
}
21862179
MaybeLtoModules::ThinLto { cgcx, needs_thin_lto } => {
2187-
let tm_factory = self.backend.target_machine_factory(
2188-
sess,
2189-
cgcx.opt_level,
2190-
&cgcx.backend_features,
2191-
);
2180+
let tm_factory = self.backend.target_machine_factory(sess, cgcx.opt_level);
21922181

21932182
CompiledModules {
21942183
modules: do_thin_lto::<B>(

‎compiler/rustc_codegen_ssa/src/base.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ impl CrateInfo {
10581058
let n_crates = crates.len();
10591059
let mut info = CrateInfo {
10601060
target_cpu,
1061-
target_features: tcx.global_backend_features(()).clone(),
1061+
target_features: tcx.sess.global_backend_features.clone(),
10621062
crate_types,
10631063
exported_symbols,
10641064
linked_symbols,

0 commit comments

Comments
 (0)