rustc_monomorphize/mono_checks/
abi_check.rs1use rustc_abi::{BackendRepr, RegKind};
4use rustc_hir::{CRATE_HIR_ID, HirId};
5use rustc_middle::mir::{self, Location, traversal};
6use rustc_middle::ty::layout::LayoutCx;
7use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TyCtxt, TypingEnv};
8use rustc_session::lint::builtin::{ABI_UNSUPPORTED_VECTOR_TYPES, WASM_C_ABI};
9use rustc_span::def_id::DefId;
10use rustc_span::{DUMMY_SP, Span, Symbol, sym};
11use rustc_target::callconv::{ArgAbi, Conv, FnAbi, PassMode};
12use rustc_target::spec::{HasWasmCAbiOpt, WasmCAbi};
13
14use crate::errors;
15
16fn uses_vector_registers(mode: &PassMode, repr: &BackendRepr) -> bool {
17 match mode {
18 PassMode::Ignore | PassMode::Indirect { .. } => false,
19 PassMode::Cast { pad_i32: _, cast } => {
20 cast.prefix.iter().any(|r| r.is_some_and(|x| x.kind == RegKind::Vector))
21 || cast.rest.unit.kind == RegKind::Vector
22 }
23 PassMode::Direct(..) | PassMode::Pair(..) => matches!(repr, BackendRepr::SimdVector { .. }),
24 }
25}
26
27fn do_check_simd_vector_abi<'tcx>(
32 tcx: TyCtxt<'tcx>,
33 abi: &FnAbi<'tcx, Ty<'tcx>>,
34 def_id: DefId,
35 is_call: bool,
36 loc: impl Fn() -> (Span, HirId),
37) {
38 let feature_def = tcx.sess.target.features_for_correct_vector_abi();
41 let codegen_attrs = tcx.codegen_fn_attrs(def_id);
42 let have_feature = |feat: Symbol| {
43 tcx.sess.unstable_target_features.contains(&feat)
44 || codegen_attrs.target_features.iter().any(|x| x.name == feat)
45 };
46 for arg_abi in abi.args.iter().chain(std::iter::once(&abi.ret)) {
47 let size = arg_abi.layout.size;
48 if uses_vector_registers(&arg_abi.mode, &arg_abi.layout.backend_repr) {
49 let feature = match feature_def.iter().find(|(bits, _)| size.bits() <= *bits) {
51 Some((_, feature)) => feature,
52 None => {
53 let (span, hir_id) = loc();
54 tcx.emit_node_span_lint(
55 ABI_UNSUPPORTED_VECTOR_TYPES,
56 hir_id,
57 span,
58 errors::AbiErrorUnsupportedVectorType {
59 span,
60 ty: arg_abi.layout.ty,
61 is_call,
62 },
63 );
64 continue;
65 }
66 };
67 if !have_feature(Symbol::intern(feature)) {
68 let (span, hir_id) = loc();
70 tcx.emit_node_span_lint(
71 ABI_UNSUPPORTED_VECTOR_TYPES,
72 hir_id,
73 span,
74 errors::AbiErrorDisabledVectorType {
75 span,
76 required_feature: feature,
77 ty: arg_abi.layout.ty,
78 is_call,
79 },
80 );
81 }
82 }
83 }
84 if abi.conv == Conv::X86VectorCall && !have_feature(sym::sse2) {
86 let (span, _hir_id) = loc();
87 tcx.dcx().emit_err(errors::AbiRequiredTargetFeature {
88 span,
89 required_feature: "sse2",
90 abi: "vectorcall",
91 is_call,
92 });
93 }
94}
95
96fn wasm_abi_safe<'tcx>(tcx: TyCtxt<'tcx>, arg: &ArgAbi<'tcx, Ty<'tcx>>) -> bool {
98 if matches!(arg.layout.backend_repr, BackendRepr::Scalar(_)) {
99 return true;
100 }
101
102 if arg.layout.is_aggregate() {
104 let cx = LayoutCx::new(tcx, TypingEnv::fully_monomorphized());
105 if let Some(unit) = arg.layout.homogeneous_aggregate(&cx).ok().and_then(|ha| ha.unit()) {
106 let size = arg.layout.size;
107 if unit.size == size {
109 return true;
110 }
111 }
112 }
113
114 false
115}
116
117fn do_check_wasm_abi<'tcx>(
120 tcx: TyCtxt<'tcx>,
121 abi: &FnAbi<'tcx, Ty<'tcx>>,
122 is_call: bool,
123 loc: impl Fn() -> (Span, HirId),
124) {
125 if !(tcx.sess.target.arch == "wasm32"
128 && tcx.sess.target.os == "unknown"
129 && tcx.wasm_c_abi_opt() == WasmCAbi::Legacy { with_lint: true }
130 && abi.conv == Conv::C)
131 {
132 return;
133 }
134 for arg_abi in abi.args.iter() {
136 if wasm_abi_safe(tcx, arg_abi) {
137 continue;
138 }
139 let (span, hir_id) = loc();
140 tcx.emit_node_span_lint(
141 WASM_C_ABI,
142 hir_id,
143 span,
144 errors::WasmCAbiTransition { ty: arg_abi.layout.ty, is_call },
145 );
146 break;
148 }
149}
150
151fn check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
154 let typing_env = ty::TypingEnv::fully_monomorphized();
155 let Ok(abi) = tcx.fn_abi_of_instance(typing_env.as_query_input((instance, ty::List::empty())))
156 else {
157 return;
160 };
161 let loc = || {
162 let def_id = instance.def_id();
163 (
164 tcx.def_span(def_id),
165 def_id.as_local().map(|did| tcx.local_def_id_to_hir_id(did)).unwrap_or(CRATE_HIR_ID),
166 )
167 };
168 do_check_simd_vector_abi(tcx, abi, instance.def_id(), false, loc);
169 do_check_wasm_abi(tcx, abi, false, loc);
170}
171
172fn check_call_site_abi<'tcx>(
175 tcx: TyCtxt<'tcx>,
176 callee: Ty<'tcx>,
177 caller: InstanceKind<'tcx>,
178 loc: impl Fn() -> (Span, HirId) + Copy,
179) {
180 if callee.fn_sig(tcx).abi().is_rustic_abi() {
181 return;
183 }
184 let typing_env = ty::TypingEnv::fully_monomorphized();
185 let callee_abi = match *callee.kind() {
186 ty::FnPtr(..) => {
187 tcx.fn_abi_of_fn_ptr(typing_env.as_query_input((callee.fn_sig(tcx), ty::List::empty())))
188 }
189 ty::FnDef(def_id, args) => {
190 if tcx.intrinsic(def_id).is_some() {
192 return;
193 }
194 let instance = ty::Instance::expect_resolve(tcx, typing_env, def_id, args, DUMMY_SP);
195 tcx.fn_abi_of_instance(typing_env.as_query_input((instance, ty::List::empty())))
196 }
197 _ => {
198 panic!("Invalid function call");
199 }
200 };
201
202 let Ok(callee_abi) = callee_abi else {
203 return;
205 };
206 do_check_simd_vector_abi(tcx, callee_abi, caller.def_id(), true, loc);
207 do_check_wasm_abi(tcx, callee_abi, true, loc);
208}
209
210fn check_callees_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>, body: &mir::Body<'tcx>) {
211 for (bb, _data) in traversal::mono_reachable(body, tcx, instance) {
213 let terminator = body.basic_blocks[bb].terminator();
214 match terminator.kind {
215 mir::TerminatorKind::Call { ref func, ref fn_span, .. }
216 | mir::TerminatorKind::TailCall { ref func, ref fn_span, .. } => {
217 let callee_ty = func.ty(body, tcx);
218 let callee_ty = instance.instantiate_mir_and_normalize_erasing_regions(
219 tcx,
220 ty::TypingEnv::fully_monomorphized(),
221 ty::EarlyBinder::bind(callee_ty),
222 );
223 check_call_site_abi(tcx, callee_ty, body.source.instance, || {
224 let loc = Location {
225 block: bb,
226 statement_index: body.basic_blocks[bb].statements.len(),
227 };
228 (
229 *fn_span,
230 body.source_info(loc)
231 .scope
232 .lint_root(&body.source_scopes)
233 .unwrap_or(CRATE_HIR_ID),
234 )
235 });
236 }
237 _ => {}
238 }
239 }
240}
241
242pub(crate) fn check_feature_dependent_abi<'tcx>(
243 tcx: TyCtxt<'tcx>,
244 instance: Instance<'tcx>,
245 body: &'tcx mir::Body<'tcx>,
246) {
247 check_instance_abi(tcx, instance);
248 check_callees_abi(tcx, instance, body);
249}