Auto merge of #87402 - nagisa:nagisa/request-feature-requests-for-features, r=estebank
Direct users towards using Rust target feature names in CLI This PR consists of a couple of changes on how we handle target features. In particular there is a bug-fix wherein we avoid passing through features that aren't prefixed by `+` or `-` to LLVM. These appear to be causing LLVM to assert, which is pretty poor a behaviour (and also makes it pretty clear we expect feature names to be prefixed). The other commit, I anticipate to be somewhat more controversial is outputting a warning when users specify a LLVM-specific, or otherwise unknown, feature name on the CLI. In those situations we request users to either replace it with a known Rust feature name (e.g. `bmi` -> `bmi1`) or file a feature request. I've a couple motivations for this: first of all, if users are specifying these features on the command line, I'm pretty confident there is also a need for these features to be usable via `#[cfg(target_feature)]` machinery. And second, we're growing a fair number of backends recently and having ability to provide some sort of unified-ish interface in this place seems pretty useful to me. Sponsored by: standard.ai
This commit is contained in:
commit
39a3b52767
@ -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(())
|
||||||
|
@ -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));
|
||||||
@ -382,10 +380,7 @@ pub fn from_fn_attrs<'ll, 'tcx>(
|
|||||||
let mut function_features = function_features
|
let mut function_features = function_features
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|feat| {
|
.flat_map(|feat| {
|
||||||
llvm_util::to_llvm_feature(cx.tcx.sess, feat)
|
llvm_util::to_llvm_features(cx.tcx.sess, feat).into_iter().map(|f| format!("+{}", f))
|
||||||
.into_iter()
|
|
||||||
.map(|f| format!("+{}", f))
|
|
||||||
.collect::<Vec<String>>()
|
|
||||||
})
|
})
|
||||||
.chain(codegen_fn_attrs.instruction_set.iter().map(|x| match x {
|
.chain(codegen_fn_attrs.instruction_set.iter().map(|x| match x {
|
||||||
InstructionSetAttr::ArmA32 => "-thumb-mode".to_string(),
|
InstructionSetAttr::ArmA32 => "-thumb-mode".to_string(),
|
||||||
@ -418,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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
@ -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 => {
|
||||||
|
@ -2,14 +2,18 @@ use crate::back::write::create_informational_target_machine;
|
|||||||
use crate::{llvm, llvm_util};
|
use crate::{llvm, llvm_util};
|
||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
use libloading::Library;
|
use libloading::Library;
|
||||||
use rustc_codegen_ssa::target_features::{supported_target_features, tied_target_features};
|
use rustc_codegen_ssa::target_features::{
|
||||||
|
supported_target_features, tied_target_features, RUSTC_SPECIFIC_FEATURES,
|
||||||
|
};
|
||||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
||||||
|
use rustc_data_structures::small_c_str::SmallCStr;
|
||||||
use rustc_fs_util::path_to_c_string;
|
use rustc_fs_util::path_to_c_string;
|
||||||
use rustc_middle::bug;
|
use rustc_middle::bug;
|
||||||
use rustc_session::config::PrintRequest;
|
use rustc_session::config::PrintRequest;
|
||||||
use rustc_session::Session;
|
use rustc_session::Session;
|
||||||
use rustc_span::symbol::Symbol;
|
use rustc_span::symbol::Symbol;
|
||||||
use rustc_target::spec::{MergeFunctions, PanicStrategy};
|
use rustc_target::spec::{MergeFunctions, PanicStrategy};
|
||||||
|
use smallvec::{smallvec, SmallVec};
|
||||||
use std::ffi::{CStr, CString};
|
use std::ffi::{CStr, CString};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
@ -155,9 +159,10 @@ pub fn time_trace_profiler_finish(file_name: &Path) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WARNING: the features after applying `to_llvm_feature` must be known
|
// WARNING: the features after applying `to_llvm_features` must be known
|
||||||
// to LLVM or the feature detection code will walk past the end of the feature
|
// to LLVM or the feature detection code will walk past the end of the feature
|
||||||
// array, leading to crashes.
|
// array, leading to crashes.
|
||||||
|
//
|
||||||
// To find a list of LLVM's names, check llvm-project/llvm/include/llvm/Support/*TargetParser.def
|
// To find a list of LLVM's names, check llvm-project/llvm/include/llvm/Support/*TargetParser.def
|
||||||
// where the * matches the architecture's name
|
// where the * matches the architecture's name
|
||||||
// Beware to not use the llvm github project for this, but check the git submodule
|
// Beware to not use the llvm github project for this, but check the git submodule
|
||||||
@ -165,35 +170,35 @@ pub fn time_trace_profiler_finish(file_name: &Path) {
|
|||||||
// Though note that Rust can also be build with an external precompiled version of LLVM
|
// Though note that Rust can also be build with an external precompiled version of LLVM
|
||||||
// which might lead to failures if the oldest tested / supported LLVM version
|
// which might lead to failures if the oldest tested / supported LLVM version
|
||||||
// doesn't yet support the relevant intrinsics
|
// doesn't yet support the relevant intrinsics
|
||||||
pub fn to_llvm_feature<'a>(sess: &Session, s: &'a str) -> Vec<&'a str> {
|
pub fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> SmallVec<[&'a str; 2]> {
|
||||||
let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch };
|
let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch };
|
||||||
match (arch, s) {
|
match (arch, s) {
|
||||||
("x86", "sse4.2") => {
|
("x86", "sse4.2") => {
|
||||||
if get_version() >= (14, 0, 0) {
|
if get_version() >= (14, 0, 0) {
|
||||||
vec!["sse4.2", "crc32"]
|
smallvec!["sse4.2", "crc32"]
|
||||||
} else {
|
} else {
|
||||||
vec!["sse4.2"]
|
smallvec!["sse4.2"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
("x86", "pclmulqdq") => vec!["pclmul"],
|
("x86", "pclmulqdq") => smallvec!["pclmul"],
|
||||||
("x86", "rdrand") => vec!["rdrnd"],
|
("x86", "rdrand") => smallvec!["rdrnd"],
|
||||||
("x86", "bmi1") => vec!["bmi"],
|
("x86", "bmi1") => smallvec!["bmi"],
|
||||||
("x86", "cmpxchg16b") => vec!["cx16"],
|
("x86", "cmpxchg16b") => smallvec!["cx16"],
|
||||||
("x86", "avx512vaes") => vec!["vaes"],
|
("x86", "avx512vaes") => smallvec!["vaes"],
|
||||||
("x86", "avx512gfni") => vec!["gfni"],
|
("x86", "avx512gfni") => smallvec!["gfni"],
|
||||||
("x86", "avx512vpclmulqdq") => vec!["vpclmulqdq"],
|
("x86", "avx512vpclmulqdq") => smallvec!["vpclmulqdq"],
|
||||||
("aarch64", "fp") => vec!["fp-armv8"],
|
("aarch64", "fp") => smallvec!["fp-armv8"],
|
||||||
("aarch64", "fp16") => vec!["fullfp16"],
|
("aarch64", "fp16") => smallvec!["fullfp16"],
|
||||||
("aarch64", "fhm") => vec!["fp16fml"],
|
("aarch64", "fhm") => smallvec!["fp16fml"],
|
||||||
("aarch64", "rcpc2") => vec!["rcpc-immo"],
|
("aarch64", "rcpc2") => smallvec!["rcpc-immo"],
|
||||||
("aarch64", "dpb") => vec!["ccpp"],
|
("aarch64", "dpb") => smallvec!["ccpp"],
|
||||||
("aarch64", "dpb2") => vec!["ccdp"],
|
("aarch64", "dpb2") => smallvec!["ccdp"],
|
||||||
("aarch64", "frintts") => vec!["fptoint"],
|
("aarch64", "frintts") => smallvec!["fptoint"],
|
||||||
("aarch64", "fcma") => vec!["complxnum"],
|
("aarch64", "fcma") => smallvec!["complxnum"],
|
||||||
("aarch64", "pmuv3") => vec!["perfmon"],
|
("aarch64", "pmuv3") => smallvec!["perfmon"],
|
||||||
("aarch64", "paca") => vec!["pauth"],
|
("aarch64", "paca") => smallvec!["pauth"],
|
||||||
("aarch64", "pacg") => vec!["pauth"],
|
("aarch64", "pacg") => smallvec!["pauth"],
|
||||||
(_, s) => vec![s],
|
(_, s) => smallvec![s],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,7 +212,6 @@ pub fn check_tied_features(
|
|||||||
// Tied features must be set to the same value, or not set at all
|
// Tied features must be set to the same value, or not set at all
|
||||||
let mut tied_iter = tied.iter();
|
let mut tied_iter = tied.iter();
|
||||||
let enabled = features.get(tied_iter.next().unwrap());
|
let enabled = features.get(tied_iter.next().unwrap());
|
||||||
|
|
||||||
if tied_iter.any(|f| enabled != features.get(f)) {
|
if tied_iter.any(|f| enabled != features.get(f)) {
|
||||||
return Some(tied);
|
return Some(tied);
|
||||||
}
|
}
|
||||||
@ -224,8 +228,8 @@ pub fn target_features(sess: &Session) -> Vec<Symbol> {
|
|||||||
if sess.is_nightly_build() || gate.is_none() { Some(feature) } else { None }
|
if sess.is_nightly_build() || gate.is_none() { Some(feature) } else { None }
|
||||||
})
|
})
|
||||||
.filter(|feature| {
|
.filter(|feature| {
|
||||||
for llvm_feature in to_llvm_feature(sess, feature) {
|
for llvm_feature in to_llvm_features(sess, feature) {
|
||||||
let cstr = CString::new(llvm_feature).unwrap();
|
let cstr = SmallCStr::new(llvm_feature);
|
||||||
if unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) } {
|
if unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) } {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -292,9 +296,9 @@ fn print_target_features(sess: &Session, tm: &llvm::TargetMachine) {
|
|||||||
let mut rustc_target_features = supported_target_features(sess)
|
let mut rustc_target_features = supported_target_features(sess)
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|(feature, _gate)| {
|
.filter_map(|(feature, _gate)| {
|
||||||
for llvm_feature in to_llvm_feature(sess, *feature) {
|
for llvm_feature in to_llvm_features(sess, *feature) {
|
||||||
// LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these strings.
|
// LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these strings.
|
||||||
match target_features.binary_search_by_key(&llvm_feature, |(f, _d)| (*f)).ok().map(
|
match target_features.binary_search_by_key(&llvm_feature, |(f, _d)| f).ok().map(
|
||||||
|index| {
|
|index| {
|
||||||
let (_f, desc) = target_features.remove(index);
|
let (_f, desc) = target_features.remove(index);
|
||||||
(*feature, desc)
|
(*feature, desc)
|
||||||
@ -364,14 +368,7 @@ pub fn target_cpu(sess: &Session) -> &str {
|
|||||||
|
|
||||||
/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
|
/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
|
||||||
/// `--target` and similar).
|
/// `--target` and similar).
|
||||||
// FIXME(nagisa): Cache the output of this somehow? Maybe make this a query? We're calling this
|
pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<String> {
|
||||||
// for every function that has `#[target_feature]` on it. The global features won't change between
|
|
||||||
// the functions; only crates, maybe…
|
|
||||||
pub fn llvm_global_features(sess: &Session) -> Vec<String> {
|
|
||||||
// FIXME(nagisa): this should definitely be available more centrally and to other codegen backends.
|
|
||||||
/// These features control behaviour of rustc rather than llvm.
|
|
||||||
const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
|
|
||||||
|
|
||||||
// Features that come earlier are overriden by conflicting features later in the string.
|
// Features that come earlier are overriden by conflicting features later in the string.
|
||||||
// Typically we'll want more explicit settings to override the implicit ones, so:
|
// Typically we'll want more explicit settings to override the implicit ones, so:
|
||||||
//
|
//
|
||||||
@ -417,49 +414,110 @@ pub fn llvm_global_features(sess: &Session) -> Vec<String> {
|
|||||||
Some(_) | None => {}
|
Some(_) | None => {}
|
||||||
};
|
};
|
||||||
|
|
||||||
fn strip(s: &str) -> &str {
|
// Features implied by an implicit or explicit `--target`.
|
||||||
s.strip_prefix(&['+', '-']).unwrap_or(s)
|
features.extend(
|
||||||
|
sess.target
|
||||||
|
.features
|
||||||
|
.split(',')
|
||||||
|
.filter(|v| !v.is_empty() && backend_feature_name(v).is_some())
|
||||||
|
.map(String::from),
|
||||||
|
);
|
||||||
|
|
||||||
|
// -Ctarget-features
|
||||||
|
let supported_features = supported_target_features(sess);
|
||||||
|
let feats = sess
|
||||||
|
.opts
|
||||||
|
.cg
|
||||||
|
.target_feature
|
||||||
|
.split(',')
|
||||||
|
.filter_map(|s| {
|
||||||
|
let enable_disable = match s.chars().next() {
|
||||||
|
None => return None,
|
||||||
|
Some(c @ '+' | c @ '-') => c,
|
||||||
|
Some(_) => {
|
||||||
|
if diagnostics {
|
||||||
|
let mut diag = sess.struct_warn(&format!(
|
||||||
|
"unknown feature specified for `-Ctarget-feature`: `{}`",
|
||||||
|
s
|
||||||
|
));
|
||||||
|
diag.note("features must begin with a `+` to enable or `-` to disable it");
|
||||||
|
diag.emit();
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let feature = backend_feature_name(s)?;
|
||||||
|
// Warn against use of LLVM specific feature names on the CLI.
|
||||||
|
if diagnostics && !supported_features.iter().any(|&(v, _)| v == feature) {
|
||||||
|
let rust_feature = supported_features.iter().find_map(|&(rust_feature, _)| {
|
||||||
|
let llvm_features = to_llvm_features(sess, rust_feature);
|
||||||
|
if llvm_features.contains(&feature) && !llvm_features.contains(&rust_feature) {
|
||||||
|
Some(rust_feature)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let mut diag = sess.struct_warn(&format!(
|
||||||
|
"unknown feature specified for `-Ctarget-feature`: `{}`",
|
||||||
|
feature
|
||||||
|
));
|
||||||
|
diag.note("it is still passed through to the codegen backend");
|
||||||
|
if let Some(rust_feature) = rust_feature {
|
||||||
|
diag.help(&format!("you might have meant: `{}`", rust_feature));
|
||||||
|
} else {
|
||||||
|
diag.note("consider filing a feature request");
|
||||||
|
}
|
||||||
|
diag.emit();
|
||||||
|
}
|
||||||
|
Some((enable_disable, feature))
|
||||||
|
})
|
||||||
|
.collect::<SmallVec<[(char, &str); 8]>>();
|
||||||
|
|
||||||
|
if diagnostics {
|
||||||
|
// FIXME(nagisa): figure out how to not allocate a full hashset here.
|
||||||
|
let featmap = feats.iter().map(|&(flag, feat)| (feat, flag == '+')).collect();
|
||||||
|
if let Some(f) = check_tied_features(sess, &featmap) {
|
||||||
|
sess.err(&format!(
|
||||||
|
"target features {} must all be enabled or disabled together",
|
||||||
|
f.join(", ")
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let filter = |s: &str| {
|
features.extend(feats.into_iter().flat_map(|(enable_disable, feature)| {
|
||||||
if s.is_empty() {
|
// rustc-specific features do not get passed down to LLVM…
|
||||||
return vec![];
|
|
||||||
}
|
|
||||||
let feature = strip(s);
|
|
||||||
if feature == s {
|
|
||||||
return vec![s.to_string()];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rustc-specific feature requests like `+crt-static` or `-crt-static`
|
|
||||||
// are not passed down to LLVM.
|
|
||||||
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
|
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
|
||||||
return vec![];
|
return SmallVec::<[_; 2]>::new();
|
||||||
}
|
}
|
||||||
// ... otherwise though we run through `to_llvm_feature` feature when
|
// ... otherwise though we run through `to_llvm_feature when
|
||||||
// passing requests down to LLVM. This means that all in-language
|
// passing requests down to LLVM. This means that all in-language
|
||||||
// features also work on the command line instead of having two
|
// features also work on the command line instead of having two
|
||||||
// different names when the LLVM name and the Rust name differ.
|
// different names when the LLVM name and the Rust name differ.
|
||||||
to_llvm_feature(sess, feature).iter().map(|f| format!("{}{}", &s[..1], f)).collect()
|
to_llvm_features(sess, feature)
|
||||||
};
|
.into_iter()
|
||||||
|
.map(|f| format!("{}{}", enable_disable, f))
|
||||||
// Features implied by an implicit or explicit `--target`.
|
.collect()
|
||||||
features.extend(sess.target.features.split(',').flat_map(&filter));
|
}));
|
||||||
|
|
||||||
// -Ctarget-features
|
|
||||||
let feats: Vec<&str> = sess.opts.cg.target_feature.split(',').collect();
|
|
||||||
// LLVM enables based on the last occurence of a feature
|
|
||||||
if let Some(f) =
|
|
||||||
check_tied_features(sess, &feats.iter().map(|f| (strip(f), !f.starts_with("-"))).collect())
|
|
||||||
{
|
|
||||||
sess.err(&format!(
|
|
||||||
"Target features {} must all be enabled or disabled together",
|
|
||||||
f.join(", ")
|
|
||||||
));
|
|
||||||
}
|
|
||||||
features.extend(feats.iter().flat_map(|&f| filter(f)));
|
|
||||||
features
|
features
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a feature name for the given `+feature` or `-feature` string.
|
||||||
|
///
|
||||||
|
/// Only allows features that are backend specific (i.e. not [`RUSTC_SPECIFIC_FEATURES`].)
|
||||||
|
fn backend_feature_name(s: &str) -> Option<&str> {
|
||||||
|
// features must start with a `+` or `-`.
|
||||||
|
let feature = s.strip_prefix(&['+', '-'][..]).unwrap_or_else(|| {
|
||||||
|
bug!("target feature `{}` must begin with a `+` or `-`", s);
|
||||||
|
});
|
||||||
|
// Rustc-specific feature requests like `+crt-static` or `-crt-static`
|
||||||
|
// are not passed down to LLVM.
|
||||||
|
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(feature)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn tune_cpu(sess: &Session) -> Option<&str> {
|
pub fn tune_cpu(sess: &Session) -> Option<&str> {
|
||||||
let name = sess.opts.debugging_opts.tune_cpu.as_ref()?;
|
let name = sess.opts.debugging_opts.tune_cpu.as_ref()?;
|
||||||
Some(handle_native(name))
|
Some(handle_native(name))
|
||||||
|
@ -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,
|
||||||
|
@ -4,6 +4,9 @@ use rustc_session::Session;
|
|||||||
use rustc_span::symbol::sym;
|
use rustc_span::symbol::sym;
|
||||||
use rustc_span::symbol::Symbol;
|
use rustc_span::symbol::Symbol;
|
||||||
|
|
||||||
|
/// Features that control behaviour of rustc, rather than the codegen.
|
||||||
|
pub const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
|
||||||
|
|
||||||
// When adding features to the below lists
|
// When adding features to the below lists
|
||||||
// check whether they're named already elsewhere in rust
|
// check whether they're named already elsewhere in rust
|
||||||
// e.g. in stdarch and whether the given name matches LLVM's
|
// e.g. in stdarch and whether the given name matches LLVM's
|
||||||
|
@ -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>;
|
||||||
|
@ -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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
6
src/test/ui/target-feature/missing-plusminus-2.rs
Normal file
6
src/test/ui/target-feature/missing-plusminus-2.rs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// compile-flags: -Ctarget-feature=rdrand --crate-type=rlib --target=x86_64-unknown-linux-gnu
|
||||||
|
// build-pass
|
||||||
|
// needs-llvm-components: x86
|
||||||
|
|
||||||
|
#![feature(no_core)]
|
||||||
|
#![no_core]
|
6
src/test/ui/target-feature/missing-plusminus-2.stderr
Normal file
6
src/test/ui/target-feature/missing-plusminus-2.stderr
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
warning: unknown feature specified for `-Ctarget-feature`: `rdrand`
|
||||||
|
|
|
||||||
|
= note: features must begin with a `+` to enable or `-` to disable it
|
||||||
|
|
||||||
|
warning: 1 warning emitted
|
||||||
|
|
2
src/test/ui/target-feature/missing-plusminus.rs
Normal file
2
src/test/ui/target-feature/missing-plusminus.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
// compile-flags: -Ctarget-feature=banana --crate-type=rlib
|
||||||
|
// build-pass
|
6
src/test/ui/target-feature/missing-plusminus.stderr
Normal file
6
src/test/ui/target-feature/missing-plusminus.stderr
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
warning: unknown feature specified for `-Ctarget-feature`: `banana`
|
||||||
|
|
|
||||||
|
= note: features must begin with a `+` to enable or `-` to disable it
|
||||||
|
|
||||||
|
warning: 1 warning emitted
|
||||||
|
|
6
src/test/ui/target-feature/similar-feature-suggestion.rs
Normal file
6
src/test/ui/target-feature/similar-feature-suggestion.rs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// compile-flags: -Ctarget-feature=+rdrnd --crate-type=rlib --target=x86_64-unknown-linux-gnu
|
||||||
|
// build-pass
|
||||||
|
// needs-llvm-components: x86
|
||||||
|
|
||||||
|
#![feature(no_core)]
|
||||||
|
#![no_core]
|
@ -0,0 +1,7 @@
|
|||||||
|
warning: unknown feature specified for `-Ctarget-feature`: `rdrnd`
|
||||||
|
|
|
||||||
|
= note: it is still passed through to the codegen backend
|
||||||
|
= help: you might have meant: `rdrand`
|
||||||
|
|
||||||
|
warning: 1 warning emitted
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
error: Target features paca, pacg must all be enabled or disabled together
|
error: target features paca, pacg must all be enabled or disabled together
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -1,9 +1,20 @@
|
|||||||
// only-aarch64
|
// revisions: one two three
|
||||||
// revisions: one two three four
|
// compile-flags: --crate-type=rlib --target=aarch64-unknown-linux-gnu
|
||||||
//[one] compile-flags: -C target-feature=+paca
|
// needs-llvm-components: aarch64
|
||||||
//[two] compile-flags: -C target-feature=-pacg,+pacg
|
//
|
||||||
//[three] compile-flags: -C target-feature=+paca,+pacg,-paca
|
//
|
||||||
//[four] check-pass
|
// [one] check-fail
|
||||||
//[four] compile-flags: -C target-feature=-paca,+pacg -C target-feature=+paca
|
// [one] compile-flags: -C target-feature=+paca
|
||||||
|
// [two] check-fail
|
||||||
|
// [two] compile-flags: -C target-feature=-pacg,+pacg
|
||||||
|
// [three] check-fail
|
||||||
|
// [three] compile-flags: -C target-feature=+paca,+pacg,-paca
|
||||||
|
// [four] build-pass
|
||||||
|
// [four] compile-flags: -C target-feature=-paca,+pacg -C target-feature=+paca
|
||||||
|
#![feature(no_core, lang_items)]
|
||||||
|
#![no_core]
|
||||||
|
|
||||||
|
#[lang="sized"]
|
||||||
|
trait Sized {}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
error: Target features paca, pacg must all be enabled or disabled together
|
error: target features paca, pacg must all be enabled or disabled together
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
error: Target features paca, pacg must all be enabled or disabled together
|
error: target features paca, pacg must all be enabled or disabled together
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -1,9 +1,15 @@
|
|||||||
// only-aarch64
|
|
||||||
// build-fail
|
// build-fail
|
||||||
|
// compile-flags: --crate-type=rlib --target=aarch64-unknown-linux-gnu
|
||||||
|
// needs-llvm-components: aarch64
|
||||||
#![feature(aarch64_target_feature, target_feature_11)]
|
#![feature(aarch64_target_feature, target_feature_11)]
|
||||||
|
#![feature(no_core, lang_items)]
|
||||||
|
#![no_core]
|
||||||
|
|
||||||
fn main() {
|
#[lang="sized"]
|
||||||
|
trait Sized {}
|
||||||
|
|
||||||
|
// FIXME: this should not need to be public.
|
||||||
|
pub fn main() {
|
||||||
#[target_feature(enable = "pacg")]
|
#[target_feature(enable = "pacg")]
|
||||||
//~^ ERROR must all be either enabled or disabled together
|
//~^ ERROR must all be either enabled or disabled together
|
||||||
unsafe fn inner() {}
|
unsafe fn inner() {}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
error: the target features paca, pacg must all be either enabled or disabled together
|
error: the target features paca, pacg must all be either enabled or disabled together
|
||||||
--> $DIR/tied-features.rs:7:5
|
--> $DIR/tied-features.rs:13:5
|
||||||
|
|
|
|
||||||
LL | #[target_feature(enable = "pacg")]
|
LL | #[target_feature(enable = "pacg")]
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
@ -7,7 +7,7 @@ LL | #[target_feature(enable = "pacg")]
|
|||||||
= help: add the missing features in a `target_feature` attribute
|
= help: add the missing features in a `target_feature` attribute
|
||||||
|
|
||||||
error: the target features paca, pacg must all be either enabled or disabled together
|
error: the target features paca, pacg must all be either enabled or disabled together
|
||||||
--> $DIR/tied-features.rs:19:1
|
--> $DIR/tied-features.rs:25:1
|
||||||
|
|
|
|
||||||
LL | #[target_feature(enable = "paca")]
|
LL | #[target_feature(enable = "paca")]
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
Loading…
x
Reference in New Issue
Block a user