-
Notifications
You must be signed in to change notification settings - Fork 13.4k
New mir-opt deref_separator #95649
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
New mir-opt deref_separator #95649
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::MirPass; | ||
use rustc_middle::mir::patch::MirPatch; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::ty::TyCtxt; | ||
pub struct Derefer; | ||
|
||
pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { | ||
let mut patch = MirPatch::new(body); | ||
let (basic_blocks, local_decl) = body.basic_blocks_and_local_decls_mut(); | ||
for (block, data) in basic_blocks.iter_enumerated_mut() { | ||
for (i, stmt) in data.statements.iter_mut().enumerate() { | ||
match stmt.kind { | ||
StatementKind::Assign(box (og_place, Rvalue::Ref(region, borrow_knd, place))) => { | ||
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() { | ||
if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() { | ||
// The type that we are derefing | ||
let ty = p_ref.ty(local_decl, tcx).ty; | ||
let temp = patch.new_temp(ty, stmt.source_info.span); | ||
|
||
// Because we are assigning this right before original statement | ||
// we are using index i of statement | ||
let loc = Location { block: block, statement_index: i }; | ||
patch.add_statement(loc, StatementKind::StorageLive(temp)); | ||
|
||
// We are adding current p_ref's projections to our | ||
// temp value | ||
let deref_place = | ||
Place::from(p_ref.local).project_deeper(p_ref.projection, tcx); | ||
patch.add_assign( | ||
loc, | ||
Place::from(temp), | ||
Rvalue::Use(Operand::Move(deref_place)), | ||
); | ||
|
||
// We are creating a place by using our temp value's location | ||
// and copying derefed values which we need to create new statement | ||
let temp_place = | ||
Place::from(temp).project_deeper(&place.projection[idx..], tcx); | ||
patch.add_assign( | ||
loc, | ||
og_place, | ||
Rvalue::Ref(region, borrow_knd, temp_place), | ||
); | ||
|
||
let new_stmt = Statement { | ||
source_info: stmt.source_info, | ||
kind: StatementKind::Assign(Box::new(( | ||
og_place, | ||
Rvalue::Ref(region, borrow_knd, temp_place), | ||
))), | ||
}; | ||
// Replace current statement with newly created one | ||
*stmt = new_stmt; | ||
} | ||
} | ||
} | ||
_ => (), | ||
} | ||
} | ||
} | ||
patch.apply(body); | ||
} | ||
|
||
impl<'tcx> MirPass<'tcx> for Derefer { | ||
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { | ||
deref_finder(tcx, body); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
- // MIR for `main` before Derefer | ||
+ // MIR for `main` after Derefer | ||
|
||
fn main() -> () { | ||
let mut _0: (); // return place in scope 0 at $DIR/derefer_test.rs:2:11: 2:11 | ||
let mut _1: (i32, i32); // in scope 0 at $DIR/derefer_test.rs:3:9: 3:14 | ||
let mut _3: &mut (i32, i32); // in scope 0 at $DIR/derefer_test.rs:4:22: 4:28 | ||
+ let mut _6: &mut (i32, i32); // in scope 0 at $DIR/derefer_test.rs:5:13: 5:26 | ||
+ let mut _7: &mut (i32, i32); // in scope 0 at $DIR/derefer_test.rs:6:13: 6:26 | ||
scope 1 { | ||
debug a => _1; // in scope 1 at $DIR/derefer_test.rs:3:9: 3:14 | ||
let mut _2: (i32, &mut (i32, i32)); // in scope 1 at $DIR/derefer_test.rs:4:9: 4:14 | ||
scope 2 { | ||
debug b => _2; // in scope 2 at $DIR/derefer_test.rs:4:9: 4:14 | ||
let _4: &mut i32; // in scope 2 at $DIR/derefer_test.rs:5:9: 5:10 | ||
scope 3 { | ||
debug x => _4; // in scope 3 at $DIR/derefer_test.rs:5:9: 5:10 | ||
let _5: &mut i32; // in scope 3 at $DIR/derefer_test.rs:6:9: 6:10 | ||
scope 4 { | ||
debug y => _5; // in scope 4 at $DIR/derefer_test.rs:6:9: 6:10 | ||
} | ||
} | ||
} | ||
} | ||
|
||
bb0: { | ||
StorageLive(_1); // scope 0 at $DIR/derefer_test.rs:3:9: 3:14 | ||
(_1.0: i32) = const 42_i32; // scope 0 at $DIR/derefer_test.rs:3:17: 3:24 | ||
(_1.1: i32) = const 43_i32; // scope 0 at $DIR/derefer_test.rs:3:17: 3:24 | ||
StorageLive(_2); // scope 1 at $DIR/derefer_test.rs:4:9: 4:14 | ||
StorageLive(_3); // scope 1 at $DIR/derefer_test.rs:4:22: 4:28 | ||
_3 = &mut _1; // scope 1 at $DIR/derefer_test.rs:4:22: 4:28 | ||
(_2.0: i32) = const 99_i32; // scope 1 at $DIR/derefer_test.rs:4:17: 4:29 | ||
(_2.1: &mut (i32, i32)) = move _3; // scope 1 at $DIR/derefer_test.rs:4:17: 4:29 | ||
StorageDead(_3); // scope 1 at $DIR/derefer_test.rs:4:28: 4:29 | ||
StorageLive(_4); // scope 2 at $DIR/derefer_test.rs:5:9: 5:10 | ||
- _4 = &mut ((*(_2.1: &mut (i32, i32))).0: i32); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26 | ||
+ StorageLive(_6); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26 | ||
+ _6 = move (_2.1: &mut (i32, i32)); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26 | ||
+ _4 = &mut ((*_6).0: i32); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26 | ||
+ _4 = &mut ((*_6).0: i32); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26 | ||
StorageLive(_5); // scope 3 at $DIR/derefer_test.rs:6:9: 6:10 | ||
- _5 = &mut ((*(_2.1: &mut (i32, i32))).1: i32); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26 | ||
+ StorageLive(_7); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26 | ||
+ _7 = move (_2.1: &mut (i32, i32)); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26 | ||
+ _5 = &mut ((*_7).1: i32); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26 | ||
+ _5 = &mut ((*_7).1: i32); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26 | ||
_0 = const (); // scope 0 at $DIR/derefer_test.rs:2:11: 7:2 | ||
StorageDead(_5); // scope 3 at $DIR/derefer_test.rs:7:1: 7:2 | ||
StorageDead(_4); // scope 2 at $DIR/derefer_test.rs:7:1: 7:2 | ||
StorageDead(_2); // scope 1 at $DIR/derefer_test.rs:7:1: 7:2 | ||
StorageDead(_1); // scope 0 at $DIR/derefer_test.rs:7:1: 7:2 | ||
return; // scope 0 at $DIR/derefer_test.rs:7:2: 7:2 | ||
+ } | ||
+ | ||
+ bb1 (cleanup): { | ||
+ resume; // scope 0 at $DIR/derefer_test.rs:2:1: 7:2 | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// EMIT_MIR derefer_test.main.Derefer.diff | ||
fn main() { | ||
let mut a = (42,43); | ||
let mut b = (99, &mut a); | ||
let x = &mut (*b.1).0; | ||
let y = &mut (*b.1).1; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This add call is now not necessary anymore, as the exact same statement is created via mutation below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you can add though is a storagdead statement for the temp. Maybe that'll eliminate the extra cleanup block that showed up in a bunch of tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mir dump looks clean now 😸
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are still a bunch of new cleanup blocks showing up. Let's see if a storagedead resolves those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought storagedead would resolve all of those 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, misread the code. Yea, not sure what is going on, i'll investigate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, it's just MirPatch doing that eagerly even if unneeded.