From 66952eb1f4470dbe753a3d2ff38d7fddc89e2c0a Mon Sep 17 00:00:00 2001 From: SATVIKsynopsis Date: Tue, 23 Dec 2025 01:13:00 +0530 Subject: [PATCH 1/2] removed redundant assume(runtimechecks) after simplifyCfg --- compiler/rustc_mir_transform/src/simplify.rs | 19 ++++++++++++++---- tests/mir-opt/ub_checks_assume_removals.rs | 21 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 tests/mir-opt/ub_checks_assume_removals.rs diff --git a/compiler/rustc_mir_transform/src/simplify.rs b/compiler/rustc_mir_transform/src/simplify.rs index da31600e8324c..2a8e0c7d22cbf 100644 --- a/compiler/rustc_mir_transform/src/simplify.rs +++ b/compiler/rustc_mir_transform/src/simplify.rs @@ -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(); } @@ -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(); diff --git a/tests/mir-opt/ub_checks_assume_removals.rs b/tests/mir-opt/ub_checks_assume_removals.rs new file mode 100644 index 0000000000000..eef93e9b7404d --- /dev/null +++ b/tests/mir-opt/ub_checks_assume_removals.rs @@ -0,0 +1,21 @@ +//@ 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. + +// CHECK-NOT: assume(RuntimeChecks) +// CHECK-NOT: RuntimeChecks + +// 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) +} From 059ad7a9ff7b21cb659b9522f785ef46ac87dc9c Mon Sep 17 00:00:00 2001 From: SATVIKsynopsis Date: Tue, 23 Dec 2025 01:27:49 +0530 Subject: [PATCH 2/2] small fix --- tests/mir-opt/ub_checks_assume_removals.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/mir-opt/ub_checks_assume_removals.rs b/tests/mir-opt/ub_checks_assume_removals.rs index eef93e9b7404d..5bfc8a582ddcc 100644 --- a/tests/mir-opt/ub_checks_assume_removals.rs +++ b/tests/mir-opt/ub_checks_assume_removals.rs @@ -7,9 +7,6 @@ // This test ensures that `assume(RuntimeChecks)` // is not emitted in MIR after SimplifyCfg. -// CHECK-NOT: assume(RuntimeChecks) -// CHECK-NOT: RuntimeChecks - // EMIT_MIR_FOR_EACH ub_checks_assume_removals.test SimplifyCfg-final pub unsafe fn test(ptr: *const u8) -> u8 { ptr.read()