Skip to content

Commit efa5eaa

Browse files
committed
Avoid invalidating the CFG in MirPatch.
As a part of this change, we adjust MirPatch to not needlessly create unnecessary resume blocks.
1 parent e4417cf commit efa5eaa

File tree

45 files changed

+32
-209
lines changed

Some content is hidden

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

45 files changed

+32
-209
lines changed

compiler/rustc_middle/src/mir/patch.rs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ pub struct MirPatch<'tcx> {
1111
new_blocks: Vec<BasicBlockData<'tcx>>,
1212
new_statements: Vec<(Location, StatementKind<'tcx>)>,
1313
new_locals: Vec<LocalDecl<'tcx>>,
14-
resume_block: BasicBlock,
14+
resume_block: Option<BasicBlock>,
15+
body_span: Span,
1516
next_local: usize,
1617
}
1718

@@ -23,47 +24,36 @@ impl<'tcx> MirPatch<'tcx> {
2324
new_statements: vec![],
2425
new_locals: vec![],
2526
next_local: body.local_decls.len(),
26-
resume_block: START_BLOCK,
27+
resume_block: None,
28+
body_span: body.span,
2729
};
2830

29-
// make sure the MIR we create has a resume block. It is
30-
// completely legal to convert jumps to the resume block
31-
// to jumps to None, but we occasionally have to add
32-
// instructions just before that.
33-
34-
let mut resume_block = None;
35-
let mut resume_stmt_block = None;
31+
// Check if we already have a resume block
3632
for (bb, block) in body.basic_blocks().iter_enumerated() {
37-
if let TerminatorKind::Resume = block.terminator().kind {
38-
if !block.statements.is_empty() {
39-
assert!(resume_stmt_block.is_none());
40-
resume_stmt_block = Some(bb);
41-
} else {
42-
resume_block = Some(bb);
43-
}
33+
if let TerminatorKind::Resume = block.terminator().kind && block.statements.is_empty() {
34+
result.resume_block = Some(bb);
4435
break;
4536
}
4637
}
47-
let resume_block = resume_block.unwrap_or_else(|| {
48-
result.new_block(BasicBlockData {
49-
statements: vec![],
50-
terminator: Some(Terminator {
51-
source_info: SourceInfo::outermost(body.span),
52-
kind: TerminatorKind::Resume,
53-
}),
54-
is_cleanup: true,
55-
})
56-
});
57-
result.resume_block = resume_block;
58-
if let Some(resume_stmt_block) = resume_stmt_block {
59-
result
60-
.patch_terminator(resume_stmt_block, TerminatorKind::Goto { target: resume_block });
61-
}
38+
6239
result
6340
}
6441

65-
pub fn resume_block(&self) -> BasicBlock {
66-
self.resume_block
42+
pub fn resume_block(&mut self) -> BasicBlock {
43+
if let Some(bb) = self.resume_block {
44+
return bb;
45+
}
46+
47+
let bb = self.new_block(BasicBlockData {
48+
statements: vec![],
49+
terminator: Some(Terminator {
50+
source_info: SourceInfo::outermost(self.body_span),
51+
kind: TerminatorKind::Resume,
52+
}),
53+
is_cleanup: true,
54+
});
55+
self.resume_block = Some(bb);
56+
bb
6757
}
6858

6959
pub fn is_patched(&self, bb: BasicBlock) -> bool {
@@ -138,12 +128,17 @@ impl<'tcx> MirPatch<'tcx> {
138128
self.new_blocks.len(),
139129
body.basic_blocks().len()
140130
);
141-
body.basic_blocks_mut().extend(self.new_blocks);
131+
let bbs = if self.patch_map.is_empty() && self.new_blocks.is_empty() {
132+
body.basic_blocks.as_mut_preserves_cfg()
133+
} else {
134+
body.basic_blocks.as_mut()
135+
};
136+
bbs.extend(self.new_blocks);
142137
body.local_decls.extend(self.new_locals);
143138
for (src, patch) in self.patch_map.into_iter_enumerated() {
144139
if let Some(patch) = patch {
145140
debug!("MirPatch: patching block {:?}", src);
146-
body[src].terminator_mut().kind = patch;
141+
bbs[src].terminator_mut().kind = patch;
147142
}
148143
}
149144

compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ impl RemoveNoopLandingPads {
8383
fn remove_nop_landing_pads(&self, body: &mut Body<'_>) {
8484
debug!("body: {:#?}", body);
8585

86-
// make sure there's a single resume block
86+
// make sure there's a resume block
8787
let resume_block = {
88-
let patch = MirPatch::new(body);
88+
let mut patch = MirPatch::new(body);
8989
let resume_block = patch.resume_block();
9090
patch.apply(body);
9191
resume_block

src/test/mir-opt/derefer_complex_case.main.Derefer.diff

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@
102102
StorageDead(_6); // scope 1 at $DIR/derefer_complex_case.rs:+1:39: +1:40
103103
_5 = const (); // scope 1 at $DIR/derefer_complex_case.rs:+1:5: +1:40
104104
goto -> bb2; // scope 1 at $DIR/derefer_complex_case.rs:+1:5: +1:40
105-
+ }
106-
+
107-
+ bb8 (cleanup): {
108-
+ resume; // scope 0 at $DIR/derefer_complex_case.rs:+0:1: +2:2
109105
}
110106
}
111107

src/test/mir-opt/derefer_terminator_test.main.Derefer.diff

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@
9494
StorageDead(_2); // scope 1 at $DIR/derefer_terminator_test.rs:+8:1: +8:2
9595
StorageDead(_1); // scope 0 at $DIR/derefer_terminator_test.rs:+8:1: +8:2
9696
return; // scope 0 at $DIR/derefer_terminator_test.rs:+8:2: +8:2
97-
+ }
98-
+
99-
+ bb6 (cleanup): {
100-
+ resume; // scope 0 at $DIR/derefer_terminator_test.rs:+0:1: +8:2
10197
}
10298
}
10399

src/test/mir-opt/derefer_test.main.Derefer.diff

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@
4949
StorageDead(_2); // scope 1 at $DIR/derefer_test.rs:+5:1: +5:2
5050
StorageDead(_1); // scope 0 at $DIR/derefer_test.rs:+5:1: +5:2
5151
return; // scope 0 at $DIR/derefer_test.rs:+5:2: +5:2
52-
+ }
53-
+
54-
+ bb1 (cleanup): {
55-
+ resume; // scope 0 at $DIR/derefer_test.rs:+0:1: +5:2
5652
}
5753
}
5854

