Skip to content

Commit

Permalink
Split gvn wide ptr tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jan 20, 2024
1 parent 58af3cb commit e582721
Show file tree
Hide file tree
Showing 8 changed files with 894 additions and 799 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {

let mut from = operand.ty(self.local_decls, self.tcx);
let mut value = self.simplify_operand(operand, location)?;
if from == to {
return Some(value);
}

if let CastKind::PointerCoercion(ReifyFnPointer | ClosureFnPointer(_)) = kind {
// Each reification of a generic fn may get a different pointer.
Expand Down
56 changes: 43 additions & 13 deletions tests/mir-opt/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,25 +644,53 @@ fn constant_index_overflow<T: Copy>(x: &[T]) {
opaque(b)
}

fn wide_ptr_ops() {
/// Check that we do not attempt to simplify anything when there is provenance.
fn wide_ptr_provenance() {
// CHECK-LABEL: fn wide_ptr_provenance(
let a: *const dyn Send = &1 as &dyn Send;
let b: *const dyn Send = &1 as &dyn Send;
let _val = a == b;
let _val = a != b;
let _val = a < b;
let _val = a <= b;
let _val = a > b;
let _val = a >= b;

// CHECK: [[eqp:_.*]] = Eq([[a:_.*]], [[b:_.*]]);
// CHECK: opaque::<bool>(move [[eqp]])
opaque(a == b);
// CHECK: [[nep:_.*]] = Ne([[a]], [[b]]);
// CHECK: opaque::<bool>(move [[nep]])
opaque(a != b);
// CHECK: [[ltp:_.*]] = Lt([[a]], [[b]]);
// CHECK: opaque::<bool>(move [[ltp]])
opaque(a < b);
// CHECK: [[lep:_.*]] = Le([[a]], [[b]]);
// CHECK: opaque::<bool>(move [[lep]])
opaque(a <= b);
// CHECK: [[gtp:_.*]] = Gt([[a]], [[b]]);
// CHECK: opaque::<bool>(move [[gtp]])
opaque(a > b);
// CHECK: [[gep:_.*]] = Ge([[a]], [[b]]);
// CHECK: opaque::<bool>(move [[gep]])
opaque(a >= b);
}

/// Check that we do simplify when there is no provenance, and do not ICE.
fn wide_ptr_integer() {
// CHECK-LABEL: fn wide_ptr_integer(
// CHECK: debug a => [[a:_.*]];
// CHECK: debug b => [[b:_.*]];

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

opaque(!(a == b));
// CHECK: opaque::<bool>(const false)
opaque(a == b);
// CHECK: opaque::<bool>(const true)
opaque(a != b);
opaque(a <= b);
// CHECK: opaque::<bool>(const true)
opaque(a < b);
opaque(!(a >= b));
opaque(!(a > b));
// CHECK: opaque::<bool>(const true)
opaque(a <= b);
// CHECK: opaque::<bool>(const false)
opaque(a > b);
// CHECK: opaque::<bool>(const false)
opaque(a >= b);
}

fn main() {
Expand All @@ -685,7 +713,8 @@ fn main() {
fn_pointers();
indirect_static();
constant_index_overflow(&[5, 3]);
wide_ptr_ops();
wide_ptr_provenance();
wide_ptr_integer();
}

#[inline(never)]
Expand Down Expand Up @@ -714,4 +743,5 @@ fn identity<T>(x: T) -> T {
// EMIT_MIR gvn.fn_pointers.GVN.diff
// EMIT_MIR gvn.indirect_static.GVN.diff
// EMIT_MIR gvn.constant_index_overflow.GVN.diff
// EMIT_MIR gvn.wide_ptr_ops.GVN.diff
// EMIT_MIR gvn.wide_ptr_provenance.GVN.diff
// EMIT_MIR gvn.wide_ptr_integer.GVN.diff
192 changes: 192 additions & 0 deletions tests/mir-opt/gvn.wide_ptr_integer.GVN.panic-abort.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
- // MIR for `wide_ptr_integer` before GVN
+ // MIR for `wide_ptr_integer` after GVN

fn wide_ptr_integer() -> () {
let mut _0: ();
let _1: *const [u8];
let mut _2: (usize, usize);
let mut _4: (usize, usize);
let _5: ();
let mut _6: bool;
let mut _7: *const [u8];
let mut _8: *const [u8];
let _9: ();
let mut _10: bool;
let mut _11: *const [u8];
let mut _12: *const [u8];
let _13: ();
let mut _14: bool;
let mut _15: *const [u8];
let mut _16: *const [u8];
let _17: ();
let mut _18: bool;
let mut _19: *const [u8];
let mut _20: *const [u8];
let _21: ();
let mut _22: bool;
let mut _23: *const [u8];
let mut _24: *const [u8];
let _25: ();
let mut _26: bool;
let mut _27: *const [u8];
let mut _28: *const [u8];
scope 1 {
debug a => _1;
let _3: *const [u8];
scope 3 {
debug b => _3;
}
scope 4 {
}
}
scope 2 {
}

bb0: {
- StorageLive(_1);
+ nop;
StorageLive(_2);
- _2 = (const 1_usize, const 1_usize);
- _1 = move _2 as *const [u8] (Transmute);
+ _2 = const (1_usize, 1_usize);
+ _1 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
StorageDead(_2);
- StorageLive(_3);
+ nop;
StorageLive(_4);
- _4 = (const 1_usize, const 2_usize);
- _3 = move _4 as *const [u8] (Transmute);
+ _4 = const (1_usize, 2_usize);
+ _3 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
StorageDead(_4);
StorageLive(_5);
StorageLive(_6);
StorageLive(_7);
- _7 = _1;
+ _7 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
StorageLive(_8);
- _8 = _3;
- _6 = Eq(move _7, move _8);
+ _8 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
+ _6 = const false;
StorageDead(_8);
StorageDead(_7);
- _5 = opaque::<bool>(move _6) -> [return: bb1, unwind unreachable];
+ _5 = opaque::<bool>(const false) -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_6);
StorageDead(_5);
StorageLive(_9);
StorageLive(_10);
StorageLive(_11);
- _11 = _1;
+ _11 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
StorageLive(_12);
- _12 = _3;
- _10 = Ne(move _11, move _12);
+ _12 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
+ _10 = const true;
StorageDead(_12);
StorageDead(_11);
- _9 = opaque::<bool>(move _10) -> [return: bb2, unwind unreachable];
+ _9 = opaque::<bool>(const true) -> [return: bb2, unwind unreachable];
}

bb2: {
StorageDead(_10);
StorageDead(_9);
StorageLive(_13);
StorageLive(_14);
StorageLive(_15);
- _15 = _1;
+ _15 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
StorageLive(_16);
- _16 = _3;
- _14 = Lt(move _15, move _16);
+ _16 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
+ _14 = const true;
StorageDead(_16);
StorageDead(_15);
- _13 = opaque::<bool>(move _14) -> [return: bb3, unwind unreachable];
+ _13 = opaque::<bool>(const true) -> [return: bb3, unwind unreachable];
}

bb3: {
StorageDead(_14);
StorageDead(_13);
StorageLive(_17);
StorageLive(_18);
StorageLive(_19);
- _19 = _1;
+ _19 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
StorageLive(_20);
- _20 = _3;
- _18 = Le(move _19, move _20);
+ _20 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
+ _18 = const true;
StorageDead(_20);
StorageDead(_19);
- _17 = opaque::<bool>(move _18) -> [return: bb4, unwind unreachable];
+ _17 = opaque::<bool>(const true) -> [return: bb4, unwind unreachable];
}

bb4: {
StorageDead(_18);
StorageDead(_17);
StorageLive(_21);
StorageLive(_22);
StorageLive(_23);
- _23 = _1;
+ _23 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
StorageLive(_24);
- _24 = _3;
- _22 = Gt(move _23, move _24);
+ _24 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
+ _22 = const false;
StorageDead(_24);
StorageDead(_23);
- _21 = opaque::<bool>(move _22) -> [return: bb5, unwind unreachable];
+ _21 = opaque::<bool>(const false) -> [return: bb5, unwind unreachable];
}

bb5: {
StorageDead(_22);
StorageDead(_21);
StorageLive(_25);
StorageLive(_26);
StorageLive(_27);
- _27 = _1;
+ _27 = const Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [u8];
StorageLive(_28);
- _28 = _3;
- _26 = Ge(move _27, move _28);
+ _28 = const Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [u8];
+ _26 = const false;
StorageDead(_28);
StorageDead(_27);
- _25 = opaque::<bool>(move _26) -> [return: bb6, unwind unreachable];
+ _25 = opaque::<bool>(const false) -> [return: bb6, unwind unreachable];
}

bb6: {
StorageDead(_26);
StorageDead(_25);
_0 = const ();
- StorageDead(_3);
- StorageDead(_1);
+ nop;
+ nop;
return;
}
+ }
+
+ ALLOC1 (size: 16, align: 8) {
+ 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 │ ................
+ }
+
+ ALLOC0 (size: 16, align: 8) {
+ 01 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 │ ................
}

Loading

0 comments on commit e582721

Please sign in to comment.