[mlir][gpu] Introduce gpu.global_id op

Introduce OpenCL-style global_id op and corresponding spirv lowering.

Differential Revision: https://reviews.llvm.org/D121548
This commit is contained in:
Ivan Butygin 2022-03-13 13:56:59 +03:00
parent 7b0e041df8
commit 9f864a5447
4 changed files with 94 additions and 0 deletions

View File

@ -112,6 +112,21 @@ def GPU_SubgroupIdOp : GPU_Op<"subgroup_id", [NoSideEffect]>,
let assemblyFormat = "attr-dict `:` type($result)";
}
def GPU_GlobalIdOp : GPU_IndexOp<"global_id"> {
let description = [{
Returns the unique global workitem/thread id, i.e., the unique index of the
current workitem/thread within all workgroups / grid along the x, y, or z
`dimension`.
Example:
```mlir
%gidX = gpu.global_id x
```
}];
}
def GPU_NumSubgroupsOp : GPU_Op<"num_subgroups", [NoSideEffect]>,
Arguments<(ins)>, Results<(outs Index:$result)> {
let description = [{

View File

@ -373,6 +373,8 @@ void mlir::populateGPUToSPIRVPatterns(SPIRVTypeConverter &typeConverter,
LaunchConfigConversion<gpu::BlockDimOp, spirv::BuiltIn::WorkgroupSize>,
LaunchConfigConversion<gpu::ThreadIdOp,
spirv::BuiltIn::LocalInvocationId>,
LaunchConfigConversion<gpu::GlobalIdOp,
spirv::BuiltIn::GlobalInvocationId>,
SingleDimLaunchConfigConversion<gpu::SubgroupIdOp,
spirv::BuiltIn::SubgroupId>,
SingleDimLaunchConfigConversion<gpu::NumSubgroupsOp,

View File

@ -293,6 +293,79 @@ module attributes {gpu.container_module} {
}
}
// -----
module attributes {gpu.container_module} {
func @builtin() {
%c0 = arith.constant 1 : index
gpu.launch_func @kernels::@builtin_global_id_x
blocks in (%c0, %c0, %c0) threads in (%c0, %c0, %c0)
return
}
// CHECK-LABEL: spv.module @{{.*}} Logical GLSL450
// CHECK: spv.GlobalVariable [[GLOBALINVOCATIONID:@.*]] built_in("GlobalInvocationId")
gpu.module @kernels {
gpu.func @builtin_global_id_x() kernel
attributes {spv.entry_point_abi = {local_size = dense<[16, 1, 1]>: vector<3xi32>}} {
// CHECK: [[ADDRESS:%.*]] = spv.mlir.addressof [[GLOBALINVOCATIONID]]
// CHECK-NEXT: [[VEC:%.*]] = spv.Load "Input" [[ADDRESS]]
// CHECK-NEXT: {{%.*}} = spv.CompositeExtract [[VEC]]{{\[}}0 : i32{{\]}}
%0 = gpu.global_id x
gpu.return
}
}
}
// -----
module attributes {gpu.container_module} {
func @builtin() {
%c0 = arith.constant 1 : index
gpu.launch_func @kernels::@builtin_global_id_y
blocks in (%c0, %c0, %c0) threads in (%c0, %c0, %c0)
return
}
// CHECK-LABEL: spv.module @{{.*}} Logical GLSL450
// CHECK: spv.GlobalVariable [[GLOBALINVOCATIONID:@.*]] built_in("GlobalInvocationId")
gpu.module @kernels {
gpu.func @builtin_global_id_y() kernel
attributes {spv.entry_point_abi = {local_size = dense<[16, 1, 1]>: vector<3xi32>}} {
// CHECK: [[ADDRESS:%.*]] = spv.mlir.addressof [[GLOBALINVOCATIONID]]
// CHECK-NEXT: [[VEC:%.*]] = spv.Load "Input" [[ADDRESS]]
// CHECK-NEXT: {{%.*}} = spv.CompositeExtract [[VEC]]{{\[}}1 : i32{{\]}}
%0 = gpu.global_id y
gpu.return
}
}
}
// -----
module attributes {gpu.container_module} {
func @builtin() {
%c0 = arith.constant 1 : index
gpu.launch_func @kernels::@builtin_global_id_z
blocks in (%c0, %c0, %c0) threads in (%c0, %c0, %c0)
return
}
// CHECK-LABEL: spv.module @{{.*}} Logical GLSL450
// CHECK: spv.GlobalVariable [[GLOBALINVOCATIONID:@.*]] built_in("GlobalInvocationId")
gpu.module @kernels {
gpu.func @builtin_global_id_z() kernel
attributes {spv.entry_point_abi = {local_size = dense<[16, 1, 1]>: vector<3xi32>}} {
// CHECK: [[ADDRESS:%.*]] = spv.mlir.addressof [[GLOBALINVOCATIONID]]
// CHECK-NEXT: [[VEC:%.*]] = spv.Load "Input" [[ADDRESS]]
// CHECK-NEXT: {{%.*}} = spv.CompositeExtract [[VEC]]{{\[}}2 : i32{{\]}}
%0 = gpu.global_id z
gpu.return
}
}
}
// -----
module attributes {gpu.container_module} {

View File

@ -44,6 +44,10 @@ module attributes {gpu.container_module} {
%gDimY = gpu.grid_dim y
%gDimZ = gpu.grid_dim z
%gIdX = gpu.global_id x
%gIdY = gpu.global_id y
%gIdZ = gpu.global_id z
%sgId = gpu.subgroup_id : index
%numSg = gpu.num_subgroups : index
%SgSi = gpu.subgroup_size : index