Skip to content

Commit 274fb83

Browse files
committed
[Backport to 16] Handle OpVectorShuffle with differing vector sizes (KhronosGroup#2391)
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 473c348 commit 274fb83

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 {
@@ -549,6 +550,10 @@ std::string mapLLVMTypeToOCLType(const Type *Ty, bool Signed,
549550
Type *PointerElementType = nullptr);
550551
SPIRVDecorate *mapPostfixToDecorate(StringRef Postfix, SPIRVEntry *Target);
551552

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

lib/SPIRV/SPIRVReader.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,10 +2227,37 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
22272227
if (BB) {
22282228
Builder.SetInsertPoint(BB);
22292229
}
2230-
return mapValue(BV, Builder.CreateShuffleVector(
2231-
transValue(VS->getVector1(), F, BB),
2232-
transValue(VS->getVector2(), F, BB),
2233-
ConstantVector::get(Components), BV->getName()));
2230+
Value *Vec1 = transValue(VS->getVector1(), F, BB);
2231+
Value *Vec2 = transValue(VS->getVector2(), F, BB);
2232+
auto *Vec1Ty = cast<FixedVectorType>(Vec1->getType());
2233+
auto *Vec2Ty = cast<FixedVectorType>(Vec2->getType());
2234+
if (Vec1Ty->getNumElements() != Vec2Ty->getNumElements()) {
2235+
// LLVM's shufflevector requires that the two vector operands have the
2236+
// same type; SPIR-V's OpVectorShuffle allows the vector operands to
2237+
// differ in the number of components. Adjust for that by extending
2238+
// the smaller vector.
2239+
if (Vec1Ty->getNumElements() < Vec2Ty->getNumElements()) {
2240+
Vec1 = extendVector(Vec1, Vec2Ty, Builder);
2241+
// Extending Vec1 requires offsetting any Vec2 indices in Components by
2242+
// the number of new elements.
2243+
unsigned Offset = Vec2Ty->getNumElements() - Vec1Ty->getNumElements();
2244+
unsigned Vec2Start = Vec1Ty->getNumElements();
2245+
for (auto &C : Components) {
2246+
if (auto *CI = dyn_cast<ConstantInt>(C)) {
2247+
uint64_t V = CI->getZExtValue();
2248+
if (V >= Vec2Start) {
2249+
// This is a Vec2 index; add the offset to it.
2250+
C = ConstantInt::get(Int32Ty, V + Offset);
2251+
}
2252+
}
2253+
}
2254+
} else {
2255+
Vec2 = extendVector(Vec2, Vec1Ty, Builder);
2256+
}
2257+
}
2258+
return mapValue(
2259+
BV, Builder.CreateShuffleVector(
2260+
Vec1, Vec2, ConstantVector::get(Components), BV->getName()));
22342261
}
22352262

22362263
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
@@ -2183,15 +2183,11 @@ class SPIRVVectorShuffleBase : public SPIRVInstTemplateBase {
21832183
protected:
21842184
void validate() const override {
21852185
SPIRVInstruction::validate();
2186-
SPIRVId Vector1 = Ops[0];
2187-
SPIRVId Vector2 = Ops[1];
2186+
[[maybe_unused]] SPIRVId Vector1 = Ops[0];
21882187
assert(OpCode == OpVectorShuffle);
21892188
assert(Type->isTypeVector());
21902189
assert(Type->getVectorComponentType() ==
21912190
getValueType(Vector1)->getVectorComponentType());
2192-
if (getValue(Vector1)->isForward() || getValue(Vector2)->isForward())
2193-
return;
2194-
assert(getValueType(Vector1) == getValueType(Vector2));
21952191
assert(Ops.size() - 2 == Type->getVectorComponentCount());
21962192
}
21972193
};

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 undef>
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 undef>
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)