Skip to content

Commit

Permalink
Fix translation of load instruction used with `SPV_KHR_untyped_poin…
Browse files Browse the repository at this point in the history
…ters` (#2867)

Do not lose variable type in forward translation - take it from the already translated "untyped" variable.
  • Loading branch information
vmaksimo authored Nov 22, 2024
1 parent 88b1831 commit 9207ef2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
42 changes: 28 additions & 14 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2251,11 +2251,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
static_cast<SPIRVExtInst *>(FrexpResult)->getArgValues()[1];
SPIRVType *LoadTy = nullptr;

if (IntFromFrexpResult->isUntypedVariable()) {
auto *BV =
static_cast<SPIRVUntypedVariableKHR *>(IntFromFrexpResult);
LoadTy = BV->getDataType();
}
if (IntFromFrexpResult->isUntypedVariable())
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(IntFromFrexpResult)
->getDataType();

IntFromFrexpResult =
BM->addLoadInst(IntFromFrexpResult, {}, BB, LoadTy);

Expand Down Expand Up @@ -2483,10 +2482,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
SPIRVValue *IntFromFrexpResult =
static_cast<SPIRVExtInst *>(Val)->getArgValues()[1];
SPIRVType *LoadTy = nullptr;
if (IntFromFrexpResult->isUntypedVariable()) {
auto *BV = static_cast<SPIRVUntypedVariableKHR *>(IntFromFrexpResult);
LoadTy = BV->getDataType();
}
if (IntFromFrexpResult->isUntypedVariable())
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(IntFromFrexpResult)
->getDataType();

IntFromFrexpResult =
BM->addLoadInst(IntFromFrexpResult, {}, BB, LoadTy);
return mapValue(V, IntFromFrexpResult);
Expand Down Expand Up @@ -6499,8 +6498,11 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,

// Input - integer input of any width or 'byval' pointer to this integer
SPIRVValue *Input = transValue(*OpItr, BB);
SPIRVType *LoadTy = nullptr;
if (Input->isUntypedVariable())
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(Input)->getDataType();
if (OpItr->getType()->isPointerTy())
Input = BM->addLoadInst(Input, {}, BB);
Input = BM->addLoadInst(Input, {}, BB, LoadTy);
OpItr++;

std::vector<SPIRVWord> Literals;
Expand Down Expand Up @@ -6590,8 +6592,11 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,

// Input - integer input of any width or 'byval' pointer to this integer
SPIRVValue *Input = transValue(*OpItr, BB);
SPIRVType *LoadTy = nullptr;
if (Input->isUntypedVariable())
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(Input)->getDataType();
if (OpItr->getType()->isPointerTy())
Input = BM->addLoadInst(Input, {}, BB);
Input = BM->addLoadInst(Input, {}, BB, LoadTy);
OpItr++;

std::vector<SPIRVWord> Literals;
Expand Down Expand Up @@ -6667,18 +6672,24 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,

// InA - integer input of any width or 'byval' pointer to this integer
SPIRVValue *InA = transValue(*OpItr, BB);
SPIRVType *LoadTy = nullptr;
if (InA->isUntypedVariable())
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(InA)->getDataType();
if (OpItr->getType()->isPointerTy())
InA = BM->addLoadInst(InA, {}, BB);
InA = BM->addLoadInst(InA, {}, BB, LoadTy);
OpItr++;

std::vector<SPIRVWord> Literals;
Literals.push_back(cast<llvm::ConstantInt>(*OpItr++)->getZExtValue());

// InB - integer input of any width or 'byval' pointer to this integer
SPIRVValue *InB = transValue(*OpItr, BB);
LoadTy = nullptr;
if (InB->isUntypedVariable())
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(InB)->getDataType();
if (OpItr->getType()->isPointerTy()) {
std::vector<SPIRVWord> Mem;
InB = BM->addLoadInst(InB, Mem, BB);
InB = BM->addLoadInst(InB, Mem, BB, LoadTy);
}
OpItr++;

Expand Down Expand Up @@ -6758,7 +6769,10 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
transValue(CI->getArgOperand(0)->stripPointerCasts(), BB);
SPIRVId ScopeId = transValue(CI->getArgOperand(1), BB)->getId();
SPIRVValue *Delta = transValue(CI->getArgOperand(3), BB);
SPIRVValue *Composite0 = BM->addLoadInst(InValue, {}, BB);
SPIRVType *LoadTy = nullptr;
if (InValue->isUntypedVariable())
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(InValue)->getDataType();
SPIRVValue *Composite0 = BM->addLoadInst(InValue, {}, BB, LoadTy);
Type *MemberTy = St->getElementType(0);
SPIRVType *ElementTy = transType(MemberTy);
SPIRVValue *Element0 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@
; CHECK-ERROR: InvalidInstruction: Can't translate llvm instruction:
; CHECK-ERROR: Fixed point instructions can't be translated correctly without enabled SPV_INTEL_arbitrary_precision_fixed_point extension!

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

; Test with untyped pointers enabled.
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_arbitrary_precision_integers,+SPV_INTEL_arbitrary_precision_fixed_point,+SPV_KHR_untyped_pointers -o %t.spv
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM

; CHECK-SPIRV: 2 Capability Kernel
; CHECK-SPIRV: 2 Capability ArbitraryPrecisionIntegersINTEL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@
; RUN: llvm-spirv -r %t.spv -o %t.r.bc
; RUN: llvm-dis < %t.r.bc | FileCheck %s --check-prefix=CHECK-LLVM

; Test with untyped pointers enabled.
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_arbitrary_precision_integers,+SPV_INTEL_arbitrary_precision_floating_point,+SPV_KHR_untyped_pointers -o %t.spv
; RUN: llvm-spirv -r %t.spv -o %t.r.bc
; RUN: llvm-dis < %t.r.bc | FileCheck %s --check-prefix=CHECK-LLVM

; CHECK-SPIRV: 2 Capability Kernel
; CHECK-SPIRV: 2 Capability ArbitraryPrecisionIntegersINTEL
; CHECK-SPIRV: 2 Capability ArbitraryPrecisionFloatingPointINTEL
Expand Down
17 changes: 13 additions & 4 deletions test/group_non_uniform_shuffle_down.ll
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
; RUN: llvm-as %s -o %t.bc
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck --check-prefix CHECK-SPIRV %s
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-TYPED-PTR %s
; RUN: llvm-spirv %t.bc -o %t.spv
; RUN: spirv-val %t.spv
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis %t.rev.bc -o - | FileCheck --check-prefix CHECK-LLVM %s

; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_KHR_untyped_pointers -spirv-text -o - | FileCheck --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-UNTYPED-PTR %s
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_KHR_untyped_pointers -o %t.spv
; RUN: spirv-val %t.spv
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis %t.rev.bc -o - | FileCheck --check-prefix CHECK-LLVM %s

; CHECK-SPIRV-DAG: TypeInt [[#I32:]] 32 0
; CHECK-SPIRV-DAG: Constant [[#I32]] [[#CONST_I32_3:]] 3
; CHECK-SPIRV-DAG: Constant [[#I32]] [[#CONST_I32_8:]] 8
; CHECK-SPIRV-DAG: TypeFloat [[#HALF:]] 16
; CHECK-SPIRV-DAG: TypeStruct [[#S_HALF:]] [[#HALF]]
; CHECK-SPIRV-DAG: TypePointer [[#PTR_S_HALF:]] {{[0-9]+}} [[#S_HALF]]
; CHECK-SPIRV-TYPED-PTR-DAG: TypePointer [[#PTR_S_HALF:]] {{[0-9]+}} [[#S_HALF]]
; CHECK-SPIRV-UNTYPED-PTR-DAG: TypeUntypedPointerKHR [[#PTR:]] [[#]]

target triple = "spir64-unknown-unknown"

Expand All @@ -25,8 +32,10 @@ entry:
ret void
}

; CHECK-SPIRV: Variable {{[0-9]+}} {{[0-9]+}}
; CHECK-SPIRV: Variable [[#PTR_S_HALF]] [[#VAR_0:]]
; CHECK-SPIRV-TYPED-PTR: Variable {{[0-9]+}} {{[0-9]+}}
; CHECK-SPIRV-TYPED-PTR: Variable [[#PTR_S_HALF]] [[#VAR_0:]]
; CHECK-SPIRV-UNTYPED-PTR: UntypedVariableKHR {{[0-9]+}} {{[0-9]+}}
; CHECK-SPIRV-UNTYPED-PTR: UntypedVariableKHR [[#PTR]] [[#VAR_0:]] [[#HALF]]
; CHECK-SPIRV: Load [[#S_HALF]] [[#COMP_0:]] [[#VAR_0]]
; CHECK-SPIRV: CompositeExtract [[#HALF]] [[#ELEM_0:]] [[#COMP_0]] 0
; CHECK-SPIRV: GroupNonUniformShuffleDown [[#HALF]] [[#ELEM_1:]] [[#CONST_I32_3]] [[#ELEM_0]] [[#CONST_I32_8]]
Expand Down

0 comments on commit 9207ef2

Please sign in to comment.