Skip to content

Commit dfcfaa4

Browse files
committed
Do not pass through features without +/- prefix
LLVM really dislikes this and will assert, saying something along the lines of: ``` rustc: llvm/lib/MC/MCSubtargetInfo.cpp:60: void ApplyFeatureFlag( llvm::FeatureBitset&, llvm::StringRef, llvm::ArrayRef<llvm::SubtargetFeatureKV> ): Assertion `SubtargetFeatures::hasFlag(Feature) && "Feature flags should start with '+' or '-'"` failed. ```
1 parent 3b1fe7e commit dfcfaa4

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

compiler/rustc_codegen_llvm/src/llvm_util.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,11 @@ pub fn target_features(sess: &Session) -> Vec<Symbol> {
221221
supported_target_features(sess)
222222
.iter()
223223
.filter_map(|&(feature, gate)| {
224-
if sess.is_nightly_build() || gate.is_none() { Some(feature) } else { None }
224+
if sess.is_nightly_build() || gate.is_none() {
225+
Some(feature)
226+
} else {
227+
None
228+
}
225229
})
226230
.filter(|feature| {
227231
for llvm_feature in to_llvm_feature(sess, feature) {
@@ -428,20 +432,15 @@ pub fn llvm_global_features(sess: &Session) -> Vec<String> {
428432
}
429433

430434
let filter = |s: &str| {
431-
if s.is_empty() {
432-
return vec![];
433-
}
434-
let feature = strip(s);
435-
if feature == s {
436-
return vec![s.to_string()];
437-
}
438-
439-
// Rustc-specific feature requests like `+crt-static` or `-crt-static`
440-
// are not passed down to LLVM.
441-
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
442-
return vec![];
443-
}
444-
// ... otherwise though we run through `to_llvm_feature` feature when
435+
// features must start with a `+` or `-`.
436+
let feature = match s.strip_prefix(&['+', '-'][..]) {
437+
None => return vec![],
438+
// Rustc-specific feature requests like `+crt-static` or `-crt-static`
439+
// are not passed down to LLVM.
440+
Some(feature) if RUSTC_SPECIFIC_FEATURES.contains(&feature) => return vec![],
441+
Some(feature) => feature,
442+
};
443+
// ... otherwise though we run through `to_llvm_feature` when
445444
// passing requests down to LLVM. This means that all in-language
446445
// features also work on the command line instead of having two
447446
// different names when the LLVM name and the Rust name differ.
@@ -458,11 +457,11 @@ pub fn llvm_global_features(sess: &Session) -> Vec<String> {
458457
check_tied_features(sess, &feats.iter().map(|f| (strip(f), !f.starts_with("-"))).collect())
459458
{
460459
sess.err(&format!(
461-
"Target features {} must all be enabled or disabled together",
460+
"target features {} must all be enabled or disabled together",
462461
f.join(", ")
463462
));
464463
}
465-
features.extend(feats.iter().flat_map(|&f| filter(f)));
464+
features.extend(feats.iter().flat_map(&filter));
466465
features
467466
}
468467

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// compile-flags: -Ctarget-feature=rdrand --crate-type=rlib --target=x86_64-unknown-linux-gnu
2+
// build-pass
3+
// needs-llvm-components: x86
4+
5+
#![feature(no_core)]
6+
#![no_core]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
warning: unknown feature specified for `-Ctarget-feature`: `rdrand`
2+
|
3+
= note: features must begin with a `+` to enable or `-` to disable it
4+
5+
warning: 1 warning emitted
6+

0 commit comments

Comments
 (0)