|
| 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_hir::def::DefKind; |
| 5 | +use rustc_middle::query::Providers; |
| 6 | +use rustc_middle::ty::inherent::*; |
| 7 | +use rustc_middle::ty::{self, Instance, InstanceKind, ParamEnv, Ty, TyCtxt}; |
| 8 | +use rustc_session::lint::builtin::ABI_UNSUPPORTED_VECTOR_TYPES; |
| 9 | +use rustc_span::def_id::DefId; |
| 10 | +use rustc_span::{DUMMY_SP, Span, Symbol}; |
| 11 | +use rustc_target::abi::call::{FnAbi, PassMode}; |
| 12 | +use rustc_target::abi::{Abi as PassAbi, RegKind}; |
| 13 | + |
| 14 | +use crate::errors::{AbiErrorDisabledVectorTypeCall, AbiErrorDisabledVectorTypeDef}; |
| 15 | + |
| 16 | +fn uses_vector_registers(mode: &PassMode, abi: &PassAbi) -> 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!(abi, PassAbi::Vector { .. }), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn do_check_abi<'tcx>( |
| 28 | + tcx: TyCtxt<'tcx>, |
| 29 | + abi: &FnAbi<'tcx, Ty<'tcx>>, |
| 30 | + target_feature_def: DefId, |
| 31 | + mut emit_err: impl FnMut(&'static str), |
| 32 | +) { |
| 33 | + let Some(feature_def) = tcx.sess.target.features_for_correct_vector_abi() else { |
| 34 | + return; |
| 35 | + }; |
| 36 | + let codegen_attrs = tcx.codegen_fn_attrs(target_feature_def); |
| 37 | + for arg_abi in abi.args.iter().chain(std::iter::once(&abi.ret)) { |
| 38 | + let size = arg_abi.layout.size; |
| 39 | + if uses_vector_registers(&arg_abi.mode, &arg_abi.layout.abi) { |
| 40 | + // Find the first feature that provides at least this vector size. |
| 41 | + let feature = match feature_def.iter().find(|(bits, _)| size.bits() <= *bits) { |
| 42 | + Some((_, feature)) => feature, |
| 43 | + None => { |
| 44 | + emit_err("<no available feature for this size>"); |
| 45 | + continue; |
| 46 | + } |
| 47 | + }; |
| 48 | + let feature_sym = Symbol::intern(feature); |
| 49 | + if !tcx.sess.unstable_target_features.contains(&feature_sym) |
| 50 | + && !codegen_attrs.target_features.iter().any(|x| x.name == feature_sym) |
| 51 | + { |
| 52 | + emit_err(feature); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Checks that the ABI of a given instance of a function does not contain vector-passed arguments |
| 59 | +/// or return values for which the corresponding target feature is not enabled. |
| 60 | +fn check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) { |
| 61 | + let param_env = ParamEnv::reveal_all(); |
| 62 | + let Ok(abi) = tcx.fn_abi_of_instance(param_env.and((instance, ty::List::empty()))) else { |
| 63 | + // An error will be reported during codegen if we cannot determine the ABI of this |
| 64 | + // function. |
| 65 | + return; |
| 66 | + }; |
| 67 | + do_check_abi(tcx, abi, instance.def_id(), |required_feature| { |
| 68 | + let span = tcx.def_span(instance.def_id()); |
| 69 | + tcx.emit_node_span_lint( |
| 70 | + ABI_UNSUPPORTED_VECTOR_TYPES, |
| 71 | + CRATE_HIR_ID, |
| 72 | + span, |
| 73 | + AbiErrorDisabledVectorTypeDef { span, required_feature }, |
| 74 | + ); |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +fn call_site_abi_missing_features<'tcx>( |
| 79 | + tcx: TyCtxt<'tcx>, |
| 80 | + (callee, caller): (Ty<'tcx>, DefId), |
| 81 | +) -> Vec<String> { |
| 82 | + let mut answer = vec![]; |
| 83 | + let param_env = ParamEnv::reveal_all(); |
| 84 | + let callee_abi = match *callee.kind() { |
| 85 | + ty::FnPtr(..) => { |
| 86 | + tcx.fn_abi_of_fn_ptr(param_env.and((callee.fn_sig(tcx), ty::List::empty()))) |
| 87 | + } |
| 88 | + ty::FnDef(def_id, args) => { |
| 89 | + // Intrinsics are handled separately by the compiler. |
| 90 | + if tcx.intrinsic(def_id).is_some() { |
| 91 | + return vec![]; |
| 92 | + } |
| 93 | + let instance = ty::Instance::expect_resolve(tcx, param_env, def_id, args, DUMMY_SP); |
| 94 | + tcx.fn_abi_of_instance(param_env.and((instance, ty::List::empty()))) |
| 95 | + } |
| 96 | + _ => { |
| 97 | + panic!("Invalid function call"); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + let Ok(callee_abi) = callee_abi else { |
| 102 | + // ABI failed to compute; this will not get through codegen. |
| 103 | + return vec![]; |
| 104 | + }; |
| 105 | + do_check_abi(tcx, callee_abi, caller, |required_feature| { |
| 106 | + answer.push(required_feature.to_string()); |
| 107 | + }); |
| 108 | + answer |
| 109 | +} |
| 110 | + |
| 111 | +/// Checks that a call expression does not try to pass a vector-passed argument which requires a |
| 112 | +/// target feature that the caller does not have, as doing so causes UB because of ABI mismatch. |
| 113 | +pub(super) fn check_call_site_abi<'tcx>( |
| 114 | + tcx: TyCtxt<'tcx>, |
| 115 | + callee: Ty<'tcx>, |
| 116 | + span: Span, |
| 117 | + caller: InstanceKind<'tcx>, |
| 118 | +) { |
| 119 | + if callee.fn_sig(tcx).abi().is_rust() { |
| 120 | + // "Rust" ABI never passes arguments in vector registers. |
| 121 | + return; |
| 122 | + } |
| 123 | + for required_feature in tcx.call_site_abi_missing_features((callee, caller.def_id())) { |
| 124 | + tcx.emit_node_span_lint( |
| 125 | + ABI_UNSUPPORTED_VECTOR_TYPES, |
| 126 | + CRATE_HIR_ID, |
| 127 | + span, |
| 128 | + AbiErrorDisabledVectorTypeCall { span, required_feature }, |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +pub(super) fn provide(providers: &mut Providers) { |
| 134 | + *providers = Providers { check_instance_abi, call_site_abi_missing_features, ..*providers } |
| 135 | +} |
| 136 | + |
| 137 | +pub(super) fn should_check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool { |
| 138 | + // We do not need to check the instance for feature-dependent ABI if we can determine that the |
| 139 | + // function has "Rust" ABI, which is known not to be feature-dependent. |
| 140 | + // Note that the check is still correct on Rust ABI functions, but somewhat expensive. Hence, |
| 141 | + // checking for "Rust" ABI is just an optimization. |
| 142 | + // We also avoid to try to determine the type of the instance, as doing so involves running a |
| 143 | + // query that does not usually run for unchanged functions in incremental builds. |
| 144 | + if let InstanceKind::Item(def) | InstanceKind::ReifyShim(def, ..) = instance.def { |
| 145 | + // fn_sig ICEs on defs that are not functions. |
| 146 | + if matches!(tcx.def_kind(def), DefKind::Fn | DefKind::AssocFn) { |
| 147 | + !tcx.fn_sig(def).skip_binder().abi().is_rust() |
| 148 | + } else if matches!(tcx.def_kind(def), DefKind::Ctor(..)) { |
| 149 | + // Struct constructors are certainly always fine. |
| 150 | + false |
| 151 | + } else { |
| 152 | + true |
| 153 | + } |
| 154 | + } else if matches!(instance.def, InstanceKind::DropGlue(..) | InstanceKind::CloneShim(..)) { |
| 155 | + // Drop glue and clone shims are always fine. |
| 156 | + false |
| 157 | + } else { |
| 158 | + true |
| 159 | + } |
| 160 | +} |
0 commit comments