Skip to content

Commit cc965f7

Browse files
committed
[mlir][gpu][RFC] Add a source language enum attribute to gpu.func and gpu.launch ops
1 parent a80aad2 commit cc965f7

File tree

4 files changed

+82
-21
lines changed

4 files changed

+82
-21
lines changed

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

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,23 @@ def GPU_OptionalDimSizeHintAttr : ConfinedAttr<OptionalAttr<DenseI32ArrayAttr>,
351351
[AttrConstraint<Or<[IsNullAttr.predicate, DenseArrayCount<3>.predicate]>,
352352
"with 3 elements (if present)">]>;
353353

354+
// Source language of the gpu.func or gpu.launc_func operations.
355+
def GPU_KernelSourceLangOpenACC : I32EnumAttrCase<"OpenACC", 0, "openacc">;
356+
def GPU_KernelSourceLangOpenMP : I32EnumAttrCase<"OpenMP", 1, "openmp">;
357+
def GPU_KernelSourceLangCUDAFortran
358+
: I32EnumAttrCase<"CUDAFortran", 2, "cuda_fortran">;
359+
360+
def GPU_KernelSourceLang
361+
: I32EnumAttr<"KernelSourceLang", "Source language of a kernel",
362+
[GPU_KernelSourceLangOpenACC, GPU_KernelSourceLangOpenMP,
363+
GPU_KernelSourceLangCUDAFortran,
364+
]> {
365+
let genSpecializedAttr = 0;
366+
let cppNamespace = "::mlir::gpu";
367+
}
368+
def GPU_KernelSourceLangAttr
369+
: EnumAttr<GPU_Dialect, GPU_KernelSourceLang, "kernel_source_lang">;
370+
354371
def GPU_GPUFuncOp : GPU_Op<"func", [
355372
HasParent<"GPUModuleOp">, AutomaticAllocationScope, FunctionOpInterface,
356373
IsolatedFromAbove, AffineScope
@@ -426,12 +443,13 @@ def GPU_GPUFuncOp : GPU_Op<"func", [
426443
}];
427444

428445
let arguments = (ins TypeAttrOf<FunctionType>:$function_type,
429-
OptionalAttr<DictArrayAttr>:$arg_attrs,
430-
OptionalAttr<DictArrayAttr>:$res_attrs,
431-
OptionalAttr<DictArrayAttr>:$workgroup_attrib_attrs,
432-
OptionalAttr<DictArrayAttr>:$private_attrib_attrs,
433-
GPU_OptionalDimSizeHintAttr:$known_block_size,
434-
GPU_OptionalDimSizeHintAttr:$known_grid_size);
446+
OptionalAttr<DictArrayAttr>:$arg_attrs,
447+
OptionalAttr<DictArrayAttr>:$res_attrs,
448+
OptionalAttr<DictArrayAttr>:$workgroup_attrib_attrs,
449+
OptionalAttr<DictArrayAttr>:$private_attrib_attrs,
450+
GPU_OptionalDimSizeHintAttr:$known_block_size,
451+
GPU_OptionalDimSizeHintAttr:$known_grid_size,
452+
OptionalAttr<GPU_KernelSourceLangAttr>:$kernel_source_lang);
435453
let regions = (region AnyRegion:$body);
436454

437455
let skipDefaultBuilders = 1;
@@ -793,20 +811,21 @@ def GPU_LaunchFuncOp :GPU_Op<"launch_func", [
793811
let hasVerifier = 1;
794812
}
795813

