Skip to content

Commit b1691f6

Browse files
committed
Auto merge of rust-lang#105323 - cjgillot:simplify-const-prop, r=davidtwco
Perform SimplifyLocals before ConstProp. MIR before `ConstProp` may have a lot of dead writes, this makes `ConstProp` do unnecessary work. r? `@ghost`
2 parents a2112fc + a48127f commit b1691f6

File tree

81 files changed

+379
-512
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+379
-512
lines changed

compiler/rustc_mir_transform/src/dead_store_elimination.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
7171
bbs[block].statements[statement_index].make_nop();
7272
}
7373

74-
crate::simplify::SimplifyLocals.run_pass(tcx, body)
74+
crate::simplify::simplify_locals(body, tcx)
7575
}
7676

7777
pub struct DeadStoreElimination;

compiler/rustc_mir_transform/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
557557
&multiple_return_terminators::MultipleReturnTerminators,
558558
&instcombine::InstCombine,
559559
&separate_const_switch::SeparateConstSwitch,
560+
&simplify::SimplifyLocals::new("before-const-prop"),
560561
//
561562
// FIXME(#70073): This pass is responsible for both optimization as well as some lints.
562563
&const_prop::ConstProp,
@@ -575,7 +576,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
575576
&o1(remove_noop_landing_pads::RemoveNoopLandingPads),
576577
&o1(simplify::SimplifyCfg::new("final")),
577578
&nrvo::RenameReturnPlace,
578-
&simplify::SimplifyLocals,
579+
&simplify::SimplifyLocals::new("final"),
579580
&multiple_return_terminators::MultipleReturnTerminators,
580581
&deduplicate_blocks::DeduplicateBlocks,
581582
// Some cleanup necessary at least for LLVM and potentially other codegen backends.

compiler/rustc_mir_transform/src/simplify.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,21 @@ fn save_unreachable_coverage(
379379
));
380380
}
381381

382-
pub struct SimplifyLocals;
382+
pub struct SimplifyLocals {
383+
label: String,
384+
}
385+
386+
impl SimplifyLocals {
387+
pub fn new(label: &str) -> SimplifyLocals {
388+
SimplifyLocals { label: format!("SimplifyLocals-{}", label) }
389+
}
390+
}
383391

384392
impl<'tcx> MirPass<'tcx> for SimplifyLocals {
393+
fn name(&self) -> &str {
394+
&self.label
395+
}
396+
385397
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
386398
sess.mir_opt_level() > 0
387399
}
@@ -557,6 +569,7 @@ fn remove_unused_definitions(used_locals: &mut UsedLocals, body: &mut Body<'_>)
557569

558570
StatementKind::SetDiscriminant { ref place, .. }
559571
| StatementKind::Deinit(ref place) => used_locals.is_used(place.local),
572+
StatementKind::Nop => false,
560573
_ => true,
561574
};
562575

