|
| 1 | +#[cfg_attr(not(feature="master"), allow(unused_imports))] |
| 2 | +use gccjit::FnAttribute; |
| 3 | +use gccjit::Function; |
| 4 | +use rustc_attr::InstructionSetAttr; |
| 5 | +use rustc_codegen_ssa::target_features::tied_target_features; |
| 6 | +use rustc_data_structures::fx::FxHashMap; |
| 7 | +use rustc_middle::ty; |
| 8 | +use rustc_session::Session; |
| 9 | +use rustc_span::symbol::sym; |
| 10 | +use smallvec::{smallvec, SmallVec}; |
| 11 | + |
| 12 | +use crate::context::CodegenCx; |
| 13 | + |
| 14 | +// Given a map from target_features to whether they are enabled or disabled, |
| 15 | +// ensure only valid combinations are allowed. |
| 16 | +pub fn check_tied_features(sess: &Session, features: &FxHashMap<&str, bool>) -> Option<&'static [&'static str]> { |
| 17 | + for tied in tied_target_features(sess) { |
| 18 | + // Tied features must be set to the same value, or not set at all |
| 19 | + let mut tied_iter = tied.iter(); |
| 20 | + let enabled = features.get(tied_iter.next().unwrap()); |
| 21 | + if tied_iter.any(|feature| enabled != features.get(feature)) { |
| 22 | + return Some(tied); |
| 23 | + } |
| 24 | + } |
| 25 | + None |
| 26 | +} |
| 27 | + |
| 28 | +// TODO(antoyo): maybe move to a new module gcc_util. |
| 29 | +// To find a list of GCC's names, check https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| 30 | +fn to_gcc_features<'a>(sess: &Session, s: &'a str) -> SmallVec<[&'a str; 2]> { |
| 31 | + let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch }; |
| 32 | + match (arch, s) { |
| 33 | + ("x86", "sse4.2") => smallvec!["sse4.2", "crc32"], |
| 34 | + ("x86", "pclmulqdq") => smallvec!["pclmul"], |
| 35 | + ("x86", "rdrand") => smallvec!["rdrnd"], |
| 36 | + ("x86", "bmi1") => smallvec!["bmi"], |
| 37 | + ("x86", "cmpxchg16b") => smallvec!["cx16"], |
| 38 | + ("x86", "avx512vaes") => smallvec!["vaes"], |
| 39 | + ("x86", "avx512gfni") => smallvec!["gfni"], |
| 40 | + ("x86", "avx512vpclmulqdq") => smallvec!["vpclmulqdq"], |
| 41 | + // NOTE: seems like GCC requires 'avx512bw' for 'avx512vbmi2'. |
| 42 | + ("x86", "avx512vbmi2") => smallvec!["avx512vbmi2", "avx512bw"], |
| 43 | + // NOTE: seems like GCC requires 'avx512bw' for 'avx512bitalg'. |
| 44 | + ("x86", "avx512bitalg") => smallvec!["avx512bitalg", "avx512bw"], |
| 45 | + ("aarch64", "rcpc2") => smallvec!["rcpc-immo"], |
| 46 | + ("aarch64", "dpb") => smallvec!["ccpp"], |
| 47 | + ("aarch64", "dpb2") => smallvec!["ccdp"], |
| 48 | + ("aarch64", "frintts") => smallvec!["fptoint"], |
| 49 | + ("aarch64", "fcma") => smallvec!["complxnum"], |
| 50 | + ("aarch64", "pmuv3") => smallvec!["perfmon"], |
| 51 | + ("aarch64", "paca") => smallvec!["pauth"], |
| 52 | + ("aarch64", "pacg") => smallvec!["pauth"], |
| 53 | + // Rust ties fp and neon together. In LLVM neon implicitly enables fp, |
| 54 | + // but we manually enable neon when a feature only implicitly enables fp |
| 55 | + ("aarch64", "f32mm") => smallvec!["f32mm", "neon"], |
| 56 | + ("aarch64", "f64mm") => smallvec!["f64mm", "neon"], |
| 57 | + ("aarch64", "fhm") => smallvec!["fp16fml", "neon"], |
| 58 | + ("aarch64", "fp16") => smallvec!["fullfp16", "neon"], |
| 59 | + ("aarch64", "jsconv") => smallvec!["jsconv", "neon"], |
| 60 | + ("aarch64", "sve") => smallvec!["sve", "neon"], |
| 61 | + ("aarch64", "sve2") => smallvec!["sve2", "neon"], |
| 62 | + ("aarch64", "sve2-aes") => smallvec!["sve2-aes", "neon"], |
| 63 | + ("aarch64", "sve2-sm4") => smallvec!["sve2-sm4", "neon"], |
| 64 | + ("aarch64", "sve2-sha3") => smallvec!["sve2-sha3", "neon"], |
| 65 | + ("aarch64", "sve2-bitperm") => smallvec!["sve2-bitperm", "neon"], |
| 66 | + (_, s) => smallvec![s], |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/// Composite function which sets GCC attributes for function depending on its AST (`#[attribute]`) |
| 71 | +/// attributes. |
| 72 | +pub fn from_fn_attrs<'gcc, 'tcx>( |
| 73 | + cx: &CodegenCx<'gcc, 'tcx>, |
| 74 | + #[cfg_attr(not(feature="master"), allow(unused_variables))] |
| 75 | + func: Function<'gcc>, |
| 76 | + instance: ty::Instance<'tcx>, |
| 77 | +) { |
| 78 | + let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id()); |
| 79 | + |
| 80 | + let function_features = |
| 81 | + codegen_fn_attrs.target_features.iter().map(|features| features.as_str()).collect::<Vec<&str>>(); |
| 82 | + |
| 83 | + if let Some(features) = check_tied_features(cx.tcx.sess, &function_features.iter().map(|features| (*features, true)).collect()) { |
| 84 | + let span = cx.tcx |
| 85 | + .get_attr(instance.def_id(), sym::target_feature) |
| 86 | + .map_or_else(|| cx.tcx.def_span(instance.def_id()), |a| a.span); |
| 87 | + let msg = format!("the target features {} must all be either enabled or disabled together", features.join(", ")); |
| 88 | + let mut err = cx.tcx.sess.struct_span_err(span, &msg); |
| 89 | + err.help("add the missing features in a `target_feature` attribute"); |
| 90 | + err.emit(); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + let mut function_features = function_features |
| 95 | + .iter() |
| 96 | + .flat_map(|feat| to_gcc_features(cx.tcx.sess, feat).into_iter()) |
| 97 | + .chain(codegen_fn_attrs.instruction_set.iter().map(|x| match x { |
| 98 | + InstructionSetAttr::ArmA32 => "-thumb-mode", // TODO(antoyo): support removing feature. |
| 99 | + InstructionSetAttr::ArmT32 => "thumb-mode", |
| 100 | + })) |
| 101 | + .collect::<Vec<_>>(); |
| 102 | + |
| 103 | + // TODO(antoyo): check if we really need global backend features. (Maybe they could be applied |
| 104 | + // globally?) |
| 105 | + let mut global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str()); |
| 106 | + function_features.extend(&mut global_features); |
| 107 | + let target_features = function_features.join(","); |
| 108 | + if !target_features.is_empty() { |
| 109 | + #[cfg(feature="master")] |
| 110 | + func.add_attribute(FnAttribute::Target, &target_features); |
| 111 | + } |
| 112 | +} |
0 commit comments