Skip to content

Commit b5938f0

Browse files
authored
Fix element type when reading OpPtrDiff (#2683)
The first argument to IRBuilder's `CreatePtrDiff` must be the element type, not the pointer type; otherwise the resulting difference will be scaled incorrectly.
1 parent f7057b4 commit b5938f0

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
@@ -2185,7 +2185,9 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
21852185
auto *BC = static_cast<SPIRVBinary *>(BV);
21862186
auto Ops = transValue(BC->getOperands(), F, BB);
21872187
IRBuilder<> Builder(BB);
2188-
Value *V = Builder.CreatePtrDiff(transType(BC->getType()), Ops[0], Ops[1]);
2188+
Type *ElemTy =
2189+
transType(BC->getOperands()[0]->getType()->getPointerElementType());
2190+
Value *V = Builder.CreatePtrDiff(ElemTy, Ops[0], Ops[1]);
21892191
return mapValue(BV, V);
21902192
}
21912193

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)