Skip to content

Commit 755acb1

Browse files
[SPIR-V] Add SPV_INTEL_2d_block_io extension (llvm#140140)
Adds additional subgroup block prefetch, load, load transposed, load transformed and store instructions to read two-dimensional blocks of data from a two-dimensional region of memory, or to write two-dimensional blocks of data to a two-dimensional region of memory. Spec: https://github.com/KhronosGroup/SPIRV-Registry/blob/main/extensions/INTEL/SPV_INTEL_2d_block_io.asciidoc --------- Co-authored-by: Dmitry Sidorov <[email protected]>
1 parent 383e5f3 commit 755acb1

File tree

8 files changed

+137
-1
lines changed

8 files changed

+137
-1
lines changed

llvm/docs/SPIRVUsage.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ list of supported SPIR-V extensions, sorted alphabetically by their extension na
153153
- Adds atomic add instruction on floating-point numbers.
154154
* - ``SPV_EXT_shader_atomic_float_min_max``
155155
- Adds atomic min and max instruction on floating-point numbers.
156+
* - ``SPV_INTEL_2d_block_io``
157+
- Adds additional subgroup block prefetch, load, load transposed, load transformed and store instructions to read two-dimensional blocks of data from a two-dimensional region of memory, or to write two-dimensional blocks of data to a two dimensional region of memory.
156158
* - ``SPV_INTEL_arbitrary_precision_integers``
157159
- Allows generating arbitrary width integer types.
158160
* - ``SPV_INTEL_bindless_images``

llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,24 @@ static bool buildTernaryBitwiseFunctionINTELInst(
10761076
return true;
10771077
}
10781078

1079+
/// Helper function for building Intel's 2d block io instructions.
1080+
static bool build2DBlockIOINTELInst(const SPIRV::IncomingCall *Call,
1081+
unsigned Opcode,
1082+
MachineIRBuilder &MIRBuilder,
1083+
SPIRVGlobalRegistry *GR) {
1084+
// Generate SPIRV instruction accordingly.
1085+
if (Call->isSpirvOp())
1086+
return buildOpFromWrapper(MIRBuilder, Opcode, Call, Register(0));
1087+
1088+
auto MIB = MIRBuilder.buildInstr(Opcode)
1089+
.addDef(Call->ReturnRegister)
1090+
.addUse(GR->getSPIRVTypeID(Call->ReturnType));
1091+
for (unsigned i = 0; i < Call->Arguments.size(); ++i)
1092+
MIB.addUse(Call->Arguments[i]);
1093+
1094+
return true;
1095+
}
1096+
10791097
static unsigned getNumComponentsForDim(SPIRV::Dim::Dim dim) {
10801098
switch (dim) {
10811099
case SPIRV::Dim::DIM_1D:
@@ -2319,6 +2337,17 @@ generateTernaryBitwiseFunctionINTELInst(const SPIRV::IncomingCall *Call,
23192337
return buildTernaryBitwiseFunctionINTELInst(Call, Opcode, MIRBuilder, GR);
23202338
}
23212339

2340+
static bool generate2DBlockIOINTELInst(const SPIRV::IncomingCall *Call,
2341+
MachineIRBuilder &MIRBuilder,
2342+
SPIRVGlobalRegistry *GR) {
2343+
// Lookup the instruction opcode in the TableGen records.
2344+
const SPIRV::DemangledBuiltin *Builtin = Call->Builtin;
2345+
unsigned Opcode =
2346+
SPIRV::lookupNativeBuiltin(Builtin->Name, Builtin->Set)->Opcode;
2347+
2348+
return build2DBlockIOINTELInst(Call, Opcode, MIRBuilder, GR);
2349+
}
2350+
23222351
static bool buildNDRange(const SPIRV::IncomingCall *Call,
23232352
MachineIRBuilder &MIRBuilder,
23242353
SPIRVGlobalRegistry *GR) {
@@ -2902,6 +2931,8 @@ std::optional<bool> lowerBuiltin(const StringRef DemangledCall,
29022931
return generateBindlessImageINTELInst(Call.get(), MIRBuilder, GR);
29032932
case SPIRV::TernaryBitwiseINTEL:
29042933
return generateTernaryBitwiseFunctionINTELInst(Call.get(), MIRBuilder, GR);
2934+
case SPIRV::Block2DLoadStore:
2935+
return generate2DBlockIOINTELInst(Call.get(), MIRBuilder, GR);
29052936
}
29062937
return false;
29072938
}

llvm/lib/Target/SPIRV/SPIRVBuiltins.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def ICarryBorrow : BuiltinGroup;
6868
def ExtendedBitOps : BuiltinGroup;
6969
def BindlessINTEL : BuiltinGroup;
7070
def TernaryBitwiseINTEL : BuiltinGroup;
71+
def Block2DLoadStore : BuiltinGroup;
7172

7273
//===----------------------------------------------------------------------===//
7374
// Class defining a demangled builtin record. The information in the record
@@ -718,6 +719,13 @@ defm : DemangledNativeBuiltin<"__spirv_ConvertHandleToSampledImageINTEL", OpenCL
718719
// SPV_INTEL_ternary_bitwise_function builtin records:
719720
defm : DemangledNativeBuiltin<"__spirv_BitwiseFunctionINTEL", OpenCL_std, TernaryBitwiseINTEL, 4, 4, OpBitwiseFunctionINTEL>;
720721

722+
// SPV_INTEL_2d_block_io builtin records
723+
defm : DemangledNativeBuiltin<"__spirv_Subgroup2DBlockLoadINTEL", OpenCL_std, Block2DLoadStore, 10, 10, OpSubgroup2DBlockLoadINTEL>;
724+
defm : DemangledNativeBuiltin<"__spirv_Subgroup2DBlockLoadTransposeINTEL", OpenCL_std, Block2DLoadStore, 10, 10, OpSubgroup2DBlockLoadTransposeINTEL>;
725+
defm : DemangledNativeBuiltin<"__spirv_Subgroup2DBlockLoadTransformINTEL", OpenCL_std, Block2DLoadStore, 10, 10, OpSubgroup2DBlockLoadTransformINTEL>;
726+
defm : DemangledNativeBuiltin<"__spirv_Subgroup2DBlockPrefetchINTEL", OpenCL_std, Block2DLoadStore, 9, 9, OpSubgroup2DBlockPrefetchINTEL>;
727+
defm : DemangledNativeBuiltin<"__spirv_Subgroup2DBlockStoreINTEL", OpenCL_std, Block2DLoadStore, 10, 10, OpSubgroup2DBlockStoreINTEL>;
728+
721729
//===----------------------------------------------------------------------===//
722730
// Class defining a work/sub group builtin that should be translated into a
723731
// SPIR-V instruction using the defined properties.

llvm/lib/Target/SPIRV/SPIRVCommandLine.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ static const std::map<std::string, SPIRV::Extension::Extension, std::less<>>
9797
SPIRV::Extension::Extension::
9898
SPV_INTEL_subgroup_matrix_multiply_accumulate},
9999
{"SPV_INTEL_ternary_bitwise_function",
100-
SPIRV::Extension::Extension::SPV_INTEL_ternary_bitwise_function}};
100+
SPIRV::Extension::Extension::SPV_INTEL_ternary_bitwise_function},
101+
{"SPV_INTEL_2d_block_io",
102+
SPIRV::Extension::Extension::SPV_INTEL_2d_block_io}};
101103

