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