Skip to content

Commit a9c82b2

Browse files
fabianmcgjustinfargnoli
authored andcommitted
[mlir][gpu] Add an offloading handler attribute to gpu.module (llvm#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 2eecdf5 commit a9c82b2

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
@@ -1727,19 +1727,24 @@ LogicalResult gpu::ReturnOp::verify() {
17271727
//===----------------------------------------------------------------------===//
17281728

17291729
void GPUModuleOp::build(OpBuilder &builder, OperationState &result,
1730-
StringRef name, ArrayAttr targets) {
1730+
StringRef name, ArrayAttr targets,
1731+
Attribute offloadingHandler) {
17311732
ensureTerminator(*result.addRegion(), builder, result.location);
17321733
result.attributes.push_back(builder.getNamedAttr(
17331734
::mlir::SymbolTable::getSymbolAttrName(), builder.getStringAttr(name)));
17341735

1736+
Properties &props = result.getOrAddProperties<Properties>();
17351737
if (targets)
1736-
result.getOrAddProperties<Properties>().targets = targets;
1738+
props.targets = targets;
1739+
props.offloadingHandler = offloadingHandler;
17371740
}
17381741

17391742
void GPUModuleOp::build(OpBuilder &builder, OperationState &result,
1740-
StringRef name, ArrayRef<Attribute> targets) {
1743+
StringRef name, ArrayRef<Attribute> targets,
1744+
Attribute offloadingHandler) {
17411745
build(builder, result, name,
1742-
targets.empty() ? ArrayAttr() : builder.getArrayAttr(targets));
1746+
targets.empty() ? ArrayAttr() : builder.getArrayAttr(targets),
1747+
offloadingHandler);
17431748
}
17441749

17451750
ParseResult GPUModuleOp::parse(OpAsmParser &parser, OperationState &result) {
@@ -1750,14 +1755,24 @@ ParseResult GPUModuleOp::parse(OpAsmParser &parser, OperationState &result) {
17501755
result.attributes))
17511756
return failure();
17521757

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

17631778
// If module attributes are present, parse them.
@@ -1778,15 +1793,22 @@ void GPUModuleOp::print(OpAsmPrinter &p) {
17781793
p << ' ';
17791794
p.printSymbolName(getName());
17801795

1796+
if (Attribute attr = getOffloadingHandlerAttr()) {
1797+
p << " <";
1798+
p.printAttribute(attr);
1799+
p << ">";
1800+
}
1801+
17811802
if (Attribute attr = getTargetsAttr()) {
17821803
p << ' ';
17831804
p.printAttribute(attr);
17841805
p << ' ';
17851806
}
17861807

1787-
p.printOptionalAttrDictWithKeyword(
1788-
(*this)->getAttrs(),
1789-
{mlir::SymbolTable::getSymbolAttrName(), getTargetsAttrName()});
1808+
p.printOptionalAttrDictWithKeyword((*this)->getAttrs(),
1809+
{mlir::SymbolTable::getSymbolAttrName(),
1810+
getTargetsAttrName(),
1811+
getOffloadingHandlerAttrName()});
17901812
p << ' ';
17911813
p.printRegion(getRegion(), /*printEntryBlockArgs=*/false,
17921814
/*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)