Skip to content

Commit ad99707

Browse files
authored
[Backport to 18] Handle OpVectorShuffle with differing vector sizes (#2391) (#2409)
The SPIR-V to LLVM conversion would bail out when encountering an `OpVectorShuffle` whose vector operands differ in size. SPIR-V allows differing vector sizes, but LLVM's `shufflevector` does not. Remove the assert and insert an additional `shufflevector` to align the vector operands when needed. (cherry picked from commit 3df5e38)
1 parent d970c91 commit ad99707

File tree

5 files changed

+91
-9
lines changed

5 files changed

+91
-9
lines changed

lib/SPIRV/SPIRVInternal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ using namespace llvm;
6060

6161
namespace llvm {
6262
class IntrinsicInst;
63+
class IRBuilderBase;
6364
}
6465

6566
namespace SPIRV {
@@ -552,6 +553,10 @@ std::string mapLLVMTypeToOCLType(const Type *Ty, bool Signed,
552553
Type *PointerElementType = nullptr);
553554
SPIRVDecorate *mapPostfixToDecorate(StringRef Postfix, SPIRVEntry *Target);
554555

556+
/// Return vector V extended with poison elements to match the number of
557+
/// components of NewType.
558+
Value *extendVector(Value *V, FixedVectorType *NewType, IRBuilderBase &Builder);
559+
555560
/// Add decorations to a SPIR-V entry.
556561
/// \param Decs Each string is a postfix without _ at the beginning.
557562
SPIRVValue *addDecorations(SPIRVValue *Target,

lib/SPIRV/SPIRVReader.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,10 +2375,37 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
23752375
if (BB) {
23762376
Builder.SetInsertPoint(BB);
23772377
}
2378-
return mapValue(BV, Builder.CreateShuffleVector(
2379-
transValue(VS->getVector1(), F, BB),
2380-
transValue(VS->getVector2(), F, BB),
2381-
ConstantVector::get(Components), BV->getName()));
2378+
Value *Vec1 = transValue(VS->getVector1(), F, BB);
2379+
Value *Vec2 = transValue(VS->getVector2(), F, BB);
2380+
auto *Vec1Ty = cast<FixedVectorType>(Vec1->getType());
2381+
auto *Vec2Ty = cast<FixedVectorType>(Vec2->getType());
2382+
if (Vec1Ty->getNumElements() != Vec2Ty->getNumElements()) {
2383+
// LLVM's shufflevector requires that the two vector operands have the
2384+
// same type; SPIR-V's OpVectorShuffle allows the vector operands to
2385+
// differ in the number of components. Adjust for that by extending
2386+
// the smaller vector.
2387+
if (Vec1Ty->getNumElements() < Vec2Ty->getNumElements()) {
2388+
Vec1 = extendVector(Vec1, Vec2Ty, Builder);
2389+
// Extending Vec1 requires offsetting any Vec2 indices in Components by
2390+
// the number of new elements.
2391+
unsigned Offset = Vec2Ty->getNumElements() - Vec1Ty->getNumElements();
2392+
unsigned Vec2Start = Vec1Ty->getNumElements();
2393+
for (auto &C : Components) {
2394+
if (auto *CI = dyn_cast<ConstantInt>(C)) {
2395+
uint64_t V = CI->getZExtValue();
2396+
if (V >= Vec2Start) {
2397+
// This is a Vec2 index; add the offset to it.
2398+
C = ConstantInt::get(Int32Ty, V + Offset);
2399+
}
2400+
}
2401+
}
2402+
} else {
2403+
Vec2 = extendVector(Vec2, Vec1Ty, Builder);
2404+
}
2405+
}
2406+
return mapValue(
2407+
BV, Builder.CreateShuffleVector(
2408+
Vec1, Vec2, ConstantVector::get(Components), BV->getName()));
23822409
}
23832410

23842411
case OpBitReverse: {

lib/SPIRV/SPIRVUtil.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ void removeFnAttr(CallInst *Call, Attribute::AttrKind Attr) {
9494
Call->removeFnAttr(Attr);
9595
}
9696

97+
Value *extendVector(Value *V, FixedVectorType *NewType,
98+
IRBuilderBase &Builder) {
99+
unsigned OldSize = cast<FixedVectorType>(V->getType())->getNumElements();
100+
unsigned NewSize = NewType->getNumElements();
101+
assert(OldSize < NewSize);
102+
std::vector<Constant *> Components;
103+
IntegerType *Int32Ty = Builder.getInt32Ty();
104+
for (unsigned I = 0; I < NewSize; I++) {
105+
if (I < OldSize)
106+
Components.push_back(ConstantInt::get(Int32Ty, I));
107+
else
108+
Components.push_back(PoisonValue::get(Int32Ty));
109+
}
110+
111+
return Builder.CreateShuffleVector(V, PoisonValue::get(V->getType()),
112+
ConstantVector::get(Components), "vecext");
113+
}
114+
97115
void saveLLVMModule(Module *M, const std::string &OutputFile) {
98116
std::error_code EC;
99117
ToolOutputFile Out(OutputFile.c_str(), EC, sys::fs::OF_None);

lib/SPIRV/libSPIRV/SPIRVInstruction.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,15 +2210,11 @@ class SPIRVVectorShuffleBase : public SPIRVInstTemplateBase {
22102210
protected:
22112211
void validate() const override {
22122212
SPIRVInstruction::validate();
2213-
SPIRVId Vector1 = Ops[0];
2214-
SPIRVId Vector2 = Ops[1];
2213+
[[maybe_unused]] SPIRVId Vector1 = Ops[0];
22152214
assert(OpCode == OpVectorShuffle);
22162215
assert(Type->isTypeVector());
22172216
assert(Type->getVectorComponentType() ==
22182217
getValueType(Vector1)->getVectorComponentType());
2219-
if (getValue(Vector1)->isForward() || getValue(Vector2)->isForward())
2220-
return;
2221-
assert(getValueType(Vector1) == getValueType(Vector2));
22222218
assert(Ops.size() - 2 == Type->getVectorComponentCount());
22232219
}
22242220
};

test/OpVectorShuffle.spvasm

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; REQUIRES: spirv-as
2+
; RUN: spirv-as --target-env spv1.0 -o %t.spv %s
3+
; RUN: spirv-val %t.spv
4+
; RUN: llvm-spirv -r -o - %t.spv | llvm-dis | FileCheck %s
5+
OpCapability Addresses
6+
OpCapability Kernel
7+
OpMemoryModel Physical32 OpenCL
8+
OpEntryPoint Kernel %1 "testVecShuffle"
9+
%void = OpTypeVoid
10+
%uint = OpTypeInt 32 0
11+
%uintv2 = OpTypeVector %uint 2
12+
%uintv3 = OpTypeVector %uint 3
13+
%uintv4 = OpTypeVector %uint 4
14+
%func = OpTypeFunction %void %uintv2 %uintv3
15+
16+
%1 = OpFunction %void None %func
17+
%pv2 = OpFunctionParameter %uintv2
18+
%pv3 = OpFunctionParameter %uintv3
19+
%entry = OpLabel
20+
21+
; Same vector lengths
22+
%vs1 = OpVectorShuffle %uintv4 %pv3 %pv3 0 1 3 5
23+
; CHECK: shufflevector <3 x i32> %[[#]], <3 x i32> %[[#]], <4 x i32> <i32 0, i32 1, i32 3, i32 5>
24+
25+
; vec1 smaller than vec2
26+
%vs2 = OpVectorShuffle %uintv4 %pv2 %pv3 0 1 3 4
27+
; CHECK: %[[VS2EXT:[0-9a-z]+]] = shufflevector <2 x i32> %0, <2 x i32> poison, <3 x i32> <i32 0, i32 1, i32 poison>
28+
; CHECK: shufflevector <3 x i32> %[[VS2EXT]], <3 x i32> %[[#]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
29+
30+
; vec1 larger than vec2
31+
%vs3 = OpVectorShuffle %uintv4 %pv3 %pv2 0 1 3 4
32+
; CHECK: %[[VS3EXT:[0-9a-z]+]] = shufflevector <2 x i32> %0, <2 x i32> poison, <3 x i32> <i32 0, i32 1, i32 poison>
33+
; CHECK: shufflevector <3 x i32> %[[#]], <3 x i32> %[[VS3EXT]], <4 x i32> <i32 0, i32 1, i32 3, i32 4>
34+
35+
OpReturn
36+
OpFunctionEnd

0 commit comments

Comments
 (0)