From 1c4ca283df4e918ce2fdba1c5826c957a8daad3d Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 22 Jun 2022 17:48:54 -0400 Subject: [PATCH] Support #[target(enable)] function attribute --- Cargo.lock | 9 +++- Cargo.toml | 5 +- src/attributes.rs | 114 ++++++++++++++++++++++++++++++++++++++++++++++ src/base.rs | 28 +++++++----- src/callee.rs | 7 ++- src/declare.rs | 5 +- src/lib.rs | 22 +++++++++ src/mono_item.rs | 5 +- 8 files changed, 174 insertions(+), 21 deletions(-) create mode 100644 src/attributes.rs diff --git a/Cargo.lock b/Cargo.lock index 6df2102470f..e52e742ec6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,7 +41,6 @@ dependencies = [ [[package]] name = "gccjit" version = "1.0.0" -source = "git+https://github.com/antoyo/gccjit.rs#bdb86fb5092895ff5589726b33250010c64d93f6" dependencies = [ "gccjit_sys", ] @@ -49,7 +48,6 @@ dependencies = [ [[package]] name = "gccjit_sys" version = "0.0.1" -source = "git+https://github.com/antoyo/gccjit.rs#bdb86fb5092895ff5589726b33250010c64d93f6" dependencies = [ "libc 0.1.12", ] @@ -215,6 +213,7 @@ dependencies = [ "ar", "gccjit", "lang_tester", + "smallvec", "target-lexicon", "tempfile", ] @@ -228,6 +227,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "smallvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" + [[package]] name = "target-lexicon" version = "0.10.0" diff --git a/Cargo.toml b/Cargo.toml index 211d19a8dc8..26a0e92923d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,11 +22,12 @@ default = ["master"] master = ["gccjit/master"] [dependencies] -gccjit = { git = "https://github.com/antoyo/gccjit.rs" } +#gccjit = { git = "https://github.com/antoyo/gccjit.rs" } # Local copy. -#gccjit = { path = "../gccjit.rs" } +gccjit = { path = "../gccjit.rs" } +smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } target-lexicon = "0.10.0" ar = "0.8.0" diff --git a/src/attributes.rs b/src/attributes.rs new file mode 100644 index 00000000000..c52b10ee20a --- /dev/null +++ b/src/attributes.rs @@ -0,0 +1,114 @@ +use gccjit::{FnAttribute, Function}; +use rustc_attr::InstructionSetAttr; +use rustc_codegen_ssa::target_features::tied_target_features; +use rustc_data_structures::fx::FxHashMap; +use rustc_middle::ty; +use rustc_session::Session; +use rustc_span::symbol::sym; +use smallvec::{smallvec, SmallVec}; + +use crate::context::CodegenCx; + +// Given a map from target_features to whether they are enabled or disabled, +// ensure only valid combinations are allowed. +pub fn check_tied_features( + sess: &Session, + features: &FxHashMap<&str, bool>, +) -> Option<&'static [&'static str]> { + for tied in tied_target_features(sess) { + // Tied features must be set to the same value, or not set at all + let mut tied_iter = tied.iter(); + let enabled = features.get(tied_iter.next().unwrap()); + if tied_iter.any(|f| enabled != features.get(f)) { + return Some(tied); + } + } + None +} + +// TODO: maybe move to a new module gcc_util. +// To find a list of GCC's names, check https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html +fn to_gcc_features<'a>(sess: &Session, s: &'a str) -> SmallVec<[&'a str; 2]> { + let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch }; + match (arch, s) { + ("x86", "sse4.2") => smallvec!["sse4.2", "crc32"], + ("x86", "pclmulqdq") => smallvec!["pclmul"], + ("x86", "rdrand") => smallvec!["rdrnd"], + ("x86", "bmi1") => smallvec!["bmi"], + ("x86", "cmpxchg16b") => smallvec!["cx16"], + ("x86", "avx512vaes") => smallvec!["vaes"], + ("x86", "avx512gfni") => smallvec!["gfni"], + ("x86", "avx512vpclmulqdq") => smallvec!["vpclmulqdq"], + // NOTE: seems like GCC requires 'avx512bw' for 'avx512vbmi2'. + ("x86", "avx512vbmi2") => smallvec!["avx512vbmi2", "avx512bw"], + // NOTE: seems like GCC requires 'avx512bw' for 'avx512bitalg'. + ("x86", "avx512bitalg") => smallvec!["avx512bitalg", "avx512bw"], + ("aarch64", "rcpc2") => smallvec!["rcpc-immo"], + ("aarch64", "dpb") => smallvec!["ccpp"], + ("aarch64", "dpb2") => smallvec!["ccdp"], + ("aarch64", "frintts") => smallvec!["fptoint"], + ("aarch64", "fcma") => smallvec!["complxnum"], + ("aarch64", "pmuv3") => smallvec!["perfmon"], + ("aarch64", "paca") => smallvec!["pauth"], + ("aarch64", "pacg") => smallvec!["pauth"], + // Rust ties fp and neon together. In LLVM neon implicitly enables fp, + // but we manually enable neon when a feature only implicitly enables fp + ("aarch64", "f32mm") => smallvec!["f32mm", "neon"], + ("aarch64", "f64mm") => smallvec!["f64mm", "neon"], + ("aarch64", "fhm") => smallvec!["fp16fml", "neon"], + ("aarch64", "fp16") => smallvec!["fullfp16", "neon"], + ("aarch64", "jsconv") => smallvec!["jsconv", "neon"], + ("aarch64", "sve") => smallvec!["sve", "neon"], + ("aarch64", "sve2") => smallvec!["sve2", "neon"], + ("aarch64", "sve2-aes") => smallvec!["sve2-aes", "neon"], + ("aarch64", "sve2-sm4") => smallvec!["sve2-sm4", "neon"], + ("aarch64", "sve2-sha3") => smallvec!["sve2-sha3", "neon"], + ("aarch64", "sve2-bitperm") => smallvec!["sve2-bitperm", "neon"], + (_, s) => smallvec![s], + } +} + +/// Composite function which sets GCC attributes for function depending on its AST (`#[attribute]`) +/// attributes. +pub fn from_fn_attrs<'gcc, 'tcx>( + cx: &CodegenCx<'gcc, 'tcx>, + func: Function<'gcc>, + instance: ty::Instance<'tcx>, +) { + let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id()); + + let function_features = + codegen_fn_attrs.target_features.iter().map(|f| f.as_str()).collect::>(); + + if let Some(f) = check_tied_features(cx.tcx.sess, &function_features.iter().map(|f| (*f, true)).collect()) { + let span = cx.tcx + .get_attr(instance.def_id(), sym::target_feature) + .map_or_else(|| cx.tcx.def_span(instance.def_id()), |a| a.span); + let msg = format!( + "the target features {} must all be either enabled or disabled together", + f.join(", ") + ); + let mut err = cx.tcx.sess.struct_span_err(span, &msg); + err.help("add the missing features in a `target_feature` attribute"); + err.emit(); + return; + } + + let mut function_features = function_features + .iter() + .flat_map(|feat| to_gcc_features(cx.tcx.sess, feat).into_iter()) + .chain(codegen_fn_attrs.instruction_set.iter().map(|x| match x { + InstructionSetAttr::ArmA32 => "-thumb-mode", // TODO: support removing feature. + InstructionSetAttr::ArmT32 => "thumb-mode", + })) + .collect::>(); + + // TODO(antoyo): check if we really need global backend features. (Maybe they could be applied + // globally?) + let mut global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str()); + function_features.extend(&mut global_features); + let target_features = function_features.join(","); + if !target_features.is_empty() { + func.add_attribute(FnAttribute::Target, &target_features); + } +} diff --git a/src/base.rs b/src/base.rs index 94300142384..2f77978df1e 100644 --- a/src/base.rs +++ b/src/base.rs @@ -79,37 +79,41 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol, supports_ // TODO(antoyo): only set on x86 platforms. context.add_command_line_option("-masm=intel"); // TODO(antoyo): only add the following cli argument if the feature is supported. - context.add_command_line_option("-msse2"); + /*context.add_command_line_option("-msse2"); context.add_command_line_option("-mavx2"); // FIXME(antoyo): the following causes an illegal instruction on vmovdqu64 in std_example on my CPU. // Only add if the CPU supports it. - //context.add_command_line_option("-mavx512f"); context.add_command_line_option("-msha"); context.add_command_line_option("-mpclmul"); context.add_command_line_option("-mfma"); context.add_command_line_option("-mfma4"); - //context.add_command_line_option("-mavx512vpopcntdq"); - //context.add_command_line_option("-mavx512vl"); context.add_command_line_option("-m64"); context.add_command_line_option("-mbmi"); context.add_command_line_option("-mgfni"); context.add_command_line_option("-mavxvnni"); - //context.add_command_line_option("-mavx512vnni"); - //context.add_command_line_option("-mavx512bw"); context.add_command_line_option("-mf16c"); - //context.add_command_line_option("-mavx512bitalg"); context.add_command_line_option("-maes"); context.add_command_line_option("-mxsavec"); context.add_command_line_option("-mbmi2"); - //context.add_command_line_option("-mavx512bf16"); context.add_command_line_option("-mrtm"); context.add_command_line_option("-mvaes"); context.add_command_line_option("-mvpclmulqdq"); context.add_command_line_option("-mavx"); - //context.add_command_line_option("-mavx512vbmi2"); - //context.add_command_line_option("-mavx512vbmi"); - //context.add_command_line_option("-mavx512ifma"); - //context.add_command_line_option("-mavx512cd"); + + if env::var("CG_GCCJIT_ENABLE_AVX512").as_deref() == Ok("1") { + context.add_command_line_option("-mavx512f"); + context.add_command_line_option("-mavx512vpopcntdq"); + context.add_command_line_option("-mavx512vl"); + context.add_command_line_option("-mavx512vnni"); + context.add_command_line_option("-mavx512bw"); + context.add_command_line_option("-mavx512bitalg"); + context.add_command_line_option("-mavx512bf16"); + context.add_command_line_option("-mavx512vbmi2"); + context.add_command_line_option("-mavx512vbmi"); + context.add_command_line_option("-mavx512ifma"); + context.add_command_line_option("-mavx512cd"); + }*/ + for arg in &tcx.sess.opts.cg.llvm_args { context.add_command_line_option(arg); } diff --git a/src/callee.rs b/src/callee.rs index 76419b103d0..5557f886b28 100644 --- a/src/callee.rs +++ b/src/callee.rs @@ -4,6 +4,7 @@ use rustc_middle::ty::{self, Instance, TypeFoldable}; use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt}; use crate::abi::FnAbiGccExt; +use crate::attributes; use crate::context::CodegenCx; /// Codegens a reference to a fn/method item, monomorphizing and @@ -67,8 +68,12 @@ pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>) cx.linkage.set(FunctionType::Extern); let func = cx.declare_fn(&sym, &fn_abi); + attributes::from_fn_attrs(cx, func, instance); + // TODO(antoyo): set linkage and attributes. - func + + // FIXME(antoyo): this is a wrong cast. That requires changing the compiler API. + unsafe { std::mem::transmute(func) } }; cx.function_instances.borrow_mut().insert(instance, func); diff --git a/src/declare.rs b/src/declare.rs index a619e2f7712..5f6360a7da5 100644 --- a/src/declare.rs +++ b/src/declare.rs @@ -79,12 +79,11 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> { unsafe { std::mem::transmute(func) } } - pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> RValue<'gcc> { + pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Function<'gcc> { let (return_type, params, variadic, on_stack_param_indices) = fn_abi.gcc_type(self); let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, ¶ms, variadic); self.on_stack_function_params.borrow_mut().insert(func, on_stack_param_indices); - // FIXME(antoyo): this is a wrong cast. That requires changing the compiler API. - unsafe { std::mem::transmute(func) } + func } pub fn define_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option) -> LValue<'gcc> { diff --git a/src/lib.rs b/src/lib.rs index f83c1e53635..1ca2394abdc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ #![warn(unused_lifetimes)] extern crate rustc_ast; +extern crate rustc_attr; extern crate rustc_codegen_ssa; extern crate rustc_data_structures; extern crate rustc_errors; @@ -32,6 +33,7 @@ mod abi; mod allocator; mod archive; mod asm; +mod attributes; mod back; mod base; mod builder; @@ -188,6 +190,24 @@ pub struct GccContext { context: Context<'static>, } +impl GccContext { + fn new<'tcx>(tcx: TyCtxt<'tcx>) -> Self { + let context = create_context(tcx); + Self { + context, + } + } +} + +fn create_context<'gcc, 'tcx>(tcx: TyCtxt<'tcx>) -> Context<'gcc> { + let context = Context::default(); + if tcx.sess.target.is_builtin { + //let features = global_gcc_features(sess, false); + println!("Features: {:?}", tcx.sess.opts.cg.target_feature); + } + context +} + unsafe impl Send for GccContext {} // FIXME(antoyo): that shouldn't be Sync. Parallel compilation is currently disabled with "-Zno-parallel-llvm". Try to disable it here. unsafe impl Sync for GccContext {} @@ -302,6 +322,8 @@ pub fn target_features(sess: &Session) -> Vec { .filter(|_feature| { // TODO(antoyo): implement a way to get enabled feature in libgccjit. // Probably using the equivalent of __builtin_cpu_supports. + // TODO: maybe use whatever outputs the following command: + // gcc -march=native -Q --help=target #[cfg(feature="master")] { (_feature.contains("sse") || _feature.contains("avx")) && !_feature.contains("avx512") diff --git a/src/mono_item.rs b/src/mono_item.rs index e21d40b6c37..60a42846bd3 100644 --- a/src/mono_item.rs +++ b/src/mono_item.rs @@ -5,6 +5,7 @@ use rustc_middle::ty::{self, Instance, TypeFoldable}; use rustc_middle::ty::layout::{FnAbiOf, LayoutOf}; use rustc_span::def_id::DefId; +use crate::attributes; use crate::base; use crate::context::CodegenCx; use crate::type_of::LayoutGccExt; @@ -28,9 +29,11 @@ impl<'gcc, 'tcx> PreDefineMethods<'tcx> for CodegenCx<'gcc, 'tcx> { let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty()); self.linkage.set(base::linkage_to_gcc(linkage)); - let _decl = self.declare_fn(symbol_name, &fn_abi); + let decl = self.declare_fn(symbol_name, &fn_abi); //let attrs = self.tcx.codegen_fn_attrs(instance.def_id()); + attributes::from_fn_attrs(self, decl, instance); + // TODO(antoyo): call set_link_section() to allow initializing argc/argv. // TODO(antoyo): set unique comdat. // TODO(antoyo): use inline attribute from there in linkage.set() above.