Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions compiler/rustc_mir_transform/src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,10 @@ impl SimplifyCfg {

pub(super) fn simplify_cfg<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if CfgSimplifier::new(tcx, body).simplify() {
// `simplify` returns that it changed something. We must invalidate the CFG caches as they
// are not consistent with the modified CFG any more.
body.basic_blocks.invalidate_cfg_cache();
}
remove_dead_blocks(body);

// FIXME: Should probably be moved into some kind of pass manager
remove_runtime_checks_assumes(body);
body.basic_blocks.as_mut_preserves_cfg().shrink_to_fit();
}

Expand Down Expand Up @@ -334,6 +331,20 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
}
}

fn remove_runtime_checks_assumes<'tcx>(body: &mut Body<'tcx>) {
for block in body.basic_blocks.as_mut_preserves_cfg() {
for statement in &mut block.statements {
if let StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(operand)) =
&statement.kind
{
if matches!(operand, Operand::RuntimeChecks(_)) {
statement.make_nop(true);
}
}
}
}
}

pub(super) fn simplify_duplicate_switch_targets(terminator: &mut Terminator<'_>) {
if let TerminatorKind::SwitchInt { targets, .. } = &mut terminator.kind {
let otherwise = targets.otherwise();
Expand Down
18 changes: 18 additions & 0 deletions tests/mir-opt/ub_checks_assume_removals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ test-mir-pass: SimplifyCfg-final
//@ compile-flags: -Zmir-opt-level=1

#![crate_type = "lib"]
#![feature(core_intrinsics)]

// This test ensures that `assume(RuntimeChecks)`
// is not emitted in MIR after SimplifyCfg.

// EMIT_MIR_FOR_EACH ub_checks_assume_removals.test SimplifyCfg-final
pub unsafe fn test(ptr: *const u8) -> u8 {
ptr.read()
}

// EMIT_MIR_FOR_EACH ub_checks_assume_removals.with_unchecked SimplifyCfg-final
pub unsafe fn with_unchecked(x: i32, y: i32) -> i32 {
x.unchecked_add(y)
}
Loading