Skip to content

Commit 412025f

Browse files
svenvhjsji
authored andcommitted
SPIRVReader: Support LocalSizeId (#2898)
If there is no `OpExecutionMode .. LocalSize` in the input, see if there is an `OpExecutionModeId .. LocalSizeId` and take the value for the `reqd_work_group_size` metadata from the referenced constants instead. Once `LocalSizeId` has been translated to LLVM IR, it is indistinguishable from a (non-ID) `LocalSize` execution mode. Original commit: KhronosGroup/SPIRV-LLVM-Translator@d20ca5dd1c92493
1 parent 20362b5 commit 412025f

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4552,6 +4552,14 @@ bool SPIRVToLLVM::transMetadata() {
45524552
if (auto *EM = BF->getExecutionMode(ExecutionModeLocalSize)) {
45534553
F->setMetadata(kSPIR2MD::WGSize,
45544554
getMDNodeStringIntVec(Context, EM->getLiterals()));
4555+
} else if (auto *EM = BF->getExecutionModeId(ExecutionModeLocalSizeId)) {
4556+
std::vector<SPIRVWord> Values;
4557+
for (const auto Id : EM->getLiterals()) {
4558+
if (auto Val = transIdAsConstant(Id)) {
4559+
Values.emplace_back(static_cast<SPIRVWord>(*Val));
4560+
}
4561+
}
4562+
F->setMetadata(kSPIR2MD::WGSize, getMDNodeStringIntVec(Context, Values));
45554563
}
45564564
// Generate metadata for work_group_size_hint
45574565
if (auto *EM = BF->getExecutionMode(ExecutionModeLocalSizeHint)) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ void SPIRVExecutionMode::decode(std::istream &I) {
656656
getDecoder(I) >> Target >> ExecMode;
657657
switch (static_cast<uint32_t>(ExecMode)) {
658658
case ExecutionModeLocalSize:
659+
case ExecutionModeLocalSizeId:
659660
case ExecutionModeLocalSizeHint:
660661
case ExecutionModeMaxWorkgroupSizeINTEL:
661662
WordLiterals.resize(3);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,12 @@ class SPIRVComponentExecutionModes {
807807
return nullptr;
808808
return Loc->second;
809809
}
810+
SPIRVExecutionModeId *getExecutionModeId(SPIRVExecutionModeKind EMK) const {
811+
auto Loc = ExecModes.find(EMK);
812+
if (Loc == ExecModes.end())
813+
return nullptr;
814+
return static_cast<SPIRVExecutionModeId *>(Loc->second);
815+
}
810816
SPIRVExecutionModeRange
811817
getExecutionModeRange(SPIRVExecutionModeKind EMK) const {
812818
return ExecModes.equal_range(EMK);

llvm-spirv/test/LocalSizeId.spvasm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; REQUIRES: spirv-as
2+
3+
; RUN: spirv-as %s --target-env spv1.2 -o %t.spv
4+
; RUN: spirv-val %t.spv
5+
; RUN: llvm-spirv -r -o %t.rev.bc %t.spv
6+
; RUN: llvm-dis %t.rev.bc -o - | FileCheck %s
7+
8+
OpCapability Addresses
9+
OpCapability Linkage
10+
OpCapability Kernel
11+
OpMemoryModel Physical64 OpenCL
12+
OpEntryPoint Kernel %fn "testLocalSizeId"
13+
OpExecutionModeId %fn LocalSizeId %uint_64 %uint_1 %uint_1sco
14+
%void = OpTypeVoid
15+
%uint = OpTypeInt 32 0
16+
%uint_1 = OpConstant %uint 1
17+
%uint_64 = OpConstant %uint 64
18+
%uint_1sco = OpSpecConstantOp %uint UDiv %uint_64 %uint_64
19+
%fnTy = OpTypeFunction %void
20+
21+
; CHECK: define spir_kernel void @testLocalSizeId() {{.*}} !reqd_work_group_size ![[MD:[0-9]+]]
22+
; CHECK: ![[MD]] = !{i32 64, i32 1, i32 1}
23+
24+
%fn = OpFunction %void None %fnTy
25+
%entry = OpLabel
26+
OpReturn
27+
OpFunctionEnd

0 commit comments

Comments
 (0)