Skip to content

Commit

Permalink
Auto merge of rust-lang#129611 - DianQK:dse, r=<try>
Browse files Browse the repository at this point in the history
dse: Eliminate dead assignment statements when `debuginfo` is not set to `full`

This will help: rust-lang#128299 (comment)

r? cjgillot or mir-opt
  • Loading branch information
bors committed Aug 26, 2024
2 parents 22572d0 + a2bc389 commit 2124150
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 174 deletions.
43 changes: 36 additions & 7 deletions compiler/rustc_mir_transform/src/dead_store_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
//!

use rustc_middle::bug;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::visit::*;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use rustc_mir_dataflow::debuginfo::debuginfo_locals;
use rustc_mir_dataflow::impls::{
borrowed_locals, LivenessTransferFunction, MaybeTransitiveLiveLocals,
};
use rustc_mir_dataflow::Analysis;
use rustc_session::config::DebugInfo;

use crate::util::is_within_packed;

Expand All @@ -31,10 +32,15 @@ use crate::util::is_within_packed;
pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let borrowed_locals = borrowed_locals(body);

// If the user requests complete debuginfo, mark the locals that appear in it as live, so
// we don't remove assignements to them.
let mut always_live = debuginfo_locals(body);
always_live.union(&borrowed_locals);
let always_live = if tcx.sess.opts.debuginfo == DebugInfo::Full {
// If the user requests complete debuginfo, mark the locals that appear in it as live, so
// we don't remove assignements to them.
let mut always_live = debuginfo_locals(body);
always_live.union(&borrowed_locals);
always_live
} else {
borrowed_locals.clone()
};

let mut live = MaybeTransitiveLiveLocals::new(&always_live)
.into_engine(tcx, body)
Expand Down Expand Up @@ -89,7 +95,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if !place.is_indirect() && !always_live.contains(place.local) {
live.seek_before_primary_effect(loc);
if !live.get().contains(place.local) {
patch.push(loc);
patch.push((place.local, loc));
}
}
}
Expand All @@ -114,8 +120,31 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
}

let bbs = body.basic_blocks.as_mut_preserves_cfg();
for Location { block, statement_index } in patch {
for (local, Location { block, statement_index }) in patch {
bbs[block].statements[statement_index].make_nop();
if bbs[block].statements.iter().all(|stmt| match stmt.kind {
StatementKind::Assign(box (place, _))
| StatementKind::SetDiscriminant { place: box place, .. }
| StatementKind::Deinit(box place) => place.local != local,
_ => true,
}) {
if let Some(storage_live_index) = bbs[block]
.statements
.iter()
.take(statement_index)
.position(|stmt| stmt.kind == StatementKind::StorageLive(local))
{
if let Some(storage_dead_index) = bbs[block]
.statements
.iter()
.skip(statement_index)
.position(|stmt| stmt.kind == StatementKind::StorageDead(local))
{
bbs[block].statements[storage_live_index].make_nop();
bbs[block].statements[storage_dead_index + statement_index].make_nop();
}
}
}
}
for (block, argument_index) in call_operands_to_move {
let TerminatorKind::Call { ref mut args, .. } = bbs[block].terminator_mut().kind else {
Expand Down
1 change: 0 additions & 1 deletion tests/mir-opt/issue_101973.inner.GVN.panic-abort.diff
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
StorageLive(_4);
StorageLive(_5);
_5 = copy _1;
nop;
- StorageLive(_14);
- _14 = BitAnd(copy _5, const 255_u32);
- _4 = BitOr(const 0_u32, move _14);
Expand Down
1 change: 0 additions & 1 deletion tests/mir-opt/issue_101973.inner.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
StorageLive(_4);
StorageLive(_5);
_5 = copy _1;
nop;
- StorageLive(_14);
- _14 = BitAnd(copy _5, const 255_u32);
- _4 = BitOr(const 0_u32, move _14);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@
}

bb2: {
StorageLive(_7);
_7 = &(*_2)[0 of 3];
StorageLive(_8);
_8 = &(*_2)[1 of 3];
StorageLive(_9);
_9 = &(*_2)[2 of 3];
StorageDead(_9);
StorageDead(_8);
StorageDead(_7);
StorageDead(_4);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@
}

bb2: {
StorageLive(_7);
_7 = &(*_2)[0 of 3];
StorageLive(_8);
_8 = &(*_2)[1 of 3];
StorageLive(_9);
_9 = &(*_2)[2 of 3];
StorageDead(_9);
StorageDead(_8);
StorageDead(_7);
StorageDead(_4);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,16 @@
}

bb6: {
_5 = move ((_6 as Ok).0: std::ptr::NonNull<[u8]>);
nop;
StorageDead(_16);
StorageDead(_12);
StorageDead(_6);
- StorageLive(_17);
+ nop;
_17 = copy (_5.0: *const [u8]);
- _4 = move _17 as *mut [u8] (PtrToPtr);
- StorageDead(_17);
+ _4 = copy _17 as *mut [u8] (PtrToPtr);
+ nop;
nop;
nop;
nop;
nop;
StorageDead(_5);
- _3 = move _4 as *mut u8 (PtrToPtr);
+ _3 = copy _17 as *mut u8 (PtrToPtr);
nop;
StorageDead(_4);
StorageDead(_3);
- StorageDead(_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@

bb1: {
StorageDead(_6);
- StorageLive(_12);
+ nop;
_12 = copy (_5.0: *const [u8]);
- _4 = move _12 as *mut [u8] (PtrToPtr);
- StorageDead(_12);
+ _4 = copy _12 as *mut [u8] (PtrToPtr);
+ nop;
nop;
nop;
nop;
nop;
StorageDead(_5);
- _3 = move _4 as *mut u8 (PtrToPtr);
+ _3 = copy _12 as *mut u8 (PtrToPtr);
nop;
StorageDead(_4);
StorageDead(_3);
- StorageDead(_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,16 @@
}

bb6: {
_5 = move ((_6 as Ok).0: std::ptr::NonNull<[u8]>);
nop;
StorageDead(_16);
StorageDead(_12);
StorageDead(_6);
- StorageLive(_17);
+ nop;
_17 = copy (_5.0: *const [u8]);
- _4 = move _17 as *mut [u8] (PtrToPtr);
- StorageDead(_17);
+ _4 = copy _17 as *mut [u8] (PtrToPtr);
+ nop;
nop;
nop;
nop;
nop;
StorageDead(_5);
- _3 = move _4 as *mut u8 (PtrToPtr);
+ _3 = copy _17 as *mut u8 (PtrToPtr);
nop;
StorageDead(_4);
StorageDead(_3);
- StorageDead(_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@

bb1: {
StorageDead(_6);
- StorageLive(_12);
+ nop;
_12 = copy (_5.0: *const [u8]);
- _4 = move _12 as *mut [u8] (PtrToPtr);
- StorageDead(_12);
+ _4 = copy _12 as *mut [u8] (PtrToPtr);
+ nop;
nop;
nop;
nop;
nop;
StorageDead(_5);
- _3 = move _4 as *mut u8 (PtrToPtr);
+ _3 = copy _12 as *mut u8 (PtrToPtr);
nop;
StorageDead(_4);
StorageDead(_3);
- StorageDead(_1);
Expand Down
Loading

0 comments on commit 2124150

Please sign in to comment.