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