Auto merge of #96116 - ouz-a:mir-opt, r=oli-obk
Make derefer work everwhere Follow up work on previous PR's #95649 and #95857. r? rust-lang/mir-opt _Co-Authored-By: `@oli-obk_`
This commit is contained in:
commit
055bf4ccd5
12 changed files with 609 additions and 267 deletions
|
@ -141,6 +141,7 @@ impl<'tcx> MirPatch<'tcx> {
|
|||
|
||||
let mut delta = 0;
|
||||
let mut last_bb = START_BLOCK;
|
||||
let mut stmts_and_targets: Vec<(Statement<'_>, BasicBlock)> = Vec::new();
|
||||
for (mut loc, stmt) in new_statements {
|
||||
if loc.block != last_bb {
|
||||
delta = 0;
|
||||
|
@ -149,11 +150,30 @@ impl<'tcx> MirPatch<'tcx> {
|
|||
debug!("MirPatch: adding statement {:?} at loc {:?}+{}", stmt, loc, delta);
|
||||
loc.statement_index += delta;
|
||||
let source_info = Self::source_info_for_index(&body[loc.block], loc);
|
||||
|
||||
// For mir-opt `Derefer` to work in all cases we need to
|
||||
// get terminator's targets and apply the statement to all of them.
|
||||
if loc.statement_index > body[loc.block].statements.len() {
|
||||
let term = body[loc.block].terminator();
|
||||
let successors = term.successors().clone();
|
||||
|
||||
for i in successors {
|
||||
stmts_and_targets
|
||||
.push((Statement { source_info, kind: stmt.clone() }, i.clone()));
|
||||
}
|
||||
delta += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
body[loc.block]
|
||||
.statements
|
||||
.insert(loc.statement_index, Statement { source_info, kind: stmt });
|
||||
delta += 1;
|
||||
}
|
||||
|
||||
for (stmt, target) in stmts_and_targets.into_iter().rev() {
|
||||
body[target].statements.insert(0, stmt);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn source_info_for_index(data: &BasicBlockData<'_>, loc: Location) -> SourceInfo {
|
||||
|
|
|
@ -1,68 +1,89 @@
|
|||
use crate::MirPass;
|
||||
use rustc_index::vec::IndexVec;
|
||||
use rustc_middle::mir::patch::MirPatch;
|
||||
use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
|
||||
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))) => {
|
||||
let mut place_local = place.local;
|
||||
let mut last_len = 0;
|
||||
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);
|
||||
pub struct DerefChecker<'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
patcher: MirPatch<'tcx>,
|
||||
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
||||
}
|
||||
|
||||
// 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));
|
||||
impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {
|
||||
fn tcx(&self) -> TyCtxt<'tcx> {
|
||||
self.tcx
|
||||
}
|
||||
|
||||
// We are adding current p_ref's projections to our
|
||||
// temp value, excluding projections we already covered.
|
||||
let deref_place = Place::from(place_local)
|
||||
.project_deeper(&p_ref.projection[last_len..], tcx);
|
||||
patch.add_assign(
|
||||
loc,
|
||||
Place::from(temp),
|
||||
Rvalue::Use(Operand::Move(deref_place)),
|
||||
);
|
||||
fn visit_place(&mut self, place: &mut Place<'tcx>, _: PlaceContext, loc: Location) {
|
||||
let mut place_local = place.local;
|
||||
let mut last_len = 0;
|
||||
let mut last_deref_idx = 0;
|
||||
|
||||
place_local = temp;
|
||||
last_len = p_ref.projection.len();
|
||||
let mut prev_temp: Option<Local> = None;
|
||||
|
||||
// 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);
|
||||
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;
|
||||
|
||||
// Since our job with the temp is done it should be gone
|
||||
let loc = Location { block: block, statement_index: i + 1 };
|
||||
patch.add_statement(loc, StatementKind::StorageDead(temp));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
|
||||
if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() {
|
||||
last_deref_idx = idx;
|
||||
}
|
||||
}
|
||||
|
||||
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
|
||||
if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() {
|
||||
let ty = p_ref.ty(&self.local_decls, self.tcx).ty;
|
||||
let temp =
|
||||
self.patcher.new_temp(ty, self.local_decls[p_ref.local].source_info.span);
|
||||
|
||||
self.patcher.add_statement(loc, StatementKind::StorageLive(temp));
|
||||
|
||||
// We are adding current p_ref's projections to our
|
||||
// temp value, excluding projections we already covered.
|
||||
let deref_place = Place::from(place_local)
|
||||
.project_deeper(&p_ref.projection[last_len..], self.tcx);
|
||||
self.patcher.add_assign(
|
||||
loc,
|
||||
Place::from(temp),
|
||||
Rvalue::Use(Operand::Move(deref_place)),
|
||||
);
|
||||
|
||||
place_local = temp;
|
||||
last_len = p_ref.projection.len();
|
||||
|
||||
// Change `Place` only if we are actually at the Place's last deref
|
||||
if idx == last_deref_idx {
|
||||
let temp_place =
|
||||
Place::from(temp).project_deeper(&place.projection[idx..], self.tcx);
|
||||
*place = temp_place;
|
||||
}
|
||||
|
||||
// We are destroying last temp since it's no longer used.
|
||||
if let Some(prev_temp) = prev_temp {
|
||||
self.patcher.add_statement(loc, StatementKind::StorageDead(prev_temp));
|
||||
}
|
||||
|
||||
prev_temp = Some(temp);
|
||||
}
|
||||
}
|
||||
|
||||
// Since we won't be able to reach final temp, we destroy it outside the loop.
|
||||
if let Some(prev_temp) = prev_temp {
|
||||
let last_loc = Location { block: loc.block, statement_index: loc.statement_index + 1 };
|
||||
self.patcher.add_statement(last_loc, StatementKind::StorageDead(prev_temp));
|
||||
}
|
||||
}
|
||||
patch.apply(body);
|
||||
}
|
||||
|
||||
pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
||||
let patch = MirPatch::new(body);
|
||||
let mut checker = DerefChecker { tcx, patcher: patch, local_decls: body.local_decls.clone() };
|
||||
|
||||
for (bb, data) in body.basic_blocks_mut().iter_enumerated_mut() {
|
||||
checker.visit_basic_block_data(bb, data);
|
||||
}
|
||||
|
||||
checker.patcher.apply(body);
|
||||
}
|
||||
|
||||
impl<'tcx> MirPass<'tcx> for Derefer {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue