Add unsigned_offset_from
on pointers
Like we have `add`/`sub` which are the `usize` version of `offset`, this adds the `usize` equivalent of `offset_from`. Like how `.add(d)` replaced a whole bunch of `.offset(d as isize)`, you can see from the changes here that it's fairly common that code actually knows the order between the pointers and *wants* a `usize`, not an `isize`. As a bonus, this can do `sub nuw`+`udiv exact`, rather than `sub`+`sdiv exact`, which can be optimized slightly better because it doesn't have to worry about negatives. That's why the slice iterators weren't using `offset_from`, though I haven't updated that code in this PR because slices are so perf-critical that I'll do it as its own change. This is an intrinsic, like `offset_from`, so that it can eventually be allowed in CTFE. It also allows checking the extra safety condition -- see the test confirming that CTFE catches it if you pass the pointers in the wrong order.
This commit is contained in:
parent
6dd68402c5
commit
89a18cb600
19 changed files with 265 additions and 25 deletions
|
@ -308,7 +308,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||
let offset_ptr = ptr.wrapping_signed_offset(offset_bytes, self);
|
||||
self.write_pointer(offset_ptr, dest)?;
|
||||
}
|
||||
sym::ptr_offset_from => {
|
||||
sym::ptr_offset_from | sym::ptr_offset_from_unsigned => {
|
||||
let a = self.read_pointer(&args[0])?;
|
||||
let b = self.read_pointer(&args[1])?;
|
||||
|
||||
|
@ -330,8 +330,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||
// Both are pointers. They must be into the same allocation.
|
||||
if a_alloc_id != b_alloc_id {
|
||||
throw_ub_format!(
|
||||
"ptr_offset_from cannot compute offset of pointers into different \
|
||||
allocations.",
|
||||
"{} cannot compute offset of pointers into different allocations.",
|
||||
intrinsic_name,
|
||||
);
|
||||
}
|
||||
// And they must both be valid for zero-sized accesses ("in-bounds or one past the end").
|
||||
|
@ -348,16 +348,30 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||
CheckInAllocMsg::OffsetFromTest,
|
||||
)?;
|
||||
|
||||
if intrinsic_name == sym::ptr_offset_from_unsigned && a_offset < b_offset {
|
||||
throw_ub_format!(
|
||||
"{} cannot compute a negative offset, but {} < {}",
|
||||
intrinsic_name,
|
||||
a_offset.bytes(),
|
||||
b_offset.bytes(),
|
||||
);
|
||||
}
|
||||
|
||||
// Compute offset.
|
||||
let usize_layout = self.layout_of(self.tcx.types.usize)?;
|
||||
let isize_layout = self.layout_of(self.tcx.types.isize)?;
|
||||
let ret_layout = if intrinsic_name == sym::ptr_offset_from {
|
||||
isize_layout
|
||||
} else {
|
||||
usize_layout
|
||||
};
|
||||
let a_offset = ImmTy::from_uint(a_offset.bytes(), usize_layout);
|
||||
let b_offset = ImmTy::from_uint(b_offset.bytes(), usize_layout);
|
||||
let (val, _overflowed, _ty) =
|
||||
self.overflowing_binary_op(BinOp::Sub, &a_offset, &b_offset)?;
|
||||
let pointee_layout = self.layout_of(substs.type_at(0))?;
|
||||
let val = ImmTy::from_scalar(val, isize_layout);
|
||||
let size = ImmTy::from_int(pointee_layout.size.bytes(), isize_layout);
|
||||
let val = ImmTy::from_scalar(val, ret_layout);
|
||||
let size = ImmTy::from_int(pointee_layout.size.bytes(), ret_layout);
|
||||
self.exact_div(&val, &size, dest)?;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue