Skip to content

Commit 9bc1d5a

Browse files
committed
Handle OpSpecConstantOp with CompositeExtract and CompositeInsert
Modify the constructors to allow passing a nullptr basic block (as is the case for variable initializers). Modify the constructors to take `SPIRVId`s instead of `SPIRVValue`s, which better reflects what is stored in the class, and saves us an unnecessary ID-to-Value-to-ID round trip in `createInstFromSpecConstantOp`. Modify test/opundef.spt to return the constructed value, so that it does not get optimized out.
1 parent 72f99e3 commit 9bc1d5a

File tree

6 files changed

+69
-37
lines changed

6 files changed

+69
-37
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,17 +2317,21 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
23172317

23182318
case OpCompositeExtract: {
23192319
SPIRVCompositeExtract *CE = static_cast<SPIRVCompositeExtract *>(BV);
2320+
IRBuilder<> Builder(*Context);
2321+
if (BB) {
2322+
Builder.SetInsertPoint(BB);
2323+
}
23202324
if (CE->getComposite()->getType()->isTypeVector()) {
23212325
assert(CE->getIndices().size() == 1 && "Invalid index");
23222326
return mapValue(
2323-
BV, ExtractElementInst::Create(
2327+
BV, Builder.CreateExtractElement(
23242328
transValue(CE->getComposite(), F, BB),
23252329
ConstantInt::get(*Context, APInt(32, CE->getIndices()[0])),
2326-
BV->getName(), BB));
2330+
BV->getName()));
23272331
}
23282332
return mapValue(
2329-
BV, ExtractValueInst::Create(transValue(CE->getComposite(), F, BB),
2330-
CE->getIndices(), BV->getName(), BB));
2333+
BV, Builder.CreateExtractValue(transValue(CE->getComposite(), F, BB),
2334+
CE->getIndices(), BV->getName()));
23312335
}
23322336

23332337
case OpVectorExtractDynamic: {
@@ -2340,19 +2344,23 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
23402344

23412345
case OpCompositeInsert: {
23422346
auto CI = static_cast<SPIRVCompositeInsert *>(BV);
2347+
IRBuilder<> Builder(*Context);
2348+
if (BB) {
2349+
Builder.SetInsertPoint(BB);
2350+
}
23432351
if (CI->getComposite()->getType()->isTypeVector()) {
23442352
assert(CI->getIndices().size() == 1 && "Invalid index");
23452353
return mapValue(
2346-
BV, InsertElementInst::Create(
2354+
BV, Builder.CreateInsertElement(
23472355
transValue(CI->getComposite(), F, BB),
23482356
transValue(CI->getObject(), F, BB),
23492357
ConstantInt::get(*Context, APInt(32, CI->getIndices()[0])),
2350-
BV->getName(), BB));
2358+
BV->getName()));
23512359
}
23522360
return mapValue(
2353-
BV, InsertValueInst::Create(transValue(CI->getComposite(), F, BB),
2354-
transValue(CI->getObject(), F, BB),
2355-
CI->getIndices(), BV->getName(), BB));
2361+
BV, Builder.CreateInsertValue(transValue(CI->getComposite(), F, BB),
2362+
transValue(CI->getObject(), F, BB),
2363+
CI->getIndices(), BV->getName()));
23562364
}
23572365

