Skip to content

Commit a087f3e

Browse files
svenvhvmaksimo
authored andcommitted
Handle OpSpecConstantOp with VectorShuffle
Modify the `SPIRVShuffleVector` constructor to allow passing a nullptr basic block (as is the case for variable initializers). Modify the `SPIRVShuffleVector` constructor 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`. Original commit: KhronosGroup/SPIRV-LLVM-Translator@72f99e3
1 parent 39d0d53 commit a087f3e

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,11 +2375,14 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
23752375
else
23762376
Components.push_back(ConstantInt::get(Int32Ty, I));
23772377
}
2378-
return mapValue(BV,
2379-
new ShuffleVectorInst(transValue(VS->getVector1(), F, BB),
2380-
transValue(VS->getVector2(), F, BB),
2381-
ConstantVector::get(Components),
2382-
BV->getName(), BB));
2378+
IRBuilder<> Builder(*Context);
2379+
if (BB) {
2380+
Builder.SetInsertPoint(BB);
2381+
}
2382+
return mapValue(BV, Builder.CreateShuffleVector(
2383+
transValue(VS->getVector1(), F, BB),
2384+
transValue(VS->getVector2(), F, BB),
2385+
ConstantVector::get(Components), BV->getName()));
23832386
}
23842387

23852388
case OpBitReverse: {

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVInstruction.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ SPIRVInstruction *createInstFromSpecConstantOp(SPIRVSpecConstantOp *Inst) {
245245
"Op code not allowed for OpSpecConstantOp");
246246
Ops.erase(Ops.begin(), Ops.begin() + 1);
247247
switch (OC) {
248+
case OpVectorShuffle: {
249+
std::vector<SPIRVWord> Comp;
250+
for (auto I = Ops.begin() + 2, E = Ops.end(); I != E; ++I) {
251+
Comp.push_back(*I);
252+
}
253+
return new SPIRVVectorShuffle(Inst->getId(), Inst->getType(), Ops[0],
254+
Ops[1], Comp, nullptr, Inst->getModule());
255+
}
248256
case OpSelect:
249257
return new SPIRVSelect(Inst->getId(), Inst->getType(), Ops[0], Ops[1],
250258
Ops[2], nullptr, Inst->getModule());

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVInstruction.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,16 +2179,14 @@ class SPIRVVectorShuffle : public SPIRVInstruction {
21792179
const static Op OC = OpVectorShuffle;
21802180
const static SPIRVWord FixedWordCount = 5;
21812181
// Complete constructor
2182-
SPIRVVectorShuffle(SPIRVId TheId, SPIRVType *TheType, SPIRVValue *TheVector1,
2183-
SPIRVValue *TheVector2,
2182+
SPIRVVectorShuffle(SPIRVId TheId, SPIRVType *TheType, SPIRVId TheVector1,
2183+
SPIRVId TheVector2,
21842184
const std::vector<SPIRVWord> &TheComponents,
2185-
SPIRVBasicBlock *TheBB)
2185+
SPIRVBasicBlock *TheBB, SPIRVModule *TheM)
21862186
: SPIRVInstruction(TheComponents.size() + FixedWordCount, OC, TheType,
2187-
TheId, TheBB),
2188-
Vector1(TheVector1->getId()), Vector2(TheVector2->getId()),
2189-
Components(TheComponents) {
2187+
TheId, TheBB, TheM),
2188+
Vector1(TheVector1), Vector2(TheVector2), Components(TheComponents) {
21902189
validate();
2191-
assert(TheBB && "Invalid BB");
21922190
}
21932191
// Incomplete constructor
21942192
SPIRVVectorShuffle()

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVModule.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,8 +1367,10 @@ SPIRVInstruction *SPIRVModuleImpl::addVectorInsertDynamicInst(
13671367
SPIRVValue *SPIRVModuleImpl::addVectorShuffleInst(
13681368
SPIRVType *Type, SPIRVValue *Vec1, SPIRVValue *Vec2,
13691369
const std::vector<SPIRVWord> &Components, SPIRVBasicBlock *BB) {
1370-
return addInstruction(
1371-
new SPIRVVectorShuffle(getId(), Type, Vec1, Vec2, Components, BB), BB);
1370+
return addInstruction(new SPIRVVectorShuffle(getId(), Type, Vec1->getId(),
1371+
Vec2->getId(), Components, BB,
1372+
this),
1373+
BB);
13721374
}
13731375

13741376
SPIRVInstruction *SPIRVModuleImpl::addBranchInst(SPIRVLabel *TargetLabel,

llvm-spirv/test/SpecConstants/specconstantop-init.spvasm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
; CHECK: @var_bitor = addrspace(1) global i32 -3
3131
; CHECK: @var_bitxor = addrspace(1) global i32 -55
3232
; CHECK: @var_bitand = addrspace(1) global i32 52
33+
; CHECK: @var_vecshuf = addrspace(1) global <2 x i32> <i32 4, i32 53>
3334
; CHECK: @var_logor = addrspace(1) global i1 true
3435
; CHECK: @var_logand = addrspace(1) global i1 false
3536
; CHECK: @var_lognot = addrspace(1) global i1 false
@@ -76,6 +77,7 @@
7677
OpDecorate %var_bitor LinkageAttributes "var_bitor" Export
7778
OpDecorate %var_bitxor LinkageAttributes "var_bitxor" Export
7879
OpDecorate %var_bitand LinkageAttributes "var_bitand" Export
80+
OpDecorate %var_vecshuf LinkageAttributes "var_vecshuf" Export
7981
OpDecorate %var_logor LinkageAttributes "var_logor" Export
8082
OpDecorate %var_logand LinkageAttributes "var_logand" Export
8183
OpDecorate %var_lognot LinkageAttributes "var_lognot" Export
@@ -99,11 +101,14 @@
99101
%uchar = OpTypeInt 8 0
100102
%uint = OpTypeInt 32 0
101103
%float = OpTypeFloat 32
104+
%v2i32 = OpTypeVector %uint 2
102105
%uint_0 = OpConstant %uint 0
103106
%uint_4 = OpConstant %uint 4
104107
%uint_53 = OpConstant %uint 53
105108
%uint_min4 = OpConstant %uint 0xfffffffc
106109
%float_1 = OpConstant %float 1.0
110+
%vec_53_0 = OpConstantComposite %v2i32 %uint_53 %uint_0
111+
%vec_4_4 = OpConstantComposite %v2i32 %uint_4 %uint_4
107112
%sconvert = OpSpecConstantOp %uchar SConvert %uint_53
108113
%uconvert = OpSpecConstantOp %uchar UConvert %uint_53
109114
%snegate = OpSpecConstantOp %uint SNegate %uint_53
@@ -125,6 +130,7 @@
125130
%bitor = OpSpecConstantOp %uint BitwiseOr %uint_53 %uint_min4
126131
%bitxor = OpSpecConstantOp %uint BitwiseXor %uint_53 %uint_min4
127132
%bitand = OpSpecConstantOp %uint BitwiseAnd %uint_53 %uint_min4
133+
%vecshuf = OpSpecConstantOp %v2i32 VectorShuffle %vec_53_0 %vec_4_4 2 0
128134
%logor = OpSpecConstantOp %bool LogicalOr %true %false
129135
%logand = OpSpecConstantOp %bool LogicalAnd %true %false
130136
%lognot = OpSpecConstantOp %bool LogicalNot %true
@@ -145,6 +151,7 @@
145151
%_ptr_uchar = OpTypePointer CrossWorkgroup %uchar
146152
%_ptr_uint = OpTypePointer CrossWorkgroup %uint
147153
%_ptr_bool = OpTypePointer CrossWorkgroup %bool
154+
%_ptr_v2i32 = OpTypePointer CrossWorkgroup %v2i32
148155
%void = OpTypeVoid
149156
%14 = OpTypeFunction %void
150157

@@ -169,6 +176,7 @@
169176
%var_bitor = OpVariable %_ptr_uint CrossWorkgroup %bitor
170177
%var_bitxor = OpVariable %_ptr_uint CrossWorkgroup %bitxor
171178
%var_bitand = OpVariable %_ptr_uint CrossWorkgroup %bitand
179+
%var_vecshuf = OpVariable %_ptr_v2i32 CrossWorkgroup %vecshuf
172180
%var_logor = OpVariable %_ptr_bool CrossWorkgroup %logor
173181
%var_logand = OpVariable %_ptr_bool CrossWorkgroup %logand
174182
%var_lognot = OpVariable %_ptr_bool CrossWorkgroup %lognot

0 commit comments

Comments
 (0)