Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit acc7f0d

Browse files
committedApr 20, 2024·
MIR: Stop needing an explicit BB for otherwise:unreachable
So many places to update :D For this PR I tried to keep things doing essentially the same thing as before. No new passes to try to use it more, no change to MIR building for exhaustive matches, etc. That said, `UnreachableProp` still picks it up, and thus there's still a bunch of `otherwise` arms removed and `unreachable` blocks that no longer show.
1 parent c8d19a9 commit acc7f0d

File tree

64 files changed

+382
-337
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+382
-337
lines changed
 

‎compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub(crate) fn codegen_fn<'tcx>(
9595
bcx,
9696
block_map,
9797
local_map: IndexVec::with_capacity(mir.local_decls.len()),
98+
shared_unreachable: None,
9899
caller_location: None, // set by `codegen_fn_prelude`
99100

100101
clif_comments,
@@ -404,7 +405,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
404405
assert_eq!(targets.iter().count(), 1);
405406
let (then_value, then_block) = targets.iter().next().unwrap();
406407
let then_block = fx.get_block(then_block);
407-
let else_block = fx.get_block(targets.otherwise());
408+
let else_block = fx.get_switch_block(targets.otherwise());
408409
let test_zero = match then_value {
409410
0 => true,
410411
1 => false,
@@ -435,7 +436,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
435436
let block = fx.get_block(block);
436437
switch.set_entry(value, block);
437438
}
438-
let otherwise_block = fx.get_block(targets.otherwise());
439+
let otherwise_block = fx.get_switch_block(targets.otherwise());
439440
switch.emit(&mut fx.bcx, discr, otherwise_block);
440441
}
441442
}
@@ -520,6 +521,11 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
520521
}
521522
};
522523
}
524+
525+
if let Some(unreachable) = fx.shared_unreachable {
526+
fx.bcx.switch_to_block(unreachable);
527+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
528+
}
523529
}
524530

525531
fn codegen_stmt<'tcx>(

‎compiler/rustc_codegen_cranelift/src/common.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
296296
pub(crate) bcx: FunctionBuilder<'clif>,
297297
pub(crate) block_map: IndexVec<BasicBlock, Block>,
298298
pub(crate) local_map: IndexVec<Local, CPlace<'tcx>>,
299+
pub(crate) shared_unreachable: Option<Block>,
299300

300301
/// When `#[track_caller]` is used, the implicit caller location is stored in this variable.
301302
pub(crate) caller_location: Option<CValue<'tcx>>,
@@ -377,6 +378,17 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
377378
*self.block_map.get(bb).unwrap()
378379
}
379380

381+
pub(crate) fn get_switch_block(&mut self, sa: SwitchAction) -> Block {
382+
match sa {
383+
SwitchAction::Goto(bb) => self.get_block(bb),
384+
SwitchAction::Unreachable => self.unreachable_block(),
385+
}
386+
}
387+
388+
pub(crate) fn unreachable_block(&mut self) -> Block {
389+
*self.shared_unreachable.get_or_insert_with(|| self.bcx.create_block())
390+
}
391+
380392
pub(crate) fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
381393
*self.local_map.get(local).unwrap_or_else(|| {
382394
panic!("Local {:?} doesn't exist", local);

0 commit comments

Comments
 (0)
Please sign in to comment.