Skip to content

Commit

Permalink
Auto merge of #105323 - cjgillot:simplify-const-prop, r=davidtwco
Browse files Browse the repository at this point in the history
Perform SimplifyLocals before ConstProp.

MIR before `ConstProp` may have a lot of dead writes, this makes `ConstProp` do unnecessary work.

r? `@ghost`
  • Loading branch information
bors committed Jan 7, 2023
2 parents a2112fc + a48127f commit b1691f6
Show file tree
Hide file tree
Showing 81 changed files with 379 additions and 512 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/dead_store_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
bbs[block].statements[statement_index].make_nop();
}

crate::simplify::SimplifyLocals.run_pass(tcx, body)
crate::simplify::simplify_locals(body, tcx)
}

pub struct DeadStoreElimination;
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&multiple_return_terminators::MultipleReturnTerminators,
&instcombine::InstCombine,
&separate_const_switch::SeparateConstSwitch,
&simplify::SimplifyLocals::new("before-const-prop"),
//
// FIXME(#70073): This pass is responsible for both optimization as well as some lints.
&const_prop::ConstProp,
Expand All @@ -575,7 +576,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&o1(remove_noop_landing_pads::RemoveNoopLandingPads),
&o1(simplify::SimplifyCfg::new("final")),
&nrvo::RenameReturnPlace,
&simplify::SimplifyLocals,
&simplify::SimplifyLocals::new("final"),
&multiple_return_terminators::MultipleReturnTerminators,
&deduplicate_blocks::DeduplicateBlocks,
// Some cleanup necessary at least for LLVM and potentially other codegen backends.
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_mir_transform/src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,21 @@ fn save_unreachable_coverage(
));
}

pub struct SimplifyLocals;
pub struct SimplifyLocals {
label: String,
}

impl SimplifyLocals {
pub fn new(label: &str) -> SimplifyLocals {
SimplifyLocals { label: format!("SimplifyLocals-{}", label) }
}
}

impl<'tcx> MirPass<'tcx> for SimplifyLocals {
fn name(&self) -> &str {
&self.label
}

fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}
Expand Down Expand Up @@ -557,6 +569,7 @@ fn remove_unused_definitions(used_locals: &mut UsedLocals, body: &mut Body<'_>)

StatementKind::SetDiscriminant { ref place, .. }
| StatementKind::Deinit(ref place) => used_locals.is_used(place.local),
StatementKind::Nop => false,
_ => true,
};

