Skip to content

Commit 832e51f

Browse files
vmaksimosys-ce-bb
authored andcommitted
Fix reverse translation of non-constant values of OpCompositeConstruct (#2256)
This patch introduces a way to use runtime values for structure fields. Co-authored-by: Arvind Sudarsanam <[email protected]> Original commit: KhronosGroup/SPIRV-LLVM-Translator@a6f017a
1 parent 7d1d83d commit 832e51f

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,9 +2235,14 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
22352235
auto *CC = static_cast<SPIRVCompositeConstruct *>(BV);
22362236
auto Constituents = transValue(CC->getOperands(), F, BB);
22372237
std::vector<Constant *> CV;
2238+
bool HasRtValues = false;
22382239
for (const auto &I : Constituents) {
2239-
CV.push_back(dyn_cast<Constant>(I));
2240+
auto *C = dyn_cast<Constant>(I);
2241+
CV.push_back(C);
2242+
if (!HasRtValues && C == nullptr)
2243+
HasRtValues = true;
22402244
}
2245+
22412246
switch (static_cast<size_t>(BV->getType()->getOpCode())) {
22422247
case OpTypeVector:
22432248
return mapValue(BV, ConstantVector::get(CV));
@@ -2247,7 +2252,22 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
22472252
}
22482253
case OpTypeStruct: {
22492254
auto *ST = cast<StructType>(transType(CC->getType()));
2250-
return mapValue(BV, ConstantStruct::get(ST, CV));
2255+
if (!HasRtValues)
2256+
return mapValue(BV, ConstantStruct::get(ST, CV));
2257+
2258+
AllocaInst *Alloca = new AllocaInst(ST, SPIRAS_Private, "", BB);
2259+
2260+
// get pointer to the element of structure
2261+
// store the result of argument
2262+
for (size_t I = 0; I < Constituents.size(); I++) {
2263+
auto *GEP = GetElementPtrInst::Create(
2264+
Constituents[I]->getType(), Alloca, {getInt32(M, I)}, "gep", BB);
2265+
GEP->setIsInBounds(true);
2266+
new StoreInst(Constituents[I], GEP, false, BB);
2267+
}
2268+
2269+
auto *Load = new LoadInst(ST, Alloca, "load", false, BB);
2270+
return mapValue(BV, Load);
22512271
}
22522272
case internal::OpTypeJointMatrixINTEL:
22532273
case OpTypeCooperativeMatrixKHR:
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; RUN: llvm-spirv %s -to-binary -o %t.spv
2+
; RUN: spirv-val %t.spv
3+
; RUN: llvm-spirv -r %t.spv -o %t.bc
4+
; RUN: llvm-dis %t.bc
5+
; RUN: FileCheck < %t.ll %s --check-prefix=CHECK-LLVM
6+
7+
; CHECK-LLVM: %[[StructTy:[0-9a-z.]+]] = type { float, i32 }
8+
; CHECK-LLVM: %[[#StructPtr:]] = alloca %[[StructTy]]
9+
; CHECK-LLVM: %[[GEP:[0-9a-z.]+]] = getelementptr inbounds float, ptr %[[#StructPtr]], i32 0
10+
; CHECK-LLVM: store float %[[#]], ptr %[[GEP]]
11+
; CHECK-LLVM: %[[GEP1:[0-9a-z.]+]] = getelementptr inbounds i32, ptr %[[#StructPtr]], i32 1
12+
; CHECK-LLVM: store i32 %[[#]], ptr %[[GEP1]]
13+
; CHECK-LLVM: %[[LoadStr:[0-9a-z.]+]] = load %[[StructTy]], ptr %[[#StructPtr]]
14+
; CHECK-LLVM: ret %[[StructTy]] %[[LoadStr]]
15+
16+
119734787 65536 393230 23 0
17+
2 Capability Addresses
18+
2 Capability Linkage
19+
2 Capability Kernel
20+
5 ExtInstImport 1 "OpenCL.std"
21+
3 MemoryModel 2 2
22+
3 Source 0 0
23+
5 Name 2 "structtype"
24+
9 Name 6 "non_constant_struct_fields"
25+
11 Decorate 6 LinkageAttributes "non_constant_struct_fields" Export
26+
4 Decorate 9 Alignment 4
27+
4 Decorate 11 Alignment 4
28+
4 TypeInt 4 32 0
29+
4 Constant 4 17 0
30+
4 Constant 4 20 1
31+
3 TypeFloat 3 32
32+
4 TypeStruct 2 3 4
33+
34+
3 TypeFunction 5 2
35+
4 TypePointer 8 7 4
36+
4 TypePointer 10 7 3
37+
38+
5 Function 2 6 0 5
39+
40+
2 Label 7
41+
4 Variable 8 9 7
42+
4 Variable 10 11 7
43+
4 Load 4 12 9
44+
4 Load 3 13 11
45+
5 CompositeConstruct 2 14 13 12
46+
2 ReturnValue 14
47+
48+
1 FunctionEnd

0 commit comments

Comments
 (0)