Skip to content

Commit aa05458

Browse files
committed
Split gvn wide ptr tests.
1 parent cf3ad0e commit aa05458

8 files changed

+894
-799
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10421042

10431043
let mut from = operand.ty(self.local_decls, self.tcx);
10441044
let mut value = self.simplify_operand(operand, location)?;
1045+
if from == to {
1046+
return Some(value);
1047+
}
10451048

10461049
if let CastKind::PointerCoercion(ReifyFnPointer | ClosureFnPointer(_)) = kind {
10471050
// Each reification of a generic fn may get a different pointer.

tests/mir-opt/gvn.rs

+43-13
Original file line numberDiff line numberDiff line change
@@ -644,25 +644,53 @@ fn constant_index_overflow<T: Copy>(x: &[T]) {
644644
opaque(b)
645645
}
646646

647-
fn wide_ptr_ops() {
647+
/// Check that we do not attempt to simplify anything when there is provenance.
648+
fn wide_ptr_provenance() {
649+
// CHECK-LABEL: fn wide_ptr_provenance(
648650
let a: *const dyn Send = &1 as &dyn Send;
649651
let b: *const dyn Send = &1 as &dyn Send;
650-
let _val = a == b;
651-
let _val = a != b;
652-
let _val = a < b;
653-
let _val = a <= b;
654-
let _val = a > b;
655-
let _val = a >= b;
652+
653+
// CHECK: [[eqp:_.*]] = Eq([[a:_.*]], [[b:_.*]]);
654+
// CHECK: opaque::<bool>(move [[eqp]])
655+
opaque(a == b);
656+
// CHECK: [[nep:_.*]] = Ne([[a]], [[b]]);
657+
// CHECK: opaque::<bool>(move [[nep]])
658+
opaque(a != b);
659+
// CHECK: [[ltp:_.*]] = Lt([[a]], [[b]]);
660+
// CHECK: opaque::<bool>(move [[ltp]])
661+
opaque(a < b);
662+
// CHECK: [[lep:_.*]] = Le([[a]], [[b]]);
663+
// CHECK: opaque::<bool>(move [[lep]])
664+
opaque(a <= b);
665+
// CHECK: [[gtp:_.*]] = Gt([[a]], [[b]]);
666+
// CHECK: opaque::<bool>(move [[gtp]])
667+
opaque(a > b);
668+
// CHECK: [[gep:_.*]] = Ge([[a]], [[b]]);
669+
// CHECK: opaque::<bool>(move [[gep]])
670+
opaque(a >= b);
671+
}
672+
673+
/// Check that we do simplify when there is no provenance, and do not ICE.
674+
fn wide_ptr_integer() {
675+
// CHECK-LABEL: fn wide_ptr_integer(
676+
// CHECK: debug a => [[a:_.*]];
677+
// CHECK: debug b => [[b:_.*]];
656678

657679
let a: *const [u8] = unsafe { transmute((1usize, 1usize)) };
658680
let b: *const [u8] = unsafe { transmute((1usize, 2usize)) };
659681

660-
opaque(!(a == b));
682+
// CHECK: opaque::<bool>(const false)
683+
opaque(a == b);
684+
// CHECK: opaque::<bool>(const true)
661685
opaque(a != b);
662-
opaque(a <= b);
686+
// CHECK: opaque::<bool>(const true)
663687
opaque(a < b);
664-
opaque(!(a >= b));
665-
opaque(!(a > b));
688+
// CHECK: opaque::<bool>(const true)
689+
opaque(a <= b);
690+
// CHECK: opaque::<bool>(const false)
691+
opaque(a > b);
692+
// CHECK: opaque::<bool>(const false)
693+
opaque(a >= b);
666694
}
667695

668696
fn main() {
@@ -685,7 +713,8 @@ fn main() {
685713
fn_pointers();
686714
indirect_static();
687715
constant_index_overflow(&[5, 3]);
688-
wide_ptr_ops();
716+
wide_ptr_provenance();
717+
wide_ptr_integer();
689718
}
690719

691720
#[inline(never)]
@@ -714,4 +743,5 @@ fn identity<T>(x: T) -> T {
714743
// EMIT_MIR gvn.fn_pointers.GVN.diff
715744
// EMIT_MIR gvn.indirect_static.GVN.diff
716745
// EMIT_MIR gvn.constant_index_overflow.GVN.diff
717-
// EMIT_MIR gvn.wide_ptr_ops.GVN.diff
746+
// EMIT_MIR gvn.wide_ptr_provenance.GVN.diff
747+
// EMIT_MIR gvn.wide_ptr_integer.GVN.diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
- // MIR for `wide_ptr_integer` before GVN
2+
+ // MIR for `wide_ptr_integer` after GVN
3+
4+
fn wide_ptr_integer() -> () {
5+
let mut _0: ();
6+
let _1: *const [u8];
7+
let mut _2: (usize, usize);
8+
let mut _4: (usize, usize);
9+
let _5: ();
10+
let mut _6: bool;
11+
let mut _7: *const [u8];
12+
let mut _8: *const [u8];
13+
let _9: ();
14+
let mut _10: bool;
15+
let mut _11: *const [u8];
16+
let mut _12: *const [u8];
17+
let _13: ();
18+
let mut _14: bool;
19+
let mut _15: *const [u8];
20+
let mut _16: *const [u8];
21+
let _17: ();
22+
let mut _18: bool;
23+
let mut _19: *const [u8];
24+
let mut _20: *const [u8];
25+
let _21: ();
26+
let mut _22: bool;
27+
let mut _23: *const [u8];
28+
let mut _24: *const [u8];
29+
let _25: ();
30+
let mut _26: bool;
31+
let mut _27: *const [u8];
32+
let mut _28: *const [u8];
33+
scope 1 {
34+
debug a => _1;
35+
let _3: *const [u8];
36+
scope 3 {
37+
debug b => _3;
38+
}
39+
scope 4 {
40+
}
41+
}
42+
scope 2 {
43+
}
44+
45+
bb0: {
46+
- StorageLive(_1);
47+
+ nop;
48+
StorageLive(_2);
49+
- _2 = (const 1_usize, const 1_usize);
50+
- _1 = move _2 as *const [u8] (Transmute);
51+
+ _2 = const (1_usize, 1_usize);
52+
+ _1 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
53+
StorageDead(_2);
54+
- StorageLive(_3);
55+
+ nop;
56+
StorageLive(_4);
57+
- _4 = (const 1_usize, const 2_usize);
58+
- _3 = move _4 as *const [u8] (Transmute);
59+
+ _4 = const (1_usize, 2_usize);
60+
+ _3 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
61+
StorageDead(_4);
62+
StorageLive(_5);
63+
StorageLive(_6);
64+
StorageLive(_7);
65+
- _7 = _1;
66+
+ _7 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
67+
StorageLive(_8);
68+
- _8 = _3;
69+
- _6 = Eq(move _7, move _8);
70+
+ _8 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
71+
+ _6 = const false;
72+
StorageDead(_8);
73+
StorageDead(_7);
74+
- _5 = opaque::<bool>(move _6) -> [return: bb1, unwind unreachable];
75+
+ _5 = opaque::<bool>(const false) -> [return: bb1, unwind unreachable];
76+
}
77+
78+
bb1: {
79+
StorageDead(_6);
80+
StorageDead(_5);
81+
StorageLive(_9);
82+
StorageLive(_10);
83+
StorageLive(_11);
84+
- _11 = _1;
85+
+ _11 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
86+
StorageLive(_12);
87+
- _12 = _3;
88+
- _10 = Ne(move _11, move _12);
89+
+ _12 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
90+
+ _10 = const true;
91+
StorageDead(_12);
92+
StorageDead(_11);
93+
- _9 = opaque::<bool>(move _10) -> [return: bb2, unwind unreachable];
94+
+ _9 = opaque::<bool>(const true) -> [return: bb2, unwind unreachable];
95+
}
96+
97+
bb2: {
98+
StorageDead(_10);
99+
StorageDead(_9);
100+
StorageLive(_13);
101+
StorageLive(_14);
102+
StorageLive(_15);
103+
- _15 = _1;
104+
+ _15 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
105+
StorageLive(_16);
106+
- _16 = _3;
107+
- _14 = Lt(move _15, move _16);
108+
+ _16 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
109+
+ _14 = const true;
110+
StorageDead(_16);
111+
StorageDead(_15);
112+
- _13 = opaque::<bool>(move _14) -> [return: bb3, unwind unreachable];
113+
+ _13 = opaque::<bool>(const true) -> [return: bb3, unwind unreachable];
114+
}
115+
116+
bb3: {
117+
StorageDead(_14);
118+
StorageDead(_13);
119+
StorageLive(_17);
120+
StorageLive(_18);
121+
StorageLive(_19);
122+
- _19 = _1;
123+
+ _19 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
124+
StorageLive(_20);
125+
- _20 = _3;
126+
- _18 = Le(move _19, move _20);
127+
+ _20 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
128+
+ _18 = const true;
129+
StorageDead(_20);
130+
StorageDead(_19);
131+
- _17 = opaque::<bool>(move _18) -> [return: bb4, unwind unreachable];
132+
+ _17 = opaque::<bool>(const true) -> [return: bb4, unwind unreachable];
133+
}
134+
135+
bb4: {
136+
StorageDead(_18);
137+
StorageDead(_17);
138+
StorageLive(_21);
139+
StorageLive(_22);
140+
StorageLive(_23);
141+
- _23 = _1;
142+
+ _23 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
143+
StorageLive(_24);
144+
- _24 = _3;
145+
- _22 = Gt(move _23, move _24);
146+
+ _24 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
147+
+ _22 = const false;
148+
StorageDead(_24);
149+
StorageDead(_23);
150+
- _21 = opaque::<bool>(move _22) -> [return: bb5, unwind unreachable];
151+
+ _21 = opaque::<bool>(const false) -> [return: bb5, unwind unreachable];
152+
}
153+
154+
bb5: {
155+
StorageDead(_22);
156+
StorageDead(_21);
157+
StorageLive(_25);
158+
StorageLive(_26);
159+
StorageLive(_27);
160+
- _27 = _1;
161+
+ _27 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
162+
StorageLive(_28);
163+
- _28 = _3;
164+
- _26 = Ge(move _27, move _28);
165+
+ _28 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
166+
+ _26 = const false;
167+
StorageDead(_28);
168+
StorageDead(_27);
169+
- _25 = opaque::<bool>(move _26) -> [return: bb6, unwind unreachable];
170+
+ _25 = opaque::<bool>(const false) -> [return: bb6, unwind unreachable];
171+
}
172+
173+
bb6: {
174+
StorageDead(_26);
175+
StorageDead(_25);
176+
_0 = const ();
177+
- StorageDead(_3);
178+
- StorageDead(_1);
179+
+ nop;
180+
+ nop;
181+
return;
182+
}
183+
+ }
184+
+
185+
+ ALLOC1 (size: 16, align: 8) {
186+
+ 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 │ ................
187+
+ }
188+
+
189+
+ ALLOC0 (size: 16, align: 8) {
190+
+ 01 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 │ ................
191+
}
192+

0 commit comments

Comments
 (0)