Skip to content

Commit 2237a18

Browse files
authored
[SPIRV] Enable OpenCL max_work_group_size translation via SPV_INTEL_kernel_attributes (#165891)
This adds BE support for the [`SPV_INTEL_kernel_attributes`](https://github.khronos.org/SPIRV-Registry/extensions/INTEL/SPV_INTEL_kernel_attributes.html) extension. The extension is necessary to encode the rather useful `max_work_group_size` kernel attribute, via `OpExecutionMode MaxWorkgroupSizeINTEL`, which is the only Execution Mode added by the extension that this patch adds full processing for. Future patches will add the other Execution Modes and Capabilities. The test is adapted from the equivalent Translator test; it depends on #165815.
1 parent a01e4da commit 2237a18

File tree

6 files changed

+63
-4
lines changed

6 files changed

+63
-4
lines changed

llvm/docs/SPIRVUsage.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ Below is a list of supported SPIR-V extensions, sorted alphabetically by their e
187187
- Adds decorations that can be applied to global (module scope) variables.
188188
* - ``SPV_INTEL_global_variable_fpga_decorations``
189189
- Adds decorations that can be applied to global (module scope) variables to help code generation for FPGA devices.
190+
* - ``SPV_INTEL_kernel_attributes``
191+
- Adds execution modes that can be applied to entry points to inform scheduling.
190192
* - ``SPV_INTEL_media_block_io``
191193
- Adds additional subgroup block read and write functionality that allow applications to flexibly specify the width and height of the block to read from or write to a 2D image.
192194
* - ``SPV_INTEL_memory_access_aliasing``
@@ -226,9 +228,9 @@ Below is a list of supported SPIR-V extensions, sorted alphabetically by their e
226228
* - ``SPV_INTEL_fp_max_error``
227229
- Adds the ability to specify the maximum error for floating-point operations.
228230
* - ``SPV_INTEL_ternary_bitwise_function``
229-
- Adds a bitwise instruction on three operands and a look-up table index for specifying the bitwise operation to perform.
231+
- Adds a bitwise instruction on three operands and a look-up table index for specifying the bitwise operation to perform.
230232
* - ``SPV_INTEL_subgroup_matrix_multiply_accumulate``
231-
- Adds an instruction to compute the matrix product of an M x K matrix with a K x N matrix and then add an M x N matrix.
233+
- Adds an instruction to compute the matrix product of an M x K matrix with a K x N matrix and then add an M x N matrix.
232234
* - ``SPV_INTEL_int4``
233235
- Adds support for 4-bit integer type, and allow this type to be used in cooperative matrices.
234236
* - ``SPV_KHR_float_controls2``

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,11 @@ void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
577577
if (MDNode *Node = F.getMetadata("intel_reqd_sub_group_size"))
578578
outputExecutionModeFromMDNode(FReg, Node,
579579
SPIRV::ExecutionMode::SubgroupSize, 0, 0);
580+
if (MDNode *Node = F.getMetadata("max_work_group_size")) {
581+
if (ST->canUseExtension(SPIRV::Extension::SPV_INTEL_kernel_attributes))
582+
outputExecutionModeFromMDNode(
583+
FReg, Node, SPIRV::ExecutionMode::MaxWorkgroupSizeINTEL, 3, 1);
584+
}
580585
if (MDNode *Node = F.getMetadata("vec_type_hint")) {
581586
MCInst Inst;
582587
Inst.setOpcode(SPIRV::OpExecutionMode);

llvm/lib/Target/SPIRV/SPIRVCommandLine.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ static const std::map<std::string, SPIRV::Extension::Extension, std::less<>>
155155
{"SPV_INTEL_predicated_io",
156156
SPIRV::Extension::Extension::SPV_INTEL_predicated_io},
157157
{"SPV_KHR_maximal_reconvergence",
158-
SPIRV::Extension::Extension::SPV_KHR_maximal_reconvergence}};
158+
SPIRV::Extension::Extension::SPV_KHR_maximal_reconvergence},
159+
{"SPV_INTEL_kernel_attributes",
160+
SPIRV::Extension::Extension::SPV_INTEL_kernel_attributes}};
159161

