From 9b903ca6e8b1fa49caae21be3b807d28ecda1517 Mon Sep 17 00:00:00 2001 From: sayantn Date: Fri, 30 May 2025 19:56:18 +0530 Subject: [PATCH 01/10] Codegen **non-overloaded** LLVM intrinsics using their name --- compiler/rustc_codegen_gcc/src/type_of.rs | 8 +- compiler/rustc_codegen_llvm/src/abi.rs | 161 +++++++++++++++--- compiler/rustc_codegen_llvm/src/builder.rs | 6 +- compiler/rustc_codegen_llvm/src/declare.rs | 41 +++-- compiler/rustc_codegen_llvm/src/intrinsic.rs | 2 +- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 8 + compiler/rustc_codegen_llvm/src/llvm/mod.rs | 8 + compiler/rustc_codegen_llvm/src/type_.rs | 12 +- compiler/rustc_codegen_ssa/src/mir/block.rs | 4 +- compiler/rustc_codegen_ssa/src/mir/rvalue.rs | 2 +- compiler/rustc_codegen_ssa/src/size_of_val.rs | 2 +- .../rustc_codegen_ssa/src/traits/type_.rs | 6 +- 12 files changed, 214 insertions(+), 46 deletions(-) diff --git a/compiler/rustc_codegen_gcc/src/type_of.rs b/compiler/rustc_codegen_gcc/src/type_of.rs index 5745acce6fee7..25b7a2e8c682b 100644 --- a/compiler/rustc_codegen_gcc/src/type_of.rs +++ b/compiler/rustc_codegen_gcc/src/type_of.rs @@ -1,6 +1,6 @@ use std::fmt::Write; -use gccjit::{Struct, Type}; +use gccjit::{RValue, Struct, Type}; use rustc_abi as abi; use rustc_abi::Primitive::*; use rustc_abi::{ @@ -373,7 +373,11 @@ impl<'gcc, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> { unimplemented!(); } - fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> { + fn fn_decl_backend_type( + &self, + fn_abi: &FnAbi<'tcx, Ty<'tcx>>, + _fn_ptr: RValue<'gcc>, + ) -> Type<'gcc> { // FIXME(antoyo): Should we do something with `FnAbiGcc::fn_attributes`? let FnAbiGcc { return_type, arguments_type, is_c_variadic, .. } = fn_abi.gcc_type(self); self.context.new_function_pointer_type(None, return_type, &arguments_type, is_c_variadic) diff --git a/compiler/rustc_codegen_llvm/src/abi.rs b/compiler/rustc_codegen_llvm/src/abi.rs index aba63d75f1dfa..fa0d68b677982 100644 --- a/compiler/rustc_codegen_llvm/src/abi.rs +++ b/compiler/rustc_codegen_llvm/src/abi.rs @@ -1,5 +1,5 @@ use std::borrow::Borrow; -use std::cmp; +use std::{cmp, iter}; use libc::c_uint; use rustc_abi::{ @@ -300,8 +300,39 @@ impl<'ll, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { } } +pub(crate) enum FunctionSignature<'ll> { + /// The signature is obtained directly from LLVM, and **may not match the Rust signature** + Intrinsic(&'ll Type), + /// The name starts with `llvm.`, but can't obtain the intrinsic ID. May be invalid or upgradable + MaybeInvalidIntrinsic(&'ll Type), + /// Just the Rust signature + Rust(&'ll Type), +} + +impl<'ll> FunctionSignature<'ll> { + pub(crate) fn fn_ty(&self) -> &'ll Type { + match self { + FunctionSignature::Intrinsic(fn_ty) + | FunctionSignature::MaybeInvalidIntrinsic(fn_ty) + | FunctionSignature::Rust(fn_ty) => fn_ty, + } + } +} + pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> { - fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type; + fn llvm_return_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type; + fn llvm_argument_types(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<&'ll Type>; + /// When `do_verify` is set, this function performs checks for the signature of LLVM intrinsics + /// and emits a fatal error if it doesn't match. These checks are important,but somewhat expensive + /// So they are only used at function definitions, not at callsites + fn llvm_type( + &self, + cx: &CodegenCx<'ll, 'tcx>, + name: &[u8], + do_verify: bool, + ) -> FunctionSignature<'ll>; + /// **If this function is an LLVM intrinsic** checks if the LLVM signature provided matches with this + fn verify_intrinsic_signature(&self, cx: &CodegenCx<'ll, 'tcx>, llvm_ty: &'ll Type) -> bool; fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type; fn llvm_cconv(&self, cx: &CodegenCx<'ll, 'tcx>) -> llvm::CallConv; @@ -314,30 +345,38 @@ pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> { ); /// Apply attributes to a function call. - fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value); + fn apply_attrs_callsite( + &self, + bx: &mut Builder<'_, 'll, 'tcx>, + callsite: &'ll Value, + llfn: &'ll Value, + ); } impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> { - fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type { + fn llvm_return_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type { + match &self.ret.mode { + PassMode::Ignore => cx.type_void(), + PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx), + PassMode::Cast { cast, pad_i32: _ } => cast.llvm_type(cx), + PassMode::Indirect { .. } => cx.type_void(), + } + } + + fn llvm_argument_types(&self, cx: &CodegenCx<'ll, 'tcx>) -> Vec<&'ll Type> { + let indirect_return = matches!(self.ret.mode, PassMode::Indirect { .. }); + // Ignore "extra" args from the call site for C variadic functions. // Only the "fixed" args are part of the LLVM function signature. let args = if self.c_variadic { &self.args[..self.fixed_count as usize] } else { &self.args }; - // This capacity calculation is approximate. - let mut llargument_tys = Vec::with_capacity( - self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 }, - ); + let mut llargument_tys = + Vec::with_capacity(args.len() + if indirect_return { 1 } else { 0 }); - let llreturn_ty = match &self.ret.mode { - PassMode::Ignore => cx.type_void(), - PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx), - PassMode::Cast { cast, pad_i32: _ } => cast.llvm_type(cx), - PassMode::Indirect { .. } => { - llargument_tys.push(cx.type_ptr()); - cx.type_void() - } - }; + if indirect_return { + llargument_tys.push(cx.type_ptr()); + } for arg in args { // Note that the exact number of arguments pushed here is carefully synchronized with @@ -384,10 +423,74 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> { llargument_tys.push(llarg_ty); } - if self.c_variadic { - cx.type_variadic_func(&llargument_tys, llreturn_ty) + llargument_tys + } + + fn verify_intrinsic_signature(&self, cx: &CodegenCx<'ll, 'tcx>, llvm_fn_ty: &'ll Type) -> bool { + let rust_return_ty = self.llvm_return_type(cx); + let rust_argument_tys = self.llvm_argument_types(cx); + + let llvm_return_ty = cx.get_return_type(llvm_fn_ty); + let llvm_argument_tys = cx.func_params_types(llvm_fn_ty); + let llvm_is_variadic = cx.func_is_variadic(llvm_fn_ty); + + if self.c_variadic != llvm_is_variadic || rust_argument_tys.len() != llvm_argument_tys.len() + { + return false; + } + + // todo: add bypasses for types not accessible from Rust here + iter::once((rust_return_ty, llvm_return_ty)) + .chain(iter::zip(rust_argument_tys, llvm_argument_tys)) + .all(|(rust_ty, llvm_ty)| rust_ty == llvm_ty) + } + + fn llvm_type( + &self, + cx: &CodegenCx<'ll, 'tcx>, + name: &[u8], + do_verify: bool, + ) -> FunctionSignature<'ll> { + let mut maybe_invalid = false; + + if name.starts_with(b"llvm.") { + if let Some(intrinsic) = llvm::Intrinsic::lookup(name) { + if !intrinsic.is_overloaded() { + // FIXME: also do this for overloaded intrinsics + let llvm_fn_ty = intrinsic.get_type(cx.llcx, &[]); + if do_verify { + if !self.verify_intrinsic_signature(cx, llvm_fn_ty) { + cx.tcx.dcx().fatal(format!( + "Intrinsic signature mismatch for `{}`: expected signature `{llvm_fn_ty:?}`", + str::from_utf8(name).unwrap() + )); + } + } + return FunctionSignature::Intrinsic(llvm_fn_ty); + } + } else { + // it's one of 2 cases, + // - either the base name is invalid + // - it has been superceded by something else, so the intrinsic was removed entirely + // to check for upgrades, we need the `llfn`, so we defer it for now + + maybe_invalid = true; + } + } + + let return_ty = self.llvm_return_type(cx); + let argument_tys = self.llvm_argument_types(cx); + + let fn_ty = if self.c_variadic { + cx.type_variadic_func(&argument_tys, return_ty) } else { - cx.type_func(&llargument_tys, llreturn_ty) + cx.type_func(&argument_tys, return_ty) + }; + + if maybe_invalid { + FunctionSignature::MaybeInvalidIntrinsic(fn_ty) + } else { + FunctionSignature::Rust(fn_ty) } } @@ -530,7 +633,23 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> { } } - fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value) { + fn apply_attrs_callsite( + &self, + bx: &mut Builder<'_, 'll, 'tcx>, + callsite: &'ll Value, + llfn: &'ll Value, + ) { + // if we are using the LLVM signature, use the LLVM attributes otherwise it might be problematic + let name = llvm::get_value_name(llfn); + if name.starts_with(b"llvm.") + && let Some(intrinsic) = llvm::Intrinsic::lookup(name) + { + // FIXME: also do this for overloaded intrinsics + if !intrinsic.is_overloaded() { + return; + } + } + let mut func_attrs = SmallVec::<[_; 2]>::new(); if self.ret.layout.is_uninhabited() { func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(bx.cx.llcx)); diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 5e9594dd06bb7..5e782b9c038ca 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -379,7 +379,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { ) }; if let Some(fn_abi) = fn_abi { - fn_abi.apply_attrs_callsite(self, invoke); + fn_abi.apply_attrs_callsite(self, invoke, llfn); } invoke } @@ -1372,7 +1372,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { ) }; if let Some(fn_abi) = fn_abi { - fn_abi.apply_attrs_callsite(self, call); + fn_abi.apply_attrs_callsite(self, call, llfn); } call } @@ -1695,7 +1695,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { ) }; if let Some(fn_abi) = fn_abi { - fn_abi.apply_attrs_callsite(self, callbr); + fn_abi.apply_attrs_callsite(self, callbr, llfn); } callbr } diff --git a/compiler/rustc_codegen_llvm/src/declare.rs b/compiler/rustc_codegen_llvm/src/declare.rs index 2419ec1f88854..0692e23b7c84a 100644 --- a/compiler/rustc_codegen_llvm/src/declare.rs +++ b/compiler/rustc_codegen_llvm/src/declare.rs @@ -22,7 +22,7 @@ use rustc_target::callconv::FnAbi; use smallvec::SmallVec; use tracing::debug; -use crate::abi::FnAbiLlvmExt; +use crate::abi::{FnAbiLlvmExt, FunctionSignature}; use crate::common::AsCCharPtr; use crate::context::{CodegenCx, GenericCx, SCx, SimpleCx}; use crate::llvm::AttributePlace::Function; @@ -150,17 +150,34 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { ) -> &'ll Value { debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi); - // Function addresses in Rust are never significant, allowing functions to - // be merged. - let llfn = declare_raw_fn( - self, - name, - fn_abi.llvm_cconv(self), - llvm::UnnamedAddr::Global, - llvm::Visibility::Default, - fn_abi.llvm_type(self), - ); - fn_abi.apply_attrs_llfn(self, llfn, instance); + let signature = fn_abi.llvm_type(self, name.as_bytes(), true); + let llfn; + + if let FunctionSignature::Intrinsic(fn_ty) = signature { + // intrinsics have a specified set of attributes, so we don't use the `FnAbi` set for them + llfn = declare_simple_fn( + self, + name, + fn_abi.llvm_cconv(self), + llvm::UnnamedAddr::Global, + llvm::Visibility::Default, + fn_ty, + ); + } else { + // Function addresses in Rust are never significant, allowing functions to + // be merged. + llfn = declare_raw_fn( + self, + name, + fn_abi.llvm_cconv(self), + llvm::UnnamedAddr::Global, + llvm::Visibility::Default, + signature.fn_ty(), + ); + fn_abi.apply_attrs_llfn(self, llfn, instance); + } + + // todo: check for upgrades, and emit error if not upgradable if self.tcx.sess.is_sanitizer_cfi_enabled() { if let Some(instance) = instance { diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index f7f062849a8b5..826e997ad84e9 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -1059,7 +1059,7 @@ fn gen_fn<'a, 'll, 'tcx>( codegen: &mut dyn FnMut(Builder<'a, 'll, 'tcx>), ) -> (&'ll Type, &'ll Value) { let fn_abi = cx.fn_abi_of_fn_ptr(rust_fn_sig, ty::List::empty()); - let llty = fn_abi.llvm_type(cx); + let llty = fn_abi.llvm_type(cx, name.as_bytes(), true).fn_ty(); let llfn = cx.declare_fn(name, fn_abi, None); cx.set_frame_pointer_type(llfn); cx.apply_target_cpu_attr(llfn); diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 91ada856d5977..80537b81ed61a 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -1060,6 +1060,7 @@ unsafe extern "C" { ) -> &'a Type; pub(crate) fn LLVMCountParamTypes(FunctionTy: &Type) -> c_uint; pub(crate) fn LLVMGetParamTypes<'a>(FunctionTy: &'a Type, Dest: *mut &'a Type); + pub(crate) fn LLVMIsFunctionVarArg(FunctionTy: &Type) -> Bool; // Operations on struct types pub(crate) fn LLVMStructTypeInContext<'a>( @@ -1196,6 +1197,13 @@ unsafe extern "C" { // Operations about llvm intrinsics pub(crate) fn LLVMLookupIntrinsicID(Name: *const c_char, NameLen: size_t) -> c_uint; + pub(crate) fn LLVMIntrinsicIsOverloaded(ID: NonZero) -> Bool; + pub(crate) fn LLVMIntrinsicGetType<'a>( + C: &'a Context, + ID: NonZero, + ParamTypes: *const &'a Type, + ParamCount: size_t, + ) -> &'a Type; pub(crate) fn LLVMGetIntrinsicDeclaration<'a>( Mod: &'a Module, ID: NonZero, diff --git a/compiler/rustc_codegen_llvm/src/llvm/mod.rs b/compiler/rustc_codegen_llvm/src/llvm/mod.rs index 661174a80dfbd..ea462cb72a318 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/mod.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/mod.rs @@ -339,6 +339,14 @@ impl Intrinsic { NonZero::new(id).map(|id| Self { id }) } + pub(crate) fn is_overloaded(self) -> bool { + unsafe { LLVMIntrinsicIsOverloaded(self.id) == True } + } + + pub(crate) fn get_type<'ll>(self, llcx: &'ll Context, type_params: &[&'ll Type]) -> &'ll Type { + unsafe { LLVMIntrinsicGetType(llcx, self.id, type_params.as_ptr(), type_params.len()) } + } + pub(crate) fn get_declaration<'ll>( self, llmod: &'ll Module, diff --git a/compiler/rustc_codegen_llvm/src/type_.rs b/compiler/rustc_codegen_llvm/src/type_.rs index 453eca2bbe173..0727722cd0559 100644 --- a/compiler/rustc_codegen_llvm/src/type_.rs +++ b/compiler/rustc_codegen_llvm/src/type_.rs @@ -77,6 +77,10 @@ impl<'ll, CX: Borrow>> GenericCx<'ll, CX> { args } } + + pub(crate) fn func_is_variadic(&self, ty: &'ll Type) -> bool { + unsafe { llvm::LLVMIsFunctionVarArg(ty) == True } + } } impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { pub(crate) fn type_bool(&self) -> &'ll Type { @@ -286,8 +290,12 @@ impl<'ll, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { fn cast_backend_type(&self, ty: &CastTarget) -> &'ll Type { ty.llvm_type(self) } - fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type { - fn_abi.llvm_type(self) + fn fn_decl_backend_type( + &self, + fn_abi: &FnAbi<'tcx, Ty<'tcx>>, + fn_ptr: &'ll Value, + ) -> &'ll Type { + fn_abi.llvm_type(self, llvm::get_value_name(fn_ptr), false).fn_ty() } fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type { fn_abi.ptr_to_llvm_type(self) diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index 43b87171d510d..993b918ab2e9b 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -187,7 +187,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> { // If there is a cleanup block and the function we're calling can unwind, then // do an invoke, otherwise do a call. - let fn_ty = bx.fn_decl_backend_type(fn_abi); + let fn_ty = bx.fn_decl_backend_type(fn_abi, fn_ptr); let fn_attrs = if bx.tcx().def_kind(fx.instance.def_id()).has_codegen_attrs() { Some(bx.tcx().codegen_fn_attrs(fx.instance.def_id())) @@ -1835,7 +1835,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { if is_call_from_compiler_builtins_to_upstream_monomorphization(bx.tcx(), instance) { bx.abort(); } else { - let fn_ty = bx.fn_decl_backend_type(fn_abi); + let fn_ty = bx.fn_decl_backend_type(fn_abi, fn_ptr); let llret = bx.call(fn_ty, None, Some(fn_abi), fn_ptr, &[], funclet.as_ref(), None); bx.apply_attrs_to_cleanup_callsite(llret); diff --git a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs index b62ac89661f26..6a089c0acf1d6 100644 --- a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs +++ b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs @@ -687,7 +687,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { }; let fn_ptr = bx.get_fn_addr(instance); let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty()); - let fn_ty = bx.fn_decl_backend_type(fn_abi); + let fn_ty = bx.fn_decl_backend_type(fn_abi, fn_ptr); let fn_attrs = if bx.tcx().def_kind(instance.def_id()).has_codegen_attrs() { Some(bx.tcx().codegen_fn_attrs(instance.def_id())) } else { diff --git a/compiler/rustc_codegen_ssa/src/size_of_val.rs b/compiler/rustc_codegen_ssa/src/size_of_val.rs index 577012151e49f..778440853987d 100644 --- a/compiler/rustc_codegen_ssa/src/size_of_val.rs +++ b/compiler/rustc_codegen_ssa/src/size_of_val.rs @@ -68,7 +68,7 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( // Generate the call. Cannot use `do_call` since we don't have a MIR terminator so we // can't create a `TerminationCodegenHelper`. (But we are in good company, this code is // duplicated plenty of times.) - let fn_ty = bx.fn_decl_backend_type(fn_abi); + let fn_ty = bx.fn_decl_backend_type(fn_abi, llfn); bx.call( fn_ty, diff --git a/compiler/rustc_codegen_ssa/src/traits/type_.rs b/compiler/rustc_codegen_ssa/src/traits/type_.rs index 70331b72353e4..b70bf67e52bde 100644 --- a/compiler/rustc_codegen_ssa/src/traits/type_.rs +++ b/compiler/rustc_codegen_ssa/src/traits/type_.rs @@ -96,7 +96,11 @@ pub trait LayoutTypeCodegenMethods<'tcx>: BackendTypes { /// such as when it's stack-allocated or when it's being loaded or stored. fn backend_type(&self, layout: TyAndLayout<'tcx>) -> Self::Type; fn cast_backend_type(&self, ty: &CastTarget) -> Self::Type; - fn fn_decl_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Type; + fn fn_decl_backend_type( + &self, + fn_abi: &FnAbi<'tcx, Ty<'tcx>>, + fn_ptr: Self::Value, + ) -> Self::Type; fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Type; fn reg_backend_type(&self, ty: &Reg) -> Self::Type; /// The backend type used for a rust type when it's in an SSA register. From c0d90267374acc286290bfca375338e380ed614a Mon Sep 17 00:00:00 2001 From: sayantn Date: Fri, 30 May 2025 12:19:41 +0530 Subject: [PATCH 02/10] Check for AutoUpgraded intrinsics, and emit a hard error for unknown intrinsics --- compiler/rustc_codegen_llvm/src/declare.rs | 22 ++++++++++++++++++- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 5 +++++ .../rustc_llvm/llvm-wrapper/RustWrapper.cpp | 12 ++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/declare.rs b/compiler/rustc_codegen_llvm/src/declare.rs index 0692e23b7c84a..7d29e88d73aee 100644 --- a/compiler/rustc_codegen_llvm/src/declare.rs +++ b/compiler/rustc_codegen_llvm/src/declare.rs @@ -177,7 +177,27 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { fn_abi.apply_attrs_llfn(self, llfn, instance); } - // todo: check for upgrades, and emit error if not upgradable + if let FunctionSignature::MaybeInvalidIntrinsic(..) = signature { + let mut new_llfn = None; + let can_upgrade = + unsafe { llvm::LLVMRustUpgradeIntrinsicFunction(llfn, &mut new_llfn, false) }; + + if can_upgrade { + // not all intrinsics are upgraded to some other intrinsics, most are upgraded to instruction sequences + if let Some(new_llfn) = new_llfn { + self.tcx.dcx().note(format!( + "Using deprecated intrinsic `{name}`, `{}` can be used instead", + str::from_utf8(llvm::get_value_name(new_llfn)).unwrap() + )); + } else if self.tcx.sess.opts.verbose { + self.tcx.dcx().note(format!( + "Using deprecated intrinsic `{name}`, consider using other intrinsics/instructions" + )); + } + } else { + self.tcx.dcx().fatal(format!("Invalid LLVM intrinsic: `{name}`")) + } + } if self.tcx.sess.is_sanitizer_cfi_enabled() { if let Some(instance) = instance { diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 80537b81ed61a..ce3439bd7b34e 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -1210,6 +1210,11 @@ unsafe extern "C" { ParamTypes: *const &'a Type, ParamCount: size_t, ) -> &'a Value; + pub(crate) fn LLVMRustUpgradeIntrinsicFunction<'a>( + Fn: &'a Value, + NewFn: &mut Option<&'a Value>, + CanUpgradeDebugIntrinsicsToRecords: bool, + ) -> bool; // Operations on parameters pub(crate) fn LLVMIsAArgument(Val: &Value) -> Option<&Value>; diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 90aa9188c8300..a935dc6dca8df 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -9,6 +9,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/BinaryFormat/Magic.h" #include "llvm/Bitcode/BitcodeWriter.h" +#include "llvm/IR/AutoUpgrade.h" #include "llvm/IR/DIBuilder.h" #include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DiagnosticHandler.h" @@ -1901,6 +1902,17 @@ extern "C" void LLVMRustGetMangledName(LLVMValueRef V, RustStringRef Str) { Mangler().getNameWithPrefix(OS, GV, true); } +extern "C" bool +LLVMRustUpgradeIntrinsicFunction(LLVMValueRef Fn, LLVMValueRef *NewFn, + bool canUpgradeDebugIntrinsicsToRecords) { + Function *F = unwrap(Fn); + Function *NewF = nullptr; + bool CanUpgrade = + UpgradeIntrinsicFunction(F, NewF, canUpgradeDebugIntrinsicsToRecords); + *NewFn = wrap(NewF); + return CanUpgrade; +} + extern "C" int32_t LLVMRustGetElementTypeArgIndex(LLVMValueRef CallSite) { auto *CB = unwrap(CallSite); switch (CB->getIntrinsicID()) { From 45283e4e33db8a825305ea8fe27c9e80348001f9 Mon Sep 17 00:00:00 2001 From: sayantn Date: Fri, 30 May 2025 12:38:52 +0530 Subject: [PATCH 03/10] Add auto-destructuring for structs - Remove redundant bitcasts at callsite --- compiler/rustc_codegen_llvm/src/abi.rs | 31 ++++- compiler/rustc_codegen_llvm/src/builder.rs | 147 +++++++++++++------- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 3 + compiler/rustc_codegen_llvm/src/type_.rs | 10 ++ 4 files changed, 141 insertions(+), 50 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/abi.rs b/compiler/rustc_codegen_llvm/src/abi.rs index fa0d68b677982..71d027582b963 100644 --- a/compiler/rustc_codegen_llvm/src/abi.rs +++ b/compiler/rustc_codegen_llvm/src/abi.rs @@ -7,6 +7,7 @@ use rustc_abi::{ X86Call, }; use rustc_codegen_ssa::MemFlags; +use rustc_codegen_ssa::common::TypeKind; use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue}; use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue}; use rustc_codegen_ssa::traits::*; @@ -22,7 +23,7 @@ use smallvec::SmallVec; use crate::attributes::{self, llfn_attrs_from_instance}; use crate::builder::Builder; -use crate::context::CodegenCx; +use crate::context::{CodegenCx, GenericCx, SCx}; use crate::llvm::{self, Attribute, AttributePlace}; use crate::type_::Type; use crate::type_of::LayoutLlvmExt; @@ -353,6 +354,32 @@ pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> { ); } +impl<'ll, CX: Borrow>> GenericCx<'ll, CX> { + pub(crate) fn equate_ty(&self, rust_ty: &'ll Type, llvm_ty: &'ll Type) -> bool { + if rust_ty == llvm_ty { + return true; + } + + match self.type_kind(llvm_ty) { + TypeKind::Struct if self.type_kind(rust_ty) == TypeKind::Struct => { + let rust_element_tys = self.struct_element_types(rust_ty); + let llvm_element_tys = self.struct_element_types(llvm_ty); + + if rust_element_tys.len() != llvm_element_tys.len() { + return false; + } + + iter::zip(rust_element_tys, llvm_element_tys).all( + |(rust_element_ty, llvm_element_ty)| { + self.equate_ty(rust_element_ty, llvm_element_ty) + }, + ) + } + _ => false, + } + } +} + impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> { fn llvm_return_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type { match &self.ret.mode { @@ -442,7 +469,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> { // todo: add bypasses for types not accessible from Rust here iter::once((rust_return_ty, llvm_return_ty)) .chain(iter::zip(rust_argument_tys, llvm_argument_tys)) - .all(|(rust_ty, llvm_ty)| rust_ty == llvm_ty) + .all(|(rust_ty, llvm_ty)| cx.equate_ty(rust_ty, llvm_ty)) } fn llvm_type( diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 5e782b9c038ca..4047a7409d6b0 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -66,7 +66,6 @@ impl<'a, 'll> SBuilder<'a, 'll> { ) -> &'ll Value { debug!("call {:?} with args ({:?})", llfn, args); - let args = self.check_call("call", llty, llfn, args); let funclet_bundle = funclet.map(|funclet| funclet.bundle()); let mut bundles: SmallVec<[_; 2]> = SmallVec::new(); if let Some(funclet_bundle) = funclet_bundle { @@ -348,7 +347,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { ) -> &'ll Value { debug!("invoke {:?} with args ({:?})", llfn, args); - let args = self.check_call("invoke", llty, llfn, args); + let args = self.cast_arguments("invoke", llty, llfn, args, fn_abi.is_some()); let funclet_bundle = funclet.map(|funclet| funclet.bundle()); let mut bundles: SmallVec<[_; 2]> = SmallVec::new(); if let Some(funclet_bundle) = funclet_bundle { @@ -380,8 +379,10 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { }; if let Some(fn_abi) = fn_abi { fn_abi.apply_attrs_callsite(self, invoke, llfn); + self.cast_return(fn_abi, llfn, invoke) + } else { + invoke } - invoke } fn unreachable(&mut self) { @@ -1343,7 +1344,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { ) -> &'ll Value { debug!("call {:?} with args ({:?})", llfn, args); - let args = self.check_call("call", llty, llfn, args); + let args = self.cast_arguments("call", llty, llfn, args, fn_abi.is_some()); let funclet_bundle = funclet.map(|funclet| funclet.bundle()); let mut bundles: SmallVec<[_; 2]> = SmallVec::new(); if let Some(funclet_bundle) = funclet_bundle { @@ -1373,8 +1374,10 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { }; if let Some(fn_abi) = fn_abi { fn_abi.apply_attrs_callsite(self, call, llfn); + self.cast_return(fn_abi, llfn, call) + } else { + call } - call } fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value { @@ -1542,47 +1545,6 @@ impl<'a, 'll, CX: Borrow>> GenericBuilder<'a, 'll, CX> { ret.expect("LLVM does not have support for catchret") } - fn check_call<'b>( - &mut self, - typ: &str, - fn_ty: &'ll Type, - llfn: &'ll Value, - args: &'b [&'ll Value], - ) -> Cow<'b, [&'ll Value]> { - assert!( - self.cx.type_kind(fn_ty) == TypeKind::Function, - "builder::{typ} not passed a function, but {fn_ty:?}" - ); - - let param_tys = self.cx.func_params_types(fn_ty); - - let all_args_match = iter::zip(¶m_tys, args.iter().map(|&v| self.cx.val_ty(v))) - .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty); - - if all_args_match { - return Cow::Borrowed(args); - } - - let casted_args: Vec<_> = iter::zip(param_tys, args) - .enumerate() - .map(|(i, (expected_ty, &actual_val))| { - let actual_ty = self.cx.val_ty(actual_val); - if expected_ty != actual_ty { - debug!( - "type mismatch in function call of {:?}. \ - Expected {:?} for param {}, got {:?}; injecting bitcast", - llfn, expected_ty, i, actual_ty - ); - self.bitcast(actual_val, expected_ty) - } else { - actual_val - } - }) - .collect(); - - Cow::Owned(casted_args) - } - pub(crate) fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value { unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) } } @@ -1634,6 +1596,93 @@ impl<'a, 'll, CX: Borrow>> GenericBuilder<'a, 'll, CX> { } } impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { + fn autocast( + &mut self, + llfn: &'ll Value, + val: &'ll Value, + src_ty: &'ll Type, + dest_ty: &'ll Type, + is_argument: bool, + ) -> &'ll Value { + let (rust_ty, llvm_ty) = if is_argument { (src_ty, dest_ty) } else { (dest_ty, src_ty) }; + + if rust_ty == llvm_ty { + return val; + } + + match self.type_kind(llvm_ty) { + TypeKind::Struct => { + let mut ret = self.const_poison(dest_ty); + for (idx, (src_element_ty, dest_element_ty)) in + iter::zip(self.struct_element_types(src_ty), self.struct_element_types(dest_ty)) + .enumerate() + { + let elt = self.extract_value(val, idx as u64); + let casted_elt = + self.autocast(llfn, elt, src_element_ty, dest_element_ty, is_argument); + ret = self.insert_value(ret, casted_elt, idx as u64); + } + ret + } + _ => unreachable!(), + } + } + + fn cast_arguments<'b>( + &mut self, + typ: &str, + fn_ty: &'ll Type, + llfn: &'ll Value, + args: &'b [&'ll Value], + has_fnabi: bool, + ) -> Cow<'b, [&'ll Value]> { + assert_eq!( + self.type_kind(fn_ty), + TypeKind::Function, + "{typ} not passed a function, but {fn_ty:?}" + ); + + let param_tys = self.func_params_types(fn_ty); + + let mut casted_args = Cow::Borrowed(args); + + for (idx, (dest_ty, &arg)) in iter::zip(param_tys, args).enumerate() { + let src_ty = self.val_ty(arg); + assert!( + self.equate_ty(src_ty, dest_ty), + "Cannot match `{dest_ty:?}` (expected) with `{src_ty:?}` (found) in `{llfn:?}`" + ); + + let casted_arg = self.autocast(llfn, arg, src_ty, dest_ty, true); + if arg != casted_arg { + assert!( + has_fnabi, + "Should inject autocasts in function call of {llfn:?}, but not able to get Rust signature" + ); + + casted_args.to_mut()[idx] = casted_arg; + } + } + + casted_args + } + + fn cast_return( + &mut self, + fn_abi: &FnAbi<'tcx, Ty<'tcx>>, + llfn: &'ll Value, + ret: &'ll Value, + ) -> &'ll Value { + let src_ty = self.val_ty(ret); + let dest_ty = fn_abi.llvm_return_type(self); + assert!( + self.equate_ty(dest_ty, src_ty), + "Cannot match `{src_ty:?}` (expected) with `{dest_ty:?}` (found) in `{llfn:?}`" + ); + + self.autocast(llfn, ret, src_ty, dest_ty, false) + } + pub(crate) fn landing_pad( &mut self, ty: &'ll Type, @@ -1663,7 +1712,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { ) -> &'ll Value { debug!("invoke {:?} with args ({:?})", llfn, args); - let args = self.check_call("callbr", llty, llfn, args); + let args = self.cast_arguments("callbr", llty, llfn, args, fn_abi.is_some()); let funclet_bundle = funclet.map(|funclet| funclet.bundle()); let mut bundles: SmallVec<[_; 2]> = SmallVec::new(); if let Some(funclet_bundle) = funclet_bundle { @@ -1696,8 +1745,10 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { }; if let Some(fn_abi) = fn_abi { fn_abi.apply_attrs_callsite(self, callbr, llfn); + self.cast_return(fn_abi, llfn, callbr) + } else { + callbr } - callbr } // Emits CFI pointer type membership tests. diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index ce3439bd7b34e..f95d7d5e194c7 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -1735,6 +1735,9 @@ unsafe extern "C" { Packed: Bool, ); + pub(crate) fn LLVMCountStructElementTypes(StructTy: &Type) -> c_uint; + pub(crate) fn LLVMGetStructElementTypes<'a>(StructTy: &'a Type, Dest: *mut &'a Type); + pub(crate) safe fn LLVMMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value; pub(crate) fn LLVMSetUnnamedAddress(Global: &Value, UnnamedAddr: UnnamedAddr); diff --git a/compiler/rustc_codegen_llvm/src/type_.rs b/compiler/rustc_codegen_llvm/src/type_.rs index 0727722cd0559..534aa7f3f2081 100644 --- a/compiler/rustc_codegen_llvm/src/type_.rs +++ b/compiler/rustc_codegen_llvm/src/type_.rs @@ -81,6 +81,16 @@ impl<'ll, CX: Borrow>> GenericCx<'ll, CX> { pub(crate) fn func_is_variadic(&self, ty: &'ll Type) -> bool { unsafe { llvm::LLVMIsFunctionVarArg(ty) == True } } + + pub(crate) fn struct_element_types(&self, ty: &'ll Type) -> Vec<&'ll Type> { + unsafe { + let n_args = llvm::LLVMCountStructElementTypes(ty) as usize; + let mut args = Vec::with_capacity(n_args); + llvm::LLVMGetStructElementTypes(ty, args.as_mut_ptr()); + args.set_len(n_args); + args + } + } } impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { pub(crate) fn type_bool(&self) -> &'ll Type { From c7f8d77ae6fa64758a7197269142469541443b6c Mon Sep 17 00:00:00 2001 From: sayantn Date: Fri, 30 May 2025 13:10:20 +0530 Subject: [PATCH 04/10] Add bypass for `bf16` and `bf16xN` --- compiler/rustc_codegen_llvm/src/abi.rs | 11 +++++++++++ compiler/rustc_codegen_llvm/src/builder.rs | 2 +- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 3 +++ compiler/rustc_codegen_llvm/src/type_.rs | 6 +++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/abi.rs b/compiler/rustc_codegen_llvm/src/abi.rs index 71d027582b963..e96ffab926cdc 100644 --- a/compiler/rustc_codegen_llvm/src/abi.rs +++ b/compiler/rustc_codegen_llvm/src/abi.rs @@ -361,6 +361,17 @@ impl<'ll, CX: Borrow>> GenericCx<'ll, CX> { } match self.type_kind(llvm_ty) { + TypeKind::BFloat => rust_ty == self.type_i16(), + TypeKind::Vector => { + let llvm_element_count = self.vector_length(llvm_ty) as u64; + let llvm_element_ty = self.element_type(llvm_ty); + + if llvm_element_ty == self.type_bf16() { + rust_ty == self.type_vector(self.type_i16(), llvm_element_count) + } else { + false + } + } TypeKind::Struct if self.type_kind(rust_ty) == TypeKind::Struct => { let rust_element_tys = self.struct_element_types(rust_ty); let llvm_element_tys = self.struct_element_types(llvm_ty); diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 4047a7409d6b0..32a1a8eb6091d 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -1624,7 +1624,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { } ret } - _ => unreachable!(), + _ => self.bitcast(val, dest_ty), // for `bf16(xN)` <-> `u16(xN)` } } diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index f95d7d5e194c7..d05ff5b488394 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -1051,6 +1051,9 @@ unsafe extern "C" { pub(crate) fn LLVMDoubleTypeInContext(C: &Context) -> &Type; pub(crate) fn LLVMFP128TypeInContext(C: &Context) -> &Type; + // Operations on non-IEEE real types + pub(crate) fn LLVMBFloatTypeInContext(C: &Context) -> &Type; + // Operations on function types pub(crate) fn LLVMFunctionType<'a>( ReturnType: &'a Type, diff --git a/compiler/rustc_codegen_llvm/src/type_.rs b/compiler/rustc_codegen_llvm/src/type_.rs index 534aa7f3f2081..6c7a7c95fcd65 100644 --- a/compiler/rustc_codegen_llvm/src/type_.rs +++ b/compiler/rustc_codegen_llvm/src/type_.rs @@ -170,6 +170,10 @@ impl<'ll, CX: Borrow>> GenericCx<'ll, CX> { ) } } + + pub(crate) fn type_bf16(&self) -> &'ll Type { + unsafe { llvm::LLVMBFloatTypeInContext(self.llcx()) } + } } impl<'ll, CX: Borrow>> BaseTypeCodegenMethods for GenericCx<'ll, CX> { @@ -243,7 +247,7 @@ impl<'ll, CX: Borrow>> BaseTypeCodegenMethods for GenericCx<'ll, CX> { fn float_width(&self, ty: &'ll Type) -> usize { match self.type_kind(ty) { - TypeKind::Half => 16, + TypeKind::Half | TypeKind::BFloat => 16, TypeKind::Float => 32, TypeKind::Double => 64, TypeKind::X86_FP80 => 80, From cb5e319f1ab433961bcdd8559a6a45962a013e46 Mon Sep 17 00:00:00 2001 From: sayantn Date: Fri, 30 May 2025 13:11:54 +0530 Subject: [PATCH 05/10] Add bypass for `i1xN` --- compiler/rustc_codegen_llvm/src/abi.rs | 3 ++ compiler/rustc_codegen_llvm/src/builder.rs | 49 +++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_llvm/src/abi.rs b/compiler/rustc_codegen_llvm/src/abi.rs index e96ffab926cdc..6ead816bbaf3c 100644 --- a/compiler/rustc_codegen_llvm/src/abi.rs +++ b/compiler/rustc_codegen_llvm/src/abi.rs @@ -368,6 +368,9 @@ impl<'ll, CX: Borrow>> GenericCx<'ll, CX> { if llvm_element_ty == self.type_bf16() { rust_ty == self.type_vector(self.type_i16(), llvm_element_count) + } else if llvm_element_ty == self.type_i1() { + let int_width = cmp::max(llvm_element_count.next_power_of_two(), 8); + rust_ty == self.type_ix(int_width) } else { false } diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 32a1a8eb6091d..1a43da48ff607 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -1,6 +1,6 @@ use std::borrow::{Borrow, Cow}; use std::ops::Deref; -use std::{iter, ptr}; +use std::{cmp, iter, ptr}; pub(crate) mod autodiff; @@ -1596,6 +1596,46 @@ impl<'a, 'll, CX: Borrow>> GenericBuilder<'a, 'll, CX> { } } impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { + fn trunc_int_to_i1_vector(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value { + let vector_length = self.vector_length(dest_ty) as u64; + let int_width = cmp::max(vector_length.next_power_of_two(), 8); + + let bitcasted = self.bitcast(val, self.type_vector(self.type_i1(), int_width)); + if vector_length == int_width { + bitcasted + } else { + let shuffle_mask = + (0..vector_length).map(|i| self.const_i32(i as i32)).collect::>(); + self.shuffle_vector(bitcasted, bitcasted, self.const_vector(&shuffle_mask)) + } + } + + fn zext_i1_vector_to_int( + &mut self, + mut val: &'ll Value, + src_ty: &'ll Type, + dest_ty: &'ll Type, + ) -> &'ll Value { + let vector_length = self.vector_length(src_ty) as u64; + let int_width = cmp::max(vector_length.next_power_of_two(), 8); + + if vector_length != int_width { + let shuffle_indices = match vector_length { + 0 => unreachable!("zero length vectors are not allowed"), + 1 => vec![0, 1, 1, 1, 1, 1, 1, 1], + 2 => vec![0, 1, 2, 3, 2, 3, 2, 3], + 3 => vec![0, 1, 2, 3, 4, 5, 3, 4], + 4.. => (0..int_width as i32).collect(), + }; + let shuffle_mask = + shuffle_indices.into_iter().map(|i| self.const_i32(i)).collect::>(); + val = + self.shuffle_vector(val, self.const_null(src_ty), self.const_vector(&shuffle_mask)); + } + + self.bitcast(val, dest_ty) + } + fn autocast( &mut self, llfn: &'ll Value, @@ -1611,6 +1651,13 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { } match self.type_kind(llvm_ty) { + TypeKind::Vector if self.element_type(llvm_ty) == self.type_i1() => { + if is_argument { + self.trunc_int_to_i1_vector(val, dest_ty) + } else { + self.zext_i1_vector_to_int(val, src_ty, dest_ty) + } + } TypeKind::Struct => { let mut ret = self.const_poison(dest_ty); for (idx, (src_element_ty, dest_element_ty)) in From 2034fd67d7f1cb8bd8b39f092f7662e4a24793b3 Mon Sep 17 00:00:00 2001 From: sayantn Date: Fri, 30 May 2025 21:42:24 +0530 Subject: [PATCH 06/10] Add bypass for `x86amx` --- compiler/rustc_codegen_llvm/src/abi.rs | 19 +++++++++++++++++++ compiler/rustc_codegen_llvm/src/builder.rs | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/compiler/rustc_codegen_llvm/src/abi.rs b/compiler/rustc_codegen_llvm/src/abi.rs index 6ead816bbaf3c..beb823c77c3e3 100644 --- a/compiler/rustc_codegen_llvm/src/abi.rs +++ b/compiler/rustc_codegen_llvm/src/abi.rs @@ -361,6 +361,25 @@ impl<'ll, CX: Borrow>> GenericCx<'ll, CX> { } match self.type_kind(llvm_ty) { + TypeKind::X86_AMX if self.type_kind(rust_ty) == TypeKind::Vector => { + let element_count = self.vector_length(rust_ty); + let element_ty = self.element_type(rust_ty); + + let element_size_bits = match self.type_kind(element_ty) { + TypeKind::Half => 16, + TypeKind::Float => 32, + TypeKind::Double => 64, + TypeKind::FP128 => 128, + TypeKind::Integer => self.int_width(element_ty), + TypeKind::Pointer => self.int_width(self.isize_ty()), + _ => bug!( + "Vector element type `{element_ty:?}` not one of integer, float or pointer" + ), + }; + let vector_size_bits = element_size_bits * element_count as u64; + + vector_size_bits == 8192 + } TypeKind::BFloat => rust_ty == self.type_i16(), TypeKind::Vector => { let llvm_element_count = self.vector_length(llvm_ty) as u64; diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 1a43da48ff607..4725e97b6a51e 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -1651,6 +1651,15 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { } match self.type_kind(llvm_ty) { + TypeKind::X86_AMX => { + let base_name = if is_argument { + "llvm.x86.cast.vector.to.tile" + } else { + "llvm.x86.cast.tile.to.vector" + }; + + self.call_intrinsic(base_name, &[rust_ty], &[val]) + } TypeKind::Vector if self.element_type(llvm_ty) == self.type_i1() => { if is_argument { self.trunc_int_to_i1_vector(val, dest_ty) From fa2e0d87d94f0344c9a94a80943e9a19c4e55073 Mon Sep 17 00:00:00 2001 From: sayantn Date: Fri, 30 May 2025 12:20:16 +0530 Subject: [PATCH 07/10] Disable ABI checks for the `unadjusted` ABI --- .../rustc_monomorphize/src/mono_checks/abi_check.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_monomorphize/src/mono_checks/abi_check.rs b/compiler/rustc_monomorphize/src/mono_checks/abi_check.rs index b8c001d357e6c..23f6ab4e46fe1 100644 --- a/compiler/rustc_monomorphize/src/mono_checks/abi_check.rs +++ b/compiler/rustc_monomorphize/src/mono_checks/abi_check.rs @@ -1,6 +1,6 @@ //! This module ensures that if a function's ABI requires a particular target feature, //! that target feature is enabled both on the callee and all callers. -use rustc_abi::{BackendRepr, CanonAbi, RegKind, X86Call}; +use rustc_abi::{BackendRepr, CanonAbi, ExternAbi, RegKind, X86Call}; use rustc_hir::{CRATE_HIR_ID, HirId}; use rustc_middle::mir::{self, Location, traversal}; use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TyCtxt}; @@ -82,6 +82,12 @@ fn do_check_simd_vector_abi<'tcx>( /// or return values for which the corresponding target feature is not enabled. fn check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) { let typing_env = ty::TypingEnv::fully_monomorphized(); + let ty = instance.ty(tcx, typing_env); + if ty.is_fn() && ty.fn_sig(tcx).abi() == ExternAbi::Unadjusted { + // We disable all checks for the unadjusted abi to allow linking to arbitrary LLVM + // intrinsics + return; + } let Ok(abi) = tcx.fn_abi_of_instance(typing_env.as_query_input((instance, ty::List::empty()))) else { // An error will be reported during codegen if we cannot determine the ABI of this @@ -113,9 +119,12 @@ fn check_call_site_abi<'tcx>( caller: InstanceKind<'tcx>, loc: impl Fn() -> (Span, HirId) + Copy, ) { - if callee.fn_sig(tcx).abi().is_rustic_abi() { + let extern_abi = callee.fn_sig(tcx).abi(); + if extern_abi.is_rustic_abi() || extern_abi == ExternAbi::Unadjusted { // We directly handle the soundness of Rust ABIs -- so let's skip the majority of // call sites to avoid a perf regression. + // We disable all checks for the unadjusted abi to allow linking to arbitrary LLVM + // intrinsics return; } let typing_env = ty::TypingEnv::fully_monomorphized(); From 97da38286029090e84e16ea8bdc9fb21e7072562 Mon Sep 17 00:00:00 2001 From: sayantn Date: Fri, 30 May 2025 23:22:11 +0530 Subject: [PATCH 08/10] Add autocast tests - Correct usage of invalid intrinsics in tests --- tests/codegen/inject-autocast.rs | 119 ++++++++++++++++++ tests/run-make/simd-ffi/simd.rs | 2 +- tests/ui/codegen/deprecated-llvm-intrinsic.rs | 28 +++++ .../codegen/deprecated-llvm-intrinsic.stderr | 2 + .../incorrect-llvm-intrinsic-signature.rs | 15 +++ .../incorrect-llvm-intrinsic-signature.stderr | 4 + tests/ui/codegen/invalid-llvm-intrinsic.rs | 15 +++ .../ui/codegen/invalid-llvm-intrinsic.stderr | 4 + 8 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 tests/codegen/inject-autocast.rs create mode 100644 tests/ui/codegen/deprecated-llvm-intrinsic.rs create mode 100644 tests/ui/codegen/deprecated-llvm-intrinsic.stderr create mode 100644 tests/ui/codegen/incorrect-llvm-intrinsic-signature.rs create mode 100644 tests/ui/codegen/incorrect-llvm-intrinsic-signature.stderr create mode 100644 tests/ui/codegen/invalid-llvm-intrinsic.rs create mode 100644 tests/ui/codegen/invalid-llvm-intrinsic.stderr diff --git a/tests/codegen/inject-autocast.rs b/tests/codegen/inject-autocast.rs new file mode 100644 index 0000000000000..2d1cbb0d98709 --- /dev/null +++ b/tests/codegen/inject-autocast.rs @@ -0,0 +1,119 @@ +//@ compile-flags: -C opt-level=0 +//@ only-x86_64 + +#![feature(link_llvm_intrinsics, abi_unadjusted, repr_simd, simd_ffi, portable_simd, f16)] +#![crate_type = "lib"] + +use std::simd::{f32x4, i16x8, i64x2}; + +#[repr(simd)] +pub struct Tile([i8; 1024]); + +#[repr(C, packed)] +pub struct Bar(u32, i64x2, i64x2, i64x2, i64x2, i64x2, i64x2); +// CHECK: %Bar = type <{ i32, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64> }> + +#[repr(simd)] +pub struct f16x8([f16; 8]); + +// CHECK-LABEL: @amx_autocast +#[no_mangle] +pub unsafe fn amx_autocast(m: u16, n: u16, k: u16, a: Tile, b: Tile, c: Tile) -> Tile { + extern "unadjusted" { + #[link_name = "llvm.x86.tdpbuud.internal"] + fn foo(m: u16, n: u16, k: u16, a: Tile, b: Tile, c: Tile) -> Tile; + } + + // CHECK: %3 = call x86_amx @llvm.x86.cast.vector.to.tile.v1024i8(<1024 x i8> %0) + // CHECK-NEXT: %4 = call x86_amx @llvm.x86.cast.vector.to.tile.v1024i8(<1024 x i8> %1) + // CHECK-NEXT: %5 = call x86_amx @llvm.x86.cast.vector.to.tile.v1024i8(<1024 x i8> %2) + // CHECK-NEXT: %6 = call x86_amx @llvm.x86.tdpbuud.internal(i16 %m, i16 %n, i16 %k, x86_amx %3, x86_amx %4, x86_amx %5) + // CHECK-NEXT: %7 = call <1024 x i8> @llvm.x86.cast.tile.to.vector.v1024i8(x86_amx %6) + foo(m, n, k, a, b, c) +} + +// CHECK-LABEL: @struct_with_i1_vector_autocast +#[no_mangle] +pub unsafe fn struct_with_i1_vector_autocast(a: i64x2, b: i64x2) -> (u8, u8) { + extern "unadjusted" { + #[link_name = "llvm.x86.avx512.vp2intersect.q.128"] + fn foo(a: i64x2, b: i64x2) -> (u8, u8); + } + + // CHECK: %2 = call { <2 x i1>, <2 x i1> } @llvm.x86.avx512.vp2intersect.q.128(<2 x i64> %0, <2 x i64> %1) + // CHECK-NEXT: %3 = extractvalue { <2 x i1>, <2 x i1> } %2, 0 + // CHECK-NEXT: %4 = shufflevector <2 x i1> %3, <2 x i1> zeroinitializer, <8 x i32> + // CHECK-NEXT: %5 = bitcast <8 x i1> %4 to i8 + // CHECK-NEXT: %6 = insertvalue { i8, i8 } poison, i8 %5, 0 + // CHECK-NEXT: %7 = extractvalue { <2 x i1>, <2 x i1> } %2, 1 + // CHECK-NEXT: %8 = shufflevector <2 x i1> %7, <2 x i1> zeroinitializer, <8 x i32> + // CHECK-NEXT: %9 = bitcast <8 x i1> %8 to i8 + // CHECK-NEXT: %10 = insertvalue { i8, i8 } %6, i8 %9, 1 + foo(a, b) +} + +// CHECK-LABEL: @bf16_vector_autocast +#[no_mangle] +pub unsafe fn bf16_vector_autocast(a: f32x4) -> i16x8 { + extern "unadjusted" { + #[link_name = "llvm.x86.vcvtneps2bf16128"] + fn foo(a: f32x4) -> i16x8; + } + + // CHECK: %1 = call <8 x bfloat> @llvm.x86.vcvtneps2bf16128(<4 x float> %0) + // CHECK-NEXT: %2 = bitcast <8 x bfloat> %1 to <8 x i16> + foo(a) +} + +// CHECK-LABEL: @struct_autocast +#[no_mangle] +pub unsafe fn struct_autocast(key_metadata: u32, key: i64x2) -> Bar { + extern "unadjusted" { + #[link_name = "llvm.x86.encodekey128"] + fn foo(key_metadata: u32, key: i64x2) -> Bar; + } + + // CHECK: %1 = call { i32, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64> } @llvm.x86.encodekey128(i32 %key_metadata, <2 x i64> %0) + // CHECK-NEXT: %2 = extractvalue { i32, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64> } %1, 0 + // CHECK-NEXT: %3 = insertvalue %Bar poison, i32 %2, 0 + // CHECK-NEXT: %4 = extractvalue { i32, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64> } %1, 1 + // CHECK-NEXT: %5 = insertvalue %Bar %3, <2 x i64> %4, 1 + // CHECK-NEXT: %6 = extractvalue { i32, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64> } %1, 2 + // CHECK-NEXT: %7 = insertvalue %Bar %5, <2 x i64> %6, 2 + // CHECK-NEXT: %8 = extractvalue { i32, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64> } %1, 3 + // CHECK-NEXT: %9 = insertvalue %Bar %7, <2 x i64> %8, 3 + // CHECK-NEXT: %10 = extractvalue { i32, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64> } %1, 4 + // CHECK-NEXT: %11 = insertvalue %Bar %9, <2 x i64> %10, 4 + // CHECK-NEXT: %12 = extractvalue { i32, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64> } %1, 5 + // CHECK-NEXT: %13 = insertvalue %Bar %11, <2 x i64> %12, 5 + // CHECK-NEXT: %14 = extractvalue { i32, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64> } %1, 6 + // CHECK-NEXT: %15 = insertvalue %Bar %13, <2 x i64> %14, 6 + foo(key_metadata, key) +} + +// CHECK-LABEL: @i1_vector_autocast +#[no_mangle] +pub unsafe fn i1_vector_autocast(a: f16x8) -> u8 { + extern "unadjusted" { + #[link_name = "llvm.x86.avx512fp16.fpclass.ph.128"] + fn foo(a: f16x8, b: i32) -> u8; + } + + // CHECK: %1 = call <8 x i1> @llvm.x86.avx512fp16.fpclass.ph.128(<8 x half> %0, i32 1) + // CHECK-NEXT: %_0 = bitcast <8 x i1> %1 to i8 + foo(a, 1) +} + +// CHECK: declare x86_amx @llvm.x86.tdpbuud.internal(i16, i16, i16, x86_amx, x86_amx, x86_amx) + +// CHECK: declare x86_amx @llvm.x86.cast.vector.to.tile.v1024i8(<1024 x i8>) + +// CHECK: declare <1024 x i8> @llvm.x86.cast.tile.to.vector.v1024i8(x86_amx) + +// CHECK: declare { <2 x i1>, <2 x i1> } @llvm.x86.avx512.vp2intersect.q.128(<2 x i64>, <2 x i64>) + +// CHECK: declare <8 x bfloat> @llvm.x86.vcvtneps2bf16128(<4 x float>) + +// CHECK: declare { i32, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64>, <2 x i64> } @llvm.x86.encodekey128(i32, <2 x i64>) + +// CHECK: declare <8 x i1> @llvm.x86.avx512fp16.fpclass.ph.128(<8 x half>, i32 immarg) diff --git a/tests/run-make/simd-ffi/simd.rs b/tests/run-make/simd-ffi/simd.rs index 9ea8eb8cf8831..2a2032f218e75 100644 --- a/tests/run-make/simd-ffi/simd.rs +++ b/tests/run-make/simd-ffi/simd.rs @@ -35,7 +35,7 @@ extern "C" { fn integer(a: i32x4, b: i32x4) -> i32x4; // vmaxq_s32 #[cfg(target_arch = "aarch64")] - #[link_name = "llvm.aarch64.neon.maxs.v4i32"] + #[link_name = "llvm.aarch64.neon.smax.v4i32"] fn integer(a: i32x4, b: i32x4) -> i32x4; // Use a generic LLVM intrinsic to do type checking on other platforms diff --git a/tests/ui/codegen/deprecated-llvm-intrinsic.rs b/tests/ui/codegen/deprecated-llvm-intrinsic.rs new file mode 100644 index 0000000000000..8ae363054cc74 --- /dev/null +++ b/tests/ui/codegen/deprecated-llvm-intrinsic.rs @@ -0,0 +1,28 @@ +//@ add-core-stubs +//@ build-pass +//@ ignore-pass +//@ compile-flags: --target aarch64-unknown-linux-gnu +//@ needs-llvm-components: aarch64 +#![feature(no_core, lang_items, link_llvm_intrinsics, abi_unadjusted, repr_simd, simd_ffi)] +#![no_std] +#![no_core] +#![allow(internal_features, non_camel_case_types, improper_ctypes)] +#![crate_type = "lib"] + +extern crate minicore; +use minicore::*; + +#[repr(simd)] +pub struct i8x8([i8; 8]); + +extern "unadjusted" { + #[link_name = "llvm.aarch64.neon.rbit.v8i8"] + fn foo(a: i8x8) -> i8x8; +} + +#[target_feature(enable = "neon")] +pub unsafe fn bar(a: i8x8) -> i8x8 { + foo(a) +} + +//~? NOTE: Using deprecated intrinsic `llvm.aarch64.neon.rbit.v8i8`, `llvm.bitreverse.v8i8` can be used instead diff --git a/tests/ui/codegen/deprecated-llvm-intrinsic.stderr b/tests/ui/codegen/deprecated-llvm-intrinsic.stderr new file mode 100644 index 0000000000000..214ac269e7d0e --- /dev/null +++ b/tests/ui/codegen/deprecated-llvm-intrinsic.stderr @@ -0,0 +1,2 @@ +note: Using deprecated intrinsic `llvm.aarch64.neon.rbit.v8i8`, `llvm.bitreverse.v8i8` can be used instead + diff --git a/tests/ui/codegen/incorrect-llvm-intrinsic-signature.rs b/tests/ui/codegen/incorrect-llvm-intrinsic-signature.rs new file mode 100644 index 0000000000000..84c4c0d747247 --- /dev/null +++ b/tests/ui/codegen/incorrect-llvm-intrinsic-signature.rs @@ -0,0 +1,15 @@ +//@ build-fail + +#![feature(link_llvm_intrinsics, abi_unadjusted)] +#![allow(internal_features, non_camel_case_types, improper_ctypes)] + +extern "unadjusted" { + #[link_name = "llvm.assume"] + fn foo(); +} + +pub fn main() { + unsafe { foo() } +} + +//~? ERROR: Intrinsic signature mismatch for `llvm.assume`: expected signature `void (i1)` diff --git a/tests/ui/codegen/incorrect-llvm-intrinsic-signature.stderr b/tests/ui/codegen/incorrect-llvm-intrinsic-signature.stderr new file mode 100644 index 0000000000000..f67ba8a65a40c --- /dev/null +++ b/tests/ui/codegen/incorrect-llvm-intrinsic-signature.stderr @@ -0,0 +1,4 @@ +error: Intrinsic signature mismatch for `llvm.assume`: expected signature `void (i1)` + +error: aborting due to 1 previous error + diff --git a/tests/ui/codegen/invalid-llvm-intrinsic.rs b/tests/ui/codegen/invalid-llvm-intrinsic.rs new file mode 100644 index 0000000000000..2f1ff826ed39b --- /dev/null +++ b/tests/ui/codegen/invalid-llvm-intrinsic.rs @@ -0,0 +1,15 @@ +//@ build-fail + +#![feature(link_llvm_intrinsics, abi_unadjusted)] +#![allow(internal_features, non_camel_case_types, improper_ctypes)] + +extern "unadjusted" { + #[link_name = "llvm.abcde"] + fn foo(); +} + +pub fn main() { + unsafe { foo() } +} + +//~? ERROR: Invalid LLVM intrinsic: `llvm.abcde` diff --git a/tests/ui/codegen/invalid-llvm-intrinsic.stderr b/tests/ui/codegen/invalid-llvm-intrinsic.stderr new file mode 100644 index 0000000000000..467d6a62553cc --- /dev/null +++ b/tests/ui/codegen/invalid-llvm-intrinsic.stderr @@ -0,0 +1,4 @@ +error: Invalid LLVM intrinsic: `llvm.abcde` + +error: aborting due to 1 previous error + From aefde20972460b7fae2817e3942e608ec6c45d83 Mon Sep 17 00:00:00 2001 From: sayantn Date: Tue, 10 Jun 2025 11:40:10 +0530 Subject: [PATCH 09/10] Refactor emitting errors to `declare.rs`, and use fluently-generated error messages --- compiler/rustc_codegen_llvm/messages.ftl | 12 +++ compiler/rustc_codegen_llvm/src/abi.rs | 96 ++++++++----------- compiler/rustc_codegen_llvm/src/declare.rs | 73 +++++++------- compiler/rustc_codegen_llvm/src/errors.rs | 35 +++++++ compiler/rustc_codegen_llvm/src/intrinsic.rs | 2 +- compiler/rustc_codegen_llvm/src/type_.rs | 2 +- tests/ui/codegen/deprecated-llvm-intrinsic.rs | 3 +- .../codegen/deprecated-llvm-intrinsic.stderr | 4 + .../incorrect-llvm-intrinsic-signature.rs | 4 +- .../incorrect-llvm-intrinsic-signature.stderr | 6 +- tests/ui/codegen/invalid-llvm-intrinsic.rs | 4 +- .../ui/codegen/invalid-llvm-intrinsic.stderr | 6 +- 12 files changed, 147 insertions(+), 100 deletions(-) diff --git a/compiler/rustc_codegen_llvm/messages.ftl b/compiler/rustc_codegen_llvm/messages.ftl index 3faeb9b3b221b..5e12e116ac74c 100644 --- a/compiler/rustc_codegen_llvm/messages.ftl +++ b/compiler/rustc_codegen_llvm/messages.ftl @@ -3,6 +3,12 @@ codegen_llvm_autodiff_without_lto = using the autodiff feature requires using fa codegen_llvm_copy_bitcode = failed to copy bitcode to object file: {$err} +codegen_llvm_deprecated_intrinsic = + Using deprecated intrinsic `{$name}`, consider using other intrinsics/instructions + +codegen_llvm_deprecated_intrinsic_with_replacement = + Using deprecated intrinsic `{$name}`, `{$replacement}` can be used instead + codegen_llvm_dynamic_linking_with_lto = cannot prefer dynamic linking when performing LTO .note = only 'staticlib', 'bin', and 'cdylib' outputs are supported with LTO @@ -14,6 +20,12 @@ codegen_llvm_from_llvm_diag = {$message} codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message} +codegen_llvm_intrinsic_signature_mismatch = + Intrinsic signature mismatch for `{$name}`: expected signature `{$llvm_fn_ty}`, found `{$rust_fn_ty}` + +codegen_llvm_invalid_intrinsic = + Invalid LLVM Intrinsic `{$name}` + codegen_llvm_load_bitcode = failed to load bitcode of module "{$name}" codegen_llvm_load_bitcode_with_llvm_err = failed to load bitcode of module "{$name}": {$llvm_err} diff --git a/compiler/rustc_codegen_llvm/src/abi.rs b/compiler/rustc_codegen_llvm/src/abi.rs index beb823c77c3e3..f9b24db4b1c58 100644 --- a/compiler/rustc_codegen_llvm/src/abi.rs +++ b/compiler/rustc_codegen_llvm/src/abi.rs @@ -302,20 +302,32 @@ impl<'ll, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { } pub(crate) enum FunctionSignature<'ll> { - /// The signature is obtained directly from LLVM, and **may not match the Rust signature** - Intrinsic(&'ll Type), + /// This is an LLVM intrinsic, the signature is obtained directly from LLVM, and **may not match the Rust signature** + LLVMSignature(llvm::Intrinsic, &'ll Type), + /// This is an LLVM intrinsic, but the signature is just the Rust signature. + /// FIXME: this should ideally not exist, we should be using the LLVM signature for all LLVM intrinsics + RustSignature(llvm::Intrinsic, &'ll Type), /// The name starts with `llvm.`, but can't obtain the intrinsic ID. May be invalid or upgradable - MaybeInvalidIntrinsic(&'ll Type), + MaybeInvalid(&'ll Type), /// Just the Rust signature - Rust(&'ll Type), + NotIntrinsic(&'ll Type), } impl<'ll> FunctionSignature<'ll> { pub(crate) fn fn_ty(&self) -> &'ll Type { match self { - FunctionSignature::Intrinsic(fn_ty) - | FunctionSignature::MaybeInvalidIntrinsic(fn_ty) - | FunctionSignature::Rust(fn_ty) => fn_ty, + FunctionSignature::LLVMSignature(_, fn_ty) + | FunctionSignature::RustSignature(_, fn_ty) + | FunctionSignature::MaybeInvalid(fn_ty) + | FunctionSignature::NotIntrinsic(fn_ty) => fn_ty, + } + } + + pub(crate) fn intrinsic(&self) -> Option { + match self { + FunctionSignature::RustSignature(intrinsic, _) + | FunctionSignature::LLVMSignature(intrinsic, _) => Some(*intrinsic), + _ => None, } } } @@ -326,12 +338,9 @@ pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> { /// When `do_verify` is set, this function performs checks for the signature of LLVM intrinsics /// and emits a fatal error if it doesn't match. These checks are important,but somewhat expensive /// So they are only used at function definitions, not at callsites - fn llvm_type( - &self, - cx: &CodegenCx<'ll, 'tcx>, - name: &[u8], - do_verify: bool, - ) -> FunctionSignature<'ll>; + fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>, name: &[u8]) -> FunctionSignature<'ll>; + /// The normal Rust signature for this + fn rust_signature(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type; /// **If this function is an LLVM intrinsic** checks if the LLVM signature provided matches with this fn verify_intrinsic_signature(&self, cx: &CodegenCx<'ll, 'tcx>, llvm_ty: &'ll Type) -> bool; fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type; @@ -499,34 +508,30 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> { return false; } - // todo: add bypasses for types not accessible from Rust here iter::once((rust_return_ty, llvm_return_ty)) .chain(iter::zip(rust_argument_tys, llvm_argument_tys)) .all(|(rust_ty, llvm_ty)| cx.equate_ty(rust_ty, llvm_ty)) } - fn llvm_type( - &self, - cx: &CodegenCx<'ll, 'tcx>, - name: &[u8], - do_verify: bool, - ) -> FunctionSignature<'ll> { - let mut maybe_invalid = false; + fn rust_signature(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type { + let return_ty = self.llvm_return_type(cx); + let argument_tys = self.llvm_argument_types(cx); + + if self.c_variadic { + cx.type_variadic_func(&argument_tys, return_ty) + } else { + cx.type_func(&argument_tys, return_ty) + } + } + fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>, name: &[u8]) -> FunctionSignature<'ll> { if name.starts_with(b"llvm.") { if let Some(intrinsic) = llvm::Intrinsic::lookup(name) { if !intrinsic.is_overloaded() { // FIXME: also do this for overloaded intrinsics - let llvm_fn_ty = intrinsic.get_type(cx.llcx, &[]); - if do_verify { - if !self.verify_intrinsic_signature(cx, llvm_fn_ty) { - cx.tcx.dcx().fatal(format!( - "Intrinsic signature mismatch for `{}`: expected signature `{llvm_fn_ty:?}`", - str::from_utf8(name).unwrap() - )); - } - } - return FunctionSignature::Intrinsic(llvm_fn_ty); + FunctionSignature::LLVMSignature(intrinsic, intrinsic.get_type(cx.llcx, &[])) + } else { + FunctionSignature::RustSignature(intrinsic, self.rust_signature(cx)) } } else { // it's one of 2 cases, @@ -534,23 +539,10 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> { // - it has been superceded by something else, so the intrinsic was removed entirely // to check for upgrades, we need the `llfn`, so we defer it for now - maybe_invalid = true; + FunctionSignature::MaybeInvalid(self.rust_signature(cx)) } - } - - let return_ty = self.llvm_return_type(cx); - let argument_tys = self.llvm_argument_types(cx); - - let fn_ty = if self.c_variadic { - cx.type_variadic_func(&argument_tys, return_ty) } else { - cx.type_func(&argument_tys, return_ty) - }; - - if maybe_invalid { - FunctionSignature::MaybeInvalidIntrinsic(fn_ty) - } else { - FunctionSignature::Rust(fn_ty) + FunctionSignature::NotIntrinsic(self.rust_signature(cx)) } } @@ -699,15 +691,9 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> { callsite: &'ll Value, llfn: &'ll Value, ) { - // if we are using the LLVM signature, use the LLVM attributes otherwise it might be problematic - let name = llvm::get_value_name(llfn); - if name.starts_with(b"llvm.") - && let Some(intrinsic) = llvm::Intrinsic::lookup(name) - { - // FIXME: also do this for overloaded intrinsics - if !intrinsic.is_overloaded() { - return; - } + // Don't apply any attributes to LLVM intrinsics, they will be applied by AutoUpgrade + if llvm::get_value_name(llfn).starts_with(b"llvm.") { + return; } let mut func_attrs = SmallVec::<[_; 2]>::new(); diff --git a/compiler/rustc_codegen_llvm/src/declare.rs b/compiler/rustc_codegen_llvm/src/declare.rs index 7d29e88d73aee..45a25142ea56b 100644 --- a/compiler/rustc_codegen_llvm/src/declare.rs +++ b/compiler/rustc_codegen_llvm/src/declare.rs @@ -29,7 +29,7 @@ use crate::llvm::AttributePlace::Function; use crate::llvm::Visibility; use crate::type_::Type; use crate::value::Value; -use crate::{attributes, llvm}; +use crate::{attributes, errors, llvm}; /// Declare a function with a SimpleCx. /// @@ -150,34 +150,39 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { ) -> &'ll Value { debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi); - let signature = fn_abi.llvm_type(self, name.as_bytes(), true); - let llfn; + let signature = fn_abi.llvm_type(self, name.as_bytes()); - if let FunctionSignature::Intrinsic(fn_ty) = signature { - // intrinsics have a specified set of attributes, so we don't use the `FnAbi` set for them - llfn = declare_simple_fn( - self, - name, - fn_abi.llvm_cconv(self), - llvm::UnnamedAddr::Global, - llvm::Visibility::Default, - fn_ty, - ); - } else { - // Function addresses in Rust are never significant, allowing functions to - // be merged. - llfn = declare_raw_fn( - self, - name, - fn_abi.llvm_cconv(self), - llvm::UnnamedAddr::Global, - llvm::Visibility::Default, - signature.fn_ty(), - ); + let span = || instance.map(|instance| self.tcx.def_span(instance.def_id())); + + if let FunctionSignature::LLVMSignature(_, llvm_fn_ty) = signature { + // check if the intrinsic signatures match + if !fn_abi.verify_intrinsic_signature(self, llvm_fn_ty) { + self.tcx.dcx().emit_fatal(errors::IntrinsicSignatureMismatch { + name, + llvm_fn_ty: &format!("{llvm_fn_ty:?}"), + rust_fn_ty: &format!("{:?}", fn_abi.rust_signature(self)), + span: span(), + }); + } + } + + // Function addresses in Rust are never significant, allowing functions to + // be merged. + let llfn = declare_raw_fn( + self, + name, + fn_abi.llvm_cconv(self), + llvm::UnnamedAddr::Global, + llvm::Visibility::Default, + signature.fn_ty(), + ); + + if signature.intrinsic().is_none() { + // Don't apply any attributes to intrinsics, they will be applied by AutoUpgrade fn_abi.apply_attrs_llfn(self, llfn, instance); } - if let FunctionSignature::MaybeInvalidIntrinsic(..) = signature { + if let FunctionSignature::MaybeInvalid(..) = signature { let mut new_llfn = None; let can_upgrade = unsafe { llvm::LLVMRustUpgradeIntrinsicFunction(llfn, &mut new_llfn, false) }; @@ -185,17 +190,19 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { if can_upgrade { // not all intrinsics are upgraded to some other intrinsics, most are upgraded to instruction sequences if let Some(new_llfn) = new_llfn { - self.tcx.dcx().note(format!( - "Using deprecated intrinsic `{name}`, `{}` can be used instead", - str::from_utf8(llvm::get_value_name(new_llfn)).unwrap() - )); + self.tcx.dcx().emit_note(errors::DeprecatedIntrinsicWithReplacement { + name, + replacement: str::from_utf8(llvm::get_value_name(new_llfn)).unwrap(), + span: span(), + }); } else if self.tcx.sess.opts.verbose { - self.tcx.dcx().note(format!( - "Using deprecated intrinsic `{name}`, consider using other intrinsics/instructions" - )); + // At least for now, we are only emitting notes for deprecated intrinsics with no direct replacement + // because they are used quite a lot in stdarch. After the stdarch uses has been removed, we can make + // this always emit a note (or even an warning) + self.tcx.dcx().emit_note(errors::DeprecatedIntrinsic { name, span: span() }); } } else { - self.tcx.dcx().fatal(format!("Invalid LLVM intrinsic: `{name}`")) + self.tcx.dcx().emit_fatal(errors::InvalidIntrinsic { name, span: span() }); } } diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 8bc74fbec7ece..0f57c274c15eb 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -192,3 +192,38 @@ pub(crate) struct FixedX18InvalidArch<'a> { #[derive(Diagnostic)] #[diag(codegen_llvm_sanitizer_kcfi_arity_requires_llvm_21_0_0)] pub(crate) struct SanitizerKcfiArityRequiresLLVM2100; + +#[derive(Diagnostic)] +#[diag(codegen_llvm_intrinsic_signature_mismatch)] +pub(crate) struct IntrinsicSignatureMismatch<'a> { + pub name: &'a str, + pub llvm_fn_ty: &'a str, + pub rust_fn_ty: &'a str, + #[primary_span] + pub span: Option, +} + +#[derive(Diagnostic)] +#[diag(codegen_llvm_invalid_intrinsic)] +pub(crate) struct InvalidIntrinsic<'a> { + pub name: &'a str, + #[primary_span] + pub span: Option, +} + +#[derive(Diagnostic)] +#[diag(codegen_llvm_deprecated_intrinsic)] +pub(crate) struct DeprecatedIntrinsic<'a> { + pub name: &'a str, + #[primary_span] + pub span: Option, +} + +#[derive(Diagnostic)] +#[diag(codegen_llvm_deprecated_intrinsic_with_replacement)] +pub(crate) struct DeprecatedIntrinsicWithReplacement<'a> { + pub name: &'a str, + pub replacement: &'a str, + #[primary_span] + pub span: Option, +} diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 826e997ad84e9..1bbc915846bf8 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -1059,7 +1059,7 @@ fn gen_fn<'a, 'll, 'tcx>( codegen: &mut dyn FnMut(Builder<'a, 'll, 'tcx>), ) -> (&'ll Type, &'ll Value) { let fn_abi = cx.fn_abi_of_fn_ptr(rust_fn_sig, ty::List::empty()); - let llty = fn_abi.llvm_type(cx, name.as_bytes(), true).fn_ty(); + let llty = fn_abi.llvm_type(cx, name.as_bytes()).fn_ty(); let llfn = cx.declare_fn(name, fn_abi, None); cx.set_frame_pointer_type(llfn); cx.apply_target_cpu_attr(llfn); diff --git a/compiler/rustc_codegen_llvm/src/type_.rs b/compiler/rustc_codegen_llvm/src/type_.rs index 6c7a7c95fcd65..f5157bdcf40b5 100644 --- a/compiler/rustc_codegen_llvm/src/type_.rs +++ b/compiler/rustc_codegen_llvm/src/type_.rs @@ -309,7 +309,7 @@ impl<'ll, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> { fn_abi: &FnAbi<'tcx, Ty<'tcx>>, fn_ptr: &'ll Value, ) -> &'ll Type { - fn_abi.llvm_type(self, llvm::get_value_name(fn_ptr), false).fn_ty() + fn_abi.llvm_type(self, llvm::get_value_name(fn_ptr)).fn_ty() } fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Type { fn_abi.ptr_to_llvm_type(self) diff --git a/tests/ui/codegen/deprecated-llvm-intrinsic.rs b/tests/ui/codegen/deprecated-llvm-intrinsic.rs index 8ae363054cc74..f546c69ce9cc8 100644 --- a/tests/ui/codegen/deprecated-llvm-intrinsic.rs +++ b/tests/ui/codegen/deprecated-llvm-intrinsic.rs @@ -18,11 +18,10 @@ pub struct i8x8([i8; 8]); extern "unadjusted" { #[link_name = "llvm.aarch64.neon.rbit.v8i8"] fn foo(a: i8x8) -> i8x8; + //~^ NOTE: Using deprecated intrinsic `llvm.aarch64.neon.rbit.v8i8`, `llvm.bitreverse.v8i8` can be used instead } #[target_feature(enable = "neon")] pub unsafe fn bar(a: i8x8) -> i8x8 { foo(a) } - -//~? NOTE: Using deprecated intrinsic `llvm.aarch64.neon.rbit.v8i8`, `llvm.bitreverse.v8i8` can be used instead diff --git a/tests/ui/codegen/deprecated-llvm-intrinsic.stderr b/tests/ui/codegen/deprecated-llvm-intrinsic.stderr index 214ac269e7d0e..b0d578e4f918c 100644 --- a/tests/ui/codegen/deprecated-llvm-intrinsic.stderr +++ b/tests/ui/codegen/deprecated-llvm-intrinsic.stderr @@ -1,2 +1,6 @@ note: Using deprecated intrinsic `llvm.aarch64.neon.rbit.v8i8`, `llvm.bitreverse.v8i8` can be used instead + --> $DIR/deprecated-llvm-intrinsic.rs:20:5 + | +LL | fn foo(a: i8x8) -> i8x8; + | ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/codegen/incorrect-llvm-intrinsic-signature.rs b/tests/ui/codegen/incorrect-llvm-intrinsic-signature.rs index 84c4c0d747247..f272fb1d19a80 100644 --- a/tests/ui/codegen/incorrect-llvm-intrinsic-signature.rs +++ b/tests/ui/codegen/incorrect-llvm-intrinsic-signature.rs @@ -1,15 +1,13 @@ //@ build-fail #![feature(link_llvm_intrinsics, abi_unadjusted)] -#![allow(internal_features, non_camel_case_types, improper_ctypes)] extern "unadjusted" { #[link_name = "llvm.assume"] fn foo(); + //~^ ERROR: Intrinsic signature mismatch for `llvm.assume`: expected signature `void (i1)`, found `void ()` } pub fn main() { unsafe { foo() } } - -//~? ERROR: Intrinsic signature mismatch for `llvm.assume`: expected signature `void (i1)` diff --git a/tests/ui/codegen/incorrect-llvm-intrinsic-signature.stderr b/tests/ui/codegen/incorrect-llvm-intrinsic-signature.stderr index f67ba8a65a40c..e443fb7d48534 100644 --- a/tests/ui/codegen/incorrect-llvm-intrinsic-signature.stderr +++ b/tests/ui/codegen/incorrect-llvm-intrinsic-signature.stderr @@ -1,4 +1,8 @@ -error: Intrinsic signature mismatch for `llvm.assume`: expected signature `void (i1)` +error: Intrinsic signature mismatch for `llvm.assume`: expected signature `void (i1)`, found `void ()` + --> $DIR/incorrect-llvm-intrinsic-signature.rs:7:5 + | +LL | fn foo(); + | ^^^^^^^^^ error: aborting due to 1 previous error diff --git a/tests/ui/codegen/invalid-llvm-intrinsic.rs b/tests/ui/codegen/invalid-llvm-intrinsic.rs index 2f1ff826ed39b..01edb114d0086 100644 --- a/tests/ui/codegen/invalid-llvm-intrinsic.rs +++ b/tests/ui/codegen/invalid-llvm-intrinsic.rs @@ -1,15 +1,13 @@ //@ build-fail #![feature(link_llvm_intrinsics, abi_unadjusted)] -#![allow(internal_features, non_camel_case_types, improper_ctypes)] extern "unadjusted" { #[link_name = "llvm.abcde"] fn foo(); + //~^ ERROR: Invalid LLVM Intrinsic `llvm.abcde` } pub fn main() { unsafe { foo() } } - -//~? ERROR: Invalid LLVM intrinsic: `llvm.abcde` diff --git a/tests/ui/codegen/invalid-llvm-intrinsic.stderr b/tests/ui/codegen/invalid-llvm-intrinsic.stderr index 467d6a62553cc..39a6ca52895e8 100644 --- a/tests/ui/codegen/invalid-llvm-intrinsic.stderr +++ b/tests/ui/codegen/invalid-llvm-intrinsic.stderr @@ -1,4 +1,8 @@ -error: Invalid LLVM intrinsic: `llvm.abcde` +error: Invalid LLVM Intrinsic `llvm.abcde` + --> $DIR/invalid-llvm-intrinsic.rs:7:5 + | +LL | fn foo(); + | ^^^^^^^^^ error: aborting due to 1 previous error From c64b2ee57f7595a4c2dbdd55ee4e869d05d08ccd Mon Sep 17 00:00:00 2001 From: sayantn Date: Tue, 10 Jun 2025 13:16:24 +0530 Subject: [PATCH 10/10] Add target arch verification for LLVM intrinsics --- compiler/rustc_codegen_llvm/messages.ftl | 3 ++ compiler/rustc_codegen_llvm/src/declare.rs | 31 ++++++++++++++++++- compiler/rustc_codegen_llvm/src/errors.rs | 9 ++++++ compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 1 + compiler/rustc_codegen_llvm/src/llvm/mod.rs | 4 +++ .../rustc_llvm/llvm-wrapper/RustWrapper.cpp | 8 +++++ tests/ui/codegen/incorrect-arch-intrinsic.rs | 17 ++++++++++ .../codegen/incorrect-arch-intrinsic.stderr | 8 +++++ 8 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 tests/ui/codegen/incorrect-arch-intrinsic.rs create mode 100644 tests/ui/codegen/incorrect-arch-intrinsic.stderr diff --git a/compiler/rustc_codegen_llvm/messages.ftl b/compiler/rustc_codegen_llvm/messages.ftl index 5e12e116ac74c..9379cbc8cf4eb 100644 --- a/compiler/rustc_codegen_llvm/messages.ftl +++ b/compiler/rustc_codegen_llvm/messages.ftl @@ -23,6 +23,9 @@ codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_ codegen_llvm_intrinsic_signature_mismatch = Intrinsic signature mismatch for `{$name}`: expected signature `{$llvm_fn_ty}`, found `{$rust_fn_ty}` +codegen_llvm_intrinsic_wrong_arch = + Intrinsic `{$name}` cannot be used with target arch `{$target_arch}` + codegen_llvm_invalid_intrinsic = Invalid LLVM Intrinsic `{$name}` diff --git a/compiler/rustc_codegen_llvm/src/declare.rs b/compiler/rustc_codegen_llvm/src/declare.rs index 45a25142ea56b..447b453ccaca9 100644 --- a/compiler/rustc_codegen_llvm/src/declare.rs +++ b/compiler/rustc_codegen_llvm/src/declare.rs @@ -177,7 +177,36 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { signature.fn_ty(), ); - if signature.intrinsic().is_none() { + if let Some(intrinsic) = signature.intrinsic() { + if intrinsic.is_target_specific() { + let (llvm_arch, _) = name[5..].split_once('.').unwrap(); + let target_arch = self.tcx.sess.target.arch.as_ref(); + + let is_correct_arch = match llvm_arch { + "aarch64" => matches!(target_arch, "aarch64" | "arm64ec"), + "amdgcn" => target_arch == "amdgpu", + "arm" | "bpf" | "hexagon" => target_arch == llvm_arch, + "loongarch" => matches!(target_arch, "loongarch32" | "loongarch64"), + "mips" => target_arch.starts_with("mips"), + "nvvm" => target_arch == "nvptx64", + "ppc" => matches!(target_arch, "powerpc" | "powerpc64"), + "riscv" => matches!(target_arch, "riscv32" | "riscv64"), + "s390" => target_arch == "s390x", + "spv" => target_arch == "spirv", + "wasm" => matches!(target_arch, "wasm32" | "wasm64"), + "x86" => matches!(target_arch, "x86" | "x86_64"), + _ => true, // fallback for unknown archs + }; + + if !is_correct_arch { + self.tcx.dcx().emit_fatal(errors::IntrinsicWrongArch { + name, + target_arch, + span: span(), + }); + } + } + } else { // Don't apply any attributes to intrinsics, they will be applied by AutoUpgrade fn_abi.apply_attrs_llfn(self, llfn, instance); } diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 0f57c274c15eb..906070afab583 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -227,3 +227,12 @@ pub(crate) struct DeprecatedIntrinsicWithReplacement<'a> { #[primary_span] pub span: Option, } + +#[derive(Diagnostic)] +#[diag(codegen_llvm_intrinsic_wrong_arch)] +pub(crate) struct IntrinsicWrongArch<'a> { + pub name: &'a str, + pub target_arch: &'a str, + #[primary_span] + pub span: Option, +} diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index d05ff5b488394..e4aac5d87a24a 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -1218,6 +1218,7 @@ unsafe extern "C" { NewFn: &mut Option<&'a Value>, CanUpgradeDebugIntrinsicsToRecords: bool, ) -> bool; + pub(crate) fn LLVMRustIsTargetIntrinsic(ID: NonZero) -> bool; // Operations on parameters pub(crate) fn LLVMIsAArgument(Val: &Value) -> Option<&Value>; diff --git a/compiler/rustc_codegen_llvm/src/llvm/mod.rs b/compiler/rustc_codegen_llvm/src/llvm/mod.rs index ea462cb72a318..b8508d2080411 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/mod.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/mod.rs @@ -343,6 +343,10 @@ impl Intrinsic { unsafe { LLVMIntrinsicIsOverloaded(self.id) == True } } + pub(crate) fn is_target_specific(self) -> bool { + unsafe { LLVMRustIsTargetIntrinsic(self.id) } + } + pub(crate) fn get_type<'ll>(self, llcx: &'ll Context, type_params: &[&'ll Type]) -> &'ll Type { unsafe { LLVMIntrinsicGetType(llcx, self.id, type_params.as_ptr(), type_params.len()) } } diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index a935dc6dca8df..db3e3f039abe2 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -1913,6 +1913,14 @@ LLVMRustUpgradeIntrinsicFunction(LLVMValueRef Fn, LLVMValueRef *NewFn, return CanUpgrade; } +extern "C" bool LLVMRustIsTargetIntrinsic(unsigned ID) { +#if LLVM_VERSION_GE(20, 1) + return Intrinsic::isTargetIntrinsic(ID); +#else + return Function::isTargetIntrinsic(ID); +#endif +} + extern "C" int32_t LLVMRustGetElementTypeArgIndex(LLVMValueRef CallSite) { auto *CB = unwrap(CallSite); switch (CB->getIntrinsicID()) { diff --git a/tests/ui/codegen/incorrect-arch-intrinsic.rs b/tests/ui/codegen/incorrect-arch-intrinsic.rs new file mode 100644 index 0000000000000..22743b80aa62d --- /dev/null +++ b/tests/ui/codegen/incorrect-arch-intrinsic.rs @@ -0,0 +1,17 @@ +//@ build-fail +//@ ignore-s390x +//@ normalize-stderr: "target arch `(.*)`" -> "target arch `TARGET_ARCH`" + +#![feature(link_llvm_intrinsics, abi_unadjusted)] + +extern "unadjusted" { + #[link_name = "llvm.s390.sfpc"] + fn foo(a: i32); + //~^ ERROR: Intrinsic `llvm.s390.sfpc` cannot be used with target arch +} + +pub fn main() { + unsafe { + foo(0); + } +} diff --git a/tests/ui/codegen/incorrect-arch-intrinsic.stderr b/tests/ui/codegen/incorrect-arch-intrinsic.stderr new file mode 100644 index 0000000000000..c4ecf2ed11724 --- /dev/null +++ b/tests/ui/codegen/incorrect-arch-intrinsic.stderr @@ -0,0 +1,8 @@ +error: Intrinsic `llvm.s390.sfpc` cannot be used with target arch `TARGET_ARCH` + --> $DIR/incorrect-arch-intrinsic.rs:9:5 + | +LL | fn foo(a: i32); + | ^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error +