Skip to content

Commit 4a18324

Browse files
committed
Auto merge of #111113 - scottmcm:assume-align-offset, r=thomcc
`assume` the runtime range of `align_offset` Found when I saw code with `align_to` having extraneous checks. Demo that LLVM can't do this today: <https://rust.godbolt.org/z/6dnG749bq> (It's filed as llvm/llvm-project#62502.)
2 parents dd9a7bf + a1e5c65 commit 4a18324

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
#![feature(const_arguments_as_str)]
108108
#![feature(const_array_from_ref)]
109109
#![feature(const_array_into_iter_constructors)]
110+
#![feature(const_assume)]
110111
#![feature(const_bigint_helper_methods)]
111112
#![feature(const_black_box)]
112113
#![feature(const_caller_location)]

library/core/src/ptr/mod.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,8 @@ pub(crate) const unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usiz
16321632
// FIXME(#75598): Direct use of these intrinsics improves codegen significantly at opt-level <=
16331633
// 1, where the method versions of these operations are not inlined.
16341634
use intrinsics::{
1635-
cttz_nonzero, exact_div, mul_with_overflow, unchecked_rem, unchecked_shl, unchecked_shr,
1636-
unchecked_sub, wrapping_add, wrapping_mul, wrapping_sub,
1635+
assume, cttz_nonzero, exact_div, mul_with_overflow, unchecked_rem, unchecked_shl,
1636+
unchecked_shr, unchecked_sub, wrapping_add, wrapping_mul, wrapping_sub,
16371637
};
16381638

16391639
/// Calculate multiplicative modular inverse of `x` modulo `m`.
@@ -1724,12 +1724,18 @@ pub(crate) const unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usiz
17241724
// in a branch-free way and then bitwise-OR it with whatever result the `-p mod a`
17251725
// computation produces.
17261726

1727+
let aligned_address = wrapping_add(addr, a_minus_one) & wrapping_sub(0, a);
1728+
let byte_offset = wrapping_sub(aligned_address, addr);
1729+
// FIXME: Remove the assume after <https://github.com/llvm/llvm-project/issues/62502>
1730+
// SAFETY: Masking by `-a` can only affect the low bits, and thus cannot have reduced
1731+
// the value by more than `a-1`, so even though the intermediate values might have
1732+
// wrapped, the byte_offset is always in `[0, a)`.
1733+
unsafe { assume(byte_offset < a) };
1734+
17271735
// SAFETY: `stride == 0` case has been handled by the special case above.
17281736
let addr_mod_stride = unsafe { unchecked_rem(addr, stride) };
17291737

