|
| 1 | +//! This module ensures that if a function's ABI requires a particular target feature, |
| 2 | +//! that target feature is enabled both on the callee and all callers. |
| 3 | +use rustc_hir::CRATE_HIR_ID; |
| 4 | +use rustc_middle::ty::{self, Instance, InstanceKind, ParamEnv, Ty, TyCtxt}; |
| 5 | +use rustc_session::lint::builtin::ABI_UNSUPPORTED_VECTOR_TYPES; |
| 6 | +use rustc_span::def_id::DefId; |
| 7 | +use rustc_span::{Span, Symbol}; |
| 8 | +use rustc_target::abi::call::{FnAbi, PassMode}; |
| 9 | +use rustc_target::abi::{Abi, RegKind}; |
| 10 | + |
| 11 | +use crate::errors::{AbiErrorDisabledVectorTypeCall, AbiErrorDisabledVectorTypeDef}; |
| 12 | + |
| 13 | +fn uses_vector_registers(mode: &PassMode, abi: &Abi) -> bool { |
| 14 | + match mode { |
| 15 | + PassMode::Ignore | PassMode::Indirect { .. } => false, |
| 16 | + PassMode::Cast { pad_i32: _, cast } => { |
| 17 | + cast.prefix.iter().any(|r| r.is_some_and(|x| x.kind == RegKind::Vector)) |
| 18 | + || cast.rest.unit.kind == RegKind::Vector |
| 19 | + } |
| 20 | + PassMode::Direct(..) | PassMode::Pair(..) => matches!(abi, Abi::Vector { .. }), |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +fn do_check_abi<'tcx>( |
| 25 | + tcx: TyCtxt<'tcx>, |
| 26 | + abi: &FnAbi<'tcx, Ty<'tcx>>, |
| 27 | + target_feature_def: DefId, |
| 28 | + emit_err: impl Fn(&'static str), |
| 29 | +) { |
| 30 | + let Some(feature_def) = tcx.sess.target.features_for_correct_vector_abi() else { |
| 31 | + return; |
| 32 | + }; |
| 33 | + let codegen_attrs = tcx.codegen_fn_attrs(target_feature_def); |
| 34 | + for arg_abi in abi.args.iter().chain(std::iter::once(&abi.ret)) { |
| 35 | + let size = arg_abi.layout.size; |
| 36 | + if uses_vector_registers(&arg_abi.mode, &arg_abi.layout.abi) { |
| 37 | + // Find the first feature that provides at least this vector size. |
| 38 | + let feature = match feature_def.iter().find(|(bits, _)| size.bits() <= *bits) { |
| 39 | + Some((_, feature)) => feature, |
| 40 | + None => { |
| 41 | + emit_err("<no available feature for this size>"); |
| 42 | + continue; |
| 43 | + } |
| 44 | + }; |
| 45 | + let feature_sym = Symbol::intern(feature); |
| 46 | + if !tcx.sess.unstable_target_features.contains(&feature_sym) |
| 47 | + && !codegen_attrs.target_features.iter().any(|x| x.name == feature_sym) |
| 48 | + { |
| 49 | + emit_err(feature); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Checks that the ABI of a given instance of a function does not contain vector-passed arguments |
| 56 | +/// or return values for which the corresponding target feature is not enabled. |
| 57 | +pub(super) fn check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) { |
| 58 | + let param_env = ParamEnv::reveal_all(); |
| 59 | + let Ok(abi) = tcx.fn_abi_of_instance(param_env.and((instance, ty::List::empty()))) else { |
| 60 | + // An error will be reported during codegen if we cannot determine the ABI of this |
| 61 | + // function. |
| 62 | + return; |
| 63 | + }; |
| 64 | + do_check_abi(tcx, abi, instance.def_id(), |required_feature| { |
| 65 | + let span = tcx.def_span(instance.def_id()); |
| 66 | + tcx.emit_node_span_lint( |
| 67 | + ABI_UNSUPPORTED_VECTOR_TYPES, |
| 68 | + CRATE_HIR_ID, |
| 69 | + span, |
| 70 | + AbiErrorDisabledVectorTypeDef { span, required_feature }, |
| 71 | + ); |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +/// Checks that a call expression does not try to pass a vector-passed argument which requires a |
| 76 | +/// target feature that the caller does not have, as doing so causes UB because of ABI mismatch. |
| 77 | +pub(super) fn check_call_site_abi<'tcx>( |
| 78 | + tcx: TyCtxt<'tcx>, |
| 79 | + ty: Ty<'tcx>, |
| 80 | + span: Span, |
| 81 | + caller: InstanceKind<'tcx>, |
| 82 | +) { |
| 83 | + let param_env = ParamEnv::reveal_all(); |
| 84 | + let callee_abi = match *ty.kind() { |
| 85 | + ty::FnPtr(..) => tcx.fn_abi_of_fn_ptr(param_env.and((ty.fn_sig(tcx), ty::List::empty()))), |
| 86 | + ty::FnDef(def_id, args) => { |
| 87 | + // Intrinsics are handled separately by the compiler. |
| 88 | + if tcx.intrinsic(def_id).is_some() { |
| 89 | + return; |
| 90 | + } |
| 91 | + let instance = ty::Instance::expect_resolve(tcx, param_env, def_id, args, span); |
| 92 | + tcx.fn_abi_of_instance(param_env.and((instance, ty::List::empty()))) |
| 93 | + } |
| 94 | + _ => { |
| 95 | + panic!("Invalid function call"); |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + let Ok(callee_abi) = callee_abi else { |
| 100 | + // ABI failed to compute; this will not get through codegen. |
| 101 | + return; |
| 102 | + }; |
| 103 | + do_check_abi(tcx, callee_abi, caller.def_id(), |required_feature| { |
| 104 | + tcx.emit_node_span_lint( |
| 105 | + ABI_UNSUPPORTED_VECTOR_TYPES, |
| 106 | + CRATE_HIR_ID, |
| 107 | + span, |
| 108 | + AbiErrorDisabledVectorTypeCall { span, required_feature }, |
| 109 | + ); |
| 110 | + }) |
| 111 | +} |
0 commit comments