Skip to content

Commit 5b4f2b9

Browse files
authored
[mlir][gpu] Add an offloading handler attribute to gpu.module (#78047)
This patch adds an optional offloading handler attribute to the`gpu.module` op. This attribute will be used during `gpu-module-to-binary` pass to override the offloading handler used in the `gpu.binary` op.
1 parent 8b6b882 commit 5b4f2b9

File tree

6 files changed

+74
-12
lines changed

6 files changed

+74
-12
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,9 @@ def GPU_BarrierOp : GPU_Op<"barrier"> {
11941194
def GPU_GPUModuleOp : GPU_Op<"module", [
11951195
DataLayoutOpInterface, HasDefaultDLTIDataLayout, IsolatedFromAbove,
11961196
SymbolTable, Symbol, SingleBlockImplicitTerminator<"ModuleEndOp">
1197-
]>, Arguments<(ins OptionalAttr<GPUNonEmptyTargetArrayAttr>:$targets)> {
1197+
]>, Arguments<(ins
1198+
OptionalAttr<GPUNonEmptyTargetArrayAttr>:$targets,
1199+
OptionalAttr<OffloadingTranslationAttr>:$offloadingHandler)> {
11981200
let summary = "A top level compilation unit containing code to be run on a GPU.";
11991201
let description = [{
12001202
GPU module contains code that is intended to be run on a GPU. A host device
@@ -1215,22 +1217,33 @@ def GPU_GPUModuleOp : GPU_Op<"module", [
12151217
how to transform modules into binary strings and are used by the
12161218
`gpu-module-to-binary` pass to transform modules into GPU binaries.
12171219

1220+
Modules can contain an optional `OffloadingTranslationAttr` attribute. This
1221+
attribute will be used during the `gpu-module-to-binary` pass to specify the
1222+
`OffloadingTranslationAttr` used when creating the `gpu.binary` operation.
1223+
12181224
```
12191225
gpu.module @symbol_name {
12201226
gpu.func {}
12211227
...
12221228
gpu.module_end
12231229
}
1224-
gpu.module @symbol_name2 [#nvvm.target, #rocdl.target<chip = "gfx90a">] {
1230+
// Module with offloading handler and target attributes.
1231+
gpu.module @symbol_name2 <#gpu.select_object<1>> [
1232+
#nvvm.target,
1233+
#rocdl.target<chip = "gfx90a">] {
12251234
gpu.func {}
12261235
...
12271236
gpu.module_end
12281237
}
12291238
```
12301239
}];
12311240
let builders = [
1232-
OpBuilder<(ins "StringRef":$name, CArg<"ArrayAttr", "{}">:$targets)>,
1233-
OpBuilder<(ins "StringRef":$name, "ArrayRef<Attribute>":$targets)>
1241+
OpBuilder<(ins "StringRef":$name,
1242+
CArg<"ArrayAttr", "{}">:$targets,
1243+
CArg<"Attribute", "{}">:$handler)>,
1244+
OpBuilder<(ins "StringRef":$name,
1245+
"ArrayRef<Attribute>":$targets,
1246+
CArg<"Attribute", "{}">:$handler)>
12341247
];
12351248
let regions = (region SizedRegion<1>:$bodyRegion);
12361249
let hasCustomAssemblyFormat = 1;

mlir/lib/Dialect/GPU/IR/GPUDialect.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,19 +1724,24 @@ LogicalResult gpu::ReturnOp::verify() {
17241724
//===----------------------------------------------------------------------===//
17251725

17261726
void GPUModuleOp::build(OpBuilder &builder, OperationState &result,
1727-
StringRef name, ArrayAttr targets) {
1727+
StringRef name, ArrayAttr targets,
1728+
Attribute offloadingHandler) {
17281729
ensureTerminator(*result.addRegion(), builder, result.location);
17291730
result.attributes.push_back(builder.getNamedAttr(
17301731
::mlir::SymbolTable::getSymbolAttrName(), builder.getStringAttr(name)));
17311732

1733+
Properties &props = result.getOrAddProperties<Properties>();
17321734
if (targets)
1733-
result.getOrAddProperties<Properties>().targets = targets;
1735+
props.targets = targets;
1736+
props.offloadingHandler = offloadingHandler;
17341737
}
17351738

17361739
void GPUModuleOp::build(OpBuilder &builder, OperationState &result,
1737-
StringRef name, ArrayRef<Attribute> targets) {
1740+
StringRef name, ArrayRef<Attribute> targets,
1741+
Attribute offloadingHandler) {
17381742
build(builder, result, name,
1739-
targets.empty() ? ArrayAttr() : builder.getArrayAttr(targets));
1743+
targets.empty() ? ArrayAttr() : builder.getArrayAttr(targets),
1744+
offloadingHandler);
17401745
}
17411746

17421747
ParseResult GPUModuleOp::parse(OpAsmParser &parser, OperationState &result) {
@@ -1747,14 +1752,24 @@ ParseResult GPUModuleOp::parse(OpAsmParser &parser, OperationState &result) {
17471752
result.attributes))
17481753
return failure();
17491754

1755+
Properties &props = result.getOrAddProperties<Properties>();
1756+
1757+
// Parse the optional offloadingHandler
1758+
if (succeeded(parser.parseOptionalLess())) {
1759+
if (parser.parseAttribute(props.offloadingHandler))
1760+
return failure();
1761+
if (parser.parseGreater())
1762+
return failure();
1763+
}
1764+
17501765
// Parse the optional array of target attributes.
17511766
OptionalParseResult targetsAttrResult =
17521767
parser.parseOptionalAttribute(targetsAttr, Type{});
17531768
if (targetsAttrResult.has_value()) {
17541769
if (failed(*targetsAttrResult)) {
17551770
return failure();
17561771
}
1757-
result.getOrAddProperties<Properties>().targets = targetsAttr;
1772+
props.targets = targetsAttr;
17581773
}
17591774

17601775
// If module attributes are present, parse them.
@@ -1775,15 +1790,22 @@ void GPUModuleOp::print(OpAsmPrinter &p) {
17751790
p << ' ';
17761791
p.printSymbolName(getName());
17771792

1793+
if (Attribute attr = getOffloadingHandlerAttr()) {
1794+
p << " <";
1795+
p.printAttribute(attr);
1796+
p << ">";
1797+
}
1798+
17781799
if (Attribute attr = getTargetsAttr()) {
17791800
p << ' ';
17801801
p.printAttribute(attr);
17811802
p << ' ';
17821803
}
17831804

1784-
p.printOptionalAttrDictWithKeyword(
1785-
(*this)->getAttrs(),
1786-
{mlir::SymbolTable::getSymbolAttrName(), getTargetsAttrName()});
1805+
p.printOptionalAttrDictWithKeyword((*this)->getAttrs(),
1806+
{mlir::SymbolTable::getSymbolAttrName(),
1807+
getTargetsAttrName(),
1808+
getOffloadingHandlerAttrName()});
17871809
p << ' ';
17881810
p.printRegion(getRegion(), /*printEntryBlockArgs=*/false,
17891811
/*printBlockTerminators=*/false);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ LogicalResult moduleSerializer(GPUModuleOp op,
124124
}
125125
objects.push_back(object);
126126
}
127+
if (auto moduleHandler =
128+
dyn_cast_or_null<OffloadingLLVMTranslationAttrInterface>(
129+
op.getOffloadingHandlerAttr());
130+
!handler && moduleHandler)
131+
handler = moduleHandler;
127132
builder.setInsertionPointAfter(op);
128133
builder.create<gpu::BinaryOp>(op.getLoc(), op.getName(), handler,
129134
builder.getArrayAttr(objects));

mlir/test/Dialect/GPU/invalid.mlir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,3 +818,10 @@ func.func @main(%arg0 : index) {
818818
return
819819
}
820820

821+
// -----
822+
823+
module attributes {gpu.container_module} {
824+
// expected-error@+1 {{expected attribute value}}
825+
gpu.module @kernel <> {
826+
}
827+
}

mlir/test/Dialect/GPU/module-to-binary-nvvm.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ module attributes {gpu.container_module} {
2222
llvm.return
2323
}
2424
}
25+
26+
// CHECK-LABEL:gpu.binary @kernel_module3 <#gpu.select_object<1 : i64>>
27+
// CHECK:[#gpu.object<#nvvm.target<chip = "sm_70">, offload = "{{.*}}">, #gpu.object<#nvvm.target<chip = "sm_80">, offload = "{{.*}}">]
28+
gpu.module @kernel_module3 <#gpu.select_object<1>> [
29+
#nvvm.target<chip = "sm_70">,
30+
#nvvm.target<chip = "sm_80">] {
31+
llvm.func @kernel(%arg0: i32, %arg1: !llvm.ptr,
32+
%arg2: !llvm.ptr, %arg3: i64, %arg4: i64,
33+
%arg5: i64) attributes {gpu.kernel} {
34+
llvm.return
35+
}
36+
}
2537
}

mlir/test/Dialect/GPU/ops.mlir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,6 @@ gpu.module @module_with_two_target [#nvvm.target, #rocdl.target<chip = "gfx90a">
423423
gpu.return
424424
}
425425
}
426+
427+
gpu.module @module_with_offload_handler <#gpu.select_object<0>> [#nvvm.target] {
428+
}

0 commit comments

Comments
 (0)