Add tests.

This commit is contained in:
Camille GILLOT 2024-04-07 16:23:16 +00:00
parent 8cf135e191
commit 5d144e301c
12 changed files with 334 additions and 11 deletions

View File

@ -0,0 +1,23 @@
- // MIR for `borrowed` before CopyProp
+ // MIR for `borrowed` after CopyProp
fn borrowed(_1: u32) -> bool {
let mut _0: bool;
let mut _2: u32;
let mut _3: &u32;
bb0: {
_2 = _1;
_3 = &_1;
_0 = opaque::<&u32>(_3) -> [return: bb1, unwind unreachable];
}
bb1: {
_0 = opaque::<u32>(_2) -> [return: bb2, unwind unreachable];
}
bb2: {
return;
}
}

View File

@ -0,0 +1,23 @@
- // MIR for `borrowed` before CopyProp
+ // MIR for `borrowed` after CopyProp
fn borrowed(_1: u32) -> bool {
let mut _0: bool;
let mut _2: u32;
let mut _3: &u32;
bb0: {
_2 = _1;
_3 = &_1;
_0 = opaque::<&u32>(_3) -> [return: bb1, unwind continue];
}
bb1: {
_0 = opaque::<u32>(_2) -> [return: bb2, unwind continue];
}
bb2: {
return;
}
}

View File