160162
bool SPIRVExtensionsParser::parse(cl::Option &O, StringRef ArgName,
161163
StringRef ArgValue,

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,6 +2180,10 @@ static void collectReqs(const Module &M, SPIRV::ModuleAnalysisInfo &MAI,
21802180
MAI.Reqs.getAndAddRequirements(
21812181
SPIRV::OperandCategory::ExecutionModeOperand,
21822182
SPIRV::ExecutionMode::SubgroupSize, ST);
2183+
if (F.getMetadata("max_work_group_size"))
2184+
MAI.Reqs.getAndAddRequirements(
2185+
SPIRV::OperandCategory::ExecutionModeOperand,
2186+
SPIRV::ExecutionMode::MaxWorkgroupSizeINTEL, ST);
21832187
if (F.getMetadata("vec_type_hint"))
21842188
MAI.Reqs.getAndAddRequirements(
21852189
SPIRV::OperandCategory::ExecutionModeOperand,

llvm/lib/Target/SPIRV/SPIRVSymbolicOperands.td

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,11 @@ defm CooperativeMatrixBFloat16ComponentTypeINTEL : CapabilityOperand<6437, 0, 0,
587587
defm RoundToInfinityINTEL : CapabilityOperand<5582, 0, 0, [SPV_INTEL_float_controls2], []>;
588588
defm FloatingPointModeINTEL : CapabilityOperand<5583, 0, 0, [SPV_INTEL_float_controls2], []>;
589589
defm FunctionFloatControlINTEL : CapabilityOperand<5821, 0, 0, [SPV_INTEL_float_controls2], []>;
590+
defm KernelAttributesINTEL : CapabilityOperand<5892, 0, 0, [SPV_INTEL_kernel_attributes], [Kernel]>;
591+
// TODO-SPIRV: add these once they are used / tested.
592+
// defm FPGAKernelAttributesINTEL : CapabilityOperand<5897, 0, 0, [SPV_INTEL_kernel_attributes], [Kernel]>;
593+
// defm FPGAKernelAttributesv2INTEL : CapabilityOperand<6161, 0, 0, [SPV_INTEL_kernel_attributes], [Kernel]>;
594+
// END TODO-SPIRV
590595
defm LongCompositesINTEL : CapabilityOperand<6089, 0, 0, [SPV_INTEL_long_composites], []>;
591596
defm BindlessImagesINTEL : CapabilityOperand<6528, 0, 0, [SPV_INTEL_bindless_images], []>;
592597
defm MemoryAccessAliasingINTEL : CapabilityOperand<5910, 0, 0, [SPV_INTEL_memory_access_aliasing], []>;
@@ -805,6 +810,15 @@ defm RoundingModeRTPINTEL : ExecutionModeOperand<5620, [RoundToInfinityINTEL]>;
805810
defm RoundingModeRTNINTEL : ExecutionModeOperand<5621, [RoundToInfinityINTEL]>;
806811
defm FloatingPointModeALTINTEL : ExecutionModeOperand<5622, [FloatingPointModeINTEL]>;
807812
defm FloatingPointModeIEEEINTEL : ExecutionModeOperand<5623, [FloatingPointModeINTEL]>;
813+
defm MaxWorkgroupSizeINTEL : ExecutionModeOperand<5893, [KernelAttributesINTEL]>;
814+
// TODO-SPIRV: Add the following once they are used / tested.
815+
// defm MaxWorkDimINTEL : ExecutionModeOperand<5894, [KernelAttributesINTEL]>;
816+
// defm NoGlobalOffsetINTEL : ExecutionModeOperand<5895, [KernelAttributesINTEL]>;
817+
// defm NumSIMDWorkitemsINTEL : ExecutionModeOperand<5896, [FPGAKernelAttributesINTEL]>;
818+
// defm SchedulerTargetFmaxMhzINTEL : ExecutionModeOperand<5903, [FPGAKernelAttributesINTEL]>;
819+
// defm StreamingInterfaceINTEL : ExecutionModeOperand<6154, [FPGAKernelAttributesv2INTEL]>;
820+
// defm RegisterMapInterfaceINTEL : ExecutionModeOperand<6160, [FPGAKernelAttributesv2INTEL]>;
821+
// END TODO-SPIRV
808822
defm FPFastMathDefault : ExecutionModeOperand<6028, [FloatControls2]>;
809823
defm MaximallyReconvergesKHR : ExecutionModeOperand<6023, [Shader]>;
810824

@@ -1919,7 +1933,7 @@ defm GenericCastToPtr : SpecConstantOpOperandsOperand<122, [], [Kernel]>;
19191933
defm PtrCastToGeneric : SpecConstantOpOperandsOperand<121, [], [Kernel]>;
19201934
defm Bitcast : SpecConstantOpOperandsOperand<124, [], []>;
19211935
defm QuantizeToF16 : SpecConstantOpOperandsOperand<116, [], [Shader]>;
1922-
// Arithmetic
1936+
// Arithmetic
19231937
defm SNegate : SpecConstantOpOperandsOperand<126, [], []>;
19241938
defm Not : SpecConstantOpOperandsOperand<200, [], []>;
19251939
defm IAdd : SpecConstantOpOperandsOperand<128, [], []>;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_INTEL_kernel_attributes %s -o - | FileCheck %s
2+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-amd-amdhsa %s -o - | FileCheck %s
3+
; %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_INTEL_kernel_attributes %s -o - -filetype=obj | spirv-val %}
4+
; %if spirv-tools %{ llc -O0 -mtriple=spirv64-amd-amdhsa %s -o - -filetype=obj | spirv-val %}
5+
6+
; CHECK: OpCapability KernelAttributesINTEL
7+
; CHECK: OpExtension "SPV_INTEL_kernel_attributes"
8+
; CHECK: OpEntryPoint {{.*}} %[[DIM1:[0-9]+]] "Dim1"
9+
; CHECK: OpEntryPoint {{.*}} %[[DIM2:[0-9]+]] "Dim2"
10+
; CHECK: OpEntryPoint {{.*}} %[[DIM3:[0-9]+]] "Dim3"
11+
; CHECK: OpExecutionMode %[[DIM1]] MaxWorkgroupSizeINTEL 4 1 1
12+
; CHECK: OpExecutionMode %[[DIM2]] MaxWorkgroupSizeINTEL 8 4 1
13+
; CHECK: OpExecutionMode %[[DIM3]] MaxWorkgroupSizeINTEL 16 8 4
14+
; CHECK: %[[DIM1]] = OpFunction
15+
; CHECK: %[[DIM2]] = OpFunction
16+
; CHECK: %[[DIM3]] = OpFunction
17+
18+
define spir_kernel void @Dim1() !max_work_group_size !0 {
19+
ret void
20+
}
21+
22+
define spir_kernel void @Dim2() !max_work_group_size !1 {
23+
ret void
24+
}
25+
26+
define spir_kernel void @Dim3() !max_work_group_size !2 {
27+
ret void
28+
}
29+
30+
!0 = !{i32 4}
31+
!1 = !{i32 8, i32 4}
32+
!2 = !{i32 16, i32 8, i32 4}

0 commit comments

Comments
 (0)