]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/target_features.rs
Rollup merge of #104420 - TethysSvensson:master, r=JohnTitor
[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 /// Features that control behaviour of rustc, rather than the codegen.
8 pub const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
9
10 // When adding features to the below lists
11 // check whether they're named already elsewhere in rust
12 // e.g. in stdarch and whether the given name matches LLVM's
13 // if it doesn't, to_llvm_feature in llvm_util in rustc_codegen_llvm needs to be adapted
14
15 const ARM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
16     // tidy-alphabetical-start
17     ("aclass", Some(sym::arm_target_feature)),
18     ("aes", Some(sym::arm_target_feature)),
19     ("crc", Some(sym::arm_target_feature)),
20     ("crypto", Some(sym::arm_target_feature)),
21     ("d32", Some(sym::arm_target_feature)),
22     ("dotprod", Some(sym::arm_target_feature)),
23     ("dsp", Some(sym::arm_target_feature)),
24     ("fp-armv8", Some(sym::arm_target_feature)),
25     ("i8mm", Some(sym::arm_target_feature)),
26     ("mclass", Some(sym::arm_target_feature)),
27     ("neon", Some(sym::arm_target_feature)),
28     ("rclass", Some(sym::arm_target_feature)),
29     ("sha2", Some(sym::arm_target_feature)),
30     // This is needed for inline assembly, but shouldn't be stabilized as-is
31     // since it should be enabled per-function using #[instruction_set], not
32     // #[target_feature].
33     ("thumb-mode", Some(sym::arm_target_feature)),
34     ("thumb2", Some(sym::arm_target_feature)),
35     ("v5te", Some(sym::arm_target_feature)),
36     ("v6", Some(sym::arm_target_feature)),
37     ("v6k", Some(sym::arm_target_feature)),
38     ("v6t2", Some(sym::arm_target_feature)),
39     ("v7", Some(sym::arm_target_feature)),
40     ("v8", Some(sym::arm_target_feature)),
41     ("vfp2", Some(sym::arm_target_feature)),
42     ("vfp3", Some(sym::arm_target_feature)),
43     ("vfp4", Some(sym::arm_target_feature)),
44     // tidy-alphabetical-end
45 ];
46
47 const AARCH64_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
48     // tidy-alphabetical-start
49     // FEAT_AES
50     ("aes", None),
51     // FEAT_BF16
52     ("bf16", None),
53     // FEAT_BTI
54     ("bti", None),
55     // FEAT_CRC
56     ("crc", None),
57     // FEAT_DIT
58     ("dit", None),
59     // FEAT_DotProd
60     ("dotprod", None),
61     // FEAT_DPB
62     ("dpb", None),
63     // FEAT_DPB2
64     ("dpb2", None),
65     // FEAT_F32MM
66     ("f32mm", None),
67     // FEAT_F64MM
68     ("f64mm", None),
69     // FEAT_FCMA
70     ("fcma", None),
71     // FEAT_FHM
72     ("fhm", None),
73     // FEAT_FLAGM
74     ("flagm", None),
75     // FEAT_FP16
76     ("fp16", None),
77     // FEAT_FRINTTS
78     ("frintts", None),
79     // FEAT_I8MM
80     ("i8mm", None),
81     // FEAT_JSCVT
82     ("jsconv", None),
83     // FEAT_LOR
84     ("lor", None),
85     // FEAT_LSE
86     ("lse", None),
87     // FEAT_MTE
88     ("mte", None),
89     // FEAT_AdvSimd & FEAT_FP
90     ("neon", None),
91     // FEAT_PAUTH (address authentication)
92     ("paca", None),
93     // FEAT_PAUTH (generic authentication)
94     ("pacg", None),
95     // FEAT_PAN
96     ("pan", None),
97     // FEAT_PMUv3
98     ("pmuv3", None),
99     // FEAT_RAND
100     ("rand", None),
101     // FEAT_RAS
102     ("ras", None),
103     // FEAT_RCPC
104     ("rcpc", None),
105     // FEAT_RCPC2
106     ("rcpc2", None),
107     // FEAT_RDM
108     ("rdm", None),
109     // FEAT_SB
110     ("sb", None),
111     // FEAT_SHA1 & FEAT_SHA256
112     ("sha2", None),
113     // FEAT_SHA512 & FEAT_SHA3
114     ("sha3", None),
115     // FEAT_SM3 & FEAT_SM4
116     ("sm4", None),
117     // FEAT_SPE
118     ("spe", None),
119     // FEAT_SSBS
120     ("ssbs", None),
121     // FEAT_SVE
122     ("sve", None),
123     // FEAT_SVE2
124     ("sve2", None),
125     // FEAT_SVE2_AES
126     ("sve2-aes", None),
127     // FEAT_SVE2_BitPerm
128     ("sve2-bitperm", None),
129     // FEAT_SVE2_SHA3
130     ("sve2-sha3", None),
131     // FEAT_SVE2_SM4
132     ("sve2-sm4", None),
133     // FEAT_TME
134     ("tme", None),
135     ("v8.1a", Some(sym::aarch64_ver_target_feature)),
136     ("v8.2a", Some(sym::aarch64_ver_target_feature)),
137     ("v8.3a", Some(sym::aarch64_ver_target_feature)),
138     ("v8.4a", Some(sym::aarch64_ver_target_feature)),
139     ("v8.5a", Some(sym::aarch64_ver_target_feature)),
140     ("v8.6a", Some(sym::aarch64_ver_target_feature)),
141     ("v8.7a", Some(sym::aarch64_ver_target_feature)),
142     // FEAT_VHE
143     ("vh", None),
144     // tidy-alphabetical-end
145 ];
146
147 const AARCH64_TIED_FEATURES: &[&[&str]] = &[
148     &["paca", "pacg"], // Together these represent `pauth` in LLVM
149 ];
150
151 const X86_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
152     // tidy-alphabetical-start
153     ("adx", None),
154     ("aes", None),
155     ("avx", None),
156     ("avx2", None),
157     ("avx512bf16", Some(sym::avx512_target_feature)),
158     ("avx512bitalg", Some(sym::avx512_target_feature)),
159     ("avx512bw", Some(sym::avx512_target_feature)),
160     ("avx512cd", Some(sym::avx512_target_feature)),
161     ("avx512dq", Some(sym::avx512_target_feature)),
162     ("avx512er", Some(sym::avx512_target_feature)),
163     ("avx512f", Some(sym::avx512_target_feature)),
164     ("avx512gfni", Some(sym::avx512_target_feature)),
165     ("avx512ifma", Some(sym::avx512_target_feature)),
166     ("avx512pf", Some(sym::avx512_target_feature)),
167     ("avx512vaes", Some(sym::avx512_target_feature)),
168     ("avx512vbmi", Some(sym::avx512_target_feature)),
169     ("avx512vbmi2", Some(sym::avx512_target_feature)),
170     ("avx512vl", Some(sym::avx512_target_feature)),
171     ("avx512vnni", Some(sym::avx512_target_feature)),
172     ("avx512vp2intersect", Some(sym::avx512_target_feature)),
173     ("avx512vpclmulqdq", Some(sym::avx512_target_feature)),
174     ("avx512vpopcntdq", Some(sym::avx512_target_feature)),
175     ("bmi1", None),
176     ("bmi2", None),
177     ("cmpxchg16b", Some(sym::cmpxchg16b_target_feature)),
178     ("ermsb", Some(sym::ermsb_target_feature)),
179     ("f16c", Some(sym::f16c_target_feature)),
180     ("fma", None),
181     ("fxsr", None),
182     ("gfni", Some(sym::avx512_target_feature)),
183     ("lzcnt", None),
184     ("movbe", Some(sym::movbe_target_feature)),
185     ("pclmulqdq", None),
186     ("popcnt", None),
187     ("rdrand", None),
188     ("rdseed", None),
189     ("rtm", Some(sym::rtm_target_feature)),
190     ("sha", None),
191     ("sse", None),
192     ("sse2", None),
193     ("sse3", None),
194     ("sse4.1", None),
195     ("sse4.2", None),
196     ("sse4a", Some(sym::sse4a_target_feature)),
197     ("ssse3", None),
198     ("tbm", Some(sym::tbm_target_feature)),
199     ("vaes", Some(sym::avx512_target_feature)),
200     ("vpclmulqdq", Some(sym::avx512_target_feature)),
201     ("xsave", None),
202     ("xsavec", None),
203     ("xsaveopt", None),
204     ("xsaves", None),
205     // tidy-alphabetical-end
206 ];
207
208 const HEXAGON_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
209     // tidy-alphabetical-start
210     ("hvx", Some(sym::hexagon_target_feature)),
211     ("hvx-length128b", Some(sym::hexagon_target_feature)),
212     // tidy-alphabetical-end
213 ];
214
215 const POWERPC_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
216     // tidy-alphabetical-start
217     ("altivec", Some(sym::powerpc_target_feature)),
218     ("power8-altivec", Some(sym::powerpc_target_feature)),
219     ("power8-vector", Some(sym::powerpc_target_feature)),
220     ("power9-altivec", Some(sym::powerpc_target_feature)),
221     ("power9-vector", Some(sym::powerpc_target_feature)),
222     ("vsx", Some(sym::powerpc_target_feature)),
223     // tidy-alphabetical-end
224 ];
225
226 const MIPS_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
227     // tidy-alphabetical-start
228     ("fp64", Some(sym::mips_target_feature)),
229     ("msa", Some(sym::mips_target_feature)),
230     ("virt", Some(sym::mips_target_feature)),
231     // tidy-alphabetical-end
232 ];
233
234 const RISCV_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
235     // tidy-alphabetical-start
236     ("a", Some(sym::riscv_target_feature)),
237     ("c", Some(sym::riscv_target_feature)),
238     ("d", Some(sym::riscv_target_feature)),
239     ("e", Some(sym::riscv_target_feature)),
240     ("f", Some(sym::riscv_target_feature)),
241     ("m", Some(sym::riscv_target_feature)),
242     ("v", Some(sym::riscv_target_feature)),
243     ("zba", Some(sym::riscv_target_feature)),
244     ("zbb", Some(sym::riscv_target_feature)),
245     ("zbc", Some(sym::riscv_target_feature)),
246     ("zbkb", Some(sym::riscv_target_feature)),
247     ("zbkc", Some(sym::riscv_target_feature)),
248     ("zbkx", Some(sym::riscv_target_feature)),
249     ("zbs", Some(sym::riscv_target_feature)),
250     ("zdinx", Some(sym::riscv_target_feature)),
251     ("zfh", Some(sym::riscv_target_feature)),
252     ("zfhmin", Some(sym::riscv_target_feature)),
253     ("zfinx", Some(sym::riscv_target_feature)),
254     ("zhinx", Some(sym::riscv_target_feature)),
255     ("zhinxmin", Some(sym::riscv_target_feature)),
256     ("zk", Some(sym::riscv_target_feature)),
257     ("zkn", Some(sym::riscv_target_feature)),
258     ("zknd", Some(sym::riscv_target_feature)),
259     ("zkne", Some(sym::riscv_target_feature)),
260     ("zknh", Some(sym::riscv_target_feature)),
261     ("zkr", Some(sym::riscv_target_feature)),
262     ("zks", Some(sym::riscv_target_feature)),
263     ("zksed", Some(sym::riscv_target_feature)),
264     ("zksh", Some(sym::riscv_target_feature)),
265     ("zkt", Some(sym::riscv_target_feature)),
266     // tidy-alphabetical-end
267 ];
268
269 const WASM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
270     // tidy-alphabetical-start
271     ("atomics", Some(sym::wasm_target_feature)),
272     ("bulk-memory", Some(sym::wasm_target_feature)),
273     ("multivalue", Some(sym::wasm_target_feature)),
274     ("mutable-globals", Some(sym::wasm_target_feature)),
275     ("nontrapping-fptoint", Some(sym::wasm_target_feature)),
276     ("reference-types", Some(sym::wasm_target_feature)),
277     ("sign-ext", Some(sym::wasm_target_feature)),
278     ("simd128", None),
279     // tidy-alphabetical-end
280 ];
281
282 const BPF_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[("alu32", Some(sym::bpf_target_feature))];
283
284 /// When rustdoc is running, provide a list of all known features so that all their respective
285 /// primitives may be documented.
286 ///
287 /// IMPORTANT: If you're adding another feature list above, make sure to add it to this iterator!
288 pub fn all_known_features() -> impl Iterator<Item = (&'static str, Option<Symbol>)> {
289     std::iter::empty()
290         .chain(ARM_ALLOWED_FEATURES.iter())
291         .chain(AARCH64_ALLOWED_FEATURES.iter())
292         .chain(X86_ALLOWED_FEATURES.iter())
293         .chain(HEXAGON_ALLOWED_FEATURES.iter())
294         .chain(POWERPC_ALLOWED_FEATURES.iter())
295         .chain(MIPS_ALLOWED_FEATURES.iter())
296         .chain(RISCV_ALLOWED_FEATURES.iter())
297         .chain(WASM_ALLOWED_FEATURES.iter())
298         .chain(BPF_ALLOWED_FEATURES.iter())
299         .cloned()
300 }
301
302 pub fn supported_target_features(sess: &Session) -> &'static [(&'static str, Option<Symbol>)] {
303     match &*sess.target.arch {
304         "arm" => ARM_ALLOWED_FEATURES,
305         "aarch64" => AARCH64_ALLOWED_FEATURES,
306         "x86" | "x86_64" => X86_ALLOWED_FEATURES,
307         "hexagon" => HEXAGON_ALLOWED_FEATURES,
308         "mips" | "mips64" => MIPS_ALLOWED_FEATURES,
309         "powerpc" | "powerpc64" => POWERPC_ALLOWED_FEATURES,
310         "riscv32" | "riscv64" => RISCV_ALLOWED_FEATURES,
311         "wasm32" | "wasm64" => WASM_ALLOWED_FEATURES,
312         "bpf" => BPF_ALLOWED_FEATURES,
313         _ => &[],
314     }
315 }
316
317 pub fn tied_target_features(sess: &Session) -> &'static [&'static [&'static str]] {
318     match &*sess.target.arch {
319         "aarch64" => AARCH64_TIED_FEATURES,
320         _ => &[],
321     }
322 }
323
324 pub(crate) fn provide(providers: &mut Providers) {
325     providers.supported_target_features = |tcx, cnum| {
326         assert_eq!(cnum, LOCAL_CRATE);
327         if tcx.sess.opts.actually_rustdoc {
328             // rustdoc needs to be able to document functions that use all the features, so
329             // whitelist them all
330             all_known_features().map(|(a, b)| (a.to_string(), b)).collect()
331         } else {
332             supported_target_features(tcx.sess).iter().map(|&(a, b)| (a.to_string(), b)).collect()
333         }
334     };
335 }