Skip to content

Commit 70aac37

Browse files
Auto merge of #147079 - cjgillot:gvn-dbg-const, r=<try>
Replace SingleUseConsts by GVN.
2 parents 54a8a1d + 47b0479 commit 70aac37

File tree

140 files changed

+845
-764
lines changed

Some content is hidden

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

140 files changed

+845
-764
lines changed

compiler/rustc_middle/src/mir/consts.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,11 @@ impl<'tcx> Const<'tcx> {
495495
/// Return true if any evaluation of this constant always returns the same value,
496496
/// taking into account even pointer identity tests.
497497
pub fn is_deterministic(&self) -> bool {
498+
if self.ty().is_primitive() {
499+
// Primitive types hold no provenance, so their result is deterministic.
500+
return true;
501+
}
502+
498503
// Some constants may generate fresh allocations for pointers they contain,
499504
// so using the same constant twice can yield two different results.
500505
// Notably, valtrees purposefully generate new allocations.

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 249 additions & 152 deletions
Large diffs are not rendered by default.

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ declare_passes! {
191191
Final
192192
};
193193
mod simplify_comparison_integral : SimplifyComparisonIntegral;
194-
mod single_use_consts : SingleUseConsts;
195194
mod sroa : ScalarReplacementOfAggregates;
196195
mod strip_debuginfo : StripDebugInfo;
197196
mod unreachable_enum_branching : UnreachableEnumBranching;
@@ -709,7 +708,6 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
709708
&simplify::SimplifyLocals::AfterGVN,
710709
&match_branches::MatchBranchSimplification,
711710
&dataflow_const_prop::DataflowConstProp,
712-
&single_use_consts::SingleUseConsts,
713711
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
714712
&jump_threading::JumpThreading,
715713
&early_otherwise_branch::EarlyOtherwiseBranch,

compiler/rustc_mir_transform/src/single_use_consts.rs

Lines changed: 0 additions & 205 deletions
This file was deleted.

tests/mir-opt/building/match/sort_candidates.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
// Check specific cases of sorting candidates in match lowering.
1+
//! Check specific cases of sorting candidates in match lowering.
2+
//@ compile-flags: -Zmir-opt-level=0
23

34
// EMIT_MIR sort_candidates.constant_eq.SimplifyCfg-initial.after.mir
45
fn constant_eq(s: &str, b: bool) -> u32 {
56
// Check that we only test "a" once
67

78
// CHECK-LABEL: fn constant_eq(
89
// CHECK-NOT: const "a"
9-
// CHECK: {{_[0-9]+}} = const "a" as &[u8] (Transmute);
10+
// CHECK: eq({{.*}}, const "a")
1011
// CHECK-NOT: const "a"
1112
match (s, b) {
1213
("a", _) if true => 1,
@@ -23,11 +24,16 @@ fn disjoint_ranges(x: i32, b: bool) -> u32 {
2324
// other candidates.
2425

2526
// CHECK-LABEL: fn disjoint_ranges(
26-
// CHECK: debug b => _2;
27-
// CHECK: bb0: {
28-
// CHECK: switchInt(copy _2) -> [0: [[jump:bb.*]], otherwise: {{bb.*}}];
27+
// CHECK: debug b => [[b:_.*]];
28+
// CHECK: [[tmp:_.*]] = copy [[b]];
29+
// CHECK-NEXT: switchInt(move [[tmp]]) -> [0: [[jump:bb.*]], otherwise: {{bb.*}}];
2930
// CHECK: [[jump]]: {
31+
// CHECK-NEXT: StorageDead([[tmp]]);
32+
// CHECK-NEXT: goto -> [[jump3:bb.*]];
33+
// CHECK: [[jump3]]: {
3034
// CHECK-NEXT: _0 = const 3_u32;
35+
// CHECK-NEXT: goto -> [[jumpret:bb.*]];
36+
// CHECK: [[jumpret]]: {
3137
// CHECK-NEXT: return;
3238
match x {
3339
0..10 if b => 0,

tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff renamed to tests/mir-opt/const_debuginfo.main.GVN.diff

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `main` before SingleUseConsts
2-
+ // MIR for `main` after SingleUseConsts
1+
- // MIR for `main` before GVN
2+
+ // MIR for `main` after GVN
33

44
fn main() -> () {
55
let mut _0: ();
@@ -55,73 +55,78 @@
5555
}
5656

5757
bb0: {
58-
nop;
59-
- _1 = const 1_u8;
60-
nop;
61-
- _2 = const 2_u8;
62-
nop;
63-
- _3 = const 3_u8;
58+
- StorageLive(_1);
6459
+ nop;
60+
_1 = const 1_u8;
61+
- StorageLive(_2);
6562
+ nop;
63+
_2 = const 2_u8;
64+
- StorageLive(_3);
6665
+ nop;
66+
_3 = const 3_u8;
6767
StorageLive(_4);
6868
StorageLive(_5);
6969
StorageLive(_6);
70-
- _6 = const 1_u8;
71-
+ nop;
70+
- _6 = copy _1;
71+
+ _6 = const 1_u8;
7272
StorageLive(_7);
73-
- _7 = const 2_u8;
74-
- _5 = const 3_u8;
75-
+ nop;
76-
+ nop;
73+
- _7 = copy _2;
74+
- _5 = Add(move _6, move _7);
75+
+ _7 = const 2_u8;
76+
+ _5 = const 3_u8;
7777
StorageDead(_7);
7878
StorageDead(_6);
7979
StorageLive(_8);
80-
- _8 = const 3_u8;
81-
- _4 = const 6_u8;
82-
+ nop;
83-
+ nop;
80+
- _8 = copy _3;
81+
- _4 = Add(move _5, move _8);
82+
+ _8 = const 3_u8;
83+
+ _4 = const 6_u8;
8484
StorageDead(_8);
8585
StorageDead(_5);
8686
StorageLive(_9);
87-
- _9 = const "hello, world!";
88-
+ nop;
87+
_9 = const "hello, world!";
8988
StorageLive(_10);
9089
_10 = (const true, const false, const 123_u32);
9190
StorageLive(_11);
92-
- _11 = const Option::<u16>::Some(99_u16);
93-
+ nop;
91+
- _11 = Option::<u16>::Some(const 99_u16);
92+
+ _11 = const Option::<u16>::Some(99_u16);
9493
StorageLive(_12);
95-
- _12 = const Point {{ x: 32_u32, y: 32_u32 }};
96-
+ nop;
94+
- _12 = Point { x: const 32_u32, y: const 32_u32 };
95+
+ _12 = const Point {{ x: 32_u32, y: 32_u32 }};
9796
StorageLive(_13);
98-
nop;
99-
- _14 = const 32_u32;
97+
- StorageLive(_14);
98+
- _14 = copy (_12.0: u32);
10099
+ nop;
100+
+ _14 = const 32_u32;
101101
StorageLive(_15);
102-
- _15 = const 32_u32;
103-
- _13 = const 64_u32;
104-
+ nop;
105-
+ nop;
102+
- _15 = copy (_12.1: u32);
103+
- _13 = Add(move _14, move _15);
104+
+ _15 = const 32_u32;
105+
+ _13 = const 64_u32;
106106
StorageDead(_15);
107-
nop;
107+
- StorageDead(_14);
108+
+ nop;
108109
_0 = const ();
109110
StorageDead(_13);
110111
StorageDead(_12);
111112
StorageDead(_11);
112113
StorageDead(_10);
113114
StorageDead(_9);
114115
StorageDead(_4);
115-
nop;
116-
nop;
117-
nop;
116+
- StorageDead(_3);
117+
- StorageDead(_2);
118+
- StorageDead(_1);
119+
+ nop;
120+
+ nop;
121+
+ nop;
118122
return;
119123
}
120124
}
121125

122-
ALLOC0 (size: 8, align: 4) { .. }
123-
124-
ALLOC1 (size: 4, align: 2) { .. }
125-
126-
ALLOC2 (size: 13, align: 1) { .. }
126+
- ALLOC0 (size: 13, align: 1) { .. }
127+
+ ALLOC0 (size: 8, align: 4) { .. }
128+
+
129+
+ ALLOC1 (size: 4, align: 2) { .. }
130+
+
131+
+ ALLOC2 (size: 13, align: 1) { .. }
127132

0 commit comments

Comments
 (0)