796-
def GPU_LaunchOp : GPU_Op<"launch", [
797-
AffineScope, AutomaticAllocationScope, AttrSizedOperandSegments,
798-
DeclareOpInterfaceMethods<InferIntRangeInterface, ["inferResultRanges"]>,
799-
GPU_AsyncOpInterface, RecursiveMemoryEffects]>,
800-
Arguments<(ins Variadic<GPU_AsyncToken>:$asyncDependencies,
801-
Index:$gridSizeX, Index:$gridSizeY, Index:$gridSizeZ,
802-
Index:$blockSizeX, Index:$blockSizeY, Index:$blockSizeZ,
803-
Optional<Index>:$clusterSizeX,
804-
Optional<Index>:$clusterSizeY,
805-
Optional<Index>:$clusterSizeZ,
806-
Optional<I32>:$dynamicSharedMemorySize,
807-
OptionalAttr<SymbolRefAttr>:$kernelFunc,
808-
OptionalAttr<SymbolRefAttr>:$kernelModule)>,
809-
Results<(outs Optional<GPU_AsyncToken>:$asyncToken)> {
814+
def GPU_LaunchOp
815+
: GPU_Op<"launch", [AffineScope, AutomaticAllocationScope,
816+
AttrSizedOperandSegments,
817+
DeclareOpInterfaceMethods<
818+
InferIntRangeInterface, ["inferResultRanges"]>,
819+
GPU_AsyncOpInterface, RecursiveMemoryEffects]>,
820+
Arguments<(ins Variadic<GPU_AsyncToken>:$asyncDependencies,
821+
Index:$gridSizeX, Index:$gridSizeY, Index:$gridSizeZ,
822+
Index:$blockSizeX, Index:$blockSizeY, Index:$blockSizeZ,
823+
Optional<Index>:$clusterSizeX, Optional<Index>:$clusterSizeY,
824+
Optional<Index>:$clusterSizeZ, Optional<I32>:$dynamicSharedMemorySize,
825+
OptionalAttr<SymbolRefAttr>:$kernelFunc,
826+
OptionalAttr<SymbolRefAttr>:$kernelModule,
827+
OptionalAttr<GPU_KernelSourceLangAttr>:$kernelSourceLang)>,
828+
Results<(outs Optional<GPU_AsyncToken>:$asyncToken)> {
810829
let summary = "GPU kernel launch operation";
811830

812831
let description = [{
@@ -840,7 +859,10 @@ def GPU_LaunchOp : GPU_Op<"launch", [
840859
- a variadic number of Private memory attributions.
841860

842861
The `kernelFunc` and `kernelModule` attributes are optional and specifies
843-
the kernel name and a module in which the kernel should be outlined.
862+
the kernel name and a module in which the kernel should be outlined.
863+
864+
The optional `kernelSourceLang` attribute can be set to specify the Source
865+
language of the kernel.
844866

845867
Syntax:
846868

mlir/lib/Dialect/GPU/Transforms/KernelOutlining.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ static gpu::GPUFuncOp outlineKernelFuncImpl(gpu::LaunchOp launchOp,
202202
TypeRange(ValueRange(launchOp.getPrivateAttributions())));
203203
outlinedFunc->setAttr(gpu::GPUDialect::getKernelFuncAttrName(),
204204
builder.getUnitAttr());
205+
outlinedFunc.setKernelSourceLangAttr(launchOp.getKernelSourceLangAttr());
205206

206207
// If we can infer bounds on the grid and/or block sizes from the arguments
207208
// to the launch op, propagate them to the generated kernel. This is safe

mlir/test/Dialect/GPU/ops.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ module attributes {gpu.container_module} {
1414
// CHECK: gpu.terminator
1515
gpu.terminator
1616
}
17+
// CHECK: gpu.launch
18+
gpu.launch blocks(%bx, %by, %bz) in (%grid_x = %sz, %grid_y = %sz, %grid_z = %sz)
19+
threads(%tx, %ty, %tz) in (%block_x = %sz, %block_y = %sz, %block_z = %sz) {
20+
// CHECK: gpu.terminator
21+
gpu.terminator
22+
// CHECK: } {kernelSourceLang = #gpu<kernel_source_lang openmp>}
23+
} {kernelSourceLang = #gpu<kernel_source_lang openmp>}
1724
return
1825
}
1926

@@ -279,6 +286,12 @@ module attributes {gpu.container_module} {
279286
gpu.func @empty_attribution(%arg0: f32) workgroup() private() {
280287
gpu.return
281288
}
289+
290+
// CHECK-LABEL: gpu.func @source_lang(%{{.*}}: f32) kernel attributes {kernel_source_lang = #gpu<kernel_source_lang openacc>}
291+
// CHECK: {
292+
gpu.func @source_lang(%arg0: f32) kernel attributes {kernel_source_lang = #gpu<kernel_source_lang openacc>} {
293+
gpu.return
294+
}
282295
}
283296

284297
gpu.module @explicit_attributions {

mlir/test/Dialect/GPU/outlining.mlir

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,28 @@ func.func @testNoAttributes() {
630630
}
631631
return
632632
}
633+
634+
// -----
635+
636+
// This test tests that the kernelSourceLang is propagated to the gpu.func.
637+
638+
// CHECK-LABEL: func.func @testKernelFuncOnly()
639+
// CHECK: gpu.launch_func @testKernelFuncOnly_kernel::@testKernelFuncOnly_kernel
640+
641+
// CHECK: gpu.module @testKernelFuncOnly_kernel
642+
// CHECK: gpu.func @testKernelFuncOnly_kernel() kernel attributes {kernel_source_lang = #gpu<kernel_source_lang openacc>
643+
func.func @testKernelFuncOnly() {
644+
%gDimX = arith.constant 8 : index
645+
%gDimY = arith.constant 12 : index
646+
%gDimZ = arith.constant 16 : index
647+
%bDimX = arith.constant 32 : index
648+
%bDimY = arith.constant 16 : index
649+
%bDimZ = arith.constant 8 : index
650+
651+
gpu.launch blocks(%bx, %by, %bz) in (%grid_x = %gDimX, %grid_y = %gDimY, %grid_z = %gDimZ)
652+
threads(%tx, %ty, %tz) in (%block_x = %bDimX, %block_y = %bDimY, %block_z = %bDimZ) {
653+
"some_op"(%bx, %tx) : (index, index) -> ()
654+
gpu.terminator
655+
} {kernelSourceLang = #gpu<kernel_source_lang openacc>}
656+
return
657+
}

0 commit comments

Comments
 (0)