33use rustc_abi:: { BackendRepr , RegKind } ;
44use rustc_hir:: CRATE_HIR_ID ;
55use rustc_middle:: mir:: { self , traversal} ;
6- use rustc_middle:: ty:: { self , Instance , InstanceKind , Ty , TyCtxt } ;
7- use rustc_session:: lint:: builtin:: ABI_UNSUPPORTED_VECTOR_TYPES ;
6+ use rustc_middle:: ty:: layout:: LayoutCx ;
7+ use rustc_middle:: ty:: { self , Instance , InstanceKind , Ty , TyCtxt , TypingEnv } ;
8+ use rustc_session:: lint:: builtin:: { ABI_UNSUPPORTED_VECTOR_TYPES , WASM_C_ABI } ;
89use rustc_span:: def_id:: DefId ;
910use rustc_span:: { DUMMY_SP , Span , Symbol , sym} ;
10- use rustc_target:: callconv:: { Conv , FnAbi , PassMode } ;
11+ use rustc_target:: callconv:: { ArgAbi , Conv , FnAbi , PassMode } ;
12+ use rustc_target:: spec:: { HasWasmCAbiOpt , WasmCAbi } ;
1113
1214use crate :: errors;
1315
@@ -26,13 +28,15 @@ fn uses_vector_registers(mode: &PassMode, repr: &BackendRepr) -> bool {
2628/// for a certain function.
2729/// `is_call` indicates whether this is a call-site check or a definition-site check;
2830/// this is only relevant for the wording in the emitted error.
29- fn do_check_abi < ' tcx > (
31+ fn do_check_simd_vector_abi < ' tcx > (
3032 tcx : TyCtxt < ' tcx > ,
3133 abi : & FnAbi < ' tcx , Ty < ' tcx > > ,
3234 def_id : DefId ,
3335 is_call : bool ,
3436 span : impl Fn ( ) -> Span ,
3537) {
38+ // We check this on all functions, including those using the "Rust" ABI.
39+ // For the "Rust" ABI it would be a bug if the lint ever triggered, but better safe than sorry.
3640 let feature_def = tcx. sess . target . features_for_correct_vector_abi ( ) ;
3741 let codegen_attrs = tcx. codegen_fn_attrs ( def_id) ;
3842 let have_feature = |feat : Symbol | {
@@ -88,6 +92,60 @@ fn do_check_abi<'tcx>(
8892 }
8993}
9094
95+ fn wasm_abi_safe < ' tcx > ( tcx : TyCtxt < ' tcx > , arg : & ArgAbi < ' tcx , Ty < ' tcx > > ) -> bool {
96+ if matches ! ( arg. layout. backend_repr, BackendRepr :: Scalar ( _) ) {
97+ return true ;
98+ }
99+
100+ // This matches `unwrap_trivial_aggregate` in the wasm ABI logic.
101+ if arg. layout . is_aggregate ( ) {
102+ let cx = LayoutCx :: new ( tcx, TypingEnv :: fully_monomorphized ( ) ) ;
103+ if let Some ( unit) = arg. layout . homogeneous_aggregate ( & cx) . ok ( ) . and_then ( |ha| ha. unit ( ) ) {
104+ let size = arg. layout . size ;
105+ // Ensure there's just a single `unit` element in `arg`.
106+ if unit. size == size {
107+ return true ;
108+ }
109+ }
110+ }
111+
112+ false
113+ }
114+
115+ /// Warns against usage of `extern "C"` on wasm32-unknown-unknown that is affected by the
116+ /// ABI transition.
117+ fn do_check_wasm_abi < ' tcx > (
118+ tcx : TyCtxt < ' tcx > ,
119+ abi : & FnAbi < ' tcx , Ty < ' tcx > > ,
120+ is_call : bool ,
121+ span : impl Fn ( ) -> Span ,
122+ ) {
123+ // Only proceed for `extern "C" fn` on wasm32-unknown-unknown (same check as what `adjust_for_foreign_abi` uses to call `compute_wasm_abi_info`).
124+ if !( tcx. sess . target . arch == "wasm32"
125+ && tcx. sess . target . os == "unknown"
126+ && tcx. wasm_c_abi_opt ( ) == WasmCAbi :: Legacy
127+ && abi. conv == Conv :: C )
128+ {
129+ return ;
130+ }
131+ // Warn against all types whose ABI will change. That's all arguments except for things passed as scalars.
132+ // Return values are not affected by this change.
133+ for arg_abi in abi. args . iter ( ) {
134+ if wasm_abi_safe ( tcx, arg_abi) {
135+ continue ;
136+ }
137+ let span = span ( ) ;
138+ tcx. emit_node_span_lint (
139+ WASM_C_ABI ,
140+ CRATE_HIR_ID ,
141+ span,
142+ errors:: WasmCAbiTransition { ty : arg_abi. layout . ty , is_call } ,
143+ ) ;
144+ // Let's only warn once per function.
145+ break ;
146+ }
147+ }
148+
91149/// Checks that the ABI of a given instance of a function does not contain vector-passed arguments
92150/// or return values for which the corresponding target feature is not enabled.
93151fn check_instance_abi < ' tcx > ( tcx : TyCtxt < ' tcx > , instance : Instance < ' tcx > ) {
@@ -98,13 +156,10 @@ fn check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
98156 // function.
99157 return ;
100158 } ;
101- do_check_abi (
102- tcx,
103- abi,
104- instance. def_id ( ) ,
105- /*is_call*/ false ,
106- || tcx. def_span ( instance. def_id ( ) ) ,
107- )
159+ do_check_simd_vector_abi ( tcx, abi, instance. def_id ( ) , /*is_call*/ false , || {
160+ tcx. def_span ( instance. def_id ( ) )
161+ } ) ;
162+ do_check_wasm_abi ( tcx, abi, /*is_call*/ false , || tcx. def_span ( instance. def_id ( ) ) ) ;
108163}
109164
110165/// Checks that a call expression does not try to pass a vector-passed argument which requires a
@@ -141,7 +196,8 @@ fn check_call_site_abi<'tcx>(
141196 // ABI failed to compute; this will not get through codegen.
142197 return ;
143198 } ;
144- do_check_abi ( tcx, callee_abi, caller. def_id ( ) , /*is_call*/ true , || span) ;
199+ do_check_simd_vector_abi ( tcx, callee_abi, caller. def_id ( ) , /*is_call*/ true , || span) ;
200+ do_check_wasm_abi ( tcx, callee_abi, /*is_call*/ true , || span) ;
145201}
146202
147203fn check_callees_abi < ' tcx > ( tcx : TyCtxt < ' tcx > , instance : Instance < ' tcx > , body : & mir:: Body < ' tcx > ) {
0 commit comments