Skip to content

Commit 4fbe742

Browse files
vmaksimojsji
authored andcommitted
Fix reverse translation of OpPtrDiff with untyped pointers (#2858)
We should not lose operand type when SPV_KHR_untyped_pointers is used. Original commit: KhronosGroup/SPIRV-LLVM-Translator@926a92a57d73ccc
1 parent 712833b commit 4fbe742

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,10 +2308,17 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
23082308

23092309
case OpPtrDiff: {
23102310
auto *BC = static_cast<SPIRVBinary *>(BV);
2311-
auto Ops = transValue(BC->getOperands(), F, BB);
2311+
auto SPVOps = BC->getOperands();
2312+
auto Ops = transValue(SPVOps, F, BB);
23122313
IRBuilder<> Builder(BB);
2313-
Type *ElemTy =
2314-
transType(BC->getOperands()[0]->getType()->getPointerElementType());
2314+
2315+
Type *ElemTy = nullptr;
2316+
if (SPVOps[0]->isUntypedVariable())
2317+
ElemTy = transType(
2318+
static_cast<SPIRVUntypedVariableKHR *>(SPVOps[0])->getDataType());
2319+
else
2320+
ElemTy = transType(SPVOps[0]->getType()->getPointerElementType());
2321+
23152322
Value *V = Builder.CreatePtrDiff(ElemTy, Ops[0], Ops[1]);
23162323
return mapValue(BV, V);
23172324
}

llvm-spirv/test/transcoding/ptr_diff.ll

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55

66
; RUN: llvm-spirv %t.bc -o %t.spv
77
; RUN: llvm-spirv %t.spv -to-text -o %t.spt
8-
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
8+
; RUN: FileCheck < %t.spt %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-TYPED-PTR
9+
; RUN: spirv-val %t.spv
10+
11+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
12+
; RUN: llvm-dis %t.rev.bc
13+
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM
14+
15+
; RUN: llvm-spirv %t.bc -o %t.spv --spirv-ext=+SPV_KHR_untyped_pointers
16+
; RUN: llvm-spirv %t.spv -to-text -o %t.spt
17+
; RUN: FileCheck < %t.spt %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-UNTYPED-PTR
918
; RUN: spirv-val %t.spv
1019

1120
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
@@ -19,9 +28,11 @@
1928
; CHECK-SPIRV: 66560
2029
; CHECK-SPIRV: TypeInt [[#TypeInt:]] 32 0
2130
; CHECK-SPIRV: TypeFloat [[#TypeFloat:]] 32
22-
; CHECK-SPIRV: TypePointer [[#TypePointer:]] [[#]] [[#TypeFloat]]
31+
; CHECK-SPIRV-TYPED-PTR: TypePointer [[#TypePointer:]] [[#]] [[#TypeFloat]]
32+
; CHECK-SPIRV-UNTYPED-PTR: TypeUntypedPointerKHR [[#TypePointer:]] [[#]]
2333

24-
; CHECK-SPIRV: Variable [[#TypePointer]] [[#Var:]]
34+
; CHECK-SPIRV-TYPED-PTR: Variable [[#TypePointer]] [[#Var:]]
35+
; CHECK-SPIRV-UNTYPED-PTR: UntypedVariableKHR [[#TypePointer]] [[#Var:]] [[#]] [[#TypeFloat]]
2536
; CHECK-SPIRV: PtrDiff [[#TypeInt]] [[#]] [[#Var]] [[#Var]]
2637

2738
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"

0 commit comments

Comments
 (0)