102104
bool SPIRVExtensionsParser::parse(cl::Option &O, StringRef ArgName,
103105
StringRef ArgValue,

llvm/lib/Target/SPIRV/SPIRVInstrInfo.td

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,3 +936,20 @@ def OpAliasScopeListDeclINTEL: Op<5913, (outs ID:$res), (ins variable_ops),
936936
// SPV_INTEL_ternary_bitwise_function
937937
def OpBitwiseFunctionINTEL: Op<6242, (outs ID:$res), (ins TYPE:$type, ID:$a, ID:$b, ID:$c, ID:$lut_index),
938938
"$res = OpBitwiseFunctionINTEL $type $a $b $c $lut_index">;
939+
940+
// SPV_INTEL_2d_block_io
941+
def OpSubgroup2DBlockLoadINTEL: Op<6231, (outs), (ins ID:$element_size, ID:$block_width, ID:$block_height,
942+
ID:$block_count, ID:$src_base_ptr, ID:$memory_width, ID:$memory_height, ID:$memory_pitch, ID:$coord, ID:$dst_ptr),
943+
"OpSubgroup2DBlockLoadINTEL $element_size $block_width $block_height $block_count $src_base_ptr $memory_width $memory_height $memory_pitch $coord $dst_ptr">;
944+
def OpSubgroup2DBlockLoadTransposeINTEL: Op<6233, (outs), (ins ID:$element_size, ID:$block_width, ID:$block_height,
945+
ID:$block_count, ID:$src_base_ptr, ID:$memory_width, ID:$memory_height, ID:$memory_pitch, ID:$coord, ID:$dst_ptr),
946+
"OpSubgroup2DBlockLoadTransposeINTEL $element_size $block_width $block_height $block_count $src_base_ptr $memory_width $memory_height $memory_pitch $coord $dst_ptr">;
947+
def OpSubgroup2DBlockLoadTransformINTEL: Op<6232, (outs), (ins ID:$element_size, ID:$block_width, ID:$block_height,
948+
ID:$block_count, ID:$src_base_ptr, ID:$memory_width, ID:$memory_height, ID:$memory_pitch, ID:$coord, ID:$dst_ptr),
949+
"OpSubgroup2DBlockLoadTransformINTEL $element_size $block_width $block_height $block_count $src_base_ptr $memory_width $memory_height $memory_pitch $coord $dst_ptr">;
950+
def OpSubgroup2DBlockPrefetchINTEL: Op<6234, (outs), (ins ID:$element_size, ID:$block_width, ID:$block_height,
951+
ID:$block_count, ID:$src_base_ptr, ID:$memory_width, ID:$memory_height, ID:$memory_pitch, ID:$coord),
952+
"OpSubgroup2DBlockPrefetchINTEL $element_size $block_width $block_height $block_count $src_base_ptr $memory_width $memory_height $memory_pitch $coord">;
953+
def OpSubgroup2DBlockStoreINTEL: Op<6235, (outs), (ins ID:$element_size, ID:$block_width, ID:$block_height,
954+
ID:$block_count, ID:$src_ptr, ID:$dst_base_ptr, ID:$memory_width, ID:$memory_height, ID:$memory_pitch, ID:$coord),
955+
"OpSubgroup2DBlockStoreINTEL $element_size $block_width $block_height $block_count $src_ptr $dst_base_ptr $memory_width $memory_height $memory_pitch $coord">;

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,30 @@ void addInstrRequirements(const MachineInstr &MI,
17391739
Reqs.addExtension(SPIRV::Extension::SPV_INTEL_bindless_images);
17401740
Reqs.addCapability(SPIRV::Capability::BindlessImagesINTEL);
17411741
break;
1742+
case SPIRV::OpSubgroup2DBlockLoadINTEL:
1743+
case SPIRV::OpSubgroup2DBlockLoadTransposeINTEL:
1744+
case SPIRV::OpSubgroup2DBlockLoadTransformINTEL:
1745+
case SPIRV::OpSubgroup2DBlockPrefetchINTEL:
1746+
case SPIRV::OpSubgroup2DBlockStoreINTEL: {
1747+
if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_2d_block_io))
1748+
report_fatal_error("OpSubgroup2DBlock[Load/LoadTranspose/LoadTransform/"
1749+
"Prefetch/Store]INTEL instructions require the "
1750+
"following SPIR-V extension: SPV_INTEL_2d_block_io",
1751+
false);
1752+
Reqs.addExtension(SPIRV::Extension::SPV_INTEL_2d_block_io);
1753+
Reqs.addCapability(SPIRV::Capability::Subgroup2DBlockIOINTEL);
1754+
1755+
const auto OpCode = MI.getOpcode();
1756+
if (OpCode == SPIRV::OpSubgroup2DBlockLoadTransposeINTEL) {
1757+
Reqs.addCapability(SPIRV::Capability::Subgroup2DBlockTransposeINTEL);
1758+
break;
1759+
}
1760+
if (OpCode == SPIRV::OpSubgroup2DBlockLoadTransformINTEL) {
1761+
Reqs.addCapability(SPIRV::Capability::Subgroup2DBlockTransformINTEL);
1762+
break;
1763+
}
1764+
break;
1765+
}
17421766
case SPIRV::OpKill: {
17431767
Reqs.addCapability(SPIRV::Capability::Shader);
17441768
} break;

