use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_span::symbol::{Symbol, sym};
pub const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
pub const RUSTC_SPECIAL_FEATURES: &[&str] = &["backchain"];
#[derive(Debug, Clone, Copy)]
pub enum Stability {
Stable,
Unstable(Symbol),
}
use Stability::*;
impl Stability {
pub fn as_feature_name(self) -> Option<Symbol> {
match self {
Stable => None,
Unstable(s) => Some(s),
}
}
pub fn is_stable(self) -> bool {
matches!(self, Stable)
}
}
type ImpliedFeatures = &'static [&'static str];
const ARM_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("aclass", Unstable(sym::arm_target_feature), &[]),
("aes", Unstable(sym::arm_target_feature), &["neon"]),
("crc", Unstable(sym::arm_target_feature), &[]),
("d32", Unstable(sym::arm_target_feature), &[]),
("dotprod", Unstable(sym::arm_target_feature), &["neon"]),
("dsp", Unstable(sym::arm_target_feature), &[]),
("fp-armv8", Unstable(sym::arm_target_feature), &["vfp4"]),
("i8mm", Unstable(sym::arm_target_feature), &["neon"]),
("mclass", Unstable(sym::arm_target_feature), &[]),
("neon", Unstable(sym::arm_target_feature), &["vfp3"]),
("rclass", Unstable(sym::arm_target_feature), &[]),
("sha2", Unstable(sym::arm_target_feature), &["neon"]),
("thumb-mode", Unstable(sym::arm_target_feature), &[]),
("thumb2", Unstable(sym::arm_target_feature), &[]),
("trustzone", Unstable(sym::arm_target_feature), &[]),
("v5te", Unstable(sym::arm_target_feature), &[]),
("v6", Unstable(sym::arm_target_feature), &["v5te"]),
("v6k", Unstable(sym::arm_target_feature), &["v6"]),
("v6t2", Unstable(sym::arm_target_feature), &["v6k", "thumb2"]),
("v7", Unstable(sym::arm_target_feature), &["v6t2"]),
("v8", Unstable(sym::arm_target_feature), &["v7"]),
("vfp2", Unstable(sym::arm_target_feature), &[]),
("vfp3", Unstable(sym::arm_target_feature), &["vfp2", "d32"]),
("vfp4", Unstable(sym::arm_target_feature), &["vfp3"]),
("virtualization", Unstable(sym::arm_target_feature), &[]),
];
const AARCH64_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("aes", Stable, &["neon"]),
("bf16", Stable, &[]),
("bti", Stable, &[]),
("crc", Stable, &[]),
("cssc", Unstable(sym::aarch64_unstable_target_feature), &[]),
("dit", Stable, &[]),
("dotprod", Stable, &["neon"]),
("dpb", Stable, &[]),
("dpb2", Stable, &["dpb"]),
("ecv", Unstable(sym::aarch64_unstable_target_feature), &[]),
("f32mm", Stable, &["sve"]),
("f64mm", Stable, &["sve"]),
("faminmax", Unstable(sym::aarch64_unstable_target_feature), &[]),
("fcma", Stable, &["neon"]),
("fhm", Stable, &["fp16"]),
("flagm", Stable, &[]),
("flagm2", Unstable(sym::aarch64_unstable_target_feature), &[]),
("fp16", Stable, &["neon"]),
("fp8", Unstable(sym::aarch64_unstable_target_feature), &["faminmax", "lut", "bf16"]),
("fp8dot2", Unstable(sym::aarch64_unstable_target_feature), &["fp8dot4"]),
("fp8dot4", Unstable(sym::aarch64_unstable_target_feature), &["fp8fma"]),
("fp8fma", Unstable(sym::aarch64_unstable_target_feature), &["fp8"]),
("frintts", Stable, &[]),
("hbc", Unstable(sym::aarch64_unstable_target_feature), &[]),
("i8mm", Stable, &[]),
("jsconv", Stable, &["neon"]),
("lor", Stable, &[]),
("lse", Stable, &[]),
("lse128", Unstable(sym::aarch64_unstable_target_feature), &["lse"]),
("lse2", Unstable(sym::aarch64_unstable_target_feature), &[]),
("lut", Unstable(sym::aarch64_unstable_target_feature), &[]),
("mops", Unstable(sym::aarch64_unstable_target_feature), &[]),
("mte", Stable, &[]),
("neon", Stable, &[]),
("paca", Stable, &[]),
("pacg", Stable, &[]),
("pan", Stable, &[]),
("pauth-lr", Unstable(sym::aarch64_unstable_target_feature), &[]),
("pmuv3", Stable, &[]),
("rand", Stable, &[]),
("ras", Stable, &[]),
("rcpc", Stable, &[]),
("rcpc2", Stable, &["rcpc"]),
("rcpc3", Unstable(sym::aarch64_unstable_target_feature), &["rcpc2"]),
("rdm", Stable, &["neon"]),
("sb", Stable, &[]),
("sha2", Stable, &["neon"]),
("sha3", Stable, &["sha2"]),
("sm4", Stable, &["neon"]),
("sme", Unstable(sym::aarch64_unstable_target_feature), &["bf16"]),
("sme-b16b16", Unstable(sym::aarch64_unstable_target_feature), &["bf16", "sme2", "sve-b16b16"]),
("sme-f16f16", Unstable(sym::aarch64_unstable_target_feature), &["sme2"]),
("sme-f64f64", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
("sme-f8f16", Unstable(sym::aarch64_unstable_target_feature), &["sme-f8f32"]),
("sme-f8f32", Unstable(sym::aarch64_unstable_target_feature), &["sme2", "fp8"]),
("sme-fa64", Unstable(sym::aarch64_unstable_target_feature), &["sme", "sve2"]),
("sme-i16i64", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
("sme-lutv2", Unstable(sym::aarch64_unstable_target_feature), &[]),
("sme2", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
("sme2p1", Unstable(sym::aarch64_unstable_target_feature), &["sme2"]),
("spe", Stable, &[]),
("ssbs", Stable, &[]),
("ssve-fp8dot2", Unstable(sym::aarch64_unstable_target_feature), &["ssve-fp8dot4"]),
("ssve-fp8dot4", Unstable(sym::aarch64_unstable_target_feature), &["ssve-fp8fma"]),
("ssve-fp8fma", Unstable(sym::aarch64_unstable_target_feature), &["sme2", "fp8"]),
("sve", Stable, &["neon"]),
("sve-b16b16", Unstable(sym::aarch64_unstable_target_feature), &["bf16"]),
("sve2", Stable, &["sve"]),
("sve2-aes", Stable, &["sve2", "aes"]),
("sve2-bitperm", Stable, &["sve2"]),
("sve2-sha3", Stable, &["sve2", "sha3"]),
("sve2-sm4", Stable, &["sve2", "sm4"]),
("sve2p1", Unstable(sym::aarch64_unstable_target_feature), &["sve2"]),
("tme", Stable, &[]),
("v8.1a", Unstable(sym::aarch64_ver_target_feature), &[
"crc", "lse", "rdm", "pan", "lor", "vh",
]),
("v8.2a", Unstable(sym::aarch64_ver_target_feature), &["v8.1a", "ras", "dpb"]),
("v8.3a", Unstable(sym::aarch64_ver_target_feature), &[
"v8.2a", "rcpc", "paca", "pacg", "jsconv",
]),
("v8.4a", Unstable(sym::aarch64_ver_target_feature), &["v8.3a", "dotprod", "dit", "flagm"]),
("v8.5a", Unstable(sym::aarch64_ver_target_feature), &["v8.4a", "ssbs", "sb", "dpb2", "bti"]),
("v8.6a", Unstable(sym::aarch64_ver_target_feature), &["v8.5a", "bf16", "i8mm"]),
("v8.7a", Unstable(sym::aarch64_ver_target_feature), &["v8.6a", "wfxt"]),
("v8.8a", Unstable(sym::aarch64_ver_target_feature), &["v8.7a", "hbc", "mops"]),
("v8.9a", Unstable(sym::aarch64_ver_target_feature), &["v8.8a", "cssc"]),
("v9.1a", Unstable(sym::aarch64_ver_target_feature), &["v9a", "v8.6a"]),
("v9.2a", Unstable(sym::aarch64_ver_target_feature), &["v9.1a", "v8.7a"]),
("v9.3a", Unstable(sym::aarch64_ver_target_feature), &["v9.2a", "v8.8a"]),
("v9.4a", Unstable(sym::aarch64_ver_target_feature), &["v9.3a", "v8.9a"]),
("v9.5a", Unstable(sym::aarch64_ver_target_feature), &["v9.4a"]),
("v9a", Unstable(sym::aarch64_ver_target_feature), &["v8.5a", "sve2"]),
("vh", Stable, &[]),
("wfxt", Unstable(sym::aarch64_unstable_target_feature), &[]),
];
const AARCH64_TIED_FEATURES: &[&[&str]] = &[
&["paca", "pacg"], ];
const X86_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("adx", Stable, &[]),
("aes", Stable, &["sse2"]),
("amx-bf16", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
("amx-complex", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
("amx-fp16", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
("amx-int8", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
("amx-tile", Unstable(sym::x86_amx_intrinsics), &[]),
("avx", Stable, &["sse4.2"]),
("avx2", Stable, &["avx"]),
("avx512bf16", Unstable(sym::avx512_target_feature), &["avx512bw"]),
("avx512bitalg", Unstable(sym::avx512_target_feature), &["avx512bw"]),
("avx512bw", Unstable(sym::avx512_target_feature), &["avx512f"]),
("avx512cd", Unstable(sym::avx512_target_feature), &["avx512f"]),
("avx512dq", Unstable(sym::avx512_target_feature), &["avx512f"]),
("avx512f", Unstable(sym::avx512_target_feature), &["avx2", "fma", "f16c"]),
("avx512fp16", Unstable(sym::avx512_target_feature), &["avx512bw", "avx512vl", "avx512dq"]),
("avx512ifma", Unstable(sym::avx512_target_feature), &["avx512f"]),
("avx512vbmi", Unstable(sym::avx512_target_feature), &["avx512bw"]),
("avx512vbmi2", Unstable(sym::avx512_target_feature), &["avx512bw"]),
("avx512vl", Unstable(sym::avx512_target_feature), &["avx512f"]),
("avx512vnni", Unstable(sym::avx512_target_feature), &["avx512f"]),
("avx512vp2intersect", Unstable(sym::avx512_target_feature), &["avx512f"]),
("avx512vpopcntdq", Unstable(sym::avx512_target_feature), &["avx512f"]),
("avxifma", Unstable(sym::avx512_target_feature), &["avx2"]),
("avxneconvert", Unstable(sym::avx512_target_feature), &["avx2"]),
("avxvnni", Unstable(sym::avx512_target_feature), &["avx2"]),
("avxvnniint16", Unstable(sym::avx512_target_feature), &["avx2"]),
("avxvnniint8", Unstable(sym::avx512_target_feature), &["avx2"]),
("bmi1", Stable, &[]),
("bmi2", Stable, &[]),
("cmpxchg16b", Stable, &[]),
("ermsb", Unstable(sym::ermsb_target_feature), &[]),
("f16c", Stable, &["avx"]),
("fma", Stable, &["avx"]),
("fxsr", Stable, &[]),
("gfni", Unstable(sym::avx512_target_feature), &["sse2"]),
("lahfsahf", Unstable(sym::lahfsahf_target_feature), &[]),
("lzcnt", Stable, &[]),
("movbe", Stable, &[]),
("pclmulqdq", Stable, &["sse2"]),
("popcnt", Stable, &[]),
("prfchw", Unstable(sym::prfchw_target_feature), &[]),
("rdrand", Stable, &[]),
("rdseed", Stable, &[]),
("rtm", Unstable(sym::rtm_target_feature), &[]),
("sha", Stable, &["sse2"]),
("sha512", Unstable(sym::sha512_sm_x86), &["avx2"]),
("sm3", Unstable(sym::sha512_sm_x86), &["avx"]),
("sm4", Unstable(sym::sha512_sm_x86), &["avx2"]),
("sse", Stable, &[]),
("sse2", Stable, &["sse"]),
("sse3", Stable, &["sse2"]),
("sse4.1", Stable, &["ssse3"]),
("sse4.2", Stable, &["sse4.1"]),
("sse4a", Unstable(sym::sse4a_target_feature), &["sse3"]),
("ssse3", Stable, &["sse3"]),
("tbm", Unstable(sym::tbm_target_feature), &[]),
("vaes", Unstable(sym::avx512_target_feature), &["avx2", "aes"]),
("vpclmulqdq", Unstable(sym::avx512_target_feature), &["avx", "pclmulqdq"]),
("xop", Unstable(sym::xop_target_feature), &["avx", "sse4a"]),
("xsave", Stable, &[]),
("xsavec", Stable, &["xsave"]),
("xsaveopt", Stable, &["xsave"]),
("xsaves", Stable, &["xsave"]),
];
const HEXAGON_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("hvx", Unstable(sym::hexagon_target_feature), &[]),
("hvx-length128b", Unstable(sym::hexagon_target_feature), &["hvx"]),
];
const POWERPC_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("altivec", Unstable(sym::powerpc_target_feature), &[]),
("partword-atomics", Unstable(sym::powerpc_target_feature), &[]),
("power10-vector", Unstable(sym::powerpc_target_feature), &["power9-vector"]),
("power8-altivec", Unstable(sym::powerpc_target_feature), &["altivec"]),
("power8-vector", Unstable(sym::powerpc_target_feature), &["vsx", "power8-altivec"]),
("power9-altivec", Unstable(sym::powerpc_target_feature), &["power8-altivec"]),
("power9-vector", Unstable(sym::powerpc_target_feature), &["power8-vector", "power9-altivec"]),
("quadword-atomics", Unstable(sym::powerpc_target_feature), &[]),
("vsx", Unstable(sym::powerpc_target_feature), &["altivec"]),
];
const MIPS_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("fp64", Unstable(sym::mips_target_feature), &[]),
("msa", Unstable(sym::mips_target_feature), &[]),
("virt", Unstable(sym::mips_target_feature), &[]),
];
const RISCV_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("a", Stable, &["zaamo", "zalrsc"]),
("c", Stable, &[]),
("d", Unstable(sym::riscv_target_feature), &["f"]),
("e", Unstable(sym::riscv_target_feature), &[]),
("f", Unstable(sym::riscv_target_feature), &[]),
("m", Stable, &[]),
("relax", Unstable(sym::riscv_target_feature), &[]),
("unaligned-scalar-mem", Unstable(sym::riscv_target_feature), &[]),
("v", Unstable(sym::riscv_target_feature), &[]),
("zaamo", Unstable(sym::riscv_target_feature), &[]),
("zabha", Unstable(sym::riscv_target_feature), &["zaamo"]),
("zalrsc", Unstable(sym::riscv_target_feature), &[]),
("zba", Stable, &[]),
("zbb", Stable, &[]),
("zbc", Stable, &[]),
("zbkb", Stable, &[]),
("zbkc", Stable, &[]),
("zbkx", Stable, &[]),
("zbs", Stable, &[]),
("zdinx", Unstable(sym::riscv_target_feature), &["zfinx"]),
("zfh", Unstable(sym::riscv_target_feature), &["zfhmin"]),
("zfhmin", Unstable(sym::riscv_target_feature), &["f"]),
("zfinx", Unstable(sym::riscv_target_feature), &[]),
("zhinx", Unstable(sym::riscv_target_feature), &["zhinxmin"]),
("zhinxmin", Unstable(sym::riscv_target_feature), &["zfinx"]),
("zk", Stable, &["zkn", "zkr", "zkt"]),
("zkn", Stable, &["zbkb", "zbkc", "zbkx", "zkne", "zknd", "zknh"]),
("zknd", Stable, &[]),
("zkne", Stable, &[]),
("zknh", Stable, &[]),
("zkr", Stable, &[]),
("zks", Stable, &["zbkb", "zbkc", "zbkx", "zksed", "zksh"]),
("zksed", Stable, &[]),
("zksh", Stable, &[]),
("zkt", Stable, &[]),
];
const WASM_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("atomics", Unstable(sym::wasm_target_feature), &[]),
("bulk-memory", Stable, &[]),
("exception-handling", Unstable(sym::wasm_target_feature), &[]),
("extended-const", Stable, &[]),
("multivalue", Unstable(sym::wasm_target_feature), &[]),
("mutable-globals", Stable, &[]),
("nontrapping-fptoint", Stable, &[]),
("reference-types", Unstable(sym::wasm_target_feature), &[]),
("relaxed-simd", Stable, &["simd128"]),
("sign-ext", Stable, &[]),
("simd128", Stable, &[]),
];
const BPF_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] =
&[("alu32", Unstable(sym::bpf_target_feature), &[])];
const CSKY_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("10e60", Unstable(sym::csky_target_feature), &["7e10"]),
("2e3", Unstable(sym::csky_target_feature), &["e2"]),
("3e3r1", Unstable(sym::csky_target_feature), &[]),
("3e3r2", Unstable(sym::csky_target_feature), &["3e3r1", "doloop"]),
("3e3r3", Unstable(sym::csky_target_feature), &["doloop"]),
("3e7", Unstable(sym::csky_target_feature), &["2e3"]),
("7e10", Unstable(sym::csky_target_feature), &["3e7"]),
("cache", Unstable(sym::csky_target_feature), &[]),
("doloop", Unstable(sym::csky_target_feature), &[]),
("dsp1e2", Unstable(sym::csky_target_feature), &[]),
("dspe60", Unstable(sym::csky_target_feature), &[]),
("e1", Unstable(sym::csky_target_feature), &["elrw"]),
("e2", Unstable(sym::csky_target_feature), &["e2"]),
("edsp", Unstable(sym::csky_target_feature), &[]),
("elrw", Unstable(sym::csky_target_feature), &[]),
("float1e2", Unstable(sym::csky_target_feature), &[]),
("float1e3", Unstable(sym::csky_target_feature), &[]),
("float3e4", Unstable(sym::csky_target_feature), &[]),
("float7e60", Unstable(sym::csky_target_feature), &[]),
("floate1", Unstable(sym::csky_target_feature), &[]),
("hard-tp", Unstable(sym::csky_target_feature), &[]),
("high-registers", Unstable(sym::csky_target_feature), &[]),
("hwdiv", Unstable(sym::csky_target_feature), &[]),
("mp", Unstable(sym::csky_target_feature), &["2e3"]),
("mp1e2", Unstable(sym::csky_target_feature), &["3e7"]),
("nvic", Unstable(sym::csky_target_feature), &[]),
("trust", Unstable(sym::csky_target_feature), &[]),
("vdsp2e60f", Unstable(sym::csky_target_feature), &[]),
("vdspv1", Unstable(sym::csky_target_feature), &[]),
("vdspv2", Unstable(sym::csky_target_feature), &[]),
("fdivdu", Unstable(sym::csky_target_feature), &[]),
("fpuv2_df", Unstable(sym::csky_target_feature), &[]),
("fpuv2_sf", Unstable(sym::csky_target_feature), &[]),
("fpuv3_df", Unstable(sym::csky_target_feature), &[]),
("fpuv3_hf", Unstable(sym::csky_target_feature), &[]),
("fpuv3_hi", Unstable(sym::csky_target_feature), &[]),
("fpuv3_sf", Unstable(sym::csky_target_feature), &[]),
("hard-float", Unstable(sym::csky_target_feature), &[]),
("hard-float-abi", Unstable(sym::csky_target_feature), &[]),
];
const LOONGARCH_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("d", Unstable(sym::loongarch_target_feature), &["f"]),
("f", Unstable(sym::loongarch_target_feature), &[]),
("frecipe", Unstable(sym::loongarch_target_feature), &[]),
("lasx", Unstable(sym::loongarch_target_feature), &["lsx"]),
("lbt", Unstable(sym::loongarch_target_feature), &[]),
("lsx", Unstable(sym::loongarch_target_feature), &["d"]),
("lvz", Unstable(sym::loongarch_target_feature), &[]),
("relax", Unstable(sym::loongarch_target_feature), &[]),
("ual", Unstable(sym::loongarch_target_feature), &[]),
];
const IBMZ_ALLOWED_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("backchain", Unstable(sym::s390x_target_feature), &[]),
("vector", Unstable(sym::s390x_target_feature), &[]),
];
pub fn all_known_features() -> impl Iterator<Item = (&'static str, Stability)> {
std::iter::empty()
.chain(ARM_ALLOWED_FEATURES.iter())
.chain(AARCH64_ALLOWED_FEATURES.iter())
.chain(X86_ALLOWED_FEATURES.iter())
.chain(HEXAGON_ALLOWED_FEATURES.iter())
.chain(POWERPC_ALLOWED_FEATURES.iter())
.chain(MIPS_ALLOWED_FEATURES.iter())
.chain(RISCV_ALLOWED_FEATURES.iter())
.chain(WASM_ALLOWED_FEATURES.iter())
.chain(BPF_ALLOWED_FEATURES.iter())
.chain(CSKY_ALLOWED_FEATURES)
.chain(LOONGARCH_ALLOWED_FEATURES)
.chain(IBMZ_ALLOWED_FEATURES)
.cloned()
.map(|(f, s, _)| (f, s))
}
impl super::spec::Target {
pub fn supported_target_features(
&self,
) -> &'static [(&'static str, Stability, ImpliedFeatures)] {
match &*self.arch {
"arm" => ARM_ALLOWED_FEATURES,
"aarch64" | "arm64ec" => AARCH64_ALLOWED_FEATURES,
"x86" | "x86_64" => X86_ALLOWED_FEATURES,
"hexagon" => HEXAGON_ALLOWED_FEATURES,
"mips" | "mips32r6" | "mips64" | "mips64r6" => MIPS_ALLOWED_FEATURES,
"powerpc" | "powerpc64" => POWERPC_ALLOWED_FEATURES,
"riscv32" | "riscv64" => RISCV_ALLOWED_FEATURES,
"wasm32" | "wasm64" => WASM_ALLOWED_FEATURES,
"bpf" => BPF_ALLOWED_FEATURES,
"csky" => CSKY_ALLOWED_FEATURES,
"loongarch64" => LOONGARCH_ALLOWED_FEATURES,
"s390x" => IBMZ_ALLOWED_FEATURES,
_ => &[],
}
}
pub fn tied_target_features(&self) -> &'static [&'static [&'static str]] {
match &*self.arch {
"aarch64" | "arm64ec" => AARCH64_TIED_FEATURES,
_ => &[],
}
}
pub fn implied_target_features(
&self,
base_features: impl Iterator<Item = Symbol>,
) -> FxHashSet<Symbol> {
let implied_features = self
.supported_target_features()
.iter()
.map(|(f, _, i)| (Symbol::intern(f), i))
.collect::<FxHashMap<_, _>>();
let mut features = FxHashSet::default();
let mut new_features = base_features.collect::<Vec<Symbol>>();
while let Some(new_feature) = new_features.pop() {
if features.insert(new_feature) {
if let Some(implied_features) = implied_features.get(&new_feature) {
new_features.extend(implied_features.iter().copied().map(Symbol::intern))
}
}
}
features
}
}