Skip to content

Commit 9f864a5

Browse files
committed
[mlir][gpu] Introduce gpu.global_id op
Introduce OpenCL-style global_id op and corresponding spirv lowering. Differential Revision: https://reviews.llvm.org/D121548
1 parent 7b0e041 commit 9f864a5

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

mlir/include/mlir/Dialect/GPU/GPUOps.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ def GPU_SubgroupIdOp : GPU_Op<"subgroup_id", [NoSideEffect]>,
112112
let assemblyFormat = "attr-dict `:` type($result)";
113113
}
114114

115+
def GPU_GlobalIdOp : GPU_IndexOp<"global_id"> {
116+
let description = [{
117+
Returns the unique global workitem/thread id, i.e., the unique index of the
118+
current workitem/thread within all workgroups / grid along the x, y, or z
119+
`dimension`.
120+
121+
Example:
122+
123+
```mlir
124+
%gidX = gpu.global_id x
125+
```
126+
}];
127+
}
128+
129+
115130
def GPU_NumSubgroupsOp : GPU_Op<"num_subgroups", [NoSideEffect]>,
116131
Arguments<(ins)>, Results<(outs Index:$result)> {
117132
let description = [{

mlir/lib/Conversion/GPUToSPIRV/GPUToSPIRV.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ void mlir::populateGPUToSPIRVPatterns(SPIRVTypeConverter &typeConverter,
373373
LaunchConfigConversion<gpu::BlockDimOp, spirv::BuiltIn::WorkgroupSize>,
374374
LaunchConfigConversion<gpu::ThreadIdOp,
375375
spirv::BuiltIn::LocalInvocationId>,
376+
LaunchConfigConversion<gpu::GlobalIdOp,
377+
spirv::BuiltIn::GlobalInvocationId>,
376378
SingleDimLaunchConfigConversion<gpu::SubgroupIdOp,
377379
spirv::BuiltIn::SubgroupId>,
378380
SingleDimLaunchConfigConversion<gpu::NumSubgroupsOp,

mlir/test/Conversion/GPUToSPIRV/builtins.mlir

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,79 @@ module attributes {gpu.container_module} {
293293
}
294294
}
295295

296+
// -----
297+
298+
module attributes {gpu.container_module} {
299+
func @builtin() {
300+
%c0 = arith.constant 1 : index
301+
gpu.launch_func @kernels::@builtin_global_id_x
302+
blocks in (%c0, %c0, %c0) threads in (%c0, %c0, %c0)
303+
return
304+
}
305+
306+
// CHECK-LABEL: spv.module @{{.*}} Logical GLSL450
307+
// CHECK: spv.GlobalVariable [[GLOBALINVOCATIONID:@.*]] built_in("GlobalInvocationId")
308+
gpu.module @kernels {
309+
gpu.func @builtin_global_id_x() kernel
310+
attributes {spv.entry_point_abi = {local_size = dense<[16, 1, 1]>: vector<3xi32>}} {
311+
// CHECK: [[ADDRESS:%.*]] = spv.mlir.addressof [[GLOBALINVOCATIONID]]
312+
// CHECK-NEXT: [[VEC:%.*]] = spv.Load "Input" [[ADDRESS]]
313+
// CHECK-NEXT: {{%.*}} = spv.CompositeExtract [[VEC]]{{\[}}0 : i32{{\]}}
314+
%0 = gpu.global_id x
315+
gpu.return
316+
}
317+
}
318+
}
319+
320+
// -----
321+
322+
module attributes {gpu.container_module} {
323+
func @builtin() {
324+
%c0 = arith.constant 1 : index
325+
gpu.launch_func @kernels::@builtin_global_id_y
326+
blocks in (%c0, %c0, %c0) threads in (%c0, %c0, %c0)
327+
return
328+
}
329+
330+
// CHECK-LABEL: spv.module @{{.*}} Logical GLSL450
331+
// CHECK: spv.GlobalVariable [[GLOBALINVOCATIONID:@.*]] built_in("GlobalInvocationId")
332+
gpu.module @kernels {
333+
gpu.func @builtin_global_id_y() kernel
334+
attributes {spv.entry_point_abi = {local_size = dense<[16, 1, 1]>: vector<3xi32>}} {
335+
// CHECK: [[ADDRESS:%.*]] = spv.mlir.addressof [[GLOBALINVOCATIONID]]
336+
// CHECK-NEXT: [[VEC:%.*]] = spv.Load "Input" [[ADDRESS]]
337+
// CHECK-NEXT: {{%.*}} = spv.CompositeExtract [[VEC]]{{\[}}1 : i32{{\]}}
338+
%0 = gpu.global_id y
339+
gpu.return
340+
}
341+
}
342+
}
343+
344+
// -----
345+
346+
module attributes {gpu.container_module} {
347+
func @builtin() {
348+
%c0 = arith.constant 1 : index
349+
gpu.launch_func @kernels::@builtin_global_id_z
350+
blocks in (%c0, %c0, %c0) threads in (%c0, %c0, %c0)
351+
return
352+
}
353+
354+
// CHECK-LABEL: spv.module @{{.*}} Logical GLSL450
355+
// CHECK: spv.GlobalVariable [[GLOBALINVOCATIONID:@.*]] built_in("GlobalInvocationId")
356+
gpu.module @kernels {
357+
gpu.func @builtin_global_id_z() kernel
358+
attributes {spv.entry_point_abi = {local_size = dense<[16, 1, 1]>: vector<3xi32>}} {
359+
// CHECK: [[ADDRESS:%.*]] = spv.mlir.addressof [[GLOBALINVOCATIONID]]
360+
// CHECK-NEXT: [[VEC:%.*]] = spv.Load "Input" [[ADDRESS]]
361+
// CHECK-NEXT: {{%.*}} = spv.CompositeExtract [[VEC]]{{\[}}2 : i32{{\]}}
362+
%0 = gpu.global_id z
363+
gpu.return
364+
}
365+
}
366+
}
367+
368+
296369
// -----
297370

298371
module attributes {gpu.container_module} {

mlir/test/Dialect/GPU/ops.mlir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ module attributes {gpu.container_module} {
4444
%gDimY = gpu.grid_dim y
4545
%gDimZ = gpu.grid_dim z
4646

47+
%gIdX = gpu.global_id x
48+
%gIdY = gpu.global_id y
49+
%gIdZ = gpu.global_id z
50+
4751
%sgId = gpu.subgroup_id : index
4852
%numSg = gpu.num_subgroups : index
4953
%SgSi = gpu.subgroup_size : index

0 commit comments

Comments
 (0)