src/test/codegen/zst-offset.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn helper(_: usize) {
1515
pub fn scalar_layout(s: &(u64, ())) {
1616
// CHECK: getelementptr i8, {{.+}}, [[USIZE]] 8
1717
let x = &s.1;
18-
&x; // keep variable in an alloca
18+
witness(&x); // keep variable in an alloca
1919
}
2020

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

3030
#[repr(simd)]
@@ -36,5 +36,8 @@ pub struct U64x4(u64, u64, u64, u64);
3636
pub fn vector_layout(s: &(U64x4, ())) {
3737
// CHECK: getelementptr i8, {{.+}}, [[USIZE]] 32
3838
let x = &s.1;
39-
&x; // keep variable in an alloca
39+
witness(&x); // keep variable in an alloca
4040
}
41+
42+
#[inline(never)]
43+
fn witness(_: &impl Sized) {}

src/test/incremental/hashes/closure_expressions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ pub fn add_parameter() {
4242
}
4343

4444
#[cfg(not(any(cfail1,cfail4)))]
45-
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, optimized_mir, typeck")]
45+
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, typeck")]
4646
#[rustc_clean(cfg="cfail3")]
47-
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, optimized_mir, typeck")]
47+
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, typeck")]
4848
#[rustc_clean(cfg="cfail6")]
4949
pub fn add_parameter() {
5050
let x = 0u32;

src/test/mir-opt/const_allocation.main.ConstProp.after.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ fn main() -> () {
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
1111
_2 = const {alloc1: &&[(Option<i32>, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
1212
// mir::Constant
13-
// + span: $DIR/const_allocation.rs:8:5: 8:8
13+
// + span: $DIR/const_allocation.rs:9:5: 9:8
1414
// + literal: Const { ty: &&[(Option<i32>, &[&str])], val: Value(Scalar(alloc1)) }
1515
_1 = (*_2); // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
1616
StorageDead(_2); // scope 0 at $DIR/const_allocation.rs:+1:8: +1:9
1717
StorageDead(_1); // scope 0 at $DIR/const_allocation.rs:+1:8: +1:9
18-
nop; // scope 0 at $DIR/const_allocation.rs:+0:11: +2:2
18+
_0 = const (); // scope 0 at $DIR/const_allocation.rs:+0:11: +2:2
1919
return; // scope 0 at $DIR/const_allocation.rs:+2:2: +2:2
2020
}
2121
}

src/test/mir-opt/const_allocation.main.ConstProp.after.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ fn main() -> () {
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
1111
_2 = const {alloc1: &&[(Option<i32>, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
1212
// mir::Constant
13-
// + span: $DIR/const_allocation.rs:8:5: 8:8
13+
// + span: $DIR/const_allocation.rs:9:5: 9:8
1414
// + literal: Const { ty: &&[(Option<i32>, &[&str])], val: Value(Scalar(alloc1)) }
1515
_1 = (*_2); // scope 0 at $DIR/const_allocation.rs:+1:5: +1:8
1616
StorageDead(_2); // scope 0 at $DIR/const_allocation.rs:+1:8: +1:9
1717
StorageDead(_1); // scope 0 at $DIR/const_allocation.rs:+1:8: +1:9
18-
nop; // scope 0 at $DIR/const_allocation.rs:+0:11: +2:2
18+
_0 = const (); // scope 0 at $DIR/const_allocation.rs:+0:11: +2:2
1919
return; // scope 0 at $DIR/const_allocation.rs:+2:2: +2:2
2020
}
2121
}

src/test/mir-opt/const_allocation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// unit-test: ConstProp
12
// ignore-endian-big
23
// EMIT_MIR_FOR_EACH_BIT_WIDTH
34
static FOO: &[(Option<i32>, &[&str])] =

src/test/mir-opt/const_allocation2.main.ConstProp.after.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ fn main() -> () {
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
1111
_2 = const {alloc1: &&[(Option<i32>, &[&u8])]}; // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
1212
// mir::Constant
13-
// + span: $DIR/const_allocation2.rs:5:5: 5:8
13+
// + span: $DIR/const_allocation2.rs:6:5: 6:8
1414
// + literal: Const { ty: &&[(Option<i32>, &[&u8])], val: Value(Scalar(alloc1)) }
1515
_1 = (*_2); // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
1616
StorageDead(_2); // scope 0 at $DIR/const_allocation2.rs:+1:8: +1:9
1717
StorageDead(_1); // scope 0 at $DIR/const_allocation2.rs:+1:8: +1:9
18-
nop; // scope 0 at $DIR/const_allocation2.rs:+0:11: +2:2
18+
_0 = const (); // scope 0 at $DIR/const_allocation2.rs:+0:11: +2:2
1919
return; // scope 0 at $DIR/const_allocation2.rs:+2:2: +2:2
2020
}
2121
}

src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ fn main() -> () {
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
1111
_2 = const {alloc1: &&[(Option<i32>, &[&u8])]}; // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
1212
// mir::Constant
13-
// + span: $DIR/const_allocation2.rs:5:5: 5:8
13+
// + span: $DIR/const_allocation2.rs:6:5: 6:8
1414
// + literal: Const { ty: &&[(Option<i32>, &[&u8])], val: Value(Scalar(alloc1)) }
1515
_1 = (*_2); // scope 0 at $DIR/const_allocation2.rs:+1:5: +1:8
1616
StorageDead(_2); // scope 0 at $DIR/const_allocation2.rs:+1:8: +1:9
1717
StorageDead(_1); // scope 0 at $DIR/const_allocation2.rs:+1:8: +1:9
18-
nop; // scope 0 at $DIR/const_allocation2.rs:+0:11: +2:2
18+
_0 = const (); // scope 0 at $DIR/const_allocation2.rs:+0:11: +2:2
1919
return; // scope 0 at $DIR/const_allocation2.rs:+2:2: +2:2
2020
}
2121
}

src/test/mir-opt/const_allocation2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// unit-test: ConstProp
12
// ignore-endian-big
23
// EMIT_MIR_FOR_EACH_BIT_WIDTH
34
// EMIT_MIR const_allocation2.main.ConstProp.after.mir

src/test/mir-opt/const_allocation3.main.ConstProp.after.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ fn main() -> () {
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
1111
_2 = const {alloc1: &&Packed}; // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
1212
// mir::Constant
13-
// + span: $DIR/const_allocation3.rs:5:5: 5:8
13+
// + span: $DIR/const_allocation3.rs:6:5: 6:8
1414
// + literal: Const { ty: &&Packed, val: Value(Scalar(alloc1)) }
1515
_1 = (*_2); // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
1616
StorageDead(_2); // scope 0 at $DIR/const_allocation3.rs:+1:8: +1:9
1717
StorageDead(_1); // scope 0 at $DIR/const_allocation3.rs:+1:8: +1:9
18-
nop; // scope 0 at $DIR/const_allocation3.rs:+0:11: +2:2
18+
_0 = const (); // scope 0 at $DIR/const_allocation3.rs:+0:11: +2:2
1919
return; // scope 0 at $DIR/const_allocation3.rs:+2:2: +2:2
2020
}
2121
}

src/test/mir-opt/const_allocation3.main.ConstProp.after.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ fn main() -> () {
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
1111
_2 = const {alloc1: &&Packed}; // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
1212
// mir::Constant
13-
// + span: $DIR/const_allocation3.rs:5:5: 5:8
13+
// + span: $DIR/const_allocation3.rs:6:5: 6:8
1414
// + literal: Const { ty: &&Packed, val: Value(Scalar(alloc1)) }
1515
_1 = (*_2); // scope 0 at $DIR/const_allocation3.rs:+1:5: +1:8
1616
StorageDead(_2); // scope 0 at $DIR/const_allocation3.rs:+1:8: +1:9
1717
StorageDead(_1); // scope 0 at $DIR/const_allocation3.rs:+1:8: +1:9
18-
nop; // scope 0 at $DIR/const_allocation3.rs:+0:11: +2:2
18+
_0 = const (); // scope 0 at $DIR/const_allocation3.rs:+0:11: +2:2
1919
return; // scope 0 at $DIR/const_allocation3.rs:+2:2: +2:2
2020
}
2121
}

src/test/mir-opt/const_allocation3.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// unit-test: ConstProp
12
// ignore-endian-big
23
// EMIT_MIR_FOR_EACH_BIT_WIDTH
34
// EMIT_MIR const_allocation3.main.ConstProp.after.mir

0 commit comments

Comments
 (0)