Skip to content

Commit a33d3af

Browse files
authored
Restore GEP instruction translation for vector of pointers (#1669)
Make GEP returning vector of pointers in this case Signed-off-by: Sidorov, Dmitry <[email protected]>
1 parent 81464b0 commit a33d3af

File tree

4 files changed

+105
-5
lines changed

4 files changed

+105
-5
lines changed

lib/SPIRV/SPIRVTypeScavenger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static bool doesNotImplyType(Value *V) {
252252

253253
SPIRVTypeScavenger::DeducedType
254254
SPIRVTypeScavenger::computePointerElementType(Value *V) {
255-
assert(V->getType()->isPointerTy() &&
255+
assert(V->getType()->isPtrOrPtrVectorTy() &&
256256
"Trying to get the pointer type of a non-pointer value?");
257257

258258
// Don't try to store null, undef, or poison in our type map. We'll call these

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,9 +2106,29 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
21062106
IndexGroupArrayMap[ContainedIndexGroup].insert(AccessedArrayId);
21072107
}
21082108
}
2109-
2110-
SPIRVType *TranslatedTy = transPointerType(
2111-
GEP->getResultElementType(), GEP->getType()->getPointerAddressSpace());
2109+
// GEP can return a vector of pointers, in this case GEP will calculate
2110+
// addresses for each pointer in the vector
2111+
SPIRVType *TranslatedTy = nullptr;
2112+
if (auto *VecPtrTy = dyn_cast<VectorType>(GEP->getType())) {
2113+
if (!VecPtrTy->getElementType()->isOpaquePointerTy()) {
2114+
TranslatedTy = transType(GEP->getType());
2115+
} else {
2116+
// Re-create vector type from GEP's result element type in opaque
2117+
// pointers mode
2118+
llvm::VectorType *GEPReturn = VectorType::get(
2119+
TypedPointerType::get(GEP->getResultElementType(),
2120+
VecPtrTy->getPointerAddressSpace()),
2121+
VecPtrTy->getElementCount());
2122+
TranslatedTy = transType(GEPReturn);
2123+
// Align Base with the return type
2124+
if (!GEP->getResultElementType()->isVoidTy())
2125+
TransPointerOperand = BM->addUnaryInst(OpBitcast, TranslatedTy,
2126+
TransPointerOperand, BB);
2127+
}
2128+
} else {
2129+
TranslatedTy = transPointerType(GEP->getResultElementType(),
2130+
GEP->getType()->getPointerAddressSpace());
2131+
}
21122132
return mapValue(V,
21132133
BM->addPtrAccessChainInst(TranslatedTy, TransPointerOperand,
21142134
Indices, BB, GEP->isInBounds()));

test/extensions/INTEL/SPV_INTEL_masked_gather_scatter/intel-basic-vector-pointers.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
; CHECK-SPIRV: Bitcast [[#VECTYPE3]]
3333
; CHECK-SPIRV: GenericCastToPtr [[#VECTYPE1]]
3434
; CHECK-SPIRV: FunctionCall [[#VECTYPE1]]
35-
; CHECK-SPIRV: InBoundsPtrAccessChain [[#PTRTYPE1]]
35+
; CHECK-SPIRV: InBoundsPtrAccessChain [[#VECTYPE1]]
3636

3737
; CHECK-LLVM: alloca <4 x i8 addrspace(4)*>
3838
; CHECK-LLVM-NEXT: alloca <4 x i8 addrspace(4)*>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_masked_gather_scatter -o %t.spv
3+
; RUN: llvm-spirv %t.spv --to-text -o %t.spt
4+
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
5+
6+
; RUN: llvm-spirv -r -emit-opaque-pointers=0 %t.spv -o %t.rev.bc
7+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
8+
9+
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o %t.rev.bc
10+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM-OPAQUE
11+
12+
; RUN: not llvm-spirv %t.bc 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
13+
; CHECK-ERROR: RequiresExtension: Feature requires the following SPIR-V extension:
14+
; CHECK-ERROR-NEXT: SPV_INTEL_masked_gather_scatter
15+
; CHECK-ERROR-NEXT: NOTE: LLVM module contains vector of pointers, translation of which requires this extension
16+
17+
18+
; CHECK-SPIRV-DAG: Capability MaskedGatherScatterINTEL
19+
; CHECK-SPIRV-DAG: Extension "SPV_INTEL_masked_gather_scatter"
20+
21+
; CHECK-SPIRV-DAG: TypeInt [[#TYPEINT1:]] 8 0
22+
; CHECK-SPIRV-DAG: TypeInt [[#TYPEINT2:]] 32 0
23+
; CHECK-SPIRV-DAG: TypePointer [[#TYPEPTR1:]] 5 [[#TYPEINT1]]
24+
; CHECK-SPIRV-DAG: TypeVector [[#TYPEVEC1:]] [[#TYPEPTR1]] 4
25+
; CHECK-SPIRV-DAG: TypeVoid [[#TYPEVOID:]]
26+
; CHECK-SPIRV-DAG: TypePointer [[#TYPEPTR2:]] [[#TYPEVOID]] 2
27+
; CHECK-SPIRV-DAG: TypeVector [[#TYPEVEC2:]] [[#TYPEPTR2]] 4
28+
; CHECK-SPIRV-DAG: TypePointer [[#PTRTOVECTYPE:]] 7 [[#TYPEVEC2]]
29+
; CHECK-SPIRV-DAG: TypePointer [[#TYPEPTR4:]] 5 [[#TYPEINT2]]
30+
; CHECK-SPIRV-DAG: TypeVector [[#TYPEVEC3:]] [[#TYPEPTR4]] 4
31+
32+
; CHECK-SPIRV: Variable [[#PTRTOVECTYPE]]
33+
; CHECK-SPIRV: Variable [[#PTRTOVECTYPE]]
34+
; CHECK-SPIRV: Load [[#TYPEVEC2]]
35+
; CHECK-SPIRV: Store
36+
; CHECK-SPIRV: GenericCastToPtr [[#TYPEVEC1]]
37+
; CHECK-SPIRV: FunctionCall [[#TYPEVEC1]]
38+
; CHECK-SPIRV: Bitcast [[#TYPEVEC3]]
39+
; CHECK-SPIRV: InBoundsPtrAccessChain [[#TYPEVEC3]]
40+
41+
; CHECK-LLVM: alloca <4 x i8 addrspace(4)*>
42+
; CHECK-LLVM: alloca <4 x i8 addrspace(4)*>
43+
; CHECK-LLVM: load <4 x i8 addrspace(4)*>, <4 x i8 addrspace(4)*>*
44+
; CHECK-LLVM: store <4 x i8 addrspace(4)*> %[[#]], <4 x i8 addrspace(4)*>*
45+
; CHECK-LLVM: addrspacecast <4 x i8 addrspace(4)*> %{{.*}} to <4 x i8 addrspace(1)*>
46+
; CHECK-LLVM: call spir_func <4 x i8 addrspace(1)*> @boo(<4 x i8 addrspace(1)*>
47+
; CHECK-LLVM: bitcast <4 x i8 addrspace(1)*> %{{.*}} to <4 x i32 addrspace(1)*>
48+
; CHECK-LLVM: getelementptr inbounds i32, <4 x i32 addrspace(1)*> %{{.*}}, i32 1
49+
50+
; CHECK-LLVM-OPAQUE: alloca <4 x ptr addrspace(4)>
51+
; CHECK-LLVM-OPAQUE: alloca <4 x ptr addrspace(4)>
52+
; CHECK-LLVM-OPAQUE: load <4 x ptr addrspace(4)>, ptr
53+
; CHECK-LLVM-OPAQUE: store <4 x ptr addrspace(4)> %[[#]], ptr
54+
; CHECK-LLVM-OPAQUE: addrspacecast <4 x ptr addrspace(4)> %{{.*}} to <4 x ptr addrspace(1)>
55+
; CHECK-LLVM-OPAQUE: call spir_func <4 x ptr addrspace(1)> @boo(<4 x ptr addrspace(1)>
56+
; CHECK-LLVM-OPAQUE: getelementptr inbounds i32, <4 x ptr addrspace(1)> %{{.*}}, i32 1
57+
58+
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"
59+
target triple = "spir"
60+
61+
; Function Attrs: nounwind readnone
62+
define spir_kernel void @foo() {
63+
entry:
64+
%arg1 = alloca <4 x ptr addrspace(4)>
65+
%arg2 = alloca <4 x ptr addrspace(4)>
66+
%0 = load <4 x ptr addrspace(4)>, ptr %arg1
67+
store <4 x ptr addrspace(4)> %0, ptr %arg2
68+
%tmp1 = addrspacecast <4 x ptr addrspace(4)> %0 to <4 x ptr addrspace(1)>
69+
%tmp2 = call <4 x ptr addrspace(1)> @boo(<4 x ptr addrspace(1)> %tmp1)
70+
%tmp3 = getelementptr inbounds i32, <4 x ptr addrspace(1)> %tmp2, i32 1
71+
ret void
72+
}
73+
74+
declare <4 x i32 addrspace(1)*> @boo(<4 x i32 addrspace(1)*> %a)
75+
76+
!llvm.module.flags = !{!0}
77+
!opencl.spir.version = !{!1}
78+
79+
!0 = !{i32 1, !"wchar_size", i32 4}
80+
!1 = !{i32 1, i32 2}

0 commit comments

Comments
 (0)