Skip to content

Commit 990c297

Browse files
workingjubileebjorn3nagisa
committed
Filter for all features instead of any
Adds regression tests for feature logic Co-authored-by: bjorn3 <[email protected]> Co-authored-by: Simonas Kazlauskas <[email protected]>
1 parent b807d59 commit 990c297

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ pub fn check_tied_features(
229229
None
230230
}
231231

232+
// Used to generate cfg variables and apply features
233+
// Must express features in the way Rust understands them
232234
pub fn target_features(sess: &Session) -> Vec<Symbol> {
233235
let target_machine = create_informational_target_machine(sess);
234236
let mut features: Vec<Symbol> =
@@ -238,13 +240,14 @@ pub fn target_features(sess: &Session) -> Vec<Symbol> {
238240
if sess.is_nightly_build() || gate.is_none() { Some(feature) } else { None }
239241
})
240242
.filter(|feature| {
243+
// check that all features in a given smallvec are enabled
241244
for llvm_feature in to_llvm_features(sess, feature) {
242245
let cstr = SmallCStr::new(llvm_feature);
243-
if unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) } {
244-
return true;
246+
if !unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) } {
247+
return false;
245248
}
246249
}
247-
false
250+
true
248251
})
249252
.map(|feature| Symbol::intern(feature))
250253
.collect();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// revisions: aarch64 x86-64
2+
// [aarch64] compile-flags: -Ctarget-feature=+neon,+fp16,+fhm --target=aarch64-unknown-linux-gnu
3+
// [aarch64] needs-llvm-components: aarch64
4+
// [x86-64] compile-flags: -Ctarget-feature=+sse4.2,+rdrand --target=x86_64-unknown-linux-gnu
5+
// [x86-64] needs-llvm-components: x86
6+
// build-pass
7+
#![no_core]
8+
#![crate_type = "rlib"]
9+
#![feature(intrinsics, rustc_attrs, no_core, lang_items, staged_api)]
10+
#![stable(feature = "test", since = "1.0.0")]
11+
12+
// Supporting minimal rust core code
13+
#[lang = "sized"]
14+
trait Sized {}
15+
#[lang = "copy"]
16+
trait Copy {}
17+
impl Copy for bool {}
18+
19+
extern "rust-intrinsic" {
20+
#[rustc_const_stable(feature = "test", since = "1.0.0")]
21+
fn unreachable() -> !;
22+
}
23+
24+
#[rustc_builtin_macro]
25+
macro_rules! cfg {
26+
($($cfg:tt)*) => {};
27+
}
28+
29+
// Test code
30+
const fn do_or_die(cond: bool) {
31+
if cond {
32+
} else {
33+
unsafe { unreachable() }
34+
}
35+
}
36+
37+
macro_rules! assert {
38+
($x:expr $(,)?) => {
39+
const _: () = do_or_die($x);
40+
};
41+
}
42+
43+
44+
#[cfg(target_arch = "aarch64")]
45+
fn check_aarch64() {
46+
// This checks that the rustc feature name is used, not the LLVM feature.
47+
assert!(cfg!(target_feature = "neon"));
48+
assert!(cfg!(not(target_feature = "fp-armv8")));
49+
assert!(cfg!(target_feature = "fhm"));
50+
assert!(cfg!(not(target_feature = "fp16fml")));
51+
assert!(cfg!(target_feature = "fp16"));
52+
assert!(cfg!(not(target_feature = "fullfp16")));
53+
}
54+
55+
#[cfg(target_arch = "x86_64")]
56+
fn check_x86_64() {
57+
// This checks that the rustc feature name is used, not the LLVM feature.
58+
assert!(cfg!(target_feature = "rdrand"));
59+
assert!(cfg!(not(target_feature = "rdrnd")));
60+
61+
// Likewise: We enable LLVM's crc32 feature with SSE4.2, but Rust says it's just SSE4.2
62+
assert!(cfg!(target_feature = "sse4.2"));
63+
assert!(cfg!(not(target_feature = "crc32")));
64+
}

0 commit comments

Comments
 (0)