Skip to content

Commit

Permalink
Fix element type when reading OpPtrDiff (#2683)
Browse files Browse the repository at this point in the history
The first argument to IRBuilder's `CreatePtrDiff` must be the element
type, not the pointer type; otherwise the resulting difference will
be scaled incorrectly.
  • Loading branch information
svenvh authored Aug 26, 2024
1 parent f7057b4 commit b5938f0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2185,7 +2185,9 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
auto *BC = static_cast<SPIRVBinary *>(BV);
auto Ops = transValue(BC->getOperands(), F, BB);
IRBuilder<> Builder(BB);
Value *V = Builder.CreatePtrDiff(transType(BC->getType()), Ops[0], Ops[1]);
Type *ElemTy =
transType(BC->getOperands()[0]->getType()->getPointerElementType());
Value *V = Builder.CreatePtrDiff(ElemTy, Ops[0], Ops[1]);
return mapValue(BV, V);
}

Expand Down
2 changes: 1 addition & 1 deletion test/transcoding/ptr_diff.ll
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ entry:
; CHECK-LLVM: %[[#Arg1:]] = ptrtoint ptr %[[#]] to i64
; CHECK-LLVM: %[[#Arg2:]] = ptrtoint ptr %[[#]] to i64
; CHECK-LLVM: %[[#Sub:]] = sub i64 %[[#Arg1]], %[[#Arg2]]
; CHECK-LLVM: sdiv exact i64 %[[#Sub]], ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64)
; CHECK-LLVM: sdiv exact i64 %[[#Sub]], ptrtoint (ptr getelementptr (float, ptr null, i32 1) to i64)
%1 = call spir_func noundef i32 @_Z15__spirv_PtrDiff(ptr %0, ptr %0)
ret void
}
Expand Down

0 comments on commit b5938f0

Please sign in to comment.