From 5d144e301cda01db595220a22712905debac3b4a Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 7 Apr 2024 16:23:16 +0000 Subject: [PATCH] Add tests. --- ...d_local.borrowed.CopyProp.panic-abort.diff | 23 ++++++ ..._local.borrowed.CopyProp.panic-unwind.diff | 23 ++++++ ...compare_address.CopyProp.panic-abort.diff} | 6 +- ...ompare_address.CopyProp.panic-unwind.diff} | 6 +- ...local.non_freeze.CopyProp.panic-abort.diff | 23 ++++++ ...ocal.non_freeze.CopyProp.panic-unwind.diff | 23 ++++++ tests/mir-opt/copy-prop/borrowed_local.rs | 70 +++++++++++++++++-- .../mir-opt/gvn.borrowed.GVN.panic-abort.diff | 27 +++++++ .../gvn.borrowed.GVN.panic-unwind.diff | 27 +++++++ .../gvn.non_freeze.GVN.panic-abort.diff | 27 +++++++ .../gvn.non_freeze.GVN.panic-unwind.diff | 27 +++++++ tests/mir-opt/gvn.rs | 63 +++++++++++++++++ 12 files changed, 334 insertions(+), 11 deletions(-) create mode 100644 tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-abort.diff create mode 100644 tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-unwind.diff rename tests/mir-opt/copy-prop/{borrowed_local.f.CopyProp.panic-abort.diff => borrowed_local.compare_address.CopyProp.panic-abort.diff} (77%) rename tests/mir-opt/copy-prop/{borrowed_local.f.CopyProp.panic-unwind.diff => borrowed_local.compare_address.CopyProp.panic-unwind.diff} (77%) create mode 100644 tests/mir-opt/copy-prop/borrowed_local.non_freeze.CopyProp.panic-abort.diff create mode 100644 tests/mir-opt/copy-prop/borrowed_local.non_freeze.CopyProp.panic-unwind.diff create mode 100644 tests/mir-opt/gvn.borrowed.GVN.panic-abort.diff create mode 100644 tests/mir-opt/gvn.borrowed.GVN.panic-unwind.diff create mode 100644 tests/mir-opt/gvn.non_freeze.GVN.panic-abort.diff create mode 100644 tests/mir-opt/gvn.non_freeze.GVN.panic-unwind.diff diff --git a/tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-abort.diff b/tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-abort.diff new file mode 100644 index 00000000000..fe0833cf2e0 --- /dev/null +++ b/tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-abort.diff @@ -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::(_2) -> [return: bb2, unwind unreachable]; + } + + bb2: { + return; + } + } + diff --git a/tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-unwind.diff b/tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-unwind.diff new file mode 100644 index 00000000000..c4354a9631f --- /dev/null +++ b/tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-unwind.diff @@ -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::(_2) -> [return: bb2, unwind continue]; + } + + bb2: { + return; + } + } + diff --git a/tests/mir-opt/copy-prop/borrowed_local.f.CopyProp.panic-abort.diff b/tests/mir-opt/copy-prop/borrowed_local.compare_address.CopyProp.panic-abort.diff similarity index 77% rename from tests/mir-opt/copy-prop/borrowed_local.f.CopyProp.panic-abort.diff rename to tests/mir-opt/copy-prop/borrowed_local.compare_address.CopyProp.panic-abort.diff index 46534076c29..3d6b5dffba4 100644 --- a/tests/mir-opt/copy-prop/borrowed_local.f.CopyProp.panic-abort.diff +++ b/tests/mir-opt/copy-prop/borrowed_local.compare_address.CopyProp.panic-abort.diff @@ -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; diff --git a/tests/mir-opt/copy-prop/borrowed_local.f.CopyProp.panic-unwind.diff b/tests/mir-opt/copy-prop/borrowed_local.compare_address.CopyProp.panic-unwind.diff similarity index 77% rename from tests/mir-opt/copy-prop/borrowed_local.f.CopyProp.panic-unwind.diff rename to tests/mir-opt/copy-prop/borrowed_local.compare_address.CopyProp.panic-unwind.diff index b702e3b7d1e..0f29d2681de 100644 --- a/tests/mir-opt/copy-prop/borrowed_local.f.CopyProp.panic-unwind.diff +++ b/tests/mir-opt/copy-prop/borrowed_local.compare_address.CopyProp.panic-unwind.diff @@ -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; diff --git a/tests/mir-opt/copy-prop/borrowed_local.non_freeze.CopyProp.panic-abort.diff b/tests/mir-opt/copy-prop/borrowed_local.non_freeze.CopyProp.panic-abort.diff new file mode 100644 index 00000000000..af2aeb0dcab --- /dev/null +++ b/tests/mir-opt/copy-prop/borrowed_local.non_freeze.CopyProp.panic-abort.diff @@ -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::(_2) -> [return: bb2, unwind unreachable]; + } + + bb2: { + return; + } + } + diff --git a/tests/mir-opt/copy-prop/borrowed_local.non_freeze.CopyProp.panic-unwind.diff b/tests/mir-opt/copy-prop/borrowed_local.non_freeze.CopyProp.panic-unwind.diff new file mode 100644 index 00000000000..040ed0aec16 --- /dev/null +++ b/tests/mir-opt/copy-prop/borrowed_local.non_freeze.CopyProp.panic-unwind.diff @@ -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::(_2) -> [return: bb2, unwind continue]; + } + + bb2: { + return; + } + } + diff --git a/tests/mir-opt/copy-prop/borrowed_local.rs b/tests/mir-opt/copy-prop/borrowed_local.rs index 24b8e45532c..98b9818f827 100644 --- a/tests/mir-opt/copy-prop/borrowed_local.rs +++ b/tests/mir-opt/copy-prop/borrowed_local.rs @@ -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::(_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::(_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(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::(_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 diff --git a/tests/mir-opt/gvn.borrowed.GVN.panic-abort.diff b/tests/mir-opt/gvn.borrowed.GVN.panic-abort.diff new file mode 100644 index 00000000000..2825e0954c9 --- /dev/null +++ b/tests/mir-opt/gvn.borrowed.GVN.panic-abort.diff @@ -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::(_2) -> [return: bb2, unwind unreachable]; + } + + bb2: { + _0 = opaque::((*_3)) -> [return: bb3, unwind unreachable]; + } + + bb3: { + return; + } + } + diff --git a/tests/mir-opt/gvn.borrowed.GVN.panic-unwind.diff b/tests/mir-opt/gvn.borrowed.GVN.panic-unwind.diff new file mode 100644 index 00000000000..e03f8caa228 --- /dev/null +++ b/tests/mir-opt/gvn.borrowed.GVN.panic-unwind.diff @@ -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::(_2) -> [return: bb2, unwind continue]; + } + + bb2: { + _0 = opaque::((*_3)) -> [return: bb3, unwind continue]; + } + + bb3: { + return; + } + } + diff --git a/tests/mir-opt/gvn.non_freeze.GVN.panic-abort.diff b/tests/mir-opt/gvn.non_freeze.GVN.panic-abort.diff new file mode 100644 index 00000000000..7b6ed096118 --- /dev/null +++ b/tests/mir-opt/gvn.non_freeze.GVN.panic-abort.diff @@ -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::(_2) -> [return: bb2, unwind unreachable]; + } + + bb2: { + _0 = opaque::((*_3)) -> [return: bb3, unwind unreachable]; + } + + bb3: { + return; + } + } + diff --git a/tests/mir-opt/gvn.non_freeze.GVN.panic-unwind.diff b/tests/mir-opt/gvn.non_freeze.GVN.panic-unwind.diff new file mode 100644 index 00000000000..641a2f4609a --- /dev/null +++ b/tests/mir-opt/gvn.non_freeze.GVN.panic-unwind.diff @@ -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::(_2) -> [return: bb2, unwind continue]; + } + + bb2: { + _0 = opaque::((*_3)) -> [return: bb3, unwind continue]; + } + + bb3: { + return; + } + } + diff --git a/tests/mir-opt/gvn.rs b/tests/mir-opt/gvn.rs index 6f4d1e35585..0f7cb9c0566 100644 --- a/tests/mir-opt/gvn.rs +++ b/tests/mir-opt/gvn.rs @@ -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::(_2) + // CHECK: bb2: { + // CHECK-NEXT: _0 = opaque::((*_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(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::(_2) + // CHECK: bb2: { + // CHECK-NEXT: _0 = opaque::((*_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(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