Skip to content

Manually invalidate caches in SimplifyCfg. #142542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2025
Merged
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: 16 additions & 3 deletions compiler/rustc_mir_transform/src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ impl SimplifyCfg {
}

pub(super) fn simplify_cfg<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
CfgSimplifier::new(tcx, body).simplify();
if CfgSimplifier::new(tcx, body).simplify() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coudl use a comment (even if it's reundant, b/c it's important).

// `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
Expand Down Expand Up @@ -121,19 +125,24 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
// Preserve `SwitchInt` reads on built and analysis MIR, or if `-Zmir-preserve-ub`.
let preserve_switch_reads = matches!(body.phase, MirPhase::Built | MirPhase::Analysis(_))
|| tcx.sess.opts.unstable_opts.mir_preserve_ub;
let basic_blocks = body.basic_blocks_mut();
// Do not clear caches yet. The caller to `simplify` will do it if anything changed.
let basic_blocks = body.basic_blocks.as_mut_preserves_cfg();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use a comment explaining why this is okay


CfgSimplifier { preserve_switch_reads, basic_blocks, pred_count }
}

fn simplify(mut self) {
/// Returns whether we actually simplified anything. In that case, the caller *must* invalidate
/// the CFG caches of the MIR body.
#[must_use]
fn simplify(mut self) -> bool {
self.strip_nops();

// Vec of the blocks that should be merged. We store the indices here, instead of the
// statements itself to avoid moving the (relatively) large statements twice.
// We do not push the statements directly into the target block (`bb`) as that is slower
// due to additional reallocations
let mut merged_blocks = Vec::new();
let mut outer_changed = false;
loop {
let mut changed = false;

Expand Down Expand Up @@ -177,7 +186,11 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
if !changed {
break;
}

outer_changed = true;
}

outer_changed
}

/// This function will return `None` if
Expand Down
Loading