@ -1,7 +1,7 @@
- // MIR for `f` before CopyProp
+ // MIR for `f` after CopyProp
- // MIR for `compare_address` before CopyProp
+ // MIR for `compare_address` after CopyProp
fn f() -> bool {
fn compare_address() -> bool {
let mut _0: bool;
let mut _1: u8;
let mut _2: &u8;

View File

@ -1,7 +1,7 @@
- // MIR for `f` before CopyProp
+ // MIR for `f` after CopyProp
- // MIR for `compare_address` before CopyProp
+ // MIR for `compare_address` after CopyProp
fn f() -> bool {
fn compare_address() -> bool {
let mut _0: bool;
let mut _1: u8;
let mut _2: &u8;

View File

@ -0,0 +1,23 @@
- // MIR for `non_freeze` before CopyProp
+ // MIR for `non_freeze` after CopyProp
fn non_freeze(_1: T) -> bool {
let mut _0: bool;
let mut _2: T;
let mut _3: &T;
bb0: {
_2 = _1;
_3 = &_1;
_0 = opaque::<&T>(_3) -> [return: bb1, unwind unreachable];
}
bb1: {
_0 = opaque::<T>(_2) -> [return: bb2, unwind unreachable];
}
bb2: {
return;
}
}

View File

@ -0,0 +1,23 @@
- // MIR for `non_freeze` before CopyProp
+ // MIR for `non_freeze` after CopyProp
fn non_freeze(_1: T) -> bool {
let mut _0: bool;
let mut _2: T;
let mut _3: &T;
bb0: {
_2 = _1;
_3 = &_1;
_0 = opaque::<&T>(_3) -> [return: bb1, unwind continue];
}
bb1: {
_0 = opaque::<T>(_2) -> [return: bb2, unwind continue];
}
bb2: {
return;
}
}

View File

@ -1,4 +1,3 @@
// skip-filecheck
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
//@ unit-test: CopyProp
@ -14,7 +13,16 @@ fn cmp_ref(a: &u8, b: &u8) -> bool {
}
#[custom_mir(dialect = "analysis", phase = "post-cleanup")]
fn f() -> bool {
fn compare_address() -> bool {
// CHECK-LABEL: fn compare_address(
// CHECK: bb0: {
// CHECK-NEXT: _1 = const 5_u8;
// CHECK-NEXT: _2 = &_1;
// CHECK-NEXT: _3 = _1;
// CHECK-NEXT: _4 = &_3;
// CHECK-NEXT: _0 = cmp_ref(_2, _4)
// CHECK: bb1: {
// CHECK-NEXT: _0 = opaque::<u8>(_3)
mir!(
{
let a = 5_u8;
@ -34,8 +42,60 @@ fn f() -> bool {
)
}
fn main() {
assert!(!f());
#[custom_mir(dialect = "analysis", phase = "post-cleanup")]
fn borrowed(x: u32) -> bool {
// CHECK-LABEL: fn borrowed(
// CHECK: bb0: {
// CHECK-NEXT: _2 = _1;
// CHECK-NEXT: _3 = &_1;
// CHECK-NEXT: _0 = opaque::<&u32>(_3)
// CHECK: bb1: {
// CHECK-NEXT: _0 = opaque::<u32>(_2)
mir!(
{
let a = x;
let r1 = &x;
Call(RET = opaque(r1), ReturnTo(next), UnwindContinue())
}
next = {
Call(RET = opaque(a), ReturnTo(ret), UnwindContinue())
}
ret = {
Return()
}
)
}
// EMIT_MIR borrowed_local.f.CopyProp.diff
/// Generic type `T` is not known to be `Freeze`, so shared borrows may be mutable.
#[custom_mir(dialect = "analysis", phase = "post-cleanup")]
fn non_freeze<T: Copy>(x: T) -> bool {
// CHECK-LABEL: fn non_freeze(
// CHECK: bb0: {
// CHECK-NEXT: _2 = _1;
// CHECK-NEXT: _3 = &_1;
// CHECK-NEXT: _0 = opaque::<&T>(_3)
// CHECK: bb1: {
// CHECK-NEXT: _0 = opaque::<T>(_2)
mir!(
{
let a = x;
let r1 = &x;
Call(RET = opaque(r1), ReturnTo(next), UnwindContinue())
}
next = {
Call(RET = opaque(a), ReturnTo(ret), UnwindContinue())
}
ret = {
Return()
}
)
}
fn main() {
assert!(!compare_address());
non_freeze(5);
}
// EMIT_MIR borrowed_local.compare_address.CopyProp.diff
// EMIT_MIR borrowed_local.borrowed.CopyProp.diff
// EMIT_MIR borrowed_local.non_freeze.CopyProp.diff

View File

@ -0,0 +1,27 @@
- // MIR for `borrowed` before GVN
+ // MIR for `borrowed` after GVN
fn borrowed(_1: u32) -> () {
let mut _0: ();
let mut _2: u32;
let mut _3: &u32;
bb0: {
_2 = _1;
_3 = &_1;
_0 = opaque::<&u32>(_3) -> [return: bb1, unwind unreachable];
}
bb1: {
_0 = opaque::<u32>(_2) -> [return: bb2, unwind unreachable];
}
bb2: {
_0 = opaque::<u32>((*_3)) -> [return: bb3, unwind unreachable];
}
bb3: {
return;
}
}

View File

@ -0,0 +1,27 @@
- // MIR for `borrowed` before GVN
+ // MIR for `borrowed` after GVN
fn borrowed(_1: u32) -> () {
let mut _0: ();
let mut _2: u32;
let mut _3: &u32;
bb0: {
_2 = _1;
_3 = &_1;
_0 = opaque::<&u32>(_3) -> [return: bb1, unwind continue];
}
bb1: {
_0 = opaque::<u32>(_2) -> [return: bb2, unwind continue];
}
bb2: {
_0 = opaque::<u32>((*_3)) -> [return: bb3, unwind continue];
}
bb3: {
return;
}
}

View File

@ -0,0 +1,27 @@
- // MIR for `non_freeze` before GVN
+ // MIR for `non_freeze` after GVN
fn non_freeze(_1: T) -> () {
let mut _0: ();
let mut _2: T;
let mut _3: &T;
bb0: {
_2 = _1;
_3 = &_1;
_0 = opaque::<&T>(_3) -> [return: bb1, unwind unreachable];
}
bb1: {
_0 = opaque::<T>(_2) -> [return: bb2, unwind unreachable];
}
bb2: {
_0 = opaque::<T>((*_3)) -> [return: bb3, unwind unreachable];
}
bb3: {
return;
}
}

View File

@ -0,0 +1,27 @@
- // MIR for `non_freeze` before GVN
+ // MIR for `non_freeze` after GVN
fn non_freeze(_1: T) -> () {
let mut _0: ();
let mut _2: T;
let mut _3: &T;
bb0: {
_2 = _1;
_3 = &_1;
_0 = opaque::<&T>(_3) -> [return: bb1, unwind continue];
}
bb1: {
_0 = opaque::<T>(_2) -> [return: bb2, unwind continue];
}
bb2: {
_0 = opaque::<T>((*_3)) -> [return: bb3, unwind continue];
}
bb3: {
return;
}
}

View File

@ -720,6 +720,65 @@ fn wide_ptr_integer() {
opaque(a >= b);
}
#[custom_mir(dialect = "analysis", phase = "post-cleanup")]
fn borrowed(x: u32) {
// CHECK-LABEL: fn borrowed(
// CHECK: bb0: {
// CHECK-NEXT: _2 = _1;
// CHECK-NEXT: _3 = &_1;
// CHECK-NEXT: _0 = opaque::<&u32>(_3)
// CHECK: bb1: {
// CHECK-NEXT: _0 = opaque::<u32>(_2)
// CHECK: bb2: {
// CHECK-NEXT: _0 = opaque::<u32>((*_3))
mir!(
{
let a = x;
let r1 = &x;
Call(RET = opaque(r1), ReturnTo(next), UnwindContinue())
}
next = {
Call(RET = opaque(a), ReturnTo(deref), UnwindContinue())
}
deref = {
Call(RET = opaque(*r1), ReturnTo(ret), UnwindContinue())
}
ret = {
Return()
}
)
}
/// Generic type `T` is not known to be `Freeze`, so shared borrows may be mutable.
#[custom_mir(dialect = "analysis", phase = "post-cleanup")]
fn non_freeze<T: Copy>(x: T) {
// CHECK-LABEL: fn non_freeze(
// CHECK: bb0: {
// CHECK-NEXT: _2 = _1;
// CHECK-NEXT: _3 = &_1;
// CHECK-NEXT: _0 = opaque::<&T>(_3)
// CHECK: bb1: {
// CHECK-NEXT: _0 = opaque::<T>(_2)
// CHECK: bb2: {
// CHECK-NEXT: _0 = opaque::<T>((*_3))
mir!(
{
let a = x;
let r1 = &x;
Call(RET = opaque(r1), ReturnTo(next), UnwindContinue())
}
next = {
Call(RET = opaque(a), ReturnTo(deref), UnwindContinue())
}
deref = {
Call(RET = opaque(*r1), ReturnTo(ret), UnwindContinue())
}
ret = {
Return()
}
)
}
fn main() {
subexpression_elimination(2, 4, 5);
wrap_unwrap(5);
@ -742,6 +801,8 @@ fn main() {
constant_index_overflow(&[5, 3]);
wide_ptr_provenance();
wide_ptr_integer();
borrowed(5);
non_freeze(5);
}
#[inline(never)]
@ -773,3 +834,5 @@ fn identity<T>(x: T) -> T {
// EMIT_MIR gvn.wide_ptr_provenance.GVN.diff
// EMIT_MIR gvn.wide_ptr_same_provenance.GVN.diff
// EMIT_MIR gvn.wide_ptr_integer.GVN.diff
// EMIT_MIR gvn.borrowed.GVN.diff
// EMIT_MIR gvn.non_freeze.GVN.diff