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

Diff for: compiler/rustc_middle/src/mir/patch.rs

+30-35
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

Diff for: compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs

+2-2
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

Diff for: src/test/mir-opt/derefer_complex_case.main.Derefer.diff

-4
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

Diff for: src/test/mir-opt/derefer_terminator_test.main.Derefer.diff

-4
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

Diff for: src/test/mir-opt/derefer_test.main.Derefer.diff

-4
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

Diff for: src/test/mir-opt/derefer_test_multiple.main.Derefer.diff

-4
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

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

-4
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

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

-4
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

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

-4
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
}

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

-4
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
}

Diff for: src/test/mir-opt/inline/inline_closure_borrows_arg.foo.Inline.after.mir

-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,4 @@ fn foo(_1: T, _2: &i32) -> i32 {
5353
StorageDead(_3); // scope 0 at $DIR/inline-closure-borrows-arg.rs:+6:1: +6:2
5454
return; // scope 0 at $DIR/inline-closure-borrows-arg.rs:+6:2: +6:2
5555
}
56-
57-
bb1 (cleanup): {
58-
resume; // scope 0 at $DIR/inline-closure-borrows-arg.rs:+0:1: +6:2
59-
}
6056
}

Diff for: src/test/mir-opt/inline/inline_closure_captures.foo.Inline.after.mir

-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,4 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
6666
StorageDead(_3); // scope 0 at $DIR/inline-closure-captures.rs:+3:1: +3:2
6767
return; // scope 0 at $DIR/inline-closure-captures.rs:+3:2: +3:2
6868
}
69-
70-
bb1 (cleanup): {
71-
resume; // scope 0 at $DIR/inline-closure-captures.rs:+0:1: +3:2
72-
}
7369
}

Diff for: src/test/mir-opt/inline/inline_compatibility.inlined_no_sanitize.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:+1:18: +1:19
2020
_0 = const (); // scope 0 at $DIR/inline-compatibility.rs:+0:37: +2:2
2121
return; // scope 0 at $DIR/inline-compatibility.rs:+2:2: +2:2
22-
+ }
23-
+
24-
+ bb1 (cleanup): {
25-
+ resume; // scope 0 at $DIR/inline-compatibility.rs:+0:1: +2:2
2622
}
2723
}
2824

Diff for: src/test/mir-opt/inline/inline_compatibility.inlined_target_feature.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:+1:21: +1:22
2020
_0 = const (); // scope 0 at $DIR/inline-compatibility.rs:+0:40: +2:2
2121
return; // scope 0 at $DIR/inline-compatibility.rs:+2:2: +2:2
22-
+ }
23-
+
24-
+ bb1 (cleanup): {
25-
+ resume; // scope 0 at $DIR/inline-compatibility.rs:+0:1: +2:2
2622
}
2723
}
2824

Diff for: src/test/mir-opt/inline/inline_cycle.one.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
StorageDead(_1); // scope 0 at $DIR/inline-cycle.rs:+1:24: +1:25
2626
_0 = const (); // scope 0 at $DIR/inline-cycle.rs:+0:10: +2:2
2727
return; // scope 0 at $DIR/inline-cycle.rs:+2:2: +2:2
28-
+ }
29-
+
30-
+ bb2 (cleanup): {
31-
+ resume; // scope 0 at $DIR/inline-cycle.rs:+0:1: +2:2
3228
}
3329
}
3430

Diff for: src/test/mir-opt/inline/inline_cycle.two.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@
5050
StorageDead(_1); // scope 0 at $DIR/inline-cycle.rs:+1:12: +1:13
5151
_0 = const (); // scope 0 at $DIR/inline-cycle.rs:+0:10: +2:2
5252
return; // scope 0 at $DIR/inline-cycle.rs:+2:2: +2:2
53-
+ }
54-
+
55-
+ bb2 (cleanup): {
56-
+ resume; // scope 0 at $DIR/inline-cycle.rs:+0:1: +2:2
5753
}
5854
}
5955

Diff for: src/test/mir-opt/inline/inline_cycle_generic.main.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
StorageDead(_1); // scope 0 at $DIR/inline-cycle-generic.rs:+1:24: +1:25
2828
_0 = const (); // scope 0 at $DIR/inline-cycle-generic.rs:+0:11: +2:2
2929
return; // scope 0 at $DIR/inline-cycle-generic.rs:+2:2: +2:2
30-
+ }
31-
+
32-
+ bb2 (cleanup): {
33-
+ resume; // scope 0 at $DIR/inline-cycle-generic.rs:+0:1: +2:2
3430
}
3531
}
3632

Diff for: src/test/mir-opt/inline/inline_diverging.f.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
+
2020
+ bb1: {
2121
+ goto -> bb1; // scope 1 at $DIR/inline-diverging.rs:+32:5: +32:12
22-
+ }
23-
+
24-
+ bb2 (cleanup): {
25-
+ resume; // scope 0 at $DIR/inline-diverging.rs:+0:1: +2:2
2622
}
2723
}
2824

Diff for: src/test/mir-opt/inline/inline_diverging.g.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@
4444
+ // mir::Constant
4545
+ // + span: $SRC_DIR/std/src/panic.rs:LL:COL
4646
+ // + literal: Const { ty: &str, val: Value(Slice(..)) }
47-
+ }
48-
+
49-
+ bb3 (cleanup): {
50-
+ resume; // scope 0 at $DIR/inline-diverging.rs:+0:1: +6:2
5147
}
5248
}
5349

