Skip to content

Commit 52fe5a1

Browse files
authored
Rollup merge of #105835 - tmiasko:cleanup-post-borrowck, r=JakobDegen
Refactor post borrowck cleanup passes
2 parents 8a746f4 + acaa2c1 commit 52fe5a1

File tree

5 files changed

+35
-80
lines changed

5 files changed

+35
-80
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,48 @@
1-
//! This module provides a pass to replacing the following statements with
2-
//! [`Nop`]s
1+
//! This module provides a pass that removes parts of MIR that are no longer relevant after
2+
//! analysis phase and borrowck. In particular, it removes false edges, user type annotations and
3+
//! replaces following statements with [`Nop`]s:
34
//!
45
//! - [`AscribeUserType`]
56
//! - [`FakeRead`]
67
//! - [`Assign`] statements with a [`Shallow`] borrow
78
//!
8-
//! The `CleanFakeReadsAndBorrows` "pass" is actually implemented as two
9-
//! traversals (aka visits) of the input MIR. The first traversal,
10-
//! `DeleteAndRecordFakeReads`, deletes the fake reads and finds the
11-
//! temporaries read by [`ForMatchGuard`] reads, and `DeleteFakeBorrows`
12-
//! deletes the initialization of those temporaries.
13-
//!
149
//! [`AscribeUserType`]: rustc_middle::mir::StatementKind::AscribeUserType
15-
//! [`Shallow`]: rustc_middle::mir::BorrowKind::Shallow
16-
//! [`FakeRead`]: rustc_middle::mir::StatementKind::FakeRead
1710
//! [`Assign`]: rustc_middle::mir::StatementKind::Assign
18-
//! [`ForMatchGuard`]: rustc_middle::mir::FakeReadCause::ForMatchGuard
11+
//! [`FakeRead`]: rustc_middle::mir::StatementKind::FakeRead
1912
//! [`Nop`]: rustc_middle::mir::StatementKind::Nop
13+
//! [`Shallow`]: rustc_middle::mir::BorrowKind::Shallow
2014
2115
use crate::MirPass;
22-
use rustc_middle::mir::visit::MutVisitor;
23-
use rustc_middle::mir::{Body, BorrowKind, Location, Rvalue};
24-
use rustc_middle::mir::{Statement, StatementKind};
16+
use rustc_middle::mir::{Body, BorrowKind, Rvalue, StatementKind, TerminatorKind};
2517
use rustc_middle::ty::TyCtxt;
2618

27-
pub struct CleanupNonCodegenStatements;
19+
pub struct CleanupPostBorrowck;
2820

29-
pub struct DeleteNonCodegenStatements<'tcx> {
30-
tcx: TyCtxt<'tcx>,
31-
}
21+
impl<'tcx> MirPass<'tcx> for CleanupPostBorrowck {
22+
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
23+
for basic_block in body.basic_blocks.as_mut() {
24+
for statement in basic_block.statements.iter_mut() {
25+
match statement.kind {
26+
StatementKind::AscribeUserType(..)
27+
| StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Shallow, _)))
28+
| StatementKind::FakeRead(..) => statement.make_nop(),
29+
_ => (),
30+
}
31+
}
32+
let terminator = basic_block.terminator_mut();
33+
match terminator.kind {
34+
TerminatorKind::FalseEdge { real_target, .. }
35+
| TerminatorKind::FalseUnwind { real_target, .. } => {
36+
terminator.kind = TerminatorKind::Goto { target: real_target };
37+
}
38+
_ => {}
39+
}
40+
}
3241

33-
impl<'tcx> MirPass<'tcx> for CleanupNonCodegenStatements {
34-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
35-
let mut delete = DeleteNonCodegenStatements { tcx };
36-
delete.visit_body_preserves_cfg(body);
3742
body.user_type_annotations.raw.clear();
3843

3944
for decl in &mut body.local_decls {
4045
decl.user_ty = None;
4146
}
4247
}
4348
}
44-
45-
impl<'tcx> MutVisitor<'tcx> for DeleteNonCodegenStatements<'tcx> {
46-
fn tcx(&self) -> TyCtxt<'tcx> {
47-
self.tcx
48-
}
49-
50-
fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
51-
match statement.kind {
52-
StatementKind::AscribeUserType(..)
53-
| StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Shallow, _)))
54-
| StatementKind::FakeRead(..) => statement.make_nop(),
55-
_ => (),
56-
}
57-
self.super_statement(statement, location);
58-
}
59-
}

