From 1f9e0e36d8917cece7593771304d8db0bcd9f614 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Tue, 27 Aug 2024 11:16:51 +0200 Subject: [PATCH] [Backport to 16] Fix element type when reading OpPtrDiff (#2683) (#2688) 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 b5938f0f5e80dbe764b733c7d935c3cafc56a3df) --- lib/SPIRV/SPIRVReader.cpp | 4 +++- test/transcoding/ptr_diff.ll | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index a6904b390b..2a2e0ecdca 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -2161,7 +2161,9 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F, auto *BC = static_cast(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); } diff --git a/test/transcoding/ptr_diff.ll b/test/transcoding/ptr_diff.ll index 6a9092c438..fffab5effe 100644 --- a/test/transcoding/ptr_diff.ll +++ b/test/transcoding/ptr_diff.ll @@ -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 }