|
| 1 | +use smallvec::{smallvec, SmallVec}; |
| 2 | + |
| 3 | +use rustc_codegen_ssa::target_features::{ |
| 4 | + supported_target_features, tied_target_features, RUSTC_SPECIFIC_FEATURES, |
| 5 | +}; |
| 6 | +use rustc_data_structures::fx::FxHashMap; |
| 7 | +use rustc_middle::bug; |
| 8 | +use rustc_session::Session; |
| 9 | + |
| 10 | +use crate::errors::{PossibleFeature, TargetFeatureDisableOrEnable, UnknownCTargetFeature, UnknownCTargetFeaturePrefix}; |
| 11 | + |
| 12 | +/// The list of GCC features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`, |
| 13 | +/// `--target` and similar). |
| 14 | +pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<String> { |
| 15 | + // Features that come earlier are overridden by conflicting features later in the string. |
| 16 | + // Typically we'll want more explicit settings to override the implicit ones, so: |
| 17 | + // |
| 18 | + // * Features from -Ctarget-cpu=*; are overridden by [^1] |
| 19 | + // * Features implied by --target; are overridden by |
| 20 | + // * Features from -Ctarget-feature; are overridden by |
| 21 | + // * function specific features. |
| 22 | + // |
| 23 | + // [^1]: target-cpu=native is handled here, other target-cpu values are handled implicitly |
| 24 | + // through GCC TargetMachine implementation. |
| 25 | + // |
| 26 | + // FIXME(nagisa): it isn't clear what's the best interaction between features implied by |
| 27 | + // `-Ctarget-cpu` and `--target` are. On one hand, you'd expect CLI arguments to always |
| 28 | + // override anything that's implicit, so e.g. when there's no `--target` flag, features implied |
| 29 | + // the host target are overridden by `-Ctarget-cpu=*`. On the other hand, what about when both |
| 30 | + // `--target` and `-Ctarget-cpu=*` are specified? Both then imply some target features and both |
| 31 | + // flags are specified by the user on the CLI. It isn't as clear-cut which order of precedence |
| 32 | + // should be taken in cases like these. |
| 33 | + let mut features = vec![]; |
| 34 | + |
| 35 | + // TODO(antoyo): -Ctarget-cpu=native |
| 36 | + |
| 37 | + // Features implied by an implicit or explicit `--target`. |
| 38 | + features.extend( |
| 39 | + sess.target |
| 40 | + .features |
| 41 | + .split(',') |
| 42 | + .filter(|v| !v.is_empty() && backend_feature_name(v).is_some()) |
| 43 | + .map(String::from), |
| 44 | + ); |
| 45 | + |
| 46 | + // -Ctarget-features |
| 47 | + let supported_features = supported_target_features(sess); |
| 48 | + let mut featsmap = FxHashMap::default(); |
| 49 | + let feats = sess.opts.cg.target_feature |
| 50 | + .split(',') |
| 51 | + .filter_map(|s| { |
| 52 | + let enable_disable = match s.chars().next() { |
| 53 | + None => return None, |
| 54 | + Some(c @ ('+' | '-')) => c, |
| 55 | + Some(_) => { |
| 56 | + if diagnostics { |
| 57 | + sess.emit_warning(UnknownCTargetFeaturePrefix { feature: s }); |
| 58 | + } |
| 59 | + return None; |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + let feature = backend_feature_name(s)?; |
| 64 | + // Warn against use of GCC specific feature names on the CLI. |
| 65 | + if diagnostics && !supported_features.iter().any(|&(v, _)| v == feature) { |
| 66 | + let rust_feature = supported_features.iter().find_map(|&(rust_feature, _)| { |
| 67 | + let gcc_features = to_gcc_features(sess, rust_feature); |
| 68 | + if gcc_features.contains(&feature) && !gcc_features.contains(&rust_feature) { |
| 69 | + Some(rust_feature) |
| 70 | + } else { |
| 71 | + None |
| 72 | + } |
| 73 | + }); |
| 74 | + let unknown_feature = |
| 75 | + if let Some(rust_feature) = rust_feature { |
| 76 | + UnknownCTargetFeature { |
| 77 | + feature, |
| 78 | + rust_feature: PossibleFeature::Some { rust_feature }, |
| 79 | + } |
| 80 | + } |
| 81 | + else { |
| 82 | + UnknownCTargetFeature { feature, rust_feature: PossibleFeature::None } |
| 83 | + }; |
| 84 | + sess.emit_warning(unknown_feature); |
| 85 | + } |
| 86 | + |
| 87 | + if diagnostics { |
| 88 | + // FIXME(nagisa): figure out how to not allocate a full hashset here. |
| 89 | + featsmap.insert(feature, enable_disable == '+'); |
| 90 | + } |
| 91 | + |
| 92 | + // rustc-specific features do not get passed down to GCC… |
| 93 | + if RUSTC_SPECIFIC_FEATURES.contains(&feature) { |
| 94 | + return None; |
| 95 | + } |
| 96 | + // ... otherwise though we run through `to_gcc_features` when |
| 97 | + // passing requests down to GCC. This means that all in-language |
| 98 | + // features also work on the command line instead of having two |
| 99 | + // different names when the GCC name and the Rust name differ. |
| 100 | + Some(to_gcc_features(sess, feature) |
| 101 | + .iter() |
| 102 | + .flat_map(|feat| to_gcc_features(sess, feat).into_iter()) |
| 103 | + .map(String::from) |
| 104 | + .collect::<Vec<_>>(), |
| 105 | + ) |
| 106 | + }) |
| 107 | + .flatten(); |
| 108 | + features.extend(feats); |
| 109 | + |
| 110 | + if diagnostics { |
| 111 | + if let Some(f) = check_tied_features(sess, &featsmap) { |
| 112 | + sess.emit_err(TargetFeatureDisableOrEnable { |
| 113 | + features: f, |
| 114 | + span: None, |
| 115 | + missing_features: None, |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + features |
| 121 | +} |
| 122 | + |
| 123 | +/// Returns a feature name for the given `+feature` or `-feature` string. |
| 124 | +/// |
| 125 | +/// Only allows features that are backend specific (i.e. not [`RUSTC_SPECIFIC_FEATURES`].) |
| 126 | +fn backend_feature_name(s: &str) -> Option<&str> { |
| 127 | + // features must start with a `+` or `-`. |
| 128 | + let feature = s.strip_prefix(&['+', '-'][..]).unwrap_or_else(|| { |
| 129 | + bug!("target feature `{}` must begin with a `+` or `-`", s); |
| 130 | + }); |
| 131 | + // Rustc-specific feature requests like `+crt-static` or `-crt-static` |
| 132 | + // are not passed down to GCC. |
| 133 | + if RUSTC_SPECIFIC_FEATURES.contains(&feature) { |
| 134 | + return None; |
| 135 | + } |
| 136 | + Some(feature) |
| 137 | +} |
| 138 | + |
| 139 | +// TODO(antoyo): maybe move to a new module gcc_util. |
| 140 | +// To find a list of GCC's names, check https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| 141 | +pub fn to_gcc_features<'a>(sess: &Session, s: &'a str) -> SmallVec<[&'a str; 2]> { |
| 142 | + let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch }; |
| 143 | + match (arch, s) { |
| 144 | + ("x86", "sse4.2") => smallvec!["sse4.2", "crc32"], |
| 145 | + ("x86", "pclmulqdq") => smallvec!["pclmul"], |
| 146 | + ("x86", "rdrand") => smallvec!["rdrnd"], |
| 147 | + ("x86", "bmi1") => smallvec!["bmi"], |
| 148 | + ("x86", "cmpxchg16b") => smallvec!["cx16"], |
| 149 | + ("x86", "avx512vaes") => smallvec!["vaes"], |
| 150 | + ("x86", "avx512gfni") => smallvec!["gfni"], |
| 151 | + ("x86", "avx512vpclmulqdq") => smallvec!["vpclmulqdq"], |
| 152 | + // NOTE: seems like GCC requires 'avx512bw' for 'avx512vbmi2'. |
| 153 | + ("x86", "avx512vbmi2") => smallvec!["avx512vbmi2", "avx512bw"], |
| 154 | + // NOTE: seems like GCC requires 'avx512bw' for 'avx512bitalg'. |
| 155 | + ("x86", "avx512bitalg") => smallvec!["avx512bitalg", "avx512bw"], |
| 156 | + ("aarch64", "rcpc2") => smallvec!["rcpc-immo"], |
| 157 | + ("aarch64", "dpb") => smallvec!["ccpp"], |
| 158 | + ("aarch64", "dpb2") => smallvec!["ccdp"], |
| 159 | + ("aarch64", "frintts") => smallvec!["fptoint"], |
| 160 | + ("aarch64", "fcma") => smallvec!["complxnum"], |
| 161 | + ("aarch64", "pmuv3") => smallvec!["perfmon"], |
| 162 | + ("aarch64", "paca") => smallvec!["pauth"], |
| 163 | + ("aarch64", "pacg") => smallvec!["pauth"], |
| 164 | + // Rust ties fp and neon together. In GCC neon implicitly enables fp, |
| 165 | + // but we manually enable neon when a feature only implicitly enables fp |
| 166 | + ("aarch64", "f32mm") => smallvec!["f32mm", "neon"], |
| 167 | + ("aarch64", "f64mm") => smallvec!["f64mm", "neon"], |
| 168 | + ("aarch64", "fhm") => smallvec!["fp16fml", "neon"], |
| 169 | + ("aarch64", "fp16") => smallvec!["fullfp16", "neon"], |
| 170 | + ("aarch64", "jsconv") => smallvec!["jsconv", "neon"], |
| 171 | + ("aarch64", "sve") => smallvec!["sve", "neon"], |
| 172 | + ("aarch64", "sve2") => smallvec!["sve2", "neon"], |
| 173 | + ("aarch64", "sve2-aes") => smallvec!["sve2-aes", "neon"], |
| 174 | + ("aarch64", "sve2-sm4") => smallvec!["sve2-sm4", "neon"], |
| 175 | + ("aarch64", "sve2-sha3") => smallvec!["sve2-sha3", "neon"], |
| 176 | + ("aarch64", "sve2-bitperm") => smallvec!["sve2-bitperm", "neon"], |
| 177 | + (_, s) => smallvec![s], |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +// Given a map from target_features to whether they are enabled or disabled, |
| 182 | +// ensure only valid combinations are allowed. |
| 183 | +pub fn check_tied_features(sess: &Session, features: &FxHashMap<&str, bool>) -> Option<&'static [&'static str]> { |
| 184 | + for tied in tied_target_features(sess) { |
| 185 | + // Tied features must be set to the same value, or not set at all |
| 186 | + let mut tied_iter = tied.iter(); |
| 187 | + let enabled = features.get(tied_iter.next().unwrap()); |
| 188 | + if tied_iter.any(|feature| enabled != features.get(feature)) { |
| 189 | + return Some(tied); |
| 190 | + } |
| 191 | + } |
| 192 | + None |
| 193 | +} |
0 commit comments