Expand Down
9 changes: 6 additions & 3 deletions src/test/codegen/zst-offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn helper(_: usize) {
pub fn scalar_layout(s: &(u64, ())) {
// CHECK: getelementptr i8, {{.+}}, [[USIZE]] 8
let x = &s.1;
&x; // keep variable in an alloca
witness(&x); // keep variable in an alloca
}

// Check that we correctly generate a GEP for a ZST that is not included in ScalarPair layout
Expand All @@ -24,7 +24,7 @@ pub fn scalar_layout(s: &(u64, ())) {
pub fn scalarpair_layout(s: &(u64, u32, ())) {
// CHECK: getelementptr i8, {{.+}}, [[USIZE]] 12
let x = &s.2;
&x; // keep variable in an alloca
witness(&x); // keep variable in an alloca
}

#[repr(simd)]
Expand All @@ -36,5 +36,8 @@ pub struct U64x4(u64, u64, u64, u64);
pub fn vector_layout(s: &(U64x4, ())) {
// CHECK: getelementptr i8, {{.+}}, [[USIZE]] 32
let x = &s.1;
&x; // keep variable in an alloca
witness(&x); // keep variable in an alloca
}

#[inline(never)]
fn witness(_: &impl Sized) {}
4 changes: 2 additions & 2 deletions src/test/incremental/hashes/closure_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ pub fn add_parameter() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, optimized_mir, typeck")]
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, typeck")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, optimized_mir, typeck")]
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, typeck")]
#[rustc_clean(cfg="cfail6")]
pub fn add_parameter() {
let x = 0u32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ fn main() -> () {
StorageLive(_2); // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
_2 = const {alloc1: &&[(Option<i32>, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
// mir::Constant
// + span: $DIR/const_allocation.rs:8:5: 8:8
// + span: $DIR/const_allocation.rs:9:5: 9:8
// + literal: Const { ty: &&[(Option<i32>, &[&str])], val: Value(Scalar(alloc1)) }
_1 = (*_2); // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
StorageDead(_2); // scope 0 at $DIR/const_allocation.rs:+1:8: +1:9
StorageDead(_1); // scope 0 at $DIR/const_allocation.rs:+1:8: +1:9
nop; // scope 0 at $DIR/const_allocation.rs:+0:11: +2:2
_0 = const (); // scope 0 at $DIR/const_allocation.rs:+0:11: +2:2
return; // scope 0 at $DIR/const_allocation.rs:+2:2: +2:2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ fn main() -> () {
StorageLive(_2); // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
_2 = const {alloc1: &&[(Option<i32>, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
// mir::Constant
// + span: $DIR/const_allocation.rs:8:5: 8:8
// + span: $DIR/const_allocation.rs:9:5: 9:8
// + literal: Const { ty: &&[(Option<i32>, &[&str])], val: Value(Scalar(alloc1)) }
_1 = (*_2); // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
StorageDead(_2); // scope 0 at $DIR/const_allocation.rs:+1:8: +1:9
StorageDead(_1); // scope 0 at $DIR/const_allocation.rs:+1:8: +1:9
nop; // scope 0 at $DIR/const_allocation.rs:+0:11: +2:2
_0 = const (); // scope 0 at $DIR/const_allocation.rs:+0:11: +2:2
return; // scope 0 at $DIR/const_allocation.rs:+2:2: +2:2
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/mir-opt/const_allocation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// unit-test: ConstProp
// ignore-endian-big
// EMIT_MIR_FOR_EACH_BIT_WIDTH
static FOO: &[(Option<i32>, &[&str])] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ fn main() -> () {
StorageLive(_2); // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
_2 = const {alloc1: &&[(Option<i32>, &[&u8])]}; // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
// mir::Constant
// + span: $DIR/const_allocation2.rs:5:5: 5:8
// + span: $DIR/const_allocation2.rs:6:5: 6:8
// + literal: Const { ty: &&[(Option<i32>, &[&u8])], val: Value(Scalar(alloc1)) }
_1 = (*_2); // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
StorageDead(_2); // scope 0 at $DIR/const_allocation2.rs:+1:8: +1:9
StorageDead(_1); // scope 0 at $DIR/const_allocation2.rs:+1:8: +1:9
nop; // scope 0 at $DIR/const_allocation2.rs:+0:11: +2:2
_0 = const (); // scope 0 at $DIR/const_allocation2.rs:+0:11: +2:2
return; // scope 0 at $DIR/const_allocation2.rs:+2:2: +2:2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ fn main() -> () {
StorageLive(_2); // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
_2 = const {alloc1: &&[(Option<i32>, &[&u8])]}; // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
// mir::Constant
// + span: $DIR/const_allocation2.rs:5:5: 5:8
// + span: $DIR/const_allocation2.rs:6:5: 6:8
// + literal: Const { ty: &&[(Option<i32>, &[&u8])], val: Value(Scalar(alloc1)) }
_1 = (*_2); // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
StorageDead(_2); // scope 0 at $DIR/const_allocation2.rs:+1:8: +1:9
StorageDead(_1); // scope 0 at $DIR/const_allocation2.rs:+1:8: +1:9
nop; // scope 0 at $DIR/const_allocation2.rs:+0:11: +2:2
_0 = const (); // scope 0 at $DIR/const_allocation2.rs:+0:11: +2:2
return; // scope 0 at $DIR/const_allocation2.rs:+2:2: +2:2
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/mir-opt/const_allocation2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// unit-test: ConstProp
// ignore-endian-big
// EMIT_MIR_FOR_EACH_BIT_WIDTH
// EMIT_MIR const_allocation2.main.ConstProp.after.mir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ fn main() -> () {
StorageLive(_2); // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
_2 = const {alloc1: &&Packed}; // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
// mir::Constant
// + span: $DIR/const_allocation3.rs:5:5: 5:8
// + span: $DIR/const_allocation3.rs:6:5: 6:8
// + literal: Const { ty: &&Packed, val: Value(Scalar(alloc1)) }
_1 = (*_2); // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
StorageDead(_2); // scope 0 at $DIR/const_allocation3.rs:+1:8: +1:9
StorageDead(_1); // scope 0 at $DIR/const_allocation3.rs:+1:8: +1:9
nop; // scope 0 at $DIR/const_allocation3.rs:+0:11: +2:2
_0 = const (); // scope 0 at $DIR/const_allocation3.rs:+0:11: +2:2
return; // scope 0 at $DIR/const_allocation3.rs:+2:2: +2:2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ fn main() -> () {
StorageLive(_2); // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
_2 = const {alloc1: &&Packed}; // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
// mir::Constant
// + span: $DIR/const_allocation3.rs:5:5: 5:8
// + span: $DIR/const_allocation3.rs:6:5: 6:8
// + literal: Const { ty: &&Packed, val: Value(Scalar(alloc1)) }
_1 = (*_2); // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
StorageDead(_2); // scope 0 at $DIR/const_allocation3.rs:+1:8: +1:9
StorageDead(_1); // scope 0 at $DIR/const_allocation3.rs:+1:8: +1:9
nop; // scope 0 at $DIR/const_allocation3.rs:+0:11: +2:2
_0 = const (); // scope 0 at $DIR/const_allocation3.rs:+0:11: +2:2
return; // scope 0 at $DIR/const_allocation3.rs:+2:2: +2:2
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/mir-opt/const_allocation3.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// unit-test: ConstProp
// ignore-endian-big
// EMIT_MIR_FOR_EACH_BIT_WIDTH
// EMIT_MIR const_allocation3.main.ConstProp.after.mir
Expand Down
Loading

0 comments on commit b1691f6

Please sign in to comment.