@@ -1632,8 +1632,8 @@ pub(crate) const unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usiz
1632
1632
// FIXME(#75598): Direct use of these intrinsics improves codegen significantly at opt-level <=
1633
1633
// 1, where the method versions of these operations are not inlined.
1634
1634
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,
1637
1637
} ;
1638
1638
1639
1639
/// 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
1724
1724
// in a branch-free way and then bitwise-OR it with whatever result the `-p mod a`
1725
1725
// computation produces.
1726
1726
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
+
1727
1735
// SAFETY: `stride == 0` case has been handled by the special case above.
1728
1736
let addr_mod_stride = unsafe { unchecked_rem ( addr, stride) } ;
1729
1737
1730
1738
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) ;
1733
1739
// SAFETY: `stride` is non-zero. This is guaranteed to divide exactly as well, because
1734
1740
// addr has been verified to be aligned to the original type’s alignment requirements.
1735
1741
unsafe { exact_div ( byte_offset, stride) }
0 commit comments