17301738
return if addr_mod_stride == 0 {
1731-
let aligned_address = wrapping_add(addr, a_minus_one) & wrapping_sub(0, a);
1732-
let byte_offset = wrapping_sub(aligned_address, addr);
17331739
// SAFETY: `stride` is non-zero. This is guaranteed to divide exactly as well, because
17341740
// addr has been verified to be aligned to the original type’s alignment requirements.
17351741
unsafe { exact_div(byte_offset, stride) }

tests/codegen/align-offset.rs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// compile-flags: -O
2+
// min-llvm-version: 15.0 (because we're using opaque pointers)
3+
// ignore-debug (debug assertions in `slice::from_raw_parts` block optimizations)
4+
5+
#![crate_type = "lib"]
6+
7+
// CHECK-LABEL: @align8
8+
#[no_mangle]
9+
pub fn align8(p: *const u8) -> bool {
10+
// CHECK: ret i1 true
11+
p.align_offset(8) < 8
12+
}
13+
14+
#[repr(align(4))]
15+
pub struct Align4([u8; 4]);
16+
17+
// CHECK-LABEL: @align_to4
18+
#[no_mangle]
19+
pub fn align_to4(x: &[u8]) -> bool {
20+
// CHECK: ret i1 true
21+
let (prefix, _middle, suffix) = unsafe { x.align_to::<Align4>() };
22+
prefix.len() < 4 && suffix.len() < 4
23+
}
24+
25+
// CHECK-LABEL: @align_offset_byte_ptr(ptr{{.+}}%ptr)
26+
#[no_mangle]
27+
pub fn align_offset_byte_ptr(ptr: *const u8) -> usize {
28+
// CHECK: %[[ADDR:.+]] = ptrtoint ptr %ptr to [[USIZE:i[0-9]+]]
29+
// CHECK: %[[UP:.+]] = add [[USIZE]] %[[ADDR]], 31
30+
// CHECK: %[[ALIGNED:.+]] = and [[USIZE]] %[[UP]], -32
31+
// CHECK: %[[OFFSET:.+]] = sub [[USIZE]] %[[ALIGNED]], %[[ADDR]]
32+
33+
// Since we're offsetting a byte pointer, there's no further fixups
34+
// CHECK-NOT: shr
35+
// CHECK-NOT: div
36+
// CHECK-NOT: select
37+
38+
// CHECK: ret [[USIZE]] %[[OFFSET]]
39+
ptr.align_offset(32)
40+
}
41+
42+
// CHECK-LABEL: @align_offset_word_slice(ptr{{.+}}align 4{{.+}}%slice.0
43+
#[no_mangle]
44+
pub fn align_offset_word_slice(slice: &[Align4]) -> usize {
45+
// CHECK: %[[ADDR:.+]] = ptrtoint ptr %slice.0 to [[USIZE]]
46+
// CHECK: %[[UP:.+]] = add [[USIZE]] %[[ADDR]], 31
47+
// CHECK: %[[ALIGNED:.+]] = and [[USIZE]] %[[UP]], -32
48+
// CHECK: %[[BOFFSET:.+]] = sub [[USIZE]] %[[ALIGNED]], %[[ADDR]]
49+
// CHECK: %[[OFFSET:.+]] = lshr exact [[USIZE]] %[[BOFFSET]], 2
50+
51+
// Slices are known to be aligned, so we don't need the "maybe -1" path
52+
// CHECK-NOT: select
53+
54+
// CHECK: ret [[USIZE]] %[[OFFSET]]
55+
slice.as_ptr().align_offset(32)
56+
}
57+
58+
59+
// CHECK-LABEL: @align_offset_word_ptr(ptr{{.+}}%ptr
60+
#[no_mangle]
61+
pub fn align_offset_word_ptr(ptr: *const Align4) -> usize {
62+
// CHECK: %[[ADDR:.+]] = ptrtoint ptr %ptr to [[USIZE]]
63+
// CHECK: %[[UP:.+]] = add [[USIZE]] %[[ADDR]], 31
64+
// CHECK: %[[ALIGNED:.+]] = and [[USIZE]] %[[UP]], -32
65+
// CHECK: %[[BOFFSET:.+]] = sub [[USIZE]] %[[ALIGNED]], %[[ADDR]]
66+
67+
// While we can always get a *byte* offset that will work, if the original
68+
// pointer is unaligned it might be impossible to return an *element* offset
69+
// that will make it aligned. We want it to be a `select`, not a `br`, so
70+
// that the assembly will be branchless.
71+
// CHECK: %[[LOW:.+]] = and [[USIZE]] %[[ADDR]], 3
72+
// CHECK: %[[ORIGINAL_ALIGNED:.+]] = icmp eq [[USIZE]] %[[LOW]], 0
73+
// CHECK: %[[OFFSET:.+]] = lshr exact [[USIZE]] %[[BOFFSET]], 2
74+
// CHECK: %[[R:.+]] = select i1 %[[ORIGINAL_ALIGNED]], [[USIZE]] %[[OFFSET]], [[USIZE]] -1
75+
76+
// CHECK: ret [[USIZE]] %[[R]]
77+
ptr.align_offset(32)
78+
}

0 commit comments

Comments
 (0)