Skip to content

Avoid invalidating the CFG in MirPatch #100087

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 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
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
65 changes: 30 additions & 35 deletions compiler/rustc_middle/src/mir/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub struct MirPatch<'tcx> {
new_blocks: Vec<BasicBlockData<'tcx>>,
new_statements: Vec<(Location, StatementKind<'tcx>)>,
new_locals: Vec<LocalDecl<'tcx>>,
resume_block: BasicBlock,
resume_block: Option<BasicBlock>,
body_span: Span,
next_local: usize,
}

Expand All @@ -23,47 +24,36 @@ impl<'tcx> MirPatch<'tcx> {
new_statements: vec![],
new_locals: vec![],
next_local: body.local_decls.len(),
resume_block: START_BLOCK,
resume_block: None,
body_span: body.span,
};

// make sure the MIR we create has a resume block. It is
// completely legal to convert jumps to the resume block
// to jumps to None, but we occasionally have to add
// instructions just before that.

let mut resume_block = None;
let mut resume_stmt_block = None;
// Check if we already have a resume block
for (bb, block) in body.basic_blocks().iter_enumerated() {
if let TerminatorKind::Resume = block.terminator().kind {
if !block.statements.is_empty() {
assert!(resume_stmt_block.is_none());
resume_stmt_block = Some(bb);
} else {
resume_block = Some(bb);
}
if let TerminatorKind::Resume = block.terminator().kind && block.statements.is_empty() {
result.resume_block = Some(bb);
break;
}
}
let resume_block = resume_block.unwrap_or_else(|| {
result.new_block(BasicBlockData {
statements: vec![],
terminator: Some(Terminator {
source_info: SourceInfo::outermost(body.span),
kind: TerminatorKind::Resume,
}),
is_cleanup: true,
})
});
result.resume_block = resume_block;
if let Some(resume_stmt_block) = resume_stmt_block {
result
.patch_terminator(resume_stmt_block, TerminatorKind::Goto { target: resume_block });
}

result
}

