From 7585786ecb24e7cc3eaad08a6706ae3fb8e63ea8 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sat, 14 Feb 2026 17:58:44 -0800 Subject: [PATCH] Add a mir-opt test for alignment check generation --- tests/mir-opt/alignment_checks.rs | 19 +++++++++++++ ....sized_ptr.CheckAlignment.panic-abort.diff | 27 +++++++++++++++++++ ...sized_ptr.CheckAlignment.panic-unwind.diff | 27 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 tests/mir-opt/alignment_checks.rs create mode 100644 tests/mir-opt/alignment_checks.sized_ptr.CheckAlignment.panic-abort.diff create mode 100644 tests/mir-opt/alignment_checks.sized_ptr.CheckAlignment.panic-unwind.diff diff --git a/tests/mir-opt/alignment_checks.rs b/tests/mir-opt/alignment_checks.rs new file mode 100644 index 000000000000..6f1329cb4e64 --- /dev/null +++ b/tests/mir-opt/alignment_checks.rs @@ -0,0 +1,19 @@ +//@ compile-flags: -Copt-level=1 -Zmir-opt-level=2 -Zub-checks +//@ only-64bit +// EMIT_MIR_FOR_EACH_PANIC_STRATEGY + +#![crate_type = "lib"] + +// The diff shows exactly what is generated by the pass; +// then we check the final `-O1` output for people who want to run them +// without the codegen being too terrible. + +// EMIT_MIR alignment_checks.sized_ptr.CheckAlignment.diff +pub unsafe fn sized_ptr(ptr: *const u32) -> u32 { + // CHECK-LABEL: fn sized_ptr(_1: *const u32) + // CHECK: _2 = copy _1 as usize (Transmute); + // CHECK: _3 = BitAnd(copy _2, const 3_usize); + // CHECK: _4 = Eq(copy _3, const 0_usize); + // CHECK: assert(copy _4, + *ptr +} diff --git a/tests/mir-opt/alignment_checks.sized_ptr.CheckAlignment.panic-abort.diff b/tests/mir-opt/alignment_checks.sized_ptr.CheckAlignment.panic-abort.diff new file mode 100644 index 000000000000..c383975d9c0f --- /dev/null +++ b/tests/mir-opt/alignment_checks.sized_ptr.CheckAlignment.panic-abort.diff @@ -0,0 +1,27 @@ +- // MIR for `sized_ptr` before CheckAlignment ++ // MIR for `sized_ptr` after CheckAlignment + + fn sized_ptr(_1: *const u32) -> u32 { + debug ptr => _1; + let mut _0: u32; ++ let mut _2: *const (); ++ let mut _3: usize; ++ let mut _4: usize; ++ let mut _5: usize; ++ let mut _6: bool; + + bb0: { ++ _2 = copy _1 as *const () (PtrToPtr); ++ _3 = copy _2 as usize (Transmute); ++ _4 = Sub(const ::ALIGN, const 1_usize); ++ _5 = BitAnd(copy _3, copy _4); ++ _6 = Eq(copy _5, const 0_usize); ++ assert(copy _6, "misaligned pointer dereference: address must be a multiple of {} but is {}", const ::ALIGN, copy _3) -> [success: bb1, unwind unreachable]; ++ } ++ ++ bb1: { + _0 = copy (*_1); + return; + } + } + diff --git a/tests/mir-opt/alignment_checks.sized_ptr.CheckAlignment.panic-unwind.diff b/tests/mir-opt/alignment_checks.sized_ptr.CheckAlignment.panic-unwind.diff new file mode 100644 index 000000000000..c383975d9c0f --- /dev/null +++ b/tests/mir-opt/alignment_checks.sized_ptr.CheckAlignment.panic-unwind.diff @@ -0,0 +1,27 @@ +- // MIR for `sized_ptr` before CheckAlignment ++ // MIR for `sized_ptr` after CheckAlignment + + fn sized_ptr(_1: *const u32) -> u32 { + debug ptr => _1; + let mut _0: u32; ++ let mut _2: *const (); ++ let mut _3: usize; ++ let mut _4: usize; ++ let mut _5: usize; ++ let mut _6: bool; + + bb0: { ++ _2 = copy _1 as *const () (PtrToPtr); ++ _3 = copy _2 as usize (Transmute); ++ _4 = Sub(const ::ALIGN, const 1_usize); ++ _5 = BitAnd(copy _3, copy _4); ++ _6 = Eq(copy _5, const 0_usize); ++ assert(copy _6, "misaligned pointer dereference: address must be a multiple of {} but is {}", const ::ALIGN, copy _3) -> [success: bb1, unwind unreachable]; ++ } ++ ++ bb1: { + _0 = copy (*_1); + return; + } + } +