Skip to content

Commit 114fde6

Browse files
committed
cache the terminate block with the last reason that we saw
1 parent ddea3f9 commit 114fde6

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1581,8 +1581,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
15811581
}
15821582

15831583
fn terminate_block(&mut self, reason: UnwindTerminateReason) -> Bx::BasicBlock {
1584-
if let Some(bb) = self.terminate_in_cleanup_block && reason == UnwindTerminateReason::InCleanup {
1585-
return bb;
1584+
if let Some((cached_bb, cached_reason)) = self.terminate_block && reason == cached_reason {
1585+
return cached_bb;
15861586
}
15871587

15881588
let funclet;
@@ -1653,9 +1653,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16531653

16541654
bx.unreachable();
16551655

1656-
if reason == UnwindTerminateReason::InCleanup {
1657-
self.terminate_in_cleanup_block = Some(llbb);
1658-
}
1656+
self.terminate_block = Some((llbb, reason));
16591657
llbb
16601658
}
16611659

compiler/rustc_codegen_ssa/src/mir/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_index::IndexVec;
55
use rustc_middle::mir;
66
use rustc_middle::mir::interpret::ErrorHandled;
77
use rustc_middle::mir::traversal;
8+
use rustc_middle::mir::UnwindTerminateReason;
89
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, TyAndLayout};
910
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable, TypeVisitableExt};
1011
use rustc_target::abi::call::{FnAbi, PassMode};
@@ -83,8 +84,8 @@ pub struct FunctionCx<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
8384
/// Cached unreachable block
8485
unreachable_block: Option<Bx::BasicBlock>,
8586

86-
/// Cached terminate upon unwinding (reason: InCleanup) block
87-
terminate_in_cleanup_block: Option<Bx::BasicBlock>,
87+
/// Cached terminate upon unwinding block and its reason
88+
terminate_block: Option<(Bx::BasicBlock, UnwindTerminateReason)>,
8889

8990
/// The location where each MIR arg/var/tmp/ret is stored. This is
9091
/// usually an `PlaceRef` representing an alloca, but not always:
@@ -199,7 +200,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
199200
personality_slot: None,
200201
cached_llbbs,
201202
unreachable_block: None,
202-
terminate_in_cleanup_block: None,
203+
terminate_block: None,
203204
cleanup_kinds,
204205
landing_pads: IndexVec::from_elem(None, &mir.basic_blocks),
205206
funclets: IndexVec::from_fn_n(|_| None, mir.basic_blocks.len()),

compiler/rustc_middle/src/mir/patch.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub struct MirPatch<'tcx> {
1414
resume_block: Option<BasicBlock>,
1515
// Only for unreachable in cleanup path.
1616
unreachable_cleanup_block: Option<BasicBlock>,
17-
// Cached block for UnwindTerminate(InCleanup)
18-
terminate_in_cleanup_block: Option<BasicBlock>,
17+
// Cached block for UnwindTerminate (with reason)
18+
terminate_block: Option<(BasicBlock, UnwindTerminateReason)>,
1919
body_span: Span,
2020
next_local: usize,
2121
}
@@ -30,7 +30,7 @@ impl<'tcx> MirPatch<'tcx> {
3030
next_local: body.local_decls.len(),
3131
resume_block: None,
3232
unreachable_cleanup_block: None,
33-
terminate_in_cleanup_block: None,
33+
terminate_block: None,
3434
body_span: body.span,
3535
};
3636

@@ -53,12 +53,10 @@ impl<'tcx> MirPatch<'tcx> {
5353
}
5454

5555
// Check if we already have a terminate block
56-
if matches!(
57-
block.terminator().kind,
58-
TerminatorKind::UnwindTerminate(UnwindTerminateReason::InCleanup)
59-
) && block.statements.is_empty()
56+
if let TerminatorKind::UnwindTerminate(reason) = block.terminator().kind
57+
&& block.statements.is_empty()
6058
{
61-
result.terminate_in_cleanup_block = Some(bb);
59+
result.terminate_block = Some((bb, reason));
6260
continue;
6361
}
6462
}
@@ -101,8 +99,8 @@ impl<'tcx> MirPatch<'tcx> {
10199
}
102100

103101
pub fn terminate_block(&mut self, reason: UnwindTerminateReason) -> BasicBlock {
104-
if let Some(bb) = self.terminate_in_cleanup_block && reason == UnwindTerminateReason::InCleanup {
105-
return bb;
102+
if let Some((cached_bb, cached_reason)) = self.terminate_block && reason == cached_reason {
103+
return cached_bb;
106104
}
107105

108106
let bb = self.new_block(BasicBlockData {
@@ -113,9 +111,7 @@ impl<'tcx> MirPatch<'tcx> {
113111
}),
114112
is_cleanup: true,
115113
});
116-
if reason == UnwindTerminateReason::InCleanup {
117-
self.terminate_in_cleanup_block = Some(bb);
118-
}
114+
self.terminate_block = Some((bb, reason));
119115
bb
120116
}
121117

0 commit comments

Comments
 (0)