src/test/mir-opt/derefer_test_multiple.main.Derefer.diff

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@
8787
StorageDead(_2); // scope 1 at $DIR/derefer_test_multiple.rs:+7:1: +7:2
8888
StorageDead(_1); // scope 0 at $DIR/derefer_test_multiple.rs:+7:1: +7:2
8989
return; // scope 0 at $DIR/derefer_test_multiple.rs:+7:2: +7:2
90-
+ }
91-
+
92-
+ bb1 (cleanup): {
93-
+ resume; // scope 0 at $DIR/derefer_test_multiple.rs:+0:1: +7:2
9490
}
9591
}
9692

src/test/mir-opt/inline/dyn_trait.get_query.Inline.diff

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@
5757
StorageDead(_4); // scope 1 at $DIR/dyn-trait.rs:+2:24: +2:25
5858
StorageDead(_2); // scope 0 at $DIR/dyn-trait.rs:+3:1: +3:2
5959
return; // scope 0 at $DIR/dyn-trait.rs:+3:2: +3:2
60-
+ }
61-
+
62-
+ bb3 (cleanup): {
63-
+ resume; // scope 0 at $DIR/dyn-trait.rs:+0:1: +3:2
6460
}
6561
}
6662

src/test/mir-opt/inline/dyn_trait.try_execute_query.Inline.diff

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
+ StorageDead(_4); // scope 1 at $DIR/dyn-trait.rs:+0:21: +0:22
3333
StorageDead(_2); // scope 0 at $DIR/dyn-trait.rs:+1:15: +1:16
3434
return; // scope 0 at $DIR/dyn-trait.rs:+2:2: +2:2
35-
+ }
36-
+
37-
+ bb2 (cleanup): {
38-
+ resume; // scope 0 at $DIR/dyn-trait.rs:+0:1: +2:2
3935
}
4036
}
4137

src/test/mir-opt/inline/inline_any_operand.bar.Inline.after.mir

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,4 @@ fn bar() -> bool {
4141
StorageDead(_1); // scope 0 at $DIR/inline-any-operand.rs:+3:1: +3:2
4242
return; // scope 0 at $DIR/inline-any-operand.rs:+3:2: +3:2
4343
}
44-
45-
bb1 (cleanup): {
46-
resume; // scope 0 at $DIR/inline-any-operand.rs:+0:1: +3:2
47-
}
4844
}

src/test/mir-opt/inline/inline_closure.foo.Inline.after.mir

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,4 @@ fn foo(_1: T, _2: i32) -> i32 {
4646
StorageDead(_3); // scope 0 at $DIR/inline-closure.rs:+3:1: +3:2
4747
return; // scope 0 at $DIR/inline-closure.rs:+3:2: +3:2
4848
}
49-
50-
bb1 (cleanup): {
51-
resume; // scope 0 at $DIR/inline-closure.rs:+0:1: +3:2
52-
}
5349
}

0 commit comments

Comments
 (0)