Skip to content

Commit 69802a6

Browse files
authored
Unrolled build for #152787
Rollup merge of #152787 - scottmcm:alignment-checks-test, r=jieyouxu Add a mir-opt test for alignment check generation [zero changes outside tests] I wrote this as part of #152641 which it looks like I'm going to just close, so submitting the new test separately since we didn't have any mir-opt testing of this pass that I could find (at least `x test tests/mir-opt` didn't fail when I broke them) so figured it's something that should exist.
2 parents e0cb264 + 7585786 commit 69802a6

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

tests/mir-opt/alignment_checks.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ compile-flags: -Copt-level=1 -Zmir-opt-level=2 -Zub-checks
2+
//@ only-64bit
3+
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
4+
5+
#![crate_type = "lib"]
6+
7+
// The diff shows exactly what is generated by the pass;
8+
// then we check the final `-O1` output for people who want to run them
9+
// without the codegen being too terrible.
10+
11+
// EMIT_MIR alignment_checks.sized_ptr.CheckAlignment.diff
12+
pub unsafe fn sized_ptr(ptr: *const u32) -> u32 {
13+
// CHECK-LABEL: fn sized_ptr(_1: *const u32)
14+
// CHECK: _2 = copy _1 as usize (Transmute);
15+
// CHECK: _3 = BitAnd(copy _2, const 3_usize);
16+
// CHECK: _4 = Eq(copy _3, const 0_usize);
17+
// CHECK: assert(copy _4,
18+
*ptr
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
- // MIR for `sized_ptr` before CheckAlignment
2+
+ // MIR for `sized_ptr` after CheckAlignment
3+
4+
fn sized_ptr(_1: *const u32) -> u32 {
5+
debug ptr => _1;
6+
let mut _0: u32;
7+
+ let mut _2: *const ();
8+
+ let mut _3: usize;
9+
+ let mut _4: usize;
10+
+ let mut _5: usize;
11+
+ let mut _6: bool;
12+
13+
bb0: {
14+
+ _2 = copy _1 as *const () (PtrToPtr);
15+
+ _3 = copy _2 as usize (Transmute);
16+
+ _4 = Sub(const <u32 as std::mem::SizedTypeProperties>::ALIGN, const 1_usize);
17+
+ _5 = BitAnd(copy _3, copy _4);
18+
+ _6 = Eq(copy _5, const 0_usize);
19+
+ assert(copy _6, "misaligned pointer dereference: address must be a multiple of {} but is {}", const <u32 as std::mem::SizedTypeProperties>::ALIGN, copy _3) -> [success: bb1, unwind unreachable];
20+
+ }
21+
+
22+
+ bb1: {
23+
_0 = copy (*_1);
24+
return;
25+
}
26+
}
27+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
- // MIR for `sized_ptr` before CheckAlignment
2+
+ // MIR for `sized_ptr` after CheckAlignment
3+
4+
fn sized_ptr(_1: *const u32) -> u32 {
5+
debug ptr => _1;
6+
let mut _0: u32;
7+
+ let mut _2: *const ();
8+
+ let mut _3: usize;
9+
+ let mut _4: usize;
10+
+ let mut _5: usize;
11+
+ let mut _6: bool;
12+
13+
bb0: {
14+
+ _2 = copy _1 as *const () (PtrToPtr);
15+
+ _3 = copy _2 as usize (Transmute);
16+
+ _4 = Sub(const <u32 as std::mem::SizedTypeProperties>::ALIGN, const 1_usize);
17+
+ _5 = BitAnd(copy _3, copy _4);
18+
+ _6 = Eq(copy _5, const 0_usize);
19+
+ assert(copy _6, "misaligned pointer dereference: address must be a multiple of {} but is {}", const <u32 as std::mem::SizedTypeProperties>::ALIGN, copy _3) -> [success: bb1, unwind unreachable];
20+
+ }
21+
+
22+
+ bb1: {
23+
_0 = copy (*_1);
24+
return;
25+
}
26+
}
27+

0 commit comments

Comments
 (0)