Skip to content

Commit c7e9029

Browse files
authored
Rollup merge of #78361 - DevJPM:master, r=workingjubilee
Updated the list of white-listed target features for x86 This PR both adds in-source documentation on what to look out for when adding a new (X86) feature set and [adds all that are detectable at run-time in Rust stable as of 1.27.0](https://github.com/rust-lang/stdarch/blob/master/crates/std_detect/src/detect/arch/x86.rs). This should only enable the use of the corresponding LLVM intrinsics. Actual intrinsics need to be added separately in rust-lang/stdarch. It also re-orders the run-time-detect test statements to be more consistent with the actual list of intrinsics whitelisted and removes underscores not present in the actual names (which might be mistaken as being part of the name) The reference for LLVM's feature names used is [this file](https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/Support/X86TargetParser.def). This PR was motivated as the compiler end's part for allowing #67329 to be adressed over on rust-lang/stdarch
2 parents 7d747db + 72b83af commit c7e9029

File tree

3 files changed

+68
-25
lines changed

3 files changed

+68
-25
lines changed

compiler/rustc_codegen_llvm/src/llvm_util.rs

+10
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,23 @@ pub fn time_trace_profiler_finish(file_name: &str) {
129129
// WARNING: the features after applying `to_llvm_feature` must be known
130130
// to LLVM or the feature detection code will walk past the end of the feature
131131
// array, leading to crashes.
132+
// To find a list of LLVM's names, check llvm-project/llvm/include/llvm/Support/*TargetParser.def
133+
// where the * matches the architecture's name
134+
// Beware to not use the llvm github project for this, but check the git submodule
135+
// found in src/llvm-project
136+
// Though note that Rust can also be build with an external precompiled version of LLVM
137+
// which might lead to failures if the oldest tested / supported LLVM version
138+
// doesn't yet support the relevant intrinsics
132139
pub fn to_llvm_feature<'a>(sess: &Session, s: &'a str) -> &'a str {
133140
let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch };
134141
match (arch, s) {
135142
("x86", "pclmulqdq") => "pclmul",
136143
("x86", "rdrand") => "rdrnd",
137144
("x86", "bmi1") => "bmi",
138145
("x86", "cmpxchg16b") => "cx16",
146+
("x86", "avx512vaes") => "vaes",
147+
("x86", "avx512gfni") => "gfni",
148+
("x86", "avx512vpclmulqdq") => "vpclmulqdq",
139149
("aarch64", "fp") => "fp-armv8",
140150
("aarch64", "fp16") => "fullfp16",
141151
(_, s) => s,

compiler/rustc_codegen_ssa/src/target_features.rs

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ use rustc_session::Session;
44
use rustc_span::symbol::sym;
55
use rustc_span::symbol::Symbol;
66

7+
// When adding features to the below lists
8+
// check whether they're named already elsewhere in rust
9+
// e.g. in stdarch and whether the given name matches LLVM's
10+
// if it doesn't, to_llvm_feature in llvm_util in rustc_codegen_llvm needs to be adapted
11+
712
const ARM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
813
("aclass", Some(sym::arm_target_feature)),
914
("mclass", Some(sym::arm_target_feature)),
@@ -50,15 +55,23 @@ const X86_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
5055
("aes", None),
5156
("avx", None),
5257
("avx2", None),
58+
("avx512bf16", Some(sym::avx512_target_feature)),
59+
("avx512bitalg", Some(sym::avx512_target_feature)),
5360
("avx512bw", Some(sym::avx512_target_feature)),
5461
("avx512cd", Some(sym::avx512_target_feature)),
5562
("avx512dq", Some(sym::avx512_target_feature)),
5663
("avx512er", Some(sym::avx512_target_feature)),
5764
("avx512f", Some(sym::avx512_target_feature)),
65+
("avx512gfni", Some(sym::avx512_target_feature)),
5866
("avx512ifma", Some(sym::avx512_target_feature)),
5967
("avx512pf", Some(sym::avx512_target_feature)),
68+
("avx512vaes", Some(sym::avx512_target_feature)),
6069
("avx512vbmi", Some(sym::avx512_target_feature)),
70+
("avx512vbmi2", Some(sym::avx512_target_feature)),
6171
("avx512vl", Some(sym::avx512_target_feature)),
72+
("avx512vnni", Some(sym::avx512_target_feature)),
73+
("avx512vp2intersect", Some(sym::avx512_target_feature)),
74+
("avx512vpclmulqdq", Some(sym::avx512_target_feature)),
6275
("avx512vpopcntdq", Some(sym::avx512_target_feature)),
6376
("bmi1", None),
6477
("bmi2", None),

library/std/tests/run-time-detect.rs

+45-25
Original file line numberDiff line numberDiff line change
@@ -54,42 +54,62 @@ fn powerpc64_linux() {
5454
#[test]
5555
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
5656
fn x86_all() {
57+
// the below is the set of features we can test at runtime, but don't actually
58+
// use to gate anything and are thus not part of the X86_ALLOWED_FEATURES list
59+
60+
println!("abm: {:?}", is_x86_feature_detected!("abm")); // this is a synonym for lzcnt but we test it anyways
61+
println!("mmx: {:?}", is_x86_feature_detected!("mmx"));
62+
println!("tsc: {:?}", is_x86_feature_detected!("tsc"));
63+
64+
// the below is in alphabetical order and matches
65+
// the order of X86_ALLOWED_FEATURES in rustc_codegen_ssa's target_features.rs
66+
67+
println!("adx: {:?}", is_x86_feature_detected!("adx"));
5768
println!("aes: {:?}", is_x86_feature_detected!("aes"));
58-
println!("pcmulqdq: {:?}", is_x86_feature_detected!("pclmulqdq"));
69+
println!("avx: {:?}", is_x86_feature_detected!("avx"));
70+
println!("avx2: {:?}", is_x86_feature_detected!("avx2"));
71+
println!("avx512bf16: {:?}", is_x86_feature_detected!("avx512bf16"));
72+
println!("avx512bitalg: {:?}", is_x86_feature_detected!("avx512bitalg"));
73+
println!("avx512bw: {:?}", is_x86_feature_detected!("avx512bw"));
74+
println!("avx512cd: {:?}", is_x86_feature_detected!("avx512cd"));
75+
println!("avx512dq: {:?}", is_x86_feature_detected!("avx512dq"));
76+
println!("avx512er: {:?}", is_x86_feature_detected!("avx512er"));
77+
println!("avx512f: {:?}", is_x86_feature_detected!("avx512f"));
78+
println!("avx512gfni: {:?}", is_x86_feature_detected!("avx512gfni"));
79+
println!("avx512ifma: {:?}", is_x86_feature_detected!("avx512ifma"));
80+
println!("avx512pf: {:?}", is_x86_feature_detected!("avx512pf"));
81+
println!("avx512vaes: {:?}", is_x86_feature_detected!("avx512vaes"));
82+
println!("avx512vbmi: {:?}", is_x86_feature_detected!("avx512vbmi"));
83+
println!("avx512vbmi2: {:?}", is_x86_feature_detected!("avx512vbmi2"));
84+
println!("avx512vl: {:?}", is_x86_feature_detected!("avx512vl"));
85+
println!("avx512vnni: {:?}", is_x86_feature_detected!("avx512vnni"));
86+
println!("avx512vp2intersect: {:?}", is_x86_feature_detected!("avx512vp2intersect"));
87+
println!("avx512vpclmulqdq: {:?}", is_x86_feature_detected!("avx512vpclmulqdq"));
88+
println!("avx512vpopcntdq: {:?}", is_x86_feature_detected!("avx512vpopcntdq"));
89+
println!("bmi1: {:?}", is_x86_feature_detected!("bmi1"));
90+
println!("bmi2: {:?}", is_x86_feature_detected!("bmi2"));
91+
println!("cmpxchg16b: {:?}", is_x86_feature_detected!("cmpxchg16b"));
92+
println!("f16c: {:?}", is_x86_feature_detected!("f16c"));
93+
println!("fma: {:?}", is_x86_feature_detected!("fma"));
94+
println!("fxsr: {:?}", is_x86_feature_detected!("fxsr"));
95+
println!("lzcnt: {:?}", is_x86_feature_detected!("lzcnt"));
96+
//println!("movbe: {:?}", is_x86_feature_detected!("movbe")); // movbe is unsupported as a target feature
97+
println!("pclmulqdq: {:?}", is_x86_feature_detected!("pclmulqdq"));
98+
println!("popcnt: {:?}", is_x86_feature_detected!("popcnt"));
5999
println!("rdrand: {:?}", is_x86_feature_detected!("rdrand"));
60100
println!("rdseed: {:?}", is_x86_feature_detected!("rdseed"));
61-
println!("tsc: {:?}", is_x86_feature_detected!("tsc"));
62-
println!("mmx: {:?}", is_x86_feature_detected!("mmx"));
101+
println!("rtm: {:?}", is_x86_feature_detected!("rtm"));
102+
println!("sha: {:?}", is_x86_feature_detected!("sha"));
63103
println!("sse: {:?}", is_x86_feature_detected!("sse"));
64104
println!("sse2: {:?}", is_x86_feature_detected!("sse2"));
65105
println!("sse3: {:?}", is_x86_feature_detected!("sse3"));
66-
println!("ssse3: {:?}", is_x86_feature_detected!("ssse3"));
67106
println!("sse4.1: {:?}", is_x86_feature_detected!("sse4.1"));
68107
println!("sse4.2: {:?}", is_x86_feature_detected!("sse4.2"));
69108
println!("sse4a: {:?}", is_x86_feature_detected!("sse4a"));
70-
println!("sha: {:?}", is_x86_feature_detected!("sha"));
71-
println!("avx: {:?}", is_x86_feature_detected!("avx"));
72-
println!("avx2: {:?}", is_x86_feature_detected!("avx2"));
73-
println!("avx512f {:?}", is_x86_feature_detected!("avx512f"));
74-
println!("avx512cd {:?}", is_x86_feature_detected!("avx512cd"));
75-
println!("avx512er {:?}", is_x86_feature_detected!("avx512er"));
76-
println!("avx512pf {:?}", is_x86_feature_detected!("avx512pf"));
77-
println!("avx512bw {:?}", is_x86_feature_detected!("avx512bw"));
78-
println!("avx512dq {:?}", is_x86_feature_detected!("avx512dq"));
79-
println!("avx512vl {:?}", is_x86_feature_detected!("avx512vl"));
80-
println!("avx512_ifma {:?}", is_x86_feature_detected!("avx512ifma"));
81-
println!("avx512_vbmi {:?}", is_x86_feature_detected!("avx512vbmi"));
82-
println!("avx512_vpopcntdq {:?}", is_x86_feature_detected!("avx512vpopcntdq"));
83-
println!("fma: {:?}", is_x86_feature_detected!("fma"));
84-
println!("bmi1: {:?}", is_x86_feature_detected!("bmi1"));
85-
println!("bmi2: {:?}", is_x86_feature_detected!("bmi2"));
86-
println!("abm: {:?}", is_x86_feature_detected!("abm"));
87-
println!("lzcnt: {:?}", is_x86_feature_detected!("lzcnt"));
109+
println!("ssse3: {:?}", is_x86_feature_detected!("ssse3"));
88110
println!("tbm: {:?}", is_x86_feature_detected!("tbm"));
89-
println!("popcnt: {:?}", is_x86_feature_detected!("popcnt"));
90-
println!("fxsr: {:?}", is_x86_feature_detected!("fxsr"));
91111
println!("xsave: {:?}", is_x86_feature_detected!("xsave"));
112+
println!("xsavec: {:?}", is_x86_feature_detected!("xsavec"));
92113
println!("xsaveopt: {:?}", is_x86_feature_detected!("xsaveopt"));
93114
println!("xsaves: {:?}", is_x86_feature_detected!("xsaves"));
94-
println!("xsavec: {:?}", is_x86_feature_detected!("xsavec"));
95115
}

0 commit comments

Comments
 (0)