Skip to content

Commit a6f017a

Browse files
vmaksimoasudarsa
andauthored
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]>
1 parent 1a19f6f commit a6f017a

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,9 +2234,14 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
22342234
auto *CC = static_cast<SPIRVCompositeConstruct *>(BV);
22352235
auto Constituents = transValue(CC->getOperands(), F, BB);
22362236
std::vector<Constant *> CV;
2237+
bool HasRtValues = false;
22372238
for (const auto &I : Constituents) {
2238-
CV.push_back(dyn_cast<Constant>(I));
2239+
auto *C = dyn_cast<Constant>(I);
2240+
CV.push_back(C);
2241+
if (!HasRtValues && C == nullptr)
2242+
HasRtValues = true;
22392243
}
2244+
22402245
switch (static_cast<size_t>(BV->getType()->getOpCode())) {
22412246
case OpTypeVector:
22422247
return mapValue(BV, ConstantVector::get(CV));
@@ -2246,7 +2251,22 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
22462251
}
22472252
case OpTypeStruct: {
22482253
auto *ST = cast<StructType>(transType(CC->getType()));
2249-
return mapValue(BV, ConstantStruct::get(ST, CV));
2254+
if (!HasRtValues)
2255+
return mapValue(BV, ConstantStruct::get(ST, CV));
2256+
2257+
AllocaInst *Alloca = new AllocaInst(ST, SPIRAS_Private, "", BB);
2258+
2259+
// get pointer to the element of structure
2260+
// store the result of argument
2261+
for (size_t I = 0; I < Constituents.size(); I++) {
2262+
auto *GEP = GetElementPtrInst::Create(
2263+
Constituents[I]->getType(), Alloca, {getInt32(M, I)}, "gep", BB);
2264+
GEP->setIsInBounds(true);
2265+
new StoreInst(Constituents[I], GEP, false, BB);
2266+
}
2267+
2268+
auto *Load = new LoadInst(ST, Alloca, "load", false, BB);
2269+
return mapValue(BV, Load);
22502270
}
22512271
case internal::OpTypeJointMatrixINTEL:
22522272
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)