Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
[mlir][gpu] Add an offloading handler attribute to gpu.module (#78047)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fabianmcg authored Jan 15, 2024
1 parent 8b6b882 commit 5b4f2b9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
21 changes: 17 additions & 4 deletions mlir/include/mlir/Dialect/GPU/IR/GPUOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,9 @@ def GPU_BarrierOp : GPU_Op<"barrier"> {
def GPU_GPUModuleOp : GPU_Op<"module", [
DataLayoutOpInterface, HasDefaultDLTIDataLayout, IsolatedFromAbove,
SymbolTable, Symbol, SingleBlockImplicitTerminator<"ModuleEndOp">
]>, Arguments<(ins OptionalAttr<GPUNonEmptyTargetArrayAttr>:$targets)> {
]>, Arguments<(ins
OptionalAttr<GPUNonEmptyTargetArrayAttr>:$targets,
OptionalAttr<OffloadingTranslationAttr>:$offloadingHandler)> {
let summary = "A top level compilation unit containing code to be run on a GPU.";
let description = [{
GPU module contains code that is intended to be run on a GPU. A host device
Expand All @@ -1215,22 +1217,33 @@ def GPU_GPUModuleOp : GPU_Op<"module", [
how to transform modules into binary strings and are used by the
`gpu-module-to-binary` pass to transform modules into GPU binaries.

Modules can contain an optional `OffloadingTranslationAttr` attribute. This
attribute will be used during the `gpu-module-to-binary` pass to specify the
`OffloadingTranslationAttr` used when creating the `gpu.binary` operation.

```
gpu.module @symbol_name {
gpu.func {}
...
gpu.module_end
}
gpu.module @symbol_name2 [#nvvm.target, #rocdl.target<chip = "gfx90a">] {
// Module with offloading handler and target attributes.
gpu.module @symbol_name2 <#gpu.select_object<1>> [
#nvvm.target,
#rocdl.target<chip = "gfx90a">] {
gpu.func {}
...
gpu.module_end
}
```
}];
let builders = [
OpBuilder<(ins "StringRef":$name, CArg<"ArrayAttr", "{}">:$targets)>,
OpBuilder<(ins "StringRef":$name, "ArrayRef<Attribute>":$targets)>
OpBuilder<(ins "StringRef":$name,
CArg<"ArrayAttr", "{}">:$targets,
CArg<"Attribute", "{}">:$handler)>,
OpBuilder<(ins "StringRef":$name,
"ArrayRef<Attribute>":$targets,
CArg<"Attribute", "{}">:$handler)>
];
let regions = (region SizedRegion<1>:$bodyRegion);
let hasCustomAssemblyFormat = 1;
Expand Down
38 changes: 30 additions & 8 deletions mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1724,19 +1724,24 @@ LogicalResult gpu::ReturnOp::verify() {
//===----------------------------------------------------------------------===//

void GPUModuleOp::build(OpBuilder &builder, OperationState &result,
StringRef name, ArrayAttr targets) {
StringRef name, ArrayAttr targets,
Attribute offloadingHandler) {
ensureTerminator(*result.addRegion(), builder, result.location);
result.attributes.push_back(builder.getNamedAttr(
::mlir::SymbolTable::getSymbolAttrName(), builder.getStringAttr(name)));

Properties &props = result.getOrAddProperties<Properties>();
if (targets)
result.getOrAddProperties<Properties>().targets = targets;
props.targets = targets;
props.offloadingHandler = offloadingHandler;
}

void GPUModuleOp::build(OpBuilder &builder, OperationState &result,
StringRef name, ArrayRef<Attribute> targets) {
StringRef name, ArrayRef<Attribute> targets,
Attribute offloadingHandler) {
build(builder, result, name,
targets.empty() ? ArrayAttr() : builder.getArrayAttr(targets));
targets.empty() ? ArrayAttr() : builder.getArrayAttr(targets),
offloadingHandler);
}

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

Properties &props = result.getOrAddProperties<Properties>();

// Parse the optional offloadingHandler
if (succeeded(parser.parseOptionalLess())) {
if (parser.parseAttribute(props.offloadingHandler))
return failure();
if (parser.parseGreater())
return failure();
}

// Parse the optional array of target attributes.
OptionalParseResult targetsAttrResult =
parser.parseOptionalAttribute(targetsAttr, Type{});
if (targetsAttrResult.has_value()) {
if (failed(*targetsAttrResult)) {
return failure();
}
result.getOrAddProperties<Properties>().targets = targetsAttr;
props.targets = targetsAttr;
}

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

if (Attribute attr = getOffloadingHandlerAttr()) {
p << " <";
p.printAttribute(attr);
p << ">";
}

if (Attribute attr = getTargetsAttr()) {
p << ' ';
p.printAttribute(attr);
p << ' ';
}

p.printOptionalAttrDictWithKeyword(
(*this)->getAttrs(),
{mlir::SymbolTable::getSymbolAttrName(), getTargetsAttrName()});
p.printOptionalAttrDictWithKeyword((*this)->getAttrs(),
{mlir::SymbolTable::getSymbolAttrName(),
getTargetsAttrName(),
getOffloadingHandlerAttrName()});
p << ' ';
p.printRegion(getRegion(), /*printEntryBlockArgs=*/false,
/*printBlockTerminators=*/false);
Expand Down
5 changes: 5 additions & 0 deletions mlir/lib/Dialect/GPU/Transforms/ModuleToBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ LogicalResult moduleSerializer(GPUModuleOp op,
}
objects.push_back(object);
}
if (auto moduleHandler =
dyn_cast_or_null<OffloadingLLVMTranslationAttrInterface>(
op.getOffloadingHandlerAttr());
!handler && moduleHandler)
handler = moduleHandler;
builder.setInsertionPointAfter(op);
builder.create<gpu::BinaryOp>(op.getLoc(), op.getName(), handler,
builder.getArrayAttr(objects));
Expand Down
7 changes: 7 additions & 0 deletions mlir/test/Dialect/GPU/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,10 @@ func.func @main(%arg0 : index) {
return
}

// -----

module attributes {gpu.container_module} {
// expected-error@+1 {{expected attribute value}}
gpu.module @kernel <> {
}
}
12 changes: 12 additions & 0 deletions mlir/test/Dialect/GPU/module-to-binary-nvvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ module attributes {gpu.container_module} {
llvm.return
}
}

// CHECK-LABEL:gpu.binary @kernel_module3 <#gpu.select_object<1 : i64>>
// CHECK:[#gpu.object<#nvvm.target<chip = "sm_70">, offload = "{{.*}}">, #gpu.object<#nvvm.target<chip = "sm_80">, offload = "{{.*}}">]
gpu.module @kernel_module3 <#gpu.select_object<1>> [
#nvvm.target<chip = "sm_70">,
#nvvm.target<chip = "sm_80">] {
llvm.func @kernel(%arg0: i32, %arg1: !llvm.ptr,
%arg2: !llvm.ptr, %arg3: i64, %arg4: i64,
%arg5: i64) attributes {gpu.kernel} {
llvm.return
}
}
}
3 changes: 3 additions & 0 deletions mlir/test/Dialect/GPU/ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,6 @@ gpu.module @module_with_two_target [#nvvm.target, #rocdl.target<chip = "gfx90a">
gpu.return
}
}

gpu.module @module_with_offload_handler <#gpu.select_object<0>> [#nvvm.target] {
}

0 comments on commit 5b4f2b9

Please sign in to comment.