Skip to content

Commit 5510b38

Browse files
Rollup merge of #165863 - nnethercote:avoid-instantiating-FnSig, r=bjorn3
Avoid instantiating `FnSig` when not necessary In various places we call `fn_sig`, which is expensive because it instantiates the signature, but then only look at the ABI or the safety, which don't require the instantiation. This commit introduces `fn_sig_abi` and `fn_sig_safety` which are alternatives that skip instantiation. In `check_instance_abi` the commit also changes `instance.ty(...)` to `tcx.type_of(...)` to avoid a second unnecessary instantiation path. r? @bjorn3
2 parents e2c96e4 + 5465832 commit 5510b38

9 files changed

Lines changed: 62 additions & 26 deletions

File tree

‎compiler/rustc_middle/src/ty/sty.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::debug_assert_matches;
77
use std::ops::{ControlFlow, Range};
88

99
use hir::def::{CtorKind, DefKind};
10-
use rustc_abi::{FIRST_VARIANT, FieldIdx, NumScalableVectors, ScalableElt, VariantIdx};
10+
use rustc_abi::{ExternAbi, FIRST_VARIANT, FieldIdx, NumScalableVectors, ScalableElt, VariantIdx};
1111
use rustc_attr_ir::lang_items::LangItem;
1212
use rustc_errors::{ErrorGuaranteed, MultiSpan};
1313
use rustc_hir as hir;
@@ -1600,6 +1600,20 @@ impl<'tcx> Ty<'tcx> {
16001600
self.kind().fn_sig(tcx)
16011601
}
16021602

