@@ -20,6 +20,7 @@ use rustc_middle::ty::{Instance, Ty};
2020use rustc_sanitizers:: { cfi, kcfi} ;
2121use rustc_session:: lint:: builtin:: { DEPRECATED_LLVM_INTRINSIC , UNKNOWN_LLVM_INTRINSIC } ;
2222use rustc_target:: callconv:: FnAbi ;
23+ use rustc_target:: spec:: Arch ;
2324use smallvec:: SmallVec ;
2425use tracing:: debug;
2526
@@ -100,6 +101,26 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
100101 }
101102}
102103
104+ fn llvm_arch_for ( rust_arch : & Arch ) -> Option < & ' static str > {
105+ Some ( match rust_arch {
106+ Arch :: AArch64 | Arch :: Arm64EC => "aarch64" ,
107+ Arch :: AmdGpu => "amdgcn" ,
108+ Arch :: Arm => "arm" ,
109+ Arch :: Bpf => "bpf" ,
110+ Arch :: Hexagon => "hexagon" ,
111+ Arch :: LoongArch32 | Arch :: LoongArch64 => "loongarch" ,
112+ Arch :: Mips | Arch :: Mips32r6 | Arch :: Mips64 | Arch :: Mips64r6 => "mips" ,
113+ Arch :: Nvptx64 => "nvvm" ,
114+ Arch :: PowerPC | Arch :: PowerPC64 | Arch :: PowerPC64LE => "ppc" ,
115+ Arch :: RiscV32 | Arch :: RiscV64 => "riscv" ,
116+ Arch :: S390x => "s390" ,
117+ Arch :: SpirV => "spv" ,
118+ Arch :: Wasm32 | Arch :: Wasm64 => "wasm" ,
119+ Arch :: X86 | Arch :: X86_64 => "x86" ,
120+ _ => return None , // fallback for unknown archs
121+ } )
122+ }
123+
103124impl < ' ll , ' tcx > CodegenCx < ' ll , ' tcx > {
104125 /// Declare a C ABI function.
105126 ///
@@ -176,7 +197,22 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
176197 signature. fn_ty ( ) ,
177198 ) ;
178199
179- if signature. intrinsic ( ) . is_none ( ) {
200+ if let Some ( intrinsic) = signature. intrinsic ( )
201+ && intrinsic. is_target_specific ( )
202+ {
203+ let ( llvm_arch, _) = name[ 5 ..] . split_once ( '.' ) . unwrap ( ) ;
204+ let rust_arch = & self . tcx . sess . target . arch ;
205+
206+ if let Some ( correct_llvm_arch) = llvm_arch_for ( rust_arch)
207+ && llvm_arch != correct_llvm_arch
208+ {
209+ self . tcx . dcx ( ) . emit_fatal ( errors:: IntrinsicWrongArch {
210+ name,
211+ target_arch : rust_arch. desc ( ) ,
212+ span : span ( ) ,
213+ } ) ;
214+ }
215+ } else {
180216 // Don't apply any attributes to intrinsics, they will be applied by AutoUpgrade
181217 fn_abi. apply_attrs_llfn ( self , llfn, instance) ;
182218 }
0 commit comments