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