compiler/rustc_mir_transform/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ mod match_branches;
7777
mod multiple_return_terminators;
7878
mod normalize_array_len;
7979
mod nrvo;
80-
// This pass is public to allow external drivers to perform MIR cleanup
81-
pub mod remove_false_edges;
8280
mod remove_noop_landing_pads;
8381
mod remove_storage_markers;
8482
mod remove_uninit_drops;
@@ -494,10 +492,9 @@ fn run_analysis_to_runtime_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>
494492
/// After this series of passes, no lifetime analysis based on borrowing can be done.
495493
fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
496494
let passes: &[&dyn MirPass<'tcx>] = &[
497-
&remove_false_edges::RemoveFalseEdges,
495+
&cleanup_post_borrowck::CleanupPostBorrowck,
498496
&simplify_branches::SimplifyConstCondition::new("initial"),
499497
&remove_noop_landing_pads::RemoveNoopLandingPads,
500-
&cleanup_post_borrowck::CleanupNonCodegenStatements,
501498
&simplify::SimplifyCfg::new("early-opt"),
502499
&deref_separator::Derefer,
503500
];

compiler/rustc_mir_transform/src/remove_false_edges.rs

-29
This file was deleted.

src/test/mir-opt/remove_fake_borrows.match_guard.CleanupNonCodegenStatements.diff src/test/mir-opt/remove_fake_borrows.match_guard.CleanupPostBorrowck.diff

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `match_guard` before CleanupNonCodegenStatements
2-
+ // MIR for `match_guard` after CleanupNonCodegenStatements
1+
- // MIR for `match_guard` before CleanupPostBorrowck
2+
+ // MIR for `match_guard` after CleanupPostBorrowck
33

44
fn match_guard(_1: Option<&&i32>, _2: bool) -> i32 {
55
debug x => _1; // in scope 0 at $DIR/remove_fake_borrows.rs:+0:16: +0:17
@@ -29,7 +29,8 @@
2929
}
3030

3131
bb3: {
32-
goto -> bb4; // scope 0 at $DIR/remove_fake_borrows.rs:+2:9: +2:16
32+
- falseEdge -> [real: bb4, imaginary: bb1]; // scope 0 at $DIR/remove_fake_borrows.rs:+2:9: +2:16
33+
+ goto -> bb4; // scope 0 at $DIR/remove_fake_borrows.rs:+2:9: +2:16
3334
}
3435

3536
bb4: {
@@ -62,15 +63,12 @@
6263

6364
bb6: {
6465
StorageDead(_8); // scope 0 at $DIR/remove_fake_borrows.rs:+2:20: +2:21
65-
goto -> bb1; // scope 0 at $DIR/remove_fake_borrows.rs:+2:20: +2:21
66+
- falseEdge -> [real: bb1, imaginary: bb1]; // scope 0 at $DIR/remove_fake_borrows.rs:+2:20: +2:21
67+
+ goto -> bb1; // scope 0 at $DIR/remove_fake_borrows.rs:+2:20: +2:21
6668
}
6769

6870
bb7: {
6971
return; // scope 0 at $DIR/remove_fake_borrows.rs:+5:2: +5:2
7072
}
71-
72-
bb8 (cleanup): {
73-
resume; // scope 0 at $DIR/remove_fake_borrows.rs:+0:1: +5:2
74-
}
7573
}
7674

src/test/mir-opt/remove_fake_borrows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// ignore-wasm32-bare compiled with panic=abort by default
44

5-
// EMIT_MIR remove_fake_borrows.match_guard.CleanupNonCodegenStatements.diff
5+
// EMIT_MIR remove_fake_borrows.match_guard.CleanupPostBorrowck.diff
66
fn match_guard(x: Option<&&i32>, c: bool) -> i32 {
77
match x {
88
Some(0) if c => 0,

0 commit comments

Comments
 (0)