Skip to content

Commit

Permalink
[AMDGPULowerBufferFatPointers] Fix offset-only ptrtoint (#95543)
Browse files Browse the repository at this point in the history
For ptrtoint that truncates to the offset only, the expansion generated
a shift by the bit width, which is poison. Instead, we should return the
offset directly.

(The same problem exists for the constant expression case, but I plan to
address that separately, and more comprehensively.)
  • Loading branch information
nikic authored Jun 14, 2024
1 parent 7ad12a7 commit 0774000
Show file tree
Hide file tree
Showing 5 changed files with 2,165 additions and 1,541 deletions.
30 changes: 16 additions & 14 deletions llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1435,20 +1435,22 @@ PtrParts SplitPtrStructs::visitPtrToIntInst(PtrToIntInst &PI) {
const DataLayout &DL = PI.getModule()->getDataLayout();
unsigned FatPtrWidth = DL.getPointerSizeInBits(AMDGPUAS::BUFFER_FAT_POINTER);

Value *RsrcInt;
if (Width <= BufferOffsetWidth)
RsrcInt = ConstantExpr::getIntegerValue(ResTy, APInt::getZero(Width));
else
RsrcInt = IRB.CreatePtrToInt(Rsrc, ResTy, PI.getName() + ".rsrc");
copyMetadata(RsrcInt, &PI);

Value *Shl = IRB.CreateShl(
RsrcInt,
ConstantExpr::getIntegerValue(ResTy, APInt(Width, BufferOffsetWidth)), "",
Width >= FatPtrWidth, Width > FatPtrWidth);
Value *OffCast =
IRB.CreateIntCast(Off, ResTy, /*isSigned=*/false, PI.getName() + ".off");
Value *Res = IRB.CreateOr(Shl, OffCast);
Value *Res;
if (Width <= BufferOffsetWidth) {
Res = IRB.CreateIntCast(Off, ResTy, /*isSigned=*/false,
PI.getName() + ".off");
} else {
Value *RsrcInt = IRB.CreatePtrToInt(Rsrc, ResTy, PI.getName() + ".rsrc");
Value *Shl = IRB.CreateShl(
RsrcInt,
ConstantExpr::getIntegerValue(ResTy, APInt(Width, BufferOffsetWidth)),
"", Width >= FatPtrWidth, Width > FatPtrWidth);
Value *OffCast = IRB.CreateIntCast(Off, ResTy, /*isSigned=*/false,
PI.getName() + ".off");
Res = IRB.CreateOr(Shl, OffCast);
}

copyMetadata(Res, &PI);
Res->takeName(&PI);
SplitUsers.insert(&PI);
PI.replaceAllUsesWith(Res);
Expand Down
Loading

0 comments on commit 0774000

Please sign in to comment.