rustc_target/
target_features.rs

1//! Declares Rust's target feature names for each target.
2//! Note that these are similar to but not always identical to LLVM's feature names,
3//! and Rust adds some features that do not correspond to LLVM features at all.
4use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
6use rustc_span::{Symbol, sym};
7
8use crate::spec::{FloatAbi, RustcAbi, Target};
9
10/// Features that control behaviour of rustc, rather than the codegen.
11/// These exist globally and are not in the target-specific lists below.
12pub const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
13
14/// Stability information for target features.
15#[derive(Debug, Copy, Clone)]
16pub enum Stability {
17    /// This target feature is stable, it can be used in `#[target_feature]` and
18    /// `#[cfg(target_feature)]`.
19    Stable,
20    /// This target feature is unstable. It is only present in `#[cfg(target_feature)]` on
21    /// nightly and using it in `#[target_feature]` requires enabling the given nightly feature.
22    Unstable(
23        /// This must be a *language* feature, or else rustc will ICE when reporting a missing
24        /// feature gate!
25        Symbol,
26    ),
27    /// This feature can not be set via `-Ctarget-feature` or `#[target_feature]`, it can only be
28    /// set in the target spec. It is never set in `cfg(target_feature)`. Used in
29    /// particular for features are actually ABI configuration flags (not all targets are as nice as
30    /// RISC-V and have an explicit way to set the ABI separate from target features).
31    Forbidden { reason: &'static str },
32}
33use Stability::*;
34
35impl<CTX> HashStable<CTX> for Stability {
36    #[inline]
37    fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
38        std::mem::discriminant(self).hash_stable(hcx, hasher);
39        match self {
40            Stability::Stable => {}
41            Stability::Unstable(nightly_feature) => {
42                nightly_feature.hash_stable(hcx, hasher);
43            }
44            Stability::Forbidden { reason } => {
45                reason.hash_stable(hcx, hasher);
46            }
47        }
48    }
49}
50
51impl Stability {
52    /// Returns whether the feature can be used in `cfg(target_feature)` ever.
53    /// (It might still be nightly-only even if this returns `true`, so make sure to also check
54    /// `requires_nightly`.)
55    pub fn in_cfg(&self) -> bool {
56        matches!(self, Stability::Stable | Stability::Unstable { .. })
57    }
58
59    /// Returns the nightly feature that is required to toggle this target feature via
60    /// `#[target_feature]`/`-Ctarget-feature` or to test it via `cfg(target_feature)`.
61    /// (For `cfg` we only care whether the feature is nightly or not, we don't require
62    /// the feature gate to actually be enabled when using a nightly compiler.)
63    ///
64    /// Before calling this, ensure the feature is even permitted for this use:
65    /// - for `#[target_feature]`/`-Ctarget-feature`, check `allow_toggle()`
66    /// - for `cfg(target_feature)`, check `in_cfg`
67    pub fn requires_nightly(&self) -> Option<Symbol> {
68        match *self {
69            Stability::Unstable(nightly_feature) => Some(nightly_feature),
70            Stability::Stable { .. } => None,
71            Stability::Forbidden { .. } => panic!("forbidden features should not reach this far"),
72        }
73    }
74
75    /// Returns whether the feature may be toggled via `#[target_feature]` or `-Ctarget-feature`.
76    /// (It might still be nightly-only even if this returns `true`, so make sure to also check
77    /// `requires_nightly`.)
78    pub fn toggle_allowed(&self) -> Result<(), &'static str> {
79        match self {
80            Stability::Unstable(_) | Stability::Stable { .. } => Ok(()),
81            Stability::Forbidden { reason } => Err(reason),
82        }
83    }
84}
85
86// Here we list target features that rustc "understands": they can be used in `#[target_feature]`
87// and `#[cfg(target_feature)]`. They also do not trigger any warnings when used with
88// `-Ctarget-feature`.
89//
90// Note that even unstable (and even entirely unlisted) features can be used with `-Ctarget-feature`
91// on stable. Using a feature not on the list of Rust target features only emits a warning.
92// Only `cfg(target_feature)` and `#[target_feature]` actually do any stability gating.
93// `cfg(target_feature)` for unstable features just works on nightly without any feature gate.
94// `#[target_feature]` requires a feature gate.
95//
96// When adding features to the below lists
97// check whether they're named already elsewhere in rust
98// e.g. in stdarch and whether the given name matches LLVM's
99// if it doesn't, to_llvm_feature in llvm_util in rustc_codegen_llvm needs to be adapted.
100// Additionally, if the feature is not available in older version of LLVM supported by the current
101// rust, the same function must be updated to filter out these features to avoid triggering
102// warnings.
103//
104// Also note that all target features listed here must be purely additive: for target_feature 1.1 to
105// be sound, we can never allow features like `+soft-float` (on x86) to be controlled on a
106// per-function level, since we would then allow safe calls from functions with `+soft-float` to
107// functions without that feature!
108//
109// It is important for soundness to consider the interaction of targets features and the function
110// call ABI. For example, disabling the `x87` feature on x86 changes how scalar floats are passed as
111// arguments, so letting people toggle that feature would be unsound. To this end, the
112// `abi_required_features` function computes which target features must and must not be enabled for
113// any given target, and individual features can also be marked as `Forbidden`.
114// See https://github.com/rust-lang/rust/issues/116344 for some more context.
115//
116// The one exception to features that change the ABI is features that enable larger vector
117// registers. Those are permitted to be listed here. The `*_FOR_CORRECT_VECTOR_ABI` arrays store
118// information about which target feature is ABI-required for which vector size; this is used to
119// ensure that vectors can only be passed via `extern "C"` when the right feature is enabled. (For
120// the "Rust" ABI we generally pass vectors by-ref exactly to avoid these issues.)
121// Also see https://github.com/rust-lang/rust/issues/116558.
122//
123// Stabilizing a target feature requires t-lang approval.
124
125// If feature A "implies" feature B, then:
126// - when A gets enabled (via `-Ctarget-feature` or `#[target_feature]`), we also enable B
127// - when B gets disabled (via `-Ctarget-feature`), we also disable A
128//
129// Both of these are also applied transitively.
130type ImpliedFeatures = &'static [&'static str];
131
132static ARM_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
133    // tidy-alphabetical-start
134    ("aclass", Unstable(sym::arm_target_feature), &[]),
135    ("aes", Unstable(sym::arm_target_feature), &["neon"]),
136    (
137        "atomics-32",
138        Stability::Forbidden { reason: "unsound because it changes the ABI of atomic operations" },
139        &[],
140    ),
141    ("crc", Unstable(sym::arm_target_feature), &[]),
142    ("d32", Unstable(sym::arm_target_feature), &[]),
143    ("dotprod", Unstable(sym::arm_target_feature), &["neon"]),
144    ("dsp", Unstable(sym::arm_target_feature), &[]),
145    ("fp-armv8", Unstable(sym::arm_target_feature), &["vfp4"]),
146    ("fp16", Unstable(sym::arm_target_feature), &["neon"]),
147    ("fpregs", Unstable(sym::arm_target_feature), &[]),
148    ("i8mm", Unstable(sym::arm_target_feature), &["neon"]),
149    ("mclass", Unstable(sym::arm_target_feature), &[]),
150    ("neon", Unstable(sym::arm_target_feature), &["vfp3"]),
151    ("rclass", Unstable(sym::arm_target_feature), &[]),
152    ("sha2", Unstable(sym::arm_target_feature), &["neon"]),
153    // This can be *disabled* on non-`hf` targets to enable the use
154    // of hardfloats while keeping the softfloat ABI.
155    // FIXME before stabilization: Should we expose this as a `hard-float` target feature instead of
156    // matching the odd negative feature LLVM uses?
157    ("soft-float", Unstable(sym::arm_target_feature), &[]),
158    // This is needed for inline assembly, but shouldn't be stabilized as-is
159    // since it should be enabled per-function using #[instruction_set], not
160    // #[target_feature].
161    ("thumb-mode", Unstable(sym::arm_target_feature), &[]),
162    ("thumb2", Unstable(sym::arm_target_feature), &[]),
163    ("trustzone", Unstable(sym::arm_target_feature), &[]),
164    ("v5te", Unstable(sym::arm_target_feature), &[]),
165    ("v6", Unstable(sym::arm_target_feature), &["v5te"]),
166    ("v6k", Unstable(sym::arm_target_feature), &["v6"]),
167    ("v6t2", Unstable(sym::arm_target_feature), &["v6k", "thumb2"]),
168    ("v7", Unstable(sym::arm_target_feature), &["v6t2"]),
169    ("v8", Unstable(sym::arm_target_feature), &["v7"]),
170    ("vfp2", Unstable(sym::arm_target_feature), &[]),
171    ("vfp3", Unstable(sym::arm_target_feature), &["vfp2", "d32"]),
172    ("vfp4", Unstable(sym::arm_target_feature), &["vfp3"]),
173    ("virtualization", Unstable(sym::arm_target_feature), &[]),
174    // tidy-alphabetical-end
175];
176
177static AARCH64_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
178    // tidy-alphabetical-start
179    // FEAT_AES & FEAT_PMULL
180    ("aes", Stable, &["neon"]),
181    // FEAT_BF16
182    ("bf16", Stable, &[]),
183    // FEAT_BTI
184    ("bti", Stable, &[]),
185    // FEAT_CRC
186    ("crc", Stable, &[]),
187    // FEAT_CSSC
188    ("cssc", Unstable(sym::aarch64_unstable_target_feature), &[]),
189    // FEAT_DIT
190    ("dit", Stable, &[]),
191    // FEAT_DotProd
192    ("dotprod", Stable, &["neon"]),
193    // FEAT_DPB
194    ("dpb", Stable, &[]),
195    // FEAT_DPB2
196    ("dpb2", Stable, &["dpb"]),
197    // FEAT_ECV
198    ("ecv", Unstable(sym::aarch64_unstable_target_feature), &[]),
199    // FEAT_F32MM
200    ("f32mm", Stable, &["sve"]),
201    // FEAT_F64MM
202    ("f64mm", Stable, &["sve"]),
203    // FEAT_FAMINMAX
204    ("faminmax", Unstable(sym::aarch64_unstable_target_feature), &[]),
205    // FEAT_FCMA
206    ("fcma", Stable, &["neon"]),
207    // FEAT_FHM
208    ("fhm", Stable, &["fp16"]),
209    // FEAT_FLAGM
210    ("flagm", Stable, &[]),
211    // FEAT_FLAGM2
212    ("flagm2", Unstable(sym::aarch64_unstable_target_feature), &[]),
213    // We forbid directly toggling just `fp-armv8`; it must be toggled with `neon`.
214    ("fp-armv8", Stability::Forbidden { reason: "Rust ties `fp-armv8` to `neon`" }, &[]),
215    // FEAT_FP8
216    ("fp8", Unstable(sym::aarch64_unstable_target_feature), &["faminmax", "lut", "bf16"]),
217    // FEAT_FP8DOT2
218    ("fp8dot2", Unstable(sym::aarch64_unstable_target_feature), &["fp8dot4"]),
219    // FEAT_FP8DOT4
220    ("fp8dot4", Unstable(sym::aarch64_unstable_target_feature), &["fp8fma"]),
221    // FEAT_FP8FMA
222    ("fp8fma", Unstable(sym::aarch64_unstable_target_feature), &["fp8"]),
223    // FEAT_FP16
224    // Rust ties FP and Neon: https://github.com/rust-lang/rust/pull/91608
225    ("fp16", Stable, &["neon"]),
226    // FEAT_FRINTTS
227    ("frintts", Stable, &[]),
228    // FEAT_HBC
229    ("hbc", Unstable(sym::aarch64_unstable_target_feature), &[]),
230    // FEAT_I8MM
231    ("i8mm", Stable, &[]),
232    // FEAT_JSCVT
233    // Rust ties FP and Neon: https://github.com/rust-lang/rust/pull/91608
234    ("jsconv", Stable, &["neon"]),
235    // FEAT_LOR
236    ("lor", Stable, &[]),
237    // FEAT_LSE
238    ("lse", Stable, &[]),
239    // FEAT_LSE2
240    ("lse2", Unstable(sym::aarch64_unstable_target_feature), &[]),
241    // FEAT_LSE128
242    ("lse128", Unstable(sym::aarch64_unstable_target_feature), &["lse"]),
243    // FEAT_LUT
244    ("lut", Unstable(sym::aarch64_unstable_target_feature), &[]),
245    // FEAT_MOPS
246    ("mops", Unstable(sym::aarch64_unstable_target_feature), &[]),
247    // FEAT_MTE & FEAT_MTE2
248    ("mte", Stable, &[]),
249    // FEAT_AdvSimd & FEAT_FP
250    ("neon", Stable, &[]),
251    // FEAT_PAUTH (address authentication)
252    ("paca", Stable, &[]),
253    // FEAT_PAUTH (generic authentication)
254    ("pacg", Stable, &[]),
255    // FEAT_PAN
256    ("pan", Stable, &[]),
257    // FEAT_PAuth_LR
258    ("pauth-lr", Unstable(sym::aarch64_unstable_target_feature), &[]),
259    // FEAT_PMUv3
260    ("pmuv3", Stable, &[]),
261    // FEAT_RNG
262    ("rand", Stable, &[]),
263    // FEAT_RAS & FEAT_RASv1p1
264    ("ras", Stable, &[]),
265    // FEAT_LRCPC
266    ("rcpc", Stable, &[]),
267    // FEAT_LRCPC2
268    ("rcpc2", Stable, &["rcpc"]),
269    // FEAT_LRCPC3
270    ("rcpc3", Unstable(sym::aarch64_unstable_target_feature), &["rcpc2"]),
271    // FEAT_RDM
272    ("rdm", Stable, &["neon"]),
273    ("reserve-x18", Forbidden { reason: "use `-Zfixed-x18` compiler flag instead" }, &[]),
274    // FEAT_SB
275    ("sb", Stable, &[]),
276    // FEAT_SHA1 & FEAT_SHA256
277    ("sha2", Stable, &["neon"]),
278    // FEAT_SHA512 & FEAT_SHA3
279    ("sha3", Stable, &["sha2"]),
280    // FEAT_SM3 & FEAT_SM4
281    ("sm4", Stable, &["neon"]),
282    // FEAT_SME
283    ("sme", Unstable(sym::aarch64_unstable_target_feature), &["bf16"]),
284    // FEAT_SME_B16B16
285    ("sme-b16b16", Unstable(sym::aarch64_unstable_target_feature), &["bf16", "sme2", "sve-b16b16"]),
286    // FEAT_SME_F8F16
287    ("sme-f8f16", Unstable(sym::aarch64_unstable_target_feature), &["sme-f8f32"]),
288    // FEAT_SME_F8F32
289    ("sme-f8f32", Unstable(sym::aarch64_unstable_target_feature), &["sme2", "fp8"]),
290    // FEAT_SME_F16F16
291    ("sme-f16f16", Unstable(sym::aarch64_unstable_target_feature), &["sme2"]),
292    // FEAT_SME_F64F64
293    ("sme-f64f64", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
294    // FEAT_SME_FA64
295    ("sme-fa64", Unstable(sym::aarch64_unstable_target_feature), &["sme", "sve2"]),
296    // FEAT_SME_I16I64
297    ("sme-i16i64", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
298    // FEAT_SME_LUTv2
299    ("sme-lutv2", Unstable(sym::aarch64_unstable_target_feature), &[]),
300    // FEAT_SME2
301    ("sme2", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
302    // FEAT_SME2p1
303    ("sme2p1", Unstable(sym::aarch64_unstable_target_feature), &["sme2"]),
304    // FEAT_SPE
305    ("spe", Stable, &[]),
306    // FEAT_SSBS & FEAT_SSBS2
307    ("ssbs", Stable, &[]),
308    // FEAT_SSVE_FP8FDOT2
309    ("ssve-fp8dot2", Unstable(sym::aarch64_unstable_target_feature), &["ssve-fp8dot4"]),
310    // FEAT_SSVE_FP8FDOT4
311    ("ssve-fp8dot4", Unstable(sym::aarch64_unstable_target_feature), &["ssve-fp8fma"]),
312    // FEAT_SSVE_FP8FMA
313    ("ssve-fp8fma", Unstable(sym::aarch64_unstable_target_feature), &["sme2", "fp8"]),
314    // FEAT_SVE
315    // It was decided that SVE requires Neon: https://github.com/rust-lang/rust/pull/91608
316    //
317    // LLVM doesn't enable Neon for SVE. ARM indicates that they're separate, but probably always
318    // exist together: https://developer.arm.com/documentation/102340/0100/New-features-in-SVE2
319    //
320    // "For backwards compatibility, Neon and VFP are required in the latest architectures."
321    ("sve", Stable, &["neon"]),
322    // FEAT_SVE_B16B16 (SVE or SME Z-targeting instructions)
323    ("sve-b16b16", Unstable(sym::aarch64_unstable_target_feature), &["bf16"]),
324    // FEAT_SVE2
325    ("sve2", Stable, &["sve"]),
326    // FEAT_SVE_AES & FEAT_SVE_PMULL128
327    ("sve2-aes", Stable, &["sve2", "aes"]),
328    // FEAT_SVE2_BitPerm
329    ("sve2-bitperm", Stable, &["sve2"]),
330    // FEAT_SVE2_SHA3
331    ("sve2-sha3", Stable, &["sve2", "sha3"]),
332    // FEAT_SVE2_SM4
333    ("sve2-sm4", Stable, &["sve2", "sm4"]),
334    // FEAT_SVE2p1
335    ("sve2p1", Unstable(sym::aarch64_unstable_target_feature), &["sve2"]),
336    // FEAT_TME
337    ("tme", Stable, &[]),
338    (
339        "v8.1a",
340        Unstable(sym::aarch64_ver_target_feature),
341        &["crc", "lse", "rdm", "pan", "lor", "vh"],
342    ),
343    ("v8.2a", Unstable(sym::aarch64_ver_target_feature), &["v8.1a", "ras", "dpb"]),
344    (
345        "v8.3a",
346        Unstable(sym::aarch64_ver_target_feature),
347        &["v8.2a", "rcpc", "paca", "pacg", "jsconv"],
348    ),
349    ("v8.4a", Unstable(sym::aarch64_ver_target_feature), &["v8.3a", "dotprod", "dit", "flagm"]),
350    ("v8.5a", Unstable(sym::aarch64_ver_target_feature), &["v8.4a", "ssbs", "sb", "dpb2", "bti"]),
351    ("v8.6a", Unstable(sym::aarch64_ver_target_feature), &["v8.5a", "bf16", "i8mm"]),
352    ("v8.7a", Unstable(sym::aarch64_ver_target_feature), &["v8.6a", "wfxt"]),
353    ("v8.8a", Unstable(sym::aarch64_ver_target_feature), &["v8.7a", "hbc", "mops"]),
354    ("v8.9a", Unstable(sym::aarch64_ver_target_feature), &["v8.8a", "cssc"]),
355    ("v9.1a", Unstable(sym::aarch64_ver_target_feature), &["v9a", "v8.6a"]),
356    ("v9.2a", Unstable(sym::aarch64_ver_target_feature), &["v9.1a", "v8.7a"]),
357    ("v9.3a", Unstable(sym::aarch64_ver_target_feature), &["v9.2a", "v8.8a"]),
358    ("v9.4a", Unstable(sym::aarch64_ver_target_feature), &["v9.3a", "v8.9a"]),
359    ("v9.5a", Unstable(sym::aarch64_ver_target_feature), &["v9.4a"]),
360    ("v9a", Unstable(sym::aarch64_ver_target_feature), &["v8.5a", "sve2"]),
361    // FEAT_VHE
362    ("vh", Stable, &[]),
363    // FEAT_WFxT
364    ("wfxt", Unstable(sym::aarch64_unstable_target_feature), &[]),
365    // tidy-alphabetical-end
366];
367
368const AARCH64_TIED_FEATURES: &[&[&str]] = &[
369    &["paca", "pacg"], // Together these represent `pauth` in LLVM
370];
371
372static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
373    // tidy-alphabetical-start
374    ("adx", Stable, &[]),
375    ("aes", Stable, &["sse2"]),
376    ("amx-avx512", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
377    ("amx-bf16", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
378    ("amx-complex", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
379    ("amx-fp8", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
380    ("amx-fp16", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
381    ("amx-int8", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
382    ("amx-movrs", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
383    ("amx-tf32", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
384    ("amx-tile", Unstable(sym::x86_amx_intrinsics), &[]),
385    ("amx-transpose", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
386    ("apxf", Unstable(sym::apx_target_feature), &[]),
387    ("avx", Stable, &["sse4.2"]),
388    ("avx2", Stable, &["avx"]),
389    (
390        "avx10.1",
391        Unstable(sym::avx10_target_feature),
392        &[
393            "avx512bf16",
394            "avx512bitalg",
395            "avx512bw",
396            "avx512cd",
397            "avx512dq",
398            "avx512f",
399            "avx512fp16",
400            "avx512ifma",
401            "avx512vbmi",
402            "avx512vbmi2",
403            "avx512vl",
404            "avx512vnni",
405            "avx512vpopcntdq",
406        ],
407    ),
408    ("avx10.2", Unstable(sym::avx10_target_feature), &["avx10.1"]),
409    ("avx512bf16", Stable, &["avx512bw"]),
410    ("avx512bitalg", Stable, &["avx512bw"]),
411    ("avx512bw", Stable, &["avx512f"]),
412    ("avx512cd", Stable, &["avx512f"]),
413    ("avx512dq", Stable, &["avx512f"]),
414    ("avx512f", Stable, &["avx2", "fma", "f16c"]),
415    ("avx512fp16", Stable, &["avx512bw"]),
416    ("avx512ifma", Stable, &["avx512f"]),
417    ("avx512vbmi", Stable, &["avx512bw"]),
418    ("avx512vbmi2", Stable, &["avx512bw"]),
419    ("avx512vl", Stable, &["avx512f"]),
420    ("avx512vnni", Stable, &["avx512f"]),
421    ("avx512vp2intersect", Stable, &["avx512f"]),
422    ("avx512vpopcntdq", Stable, &["avx512f"]),
423    ("avxifma", Stable, &["avx2"]),
424    ("avxneconvert", Stable, &["avx2"]),
425    ("avxvnni", Stable, &["avx2"]),
426    ("avxvnniint8", Stable, &["avx2"]),
427    ("avxvnniint16", Stable, &["avx2"]),
428    ("bmi1", Stable, &[]),
429    ("bmi2", Stable, &[]),
430    ("cmpxchg16b", Stable, &[]),
431    ("ermsb", Unstable(sym::ermsb_target_feature), &[]),
432    ("f16c", Stable, &["avx"]),
433    ("fma", Stable, &["avx"]),
434    ("fxsr", Stable, &[]),
435    ("gfni", Stable, &["sse2"]),
436    ("kl", Stable, &["sse2"]),
437    ("lahfsahf", Unstable(sym::lahfsahf_target_feature), &[]),
438    ("lzcnt", Stable, &[]),
439    ("movbe", Stable, &[]),
440    ("movrs", Unstable(sym::movrs_target_feature), &[]),
441    ("pclmulqdq", Stable, &["sse2"]),
442    ("popcnt", Stable, &[]),
443    ("prfchw", Unstable(sym::prfchw_target_feature), &[]),
444    ("rdrand", Stable, &[]),
445    ("rdseed", Stable, &[]),
446    (
447        "retpoline-external-thunk",
448        Stability::Forbidden { reason: "use `-Zretpoline-external-thunk` compiler flag instead" },
449        &[],
450    ),
451    (
452        "retpoline-indirect-branches",
453        Stability::Forbidden { reason: "use `-Zretpoline` compiler flag instead" },
454        &[],
455    ),
456    (
457        "retpoline-indirect-calls",
458        Stability::Forbidden { reason: "use `-Zretpoline` compiler flag instead" },
459        &[],
460    ),
461    ("rtm", Unstable(sym::rtm_target_feature), &[]),
462    ("sha", Stable, &["sse2"]),
463    ("sha512", Stable, &["avx2"]),
464    ("sm3", Stable, &["avx"]),
465    ("sm4", Stable, &["avx2"]),
466    // This cannot actually be toggled, the ABI always fixes it, so it'd make little sense to
467    // stabilize. It must be in this list for the ABI check to be able to use it.
468    ("soft-float", Stability::Unstable(sym::x87_target_feature), &[]),
469    ("sse", Stable, &[]),
470    ("sse2", Stable, &["sse"]),
471    ("sse3", Stable, &["sse2"]),
472    ("sse4.1", Stable, &["ssse3"]),
473    ("sse4.2", Stable, &["sse4.1"]),
474    ("sse4a", Unstable(sym::sse4a_target_feature), &["sse3"]),
475    ("ssse3", Stable, &["sse3"]),
476    ("tbm", Unstable(sym::tbm_target_feature), &[]),
477    ("vaes", Stable, &["avx2", "aes"]),
478    ("vpclmulqdq", Stable, &["avx", "pclmulqdq"]),
479    ("widekl", Stable, &["kl"]),
480    ("x87", Unstable(sym::x87_target_feature), &[]),
481    ("xop", Unstable(sym::xop_target_feature), &[/*"fma4", */ "avx", "sse4a"]),
482    ("xsave", Stable, &[]),
483    ("xsavec", Stable, &["xsave"]),
484    ("xsaveopt", Stable, &["xsave"]),
485    ("xsaves", Stable, &["xsave"]),
486    // tidy-alphabetical-end
487];
488
489const HEXAGON_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
490    // tidy-alphabetical-start
491    ("hvx", Unstable(sym::hexagon_target_feature), &[]),
492    ("hvx-length128b", Unstable(sym::hexagon_target_feature), &["hvx"]),
493    // tidy-alphabetical-end
494];
495
496static POWERPC_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
497    // tidy-alphabetical-start
498    ("altivec", Unstable(sym::powerpc_target_feature), &[]),
499    ("msync", Unstable(sym::powerpc_target_feature), &[]),
500    ("partword-atomics", Unstable(sym::powerpc_target_feature), &[]),
501    ("power8-altivec", Unstable(sym::powerpc_target_feature), &["altivec"]),
502    ("power8-crypto", Unstable(sym::powerpc_target_feature), &["power8-altivec"]),
503    ("power8-vector", Unstable(sym::powerpc_target_feature), &["vsx", "power8-altivec"]),
504    ("power9-altivec", Unstable(sym::powerpc_target_feature), &["power8-altivec"]),
505    ("power9-vector", Unstable(sym::powerpc_target_feature), &["power8-vector", "power9-altivec"]),
506    ("power10-vector", Unstable(sym::powerpc_target_feature), &["power9-vector"]),
507    ("quadword-atomics", Unstable(sym::powerpc_target_feature), &[]),
508    ("vsx", Unstable(sym::powerpc_target_feature), &["altivec"]),
509    // tidy-alphabetical-end
510];
511
512const MIPS_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
513    // tidy-alphabetical-start
514    ("fp64", Unstable(sym::mips_target_feature), &[]),
515    ("msa", Unstable(sym::mips_target_feature), &[]),
516    ("virt", Unstable(sym::mips_target_feature), &[]),
517    // tidy-alphabetical-end
518];
519
520static RISCV_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
521    // tidy-alphabetical-start
522    ("a", Stable, &["zaamo", "zalrsc"]),
523    ("b", Unstable(sym::riscv_target_feature), &["zba", "zbb", "zbs"]),
524    ("c", Stable, &["zca"]),
525    ("d", Unstable(sym::riscv_target_feature), &["f"]),
526    ("e", Unstable(sym::riscv_target_feature), &[]),
527    ("f", Unstable(sym::riscv_target_feature), &["zicsr"]),
528    (
529        "forced-atomics",
530        Stability::Forbidden { reason: "unsound because it changes the ABI of atomic operations" },
531        &[],
532    ),
533    ("m", Stable, &[]),
534    ("relax", Unstable(sym::riscv_target_feature), &[]),
535    ("unaligned-scalar-mem", Unstable(sym::riscv_target_feature), &[]),
536    ("unaligned-vector-mem", Unstable(sym::riscv_target_feature), &[]),
537    ("v", Unstable(sym::riscv_target_feature), &["zvl128b", "zve64d"]),
538    ("za64rs", Unstable(sym::riscv_target_feature), &["za128rs"]), // Za64rs ⊃ Za128rs
539    ("za128rs", Unstable(sym::riscv_target_feature), &[]),
540    ("zaamo", Unstable(sym::riscv_target_feature), &[]),
541    ("zabha", Unstable(sym::riscv_target_feature), &["zaamo"]),
542    ("zacas", Unstable(sym::riscv_target_feature), &["zaamo"]),
543    ("zalrsc", Unstable(sym::riscv_target_feature), &[]),
544    ("zama16b", Unstable(sym::riscv_target_feature), &[]),
545    ("zawrs", Unstable(sym::riscv_target_feature), &[]),
546    ("zba", Stable, &[]),
547    ("zbb", Stable, &[]),
548    ("zbc", Stable, &["zbkc"]), // Zbc ⊃ Zbkc
549    ("zbkb", Stable, &[]),
550    ("zbkc", Stable, &[]),
551    ("zbkx", Stable, &[]),
552    ("zbs", Stable, &[]),
553    ("zca", Unstable(sym::riscv_target_feature), &[]),
554    ("zcb", Unstable(sym::riscv_target_feature), &["zca"]),
555    ("zcmop", Unstable(sym::riscv_target_feature), &["zca"]),
556    ("zdinx", Unstable(sym::riscv_target_feature), &["zfinx"]),
557    ("zfa", Unstable(sym::riscv_target_feature), &["f"]),
558    ("zfbfmin", Unstable(sym::riscv_target_feature), &["f"]), // and a subset of Zfhmin
559    ("zfh", Unstable(sym::riscv_target_feature), &["zfhmin"]),
560    ("zfhmin", Unstable(sym::riscv_target_feature), &["f"]),
561    ("zfinx", Unstable(sym::riscv_target_feature), &["zicsr"]),
562    ("zhinx", Unstable(sym::riscv_target_feature), &["zhinxmin"]),
563    ("zhinxmin", Unstable(sym::riscv_target_feature), &["zfinx"]),
564    ("zic64b", Unstable(sym::riscv_target_feature), &[]),
565    ("zicbom", Unstable(sym::riscv_target_feature), &[]),
566    ("zicbop", Unstable(sym::riscv_target_feature), &[]),
567    ("zicboz", Unstable(sym::riscv_target_feature), &[]),
568    ("ziccamoa", Unstable(sym::riscv_target_feature), &[]),
569    ("ziccif", Unstable(sym::riscv_target_feature), &[]),
570    ("zicclsm", Unstable(sym::riscv_target_feature), &[]),
571    ("ziccrse", Unstable(sym::riscv_target_feature), &[]),
572    ("zicntr", Unstable(sym::riscv_target_feature), &["zicsr"]),
573    ("zicond", Unstable(sym::riscv_target_feature), &[]),
574    ("zicsr", Unstable(sym::riscv_target_feature), &[]),
575    ("zifencei", Unstable(sym::riscv_target_feature), &[]),
576    ("zihintntl", Unstable(sym::riscv_target_feature), &[]),
577    ("zihintpause", Unstable(sym::riscv_target_feature), &[]),
578    ("zihpm", Unstable(sym::riscv_target_feature), &["zicsr"]),
579    ("zimop", Unstable(sym::riscv_target_feature), &[]),
580    ("zk", Stable, &["zkn", "zkr", "zkt"]),
581    ("zkn", Stable, &["zbkb", "zbkc", "zbkx", "zkne", "zknd", "zknh"]),
582    ("zknd", Stable, &[]),
583    ("zkne", Stable, &[]),
584    ("zknh", Stable, &[]),
585    ("zkr", Stable, &[]),
586    ("zks", Stable, &["zbkb", "zbkc", "zbkx", "zksed", "zksh"]),
587    ("zksed", Stable, &[]),
588    ("zksh", Stable, &[]),
589    ("zkt", Stable, &[]),
590    ("ztso", Unstable(sym::riscv_target_feature), &[]),
591    ("zvbb", Unstable(sym::riscv_target_feature), &["zvkb"]), // Zvbb ⊃ Zvkb
592    ("zvbc", Unstable(sym::riscv_target_feature), &["zve64x"]),
593    ("zve32f", Unstable(sym::riscv_target_feature), &["zve32x", "f"]),
594    ("zve32x", Unstable(sym::riscv_target_feature), &["zvl32b", "zicsr"]),
595    ("zve64d", Unstable(sym::riscv_target_feature), &["zve64f", "d"]),
596    ("zve64f", Unstable(sym::riscv_target_feature), &["zve32f", "zve64x"]),
597    ("zve64x", Unstable(sym::riscv_target_feature), &["zve32x", "zvl64b"]),
598    ("zvfbfmin", Unstable(sym::riscv_target_feature), &["zve32f"]),
599    ("zvfbfwma", Unstable(sym::riscv_target_feature), &["zfbfmin", "zvfbfmin"]),
600    ("zvfh", Unstable(sym::riscv_target_feature), &["zvfhmin", "zve32f", "zfhmin"]), // Zvfh ⊃ Zvfhmin
601    ("zvfhmin", Unstable(sym::riscv_target_feature), &["zve32f"]),
602    ("zvkb", Unstable(sym::riscv_target_feature), &["zve32x"]),
603    ("zvkg", Unstable(sym::riscv_target_feature), &["zve32x"]),
604    ("zvkn", Unstable(sym::riscv_target_feature), &["zvkned", "zvknhb", "zvkb", "zvkt"]),
605    ("zvknc", Unstable(sym::riscv_target_feature), &["zvkn", "zvbc"]),
606    ("zvkned", Unstable(sym::riscv_target_feature), &["zve32x"]),
607    ("zvkng", Unstable(sym::riscv_target_feature), &["zvkn", "zvkg"]),
608    ("zvknha", Unstable(sym::riscv_target_feature), &["zve32x"]),
609    ("zvknhb", Unstable(sym::riscv_target_feature), &["zvknha", "zve64x"]), // Zvknhb ⊃ Zvknha
610    ("zvks", Unstable(sym::riscv_target_feature), &["zvksed", "zvksh", "zvkb", "zvkt"]),
611    ("zvksc", Unstable(sym::riscv_target_feature), &["zvks", "zvbc"]),
612    ("zvksed", Unstable(sym::riscv_target_feature), &["zve32x"]),
613    ("zvksg", Unstable(sym::riscv_target_feature), &["zvks", "zvkg"]),
614    ("zvksh", Unstable(sym::riscv_target_feature), &["zve32x"]),
615    ("zvkt", Unstable(sym::riscv_target_feature), &[]),
616    ("zvl32b", Unstable(sym::riscv_target_feature), &[]),
617    ("zvl64b", Unstable(sym::riscv_target_feature), &["zvl32b"]),
618    ("zvl128b", Unstable(sym::riscv_target_feature), &["zvl64b"]),
619    ("zvl256b", Unstable(sym::riscv_target_feature), &["zvl128b"]),
620    ("zvl512b", Unstable(sym::riscv_target_feature), &["zvl256b"]),
621    ("zvl1024b", Unstable(sym::riscv_target_feature), &["zvl512b"]),
622    ("zvl2048b", Unstable(sym::riscv_target_feature), &["zvl1024b"]),
623    ("zvl4096b", Unstable(sym::riscv_target_feature), &["zvl2048b"]),
624    ("zvl8192b", Unstable(sym::riscv_target_feature), &["zvl4096b"]),
625    ("zvl16384b", Unstable(sym::riscv_target_feature), &["zvl8192b"]),
626    ("zvl32768b", Unstable(sym::riscv_target_feature), &["zvl16384b"]),
627    ("zvl65536b", Unstable(sym::riscv_target_feature), &["zvl32768b"]),
628    // tidy-alphabetical-end
629];
630
631static WASM_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
632    // tidy-alphabetical-start
633    ("atomics", Unstable(sym::wasm_target_feature), &[]),
634    ("bulk-memory", Stable, &[]),
635    ("exception-handling", Unstable(sym::wasm_target_feature), &[]),
636    ("extended-const", Stable, &[]),
637    ("multivalue", Stable, &[]),
638    ("mutable-globals", Stable, &[]),
639    ("nontrapping-fptoint", Stable, &[]),
640    ("reference-types", Stable, &[]),
641    ("relaxed-simd", Stable, &["simd128"]),
642    ("sign-ext", Stable, &[]),
643    ("simd128", Stable, &[]),
644    ("tail-call", Stable, &[]),
645    ("wide-arithmetic", Unstable(sym::wasm_target_feature), &[]),
646    // tidy-alphabetical-end
647];
648
649const BPF_FEATURES: &[(&str, Stability, ImpliedFeatures)] =
650    &[("alu32", Unstable(sym::bpf_target_feature), &[])];
651
652static CSKY_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
653    // tidy-alphabetical-start
654    ("2e3", Unstable(sym::csky_target_feature), &["e2"]),
655    ("3e3r1", Unstable(sym::csky_target_feature), &[]),
656    ("3e3r2", Unstable(sym::csky_target_feature), &["3e3r1", "doloop"]),
657    ("3e3r3", Unstable(sym::csky_target_feature), &["doloop"]),
658    ("3e7", Unstable(sym::csky_target_feature), &["2e3"]),
659    ("7e10", Unstable(sym::csky_target_feature), &["3e7"]),
660    ("10e60", Unstable(sym::csky_target_feature), &["7e10"]),
661    ("cache", Unstable(sym::csky_target_feature), &[]),
662    ("doloop", Unstable(sym::csky_target_feature), &[]),
663    ("dsp1e2", Unstable(sym::csky_target_feature), &[]),
664    ("dspe60", Unstable(sym::csky_target_feature), &[]),
665    ("e1", Unstable(sym::csky_target_feature), &["elrw"]),
666    ("e2", Unstable(sym::csky_target_feature), &["e2"]),
667    ("edsp", Unstable(sym::csky_target_feature), &[]),
668    ("elrw", Unstable(sym::csky_target_feature), &[]),
669    ("float1e2", Unstable(sym::csky_target_feature), &[]),
670    ("float1e3", Unstable(sym::csky_target_feature), &[]),
671    ("float3e4", Unstable(sym::csky_target_feature), &[]),
672    ("float7e60", Unstable(sym::csky_target_feature), &[]),
673    ("floate1", Unstable(sym::csky_target_feature), &[]),
674    ("hard-tp", Unstable(sym::csky_target_feature), &[]),
675    ("high-registers", Unstable(sym::csky_target_feature), &[]),
676    ("hwdiv", Unstable(sym::csky_target_feature), &[]),
677    ("mp", Unstable(sym::csky_target_feature), &["2e3"]),
678    ("mp1e2", Unstable(sym::csky_target_feature), &["3e7"]),
679    ("nvic", Unstable(sym::csky_target_feature), &[]),
680    ("trust", Unstable(sym::csky_target_feature), &[]),
681    ("vdsp2e60f", Unstable(sym::csky_target_feature), &[]),
682    ("vdspv1", Unstable(sym::csky_target_feature), &[]),
683    ("vdspv2", Unstable(sym::csky_target_feature), &[]),
684    // tidy-alphabetical-end
685    //fpu
686    // tidy-alphabetical-start
687    ("fdivdu", Unstable(sym::csky_target_feature), &[]),
688    ("fpuv2_df", Unstable(sym::csky_target_feature), &[]),
689    ("fpuv2_sf", Unstable(sym::csky_target_feature), &[]),
690    ("fpuv3_df", Unstable(sym::csky_target_feature), &[]),
691    ("fpuv3_hf", Unstable(sym::csky_target_feature), &[]),
692    ("fpuv3_hi", Unstable(sym::csky_target_feature), &[]),
693    ("fpuv3_sf", Unstable(sym::csky_target_feature), &[]),
694    ("hard-float", Unstable(sym::csky_target_feature), &[]),
695    ("hard-float-abi", Unstable(sym::csky_target_feature), &[]),
696    // tidy-alphabetical-end
697];
698
699static LOONGARCH_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
700    // tidy-alphabetical-start
701    ("d", Stable, &["f"]),
702    ("div32", Unstable(sym::loongarch_target_feature), &[]),
703    ("f", Stable, &[]),
704    ("frecipe", Stable, &[]),
705    ("lam-bh", Unstable(sym::loongarch_target_feature), &[]),
706    ("lamcas", Unstable(sym::loongarch_target_feature), &[]),
707    ("lasx", Stable, &["lsx"]),
708    ("lbt", Stable, &[]),
709    ("ld-seq-sa", Unstable(sym::loongarch_target_feature), &[]),
710    ("lsx", Stable, &["d"]),
711    ("lvz", Stable, &[]),
712    ("relax", Unstable(sym::loongarch_target_feature), &[]),
713    ("scq", Unstable(sym::loongarch_target_feature), &[]),
714    ("ual", Unstable(sym::loongarch_target_feature), &[]),
715    // tidy-alphabetical-end
716];
717
718#[rustfmt::skip]
719const IBMZ_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
720    // tidy-alphabetical-start
721    // For "backchain", https://github.com/rust-lang/rust/issues/142412 is a stabilization blocker
722    ("backchain", Unstable(sym::s390x_target_feature), &[]),
723    ("concurrent-functions", Unstable(sym::s390x_target_feature), &[]),
724    ("deflate-conversion", Unstable(sym::s390x_target_feature), &[]),
725    ("enhanced-sort", Unstable(sym::s390x_target_feature), &[]),
726    ("guarded-storage", Unstable(sym::s390x_target_feature), &[]),
727    ("high-word", Unstable(sym::s390x_target_feature), &[]),
728    // LLVM does not define message-security-assist-extension versions 1, 2, 6, 10 and 11.
729    ("message-security-assist-extension3", Unstable(sym::s390x_target_feature), &[]),
730    ("message-security-assist-extension4", Unstable(sym::s390x_target_feature), &[]),
731    ("message-security-assist-extension5", Unstable(sym::s390x_target_feature), &[]),
732    ("message-security-assist-extension8", Unstable(sym::s390x_target_feature), &["message-security-assist-extension3"]),
733    ("message-security-assist-extension9", Unstable(sym::s390x_target_feature), &["message-security-assist-extension3", "message-security-assist-extension4"]),
734    ("message-security-assist-extension12", Unstable(sym::s390x_target_feature), &[]),
735    ("miscellaneous-extensions-2", Unstable(sym::s390x_target_feature), &[]),
736    ("miscellaneous-extensions-3", Unstable(sym::s390x_target_feature), &[]),
737    ("miscellaneous-extensions-4", Unstable(sym::s390x_target_feature), &[]),
738    ("nnp-assist", Unstable(sym::s390x_target_feature), &["vector"]),
739    ("transactional-execution", Unstable(sym::s390x_target_feature), &[]),
740    ("vector", Unstable(sym::s390x_target_feature), &[]),
741    ("vector-enhancements-1", Unstable(sym::s390x_target_feature), &["vector"]),
742    ("vector-enhancements-2", Unstable(sym::s390x_target_feature), &["vector-enhancements-1"]),
743    ("vector-enhancements-3", Unstable(sym::s390x_target_feature), &["vector-enhancements-2"]),
744    ("vector-packed-decimal", Unstable(sym::s390x_target_feature), &["vector"]),
745    ("vector-packed-decimal-enhancement", Unstable(sym::s390x_target_feature), &["vector-packed-decimal"]),
746    ("vector-packed-decimal-enhancement-2", Unstable(sym::s390x_target_feature), &["vector-packed-decimal-enhancement"]),
747    ("vector-packed-decimal-enhancement-3", Unstable(sym::s390x_target_feature), &["vector-packed-decimal-enhancement-2"]),
748    // tidy-alphabetical-end
749];
750
751const SPARC_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
752    // tidy-alphabetical-start
753    ("leoncasa", Unstable(sym::sparc_target_feature), &[]),
754    ("v8plus", Unstable(sym::sparc_target_feature), &[]),
755    ("v9", Unstable(sym::sparc_target_feature), &[]),
756    // tidy-alphabetical-end
757];
758
759static M68K_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
760    // tidy-alphabetical-start
761    ("isa-68000", Unstable(sym::m68k_target_feature), &[]),
762    ("isa-68010", Unstable(sym::m68k_target_feature), &["isa-68000"]),
763    ("isa-68020", Unstable(sym::m68k_target_feature), &["isa-68010"]),
764    ("isa-68030", Unstable(sym::m68k_target_feature), &["isa-68020"]),
765    ("isa-68040", Unstable(sym::m68k_target_feature), &["isa-68030", "isa-68882"]),
766    ("isa-68060", Unstable(sym::m68k_target_feature), &["isa-68040"]),
767    // FPU
768    ("isa-68881", Unstable(sym::m68k_target_feature), &[]),
769    ("isa-68882", Unstable(sym::m68k_target_feature), &["isa-68881"]),
770    // tidy-alphabetical-end
771];
772
773/// When rustdoc is running, provide a list of all known features so that all their respective
774/// primitives may be documented.
775///
776/// IMPORTANT: If you're adding another feature list above, make sure to add it to this iterator!
777pub fn all_rust_features() -> impl Iterator<Item = (&'static str, Stability)> {
778    std::iter::empty()
779        .chain(ARM_FEATURES.iter())
780        .chain(AARCH64_FEATURES.iter())
781        .chain(X86_FEATURES.iter())
782        .chain(HEXAGON_FEATURES.iter())
783        .chain(POWERPC_FEATURES.iter())
784        .chain(MIPS_FEATURES.iter())
785        .chain(RISCV_FEATURES.iter())
786        .chain(WASM_FEATURES.iter())
787        .chain(BPF_FEATURES.iter())
788        .chain(CSKY_FEATURES)
789        .chain(LOONGARCH_FEATURES)
790        .chain(IBMZ_FEATURES)
791        .chain(SPARC_FEATURES)
792        .chain(M68K_FEATURES)
793        .cloned()
794        .map(|(f, s, _)| (f, s))
795}
796
797// These arrays represent the least-constraining feature that is required for vector types up to a
798// certain size to have their "proper" ABI on each architecture.
799// Note that they must be kept sorted by vector size.
800const X86_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] =
801    &[(128, "sse"), (256, "avx"), (512, "avx512f")]; // FIXME: might need changes for AVX10.
802const AARCH64_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "neon")];
803
804// We might want to add "helium" too.
805const ARM_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "neon")];
806
807const POWERPC_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "altivec")];
808const WASM_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "simd128")];
809const S390X_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "vector")];
810const RISCV_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[
811    (32, "zvl32b"),
812    (64, "zvl64b"),
813    (128, "zvl128b"),
814    (256, "zvl256b"),
815    (512, "zvl512b"),
816    (1024, "zvl1024b"),
817    (2048, "zvl2048b"),
818    (4096, "zvl4096b"),
819    (8192, "zvl8192b"),
820    (16384, "zvl16384b"),
821    (32768, "zvl32768b"),
822    (65536, "zvl65536b"),
823];
824// Always error on SPARC, as the necessary target features cannot be enabled in Rust at the moment.
825const SPARC_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[/*(64, "vis")*/];
826
827const HEXAGON_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] =
828    &[/*(512, "hvx-length64b"),*/ (1024, "hvx-length128b")];
829const MIPS_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "msa")];
830const CSKY_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "vdspv1")];
831const LOONGARCH_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] =
832    &[(128, "lsx"), (256, "lasx")];
833
834#[derive(Copy, Clone, Debug)]
835pub struct FeatureConstraints {
836    /// Features that must be enabled.
837    pub required: &'static [&'static str],
838    /// Features that must be disabled.
839    pub incompatible: &'static [&'static str],
840}
841
842impl Target {
843    pub fn rust_target_features(&self) -> &'static [(&'static str, Stability, ImpliedFeatures)] {
844        match &*self.arch {
845            "arm" => ARM_FEATURES,
846            "aarch64" | "arm64ec" => AARCH64_FEATURES,
847            "x86" | "x86_64" => X86_FEATURES,
848            "hexagon" => HEXAGON_FEATURES,
849            "mips" | "mips32r6" | "mips64" | "mips64r6" => MIPS_FEATURES,
850            "powerpc" | "powerpc64" => POWERPC_FEATURES,
851            "riscv32" | "riscv64" => RISCV_FEATURES,
852            "wasm32" | "wasm64" => WASM_FEATURES,
853            "bpf" => BPF_FEATURES,
854            "csky" => CSKY_FEATURES,
855            "loongarch32" | "loongarch64" => LOONGARCH_FEATURES,
856            "s390x" => IBMZ_FEATURES,
857            "sparc" | "sparc64" => SPARC_FEATURES,
858            "m68k" => M68K_FEATURES,
859            _ => &[],
860        }
861    }
862
863    pub fn features_for_correct_vector_abi(&self) -> &'static [(u64, &'static str)] {
864        match &*self.arch {
865            "x86" | "x86_64" => X86_FEATURES_FOR_CORRECT_VECTOR_ABI,
866            "aarch64" | "arm64ec" => AARCH64_FEATURES_FOR_CORRECT_VECTOR_ABI,
867            "arm" => ARM_FEATURES_FOR_CORRECT_VECTOR_ABI,
868            "powerpc" | "powerpc64" => POWERPC_FEATURES_FOR_CORRECT_VECTOR_ABI,
869            "loongarch32" | "loongarch64" => LOONGARCH_FEATURES_FOR_CORRECT_VECTOR_ABI,
870            "riscv32" | "riscv64" => RISCV_FEATURES_FOR_CORRECT_VECTOR_ABI,
871            "wasm32" | "wasm64" => WASM_FEATURES_FOR_CORRECT_VECTOR_ABI,
872            "s390x" => S390X_FEATURES_FOR_CORRECT_VECTOR_ABI,
873            "sparc" | "sparc64" => SPARC_FEATURES_FOR_CORRECT_VECTOR_ABI,
874            "hexagon" => HEXAGON_FEATURES_FOR_CORRECT_VECTOR_ABI,
875            "mips" | "mips32r6" | "mips64" | "mips64r6" => MIPS_FEATURES_FOR_CORRECT_VECTOR_ABI,
876            "bpf" | "m68k" => &[], // no vector ABI
877            "csky" => CSKY_FEATURES_FOR_CORRECT_VECTOR_ABI,
878            // FIXME: for some tier3 targets, we are overly cautious and always give warnings
879            // when passing args in vector registers.
880            _ => &[],
881        }
882    }
883
884    pub fn tied_target_features(&self) -> &'static [&'static [&'static str]] {
885        match &*self.arch {
886            "aarch64" | "arm64ec" => AARCH64_TIED_FEATURES,
887            _ => &[],
888        }
889    }
890
891    // Note: the returned set includes `base_feature`.
892    pub fn implied_target_features<'a>(&self, base_feature: &'a str) -> FxHashSet<&'a str> {
893        let implied_features =
894            self.rust_target_features().iter().map(|(f, _, i)| (f, i)).collect::<FxHashMap<_, _>>();
895
896        // Implied target features have their own implied target features, so we traverse the
897        // map until there are no more features to add.
898        let mut features = FxHashSet::default();
899        let mut new_features = vec![base_feature];
900        while let Some(new_feature) = new_features.pop() {
901            if features.insert(new_feature) {
902                if let Some(implied_features) = implied_features.get(&new_feature) {
903                    new_features.extend(implied_features.iter().copied())
904                }
905            }
906        }
907        features
908    }
909
910    /// Returns two lists of features:
911    /// the first list contains target features that must be enabled for ABI reasons,
912    /// and the second list contains target feature that must be disabled for ABI reasons.
913    ///
914    /// These features are automatically appended to whatever the target spec sets as default
915    /// features for the target.
916    ///
917    /// All features enabled/disabled via `-Ctarget-features` and `#[target_features]` are checked
918    /// against this. We also check any implied features, based on the information above. If LLVM
919    /// implicitly enables more implied features than we do, that could bypass this check!
920    pub fn abi_required_features(&self) -> FeatureConstraints {
921        const NOTHING: FeatureConstraints = FeatureConstraints { required: &[], incompatible: &[] };
922        // Some architectures don't have a clean explicit ABI designation; instead, the ABI is
923        // defined by target features. When that is the case, those target features must be
924        // "forbidden" in the list above to ensure that there is a consistent answer to the
925        // questions "which ABI is used".
926        match &*self.arch {
927            "x86" => {
928                // We use our own ABI indicator here; LLVM does not have anything native.
929                // Every case should require or forbid `soft-float`!
930                match self.rustc_abi {
931                    None => {
932                        // Default hardfloat ABI.
933                        // x87 must be enabled, soft-float must be disabled.
934                        FeatureConstraints { required: &["x87"], incompatible: &["soft-float"] }
935                    }
936                    Some(RustcAbi::X86Sse2) => {
937                        // Extended hardfloat ABI. x87 and SSE2 must be enabled, soft-float must be disabled.
938                        FeatureConstraints {
939                            required: &["x87", "sse2"],
940                            incompatible: &["soft-float"],
941                        }
942                    }
943                    Some(RustcAbi::X86Softfloat) => {
944                        // Softfloat ABI, requires corresponding target feature. That feature trumps
945                        // `x87` and all other FPU features so those do not matter.
946                        // Note that this one requirement is the entire implementation of the ABI!
947                        // LLVM handles the rest.
948                        FeatureConstraints { required: &["soft-float"], incompatible: &[] }
949                    }
950                }
951            }
952            "x86_64" => {
953                // We use our own ABI indicator here; LLVM does not have anything native.
954                // Every case should require or forbid `soft-float`!
955                match self.rustc_abi {
956                    None => {
957                        // Default hardfloat ABI. On x86-64, this always includes SSE2.
958                        FeatureConstraints {
959                            required: &["x87", "sse2"],
960                            incompatible: &["soft-float"],
961                        }
962                    }
963                    Some(RustcAbi::X86Softfloat) => {
964                        // Softfloat ABI, requires corresponding target feature. That feature trumps
965                        // `x87` and all other FPU features so those do not matter.
966                        // Note that this one requirement is the entire implementation of the ABI!
967                        // LLVM handles the rest.
968                        FeatureConstraints { required: &["soft-float"], incompatible: &[] }
969                    }
970                    Some(r) => panic!("invalid Rust ABI for x86_64: {r:?}"),
971                }
972            }
973            "arm" => {
974                // On ARM, ABI handling is reasonably sane; we use `llvm_floatabi` to indicate
975                // to LLVM which ABI we are going for.
976                match self.llvm_floatabi.unwrap() {
977                    FloatAbi::Soft => {
978                        // Nothing special required, will use soft-float ABI throughout.
979                        // We can even allow `-soft-float` here; in fact that is useful as it lets
980                        // people use FPU instructions with a softfloat ABI (corresponds to
981                        // `-mfloat-abi=softfp` in GCC/clang).
982                        NOTHING
983                    }
984                    FloatAbi::Hard => {
985                        // Must have `fpregs` and must not have `soft-float`.
986                        FeatureConstraints { required: &["fpregs"], incompatible: &["soft-float"] }
987                    }
988                }
989            }
990            "aarch64" | "arm64ec" => {
991                // Aarch64 has no sane ABI specifier, and LLVM doesn't even have a way to force
992                // the use of soft-float, so all we can do here is some crude hacks.
993                match &*self.abi {
994                    "softfloat" => {
995                        // LLVM will use float registers when `fp-armv8` is available, e.g. for
996                        // calls to built-ins. The only way to ensure a consistent softfloat ABI
997                        // on aarch64 is to never enable `fp-armv8`, so we enforce that.
998                        // In Rust we tie `neon` and `fp-armv8` together, therefore `neon` is the
999                        // feature we have to mark as incompatible.
1000                        FeatureConstraints { required: &[], incompatible: &["neon"] }
1001                    }
1002                    _ => {
1003                        // Everything else is assumed to use a hardfloat ABI. neon and fp-armv8 must be enabled.
1004                        // `FeatureConstraints` uses Rust feature names, hence only "neon" shows up.
1005                        FeatureConstraints { required: &["neon"], incompatible: &[] }
1006                    }
1007                }
1008            }
1009            "riscv32" | "riscv64" => {
1010                // RISC-V handles ABI in a very sane way, being fully explicit via `llvm_abiname`
1011                // about what the intended ABI is.
1012                match &*self.llvm_abiname {
1013                    "ilp32d" | "lp64d" => {
1014                        // Requires d (which implies f), incompatible with e and zfinx.
1015                        FeatureConstraints { required: &["d"], incompatible: &["e", "zfinx"] }
1016                    }
1017                    "ilp32f" | "lp64f" => {
1018                        // Requires f, incompatible with e and zfinx.
1019                        FeatureConstraints { required: &["f"], incompatible: &["e", "zfinx"] }
1020                    }
1021                    "ilp32" | "lp64" => {
1022                        // Requires nothing, incompatible with e.
1023                        FeatureConstraints { required: &[], incompatible: &["e"] }
1024                    }
1025                    "ilp32e" => {
1026                        // ilp32e is documented to be incompatible with features that need aligned
1027                        // load/stores > 32 bits, like `d`. (One could also just generate more
1028                        // complicated code to align the stack when needed, but the RISCV
1029                        // architecture manual just explicitly rules out this combination so we
1030                        // might as well.)
1031                        // Note that the `e` feature is not required: the ABI treats the extra
1032                        // registers as caller-save, so it is safe to use them only in some parts of
1033                        // a program while the rest doesn't know they even exist.
1034                        FeatureConstraints { required: &[], incompatible: &["d"] }
1035                    }
1036                    "lp64e" => {
1037                        // As above, `e` is not required.
1038                        NOTHING
1039                    }
1040                    _ => unreachable!(),
1041                }
1042            }
1043            "loongarch32" | "loongarch64" => {
1044                // LoongArch handles ABI in a very sane way, being fully explicit via `llvm_abiname`
1045                // about what the intended ABI is.
1046                match &*self.llvm_abiname {
1047                    "ilp32d" | "lp64d" => {
1048                        // Requires d (which implies f), incompatible with nothing.
1049                        FeatureConstraints { required: &["d"], incompatible: &[] }
1050                    }
1051                    "ilp32f" | "lp64f" => {
1052                        // Requires f, incompatible with nothing.
1053                        FeatureConstraints { required: &["f"], incompatible: &[] }
1054                    }
1055                    "ilp32s" | "lp64s" => {
1056                        // The soft-float ABI does not require any features and is also not
1057                        // incompatible with any features. Rust targets explicitly specify the
1058                        // LLVM ABI names, which allows for enabling hard-float support even on
1059                        // soft-float targets, and ensures that the ABI behavior is as expected.
1060                        NOTHING
1061                    }
1062                    _ => unreachable!(),
1063                }
1064            }
1065            _ => NOTHING,
1066        }
1067    }
1068}