Skip to content

Commit 1e170e2

Browse files
authored
[Backport to 15] Fix element type when reading OpPtrDiff (#2683) (#2689)
The first argument to IRBuilder's `CreatePtrDiff` must be the element type, not the pointer type; otherwise the resulting difference will be scaled incorrectly. (cherry picked from commit b5938f0)
1 parent 16c048f commit 1e170e2

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2264,7 +2264,9 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
22642264
auto *BC = static_cast<SPIRVBinary *>(BV);
22652265
auto Ops = transValue(BC->getOperands(), F, BB);
22662266
IRBuilder<> Builder(BB);
2267-
Value *V = Builder.CreatePtrDiff(transType(BC->getType()), Ops[0], Ops[1]);
2267+
Type *ElemTy =
2268+
transType(BC->getOperands()[0]->getType()->getPointerElementType());
2269+
Value *V = Builder.CreatePtrDiff(ElemTy, Ops[0], Ops[1]);
22682270
return mapValue(BV, V);
22692271
}
22702272

test/transcoding/ptr_diff.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ entry:
3535
; CHECK-LLVM: %[[#Arg1:]] = ptrtoint ptr %[[#]] to i64
3636
; CHECK-LLVM: %[[#Arg2:]] = ptrtoint ptr %[[#]] to i64
3737
; CHECK-LLVM: %[[#Sub:]] = sub i64 %[[#Arg1]], %[[#Arg2]]
38-
; CHECK-LLVM: sdiv exact i64 %[[#Sub]], ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64)
38+
; CHECK-LLVM: sdiv exact i64 %[[#Sub]], ptrtoint (ptr getelementptr (float, ptr null, i32 1) to i64)
3939
%1 = call spir_func noundef i32 @_Z15__spirv_PtrDiff(float* %0, float* %0)
4040
ret void
4141
}

0 commit comments

Comments
 (0)