llvm/lib/Target/SPIRV/SPIRVSymbolicOperands.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ defm SPV_INTEL_memory_access_aliasing : ExtensionOperand<118>;
315315
defm SPV_INTEL_fp_max_error : ExtensionOperand<119>;
316316
defm SPV_INTEL_ternary_bitwise_function : ExtensionOperand<120>;
317317
defm SPV_INTEL_subgroup_matrix_multiply_accumulate : ExtensionOperand<121>;
318+
defm SPV_INTEL_2d_block_io : ExtensionOperand<122>;
318319

319320
//===----------------------------------------------------------------------===//
320321
// Multiclass used to define Capabilities enum values and at the same time
@@ -517,6 +518,9 @@ defm MemoryAccessAliasingINTEL : CapabilityOperand<5910, 0, 0, [SPV_INTEL_memory
517518
defm FPMaxErrorINTEL : CapabilityOperand<6169, 0, 0, [SPV_INTEL_fp_max_error], []>;
518519
defm TernaryBitwiseFunctionINTEL : CapabilityOperand<6241, 0, 0, [SPV_INTEL_ternary_bitwise_function], []>;
519520
defm SubgroupMatrixMultiplyAccumulateINTEL : CapabilityOperand<6236, 0, 0, [SPV_INTEL_subgroup_matrix_multiply_accumulate], []>;
521+
defm Subgroup2DBlockIOINTEL : CapabilityOperand<6228, 0, 0, [SPV_INTEL_2d_block_io], []>;
522+
defm Subgroup2DBlockTransformINTEL : CapabilityOperand<6229, 0, 0, [SPV_INTEL_2d_block_io], [Subgroup2DBlockIOINTEL]>;
523+
defm Subgroup2DBlockTransposeINTEL : CapabilityOperand<6230, 0, 0, [SPV_INTEL_2d_block_io], [Subgroup2DBlockIOINTEL]>;
520524

521525
//===----------------------------------------------------------------------===//
522526
// Multiclass used to define SourceLanguage enum values and at the same time
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; RUN: not llc -O0 -mtriple=spirv64-unknown-unknown %s -o %t.spvt 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
2+
; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_INTEL_2d_block_io %s -o - | FileCheck %s
3+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s --spirv-ext=+SPV_INTEL_2d_block_io -o - -filetype=obj | spirv-val %}
4+
5+
; CHECK-ERROR: LLVM ERROR: OpSubgroup2DBlock[Load/LoadTranspose/LoadTransform/Prefetch/Store]INTEL
6+
; CHECK-ERROR-SAME: instructions require the following SPIR-V extension: SPV_INTEL_2d_block_io
7+
8+
; CHECK: OpCapability Subgroup2DBlockIOINTEL
9+
; CHECK: OpCapability Subgroup2DBlockTransformINTEL
10+
; CHECK: OpCapability Subgroup2DBlockTransposeINTEL
11+
; CHECK: OpExtension "SPV_INTEL_2d_block_io"
12+
13+
; CHECK-DAG: %[[Int8Ty:[0-9]+]] = OpTypeInt 8 0
14+
; CHECK-DAG: %[[Int32Ty:[0-9]+]] = OpTypeInt 32 0
15+
; CHECK-DAG: %[[Const42:[0-9]+]] = OpConstant %[[Int32Ty]] 42
16+
; CHECK-DAG: %[[VoidTy:[0-9]+]] = OpTypeVoid
17+
; CHECK-DAG: %[[GlbPtrTy:[0-9]+]] = OpTypePointer CrossWorkgroup %[[Int8Ty]]
18+
; CHECK-DAG: %[[VectorTy:[0-9]+]] = OpTypeVector %[[Int32Ty]] 2
19+
; CHECK-DAG: %[[PrvPtrTy:[0-9]+]] = OpTypePointer Function %[[Int8Ty]]
20+
; CHECK: %[[BaseSrc:[0-9]+]] = OpFunctionParameter %[[GlbPtrTy]]
21+
; CHECK: %[[BaseDst:[0-9]+]] = OpFunctionParameter %[[GlbPtrTy]]
22+
; CHECK: %[[Width:[0-9]+]] = OpFunctionParameter %[[Int32Ty]]
23+
; CHECK: %[[Height:[0-9]+]] = OpFunctionParameter %[[Int32Ty]]
24+
; CHECK: %[[Pitch:[0-9]+]] = OpFunctionParameter %[[Int32Ty]]
25+
; CHECK: %[[Coord:[0-9]+]] = OpFunctionParameter %[[VectorTy]]
26+
; CHECK: %[[Dst:[0-9]+]] = OpFunctionParameter %[[PrvPtrTy]]
27+
; CHECK: %[[Src:[0-9]+]] = OpFunctionParameter %[[PrvPtrTy]]
28+
; CHECK: OpSubgroup2DBlockLoadINTEL %[[Const42]] %[[Const42]] %[[Const42]] %[[Const42]] %[[BaseSrc]] %[[Width]] %[[Height]] %[[Pitch]] %[[Coord]] %[[Dst]]
29+
; CHECK: OpSubgroup2DBlockLoadTransformINTEL %[[Const42]] %[[Const42]] %[[Const42]] %[[Const42]] %[[BaseSrc]] %[[Width]] %[[Height]] %[[Pitch]] %[[Coord]] %[[Dst]]
30+
; CHECK: OpSubgroup2DBlockLoadTransposeINTEL %[[Const42]] %[[Const42]] %[[Const42]] %[[Const42]] %[[BaseSrc]] %[[Width]] %[[Height]] %[[Pitch]] %[[Coord]] %[[Dst]]
31+
; CHECK: OpSubgroup2DBlockPrefetchINTEL %[[Const42]] %[[Const42]] %[[Const42]] %[[Const42]] %[[BaseSrc]] %[[Width]] %[[Height]] %[[Pitch]] %[[Coord]]
32+
; CHECK: OpSubgroup2DBlockStoreINTEL %[[Const42]] %[[Const42]] %[[Const42]] %[[Const42]] %[[Src]] %[[BaseDst]] %[[Width]] %[[Height]] %[[Pitch]] %[[Coord]]
33+
34+
define spir_func void @foo(ptr addrspace(1) %base_address, ptr addrspace(1) %dst_base_pointer, i32 %width, i32 %height, i32 %pitch, <2 x i32> %coord, ptr %dst_pointer, ptr %src_pointer) {
35+
entry:
36+
call spir_func void @_Z32__spirv_Subgroup2DBlockLoadINTELiiiiPU3AS1KviiiDv2_iPv(i32 42, i32 42, i32 42, i32 42, ptr addrspace(1) %base_address, i32 %width, i32 %height, i32 %pitch, <2 x i32> %coord, ptr %dst_pointer)
37+
call spir_func void @_Z41__spirv_Subgroup2DBlockLoadTransformINTELiiiiPU3AS1KviiiDv2_iPv(i32 42, i32 42, i32 42, i32 42, ptr addrspace(1) %base_address, i32 %width, i32 %height, i32 %pitch, <2 x i32> %coord, ptr %dst_pointer)
38+
call spir_func void @_Z41__spirv_Subgroup2DBlockLoadTransposeINTELiiiiPU3AS1KviiiDv2_iPv(i32 42, i32 42, i32 42, i32 42, ptr addrspace(1) %base_address, i32 %width, i32 %height, i32 %pitch, <2 x i32> %coord, ptr %dst_pointer)
39+
call spir_func void @_Z36__spirv_Subgroup2DBlockPrefetchINTELiiiiPU3AS1KviiiDv2_i(i32 42, i32 42, i32 42, i32 42, ptr addrspace(1) %base_address, i32 %width, i32 %height, i32 %pitch, <2 x i32> %coord)
40+
call spir_func void @_Z33__spirv_Subgroup2DBlockStoreINTELiiiiPKvPU3AS1viiiDv2_i(i32 42, i32 42, i32 42, i32 42, ptr %src_pointer, ptr addrspace(1) %dst_base_pointer, i32 %width, i32 %height, i32 %pitch, <2 x i32> %coord)
41+
ret void
42+
}
43+
44+
declare spir_func void @_Z32__spirv_Subgroup2DBlockLoadINTELiiiiPU3AS1KviiiDv2_iPv(i32, i32, i32, i32, ptr addrspace(1), i32, i32, i32, <2 x i32>, ptr)
45+
declare spir_func void @_Z41__spirv_Subgroup2DBlockLoadTransformINTELiiiiPU3AS1KviiiDv2_iPv(i32, i32, i32, i32, ptr addrspace(1), i32, i32, i32, <2 x i32>, ptr)
46+
declare spir_func void @_Z41__spirv_Subgroup2DBlockLoadTransposeINTELiiiiPU3AS1KviiiDv2_iPv(i32, i32, i32, i32, ptr addrspace(1), i32, i32, i32, <2 x i32>, ptr)
47+
declare spir_func void @_Z36__spirv_Subgroup2DBlockPrefetchINTELiiiiPU3AS1KviiiDv2_i(i32, i32, i32, i32, ptr addrspace(1), i32, i32, i32, <2 x i32>)
48+
declare spir_func void @_Z33__spirv_Subgroup2DBlockStoreINTELiiiiPKvPU3AS1viiiDv2_i(i32, i32, i32, i32, ptr, ptr addrspace(1), i32, i32, i32, <2 x i32>)

0 commit comments

Comments
 (0)