From 4c53783f3c27ce30119018c752bdc805222409da Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 21 Aug 2023 09:57:10 +0200 Subject: when terminating during unwinding, show the reason why --- compiler/rustc_middle/src/mir/patch.rs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'compiler/rustc_middle/src/mir/patch.rs') diff --git a/compiler/rustc_middle/src/mir/patch.rs b/compiler/rustc_middle/src/mir/patch.rs index cd74a403ff6..419543b8988 100644 --- a/compiler/rustc_middle/src/mir/patch.rs +++ b/compiler/rustc_middle/src/mir/patch.rs @@ -14,7 +14,8 @@ pub struct MirPatch<'tcx> { resume_block: Option, // Only for unreachable in cleanup path. unreachable_cleanup_block: Option, - terminate_block: Option, + // Cached block for UnwindTerminate(InCleanup) + terminate_in_cleanup_block: Option, body_span: Span, next_local: usize, } @@ -29,19 +30,21 @@ impl<'tcx> MirPatch<'tcx> { next_local: body.local_decls.len(), resume_block: None, unreachable_cleanup_block: None, - terminate_block: None, + terminate_in_cleanup_block: None, body_span: body.span, }; for (bb, block) in body.basic_blocks.iter_enumerated() { // Check if we already have a resume block - if let TerminatorKind::UnwindResume = block.terminator().kind && block.statements.is_empty() { + if matches!(block.terminator().kind, TerminatorKind::UnwindResume) + && block.statements.is_empty() + { result.resume_block = Some(bb); continue; } // Check if we already have an unreachable block - if let TerminatorKind::Unreachable = block.terminator().kind + if matches!(block.terminator().kind, TerminatorKind::Unreachable) && block.statements.is_empty() && block.is_cleanup { @@ -50,8 +53,12 @@ impl<'tcx> MirPatch<'tcx> { } // Check if we already have a terminate block - if let TerminatorKind::UnwindTerminate = block.terminator().kind && block.statements.is_empty() { - result.terminate_block = Some(bb); + if matches!( + block.terminator().kind, + TerminatorKind::UnwindTerminate(UnwindTerminateReason::InCleanup) + ) && block.statements.is_empty() + { + result.terminate_in_cleanup_block = Some(bb); continue; } } @@ -93,8 +100,8 @@ impl<'tcx> MirPatch<'tcx> { bb } - pub fn terminate_block(&mut self) -> BasicBlock { - if let Some(bb) = self.terminate_block { + pub fn terminate_block(&mut self, reason: UnwindTerminateReason) -> BasicBlock { + if let Some(bb) = self.terminate_in_cleanup_block && reason == UnwindTerminateReason::InCleanup { return bb; } @@ -102,11 +109,13 @@ impl<'tcx> MirPatch<'tcx> { statements: vec![], terminator: Some(Terminator { source_info: SourceInfo::outermost(self.body_span), - kind: TerminatorKind::UnwindTerminate, + kind: TerminatorKind::UnwindTerminate(reason), }), is_cleanup: true, }); - self.terminate_block = Some(bb); + if reason == UnwindTerminateReason::InCleanup { + self.terminate_in_cleanup_block = Some(bb); + } bb } -- cgit 1.4.1-3-g733a5 From 114fde6ac74ffbaaca4d7241c3ee4e11303ae50d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 22 Aug 2023 13:57:09 +0200 Subject: cache the terminate block with the last reason that we saw --- compiler/rustc_codegen_ssa/src/mir/block.rs | 8 +++----- compiler/rustc_codegen_ssa/src/mir/mod.rs | 7 ++++--- compiler/rustc_middle/src/mir/patch.rs | 22 +++++++++------------- 3 files changed, 16 insertions(+), 21 deletions(-) (limited to 'compiler/rustc_middle/src/mir/patch.rs') diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index 0af587e8de8..6aef1664394 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -1581,8 +1581,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } fn terminate_block(&mut self, reason: UnwindTerminateReason) -> Bx::BasicBlock { - if let Some(bb) = self.terminate_in_cleanup_block && reason == UnwindTerminateReason::InCleanup { - return bb; + if let Some((cached_bb, cached_reason)) = self.terminate_block && reason == cached_reason { + return cached_bb; } let funclet; @@ -1653,9 +1653,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { bx.unreachable(); - if reason == UnwindTerminateReason::InCleanup { - self.terminate_in_cleanup_block = Some(llbb); - } + self.terminate_block = Some((llbb, reason)); llbb } diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs index 7f38c54c36f..37a209cec5e 100644 --- a/compiler/rustc_codegen_ssa/src/mir/mod.rs +++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs @@ -5,6 +5,7 @@ use rustc_index::IndexVec; use rustc_middle::mir; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::mir::traversal; +use rustc_middle::mir::UnwindTerminateReason; use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, TyAndLayout}; use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable, TypeVisitableExt}; use rustc_target::abi::call::{FnAbi, PassMode}; @@ -83,8 +84,8 @@ pub struct FunctionCx<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> { /// Cached unreachable block unreachable_block: Option, - /// Cached terminate upon unwinding (reason: InCleanup) block - terminate_in_cleanup_block: Option, + /// Cached terminate upon unwinding block and its reason + terminate_block: Option<(Bx::BasicBlock, UnwindTerminateReason)>, /// The location where each MIR arg/var/tmp/ret is stored. This is /// usually an `PlaceRef` representing an alloca, but not always: @@ -199,7 +200,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( personality_slot: None, cached_llbbs, unreachable_block: None, - terminate_in_cleanup_block: None, + terminate_block: None, cleanup_kinds, landing_pads: IndexVec::from_elem(None, &mir.basic_blocks), funclets: IndexVec::from_fn_n(|_| None, mir.basic_blocks.len()), diff --git a/compiler/rustc_middle/src/mir/patch.rs b/compiler/rustc_middle/src/mir/patch.rs index 419543b8988..da486c3465a 100644 --- a/compiler/rustc_middle/src/mir/patch.rs +++ b/compiler/rustc_middle/src/mir/patch.rs @@ -14,8 +14,8 @@ pub struct MirPatch<'tcx> { resume_block: Option, // Only for unreachable in cleanup path. unreachable_cleanup_block: Option, - // Cached block for UnwindTerminate(InCleanup) - terminate_in_cleanup_block: Option, + // Cached block for UnwindTerminate (with reason) + terminate_block: Option<(BasicBlock, UnwindTerminateReason)>, body_span: Span, next_local: usize, } @@ -30,7 +30,7 @@ impl<'tcx> MirPatch<'tcx> { next_local: body.local_decls.len(), resume_block: None, unreachable_cleanup_block: None, - terminate_in_cleanup_block: None, + terminate_block: None, body_span: body.span, }; @@ -53,12 +53,10 @@ impl<'tcx> MirPatch<'tcx> { } // Check if we already have a terminate block - if matches!( - block.terminator().kind, - TerminatorKind::UnwindTerminate(UnwindTerminateReason::InCleanup) - ) && block.statements.is_empty() + if let TerminatorKind::UnwindTerminate(reason) = block.terminator().kind + && block.statements.is_empty() { - result.terminate_in_cleanup_block = Some(bb); + result.terminate_block = Some((bb, reason)); continue; } } @@ -101,8 +99,8 @@ impl<'tcx> MirPatch<'tcx> { } pub fn terminate_block(&mut self, reason: UnwindTerminateReason) -> BasicBlock { - if let Some(bb) = self.terminate_in_cleanup_block && reason == UnwindTerminateReason::InCleanup { - return bb; + if let Some((cached_bb, cached_reason)) = self.terminate_block && reason == cached_reason { + return cached_bb; } let bb = self.new_block(BasicBlockData { @@ -113,9 +111,7 @@ impl<'tcx> MirPatch<'tcx> { }), is_cleanup: true, }); - if reason == UnwindTerminateReason::InCleanup { - self.terminate_in_cleanup_block = Some(bb); - } + self.terminate_block = Some((bb, reason)); bb } -- cgit 1.4.1-3-g733a5