Diff for: src/test/mir-opt/inline/inline_diverging.h.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@
5151
+
5252
+ bb1: {
5353
+ goto -> bb1; // scope 5 at $DIR/inline-diverging.rs:+18:5: +18:12
54-
+ }
55-
+
56-
+ bb2 (cleanup): {
57-
+ resume; // scope 0 at $DIR/inline-diverging.rs:+0:1: +2:2
5854
}
5955
}
6056

Diff for: src/test/mir-opt/inline/inline_instruction_set.default.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@
3939
StorageDead(_3); // scope 0 at $DIR/inline-instruction-set.rs:+3:30: +3:31
4040
_0 = const (); // scope 0 at $DIR/inline-instruction-set.rs:+0:18: +4:2
4141
return; // scope 0 at $DIR/inline-instruction-set.rs:+4:2: +4:2
42-
+ }
43-
+
44-
+ bb3 (cleanup): {
45-
+ resume; // scope 0 at $DIR/inline-instruction-set.rs:+0:1: +4:2
4642
}
4743
}
4844

Diff for: src/test/mir-opt/inline/inline_instruction_set.t32.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@
4141
StorageDead(_3); // scope 0 at $DIR/inline-instruction-set.rs:+5:30: +5:31
4242
_0 = const (); // scope 0 at $DIR/inline-instruction-set.rs:+0:14: +6:2
4343
return; // scope 0 at $DIR/inline-instruction-set.rs:+6:2: +6:2
44-
+ }
45-
+
46-
+ bb3 (cleanup): {
47-
+ resume; // scope 0 at $DIR/inline-instruction-set.rs:+0:1: +6:2
4844
}
4945
}
5046

Diff for: src/test/mir-opt/inline/inline_options.main.Inline.after.mir

-4
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,4 @@ fn main() -> () {
5252
_0 = const (); // scope 0 at $DIR/inline-options.rs:+0:11: +3:2
5353
return; // scope 0 at $DIR/inline-options.rs:+3:2: +3:2
5454
}
55-
56-
bb5 (cleanup): {
57-
resume; // scope 0 at $DIR/inline-options.rs:+0:1: +3:2
58-
}
5955
}

Diff for: src/test/mir-opt/inline/inline_retag.bar.Inline.after.mir

-4
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,4 @@ fn bar() -> bool {
6969
StorageDead(_4); // scope 0 at $DIR/inline-retag.rs:+3:1: +3:2
7070
return; // scope 0 at $DIR/inline-retag.rs:+3:2: +3:2
7171
}
72-
73-
bb1 (cleanup): {
74-
resume; // scope 0 at $DIR/inline-retag.rs:+0:1: +3:2
75-
}
7672
}

Diff for: src/test/mir-opt/inline/inline_shims.clone.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
+ _0 = (*_2); // scope 1 at $SRC_DIR/core/src/clone.rs:LL:COL
2222
StorageDead(_2); // scope 0 at $DIR/inline-shims.rs:+1:13: +1:14
2323
return; // scope 0 at $DIR/inline-shims.rs:+2:2: +2:2
24-
+ }
25-
+
26-
+ bb1 (cleanup): {
27-
+ resume; // scope 0 at $DIR/inline-shims.rs:+0:1: +2:2
2824
}
2925
}
3026

Diff for: src/test/mir-opt/inline/inline_shims.drop.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@
5151
+
5252
+ bb3: {
5353
+ drop((((*_5) as Some).0: B)) -> bb2; // scope 3 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
54-
+ }
55-
+
56-
+ bb4 (cleanup): {
57-
+ resume; // scope 0 at $DIR/inline-shims.rs:+0:1: +3:2
5854
}
5955
}
6056

Diff for: src/test/mir-opt/inline/inline_specialization.main.Inline.diff

-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
_0 = const (); // scope 0 at $DIR/inline-specialization.rs:+0:11: +2:2
2424
StorageDead(_1); // scope 0 at $DIR/inline-specialization.rs:+2:1: +2:2
2525
return; // scope 0 at $DIR/inline-specialization.rs:+2:2: +2:2
26-
+ }
27-
+
28-
+ bb1 (cleanup): {
29-
+ resume; // scope 0 at $DIR/inline-specialization.rs:+0:1: +2:2
3026
}
3127
}
3228

Diff for: src/test/mir-opt/inline/inline_trait_method_2.test2.Inline.after.mir

-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,4 @@ fn test2(_1: &dyn X) -> bool {
2929
StorageDead(_2); // scope 0 at $DIR/inline-trait-method_2.rs:+1:11: +1:12
3030
return; // scope 0 at $DIR/inline-trait-method_2.rs:+2:2: +2:2
3131
}
32-
33-
bb2 (cleanup): {
34-
resume; // scope 0 at $DIR/inline-trait-method_2.rs:+0:1: +2:2
35-
}
3632
}

Diff for: src/test/mir-opt/inline/issue_58867_inline_as_ref_as_mut.a.Inline.after.mir

-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,4 @@ fn a(_1: &mut [T]) -> &mut [T] {
2727
StorageDead(_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:+2:1: +2:2
2828
return; // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:+2:2: +2:2
2929
}
30-
31-
bb1 (cleanup): {
32-
resume; // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:+0:1: +2:2
33-
}
3430
}

0 commit comments

Comments
 (0)