Skip to content

Commit 35d0115

Browse files
vmaksimojsji
authored andcommitted
Fix translation of load instruction used with SPV_KHR_untyped_pointers (#2867)
Do not lose variable type in forward translation - take it from the already translated "untyped" variable. Original commit: KhronosGroup/SPIRV-LLVM-Translator@9207ef2aa150773
1 parent 179db36 commit 35d0115

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,11 +2246,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
22462246
static_cast<SPIRVExtInst *>(FrexpResult)->getArgValues()[1];
22472247
SPIRVType *LoadTy = nullptr;
22482248

2249-
if (IntFromFrexpResult->isUntypedVariable()) {
2250-
auto *BV =
2251-
static_cast<SPIRVUntypedVariableKHR *>(IntFromFrexpResult);
2252-
LoadTy = BV->getDataType();
2253-
}
2249+
if (IntFromFrexpResult->isUntypedVariable())
2250+
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(IntFromFrexpResult)
2251+
->getDataType();
2252+
22542253
IntFromFrexpResult =
22552254
BM->addLoadInst(IntFromFrexpResult, {}, BB, LoadTy);
22562255

@@ -2478,10 +2477,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
24782477
SPIRVValue *IntFromFrexpResult =
24792478
static_cast<SPIRVExtInst *>(Val)->getArgValues()[1];
24802479
SPIRVType *LoadTy = nullptr;
2481-
if (IntFromFrexpResult->isUntypedVariable()) {
2482-
auto *BV = static_cast<SPIRVUntypedVariableKHR *>(IntFromFrexpResult);
2483-
LoadTy = BV->getDataType();
2484-
}
2480+
if (IntFromFrexpResult->isUntypedVariable())
2481+
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(IntFromFrexpResult)
2482+
->getDataType();
2483+
24852484
IntFromFrexpResult =
24862485
BM->addLoadInst(IntFromFrexpResult, {}, BB, LoadTy);
24872486
return mapValue(V, IntFromFrexpResult);
@@ -6492,8 +6491,11 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
64926491

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

64996501
std::vector<SPIRVWord> Literals;
@@ -6583,8 +6585,11 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
65836585

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

65906595
std::vector<SPIRVWord> Literals;
@@ -6660,18 +6665,24 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
66606665

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

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

66706678
// InB - integer input of any width or 'byval' pointer to this integer
66716679
SPIRVValue *InB = transValue(*OpItr, BB);
6680+
LoadTy = nullptr;
6681+
if (InB->isUntypedVariable())
6682+
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(InB)->getDataType();
66726683
if (OpItr->getType()->isPointerTy()) {
66736684
std::vector<SPIRVWord> Mem;
6674-
InB = BM->addLoadInst(InB, Mem, BB);
6685+
InB = BM->addLoadInst(InB, Mem, BB, LoadTy);
66756686
}
66766687
OpItr++;
66776688

@@ -6751,7 +6762,10 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
67516762
transValue(CI->getArgOperand(0)->stripPointerCasts(), BB);
67526763
SPIRVId ScopeId = transValue(CI->getArgOperand(1), BB)->getId();
67536764
SPIRVValue *Delta = transValue(CI->getArgOperand(3), BB);
6754-
SPIRVValue *Composite0 = BM->addLoadInst(InValue, {}, BB);
6765+
SPIRVType *LoadTy = nullptr;
6766+
if (InValue->isUntypedVariable())
6767+
LoadTy = static_cast<SPIRVUntypedVariableKHR *>(InValue)->getDataType();
6768+
SPIRVValue *Composite0 = BM->addLoadInst(InValue, {}, BB, LoadTy);
67556769
Type *MemberTy = St->getElementType(0);
67566770
SPIRVType *ElementTy = transType(MemberTy);
67576771
SPIRVValue *Element0 =

llvm-spirv/test/extensions/INTEL/SPV_INTEL_arbitrary_precision_fixed_point/capability-arbitrary-precision-fixed-point-numbers.ll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@
100100
; CHECK-ERROR: InvalidInstruction: Can't translate llvm instruction:
101101
; CHECK-ERROR: Fixed point instructions can't be translated correctly without enabled SPV_INTEL_arbitrary_precision_fixed_point extension!
102102

103-
; RUN: llvm-spirv -r %t.spv -o %t.bc
104-
; RUN: llvm-dis < %t.bc | FileCheck %s --check-prefix=CHECK-LLVM
103+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
104+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
105+
106+
; Test with untyped pointers enabled.
107+
; 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
108+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
109+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
105110

106111
; CHECK-SPIRV: 2 Capability Kernel
107112
; CHECK-SPIRV: 2 Capability ArbitraryPrecisionIntegersINTEL

llvm-spirv/test/extensions/INTEL/SPV_INTEL_arbitrary_precision_floating_point/capability-arbitrary-precision-floating-point.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@
416416
; RUN: llvm-spirv -r %t.spv -o %t.r.bc
417417
; RUN: llvm-dis < %t.r.bc | FileCheck %s --check-prefix=CHECK-LLVM
418418

419+
; Test with untyped pointers enabled.
420+
; 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
421+
; RUN: llvm-spirv -r %t.spv -o %t.r.bc
422+
; RUN: llvm-dis < %t.r.bc | FileCheck %s --check-prefix=CHECK-LLVM
423+
419424
; CHECK-SPIRV: 2 Capability Kernel
420425
; CHECK-SPIRV: 2 Capability ArbitraryPrecisionIntegersINTEL
421426
; CHECK-SPIRV: 2 Capability ArbitraryPrecisionFloatingPointINTEL

llvm-spirv/test/group_non_uniform_shuffle_down.ll

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
; RUN: llvm-as %s -o %t.bc
2-
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck --check-prefix CHECK-SPIRV %s
2+
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-TYPED-PTR %s
33
; RUN: llvm-spirv %t.bc -o %t.spv
44
; RUN: spirv-val %t.spv
55
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
66
; RUN: llvm-dis %t.rev.bc -o - | FileCheck --check-prefix CHECK-LLVM %s
77

8+
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_KHR_untyped_pointers -spirv-text -o - | FileCheck --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-UNTYPED-PTR %s
9+
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_KHR_untyped_pointers -o %t.spv
10+
; RUN: spirv-val %t.spv
11+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
12+
; RUN: llvm-dis %t.rev.bc -o - | FileCheck --check-prefix CHECK-LLVM %s
13+
814
; CHECK-SPIRV-DAG: TypeInt [[#I32:]] 32 0
915
; CHECK-SPIRV-DAG: Constant [[#I32]] [[#CONST_I32_3:]] 3
1016
; CHECK-SPIRV-DAG: Constant [[#I32]] [[#CONST_I32_8:]] 8
1117
; CHECK-SPIRV-DAG: TypeFloat [[#HALF:]] 16
1218
; CHECK-SPIRV-DAG: TypeStruct [[#S_HALF:]] [[#HALF]]
13-
; CHECK-SPIRV-DAG: TypePointer [[#PTR_S_HALF:]] {{[0-9]+}} [[#S_HALF]]
19+
; CHECK-SPIRV-TYPED-PTR-DAG: TypePointer [[#PTR_S_HALF:]] {{[0-9]+}} [[#S_HALF]]
20+
; CHECK-SPIRV-UNTYPED-PTR-DAG: TypeUntypedPointerKHR [[#PTR:]] [[#]]
1421

1522
target triple = "spir64-unknown-unknown"
1623

@@ -25,8 +32,10 @@ entry:
2532
ret void
2633
}
2734

28-
; CHECK-SPIRV: Variable {{[0-9]+}} {{[0-9]+}}
29-
; CHECK-SPIRV: Variable [[#PTR_S_HALF]] [[#VAR_0:]]
35+
; CHECK-SPIRV-TYPED-PTR: Variable {{[0-9]+}} {{[0-9]+}}
36+
; CHECK-SPIRV-TYPED-PTR: Variable [[#PTR_S_HALF]] [[#VAR_0:]]
37+
; CHECK-SPIRV-UNTYPED-PTR: UntypedVariableKHR {{[0-9]+}} {{[0-9]+}}
38+
; CHECK-SPIRV-UNTYPED-PTR: UntypedVariableKHR [[#PTR]] [[#VAR_0:]] [[#HALF]]
3039
; CHECK-SPIRV: Load [[#S_HALF]] [[#COMP_0:]] [[#VAR_0]]
3140
; CHECK-SPIRV: CompositeExtract [[#HALF]] [[#ELEM_0:]] [[#COMP_0]] 0
3241
; CHECK-SPIRV: GroupNonUniformShuffleDown [[#HALF]] [[#ELEM_1:]] [[#CONST_I32_3]] [[#ELEM_0]] [[#CONST_I32_8]]

0 commit comments

Comments
 (0)