pub fn resume_block(&self) -> BasicBlock {
self.resume_block
pub fn resume_block(&mut self) -> BasicBlock {
if let Some(bb) = self.resume_block {
return bb;
}

let bb = self.new_block(BasicBlockData {
statements: vec![],
terminator: Some(Terminator {
source_info: SourceInfo::outermost(self.body_span),
kind: TerminatorKind::Resume,
}),
is_cleanup: true,
});
self.resume_block = Some(bb);
bb
}

pub fn is_patched(&self, bb: BasicBlock) -> bool {
Expand Down Expand Up @@ -138,12 +128,17 @@ impl<'tcx> MirPatch<'tcx> {
self.new_blocks.len(),
body.basic_blocks().len()
);
body.basic_blocks_mut().extend(self.new_blocks);
let bbs = if self.patch_map.is_empty() && self.new_blocks.is_empty() {
body.basic_blocks.as_mut_preserves_cfg()
} else {
body.basic_blocks.as_mut()
};
bbs.extend(self.new_blocks);
body.local_decls.extend(self.new_locals);
for (src, patch) in self.patch_map.into_iter_enumerated() {
if let Some(patch) = patch {
debug!("MirPatch: patching block {:?}", src);
body[src].terminator_mut().kind = patch;
bbs[src].terminator_mut().kind = patch;
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ impl RemoveNoopLandingPads {
fn remove_nop_landing_pads(&self, body: &mut Body<'_>) {
debug!("body: {:#?}", body);

// make sure there's a single resume block
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that the claim that this ensured that the given block was the only resume block was wrong wrt the previous implementation too.

// make sure there's a resume block
let resume_block = {
let patch = MirPatch::new(body);
let mut patch = MirPatch::new(body);
let resume_block = patch.resume_block();
patch.apply(body);
resume_block
Expand Down
4 changes: 0 additions & 4 deletions src/test/mir-opt/derefer_complex_case.main.Derefer.diff
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@
StorageDead(_6); // scope 1 at $DIR/derefer_complex_case.rs:+1:39: +1:40
_5 = const (); // scope 1 at $DIR/derefer_complex_case.rs:+1:5: +1:40
goto -> bb2; // scope 1 at $DIR/derefer_complex_case.rs:+1:5: +1:40
+ }
+
+ bb8 (cleanup): {
+ resume; // scope 0 at $DIR/derefer_complex_case.rs:+0:1: +2:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/derefer_terminator_test.main.Derefer.diff
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@
StorageDead(_2); // scope 1 at $DIR/derefer_terminator_test.rs:+8:1: +8:2
StorageDead(_1); // scope 0 at $DIR/derefer_terminator_test.rs:+8:1: +8:2
return; // scope 0 at $DIR/derefer_terminator_test.rs:+8:2: +8:2
+ }
+
+ bb6 (cleanup): {
+ resume; // scope 0 at $DIR/derefer_terminator_test.rs:+0:1: +8:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/derefer_test.main.Derefer.diff
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@
StorageDead(_2); // scope 1 at $DIR/derefer_test.rs:+5:1: +5:2
StorageDead(_1); // scope 0 at $DIR/derefer_test.rs:+5:1: +5:2
return; // scope 0 at $DIR/derefer_test.rs:+5:2: +5:2
+ }
+
+ bb1 (cleanup): {
+ resume; // scope 0 at $DIR/derefer_test.rs:+0:1: +5:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/derefer_test_multiple.main.Derefer.diff
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@
StorageDead(_2); // scope 1 at $DIR/derefer_test_multiple.rs:+7:1: +7:2
StorageDead(_1); // scope 0 at $DIR/derefer_test_multiple.rs:+7:1: +7:2
return; // scope 0 at $DIR/derefer_test_multiple.rs:+7:2: +7:2
+ }
+
+ bb1 (cleanup): {
+ resume; // scope 0 at $DIR/derefer_test_multiple.rs:+0:1: +7:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/dyn_trait.get_query.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
StorageDead(_4); // scope 1 at $DIR/dyn-trait.rs:+2:24: +2:25
StorageDead(_2); // scope 0 at $DIR/dyn-trait.rs:+3:1: +3:2
return; // scope 0 at $DIR/dyn-trait.rs:+3:2: +3:2
+ }
+
+ bb3 (cleanup): {
+ resume; // scope 0 at $DIR/dyn-trait.rs:+0:1: +3:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
+ StorageDead(_4); // scope 1 at $DIR/dyn-trait.rs:+0:21: +0:22
StorageDead(_2); // scope 0 at $DIR/dyn-trait.rs:+1:15: +1:16
return; // scope 0 at $DIR/dyn-trait.rs:+2:2: +2:2
+ }
+
+ bb2 (cleanup): {
+ resume; // scope 0 at $DIR/dyn-trait.rs:+0:1: +2:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,4 @@ fn bar() -> bool {
StorageDead(_1); // scope 0 at $DIR/inline-any-operand.rs:+3:1: +3:2
return; // scope 0 at $DIR/inline-any-operand.rs:+3:2: +3:2
}

bb1 (cleanup): {
resume; // scope 0 at $DIR/inline-any-operand.rs:+0:1: +3:2
}
}
4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_closure.foo.Inline.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,4 @@ fn foo(_1: T, _2: i32) -> i32 {
StorageDead(_3); // scope 0 at $DIR/inline-closure.rs:+3:1: +3:2
return; // scope 0 at $DIR/inline-closure.rs:+3:2: +3:2
}

bb1 (cleanup): {
resume; // scope 0 at $DIR/inline-closure.rs:+0:1: +3:2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,4 @@ fn foo(_1: T, _2: &i32) -> i32 {
StorageDead(_3); // scope 0 at $DIR/inline-closure-borrows-arg.rs:+6:1: +6:2
return; // scope 0 at $DIR/inline-closure-borrows-arg.rs:+6:2: +6:2
}

bb1 (cleanup): {
resume; // scope 0 at $DIR/inline-closure-borrows-arg.rs:+0:1: +6:2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,4 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
StorageDead(_3); // scope 0 at $DIR/inline-closure-captures.rs:+3:1: +3:2
return; // scope 0 at $DIR/inline-closure-captures.rs:+3:2: +3:2
}

bb1 (cleanup): {
resume; // scope 0 at $DIR/inline-closure-captures.rs:+0:1: +3:2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:+1:18: +1:19
_0 = const (); // scope 0 at $DIR/inline-compatibility.rs:+0:37: +2:2
return; // scope 0 at $DIR/inline-compatibility.rs:+2:2: +2:2
+ }
+
+ bb1 (cleanup): {
+ resume; // scope 0 at $DIR/inline-compatibility.rs:+0:1: +2:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:+1:21: +1:22
_0 = const (); // scope 0 at $DIR/inline-compatibility.rs:+0:40: +2:2
return; // scope 0 at $DIR/inline-compatibility.rs:+2:2: +2:2
+ }
+
+ bb1 (cleanup): {
+ resume; // scope 0 at $DIR/inline-compatibility.rs:+0:1: +2:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_cycle.one.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
StorageDead(_1); // scope 0 at $DIR/inline-cycle.rs:+1:24: +1:25
_0 = const (); // scope 0 at $DIR/inline-cycle.rs:+0:10: +2:2
return; // scope 0 at $DIR/inline-cycle.rs:+2:2: +2:2
+ }
+
+ bb2 (cleanup): {
+ resume; // scope 0 at $DIR/inline-cycle.rs:+0:1: +2:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_cycle.two.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@
StorageDead(_1); // scope 0 at $DIR/inline-cycle.rs:+1:12: +1:13
_0 = const (); // scope 0 at $DIR/inline-cycle.rs:+0:10: +2:2
return; // scope 0 at $DIR/inline-cycle.rs:+2:2: +2:2
+ }
+
+ bb2 (cleanup): {
+ resume; // scope 0 at $DIR/inline-cycle.rs:+0:1: +2:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_cycle_generic.main.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
StorageDead(_1); // scope 0 at $DIR/inline-cycle-generic.rs:+1:24: +1:25
_0 = const (); // scope 0 at $DIR/inline-cycle-generic.rs:+0:11: +2:2
return; // scope 0 at $DIR/inline-cycle-generic.rs:+2:2: +2:2
+ }
+
+ bb2 (cleanup): {
+ resume; // scope 0 at $DIR/inline-cycle-generic.rs:+0:1: +2:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_diverging.f.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
+
+ bb1: {
+ goto -> bb1; // scope 1 at $DIR/inline-diverging.rs:+32:5: +32:12
+ }
+
+ bb2 (cleanup): {
+ resume; // scope 0 at $DIR/inline-diverging.rs:+0:1: +2:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_diverging.g.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@
+ // mir::Constant
+ // + span: $SRC_DIR/std/src/panic.rs:LL:COL
+ // + literal: Const { ty: &str, val: Value(Slice(..)) }
+ }
+
+ bb3 (cleanup): {
+ resume; // scope 0 at $DIR/inline-diverging.rs:+0:1: +6:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_diverging.h.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@
+
+ bb1: {
+ goto -> bb1; // scope 5 at $DIR/inline-diverging.rs:+18:5: +18:12
+ }
+
+ bb2 (cleanup): {
+ resume; // scope 0 at $DIR/inline-diverging.rs:+0:1: +2:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
StorageDead(_3); // scope 0 at $DIR/inline-instruction-set.rs:+3:30: +3:31
_0 = const (); // scope 0 at $DIR/inline-instruction-set.rs:+0:18: +4:2
return; // scope 0 at $DIR/inline-instruction-set.rs:+4:2: +4:2
+ }
+
+ bb3 (cleanup): {
+ resume; // scope 0 at $DIR/inline-instruction-set.rs:+0:1: +4:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
StorageDead(_3); // scope 0 at $DIR/inline-instruction-set.rs:+5:30: +5:31
_0 = const (); // scope 0 at $DIR/inline-instruction-set.rs:+0:14: +6:2
return; // scope 0 at $DIR/inline-instruction-set.rs:+6:2: +6:2
+ }
+
+ bb3 (cleanup): {
+ resume; // scope 0 at $DIR/inline-instruction-set.rs:+0:1: +6:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_options.main.Inline.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,4 @@ fn main() -> () {
_0 = const (); // scope 0 at $DIR/inline-options.rs:+0:11: +3:2
return; // scope 0 at $DIR/inline-options.rs:+3:2: +3:2
}

bb5 (cleanup): {
resume; // scope 0 at $DIR/inline-options.rs:+0:1: +3:2
}
}
4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_retag.bar.Inline.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,4 @@ fn bar() -> bool {
StorageDead(_4); // scope 0 at $DIR/inline-retag.rs:+3:1: +3:2
return; // scope 0 at $DIR/inline-retag.rs:+3:2: +3:2
}

bb1 (cleanup): {
resume; // scope 0 at $DIR/inline-retag.rs:+0:1: +3:2
}
}
4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_shims.clone.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
+ _0 = (*_2); // scope 1 at $SRC_DIR/core/src/clone.rs:LL:COL
StorageDead(_2); // scope 0 at $DIR/inline-shims.rs:+1:13: +1:14
return; // scope 0 at $DIR/inline-shims.rs:+2:2: +2:2
+ }
+
+ bb1 (cleanup): {
+ resume; // scope 0 at $DIR/inline-shims.rs:+0:1: +2:2
}
}

4 changes: 0 additions & 4 deletions src/test/mir-opt/inline/inline_shims.drop.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@
+
+ bb3: {
+ drop((((*_5) as Some).0: B)) -> bb2; // scope 3 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
+ }
+
+ bb4 (cleanup): {
+ resume; // scope 0 at $DIR/inline-shims.rs:+0:1: +3:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
_0 = const (); // scope 0 at $DIR/inline-specialization.rs:+0:11: +2:2
StorageDead(_1); // scope 0 at $DIR/inline-specialization.rs:+2:1: +2:2
return; // scope 0 at $DIR/inline-specialization.rs:+2:2: +2:2
+ }
+
+ bb1 (cleanup): {
+ resume; // scope 0 at $DIR/inline-specialization.rs:+0:1: +2:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,4 @@ fn test2(_1: &dyn X) -> bool {
StorageDead(_2); // scope 0 at $DIR/inline-trait-method_2.rs:+1:11: +1:12
return; // scope 0 at $DIR/inline-trait-method_2.rs:+2:2: +2:2
}

bb2 (cleanup): {
resume; // scope 0 at $DIR/inline-trait-method_2.rs:+0:1: +2:2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,4 @@ fn a(_1: &mut [T]) -> &mut [T] {
StorageDead(_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:+2:1: +2:2
return; // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:+2:2: +2:2
}

bb1 (cleanup): {
resume; // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:+0:1: +2:2
}
}
Loading