Skip to content

Commit 60d45a9

Browse files
jzcsys-ce-bb
authored andcommitted
Add support for SPV_INTEL_subgroup_requirements (#2317)
Spec: #11301 More accurately, this PR adds support for the named subgroup related features of SPV_INTEL_subgroup_requirements to support implementation of sycl_ext_named_sub_group_sizes (also see #12335). The features related to subgroup lane mapping are not added yet. Original commit: KhronosGroup/SPIRV-LLVM-Translator@43acfef
1 parent c666620 commit 60d45a9

File tree

10 files changed

+98
-2
lines changed

10 files changed

+98
-2
lines changed

llvm-spirv/include/LLVMSPIRVExtensions.inc

+1
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ EXT(SPV_INTEL_fpga_argument_interfaces)
6969
EXT(SPV_INTEL_fpga_latency_control)
7070
EXT(SPV_INTEL_fp_max_error)
7171
EXT(SPV_INTEL_cache_controls)
72+
EXT(SPV_INTEL_subgroup_requirements)

llvm-spirv/lib/SPIRV/PreprocessMetadata.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,19 @@ void PreprocessMetadataBase::visit(Module *M) {
167167

168168
// !{void (i32 addrspace(1)*)* @kernel, i32 35, i32 size}
169169
if (MDNode *ReqdSubgroupSize = Kernel.getMetadata(kSPIR2MD::SubgroupSize)) {
170+
// A primary named subgroup size is encoded as
171+
// the metadata intel_reqd_sub_group_size with value 0.
172+
auto Val = getMDOperandAsInt(ReqdSubgroupSize, 0);
173+
if (Val == 0)
174+
EM.addOp()
175+
.add(&Kernel)
176+
.add(spv::internal::ExecutionModeNamedSubgroupSizeINTEL)
177+
.add(/* PrimarySubgroupSizeINTEL = */ 0U)
178+
.done();
170179
EM.addOp()
171180
.add(&Kernel)
172181
.add(spv::ExecutionModeSubgroupSize)
173-
.add(getMDOperandAsInt(ReqdSubgroupSize, 0))
182+
.add(Val)
174183
.done();
175184
}
176185

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -4215,6 +4215,18 @@ bool SPIRVToLLVM::transMetadata() {
42154215
ConstantAsMetadata::get(getUInt32(M, EM->getLiterals()[0]));
42164216
F->setMetadata(kSPIR2MD::SubgroupSize, MDNode::get(*Context, SizeMD));
42174217
}
4218+
// Generate metadata for intel_reqd_sub_group_size
4219+
if (BF->getExecutionMode(internal::ExecutionModeNamedSubgroupSizeINTEL)) {
4220+
// For now, there is only one named sub group size: primary, which is
4221+
// represented as a value of 0 as the argument of the OpExecutionMode.
4222+
assert(BF->getExecutionMode(internal::ExecutionModeNamedSubgroupSizeINTEL)
4223+
->getLiterals()[0] == 0 &&
4224+
"Invalid named sub group size");
4225+
// On the LLVM IR side, this is represented as the metadata
4226+
// intel_reqd_sub_group_size with value 0.
4227+
auto *SizeMD = ConstantAsMetadata::get(getUInt32(M, 0));
4228+
F->setMetadata(kSPIR2MD::SubgroupSize, MDNode::get(*Context, SizeMD));
4229+
}
42184230
// Generate metadata for max_work_group_size
42194231
if (auto *EM = BF->getExecutionMode(ExecutionModeMaxWorkgroupSizeINTEL)) {
42204232
F->setMetadata(kSPIR2MD::MaxWGSize,

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -5899,6 +5899,12 @@ bool LLVMToSPIRVBase::transExecutionMode() {
58995899
BF->addExecutionMode(BM->add(new SPIRVExecutionMode(
59005900
OpExecutionMode, BF, static_cast<ExecutionMode>(EMode))));
59015901
} break;
5902+
case spv::internal::ExecutionModeNamedSubgroupSizeINTEL: {
5903+
if (!BM->isAllowedToUseExtension(
5904+
ExtensionID::SPV_INTEL_subgroup_requirements))
5905+
break;
5906+
AddSingleArgExecutionMode(static_cast<ExecutionMode>(EMode));
5907+
} break;
59025908
default:
59035909
llvm_unreachable("invalid execution mode");
59045910
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ void SPIRVExecutionMode::decode(std::istream &I) {
660660
case ExecutionModeSchedulerTargetFmaxMhzINTEL:
661661
case ExecutionModeRegisterMapInterfaceINTEL:
662662
case ExecutionModeStreamingInterfaceINTEL:
663+
case spv::internal::ExecutionModeNamedSubgroupSizeINTEL:
663664
WordLiterals.resize(1);
664665
break;
665666
default:

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

+2
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,8 @@ class SPIRVCapability : public SPIRVEntryNoId<OpCapability> {
885885
return ExtensionID::SPV_INTEL_vector_compute;
886886
case internal::CapabilityFastCompositeINTEL:
887887
return ExtensionID::SPV_INTEL_fast_composite;
888+
case internal::CapabilitySubgroupRequirementsINTEL:
889+
return ExtensionID::SPV_INTEL_subgroup_requirements;
888890
default:
889891
return {};
890892
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ template <> inline void SPIRVMap<SPIRVExecutionModeKind, SPIRVCapVec>::init() {
291291
{CapabilityFPGAKernelAttributesINTEL});
292292
ADD_VEC_INIT(ExecutionModeNamedBarrierCountINTEL,
293293
{CapabilityVectorComputeINTEL});
294+
ADD_VEC_INIT(internal::ExecutionModeNamedSubgroupSizeINTEL,
295+
{internal::CapabilitySubgroupRequirementsINTEL});
294296
}
295297

296298
template <> inline void SPIRVMap<SPIRVMemoryModelKind, SPIRVCapVec>::init() {

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

+2
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,8 @@ template <> inline void SPIRVMap<Capability, std::string>::init() {
684684
"CooperativeMatrixInvocationInstructionsINTEL");
685685
add(internal::CapabilityCooperativeMatrixCheckedInstructionsINTEL,
686686
"CooperativeMatrixCheckedInstructionsINTEL");
687+
add(internal::CapabilitySubgroupRequirementsINTEL,
688+
"SubgroupRequirementsINTEL");
687689
}
688690
SPIRV_DEF_NAMEMAP(Capability, SPIRVCapabilityNameMap)
689691

llvm-spirv/lib/SPIRV/libSPIRV/spirv_internal.hpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ enum InternalCapability {
119119
ICapabilityJointMatrixBF16ComponentTypeINTEL = 6437,
120120
ICapabilityJointMatrixPackedInt2ComponentTypeINTEL = 6438,
121121
ICapabilityJointMatrixPackedInt4ComponentTypeINTEL = 6439,
122-
ICapabilityCacheControlsINTEL = 6441
122+
ICapabilityCacheControlsINTEL = 6441,
123+
ICapabilitySubgroupRequirementsINTEL = 6445
123124
};
124125

125126
enum InternalFunctionControlMask { IFunctionControlOptNoneINTELMask = 0x10000 };
126127

127128
enum InternalExecutionMode {
128129
IExecModeFastCompositeKernelINTEL = 6088,
130+
IExecModeNamedSubgroupSizeINTEL = 6446
129131
};
130132

131133
constexpr LinkageType LinkageTypeInternal =
@@ -213,6 +215,8 @@ _SPIRV_OP(Capability, TensorFloat32RoundingINTEL)
213215
_SPIRV_OP(Op, RoundFToTF32INTEL)
214216

215217
_SPIRV_OP(Capability, CacheControlsINTEL)
218+
219+
_SPIRV_OP(Capability, SubgroupRequirementsINTEL)
216220
#undef _SPIRV_OP
217221

218222
constexpr SourceLanguage SourceLanguagePython =
@@ -302,6 +306,9 @@ constexpr FunctionControlMask FunctionControlOptNoneINTELMask =
302306
constexpr ExecutionMode ExecutionModeFastCompositeKernelINTEL =
303307
static_cast<ExecutionMode>(IExecModeFastCompositeKernelINTEL);
304308

309+
constexpr ExecutionMode ExecutionModeNamedSubgroupSizeINTEL =
310+
static_cast<ExecutionMode>(IExecModeNamedSubgroupSizeINTEL);
311+
305312
} // namespace internal
306313
} // namespace spv
307314

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
; RUN: llvm-as < %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_subgroup_requirements -o %t.spv
3+
; RUN: llvm-spirv %t.spv -to-text -o %t.spt
4+
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
5+
6+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
7+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
8+
9+
; RUN: llvm-spirv %t.bc -o %t2.spv
10+
; RUN: llvm-spirv %t2.spv -to-text -o %t2.spt
11+
; RUN: FileCheck < %t2.spt %s --check-prefix=CHECK-SPIRV-2
12+
13+
; RUN: llvm-spirv -r %t2.spv -o %t2.rev.bc
14+
; RUN: llvm-dis < %t2.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
15+
16+
; CHECK-SPIRV: Capability SubgroupRequirementsINTEL
17+
; CHECK-SPIRV: Extension "SPV_INTEL_subgroup_requirements"
18+
; CHECK-SPIRV: EntryPoint 6 [[kernel:[0-9]+]] "_ZTSZ4mainE7Kernel1"
19+
; CHECK-SPIRV: ExecutionMode [[kernel]] 6446 0
20+
21+
; CHECK-LLVM: spir_kernel void @_ZTSZ4mainE7Kernel1() {{.*}} !intel_reqd_sub_group_size ![[MD:[0-9]+]]
22+
; CHECK-LLVM: ![[MD]] = !{i32 0}
23+
24+
; CHECK-SPIRV-2-NOT: Capability SubgroupRequirementsINTEL
25+
; CHECK-SPIRV-2-NOT: Extension "SPV_INTEL_subgroup_requirements"
26+
; CHECK-SPIRV-2: EntryPoint 6 [[kernel:[0-9]+]] "_ZTSZ4mainE7Kernel1"
27+
; CHECK-SPIRV-2: ExecutionMode [[kernel]] 35 0
28+
29+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
30+
target triple = "spir64-unknown-unknown"
31+
32+
$_ZTSZ4mainE7Kernel1 = comdat any
33+
34+
; Function Attrs: mustprogress norecurse nounwind
35+
define weak_odr dso_local spir_kernel void @_ZTSZ4mainE7Kernel1() local_unnamed_addr #0 comdat !srcloc !5 !kernel_arg_buffer_location !6 !sycl_fixed_targets !6 !sycl_kernel_omit_args !6 !intel_reqd_sub_group_size !7 {
36+
entry:
37+
ret void
38+
}
39+
40+
attributes #0 = { mustprogress norecurse nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "sycl-module-id"="ex.cpp" "sycl-optlevel"="2" "sycl-sub-group-size"="0" "uniform-work-group-size"="true" }
41+
42+
!llvm.module.flags = !{!0, !1}
43+
!opencl.spir.version = !{!2}
44+
!spirv.Source = !{!3}
45+
!llvm.ident = !{!4}
46+
47+
!0 = !{i32 1, !"wchar_size", i32 4}
48+
!1 = !{i32 7, !"frame-pointer", i32 2}
49+
!2 = !{i32 1, i32 2}
50+
!3 = !{i32 4, i32 100000}
51+
!4 = !{!"clang version 18.0.0git (/ws/llvm/clang 8fd29b3c2aa9f9ce163be557b51de39c95aaf230)"}
52+
!5 = !{i32 358}
53+
!6 = !{}
54+
!7 = !{i32 0}

0 commit comments

Comments
 (0)