23582366
case OpVectorInsertDynamic: {

lib/SPIRV/libSPIRV/SPIRVInstruction.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ SPIRVInstruction *createInstFromSpecConstantOp(SPIRVSpecConstantOp *Inst) {
253253
return new SPIRVVectorShuffle(Inst->getId(), Inst->getType(), Ops[0],
254254
Ops[1], Comp, nullptr, Inst->getModule());
255255
}
256+
case OpCompositeExtract: {
257+
std::vector<SPIRVWord> Indices;
258+
for (auto I = Ops.begin() + 1, E = Ops.end(); I != E; ++I) {
259+
Indices.push_back(*I);
260+
}
261+
return new SPIRVCompositeExtract(Inst->getType(), Inst->getId(), Ops[0],
262+
Indices, nullptr, Inst->getModule());
263+
}
264+
case OpCompositeInsert: {
265+
std::vector<SPIRVWord> Indices;
266+
for (auto I = Ops.begin() + 2, E = Ops.end(); I != E; ++I) {
267+
Indices.push_back(*I);
268+
}
269+
return new SPIRVCompositeInsert(Inst->getType(), Inst->getId(), Ops[0],
270+
Ops[1], Indices, nullptr,
271+
Inst->getModule());
272+
}
256273
case OpSelect:
257274
return new SPIRVSelect(Inst->getId(), Inst->getType(), Ops[0], Ops[1],
258275
Ops[2], nullptr, Inst->getModule());

lib/SPIRV/libSPIRV/SPIRVInstruction.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,14 +1898,13 @@ class SPIRVCompositeExtract : public SPIRVInstruction {
18981898
public:
18991899
const static Op OC = OpCompositeExtract;
19001900
// Complete constructor
1901-
SPIRVCompositeExtract(SPIRVType *TheType, SPIRVId TheId,
1902-
SPIRVValue *TheComposite,
1901+
SPIRVCompositeExtract(SPIRVType *TheType, SPIRVId TheId, SPIRVId TheComposite,
19031902
const std::vector<SPIRVWord> &TheIndices,
1904-
SPIRVBasicBlock *TheBB)
1905-
: SPIRVInstruction(TheIndices.size() + 4, OC, TheType, TheId, TheBB),
1906-
Composite(TheComposite->getId()), Indices(TheIndices) {
1903+
SPIRVBasicBlock *TheBB, SPIRVModule *TheM)
1904+
: SPIRVInstruction(TheIndices.size() + 4, OC, TheType, TheId, TheBB,
1905+
TheM),
1906+
Composite(TheComposite), Indices(TheIndices) {
19071907
validate();
1908-
assert(TheBB && "Invalid BB");
19091908
}
19101909
// Incomplete constructor
19111910
SPIRVCompositeExtract() : SPIRVInstruction(OC), Composite(SPIRVID_INVALID) {}
@@ -1936,16 +1935,14 @@ class SPIRVCompositeInsert : public SPIRVInstruction {
19361935
const static Op OC = OpCompositeInsert;
19371936
const static SPIRVWord FixedWordCount = 5;
19381937
// Complete constructor
1939-
SPIRVCompositeInsert(SPIRVId TheId, SPIRVValue *TheObject,
1940-
SPIRVValue *TheComposite,
1938+
SPIRVCompositeInsert(SPIRVType *TheType, SPIRVId TheId, SPIRVId TheObject,
1939+
SPIRVId TheComposite,
19411940
const std::vector<SPIRVWord> &TheIndices,
1942-
SPIRVBasicBlock *TheBB)
1943-
: SPIRVInstruction(TheIndices.size() + FixedWordCount, OC,
1944-
TheComposite->getType(), TheId, TheBB),
1945-
Object(TheObject->getId()), Composite(TheComposite->getId()),
1946-
Indices(TheIndices) {
1941+
SPIRVBasicBlock *TheBB, SPIRVModule *TheM)
1942+
: SPIRVInstruction(TheIndices.size() + FixedWordCount, OC, TheType, TheId,
1943+
TheBB, TheM),
1944+
Object(TheObject), Composite(TheComposite), Indices(TheIndices) {
19471945
validate();
1948-
assert(TheBB && "Invalid BB");
19491946
}
19501947
// Incomplete constructor
19511948
SPIRVCompositeInsert()

lib/SPIRV/libSPIRV/SPIRVModule.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,15 +1523,19 @@ SPIRVInstruction *
15231523
SPIRVModuleImpl::addCompositeExtractInst(SPIRVType *Type, SPIRVValue *TheVector,
15241524
const std::vector<SPIRVWord> &Indices,
15251525
SPIRVBasicBlock *BB) {
1526-
return addInstruction(
1527-
new SPIRVCompositeExtract(Type, getId(), TheVector, Indices, BB), BB);
1526+
return addInstruction(new SPIRVCompositeExtract(Type, getId(),
1527+
TheVector->getId(), Indices,
1528+
BB, this),
1529+
BB);
15281530
}
15291531

15301532
SPIRVInstruction *SPIRVModuleImpl::addCompositeInsertInst(
15311533
SPIRVValue *Object, SPIRVValue *Composite,
15321534
const std::vector<SPIRVWord> &Indices, SPIRVBasicBlock *BB) {
15331535
return addInstruction(
1534-
new SPIRVCompositeInsert(getId(), Object, Composite, Indices, BB), BB);
1536+
new SPIRVCompositeInsert(Composite->getType(), getId(), Object->getId(),
1537+
Composite->getId(), Indices, BB, this),
1538+
BB);
15351539
}
15361540

15371541
SPIRVInstruction *SPIRVModuleImpl::addCopyObjectInst(SPIRVType *TheType,

test/SpecConstants/specconstantop-init.spvasm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
; CHECK: @var_bitxor = addrspace(1) global i32 -55
3232
; CHECK: @var_bitand = addrspace(1) global i32 52
3333
; CHECK: @var_vecshuf = addrspace(1) global <2 x i32> <i32 4, i32 53>
34+
; CHECK: @var_compext = addrspace(1) global i32 53
35+
; CHECK: @var_compins = addrspace(1) global <2 x i32> <i32 53, i32 53>
3436
; CHECK: @var_logor = addrspace(1) global i1 true
3537
; CHECK: @var_logand = addrspace(1) global i1 false
3638
; CHECK: @var_lognot = addrspace(1) global i1 false
@@ -78,6 +80,8 @@
7880
OpDecorate %var_bitxor LinkageAttributes "var_bitxor" Export
7981
OpDecorate %var_bitand LinkageAttributes "var_bitand" Export
8082
OpDecorate %var_vecshuf LinkageAttributes "var_vecshuf" Export
83+
OpDecorate %var_compext LinkageAttributes "var_compext" Export
84+
OpDecorate %var_compins LinkageAttributes "var_compins" Export
8185
OpDecorate %var_logor LinkageAttributes "var_logor" Export
8286
OpDecorate %var_logand LinkageAttributes "var_logand" Export
8387
OpDecorate %var_lognot LinkageAttributes "var_lognot" Export
@@ -131,6 +135,8 @@
131135
%bitxor = OpSpecConstantOp %uint BitwiseXor %uint_53 %uint_min4
132136
%bitand = OpSpecConstantOp %uint BitwiseAnd %uint_53 %uint_min4
133137
%vecshuf = OpSpecConstantOp %v2i32 VectorShuffle %vec_53_0 %vec_4_4 2 0
138+
%compext = OpSpecConstantOp %uint CompositeExtract %vec_53_0 0
139+
%compins = OpSpecConstantOp %v2i32 CompositeInsert %uint_53 %vec_53_0 1
134140
%logor = OpSpecConstantOp %bool LogicalOr %true %false
135141
%logand = OpSpecConstantOp %bool LogicalAnd %true %false
136142
%lognot = OpSpecConstantOp %bool LogicalNot %true
@@ -177,6 +183,8 @@
177183
%var_bitxor = OpVariable %_ptr_uint CrossWorkgroup %bitxor
178184
%var_bitand = OpVariable %_ptr_uint CrossWorkgroup %bitand
179185
%var_vecshuf = OpVariable %_ptr_v2i32 CrossWorkgroup %vecshuf
186+
%var_compext = OpVariable %_ptr_uint CrossWorkgroup %compext
187+
%var_compins = OpVariable %_ptr_v2i32 CrossWorkgroup %compins
180188
%var_logor = OpVariable %_ptr_bool CrossWorkgroup %logor
181189
%var_logand = OpVariable %_ptr_bool CrossWorkgroup %logand
182190
%var_lognot = OpVariable %_ptr_bool CrossWorkgroup %lognot

test/opundef.spt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616
4 TypeInt 7 32 0
1717
4 Constant 7 10 1
1818
2 TypeVoid 2
19-
3 TypeFunction 3 2
2019
3 TypeFloat 8 32
2120
4 TypeStruct 6 7 8
21+
3 TypeFunction 3 6
2222

23-
5 Function 2 4 0 3
23+
5 Function 6 4 0 3
2424

2525
2 Label 5
2626
3 Undef 6 9
2727
6 CompositeInsert 6 11 10 9 0
28-
1 Return
28+
2 ReturnValue 11
2929

3030
1 FunctionEnd
3131

32-
5 Function 2 12 0 3
32+
5 Function 6 12 0 3
3333

3434
2 Label 13
3535
3 Undef 6 15
3636
6 CompositeInsert 6 14 10 15 0
37-
1 Return
37+
2 ReturnValue 14
3838

3939
1 FunctionEnd
4040

@@ -43,14 +43,12 @@
4343
; RUN: llvm-spirv -r %t.spv -o %t.bc
4444
; RUN: llvm-dis < %t.bc | FileCheck %s
4545

46-
; CHECK: define spir_func void @foo() #0 {
46+
; CHECK: define spir_func %structtype @foo() #0 {
4747
; CHECK-NEXT: entry:
48-
; CHECK-NEXT: %agg1 = insertvalue %{{[0-9a-z\.]*}} undef
49-
; CHECK-NEXT: ret void
48+
; CHECK-NEXT: ret %structtype { i32 1, float undef }
5049
; CHECK-NEXT: }
51-
; CHECK: define spir_func void @bar() #0 {
50+
; CHECK: define spir_func %structtype @bar() #0 {
5251
; CHECK-NEXT: entry:
53-
; CHECK-NEXT: %agg2 = insertvalue %{{[0-9a-z\.]*}} undef
54-
; CHECK-NEXT: ret void
52+
; CHECK-NEXT: ret %structtype { i32 1, float undef }
5553
; CHECK-NEXT: }
5654

0 commit comments

Comments
 (0)