2023-04-10 16:04:14 +01:00
|
|
|
use crate::errors;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_ast::ast;
|
|
|
|
use rustc_attr::InstructionSetAttr;
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2023-02-21 15:15:16 +01:00
|
|
|
use rustc_data_structures::fx::FxIndexSet;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_errors::Applicability;
|
2023-02-12 20:20:45 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2020-10-09 19:35:17 +02:00
|
|
|
use rustc_hir::def_id::LOCAL_CRATE;
|
2023-05-15 06:24:45 +02:00
|
|
|
use rustc_middle::query::Providers;
|
2023-02-22 19:51:17 +04:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_session::parse::feature_err;
|
2020-10-04 11:12:56 +02:00
|
|
|
use rustc_session::Session;
|
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
use rustc_span::symbol::Symbol;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_span::Span;
|
2020-10-04 11:12:56 +02:00
|
|
|
|
2021-07-23 16:19:22 +03:00
|
|
|
/// Features that control behaviour of rustc, rather than the codegen.
|
|
|
|
pub const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
|
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
/// Stability information for target features.
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum Stability {
|
|
|
|
/// This target feature is stable, it can be used in `#[target_feature]` and
|
|
|
|
/// `#[cfg(target_feature)]`.
|
|
|
|
Stable,
|
|
|
|
/// This target feature is unstable; using it in `#[target_feature]` or `#[cfg(target_feature)]`
|
|
|
|
/// requires enabling the given nightly feature.
|
|
|
|
Unstable(Symbol),
|
|
|
|
}
|
|
|
|
use Stability::*;
|
|
|
|
|
|
|
|
impl Stability {
|
|
|
|
fn as_feature_name(self) -> Option<Symbol> {
|
|
|
|
match self {
|
|
|
|
Stable => None,
|
|
|
|
Unstable(s) => Some(s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_stable(self) -> bool {
|
|
|
|
matches!(self, Stable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we list target features that rustc "understands": they can be used in `#[target_feature]`
|
|
|
|
// and `#[cfg(target_feature)]`. They also do not trigger any warnings when used with
|
|
|
|
// `-Ctarget-feature`.
|
|
|
|
//
|
2020-10-25 16:53:25 +01:00
|
|
|
// When adding features to the below lists
|
|
|
|
// check whether they're named already elsewhere in rust
|
|
|
|
// e.g. in stdarch and whether the given name matches LLVM's
|
2023-11-13 09:41:45 +01:00
|
|
|
// if it doesn't, to_llvm_feature in llvm_util in rustc_codegen_llvm needs to be adapted.
|
|
|
|
//
|
|
|
|
// Also note that all target features listed here must be purely additive: for target_feature 1.1 to
|
|
|
|
// be sound, we can never allow features like `+soft-float` (on x86) to be controlled on a
|
|
|
|
// per-function level, since we would then allow safe calls from functions with `+soft-float` to
|
|
|
|
// functions without that feature!
|
2023-11-05 18:06:19 +01:00
|
|
|
//
|
|
|
|
// When adding a new feature, be particularly mindful of features that affect function ABIs. Those
|
|
|
|
// need to be treated very carefully to avoid introducing unsoundness! This often affects features
|
|
|
|
// that enable/disable hardfloat support (see https://github.com/rust-lang/rust/issues/116344 for an
|
|
|
|
// example of this going wrong), but features enabling new SIMD registers are also a concern (see
|
|
|
|
// https://github.com/rust-lang/rust/issues/116558 for an example of this going wrong).
|
|
|
|
//
|
2023-11-12 12:41:22 +01:00
|
|
|
// Stabilizing a target feature requires t-lang approval.
|
2020-10-25 16:53:25 +01:00
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const ARM_ALLOWED_FEATURES: &[(&str, Stability)] = &[
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-start
|
2023-11-12 12:41:22 +01:00
|
|
|
("aclass", Unstable(sym::arm_target_feature)),
|
|
|
|
("aes", Unstable(sym::arm_target_feature)),
|
|
|
|
("crc", Unstable(sym::arm_target_feature)),
|
|
|
|
("d32", Unstable(sym::arm_target_feature)),
|
|
|
|
("dotprod", Unstable(sym::arm_target_feature)),
|
|
|
|
("dsp", Unstable(sym::arm_target_feature)),
|
|
|
|
("fp-armv8", Unstable(sym::arm_target_feature)),
|
|
|
|
("i8mm", Unstable(sym::arm_target_feature)),
|
|
|
|
("mclass", Unstable(sym::arm_target_feature)),
|
|
|
|
("neon", Unstable(sym::arm_target_feature)),
|
|
|
|
("rclass", Unstable(sym::arm_target_feature)),
|
|
|
|
("sha2", Unstable(sym::arm_target_feature)),
|
2022-10-14 22:01:18 +02:00
|
|
|
// This is needed for inline assembly, but shouldn't be stabilized as-is
|
|
|
|
// since it should be enabled per-function using #[instruction_set], not
|
|
|
|
// #[target_feature].
|
2023-11-12 12:41:22 +01:00
|
|
|
("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)),
|
|
|
|
("v6k", Unstable(sym::arm_target_feature)),
|
|
|
|
("v6t2", Unstable(sym::arm_target_feature)),
|
|
|
|
("v7", Unstable(sym::arm_target_feature)),
|
|
|
|
("v8", Unstable(sym::arm_target_feature)),
|
|
|
|
("vfp2", Unstable(sym::arm_target_feature)),
|
|
|
|
("vfp3", Unstable(sym::arm_target_feature)),
|
|
|
|
("vfp4", Unstable(sym::arm_target_feature)),
|
|
|
|
("virtualization", Unstable(sym::arm_target_feature)),
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-end
|
2020-10-04 11:12:56 +02:00
|
|
|
];
|
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const AARCH64_ALLOWED_FEATURES: &[(&str, Stability)] = &[
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-start
|
|
|
|
// FEAT_AES
|
2023-11-12 12:41:22 +01:00
|
|
|
("aes", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_BF16
|
2023-11-12 12:41:22 +01:00
|
|
|
("bf16", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_BTI
|
2023-11-12 12:41:22 +01:00
|
|
|
("bti", Stable),
|
2021-04-28 17:54:44 +01:00
|
|
|
// FEAT_CRC
|
2023-11-12 12:41:22 +01:00
|
|
|
("crc", Stable),
|
2021-04-28 17:54:44 +01:00
|
|
|
// FEAT_DIT
|
2023-11-12 12:41:22 +01:00
|
|
|
("dit", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_DotProd
|
2023-11-12 12:41:22 +01:00
|
|
|
("dotprod", Stable),
|
2021-04-28 17:54:44 +01:00
|
|
|
// FEAT_DPB
|
2023-11-12 12:41:22 +01:00
|
|
|
("dpb", Stable),
|
2021-04-28 17:54:44 +01:00
|
|
|
// FEAT_DPB2
|
2023-11-12 12:41:22 +01:00
|
|
|
("dpb2", Stable),
|
2021-04-28 17:54:44 +01:00
|
|
|
// FEAT_F32MM
|
2023-11-12 12:41:22 +01:00
|
|
|
("f32mm", Stable),
|
2021-04-28 17:54:44 +01:00
|
|
|
// FEAT_F64MM
|
2023-11-12 12:41:22 +01:00
|
|
|
("f64mm", Stable),
|
2021-04-28 17:54:44 +01:00
|
|
|
// FEAT_FCMA
|
2023-11-12 12:41:22 +01:00
|
|
|
("fcma", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_FHM
|
2023-11-12 12:41:22 +01:00
|
|
|
("fhm", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_FLAGM
|
2023-11-12 12:41:22 +01:00
|
|
|
("flagm", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_FP16
|
2023-11-12 12:41:22 +01:00
|
|
|
("fp16", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_FRINTTS
|
2023-11-12 12:41:22 +01:00
|
|
|
("frintts", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_I8MM
|
2023-11-12 12:41:22 +01:00
|
|
|
("i8mm", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_JSCVT
|
2023-11-12 12:41:22 +01:00
|
|
|
("jsconv", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_LOR
|
2023-11-12 12:41:22 +01:00
|
|
|
("lor", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_LSE
|
2023-11-12 12:41:22 +01:00
|
|
|
("lse", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_MTE
|
2023-11-12 12:41:22 +01:00
|
|
|
("mte", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_AdvSimd & FEAT_FP
|
2023-11-12 12:41:22 +01:00
|
|
|
("neon", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_PAUTH (address authentication)
|
2023-11-12 12:41:22 +01:00
|
|
|
("paca", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_PAUTH (generic authentication)
|
2023-11-12 12:41:22 +01:00
|
|
|
("pacg", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_PAN
|
2023-11-12 12:41:22 +01:00
|
|
|
("pan", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_PMUv3
|
2023-11-12 12:41:22 +01:00
|
|
|
("pmuv3", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_RAND
|
2023-11-12 12:41:22 +01:00
|
|
|
("rand", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_RAS
|
2023-11-12 12:41:22 +01:00
|
|
|
("ras", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_RCPC
|
2023-11-12 12:41:22 +01:00
|
|
|
("rcpc", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_RCPC2
|
2023-11-12 12:41:22 +01:00
|
|
|
("rcpc2", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_RDM
|
2023-11-12 12:41:22 +01:00
|
|
|
("rdm", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_SB
|
2023-11-12 12:41:22 +01:00
|
|
|
("sb", Stable),
|
2021-04-28 17:54:44 +01:00
|
|
|
// FEAT_SHA1 & FEAT_SHA256
|
2023-11-12 12:41:22 +01:00
|
|
|
("sha2", Stable),
|
2021-04-28 17:54:44 +01:00
|
|
|
// FEAT_SHA512 & FEAT_SHA3
|
2023-11-12 12:41:22 +01:00
|
|
|
("sha3", Stable),
|
2021-04-28 17:54:44 +01:00
|
|
|
// FEAT_SM3 & FEAT_SM4
|
2023-11-12 12:41:22 +01:00
|
|
|
("sm4", Stable),
|
2021-11-01 16:22:29 +00:00
|
|
|
// FEAT_SPE
|
2023-11-12 12:41:22 +01:00
|
|
|
("spe", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_SSBS
|
2023-11-12 12:41:22 +01:00
|
|
|
("ssbs", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_SVE
|
2023-11-12 12:41:22 +01:00
|
|
|
("sve", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_SVE2
|
2023-11-12 12:41:22 +01:00
|
|
|
("sve2", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_SVE2_AES
|
2023-11-12 12:41:22 +01:00
|
|
|
("sve2-aes", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_SVE2_BitPerm
|
2023-11-12 12:41:22 +01:00
|
|
|
("sve2-bitperm", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_SVE2_SHA3
|
2023-11-12 12:41:22 +01:00
|
|
|
("sve2-sha3", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_SVE2_SM4
|
2023-11-12 12:41:22 +01:00
|
|
|
("sve2-sm4", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_TME
|
2023-11-12 12:41:22 +01:00
|
|
|
("tme", Stable),
|
|
|
|
("v8.1a", Unstable(sym::aarch64_ver_target_feature)),
|
|
|
|
("v8.2a", Unstable(sym::aarch64_ver_target_feature)),
|
|
|
|
("v8.3a", Unstable(sym::aarch64_ver_target_feature)),
|
|
|
|
("v8.4a", Unstable(sym::aarch64_ver_target_feature)),
|
|
|
|
("v8.5a", Unstable(sym::aarch64_ver_target_feature)),
|
|
|
|
("v8.6a", Unstable(sym::aarch64_ver_target_feature)),
|
|
|
|
("v8.7a", Unstable(sym::aarch64_ver_target_feature)),
|
2022-10-14 22:01:18 +02:00
|
|
|
// FEAT_VHE
|
2023-11-12 12:41:22 +01:00
|
|
|
("vh", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-end
|
2020-10-04 11:12:56 +02:00
|
|
|
];
|
|
|
|
|
2022-02-10 16:43:45 +00:00
|
|
|
const AARCH64_TIED_FEATURES: &[&[&str]] = &[
|
|
|
|
&["paca", "pacg"], // Together these represent `pauth` in LLVM
|
|
|
|
];
|
2022-01-31 13:04:27 +00:00
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const X86_ALLOWED_FEATURES: &[(&str, Stability)] = &[
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-start
|
2023-11-12 12:41:22 +01:00
|
|
|
("adx", Stable),
|
|
|
|
("aes", Stable),
|
|
|
|
("avx", Stable),
|
|
|
|
("avx2", Stable),
|
|
|
|
("avx512bf16", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512bitalg", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512bw", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512cd", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512dq", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512er", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512f", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512ifma", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512pf", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512vbmi", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512vbmi2", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512vl", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512vnni", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512vp2intersect", Unstable(sym::avx512_target_feature)),
|
|
|
|
("avx512vpopcntdq", Unstable(sym::avx512_target_feature)),
|
|
|
|
("bmi1", Stable),
|
|
|
|
("bmi2", Stable),
|
|
|
|
("cmpxchg16b", Stable),
|
|
|
|
("ermsb", Unstable(sym::ermsb_target_feature)),
|
|
|
|
("f16c", Stable),
|
|
|
|
("fma", Stable),
|
|
|
|
("fxsr", Stable),
|
|
|
|
("gfni", Unstable(sym::avx512_target_feature)),
|
|
|
|
("lzcnt", Stable),
|
|
|
|
("movbe", Stable),
|
|
|
|
("pclmulqdq", Stable),
|
|
|
|
("popcnt", Stable),
|
|
|
|
("rdrand", Stable),
|
|
|
|
("rdseed", Stable),
|
|
|
|
("rtm", Unstable(sym::rtm_target_feature)),
|
|
|
|
("sha", Stable),
|
|
|
|
("sse", Stable),
|
|
|
|
("sse2", Stable),
|
|
|
|
("sse3", Stable),
|
|
|
|
("sse4.1", Stable),
|
|
|
|
("sse4.2", Stable),
|
|
|
|
("sse4a", Unstable(sym::sse4a_target_feature)),
|
|
|
|
("ssse3", Stable),
|
|
|
|
("tbm", Unstable(sym::tbm_target_feature)),
|
|
|
|
("vaes", Unstable(sym::avx512_target_feature)),
|
|
|
|
("vpclmulqdq", Unstable(sym::avx512_target_feature)),
|
|
|
|
("xsave", Stable),
|
|
|
|
("xsavec", Stable),
|
|
|
|
("xsaveopt", Stable),
|
|
|
|
("xsaves", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-end
|
2020-10-04 11:12:56 +02:00
|
|
|
];
|
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const HEXAGON_ALLOWED_FEATURES: &[(&str, Stability)] = &[
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-start
|
2023-11-12 12:41:22 +01:00
|
|
|
("hvx", Unstable(sym::hexagon_target_feature)),
|
|
|
|
("hvx-length128b", Unstable(sym::hexagon_target_feature)),
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-end
|
2020-10-04 11:12:56 +02:00
|
|
|
];
|
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const POWERPC_ALLOWED_FEATURES: &[(&str, Stability)] = &[
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-start
|
2023-11-12 12:41:22 +01:00
|
|
|
("altivec", Unstable(sym::powerpc_target_feature)),
|
|
|
|
("power10-vector", Unstable(sym::powerpc_target_feature)),
|
|
|
|
("power8-altivec", Unstable(sym::powerpc_target_feature)),
|
|
|
|
("power8-vector", Unstable(sym::powerpc_target_feature)),
|
|
|
|
("power9-altivec", Unstable(sym::powerpc_target_feature)),
|
|
|
|
("power9-vector", Unstable(sym::powerpc_target_feature)),
|
|
|
|
("vsx", Unstable(sym::powerpc_target_feature)),
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-end
|
2020-10-04 11:12:56 +02:00
|
|
|
];
|
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const MIPS_ALLOWED_FEATURES: &[(&str, Stability)] = &[
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-start
|
2023-11-12 12:41:22 +01:00
|
|
|
("fp64", Unstable(sym::mips_target_feature)),
|
|
|
|
("msa", Unstable(sym::mips_target_feature)),
|
|
|
|
("virt", Unstable(sym::mips_target_feature)),
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-end
|
2022-07-18 20:40:41 -04:00
|
|
|
];
|
2020-10-04 11:12:56 +02:00
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const RISCV_ALLOWED_FEATURES: &[(&str, Stability)] = &[
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-start
|
2023-11-12 12:41:22 +01:00
|
|
|
("a", Stable),
|
|
|
|
("c", Stable),
|
|
|
|
("d", Unstable(sym::riscv_target_feature)),
|
|
|
|
("e", Unstable(sym::riscv_target_feature)),
|
|
|
|
("f", Unstable(sym::riscv_target_feature)),
|
2023-12-04 15:52:26 +00:00
|
|
|
("fast-unaligned-access", Unstable(sym::riscv_target_feature)),
|
2023-11-12 12:41:22 +01:00
|
|
|
("m", Stable),
|
|
|
|
("relax", Unstable(sym::riscv_target_feature)),
|
|
|
|
("v", 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)),
|
|
|
|
("zfh", Unstable(sym::riscv_target_feature)),
|
|
|
|
("zfhmin", Unstable(sym::riscv_target_feature)),
|
|
|
|
("zfinx", Unstable(sym::riscv_target_feature)),
|
|
|
|
("zhinx", Unstable(sym::riscv_target_feature)),
|
|
|
|
("zhinxmin", Unstable(sym::riscv_target_feature)),
|
|
|
|
("zk", Stable),
|
|
|
|
("zkn", Stable),
|
|
|
|
("zknd", Stable),
|
|
|
|
("zkne", Stable),
|
|
|
|
("zknh", Stable),
|
|
|
|
("zkr", Stable),
|
|
|
|
("zks", Stable),
|
|
|
|
("zksed", Stable),
|
|
|
|
("zksh", Stable),
|
|
|
|
("zkt", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-end
|
2020-10-04 11:12:56 +02:00
|
|
|
];
|
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const WASM_ALLOWED_FEATURES: &[(&str, Stability)] = &[
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-start
|
2023-11-12 12:41:22 +01:00
|
|
|
("atomics", Unstable(sym::wasm_target_feature)),
|
|
|
|
("bulk-memory", Unstable(sym::wasm_target_feature)),
|
|
|
|
("exception-handling", Unstable(sym::wasm_target_feature)),
|
|
|
|
("multivalue", Unstable(sym::wasm_target_feature)),
|
|
|
|
("mutable-globals", Unstable(sym::wasm_target_feature)),
|
|
|
|
("nontrapping-fptoint", Unstable(sym::wasm_target_feature)),
|
|
|
|
("reference-types", Unstable(sym::wasm_target_feature)),
|
|
|
|
("relaxed-simd", Unstable(sym::wasm_target_feature)),
|
|
|
|
("sign-ext", Unstable(sym::wasm_target_feature)),
|
|
|
|
("simd128", Stable),
|
2022-10-14 22:01:18 +02:00
|
|
|
// tidy-alphabetical-end
|
2020-10-04 11:12:56 +02:00
|
|
|
];
|
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const BPF_ALLOWED_FEATURES: &[(&str, Stability)] = &[("alu32", Unstable(sym::bpf_target_feature))];
|
2021-04-20 19:03:10 +10:00
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const CSKY_ALLOWED_FEATURES: &[(&str, Stability)] = &[
|
2023-07-19 22:33:34 +08:00
|
|
|
// tidy-alphabetical-start
|
2023-11-12 12:41:22 +01:00
|
|
|
("10e60", Unstable(sym::csky_target_feature)),
|
|
|
|
("2e3", Unstable(sym::csky_target_feature)),
|
|
|
|
("3e3r1", Unstable(sym::csky_target_feature)),
|
|
|
|
("3e3r2", Unstable(sym::csky_target_feature)),
|
|
|
|
("3e3r3", Unstable(sym::csky_target_feature)),
|
|
|
|
("3e7", Unstable(sym::csky_target_feature)),
|
|
|
|
("7e10", Unstable(sym::csky_target_feature)),
|
|
|
|
("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)),
|
|
|
|
("e2", Unstable(sym::csky_target_feature)),
|
|
|
|
("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)),
|
|
|
|
("mp1e2", Unstable(sym::csky_target_feature)),
|
|
|
|
("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)),
|
2023-07-19 22:33:34 +08:00
|
|
|
// tidy-alphabetical-end
|
|
|
|
//fpu
|
|
|
|
// tidy-alphabetical-start
|
2023-11-12 12:41:22 +01:00
|
|
|
("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)),
|
2023-07-19 22:33:34 +08:00
|
|
|
// tidy-alphabetical-end
|
2023-07-14 19:16:38 +08:00
|
|
|
];
|
2023-10-19 21:12:19 +08:00
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
const LOONGARCH_ALLOWED_FEATURES: &[(&str, Stability)] = &[
|
2023-10-19 21:12:19 +08:00
|
|
|
// tidy-alphabetical-start
|
2023-11-12 12:41:22 +01:00
|
|
|
("d", Unstable(sym::loongarch_target_feature)),
|
|
|
|
("f", Unstable(sym::loongarch_target_feature)),
|
|
|
|
("lasx", Unstable(sym::loongarch_target_feature)),
|
|
|
|
("lbt", Unstable(sym::loongarch_target_feature)),
|
|
|
|
("lsx", Unstable(sym::loongarch_target_feature)),
|
|
|
|
("lvz", Unstable(sym::loongarch_target_feature)),
|
|
|
|
("ual", Unstable(sym::loongarch_target_feature)),
|
2023-10-19 21:12:19 +08:00
|
|
|
// tidy-alphabetical-end
|
|
|
|
];
|
|
|
|
|
2020-10-04 11:12:56 +02:00
|
|
|
/// When rustdoc is running, provide a list of all known features so that all their respective
|
|
|
|
/// primitives may be documented.
|
|
|
|
///
|
|
|
|
/// IMPORTANT: If you're adding another feature list above, make sure to add it to this iterator!
|
2023-11-12 12:41:22 +01:00
|
|
|
pub fn all_known_features() -> impl Iterator<Item = (&'static str, Stability)> {
|
2020-10-04 11:12:56 +02:00
|
|
|
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())
|
2021-04-20 19:03:10 +10:00
|
|
|
.chain(BPF_ALLOWED_FEATURES.iter())
|
2023-07-14 19:16:38 +08:00
|
|
|
.chain(CSKY_ALLOWED_FEATURES)
|
2023-10-19 21:12:19 +08:00
|
|
|
.chain(LOONGARCH_ALLOWED_FEATURES)
|
2020-10-04 11:12:56 +02:00
|
|
|
.cloned()
|
|
|
|
}
|
|
|
|
|
2023-11-12 12:41:22 +01:00
|
|
|
pub fn supported_target_features(sess: &Session) -> &'static [(&'static str, Stability)] {
|
2020-10-15 11:44:00 +02:00
|
|
|
match &*sess.target.arch {
|
2020-10-04 11:12:56 +02:00
|
|
|
"arm" => ARM_ALLOWED_FEATURES,
|
|
|
|
"aarch64" => AARCH64_ALLOWED_FEATURES,
|
|
|
|
"x86" | "x86_64" => X86_ALLOWED_FEATURES,
|
|
|
|
"hexagon" => HEXAGON_ALLOWED_FEATURES,
|
2023-06-28 13:35:39 +08:00
|
|
|
"mips" | "mips32r6" | "mips64" | "mips64r6" => MIPS_ALLOWED_FEATURES,
|
2020-10-04 11:12:56 +02:00
|
|
|
"powerpc" | "powerpc64" => POWERPC_ALLOWED_FEATURES,
|
|
|
|
"riscv32" | "riscv64" => RISCV_ALLOWED_FEATURES,
|
2020-12-30 12:52:21 -06:00
|
|
|
"wasm32" | "wasm64" => WASM_ALLOWED_FEATURES,
|
2021-04-20 19:03:10 +10:00
|
|
|
"bpf" => BPF_ALLOWED_FEATURES,
|
2023-07-14 19:16:38 +08:00
|
|
|
"csky" => CSKY_ALLOWED_FEATURES,
|
2023-10-19 21:12:19 +08:00
|
|
|
"loongarch64" => LOONGARCH_ALLOWED_FEATURES,
|
2020-10-04 11:12:56 +02:00
|
|
|
_ => &[],
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 19:35:17 +02:00
|
|
|
|
2022-01-31 13:04:27 +00:00
|
|
|
pub fn tied_target_features(sess: &Session) -> &'static [&'static [&'static str]] {
|
|
|
|
match &*sess.target.arch {
|
|
|
|
"aarch64" => AARCH64_TIED_FEATURES,
|
|
|
|
_ => &[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 03:53:35 +00:00
|
|
|
pub fn from_target_feature(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
attr: &ast::Attribute,
|
|
|
|
supported_target_features: &FxHashMap<String, Option<Symbol>>,
|
|
|
|
target_features: &mut Vec<Symbol>,
|
|
|
|
) {
|
|
|
|
let Some(list) = attr.meta_item_list() else { return };
|
|
|
|
let bad_item = |span| {
|
|
|
|
let msg = "malformed `target_feature` attribute input";
|
|
|
|
let code = "enable = \"..\"";
|
|
|
|
tcx.sess
|
|
|
|
.struct_span_err(span, msg)
|
|
|
|
.span_suggestion(span, "must be of the form", code, Applicability::HasPlaceholders)
|
|
|
|
.emit();
|
2020-10-09 19:35:17 +02:00
|
|
|
};
|
2022-12-08 03:53:35 +00:00
|
|
|
let rust_features = tcx.features();
|
|
|
|
for item in list {
|
|
|
|
// Only `enable = ...` is accepted in the meta-item list.
|
|
|
|
if !item.has_name(sym::enable) {
|
|
|
|
bad_item(item.span());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be of the form `enable = "..."` (a string).
|
|
|
|
let Some(value) = item.value_str() else {
|
|
|
|
bad_item(item.span());
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
// We allow comma separation to enable multiple features.
|
|
|
|
target_features.extend(value.as_str().split(',').filter_map(|feature| {
|
|
|
|
let Some(feature_gate) = supported_target_features.get(feature) else {
|
2023-07-25 23:04:01 +02:00
|
|
|
let msg = format!("the feature named `{feature}` is not valid for this target");
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
let mut err = tcx.sess.struct_span_err(item.span(), msg);
|
2023-07-25 23:04:01 +02:00
|
|
|
err.span_label(item.span(), format!("`{feature}` is not valid for this target"));
|
2022-12-08 03:53:35 +00:00
|
|
|
if let Some(stripped) = feature.strip_prefix('+') {
|
|
|
|
let valid = supported_target_features.contains_key(stripped);
|
|
|
|
if valid {
|
|
|
|
err.help("consider removing the leading `+` in the feature name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only allow features whose feature gates have been enabled.
|
|
|
|
let allowed = match feature_gate.as_ref().copied() {
|
|
|
|
Some(sym::arm_target_feature) => rust_features.arm_target_feature,
|
|
|
|
Some(sym::hexagon_target_feature) => rust_features.hexagon_target_feature,
|
|
|
|
Some(sym::powerpc_target_feature) => rust_features.powerpc_target_feature,
|
|
|
|
Some(sym::mips_target_feature) => rust_features.mips_target_feature,
|
|
|
|
Some(sym::riscv_target_feature) => rust_features.riscv_target_feature,
|
|
|
|
Some(sym::avx512_target_feature) => rust_features.avx512_target_feature,
|
|
|
|
Some(sym::sse4a_target_feature) => rust_features.sse4a_target_feature,
|
|
|
|
Some(sym::tbm_target_feature) => rust_features.tbm_target_feature,
|
|
|
|
Some(sym::wasm_target_feature) => rust_features.wasm_target_feature,
|
|
|
|
Some(sym::rtm_target_feature) => rust_features.rtm_target_feature,
|
|
|
|
Some(sym::ermsb_target_feature) => rust_features.ermsb_target_feature,
|
|
|
|
Some(sym::bpf_target_feature) => rust_features.bpf_target_feature,
|
|
|
|
Some(sym::aarch64_ver_target_feature) => rust_features.aarch64_ver_target_feature,
|
2023-07-14 19:16:38 +08:00
|
|
|
Some(sym::csky_target_feature) => rust_features.csky_target_feature,
|
2023-10-19 21:12:19 +08:00
|
|
|
Some(sym::loongarch_target_feature) => rust_features.loongarch_target_feature,
|
2022-12-08 03:53:35 +00:00
|
|
|
Some(name) => bug!("unknown target feature gate {}", name),
|
|
|
|
None => true,
|
|
|
|
};
|
|
|
|
if !allowed {
|
|
|
|
feature_err(
|
|
|
|
&tcx.sess.parse_sess,
|
|
|
|
feature_gate.unwrap(),
|
|
|
|
item.span(),
|
2023-07-25 23:04:01 +02:00
|
|
|
format!("the target feature `{feature}` is currently unstable"),
|
2022-12-08 03:53:35 +00:00
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
Some(Symbol::intern(feature))
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the set of target features used in a function for the purposes of
|
|
|
|
/// inline assembly.
|
2023-02-21 15:15:16 +01:00
|
|
|
fn asm_target_features(tcx: TyCtxt<'_>, did: DefId) -> &FxIndexSet<Symbol> {
|
2022-12-08 03:53:35 +00:00
|
|
|
let mut target_features = tcx.sess.unstable_target_features.clone();
|
|
|
|
if tcx.def_kind(did).has_codegen_attrs() {
|
|
|
|
let attrs = tcx.codegen_fn_attrs(did);
|
|
|
|
target_features.extend(&attrs.target_features);
|
|
|
|
match attrs.instruction_set {
|
|
|
|
None => {}
|
|
|
|
Some(InstructionSetAttr::ArmA32) => {
|
|
|
|
target_features.remove(&sym::thumb_mode);
|
|
|
|
}
|
|
|
|
Some(InstructionSetAttr::ArmT32) => {
|
|
|
|
target_features.insert(sym::thumb_mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tcx.arena.alloc(target_features)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks the function annotated with `#[target_feature]` is not a safe
|
|
|
|
/// trait method implementation, reporting an error if it is.
|
|
|
|
pub fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) {
|
2023-02-12 20:20:45 +00:00
|
|
|
if let DefKind::AssocFn = tcx.def_kind(id) {
|
|
|
|
let parent_id = tcx.local_parent(id);
|
2023-03-10 13:50:51 +01:00
|
|
|
if let DefKind::Trait | DefKind::Impl { of_trait: true } = tcx.def_kind(parent_id) {
|
2023-04-10 16:04:14 +01:00
|
|
|
tcx.sess.emit_err(errors::TargetFeatureSafeTrait {
|
|
|
|
span: attr_span,
|
|
|
|
def: tcx.def_span(id),
|
|
|
|
});
|
2022-12-08 03:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
|
|
|
*providers = Providers {
|
|
|
|
supported_target_features: |tcx, cnum| {
|
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
|
|
|
if tcx.sess.opts.actually_rustdoc {
|
|
|
|
// rustdoc needs to be able to document functions that use all the features, so
|
|
|
|
// whitelist them all
|
2023-11-12 12:41:22 +01:00
|
|
|
all_known_features().map(|(a, b)| (a.to_string(), b.as_feature_name())).collect()
|
2022-12-08 03:53:35 +00:00
|
|
|
} else {
|
|
|
|
supported_target_features(tcx.sess)
|
|
|
|
.iter()
|
2023-11-12 12:41:22 +01:00
|
|
|
.map(|&(a, b)| (a.to_string(), b.as_feature_name()))
|
2022-12-08 03:53:35 +00:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
asm_target_features,
|
|
|
|
..*providers
|
|
|
|
}
|
2020-10-09 19:35:17 +02:00
|
|
|
}
|