Skip to content

Commit 242df2c

Browse files
authored
[Backport to 18] Fix element type when reading OpPtrDiff (#2683) (#2685)
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 da16c18 commit 242df2c

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
@@ -2245,7 +2245,9 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
22452245
auto *BC = static_cast<SPIRVBinary *>(BV);
22462246
auto Ops = transValue(BC->getOperands(), F, BB);
22472247
IRBuilder<> Builder(BB);
2248-
Value *V = Builder.CreatePtrDiff(transType(BC->getType()), Ops[0], Ops[1]);
2248+
Type *ElemTy =
2249+
transType(BC->getOperands()[0]->getType()->getPointerElementType());
2250+
Value *V = Builder.CreatePtrDiff(ElemTy, Ops[0], Ops[1]);
22492251
return mapValue(BV, V);
22502252
}
22512253

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(ptr %0, ptr %0)
4040
ret void
4141
}

0 commit comments

Comments
 (0)