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    // Backend option to turn atomic operations into an intrinsic call when `lse` is not known to be
252    // available, so the intrinsic can do runtime LSE feature detection rather than unconditionally
253    // using slower non-LSE operations. Unstable since it doesn't need to user-togglable.
254    ("outline-atomics", Unstable(sym::aarch64_unstable_target_feature), &[]),
255    // FEAT_PAUTH (address authentication)
256    ("paca", Stable, &[]),
257    // FEAT_PAUTH (generic authentication)
258    ("pacg", Stable, &[]),
259    // FEAT_PAN
260    ("pan", Stable, &[]),
261    // FEAT_PAuth_LR
262    ("pauth-lr", Unstable(sym::aarch64_unstable_target_feature), &[]),
263    // FEAT_PMUv3
264    ("pmuv3", Stable, &[]),
265    // FEAT_RNG
266    ("rand", Stable, &[]),
267    // FEAT_RAS & FEAT_RASv1p1
268    ("ras", Stable, &[]),
269    // FEAT_LRCPC
270    ("rcpc", Stable, &[]),
271    // FEAT_LRCPC2
272    ("rcpc2", Stable, &["rcpc"]),
273    // FEAT_LRCPC3
274    ("rcpc3", Unstable(sym::aarch64_unstable_target_feature), &["rcpc2"]),
275    // FEAT_RDM
276    ("rdm", Stable, &["neon"]),
277    ("reserve-x18", Forbidden { reason: "use `-Zfixed-x18` compiler flag instead" }, &[]),
278    // FEAT_SB
279    ("sb", Stable, &[]),
280    // FEAT_SHA1 & FEAT_SHA256
281    ("sha2", Stable, &["neon"]),
282    // FEAT_SHA512 & FEAT_SHA3
283    ("sha3", Stable, &["sha2"]),
284    // FEAT_SM3 & FEAT_SM4
285    ("sm4", Stable, &["neon"]),
286    // FEAT_SME
287    ("sme", Unstable(sym::aarch64_unstable_target_feature), &["bf16"]),
288    // FEAT_SME_B16B16
289    ("sme-b16b16", Unstable(sym::aarch64_unstable_target_feature), &["bf16", "sme2", "sve-b16b16"]),
290    // FEAT_SME_F8F16
291    ("sme-f8f16", Unstable(sym::aarch64_unstable_target_feature), &["sme-f8f32"]),
292    // FEAT_SME_F8F32
293    ("sme-f8f32", Unstable(sym::aarch64_unstable_target_feature), &["sme2", "fp8"]),
294    // FEAT_SME_F16F16
295    ("sme-f16f16", Unstable(sym::aarch64_unstable_target_feature), &["sme2"]),
296    // FEAT_SME_F64F64
297    ("sme-f64f64", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
298    // FEAT_SME_FA64
299    ("sme-fa64", Unstable(sym::aarch64_unstable_target_feature), &["sme", "sve2"]),
300    // FEAT_SME_I16I64
301    ("sme-i16i64", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
302    // FEAT_SME_LUTv2
303    ("sme-lutv2", Unstable(sym::aarch64_unstable_target_feature), &[]),
304    // FEAT_SME2
305    ("sme2", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
306    // FEAT_SME2p1
307    ("sme2p1", Unstable(sym::aarch64_unstable_target_feature), &["sme2"]),
308    // FEAT_SPE
309    ("spe", Stable, &[]),
310    // FEAT_SSBS & FEAT_SSBS2
311    ("ssbs", Stable, &[]),
312    // FEAT_SSVE_FP8FDOT2
313    ("ssve-fp8dot2", Unstable(sym::aarch64_unstable_target_feature), &["ssve-fp8dot4"]),
314    // FEAT_SSVE_FP8FDOT4
315    ("ssve-fp8dot4", Unstable(sym::aarch64_unstable_target_feature), &["ssve-fp8fma"]),
316    // FEAT_SSVE_FP8FMA
317    ("ssve-fp8fma", Unstable(sym::aarch64_unstable_target_feature), &["sme2", "fp8"]),
318    // FEAT_SVE
319    // It was decided that SVE requires Neon: https://github.com/rust-lang/rust/pull/91608
320    //
321    // LLVM doesn't enable Neon for SVE. ARM indicates that they're separate, but probably always
322    // exist together: https://developer.arm.com/documentation/102340/0100/New-features-in-SVE2
323    //
324    // "For backwards compatibility, Neon and VFP are required in the latest architectures."
325    ("sve", Stable, &["neon"]),
326    // FEAT_SVE_B16B16 (SVE or SME Z-targeting instructions)
327    ("sve-b16b16", Unstable(sym::aarch64_unstable_target_feature), &["bf16"]),
328    // FEAT_SVE2
329    ("sve2", Stable, &["sve"]),
330    // FEAT_SVE_AES & FEAT_SVE_PMULL128
331    ("sve2-aes", Stable, &["sve2", "aes"]),
332    // FEAT_SVE2_BitPerm
333    ("sve2-bitperm", Stable, &["sve2"]),
334    // FEAT_SVE2_SHA3
335    ("sve2-sha3", Stable, &["sve2", "sha3"]),
336    // FEAT_SVE2_SM4
337    ("sve2-sm4", Stable, &["sve2", "sm4"]),
338    // FEAT_SVE2p1
339    ("sve2p1", Unstable(sym::aarch64_unstable_target_feature), &["sve2"]),
340    // FEAT_TME
341    ("tme", Stable, &[]),
342    (
343        "v8.1a",
344        Unstable(sym::aarch64_ver_target_feature),
345        &["crc", "lse", "rdm", "pan", "lor", "vh"],
346    ),
347    ("v8.2a", Unstable(sym::aarch64_ver_target_feature), &["v8.1a", "ras", "dpb"]),
348    (
349        "v8.3a",
350        Unstable(sym::aarch64_ver_target_feature),
351        &["v8.2a", "rcpc", "paca", "pacg", "jsconv"],
352    ),
353    ("v8.4a", Unstable(sym::aarch64_ver_target_feature), &["v8.3a", "dotprod", "dit", "flagm"]),
354    ("v8.5a", Unstable(sym::aarch64_ver_target_feature), &["v8.4a", "ssbs", "sb", "dpb2", "bti"]),
355    ("v8.6a", Unstable(sym::aarch64_ver_target_feature), &["v8.5a", "bf16", "i8mm"]),
356    ("v8.7a", Unstable(sym::aarch64_ver_target_feature), &["v8.6a", "wfxt"]),
357    ("v8.8a", Unstable(sym::aarch64_ver_target_feature), &["v8.7a", "hbc", "mops"]),
358    ("v8.9a", Unstable(sym::aarch64_ver_target_feature), &["v8.8a", "cssc"]),
359    ("v9.1a", Unstable(sym::aarch64_ver_target_feature), &["v9a", "v8.6a"]),
360    ("v9.2a", Unstable(sym::aarch64_ver_target_feature), &["v9.1a", "v8.7a"]),
361    ("v9.3a", Unstable(sym::aarch64_ver_target_feature), &["v9.2a", "v8.8a"]),
362    ("v9.4a", Unstable(sym::aarch64_ver_target_feature), &["v9.3a", "v8.9a"]),
363    ("v9.5a", Unstable(sym::aarch64_ver_target_feature), &["v9.4a"]),
364    ("v9a", Unstable(sym::aarch64_ver_target_feature), &["v8.5a", "sve2"]),
365    // FEAT_VHE
366    ("vh", Stable, &[]),
367    // FEAT_WFxT
368    ("wfxt", Unstable(sym::aarch64_unstable_target_feature), &[]),
369    // tidy-alphabetical-end
370];
371
372const AARCH64_TIED_FEATURES: &[&[&str]] = &[
373    &["paca", "pacg"], // Together these represent `pauth` in LLVM
374];
375
376static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
377    // tidy-alphabetical-start
378    ("adx", Stable, &[]),
379    ("aes", Stable, &["sse2"]),
380    ("amx-avx512", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
381    ("amx-bf16", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
382    ("amx-complex", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
383    ("amx-fp8", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
384    ("amx-fp16", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
385    ("amx-int8", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
386    ("amx-movrs", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
387    ("amx-tf32", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
388    ("amx-tile", Unstable(sym::x86_amx_intrinsics), &[]),
389    ("amx-transpose", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
390    ("apxf", Unstable(sym::apx_target_feature), &[]),
391    ("avx", Stable, &["sse4.2"]),
392    ("avx2", Stable, &["avx"]),
393    (
394        "avx10.1",
395        Unstable(sym::avx10_target_feature),
396        &[
397            "avx512bf16",
398            "avx512bitalg",
399            "avx512bw",
400            "avx512cd",
401            "avx512dq",
402            "avx512f",
403            "avx512fp16",
404            "avx512ifma",
405            "avx512vbmi",
406            "avx512vbmi2",
407            "avx512vl",
408            "avx512vnni",
409            "avx512vpopcntdq",
410        ],
411    ),
412    ("avx10.2", Unstable(sym::avx10_target_feature), &["avx10.1"]),
413    ("avx512bf16", Stable, &["avx512bw"]),
414    ("avx512bitalg", Stable, &["avx512bw"]),
415    ("avx512bw", Stable, &["avx512f"]),
416    ("avx512cd", Stable, &["avx512f"]),
417    ("avx512dq", Stable, &["avx512f"]),
418    ("avx512f", Stable, &["avx2", "fma", "f16c"]),
419    ("avx512fp16", Stable, &["avx512bw"]),
420    ("avx512ifma", Stable, &["avx512f"]),
421    ("avx512vbmi", Stable, &["avx512bw"]),
422    ("avx512vbmi2", Stable, &["avx512bw"]),
423    ("avx512vl", Stable, &["avx512f"]),
424    ("avx512vnni", Stable, &["avx512f"]),
425    ("avx512vp2intersect", Stable, &["avx512f"]),
426    ("avx512vpopcntdq", Stable, &["avx512f"]),
427    ("avxifma", Stable, &["avx2"]),
428    ("avxneconvert", Stable, &["avx2"]),
429    ("avxvnni", Stable, &["avx2"]),
430    ("avxvnniint8", Stable, &["avx2"]),
431    ("avxvnniint16", Stable, &["avx2"]),
432    ("bmi1", Stable, &[]),
433    ("bmi2", Stable, &[]),
434    ("cmpxchg16b", Stable, &[]),
435    ("ermsb", Unstable(sym::ermsb_target_feature), &[]),
436    ("f16c", Stable, &["avx"]),
437    ("fma", Stable, &["avx"]),
438    ("fxsr", Stable, &[]),
439    ("gfni", Stable, &["sse2"]),
440    ("kl", Stable, &["sse2"]),
441    ("lahfsahf", Unstable(sym::lahfsahf_target_feature), &[]),
442    ("lzcnt", Stable, &[]),
443    ("movbe", Stable, &[]),
444    ("movrs", Unstable(sym::movrs_target_feature), &[]),
445    ("pclmulqdq", Stable, &["sse2"]),
446    ("popcnt", Stable, &[]),
447    ("prfchw", Unstable(sym::prfchw_target_feature), &[]),
448    ("rdrand", Stable, &[]),
449    ("rdseed", Stable, &[]),
450    (
451        "retpoline-external-thunk",
452        Stability::Forbidden { reason: "use `-Zretpoline-external-thunk` compiler flag instead" },
453        &[],
454    ),
455    (
456        "retpoline-indirect-branches",
457        Stability::Forbidden { reason: "use `-Zretpoline` compiler flag instead" },
458        &[],
459    ),
460    (
461        "retpoline-indirect-calls",
462        Stability::Forbidden { reason: "use `-Zretpoline` compiler flag instead" },
463        &[],
464    ),
465    ("rtm", Unstable(sym::rtm_target_feature), &[]),
466    ("sha", Stable, &["sse2"]),
467    ("sha512", Stable, &["avx2"]),
468    ("sm3", Stable, &["avx"]),
469    ("sm4", Stable, &["avx2"]),
470    // This cannot actually be toggled, the ABI always fixes it, so it'd make little sense to
471    // stabilize. It must be in this list for the ABI check to be able to use it.
472    ("soft-float", Stability::Unstable(sym::x87_target_feature), &[]),
473    ("sse", Stable, &[]),
474    ("sse2", Stable, &["sse"]),
475    ("sse3", Stable, &["sse2"]),
476    ("sse4.1", Stable, &["ssse3"]),
477    ("sse4.2", Stable, &["sse4.1"]),
478    ("sse4a", Stable, &["sse3"]),
479    ("ssse3", Stable, &["sse3"]),
480    ("tbm", Stable, &[]),
481    ("vaes", Stable, &["avx2", "aes"]),
482    ("vpclmulqdq", Stable, &["avx", "pclmulqdq"]),
483    ("widekl", Stable, &["kl"]),
484    ("x87", Unstable(sym::x87_target_feature), &[]),
485    ("xop", Unstable(sym::xop_target_feature), &[/*"fma4", */ "avx", "sse4a"]),
486    ("xsave", Stable, &[]),
487    ("xsavec", Stable, &["xsave"]),
488    ("xsaveopt", Stable, &["xsave"]),
489    ("xsaves", Stable, &["xsave"]),
490    // tidy-alphabetical-end
491];
492
493const HEXAGON_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
494    // tidy-alphabetical-start
495    ("hvx", Unstable(sym::hexagon_target_feature), &[]),
496    ("hvx-length128b", Unstable(sym::hexagon_target_feature), &["hvx"]),
497    // tidy-alphabetical-end
498];
499
500static POWERPC_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
501    // tidy-alphabetical-start
502    ("altivec", Unstable(sym::powerpc_target_feature), &[]),
503    ("msync", Unstable(sym::powerpc_target_feature), &[]),
504    ("partword-atomics", Unstable(sym::powerpc_target_feature), &[]),
505    ("power8-altivec", Unstable(sym::powerpc_target_feature), &["altivec"]),
506    ("power8-crypto", Unstable(sym::powerpc_target_feature), &["power8-altivec"]),
507    ("power8-vector", Unstable(sym::powerpc_target_feature), &["vsx", "power8-altivec"]),
508    ("power9-altivec", Unstable(sym::powerpc_target_feature), &["power8-altivec"]),
509    ("power9-vector", Unstable(sym::powerpc_target_feature), &["power8-vector", "power9-altivec"]),
510    ("power10-vector", Unstable(sym::powerpc_target_feature), &["power9-vector"]),
511    ("quadword-atomics", Unstable(sym::powerpc_target_feature), &[]),
512    ("vsx", Unstable(sym::powerpc_target_feature), &["altivec"]),
513    // tidy-alphabetical-end
514];
515
516const MIPS_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
517    // tidy-alphabetical-start
518    ("fp64", Unstable(sym::mips_target_feature), &[]),
519    ("msa", Unstable(sym::mips_target_feature), &[]),
520    ("virt", Unstable(sym::mips_target_feature), &[]),
521    // tidy-alphabetical-end
522];
523
524const NVPTX_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
525    // tidy-alphabetical-start
526    ("sm_20", Unstable(sym::nvptx_target_feature), &[]),
527    ("sm_21", Unstable(sym::nvptx_target_feature), &["sm_20"]),
528    ("sm_30", Unstable(sym::nvptx_target_feature), &["sm_21"]),
529    ("sm_32", Unstable(sym::nvptx_target_feature), &["sm_30"]),
530    ("sm_35", Unstable(sym::nvptx_target_feature), &["sm_32"]),
531    ("sm_37", Unstable(sym::nvptx_target_feature), &["sm_35"]),
532    ("sm_50", Unstable(sym::nvptx_target_feature), &["sm_37"]),
533    ("sm_52", Unstable(sym::nvptx_target_feature), &["sm_50"]),
534    ("sm_53", Unstable(sym::nvptx_target_feature), &["sm_52"]),
535    ("sm_60", Unstable(sym::nvptx_target_feature), &["sm_53"]),
536    ("sm_61", Unstable(sym::nvptx_target_feature), &["sm_60"]),
537    ("sm_62", Unstable(sym::nvptx_target_feature), &["sm_61"]),
538    ("sm_70", Unstable(sym::nvptx_target_feature), &["sm_62"]),
539    ("sm_72", Unstable(sym::nvptx_target_feature), &["sm_70"]),
540    ("sm_75", Unstable(sym::nvptx_target_feature), &["sm_72"]),
541    ("sm_80", Unstable(sym::nvptx_target_feature), &["sm_75"]),
542    ("sm_86", Unstable(sym::nvptx_target_feature), &["sm_80"]),
543    ("sm_87", Unstable(sym::nvptx_target_feature), &["sm_86"]),
544    ("sm_89", Unstable(sym::nvptx_target_feature), &["sm_87"]),
545    ("sm_90", Unstable(sym::nvptx_target_feature), &["sm_89"]),
546    ("sm_90a", Unstable(sym::nvptx_target_feature), &["sm_90"]),
547    // tidy-alphabetical-end
548    // tidy-alphabetical-start
549    ("sm_100", Unstable(sym::nvptx_target_feature), &["sm_90"]),
550    ("sm_100a", Unstable(sym::nvptx_target_feature), &["sm_100"]),
551    ("sm_101", Unstable(sym::nvptx_target_feature), &["sm_100"]),
552    ("sm_101a", Unstable(sym::nvptx_target_feature), &["sm_101"]),
553    ("sm_120", Unstable(sym::nvptx_target_feature), &["sm_101"]),
554    ("sm_120a", Unstable(sym::nvptx_target_feature), &["sm_120"]),
555    // tidy-alphabetical-end
556    // tidy-alphabetical-start
557    ("ptx32", Unstable(sym::nvptx_target_feature), &[]),
558    ("ptx40", Unstable(sym::nvptx_target_feature), &["ptx32"]),
559    ("ptx41", Unstable(sym::nvptx_target_feature), &["ptx40"]),
560    ("ptx42", Unstable(sym::nvptx_target_feature), &["ptx41"]),
561    ("ptx43", Unstable(sym::nvptx_target_feature), &["ptx42"]),
562    ("ptx50", Unstable(sym::nvptx_target_feature), &["ptx43"]),
563    ("ptx60", Unstable(sym::nvptx_target_feature), &["ptx50"]),
564    ("ptx61", Unstable(sym::nvptx_target_feature), &["ptx60"]),
565    ("ptx62", Unstable(sym::nvptx_target_feature), &["ptx61"]),
566    ("ptx63", Unstable(sym::nvptx_target_feature), &["ptx62"]),
567    ("ptx64", Unstable(sym::nvptx_target_feature), &["ptx63"]),
568    ("ptx65", Unstable(sym::nvptx_target_feature), &["ptx64"]),
569    ("ptx70", Unstable(sym::nvptx_target_feature), &["ptx65"]),
570    ("ptx71", Unstable(sym::nvptx_target_feature), &["ptx70"]),
571    ("ptx72", Unstable(sym::nvptx_target_feature), &["ptx71"]),
572    ("ptx73", Unstable(sym::nvptx_target_feature), &["ptx72"]),
573    ("ptx74", Unstable(sym::nvptx_target_feature), &["ptx73"]),
574    ("ptx75", Unstable(sym::nvptx_target_feature), &["ptx74"]),
575    ("ptx76", Unstable(sym::nvptx_target_feature), &["ptx75"]),
576    ("ptx77", Unstable(sym::nvptx_target_feature), &["ptx76"]),
577    ("ptx78", Unstable(sym::nvptx_target_feature), &["ptx77"]),
578    ("ptx80", Unstable(sym::nvptx_target_feature), &["ptx78"]),
579    ("ptx81", Unstable(sym::nvptx_target_feature), &["ptx80"]),
580    ("ptx82", Unstable(sym::nvptx_target_feature), &["ptx81"]),
581    ("ptx83", Unstable(sym::nvptx_target_feature), &["ptx82"]),
582    ("ptx84", Unstable(sym::nvptx_target_feature), &["ptx83"]),
583    ("ptx85", Unstable(sym::nvptx_target_feature), &["ptx84"]),
584    ("ptx86", Unstable(sym::nvptx_target_feature), &["ptx85"]),
585    ("ptx87", Unstable(sym::nvptx_target_feature), &["ptx86"]),
586    // tidy-alphabetical-end
587];
588
589static RISCV_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
590    // tidy-alphabetical-start
591    ("a", Stable, &["zaamo", "zalrsc"]),
592    ("b", Unstable(sym::riscv_target_feature), &["zba", "zbb", "zbs"]),
593    ("c", Stable, &["zca"]),
594    ("d", Unstable(sym::riscv_target_feature), &["f"]),
595    ("e", Unstable(sym::riscv_target_feature), &[]),
596    ("f", Unstable(sym::riscv_target_feature), &["zicsr"]),
597    (
598        "forced-atomics",
599        Stability::Forbidden { reason: "unsound because it changes the ABI of atomic operations" },
600        &[],
601    ),
602    ("m", Stable, &[]),
603    ("relax", Unstable(sym::riscv_target_feature), &[]),
604    (
605        "rva23u64",
606        Unstable(sym::riscv_target_feature),
607        &[
608            "m",
609            "a",
610            "f",
611            "d",
612            "c",
613            "b",
614            "v",
615            "zicsr",
616            "zicntr",
617            "zihpm",
618            "ziccif",
619            "ziccrse",
620            "ziccamoa",
621            "zicclsm",
622            "zic64b",
623            "za64rs",
624            "zihintpause",
625            "zba",
626            "zbb",
627            "zbs",
628            "zicbom",
629            "zicbop",
630            "zicboz",
631            "zfhmin",
632            "zkt",
633            "zvfhmin",
634            "zvbb",
635            "zvkt",
636            "zihintntl",
637            "zicond",
638            "zimop",
639            "zcmop",
640            "zcb",
641            "zfa",
642            "zawrs",
643            "supm",
644        ],
645    ),
646    ("supm", Unstable(sym::riscv_target_feature), &[]),
647    ("unaligned-scalar-mem", Unstable(sym::riscv_target_feature), &[]),
648    ("unaligned-vector-mem", Unstable(sym::riscv_target_feature), &[]),
649    ("v", Unstable(sym::riscv_target_feature), &["zvl128b", "zve64d"]),
650    ("za64rs", Unstable(sym::riscv_target_feature), &["za128rs"]), // Za64rs ⊃ Za128rs
651    ("za128rs", Unstable(sym::riscv_target_feature), &[]),
652    ("zaamo", Unstable(sym::riscv_target_feature), &[]),
653    ("zabha", Unstable(sym::riscv_target_feature), &["zaamo"]),
654    ("zacas", Unstable(sym::riscv_target_feature), &["zaamo"]),
655    ("zalrsc", Unstable(sym::riscv_target_feature), &[]),
656    ("zama16b", Unstable(sym::riscv_target_feature), &[]),
657    ("zawrs", Unstable(sym::riscv_target_feature), &[]),
658    ("zba", Stable, &[]),
659    ("zbb", Stable, &[]),
660    ("zbc", Stable, &["zbkc"]), // Zbc ⊃ Zbkc
661    ("zbkb", Stable, &[]),
662    ("zbkc", Stable, &[]),
663    ("zbkx", Stable, &[]),
664    ("zbs", Stable, &[]),
665    ("zca", Unstable(sym::riscv_target_feature), &[]),
666    ("zcb", Unstable(sym::riscv_target_feature), &["zca"]),
667    ("zcmop", Unstable(sym::riscv_target_feature), &["zca"]),
668    ("zdinx", Unstable(sym::riscv_target_feature), &["zfinx"]),
669    ("zfa", Unstable(sym::riscv_target_feature), &["f"]),
670    ("zfbfmin", Unstable(sym::riscv_target_feature), &["f"]), // and a subset of Zfhmin
671    ("zfh", Unstable(sym::riscv_target_feature), &["zfhmin"]),
672    ("zfhmin", Unstable(sym::riscv_target_feature), &["f"]),
673    ("zfinx", Unstable(sym::riscv_target_feature), &["zicsr"]),
674    ("zhinx", Unstable(sym::riscv_target_feature), &["zhinxmin"]),
675    ("zhinxmin", Unstable(sym::riscv_target_feature), &["zfinx"]),
676    ("zic64b", Unstable(sym::riscv_target_feature), &[]),
677    ("zicbom", Unstable(sym::riscv_target_feature), &[]),
678    ("zicbop", Unstable(sym::riscv_target_feature), &[]),
679    ("zicboz", Unstable(sym::riscv_target_feature), &[]),
680    ("ziccamoa", Unstable(sym::riscv_target_feature), &[]),
681    ("ziccif", Unstable(sym::riscv_target_feature), &[]),
682    ("zicclsm", Unstable(sym::riscv_target_feature), &[]),
683    ("ziccrse", Unstable(sym::riscv_target_feature), &[]),
684    ("zicntr", Unstable(sym::riscv_target_feature), &["zicsr"]),
685    ("zicond", Unstable(sym::riscv_target_feature), &[]),
686    ("zicsr", Unstable(sym::riscv_target_feature), &[]),
687    ("zifencei", Unstable(sym::riscv_target_feature), &[]),
688    ("zihintntl", Unstable(sym::riscv_target_feature), &[]),
689    ("zihintpause", Unstable(sym::riscv_target_feature), &[]),
690    ("zihpm", Unstable(sym::riscv_target_feature), &["zicsr"]),
691    ("zimop", Unstable(sym::riscv_target_feature), &[]),
692    ("zk", Stable, &["zkn", "zkr", "zkt"]),
693    ("zkn", Stable, &["zbkb", "zbkc", "zbkx", "zkne", "zknd", "zknh"]),
694    ("zknd", Stable, &[]),
695    ("zkne", Stable, &[]),
696    ("zknh", Stable, &[]),
697    ("zkr", Stable, &[]),
698    ("zks", Stable, &["zbkb", "zbkc", "zbkx", "zksed", "zksh"]),
699    ("zksed", Stable, &[]),
700    ("zksh", Stable, &[]),
701    ("zkt", Stable, &[]),
702    ("ztso", Unstable(sym::riscv_target_feature), &[]),
703    ("zvbb", Unstable(sym::riscv_target_feature), &["zvkb"]), // Zvbb ⊃ Zvkb
704    ("zvbc", Unstable(sym::riscv_target_feature), &["zve64x"]),
705    ("zve32f", Unstable(sym::riscv_target_feature), &["zve32x", "f"]),
706    ("zve32x", Unstable(sym::riscv_target_feature), &["zvl32b", "zicsr"]),
707    ("zve64d", Unstable(sym::riscv_target_feature), &["zve64f", "d"]),
708    ("zve64f", Unstable(sym::riscv_target_feature), &["zve32f", "zve64x"]),
709    ("zve64x", Unstable(sym::riscv_target_feature), &["zve32x", "zvl64b"]),
710    ("zvfbfmin", Unstable(sym::riscv_target_feature), &["zve32f"]),
711    ("zvfbfwma", Unstable(sym::riscv_target_feature), &["zfbfmin", "zvfbfmin"]),
712    ("zvfh", Unstable(sym::riscv_target_feature), &["zvfhmin", "zve32f", "zfhmin"]), // Zvfh ⊃ Zvfhmin
713    ("zvfhmin", Unstable(sym::riscv_target_feature), &["zve32f"]),
714    ("zvkb", Unstable(sym::riscv_target_feature), &["zve32x"]),
715    ("zvkg", Unstable(sym::riscv_target_feature), &["zve32x"]),
716    ("zvkn", Unstable(sym::riscv_target_feature), &["zvkned", "zvknhb", "zvkb", "zvkt"]),
717    ("zvknc", Unstable(sym::riscv_target_feature), &["zvkn", "zvbc"]),
718    ("zvkned", Unstable(sym::riscv_target_feature), &["zve32x"]),
719    ("zvkng", Unstable(sym::riscv_target_feature), &["zvkn", "zvkg"]),
720    ("zvknha", Unstable(sym::riscv_target_feature), &["zve32x"]),
721    ("zvknhb", Unstable(sym::riscv_target_feature), &["zvknha", "zve64x"]), // Zvknhb ⊃ Zvknha
722    ("zvks", Unstable(sym::riscv_target_feature), &["zvksed", "zvksh", "zvkb", "zvkt"]),
723    ("zvksc", Unstable(sym::riscv_target_feature), &["zvks", "zvbc"]),
724    ("zvksed", Unstable(sym::riscv_target_feature), &["zve32x"]),
725    ("zvksg", Unstable(sym::riscv_target_feature), &["zvks", "zvkg"]),
726    ("zvksh", Unstable(sym::riscv_target_feature), &["zve32x"]),
727    ("zvkt", Unstable(sym::riscv_target_feature), &[]),
728    ("zvl32b", Unstable(sym::riscv_target_feature), &[]),
729    ("zvl64b", Unstable(sym::riscv_target_feature), &["zvl32b"]),
730    ("zvl128b", Unstable(sym::riscv_target_feature), &["zvl64b"]),
731    ("zvl256b", Unstable(sym::riscv_target_feature), &["zvl128b"]),
732    ("zvl512b", Unstable(sym::riscv_target_feature), &["zvl256b"]),
733    ("zvl1024b", Unstable(sym::riscv_target_feature), &["zvl512b"]),
734    ("zvl2048b", Unstable(sym::riscv_target_feature), &["zvl1024b"]),
735    ("zvl4096b", Unstable(sym::riscv_target_feature), &["zvl2048b"]),
736    ("zvl8192b", Unstable(sym::riscv_target_feature), &["zvl4096b"]),
737    ("zvl16384b", Unstable(sym::riscv_target_feature), &["zvl8192b"]),
738    ("zvl32768b", Unstable(sym::riscv_target_feature), &["zvl16384b"]),
739    ("zvl65536b", Unstable(sym::riscv_target_feature), &["zvl32768b"]),
740    // tidy-alphabetical-end
741];
742
743static WASM_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
744    // tidy-alphabetical-start
745    ("atomics", Unstable(sym::wasm_target_feature), &[]),
746    ("bulk-memory", Stable, &[]),
747    ("exception-handling", Unstable(sym::wasm_target_feature), &[]),
748    ("extended-const", Stable, &[]),
749    ("multivalue", Stable, &[]),
750    ("mutable-globals", Stable, &[]),
751    ("nontrapping-fptoint", Stable, &[]),
752    ("reference-types", Stable, &[]),
753    ("relaxed-simd", Stable, &["simd128"]),
754    ("sign-ext", Stable, &[]),
755    ("simd128", Stable, &[]),
756    ("tail-call", Stable, &[]),
757    ("wide-arithmetic", Unstable(sym::wasm_target_feature), &[]),
758    // tidy-alphabetical-end
759];
760
761const BPF_FEATURES: &[(&str, Stability, ImpliedFeatures)] =
762    &[("alu32", Unstable(sym::bpf_target_feature), &[])];
763
764static CSKY_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
765    // tidy-alphabetical-start
766    ("2e3", Unstable(sym::csky_target_feature), &["e2"]),
767    ("3e3r1", Unstable(sym::csky_target_feature), &[]),
768    ("3e3r2", Unstable(sym::csky_target_feature), &["3e3r1", "doloop"]),
769    ("3e3r3", Unstable(sym::csky_target_feature), &["doloop"]),
770    ("3e7", Unstable(sym::csky_target_feature), &["2e3"]),
771    ("7e10", Unstable(sym::csky_target_feature), &["3e7"]),
772    ("10e60", Unstable(sym::csky_target_feature), &["7e10"]),
773    ("cache", Unstable(sym::csky_target_feature), &[]),
774    ("doloop", Unstable(sym::csky_target_feature), &[]),
775    ("dsp1e2", Unstable(sym::csky_target_feature), &[]),
776    ("dspe60", Unstable(sym::csky_target_feature), &[]),
777    ("e1", Unstable(sym::csky_target_feature), &["elrw"]),
778    ("e2", Unstable(sym::csky_target_feature), &["e2"]),
779    ("edsp", Unstable(sym::csky_target_feature), &[]),
780    ("elrw", Unstable(sym::csky_target_feature), &[]),
781    ("float1e2", Unstable(sym::csky_target_feature), &[]),
782    ("float1e3", Unstable(sym::csky_target_feature), &[]),
783    ("float3e4", Unstable(sym::csky_target_feature), &[]),
784    ("float7e60", Unstable(sym::csky_target_feature), &[]),
785    ("floate1", Unstable(sym::csky_target_feature), &[]),
786    ("hard-tp", Unstable(sym::csky_target_feature), &[]),
787    ("high-registers", Unstable(sym::csky_target_feature), &[]),
788    ("hwdiv", Unstable(sym::csky_target_feature), &[]),
789    ("mp", Unstable(sym::csky_target_feature), &["2e3"]),
790    ("mp1e2", Unstable(sym::csky_target_feature), &["3e7"]),
791    ("nvic", Unstable(sym::csky_target_feature), &[]),
792    ("trust", Unstable(sym::csky_target_feature), &[]),
793    ("vdsp2e60f", Unstable(sym::csky_target_feature), &[]),
794    ("vdspv1", Unstable(sym::csky_target_feature), &[]),
795    ("vdspv2", Unstable(sym::csky_target_feature), &[]),
796    // tidy-alphabetical-end
797    //fpu
798    // tidy-alphabetical-start
799    ("fdivdu", Unstable(sym::csky_target_feature), &[]),
800    ("fpuv2_df", Unstable(sym::csky_target_feature), &[]),
801    ("fpuv2_sf", Unstable(sym::csky_target_feature), &[]),
802    ("fpuv3_df", Unstable(sym::csky_target_feature), &[]),
803    ("fpuv3_hf", Unstable(sym::csky_target_feature), &[]),
804    ("fpuv3_hi", Unstable(sym::csky_target_feature), &[]),
805    ("fpuv3_sf", Unstable(sym::csky_target_feature), &[]),
806    ("hard-float", Unstable(sym::csky_target_feature), &[]),
807    ("hard-float-abi", Unstable(sym::csky_target_feature), &[]),
808    // tidy-alphabetical-end
809];
810
811static LOONGARCH_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
812    // tidy-alphabetical-start
813    ("32s", Unstable(sym::loongarch_target_feature), &[]),
814    ("d", Stable, &["f"]),
815    ("div32", Unstable(sym::loongarch_target_feature), &[]),
816    ("f", Stable, &[]),
817    ("frecipe", Stable, &[]),
818    ("lam-bh", Unstable(sym::loongarch_target_feature), &[]),
819    ("lamcas", Unstable(sym::loongarch_target_feature), &[]),
820    ("lasx", Stable, &["lsx"]),
821    ("lbt", Stable, &[]),
822    ("ld-seq-sa", Unstable(sym::loongarch_target_feature), &[]),
823    ("lsx", Stable, &["d"]),
824    ("lvz", Stable, &[]),
825    ("relax", Unstable(sym::loongarch_target_feature), &[]),
826    ("scq", Unstable(sym::loongarch_target_feature), &[]),
827    ("ual", Unstable(sym::loongarch_target_feature), &[]),
828    // tidy-alphabetical-end
829];
830
831#[rustfmt::skip]
832const IBMZ_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
833    // tidy-alphabetical-start
834    // For "backchain", https://github.com/rust-lang/rust/issues/142412 is a stabilization blocker
835    ("backchain", Unstable(sym::s390x_target_feature), &[]),
836    ("concurrent-functions", Unstable(sym::s390x_target_feature), &[]),
837    ("deflate-conversion", Unstable(sym::s390x_target_feature), &[]),
838    ("enhanced-sort", Unstable(sym::s390x_target_feature), &[]),
839    ("guarded-storage", Unstable(sym::s390x_target_feature), &[]),
840    ("high-word", Unstable(sym::s390x_target_feature), &[]),
841    // LLVM does not define message-security-assist-extension versions 1, 2, 6, 10 and 11.
842    ("message-security-assist-extension3", Unstable(sym::s390x_target_feature), &[]),
843    ("message-security-assist-extension4", Unstable(sym::s390x_target_feature), &[]),
844    ("message-security-assist-extension5", Unstable(sym::s390x_target_feature), &[]),
845    ("message-security-assist-extension8", Unstable(sym::s390x_target_feature), &["message-security-assist-extension3"]),
846    ("message-security-assist-extension9", Unstable(sym::s390x_target_feature), &["message-security-assist-extension3", "message-security-assist-extension4"]),
847    ("message-security-assist-extension12", Unstable(sym::s390x_target_feature), &[]),
848    ("miscellaneous-extensions-2", Unstable(sym::s390x_target_feature), &[]),
849    ("miscellaneous-extensions-3", Unstable(sym::s390x_target_feature), &[]),
850    ("miscellaneous-extensions-4", Unstable(sym::s390x_target_feature), &[]),
851    ("nnp-assist", Unstable(sym::s390x_target_feature), &["vector"]),
852    ("transactional-execution", Unstable(sym::s390x_target_feature), &[]),
853    ("vector", Unstable(sym::s390x_target_feature), &[]),
854    ("vector-enhancements-1", Unstable(sym::s390x_target_feature), &["vector"]),
855    ("vector-enhancements-2", Unstable(sym::s390x_target_feature), &["vector-enhancements-1"]),
856    ("vector-enhancements-3", Unstable(sym::s390x_target_feature), &["vector-enhancements-2"]),
857    ("vector-packed-decimal", Unstable(sym::s390x_target_feature), &["vector"]),
858    ("vector-packed-decimal-enhancement", Unstable(sym::s390x_target_feature), &["vector-packed-decimal"]),
859    ("vector-packed-decimal-enhancement-2", Unstable(sym::s390x_target_feature), &["vector-packed-decimal-enhancement"]),
860    ("vector-packed-decimal-enhancement-3", Unstable(sym::s390x_target_feature), &["vector-packed-decimal-enhancement-2"]),
861    // tidy-alphabetical-end
862];
863
864const SPARC_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
865    // tidy-alphabetical-start
866    ("leoncasa", Unstable(sym::sparc_target_feature), &[]),
867    ("v8plus", Unstable(sym::sparc_target_feature), &[]),
868    ("v9", Unstable(sym::sparc_target_feature), &[]),
869    // tidy-alphabetical-end
870];
871
872static M68K_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
873    // tidy-alphabetical-start
874    ("isa-68000", Unstable(sym::m68k_target_feature), &[]),
875    ("isa-68010", Unstable(sym::m68k_target_feature), &["isa-68000"]),
876    ("isa-68020", Unstable(sym::m68k_target_feature), &["isa-68010"]),
877    ("isa-68030", Unstable(sym::m68k_target_feature), &["isa-68020"]),
878    ("isa-68040", Unstable(sym::m68k_target_feature), &["isa-68030", "isa-68882"]),
879    ("isa-68060", Unstable(sym::m68k_target_feature), &["isa-68040"]),
880    // FPU
881    ("isa-68881", Unstable(sym::m68k_target_feature), &[]),
882    ("isa-68882", Unstable(sym::m68k_target_feature), &["isa-68881"]),
883    // tidy-alphabetical-end
884];
885
886/// When rustdoc is running, provide a list of all known features so that all their respective
887/// primitives may be documented.
888///
889/// IMPORTANT: If you're adding another feature list above, make sure to add it to this iterator!
890pub fn all_rust_features() -> impl Iterator<Item = (&'static str, Stability)> {
891    std::iter::empty()
892        .chain(ARM_FEATURES.iter())
893        .chain(AARCH64_FEATURES.iter())
894        .chain(X86_FEATURES.iter())
895        .chain(HEXAGON_FEATURES.iter())
896        .chain(POWERPC_FEATURES.iter())
897        .chain(MIPS_FEATURES.iter())
898        .chain(NVPTX_FEATURES.iter())
899        .chain(RISCV_FEATURES.iter())
900        .chain(WASM_FEATURES.iter())
901        .chain(BPF_FEATURES.iter())
902        .chain(CSKY_FEATURES)
903        .chain(LOONGARCH_FEATURES)
904        .chain(IBMZ_FEATURES)
905        .chain(SPARC_FEATURES)
906        .chain(M68K_FEATURES)
907        .cloned()
908        .map(|(f, s, _)| (f, s))
909}
910
911// These arrays represent the least-constraining feature that is required for vector types up to a
912// certain size to have their "proper" ABI on each architecture.
913// Note that they must be kept sorted by vector size.
914const X86_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] =
915    &[(128, "sse"), (256, "avx"), (512, "avx512f")]; // FIXME: might need changes for AVX10.
916const AARCH64_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "neon")];
917
918// We might want to add "helium" too.
919const ARM_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "neon")];
920
921const POWERPC_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "altivec")];
922const WASM_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "simd128")];
923const S390X_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "vector")];
924const RISCV_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[
925    (32, "zvl32b"),
926    (64, "zvl64b"),
927    (128, "zvl128b"),
928    (256, "zvl256b"),
929    (512, "zvl512b"),
930    (1024, "zvl1024b"),
931    (2048, "zvl2048b"),
932    (4096, "zvl4096b"),
933    (8192, "zvl8192b"),
934    (16384, "zvl16384b"),
935    (32768, "zvl32768b"),
936    (65536, "zvl65536b"),
937];
938// Always error on SPARC, as the necessary target features cannot be enabled in Rust at the moment.
939const SPARC_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[/*(64, "vis")*/];
940
941const HEXAGON_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] =
942    &[/*(512, "hvx-length64b"),*/ (1024, "hvx-length128b")];
943const MIPS_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "msa")];
944const CSKY_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "vdspv1")];
945const LOONGARCH_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] =
946    &[(128, "lsx"), (256, "lasx")];
947
948#[derive(Copy, Clone, Debug)]
949pub struct FeatureConstraints {
950    /// Features that must be enabled.
951    pub required: &'static [&'static str],
952    /// Features that must be disabled.
953    pub incompatible: &'static [&'static str],
954}
955
956impl Target {
957    pub fn rust_target_features(&self) -> &'static [(&'static str, Stability, ImpliedFeatures)] {
958        match &*self.arch {
959            "arm" => ARM_FEATURES,
960            "aarch64" | "arm64ec" => AARCH64_FEATURES,
961            "x86" | "x86_64" => X86_FEATURES,
962            "hexagon" => HEXAGON_FEATURES,
963            "mips" | "mips32r6" | "mips64" | "mips64r6" => MIPS_FEATURES,
964            "nvptx64" => NVPTX_FEATURES,
965            "powerpc" | "powerpc64" => POWERPC_FEATURES,
966            "riscv32" | "riscv64" => RISCV_FEATURES,
967            "wasm32" | "wasm64" => WASM_FEATURES,
968            "bpf" => BPF_FEATURES,
969            "csky" => CSKY_FEATURES,
970            "loongarch32" | "loongarch64" => LOONGARCH_FEATURES,
971            "s390x" => IBMZ_FEATURES,
972            "sparc" | "sparc64" => SPARC_FEATURES,
973            "m68k" => M68K_FEATURES,
974            _ => &[],
975        }
976    }
977
978    pub fn features_for_correct_vector_abi(&self) -> &'static [(u64, &'static str)] {
979        match &*self.arch {
980            "x86" | "x86_64" => X86_FEATURES_FOR_CORRECT_VECTOR_ABI,
981            "aarch64" | "arm64ec" => AARCH64_FEATURES_FOR_CORRECT_VECTOR_ABI,
982            "arm" => ARM_FEATURES_FOR_CORRECT_VECTOR_ABI,
983            "powerpc" | "powerpc64" => POWERPC_FEATURES_FOR_CORRECT_VECTOR_ABI,
984            "loongarch32" | "loongarch64" => LOONGARCH_FEATURES_FOR_CORRECT_VECTOR_ABI,
985            "riscv32" | "riscv64" => RISCV_FEATURES_FOR_CORRECT_VECTOR_ABI,
986            "wasm32" | "wasm64" => WASM_FEATURES_FOR_CORRECT_VECTOR_ABI,
987            "s390x" => S390X_FEATURES_FOR_CORRECT_VECTOR_ABI,
988            "sparc" | "sparc64" => SPARC_FEATURES_FOR_CORRECT_VECTOR_ABI,
989            "hexagon" => HEXAGON_FEATURES_FOR_CORRECT_VECTOR_ABI,
990            "mips" | "mips32r6" | "mips64" | "mips64r6" => MIPS_FEATURES_FOR_CORRECT_VECTOR_ABI,
991            "nvptx64" | "bpf" | "m68k" => &[], // no vector ABI
992            "csky" => CSKY_FEATURES_FOR_CORRECT_VECTOR_ABI,
993            // FIXME: for some tier3 targets, we are overly cautious and always give warnings
994            // when passing args in vector registers.
995            _ => &[],
996        }
997    }
998
999    pub fn tied_target_features(&self) -> &'static [&'static [&'static str]] {
1000        match &*self.arch {
1001            "aarch64" | "arm64ec" => AARCH64_TIED_FEATURES,
1002            _ => &[],
1003        }
1004    }
1005
1006    // Note: the returned set includes `base_feature`.
1007    pub fn implied_target_features<'a>(&self, base_feature: &'a str) -> FxHashSet<&'a str> {
1008        let implied_features =
1009            self.rust_target_features().iter().map(|(f, _, i)| (f, i)).collect::<FxHashMap<_, _>>();
1010
1011        // Implied target features have their own implied target features, so we traverse the
1012        // map until there are no more features to add.
1013        let mut features = FxHashSet::default();
1014        let mut new_features = vec![base_feature];
1015        while let Some(new_feature) = new_features.pop() {
1016            if features.insert(new_feature) {
1017                if let Some(implied_features) = implied_features.get(&new_feature) {
1018                    new_features.extend(implied_features.iter().copied())
1019                }
1020            }
1021        }
1022        features
1023    }
1024
1025    /// Returns two lists of features:
1026    /// the first list contains target features that must be enabled for ABI reasons,
1027    /// and the second list contains target feature that must be disabled for ABI reasons.
1028    ///
1029    /// These features are automatically appended to whatever the target spec sets as default
1030    /// features for the target.
1031    ///
1032    /// All features enabled/disabled via `-Ctarget-features` and `#[target_features]` are checked
1033    /// against this. We also check any implied features, based on the information above. If LLVM
1034    /// implicitly enables more implied features than we do, that could bypass this check!
1035    pub fn abi_required_features(&self) -> FeatureConstraints {
1036        const NOTHING: FeatureConstraints = FeatureConstraints { required: &[], incompatible: &[] };
1037        // Some architectures don't have a clean explicit ABI designation; instead, the ABI is
1038        // defined by target features. When that is the case, those target features must be
1039        // "forbidden" in the list above to ensure that there is a consistent answer to the
1040        // questions "which ABI is used".
1041        match &*self.arch {
1042            "x86" => {
1043                // We use our own ABI indicator here; LLVM does not have anything native.
1044                // Every case should require or forbid `soft-float`!
1045                match self.rustc_abi {
1046                    None => {
1047                        // Default hardfloat ABI.
1048                        // x87 must be enabled, soft-float must be disabled.
1049                        FeatureConstraints { required: &["x87"], incompatible: &["soft-float"] }
1050                    }
1051                    Some(RustcAbi::X86Sse2) => {
1052                        // Extended hardfloat ABI. x87 and SSE2 must be enabled, soft-float must be disabled.
1053                        FeatureConstraints {
1054                            required: &["x87", "sse2"],
1055                            incompatible: &["soft-float"],
1056                        }
1057                    }
1058                    Some(RustcAbi::X86Softfloat) => {
1059                        // Softfloat ABI, requires corresponding target feature. That feature trumps
1060                        // `x87` and all other FPU features so those do not matter.
1061                        // Note that this one requirement is the entire implementation of the ABI!
1062                        // LLVM handles the rest.
1063                        FeatureConstraints { required: &["soft-float"], incompatible: &[] }
1064                    }
1065                }
1066            }
1067            "x86_64" => {
1068                // We use our own ABI indicator here; LLVM does not have anything native.
1069                // Every case should require or forbid `soft-float`!
1070                match self.rustc_abi {
1071                    None => {
1072                        // Default hardfloat ABI. On x86-64, this always includes SSE2.
1073                        FeatureConstraints {
1074                            required: &["x87", "sse2"],
1075                            incompatible: &["soft-float"],
1076                        }
1077                    }
1078                    Some(RustcAbi::X86Softfloat) => {
1079                        // Softfloat ABI, requires corresponding target feature. That feature trumps
1080                        // `x87` and all other FPU features so those do not matter.
1081                        // Note that this one requirement is the entire implementation of the ABI!
1082                        // LLVM handles the rest.
1083                        FeatureConstraints { required: &["soft-float"], incompatible: &[] }
1084                    }
1085                    Some(r) => panic!("invalid Rust ABI for x86_64: {r:?}"),
1086                }
1087            }
1088            "arm" => {
1089                // On ARM, ABI handling is reasonably sane; we use `llvm_floatabi` to indicate
1090                // to LLVM which ABI we are going for.
1091                match self.llvm_floatabi.unwrap() {
1092                    FloatAbi::Soft => {
1093                        // Nothing special required, will use soft-float ABI throughout.
1094                        // We can even allow `-soft-float` here; in fact that is useful as it lets
1095                        // people use FPU instructions with a softfloat ABI (corresponds to
1096                        // `-mfloat-abi=softfp` in GCC/clang).
1097                        NOTHING
1098                    }
1099                    FloatAbi::Hard => {
1100                        // Must have `fpregs` and must not have `soft-float`.
1101                        FeatureConstraints { required: &["fpregs"], incompatible: &["soft-float"] }
1102                    }
1103                }
1104            }
1105            "aarch64" | "arm64ec" => {
1106                // Aarch64 has no sane ABI specifier, and LLVM doesn't even have a way to force
1107                // the use of soft-float, so all we can do here is some crude hacks.
1108                match &*self.abi {
1109                    "softfloat" => {
1110                        // LLVM will use float registers when `fp-armv8` is available, e.g. for
1111                        // calls to built-ins. The only way to ensure a consistent softfloat ABI
1112                        // on aarch64 is to never enable `fp-armv8`, so we enforce that.
1113                        // In Rust we tie `neon` and `fp-armv8` together, therefore `neon` is the
1114                        // feature we have to mark as incompatible.
1115                        FeatureConstraints { required: &[], incompatible: &["neon"] }
1116                    }
1117                    _ => {
1118                        // Everything else is assumed to use a hardfloat ABI. neon and fp-armv8 must be enabled.
1119                        // `FeatureConstraints` uses Rust feature names, hence only "neon" shows up.
1120                        FeatureConstraints { required: &["neon"], incompatible: &[] }
1121                    }
1122                }
1123            }
1124            "riscv32" | "riscv64" => {
1125                // RISC-V handles ABI in a very sane way, being fully explicit via `llvm_abiname`
1126                // about what the intended ABI is.
1127                match &*self.llvm_abiname {
1128                    "ilp32d" | "lp64d" => {
1129                        // Requires d (which implies f), incompatible with e and zfinx.
1130                        FeatureConstraints { required: &["d"], incompatible: &["e", "zfinx"] }
1131                    }
1132                    "ilp32f" | "lp64f" => {
1133                        // Requires f, incompatible with e and zfinx.
1134                        FeatureConstraints { required: &["f"], incompatible: &["e", "zfinx"] }
1135                    }
1136                    "ilp32" | "lp64" => {
1137                        // Requires nothing, incompatible with e.
1138                        FeatureConstraints { required: &[], incompatible: &["e"] }
1139                    }
1140                    "ilp32e" => {
1141                        // ilp32e is documented to be incompatible with features that need aligned
1142                        // load/stores > 32 bits, like `d`. (One could also just generate more
1143                        // complicated code to align the stack when needed, but the RISCV
1144                        // architecture manual just explicitly rules out this combination so we
1145                        // might as well.)
1146                        // Note that the `e` feature is not required: the ABI treats the extra
1147                        // registers as caller-save, so it is safe to use them only in some parts of
1148                        // a program while the rest doesn't know they even exist.
1149                        FeatureConstraints { required: &[], incompatible: &["d"] }
1150                    }
1151                    "lp64e" => {
1152                        // As above, `e` is not required.
1153                        NOTHING
1154                    }
1155                    _ => unreachable!(),
1156                }
1157            }
1158            "loongarch32" | "loongarch64" => {
1159                // LoongArch handles ABI in a very sane way, being fully explicit via `llvm_abiname`
1160                // about what the intended ABI is.
1161                match &*self.llvm_abiname {
1162                    "ilp32d" | "lp64d" => {
1163                        // Requires d (which implies f), incompatible with nothing.
1164                        FeatureConstraints { required: &["d"], incompatible: &[] }
1165                    }
1166                    "ilp32f" | "lp64f" => {
1167                        // Requires f, incompatible with nothing.
1168                        FeatureConstraints { required: &["f"], incompatible: &[] }
1169                    }
1170                    "ilp32s" | "lp64s" => {
1171                        // The soft-float ABI does not require any features and is also not
1172                        // incompatible with any features. Rust targets explicitly specify the
1173                        // LLVM ABI names, which allows for enabling hard-float support even on
1174                        // soft-float targets, and ensures that the ABI behavior is as expected.
1175                        NOTHING
1176                    }
1177                    _ => unreachable!(),
1178                }
1179            }
1180            _ => NOTHING,
1181        }
1182    }
1183}