1
Fork 0

Querify global_backend_features

At the very least this serves to deduplicate the diagnostics that are
output about unknown target features provided via CLI.
This commit is contained in:
Simonas Kazlauskas 2021-09-24 18:02:02 +03:00
parent c97c216efd
commit df701a292c
10 changed files with 58 additions and 48 deletions

View file

@ -132,7 +132,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
base::compile_codegen_unit(tcx, cgu_name) base::compile_codegen_unit(tcx, cgu_name)
} }
fn target_machine_factory(&self, _sess: &Session, _opt_level: OptLevel) -> TargetMachineFactoryFn<Self> { fn target_machine_factory(&self, _sess: &Session, _opt_level: OptLevel, _features: &[String]) -> TargetMachineFactoryFn<Self> {
// TODO(antoyo): set opt level. // TODO(antoyo): set opt level.
Arc::new(|_| { Arc::new(|_| {
Ok(()) Ok(())

View file

@ -79,13 +79,11 @@ pub fn sanitize_attrs<'ll>(
} }
if enabled.contains(SanitizerSet::MEMTAG) { if enabled.contains(SanitizerSet::MEMTAG) {
// Check to make sure the mte target feature is actually enabled. // Check to make sure the mte target feature is actually enabled.
let sess = cx.tcx.sess; let features = cx.tcx.global_backend_features(());
let features = llvm_util::llvm_global_features(sess).join(","); let mte_feature =
let mte_feature_enabled = features.rfind("+mte"); features.iter().map(|s| &s[..]).rfind(|n| ["+mte", "-mte"].contains(&&n[..]));
let mte_feature_disabled = features.rfind("-mte"); if let None | Some("-mte") = mte_feature {
cx.tcx.sess.err("`-Zsanitizer=memtag` requires `-Ctarget-feature=+mte`");
if mte_feature_enabled.is_none() || (mte_feature_disabled > mte_feature_enabled) {
sess.err("`-Zsanitizer=memtag` requires `-Ctarget-feature=+mte`");
} }
attrs.push(llvm::AttributeKind::SanitizeMemTag.create_attr(cx.llcx)); attrs.push(llvm::AttributeKind::SanitizeMemTag.create_attr(cx.llcx));
@ -415,10 +413,11 @@ pub fn from_fn_attrs<'ll, 'tcx>(
} }
if !function_features.is_empty() { if !function_features.is_empty() {
let mut global_features = llvm_util::llvm_global_features(cx.tcx.sess); let global_features = cx.tcx.global_backend_features(()).iter().map(|s| &s[..]);
global_features.extend(function_features.into_iter()); let val = global_features
let features = global_features.join(","); .chain(function_features.iter().map(|s| &s[..]))
let val = CString::new(features).unwrap(); .intersperse(",")
.collect::<SmallCStr>();
to_add.push(llvm::CreateAttrStringValue(cx.llcx, cstr!("target-features"), &val)); to_add.push(llvm::CreateAttrStringValue(cx.llcx, cstr!("target-features"), &val));
} }

View file

@ -100,7 +100,10 @@ pub fn write_output_file<'ll>(
pub fn create_informational_target_machine(sess: &Session) -> &'static mut llvm::TargetMachine { pub fn create_informational_target_machine(sess: &Session) -> &'static mut llvm::TargetMachine {
let config = TargetMachineFactoryConfig { split_dwarf_file: None }; let config = TargetMachineFactoryConfig { split_dwarf_file: None };
target_machine_factory(sess, config::OptLevel::No)(config) // Can't use query system here quite yet because this function is invoked before the query
// system/tcx is set up.
let features = llvm_util::global_llvm_features(sess, false);
target_machine_factory(sess, config::OptLevel::No, &features)(config)
.unwrap_or_else(|err| llvm_err(sess.diagnostic(), &err).raise()) .unwrap_or_else(|err| llvm_err(sess.diagnostic(), &err).raise())
} }
@ -115,8 +118,12 @@ pub fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> &'static mut ll
None None
}; };
let config = TargetMachineFactoryConfig { split_dwarf_file }; let config = TargetMachineFactoryConfig { split_dwarf_file };
target_machine_factory(tcx.sess, tcx.backend_optimization_level(()))(config) target_machine_factory(
.unwrap_or_else(|err| llvm_err(tcx.sess.diagnostic(), &err).raise()) &tcx.sess,
tcx.backend_optimization_level(()),
tcx.global_backend_features(()),
)(config)
.unwrap_or_else(|err| llvm_err(tcx.sess.diagnostic(), &err).raise())
} }
pub fn to_llvm_opt_settings( pub fn to_llvm_opt_settings(
@ -171,6 +178,7 @@ pub(crate) fn to_llvm_code_model(code_model: Option<CodeModel>) -> llvm::CodeMod
pub fn target_machine_factory( pub fn target_machine_factory(
sess: &Session, sess: &Session,
optlvl: config::OptLevel, optlvl: config::OptLevel,
target_features: &[String],
) -> TargetMachineFactoryFn<LlvmCodegenBackend> { ) -> TargetMachineFactoryFn<LlvmCodegenBackend> {
let reloc_model = to_llvm_relocation_model(sess.relocation_model()); let reloc_model = to_llvm_relocation_model(sess.relocation_model());
@ -195,8 +203,7 @@ pub fn target_machine_factory(
let triple = SmallCStr::new(&sess.target.llvm_target); let triple = SmallCStr::new(&sess.target.llvm_target);
let cpu = SmallCStr::new(llvm_util::target_cpu(sess)); let cpu = SmallCStr::new(llvm_util::target_cpu(sess));
let features = llvm_util::llvm_global_features(sess).join(","); let features = CString::new(target_features.join(",")).unwrap();
let features = CString::new(features).unwrap();
let abi = SmallCStr::new(&sess.target.llvm_abiname); let abi = SmallCStr::new(&sess.target.llvm_abiname);
let trap_unreachable = let trap_unreachable =
sess.opts.debugging_opts.trap_unreachable.unwrap_or(sess.target.trap_unreachable); sess.opts.debugging_opts.trap_unreachable.unwrap_or(sess.target.trap_unreachable);

View file

@ -11,6 +11,7 @@
#![feature(extern_types)] #![feature(extern_types)]
#![feature(once_cell)] #![feature(once_cell)]
#![feature(nll)] #![feature(nll)]
#![feature(iter_intersperse)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
#![allow(rustc::potential_query_instability)] #![allow(rustc::potential_query_instability)]
@ -32,6 +33,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{ErrorReported, FatalError, Handler}; use rustc_errors::{ErrorReported, FatalError, Handler};
use rustc_metadata::EncodedMetadata; use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::config::{OptLevel, OutputFilenames, PrintRequest}; use rustc_session::config::{OptLevel, OutputFilenames, PrintRequest};
use rustc_session::Session; use rustc_session::Session;
@ -126,8 +128,9 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
&self, &self,
sess: &Session, sess: &Session,
optlvl: OptLevel, optlvl: OptLevel,
target_features: &[String],
) -> TargetMachineFactoryFn<Self> { ) -> TargetMachineFactoryFn<Self> {
back::write::target_machine_factory(sess, optlvl) back::write::target_machine_factory(sess, optlvl, target_features)
} }
fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str { fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str {
llvm_util::target_cpu(sess) llvm_util::target_cpu(sess)
@ -251,6 +254,11 @@ impl CodegenBackend for LlvmCodegenBackend {
llvm_util::init(sess); // Make sure llvm is inited llvm_util::init(sess); // Make sure llvm is inited
} }
fn provide(&self, providers: &mut Providers) {
providers.global_backend_features =
|tcx, ()| llvm_util::global_llvm_features(tcx.sess, true)
}
fn print(&self, req: PrintRequest, sess: &Session) { fn print(&self, req: PrintRequest, sess: &Session) {
match req { match req {
PrintRequest::RelocationModels => { PrintRequest::RelocationModels => {

View file

@ -1033,6 +1033,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
} else { } else {
tcx.backend_optimization_level(()) tcx.backend_optimization_level(())
}; };
let backend_features = tcx.global_backend_features(());
let cgcx = CodegenContext::<B> { let cgcx = CodegenContext::<B> {
backend: backend.clone(), backend: backend.clone(),
crate_types: sess.crate_types().to_vec(), crate_types: sess.crate_types().to_vec(),
@ -1054,7 +1055,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
regular_module_config: regular_config, regular_module_config: regular_config,
metadata_module_config: metadata_config, metadata_module_config: metadata_config,
allocator_module_config: allocator_config, allocator_module_config: allocator_config,
tm_factory: backend.target_machine_factory(tcx.sess, ol), tm_factory: backend.target_machine_factory(tcx.sess, ol, backend_features),
total_cgus, total_cgus,
msvc_imps_needed: msvc_imps_needed(tcx), msvc_imps_needed: msvc_imps_needed(tcx),
is_pe_coff: tcx.sess.target.is_like_windows, is_pe_coff: tcx.sess.target.is_like_windows,

View file

@ -134,6 +134,7 @@ pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Se
&self, &self,
sess: &Session, sess: &Session,
opt_level: config::OptLevel, opt_level: config::OptLevel,
target_features: &[String],
) -> TargetMachineFactoryFn<Self>; ) -> TargetMachineFactoryFn<Self>;
fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str; fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str;
fn tune_cpu<'b>(&self, sess: &'b Session) -> Option<&'b str>; fn tune_cpu<'b>(&self, sess: &'b Session) -> Option<&'b str>;

View file

@ -66,3 +66,15 @@ impl Deref for SmallCStr {
self.as_c_str() self.as_c_str()
} }
} }
impl<'a> FromIterator<&'a str> for SmallCStr {
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
let mut data =
iter.into_iter().flat_map(|s| s.as_bytes()).copied().collect::<SmallVec<_>>();
data.push(0);
if let Err(e) = ffi::CStr::from_bytes_with_nul(&data) {
panic!("The iterator {:?} cannot be converted into a CStr: {}", data, e);
}
Self { data }
}
}

View file

@ -1944,4 +1944,13 @@ rustc_queries! {
no_hash no_hash
desc { "performing HIR wf-checking for predicate {:?} at item {:?}", key.0, key.1 } desc { "performing HIR wf-checking for predicate {:?} at item {:?}", key.0, key.1 }
} }
/// The list of backend features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
/// `--target` and similar).
query global_backend_features(_: ()) -> Vec<String> {
storage(ArenaCacheSelector<'tcx>)
eval_always
desc { "computing the backend features for CLI flags" }
}
} }

View file

@ -2,17 +2,5 @@ warning: unknown feature specified for `-Ctarget-feature`: `banana`
| |
= note: features must begin with a `+` to enable or `-` to disable it = note: features must begin with a `+` to enable or `-` to disable it
warning: unknown feature specified for `-Ctarget-feature`: `banana` warning: 1 warning emitted
|
= note: features must begin with a `+` to enable or `-` to disable it
warning: unknown feature specified for `-Ctarget-feature`: `banana`
|
= note: features must begin with a `+` to enable or `-` to disable it
warning: unknown feature specified for `-Ctarget-feature`: `banana`
|
= note: features must begin with a `+` to enable or `-` to disable it
warning: 4 warnings emitted

View file

@ -3,20 +3,5 @@ warning: unknown feature specified for `-Ctarget-feature`: `rdrnd`
= note: it is still passed through to the codegen backend = note: it is still passed through to the codegen backend
= help: you might have meant: `rdrand` = help: you might have meant: `rdrand`
warning: unknown feature specified for `-Ctarget-feature`: `rdrnd` warning: 1 warning emitted
|
= note: it is still passed through to the codegen backend
= help: did you mean: `rdrand`
warning: unknown feature specified for `-Ctarget-feature`: `rdrnd`
|
= note: it is still passed through to the codegen backend
= help: did you mean: `rdrand`
warning: unknown feature specified for `-Ctarget-feature`: `rdrnd`
|
= note: it is still passed through to the codegen backend
= help: did you mean: `rdrand`
warning: 4 warnings emitted