Skip to content

Commit

Permalink
Fix reverse translation of OpPtrDiff with untyped pointers (#2858)
Browse files Browse the repository at this point in the history
We should not lose operand type when SPV_KHR_untyped_pointers is used.
  • Loading branch information
vmaksimo authored Nov 28, 2024
1 parent da1731b commit 926a92a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 10 additions & 3 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2305,10 +2305,17 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,

case OpPtrDiff: {
auto *BC = static_cast<SPIRVBinary *>(BV);
auto Ops = transValue(BC->getOperands(), F, BB);
auto SPVOps = BC->getOperands();
auto Ops = transValue(SPVOps, F, BB);
IRBuilder<> Builder(BB);
Type *ElemTy =
transType(BC->getOperands()[0]->getType()->getPointerElementType());

Type *ElemTy = nullptr;
if (SPVOps[0]->isUntypedVariable())
ElemTy = transType(
static_cast<SPIRVUntypedVariableKHR *>(SPVOps[0])->getDataType());
else
ElemTy = transType(SPVOps[0]->getType()->getPointerElementType());

Value *V = Builder.CreatePtrDiff(ElemTy, Ops[0], Ops[1]);
return mapValue(BV, V);
}
Expand Down
17 changes: 14 additions & 3 deletions test/transcoding/ptr_diff.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@

; RUN: llvm-spirv %t.bc -o %t.spv
; RUN: llvm-spirv %t.spv -to-text -o %t.spt
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
; RUN: FileCheck < %t.spt %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-TYPED-PTR
; RUN: spirv-val %t.spv

; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis %t.rev.bc
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM

; RUN: llvm-spirv %t.bc -o %t.spv --spirv-ext=+SPV_KHR_untyped_pointers
; RUN: llvm-spirv %t.spv -to-text -o %t.spt
; RUN: FileCheck < %t.spt %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-UNTYPED-PTR
; RUN: spirv-val %t.spv

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

; CHECK-SPIRV: Variable [[#TypePointer]] [[#Var:]]
; CHECK-SPIRV-TYPED-PTR: Variable [[#TypePointer]] [[#Var:]]
; CHECK-SPIRV-UNTYPED-PTR: UntypedVariableKHR [[#TypePointer]] [[#Var:]] [[#]] [[#TypeFloat]]
; CHECK-SPIRV: PtrDiff [[#TypeInt]] [[#]] [[#Var]] [[#Var]]

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"
Expand Down

0 comments on commit 926a92a

Please sign in to comment.