]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/target_features.rs
Rollup merge of #89876 - AlexApps99:const_ops, r=oli-obk
[rust.git] / compiler / rustc_codegen_ssa / src / target_features.rs
1 use rustc_hir::def_id::LOCAL_CRATE;
2 use rustc_middle::ty::query::Providers;
3 use rustc_session::Session;
4 use rustc_span::symbol::sym;
5 use rustc_span::symbol::Symbol;
6
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
12 const ARM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
13     ("aclass", Some(sym::arm_target_feature)),
14     ("mclass", Some(sym::arm_target_feature)),
15     ("rclass", Some(sym::arm_target_feature)),
16     ("dsp", Some(sym::arm_target_feature)),
17     ("neon", Some(sym::arm_target_feature)),
18     ("crc", Some(sym::arm_target_feature)),
19     ("crypto", Some(sym::arm_target_feature)),
20     ("aes", Some(sym::arm_target_feature)),
21     ("sha2", Some(sym::arm_target_feature)),
22     ("i8mm", Some(sym::arm_target_feature)),
23     ("v5te", Some(sym::arm_target_feature)),
24     ("v6", Some(sym::arm_target_feature)),
25     ("v6k", Some(sym::arm_target_feature)),
26     ("v6t2", Some(sym::arm_target_feature)),
27     ("v7", Some(sym::arm_target_feature)),
28     ("v8", Some(sym::arm_target_feature)),
29     ("vfp2", Some(sym::arm_target_feature)),
30     ("vfp3", Some(sym::arm_target_feature)),
31     ("vfp4", Some(sym::arm_target_feature)),
32     ("fp-armv8", Some(sym::arm_target_feature)),
33     // This is needed for inline assembly, but shouldn't be stabilized as-is
34     // since it should be enabled per-function using #[instruction_set], not
35     // #[target_feature].
36     ("thumb-mode", Some(sym::arm_target_feature)),
37 ];
38
39 // Commented features are not available in LLVM 10.0, or have since been renamed
40 const AARCH64_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
41     // FEAT_AdvSimd
42     ("neon", Some(sym::aarch64_target_feature)),
43     // FEAT_FP
44     ("fp", Some(sym::aarch64_target_feature)),
45     // FEAT_FP16
46     ("fp16", Some(sym::aarch64_target_feature)),
47     // FEAT_SVE
48     ("sve", Some(sym::aarch64_target_feature)),
49     // FEAT_CRC
50     ("crc", Some(sym::aarch64_target_feature)),
51     // FEAT_RAS
52     ("ras", Some(sym::aarch64_target_feature)),
53     // FEAT_LSE
54     ("lse", Some(sym::aarch64_target_feature)),
55     // FEAT_RDM
56     ("rdm", Some(sym::aarch64_target_feature)),
57     // FEAT_RCPC
58     ("rcpc", Some(sym::aarch64_target_feature)),
59     // FEAT_RCPC2
60     ("rcpc2", Some(sym::aarch64_target_feature)),
61     // FEAT_DotProd
62     ("dotprod", Some(sym::aarch64_target_feature)),
63     // FEAT_TME
64     ("tme", Some(sym::aarch64_target_feature)),
65     // FEAT_FHM
66     ("fhm", Some(sym::aarch64_target_feature)),
67     // FEAT_DIT
68     ("dit", Some(sym::aarch64_target_feature)),
69     // FEAT_FLAGM
70     // ("flagm", Some(sym::aarch64_target_feature)),
71     // FEAT_SSBS
72     ("ssbs", Some(sym::aarch64_target_feature)),
73     // FEAT_SB
74     ("sb", Some(sym::aarch64_target_feature)),
75     // FEAT_PAUTH
76     // ("pauth", Some(sym::aarch64_target_feature)),
77     // FEAT_DPB
78     ("dpb", Some(sym::aarch64_target_feature)),
79     // FEAT_DPB2
80     ("dpb2", Some(sym::aarch64_target_feature)),
81     // FEAT_SVE2
82     ("sve2", Some(sym::aarch64_target_feature)),
83     // FEAT_SVE2_AES
84     ("sve2-aes", Some(sym::aarch64_target_feature)),
85     // FEAT_SVE2_SM4
86     ("sve2-sm4", Some(sym::aarch64_target_feature)),
87     // FEAT_SVE2_SHA3
88     ("sve2-sha3", Some(sym::aarch64_target_feature)),
89     // FEAT_SVE2_BitPerm
90     ("sve2-bitperm", Some(sym::aarch64_target_feature)),
91     // FEAT_FRINTTS
92     ("frintts", Some(sym::aarch64_target_feature)),
93     // FEAT_I8MM
94     ("i8mm", Some(sym::aarch64_target_feature)),
95     // FEAT_F32MM
96     // ("f32mm", Some(sym::aarch64_target_feature)),
97     // FEAT_F64MM
98     // ("f64mm", Some(sym::aarch64_target_feature)),
99     // FEAT_BF16
100     // ("bf16", Some(sym::aarch64_target_feature)),
101     // FEAT_RAND
102     ("rand", Some(sym::aarch64_target_feature)),
103     // FEAT_BTI
104     ("bti", Some(sym::aarch64_target_feature)),
105     // FEAT_MTE
106     ("mte", Some(sym::aarch64_target_feature)),
107     // FEAT_JSCVT
108     ("jsconv", Some(sym::aarch64_target_feature)),
109     // FEAT_FCMA
110     ("fcma", Some(sym::aarch64_target_feature)),
111     // FEAT_AES
112     ("aes", Some(sym::aarch64_target_feature)),
113     // FEAT_SHA1 & FEAT_SHA256
114     ("sha2", Some(sym::aarch64_target_feature)),
115     // FEAT_SHA512 & FEAT_SHA3
116     ("sha3", Some(sym::aarch64_target_feature)),
117     // FEAT_SM3 & FEAT_SM4
118     ("sm4", Some(sym::aarch64_target_feature)),
119     ("v8.1a", Some(sym::aarch64_target_feature)),
120     ("v8.2a", Some(sym::aarch64_target_feature)),
121     ("v8.3a", Some(sym::aarch64_target_feature)),
122     ("v8.4a", Some(sym::aarch64_target_feature)),
123     ("v8.5a", Some(sym::aarch64_target_feature)),
124     // ("v8.6a", Some(sym::aarch64_target_feature)),
125     // ("v8.7a", Some(sym::aarch64_target_feature)),
126 ];
127
128 const X86_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
129     ("adx", Some(sym::adx_target_feature)),
130     ("aes", None),
131     ("avx", None),
132     ("avx2", None),
133     ("avx512bf16", Some(sym::avx512_target_feature)),
134     ("avx512bitalg", Some(sym::avx512_target_feature)),
135     ("avx512bw", Some(sym::avx512_target_feature)),
136     ("avx512cd", Some(sym::avx512_target_feature)),
137     ("avx512dq", Some(sym::avx512_target_feature)),
138     ("avx512er", Some(sym::avx512_target_feature)),
139     ("avx512f", Some(sym::avx512_target_feature)),
140     ("avx512gfni", Some(sym::avx512_target_feature)),
141     ("avx512ifma", Some(sym::avx512_target_feature)),
142     ("avx512pf", Some(sym::avx512_target_feature)),
143     ("avx512vaes", Some(sym::avx512_target_feature)),
144     ("avx512vbmi", Some(sym::avx512_target_feature)),
145     ("avx512vbmi2", Some(sym::avx512_target_feature)),
146     ("avx512vl", Some(sym::avx512_target_feature)),
147     ("avx512vnni", Some(sym::avx512_target_feature)),
148     ("avx512vp2intersect", Some(sym::avx512_target_feature)),
149     ("avx512vpclmulqdq", Some(sym::avx512_target_feature)),
150     ("avx512vpopcntdq", Some(sym::avx512_target_feature)),
151     ("bmi1", None),
152     ("bmi2", None),
153     ("cmpxchg16b", Some(sym::cmpxchg16b_target_feature)),
154     ("ermsb", Some(sym::ermsb_target_feature)),
155     ("f16c", Some(sym::f16c_target_feature)),
156     ("fma", None),
157     ("fxsr", None),
158     ("lzcnt", None),
159     ("movbe", Some(sym::movbe_target_feature)),
160     ("pclmulqdq", None),
161     ("popcnt", None),
162     ("rdrand", None),
163     ("rdseed", None),
164     ("rtm", Some(sym::rtm_target_feature)),
165     ("sha", None),
166     ("sse", None),
167     ("sse2", None),
168     ("sse3", None),
169     ("sse4.1", None),
170     ("sse4.2", None),
171     ("sse4a", Some(sym::sse4a_target_feature)),
172     ("ssse3", None),
173     ("tbm", Some(sym::tbm_target_feature)),
174     ("xsave", None),
175     ("xsavec", None),
176     ("xsaveopt", None),
177     ("xsaves", None),
178 ];
179
180 const HEXAGON_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
181     ("hvx", Some(sym::hexagon_target_feature)),
182     ("hvx-length128b", Some(sym::hexagon_target_feature)),
183 ];
184
185 const POWERPC_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
186     ("altivec", Some(sym::powerpc_target_feature)),
187     ("power8-altivec", Some(sym::powerpc_target_feature)),
188     ("power9-altivec", Some(sym::powerpc_target_feature)),
189     ("power8-vector", Some(sym::powerpc_target_feature)),
190     ("power9-vector", Some(sym::powerpc_target_feature)),
191     ("vsx", Some(sym::powerpc_target_feature)),
192 ];
193
194 const MIPS_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] =
195     &[("fp64", Some(sym::mips_target_feature)), ("msa", Some(sym::mips_target_feature))];
196
197 const RISCV_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
198     ("m", Some(sym::riscv_target_feature)),
199     ("a", Some(sym::riscv_target_feature)),
200     ("c", Some(sym::riscv_target_feature)),
201     ("f", Some(sym::riscv_target_feature)),
202     ("d", Some(sym::riscv_target_feature)),
203     ("e", Some(sym::riscv_target_feature)),
204 ];
205
206 const WASM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
207     ("simd128", None),
208     ("atomics", Some(sym::wasm_target_feature)),
209     ("nontrapping-fptoint", Some(sym::wasm_target_feature)),
210 ];
211
212 const BPF_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[("alu32", Some(sym::bpf_target_feature))];
213
214 /// When rustdoc is running, provide a list of all known features so that all their respective
215 /// primitives may be documented.
216 ///
217 /// IMPORTANT: If you're adding another feature list above, make sure to add it to this iterator!
218 pub fn all_known_features() -> impl Iterator<Item = (&'static str, Option<Symbol>)> {
219     std::iter::empty()
220         .chain(ARM_ALLOWED_FEATURES.iter())
221         .chain(AARCH64_ALLOWED_FEATURES.iter())
222         .chain(X86_ALLOWED_FEATURES.iter())
223         .chain(HEXAGON_ALLOWED_FEATURES.iter())
224         .chain(POWERPC_ALLOWED_FEATURES.iter())
225         .chain(MIPS_ALLOWED_FEATURES.iter())
226         .chain(RISCV_ALLOWED_FEATURES.iter())
227         .chain(WASM_ALLOWED_FEATURES.iter())
228         .chain(BPF_ALLOWED_FEATURES.iter())
229         .cloned()
230 }
231
232 pub fn supported_target_features(sess: &Session) -> &'static [(&'static str, Option<Symbol>)] {
233     match &*sess.target.arch {
234         "arm" => ARM_ALLOWED_FEATURES,
235         "aarch64" => AARCH64_ALLOWED_FEATURES,
236         "x86" | "x86_64" => X86_ALLOWED_FEATURES,
237         "hexagon" => HEXAGON_ALLOWED_FEATURES,
238         "mips" | "mips64" => MIPS_ALLOWED_FEATURES,
239         "powerpc" | "powerpc64" => POWERPC_ALLOWED_FEATURES,
240         "riscv32" | "riscv64" => RISCV_ALLOWED_FEATURES,
241         "wasm32" | "wasm64" => WASM_ALLOWED_FEATURES,
242         "bpf" => BPF_ALLOWED_FEATURES,
243         _ => &[],
244     }
245 }
246
247 pub(crate) fn provide(providers: &mut Providers) {
248     providers.supported_target_features = |tcx, cnum| {
249         assert_eq!(cnum, LOCAL_CRATE);
250         if tcx.sess.opts.actually_rustdoc {
251             // rustdoc needs to be able to document functions that use all the features, so
252             // whitelist them all
253             all_known_features().map(|(a, b)| (a.to_string(), b)).collect()
254         } else {
255             supported_target_features(tcx.sess).iter().map(|&(a, b)| (a.to_string(), b)).collect()
256         }
257     };
258 }