1603+
/// `ty.fn_sig_abi(tcx)` is much faster than `ty.fn_sig(tcx).abi()` because the latter requires
1604+
/// instantiating the signature.
1605+
#[tracing::instrument(level = "trace", skip(tcx))]
1606+
pub fn fn_sig_abi(self, tcx: TyCtxt<'tcx>) -> ExternAbi {
1607+
self.kind().fn_sig_abi(tcx)
1608+
}
1609+
1610+
/// `ty.fn_sig_safety(tcx)` is much faster than `ty.fn_sig(tcx).safety()` because the latter
1611+
/// requires instantiating the signature.
1612+
#[tracing::instrument(level = "trace", skip(tcx))]
1613+
pub fn fn_sig_safety(self, tcx: TyCtxt<'tcx>) -> hir::Safety {
1614+
self.kind().fn_sig_safety(tcx)
1615+
}
1616+
16031617
#[tracing::instrument(level = "trace", skip(tcx))]
16041618
pub fn unnormalized_fn_sig(self, tcx: TyCtxt<'tcx>) -> ty::Unnormalized<'tcx, PolyFnSig<'tcx>> {
16051619
self.kind().unnormalized_fn_sig(tcx)

‎compiler/rustc_mir_build/src/check_unsafety.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,15 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
411411
}
412412
ExprKind::Call { fun, ty: _, args: _, from_hir_call: _, fn_span: _ } => {
413413
let fn_ty = self.thir[fun].ty;
414-
let sig = fn_ty.fn_sig(self.tcx);
414+
let safety = fn_ty.fn_sig_safety(self.tcx);
415415
let (callee_features, safe_target_features): (&[_], _) = match *fn_ty.kind() {
416416
ty::FnDef(func_id, ..) => {
417417
let cg_attrs = self.tcx.codegen_fn_attrs(func_id);
418418
(&cg_attrs.target_features, cg_attrs.safe_target_features)
419419
}
420420
_ => (&[], false),
421421
};
422-
if sig.safety().is_unsafe() && !safe_target_features {
422+
if safety.is_unsafe() && !safe_target_features {
423423
let func_id = if let ty::FnDef(func_id, _) = fn_ty.kind() {
424424
Some(*func_id)
425425
} else {

‎compiler/rustc_mir_transform/src/abort_unwinding_calls.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
5252
// unwinding or not.
5353
let body_ty = tcx.type_of(def_id).skip_binder();
5454
let body_abi = match body_ty.kind() {
55-
ty::FnDef(..) => body_ty.fn_sig(tcx).abi(),
55+
ty::FnDef(..) => body_ty.fn_sig_abi(tcx),
5656
ty::Closure(..) => ExternAbi::RustCall,
5757
ty::CoroutineClosure(..) => ExternAbi::RustCall,
5858
ty::Coroutine(..) => ExternAbi::Rust,
@@ -90,13 +90,13 @@ impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
9090
let call_can_unwind = match &terminator.kind {
9191
TerminatorKind::Call { func, .. } => {
9292
let ty = func.ty(&body.local_decls, tcx);
93-
let sig = ty.fn_sig(tcx);
93+
let abi = ty.fn_sig_abi(tcx);
9494
let fn_def_id = match ty.kind() {
9595
ty::FnPtr(..) => None,
9696
&ty::FnDef(def_id, _) => Some(def_id),
9797
_ => span_bug!(span, "invalid callee of type {:?}", ty),
9898
};
99-
layout::fn_can_unwind(tcx, fn_def_id, sig.abi())
99+
layout::fn_can_unwind(tcx, fn_def_id, abi)
100100
}
101101
TerminatorKind::Drop { .. } => {
102102
tcx.sess.opts.unstable_opts.panic_in_drop == PanicStrategy::Unwind

‎compiler/rustc_mir_transform/src/ffi_unwind_calls.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn has_ffi_unwind_calls(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> bool {
2626

2727
let body_ty = tcx.type_of(def_id).skip_binder();
2828
let body_abi = match body_ty.kind() {
29-
ty::FnDef(..) => body_ty.fn_sig(tcx).abi(),
29+
ty::FnDef(..) => body_ty.fn_sig_abi(tcx),
3030
ty::Closure(..) => ExternAbi::RustCall,
3131
ty::CoroutineClosure(..) => ExternAbi::RustCall,
3232
ty::Coroutine(..) => ExternAbi::Rust,
@@ -78,11 +78,11 @@ fn has_ffi_unwind_calls(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> bool {
7878
let TerminatorKind::Call { func, .. } = &terminator.kind else { continue };
7979

8080
let ty = func.ty(body, tcx);
81-
let sig = ty.fn_sig(tcx);
81+
let abi = ty.fn_sig_abi(tcx);
8282

8383
// Rust calls cannot themselves create foreign unwinds.
8484
// We assume this is true for intrinsics as well.
85-
if sig.abi().is_rustic_abi() {
85+
if abi.is_rustic_abi() {
8686
continue;
8787
};
8888

@@ -100,7 +100,7 @@ fn has_ffi_unwind_calls(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> bool {
100100
_ => bug!("invalid callee of type {:?}", ty),
101101
};
102102

103-
if layout::fn_can_unwind(tcx, fn_def_id, sig.abi()) {
103+
if layout::fn_can_unwind(tcx, fn_def_id, abi) {
104104
// We have detected a call that can possibly leak foreign unwind.
105105
//
106106
// Because the function body itself can unwind, we are not aborting this function call

‎compiler/rustc_mir_transform/src/instsimplify.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
385385

386386
let body_ty = self.tcx.type_of(def_id).skip_binder();
387387
let body_abi = match body_ty.kind() {
388-
ty::FnDef(..) => body_ty.fn_sig(self.tcx).abi(),
388+
ty::FnDef(..) => body_ty.fn_sig_abi(self.tcx),
389389
ty::Closure(..) => ExternAbi::RustCall,
390390
ty::Coroutine(..) => ExternAbi::Rust,
391391
_ => bug!("unexpected body ty: {body_ty:?}"),

‎compiler/rustc_mir_transform/src/validate.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'tcx> crate::MirPass<'tcx> for Validator {
5252
} else {
5353
let body_ty = tcx.type_of(def_id).skip_binder();
5454
let body_abi = match body_ty.kind() {
55-
ty::FnDef(..) => body_ty.fn_sig(tcx).abi(),
55+
ty::FnDef(..) => body_ty.fn_sig_abi(tcx),
5656
ty::Closure(..) => ExternAbi::RustCall,
5757
ty::CoroutineClosure(..) => ExternAbi::RustCall,
5858
ty::Coroutine(..) => ExternAbi::Rust,

‎compiler/rustc_monomorphize/src/mono_checks/abi_check.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ fn do_check_unsized_params<'tcx>(
170170
/// - the signature requires target features that are not enabled
171171
fn check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
172172
let typing_env = ty::TypingEnv::fully_monomorphized();
173-
let ty = instance.ty(tcx, typing_env);
174-
if ty.is_fn() && ty.fn_sig(tcx).abi() == ExternAbi::LlvmIntrinsic {
173+
let ty_no_binder = tcx.type_of(instance.def.def_id()).skip_binder();
174+
if ty_no_binder.is_fn() && ty_no_binder.fn_sig_abi(tcx) == ExternAbi::LlvmIntrinsic {
175175
// We disable all checks for the llvm-intrinsic ABI to allow linking to arbitrary
176176
// LLVM intrinsics
177177
return;
@@ -213,7 +213,7 @@ fn check_call_site_abi<'tcx>(
213213
caller: Instance<'tcx>,
214214
loc: impl Fn() -> (Span, HirId) + Copy,
215215
) {
216-
let extern_abi = callee.fn_sig(tcx).abi();
216+
let extern_abi = callee.fn_sig_abi(tcx);
217217
if extern_abi.is_rustic_abi() {
218218
// We directly handle the soundness of Rust ABIs -- so let's skip the majority of
219219
// call sites to avoid a perf regression.

‎compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5534,9 +5534,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
55345534
&& tcx.is_fn_trait(trait_ref.def_id)
55355535
&& let [self_ty, found_ty] = trait_ref.args.as_slice()
55365536
&& let Some(fn_ty) = self_ty.as_type().filter(|ty| ty.is_fn())
5537-
&& let fn_sig @ ty::FnSig {
5538-
..
5539-
} = fn_ty.fn_sig(tcx).skip_binder()
5537+
&& let fn_sig = fn_ty.fn_sig(tcx).skip_binder()
55405538
// FIXME(splat): this might need to change if the Fn* traits start using/supporting splat
55415539
&& fn_sig.is_fn_trait_compatible()
55425540

‎compiler/rustc_type_ir/src/ty_kind.rs‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,27 +368,51 @@ pub enum TyKind<I: Interner> {
368368
impl<I: Interner> Eq for TyKind<I> {}
369369

370370
impl<I: Interner> TyKind<I> {
371-
pub fn fn_sig(self, interner: I) -> ty::Binder<I, ty::FnSig<I>> {
372-
self.unnormalized_fn_sig(interner).skip_normalization()
373-
}
374-
375371
pub fn unnormalized_fn_sig(self, interner: I) -> Unnormalized<I, ty::Binder<I, ty::FnSig<I>>> {
376372
match self {
377373
ty::FnPtr(sig_tys, hdr) => Unnormalized::new_wip(sig_tys.with(hdr)),
378374
ty::FnDef(def_id, args) => {
379375
interner.fn_sig(def_id).instantiate(interner, args.no_bound_vars().unwrap())
380376
}
381-
ty::Error(_) => {
382-
// ignore errors (#54954)
383-
Unnormalized::dummy(ty::Binder::dummy(ty::FnSig::dummy()))
384-
}
377+
// ignore errors (#54954)
378+
ty::Error(_) => Unnormalized::dummy(ty::Binder::dummy(ty::FnSig::dummy())),
385379
ty::Closure(..) => panic!(
386380
"to get the signature of a closure, use `args.as_closure().sig()` not `fn_sig()`",
387381
),
388382
_ => panic!("Ty::fn_sig() called on non-fn type: {:?}", self),
389383
}
390384
}
391385

386+
pub fn fn_sig(self, interner: I) -> ty::Binder<I, ty::FnSig<I>> {
387+
self.unnormalized_fn_sig(interner).skip_normalization()
388+
}
389+
390+
pub fn fn_sig_abi(self, interner: I) -> ExternAbi {
391+
match self {
392+
ty::FnPtr(_, hdr) => hdr.abi(),
393+
ty::FnDef(def_id, _) => interner.fn_sig(def_id).skip_binder().abi(),
394+
// ignore errors (#54954)
395+
ty::Error(_) => FnSig::<I>::dummy().abi(),
396+
ty::Closure(..) => panic!(
397+
"to get the ABI of a closure, use `args.as_closure().sig().abi()` not `fn_sig_abi()`",
398+
),
399+
_ => panic!("`fn_sig_abi` called on non-fn type: {:?}", self),
400+
}
401+
}
402+
403+
pub fn fn_sig_safety(self, interner: I) -> I::Safety {
404+
match self {
405+
ty::FnPtr(_, hdr) => hdr.safety(),
406+
ty::FnDef(def_id, _) => interner.fn_sig(def_id).skip_binder().safety(),
407+
// ignore errors (#54954)
408+
ty::Error(_) => FnSig::<I>::dummy().safety(),
409+
ty::Closure(..) => panic!(
410+
"to get the safety of a closure, use `args.as_closure().sig().safety()` not `fn_sig_safety()`",
411+
),
412+
_ => panic!("`fn_sig_safety` called on non-fn type: {:?}", self),
413+
}
414+
}
415+
392416
/// Returns `true` when the outermost type cannot be further normalized,
393417
/// resolved, or instantiated.
394418
///

0 commit comments

Comments
 (0)