Skip to content

Commit

Permalink
Persist target features used for codegen beyond tcx
Browse files Browse the repository at this point in the history
Bitcode linkers like llvm-bitcode-linker or bpf linker hand over the target features to llvm during link stage. During link stage the `TyCtxt` is already gone so it is not possible to create a query for the global backend features any longer. The features preserved in `Session.target_features` only incorporate target features known to rustc. This would contradict with the behaviour during codegen stage which also passes target features to llvm which are unknown to rustc.
This commit adds target features as a field to the `CrateInfo` struct and queries the target features in its new function. This way the target features are preserved beyond tcx and available at link stage.
To make sure the `global_backend_features` query is always registered even if the CodegenBackend does not register it, this registration is added to the `provide`function of the `rustc_codegen_ssa` crate.
  • Loading branch information
kulst committed Feb 9, 2025
1 parent 820bfff commit 1492e51
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ impl CrateInfo {
let n_crates = crates.len();
let mut info = CrateInfo {
target_cpu,
target_features: tcx.global_backend_features(()).clone(),
crate_types,
exported_symbols,
linked_symbols,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ impl From<&cstore::NativeLib> for NativeLib {
#[derive(Debug, Encodable, Decodable)]
pub struct CrateInfo {
pub target_cpu: String,
pub target_features: Vec<String>,
pub crate_types: Vec<CrateType>,
pub exported_symbols: UnordMap<CrateType, Vec<String>>,
pub linked_symbols: FxIndexMap<CrateType, Vec<(String, SymbolExportKind)>>,
Expand Down Expand Up @@ -230,6 +231,7 @@ pub fn provide(providers: &mut Providers) {
crate::base::provide(providers);
crate::target_features::provide(providers);
crate::codegen_attrs::provide(providers);
crate::traits::provide(providers);
}

/// Checks if the given filename ends with the `.rcgu.o` extension that `rustc`
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_codegen_ssa/src/traits/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,10 @@ pub trait ExtraBackendMethods:
std::thread::Builder::new().name(name).spawn(f)
}
}

pub(crate) fn provide(providers: &mut rustc_middle::query::Providers) {
*providers = rustc_middle::query::Providers {
global_backend_features: |_tcx: TyCtxt<'_>, ()| vec![],
..*providers
};
}
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub use self::abi::AbiBuilderMethods;
pub use self::asm::{
AsmBuilderMethods, AsmCodegenMethods, GlobalAsmOperandRef, InlineAsmOperandRef,
};
pub(crate) use self::backend::provide;
pub use self::backend::{BackendTypes, CodegenBackend, ExtraBackendMethods};
pub use self::builder::{BuilderMethods, OverflowOp};
pub use self::consts::ConstCodegenMethods;
Expand Down

0 comments on commit 1492e51

Please sign in to comment.