@@ -6,7 +6,7 @@ use rustc_middle::mir::{self, Location, traversal};
66use rustc_middle:: ty:: layout:: { FnAbiRequest , codegen_handle_fn_abi_err} ;
77use rustc_middle:: ty:: { self , Instance , InstanceKind , Ty , TyCtxt } ;
88use rustc_span:: def_id:: DefId ;
9- use rustc_span:: { DUMMY_SP , Span , Symbol , sym} ;
9+ use rustc_span:: { DUMMY_SP , Span , Symbol , span_bug , sym} ;
1010use rustc_target:: callconv:: { FnAbi , PassMode } ;
1111
1212use crate :: diagnostics;
@@ -214,15 +214,47 @@ fn check_call_site_abi<'tcx>(
214214 loc : impl Fn ( ) -> ( Span , HirId ) + Copy ,
215215) {
216216 let extern_abi = callee. fn_sig ( tcx) . abi ( ) ;
217- if extern_abi. is_rustic_abi ( ) || extern_abi == ExternAbi :: LlvmIntrinsic {
217+ if extern_abi. is_rustic_abi ( ) {
218218 // We directly handle the soundness of Rust ABIs -- so let's skip the majority of
219219 // call sites to avoid a perf regression.
220220 // FIXME(#161753, rustc_scalable_vector/stdarch_aarch64_sve): this is unsound! Above we
221221 // argue we need the callee-site check for the Rust ABI; we need the call-site check here as
222222 // well then.
223+ return ;
224+ }
225+ if extern_abi == ExternAbi :: LlvmIntrinsic {
223226 // We disable all checks for the llvm-intrinsic ABI to allow linking to arbitrary
224227 // LLVM intrinsics. The caller is responsible for ensuring the ABI makes sense.
225- return ;
228+ let typing_env = ty:: TypingEnv :: fully_monomorphized ( ) ;
229+ match * callee. kind ( ) {
230+ ty:: FnDef ( def_id, args) => {
231+ let instance = ty:: Instance :: expect_resolve (
232+ tcx,
233+ typing_env,
234+ def_id,
235+ args. no_bound_vars ( ) . unwrap ( ) ,
236+ DUMMY_SP ,
237+ ) ;
238+ let InstanceKind :: LlvmIntrinsic ( ..) = instance. def else {
239+ span_bug ! (
240+ loc( ) . 0 ,
241+ "`extern \" llvm-intrinsic\" ` may only be used for LLVM intrinsics"
242+ ) ;
243+ } ;
244+ let name = tcx
245+ . codegen_fn_attrs ( def_id)
246+ . symbol_name
247+ . expect ( "llvm-intrinsic must be foreign function" ) ;
248+ assert ! (
249+ name. as_str( ) . starts_with( "llvm." ) ,
250+ "llvm-intrinsic name must start with llvm."
251+ ) ;
252+ return ;
253+ }
254+ _ => {
255+ span_bug ! ( loc( ) . 0 , "`extern \" llvm-intrinsic\" ` may only be used for direct calls" )
256+ }
257+ } ;
226258 }
227259 let typing_env = ty:: TypingEnv :: fully_monomorphized ( ) ;
228260 let callee = caller. instantiate_mir_and_normalize_erasing_regions (
@@ -260,8 +292,15 @@ fn check_call_site_abi<'tcx>(
260292 DUMMY_SP ,
261293 ) ;
262294 if let InstanceKind :: LlvmIntrinsic ( ..) = instance. def {
263- // LLVM intrinsics don't have an ABI, so there is nothing to check.
264- return ;
295+ let name = tcx
296+ . codegen_fn_attrs ( def_id)
297+ . symbol_name
298+ . expect ( "llvm-intrinsic must be foreign function" ) ;
299+ // llvm.wasm.throw is the only unwinding LLVM intrinsic we have.
300+ // FIXME maybe replace with inline asm?
301+ if name. as_str ( ) != "llvm.wasm.throw" {
302+ span_bug ! ( loc( ) . 0 , "llvm.* function must use `extern \" llvm-intrinsic\" `" ) ;
303+ }
265304 }
266305 match tcx. fn_abi_of_instance ( typing_env. as_query_input ( ( instance, ty:: List :: empty ( ) ) ) ) {
267306 Ok ( callee_abi) => callee_abi,
0 commit comments