Skip to content

Commit 016e1eb

Browse files
fabianmcgjoker-eph
andauthored
[mlir][gpu] Add metadata attributes for storing kernel metadata in GPU objects (#95292)
This patch adds the `#gpu.kernel_metadata` and `#gpu.kernel_table` attributes. The `#gpu.kernel_metadata` attribute allows storing metadata related to a compiled kernel, for example, the number of scalar registers used by the kernel. The attribute only has 2 required parameters, the name and function type. It also has 2 optional parameters, the arguments attributes and generic dictionary for storing all other metadata. The `#gpu.kernel_table` stores a table of `#gpu.kernel_metadata`, mapping the name of the kernel to the metadata. Finally, the function `ROCDL::getAMDHSAKernelsELFMetadata` was added to collect ELF metadata from a binary, and to test the class methods in both attributes. Example: ```mlir gpu.binary @binary [#gpu.object<#rocdl.target<chip = "gfx900">, kernels = #gpu.kernel_table<[ #gpu.kernel_metadata<"kernel0", (i32) -> (), metadata = {sgpr_count = 255}>, #gpu.kernel_metadata<"kernel1", (i32, f32) -> (), arg_attrs = [{llvm.read_only}, {}]> ]> , bin = "BLOB">] ``` The motivation behind these attributes is to provide useful information for things like tunning. --------- Co-authored-by: Mehdi Amini <[email protected]>
1 parent de687ea commit 016e1eb

File tree

16 files changed

+567
-23
lines changed

16 files changed

+567
-23
lines changed

mlir/include/mlir-c/Dialect/GPU.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ MLIR_CAPI_EXPORTED MlirAttribute
3737
mlirGPUObjectAttrGet(MlirContext mlirCtx, MlirAttribute target, uint32_t format,
3838
MlirStringRef objectStrRef, MlirAttribute mlirObjectProps);
3939

40+
MLIR_CAPI_EXPORTED MlirAttribute mlirGPUObjectAttrGetWithKernels(
41+
MlirContext mlirCtx, MlirAttribute target, uint32_t format,
42+
MlirStringRef objectStrRef, MlirAttribute mlirObjectProps,
43+
MlirAttribute mlirKernelsAttr);
44+
4045
MLIR_CAPI_EXPORTED MlirAttribute
4146
mlirGPUObjectAttrGetTarget(MlirAttribute mlirObjectAttr);
4247

@@ -52,6 +57,12 @@ mlirGPUObjectAttrHasProperties(MlirAttribute mlirObjectAttr);
5257
MLIR_CAPI_EXPORTED MlirAttribute
5358
mlirGPUObjectAttrGetProperties(MlirAttribute mlirObjectAttr);
5459

60+
MLIR_CAPI_EXPORTED bool
61+
mlirGPUObjectAttrHasKernels(MlirAttribute mlirObjectAttr);
62+
63+
MLIR_CAPI_EXPORTED MlirAttribute
64+
mlirGPUObjectAttrGetKernels(MlirAttribute mlirObjectAttr);
65+
5566
#ifdef __cplusplus
5667
}
5768
#endif

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

Lines changed: 171 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,155 @@
1616
include "mlir/Dialect/GPU/IR/GPUBase.td"
1717
include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.td"
1818

19+
//===----------------------------------------------------------------------===//
20+
// GPU kernel metadata attribute
21+
//===----------------------------------------------------------------------===//
22+
23+
def GPU_KernelMetadataAttr : GPU_Attr<"KernelMetadata", "kernel_metadata"> {
24+
let description = [{
25+
GPU attribute for storing metadata related to a compiled kernel. The
26+
attribute contains the name and arguments type of the kernel.
27+
28+
The attribute also contains optional parameters for storing the arguments
29+
attributes as well as a dictionary for additional metadata, like occupancy
30+
information or other function attributes.
31+
32+
Note: The `arg_attrs` parameter is expected to follow all the constraints
33+
imposed by the `mlir::FunctionOpInterface` interface.
34+
35+
Examples:
36+
```mlir
37+
#gpu.kernel_metadata<@kernel1, (i32) -> (), arg_attrs = [...], metadata = {reg_count = 255, ...}>
38+
#gpu.kernel_metadata<@kernel2, (i32, f64) -> ()>
39+
```
40+
}];
41+
let parameters = (ins
42+
"StringAttr":$name,
43+
"Type":$function_type,
44+
OptionalParameter<"ArrayAttr", "arguments attributes">:$arg_attrs,
45+
OptionalParameter<"DictionaryAttr", "metadata dictionary">:$metadata
46+
);
47+
let assemblyFormat = [{
48+
`<` $name `,` $function_type (`,` struct($arg_attrs, $metadata)^)? `>`
49+
}];
50+
let builders = [
51+
AttrBuilderWithInferredContext<(ins "StringAttr":$name,
52+
"Type":$functionType,
53+
CArg<"ArrayAttr", "nullptr">:$argAttrs,
54+
CArg<"DictionaryAttr",
55+
"nullptr">:$metadata), [{
56+
assert(name && "invalid name");
57+
return $_get(name.getContext(), name, functionType, argAttrs, metadata);
58+
}]>,
59+
AttrBuilderWithInferredContext<(ins "FunctionOpInterface":$kernel,
60+
CArg<"DictionaryAttr",
61+
"nullptr">:$metadata)>
62+
];
63+
let genVerifyDecl = 1;
64+
let extraClassDeclaration = [{
65+
/// Compare two kernels based on the name.
66+
bool operator<(const KernelMetadataAttr& other) const {
67+
return getName().getValue() < other.getName().getValue();
68+
}
69+
70+
/// Returns the metadata attribute corresponding to `key` or `nullptr`
71+
/// if missing.
72+
Attribute getAttr(StringRef key) const {
73+
DictionaryAttr attrs = getMetadata();
74+
return attrs ? attrs.get(key) : nullptr;
75+
}
76+
template <typename ConcreteAttr>
77+
ConcreteAttr getAttr(StringRef key) const {
78+
return llvm::dyn_cast_or_null<ConcreteAttr>(getAttr(key));
79+
}
80+
Attribute getAttr(StringAttr key) const {
81+
DictionaryAttr attrs = getMetadata();
82+
return attrs ? attrs.get(key) : nullptr;
83+
}
84+
template <typename ConcreteAttr>
85+
ConcreteAttr getAttr(StringAttr key) const {
86+
return llvm::dyn_cast_or_null<ConcreteAttr>(getAttr(key));
87+
}
88+
89+
/// Returns the attribute dictionary at position `index`.
90+
DictionaryAttr getArgAttrDict(unsigned index) {
91+
ArrayAttr argArray = getArgAttrs();
92+
return argArray ? llvm::cast<DictionaryAttr>(argArray[index]) : nullptr;
93+
}
94+
95+
/// Return the specified attribute, if present, for the argument at 'index',
96+
/// null otherwise.
97+
Attribute getArgAttr(unsigned index, StringAttr name) {
98+
DictionaryAttr argDict = getArgAttrDict(index);
99+
return argDict ? argDict.get(name) : nullptr;
100+
}
101+
Attribute getArgAttr(unsigned index, StringRef name) {
102+
DictionaryAttr argDict = getArgAttrDict(index);
103+
return argDict ? argDict.get(name) : nullptr;
104+
}
105+
106+
/// Returns a new KernelMetadataAttr that contains `attrs` in the metadata dictionary.
107+
KernelMetadataAttr appendMetadata(ArrayRef<NamedAttribute> attrs) const;
108+
}];
109+
}
110+
111+
//===----------------------------------------------------------------------===//
112+
// GPU kernel table attribute
113+
//===----------------------------------------------------------------------===//
114+
115+
def GPU_KernelTableAttr : GPU_Attr<"KernelTable", "kernel_table"> {
116+
let description = [{
117+
GPU attribute representing a list of `#gpu.kernel_metadata` attributes. This
118+
attribute supports searching kernels by name. All kernels in the table must
119+
have an unique name.
120+
121+
Examples:
122+
```mlir
123+
// Empty table.
124+
#gpu.kernel_table<>
125+
126+
// Table with a single kernel.
127+
#gpu.kernel_table<[#gpu.kernel_metadata<kernel0, () -> () >]>
128+
129+
// Table with multiple kernels.
130+
#gpu.kernel_table<[
131+
#gpu.kernel_metadata<"kernel0", (i32, f32) -> (), metadata = {sgpr_count = 255}>,
132+
#gpu.kernel_metadata<"kernel1", (i32) -> ()>
133+
]>
134+
```
135+
}];
136+
let parameters = (ins
137+
OptionalArrayRefParameter<"KernelMetadataAttr", "array of kernels">:$kernel_table
138+
);
139+
let assemblyFormat = [{
140+
`<` (`[` qualified($kernel_table)^ `]`)? `>`
141+
}];
142+
let builders = [
143+
AttrBuilder<(ins "ArrayRef<KernelMetadataAttr>":$kernels,
144+
CArg<"bool", "false">:$isSorted)>
145+
];
146+
let skipDefaultBuilders = 1;
147+
let genVerifyDecl = 1;
148+
let extraClassDeclaration = [{
149+
llvm::ArrayRef<KernelMetadataAttr>::iterator begin() const {
150+
return getKernelTable().begin();
151+
}
152+
llvm::ArrayRef<KernelMetadataAttr>::iterator end() const {
153+
return getKernelTable().end();
154+
}
155+
size_t size() const {
156+
return getKernelTable().size();
157+
}
158+
bool empty() const {
159+
return getKernelTable().empty();
160+
}
161+
162+
/// Returns the kernel with name `key` or `nullptr` if not present.
163+
KernelMetadataAttr lookup(StringRef key) const;
164+
KernelMetadataAttr lookup(StringAttr key) const;
165+
}];
166+
}
167+
19168
//===----------------------------------------------------------------------===//
20169
// GPU object attribute.
21170
//===----------------------------------------------------------------------===//
@@ -36,8 +185,9 @@ def GPU_CompilationTargetEnum : GPU_I32Enum<
36185
def GPU_ObjectAttr : GPU_Attr<"Object", "object"> {
37186
let description = [{
38187
A GPU object attribute glues together a GPU target, the object kind, a
39-
binary string with the object, and the object properties, encapsulating how
40-
the object was generated and its properties with the object itself.
188+
binary string with the object, the object properties, and kernel metadata,
189+
encapsulating how the object was generated and its properties with the
190+
object itself.
41191

42192
There are four object formats:
43193
1. `Offload`: represents generic objects not described by the other three
@@ -55,6 +205,10 @@ def GPU_ObjectAttr : GPU_Attr<"Object", "object"> {
55205

56206
Object properties are specified through the `properties` dictionary
57207
attribute and can be used to define additional information.
208+
209+
Kernel metadata is specified through the `kernels` parameter, and can be
210+
used to specify additional information on a kernel by kernel basis.
211+
58212
The target attribute must implement or promise the `TargetAttrInterface`
59213
interface.
60214

@@ -63,16 +217,29 @@ def GPU_ObjectAttr : GPU_Attr<"Object", "object"> {
63217
#gpu.object<#nvvm.target, properties = {O = 3 : i32}, assembly = "..."> // An assembly object with additional properties.
64218
#gpu.object<#rocdl.target, bin = "..."> // A binary object.
65219
#gpu.object<#nvvm.target, "..."> // A fatbin object.
220+
#gpu.object<#nvvm.target, kernels = #gpu.kernel_table<...>, "..."> // An object with a kernel table.
66221
```
67222
}];
68223
let parameters = (ins
69224
"Attribute":$target,
70225
DefaultValuedParameter<"CompilationTarget", "CompilationTarget::Fatbin">:$format,
71226
"StringAttr":$object,
72-
OptionalParameter<"DictionaryAttr">:$properties
227+
OptionalParameter<"DictionaryAttr">:$properties,
228+
OptionalParameter<"KernelTableAttr">:$kernels
73229
);
230+
let builders = [
231+
AttrBuilderWithInferredContext<(ins "Attribute":$target,
232+
"CompilationTarget":$format,
233+
"StringAttr":$object,
234+
CArg<"DictionaryAttr", "nullptr">:$properties,
235+
CArg<"KernelTableAttr", "nullptr">:$kernels), [{
236+
assert(target && "invalid target");
237+
return $_get(target.getContext(), target, format, object, properties, kernels);
238+
}]>
239+
];
74240
let assemblyFormat = [{ `<`
75-
$target `,` (`properties` `=` $properties ^ `,`)?
241+
$target `,` (`properties` `=` $properties^ `,`)?
242+
(`kernels` `=` $kernels^ `,`)?
76243
custom<Object>($format, $object)
77244
`>`
78245
}];

mlir/include/mlir/Target/LLVM/ROCDL/Utils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define MLIR_TARGET_LLVM_ROCDL_UTILS_H
1515

1616
#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
17+
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
1718
#include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
1819
#include "mlir/Support/LLVM.h"
1920
#include "mlir/Target/LLVM/ModuleToObject.h"
@@ -107,6 +108,20 @@ class SerializeGPUModuleBase : public LLVM::ModuleToObject {
107108
/// AMD GCN libraries to use when linking, the default is using none.
108109
AMDGCNLibraries deviceLibs = AMDGCNLibraries::None;
109110
};
111+
112+
/// Returns a map containing the `amdhsa.kernels` ELF metadata for each of the
113+
/// kernels in the binary, or `std::nullopt` if the metadata couldn't be
114+
/// retrieved. The map associates the name of the kernel with the list of named
115+
/// attributes found in `amdhsa.kernels`. For more information on the ELF
116+
/// metadata see: https://llvm.org/docs/AMDGPUUsage.html#amdhsa
117+
std::optional<DenseMap<StringAttr, NamedAttrList>>
118+
getAMDHSAKernelsELFMetadata(Builder &builder, ArrayRef<char> elfData);
119+
120+
/// Returns a `#gpu.kernel_table` containing kernel metadata for each of the
121+
/// kernels in `gpuModule`. If `elfData` is valid, then the `amdhsa.kernels` ELF
122+
/// metadata will be added to the `#gpu.kernel_table`.
123+
gpu::KernelTableAttr getKernelMetadata(Operation *gpuModule,
124+
ArrayRef<char> elfData = {});
110125
} // namespace ROCDL
111126
} // namespace mlir
112127

mlir/lib/Bindings/Python/DialectGPU.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,21 @@ PYBIND11_MODULE(_mlirDialectsGPU, m) {
4848
.def_classmethod(
4949
"get",
5050
[](py::object cls, MlirAttribute target, uint32_t format,
51-
py::bytes object, std::optional<MlirAttribute> mlirObjectProps) {
51+
py::bytes object, std::optional<MlirAttribute> mlirObjectProps,
52+
std::optional<MlirAttribute> mlirKernelsAttr) {
5253
py::buffer_info info(py::buffer(object).request());
5354
MlirStringRef objectStrRef =
5455
mlirStringRefCreate(static_cast<char *>(info.ptr), info.size);
55-
return cls(mlirGPUObjectAttrGet(
56+
return cls(mlirGPUObjectAttrGetWithKernels(
5657
mlirAttributeGetContext(target), target, format, objectStrRef,
5758
mlirObjectProps.has_value() ? *mlirObjectProps
59+
: MlirAttribute{nullptr},
60+
mlirKernelsAttr.has_value() ? *mlirKernelsAttr
5861
: MlirAttribute{nullptr}));
5962
},
6063
"cls"_a, "target"_a, "format"_a, "object"_a,
61-
"properties"_a = py::none(), "Gets a gpu.object from parameters.")
64+
"properties"_a = py::none(), "kernels"_a = py::none(),
65+
"Gets a gpu.object from parameters.")
6266
.def_property_readonly(
6367
"target",
6468
[](MlirAttribute self) { return mlirGPUObjectAttrGetTarget(self); })
@@ -71,9 +75,16 @@ PYBIND11_MODULE(_mlirDialectsGPU, m) {
7175
MlirStringRef stringRef = mlirGPUObjectAttrGetObject(self);
7276
return py::bytes(stringRef.data, stringRef.length);
7377
})
74-
.def_property_readonly("properties", [](MlirAttribute self) {
75-
if (mlirGPUObjectAttrHasProperties(self))
76-
return py::cast(mlirGPUObjectAttrGetProperties(self));
78+
.def_property_readonly("properties",
79+
[](MlirAttribute self) {
80+
if (mlirGPUObjectAttrHasProperties(self))
81+
return py::cast(
82+
mlirGPUObjectAttrGetProperties(self));
83+
return py::none().cast<py::object>();
84+
})
85+
.def_property_readonly("kernels", [](MlirAttribute self) {
86+
if (mlirGPUObjectAttrHasKernels(self))
87+
return py::cast(mlirGPUObjectAttrGetKernels(self));
7788
return py::none().cast<py::object>();
7889
});
7990
}

mlir/lib/CAPI/Dialect/GPU.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,28 @@ MlirAttribute mlirGPUObjectAttrGet(MlirContext mlirCtx, MlirAttribute target,
4343
DictionaryAttr objectProps;
4444
if (mlirObjectProps.ptr != nullptr)
4545
objectProps = llvm::cast<DictionaryAttr>(unwrap(mlirObjectProps));
46-
return wrap(gpu::ObjectAttr::get(ctx, unwrap(target),
47-
static_cast<gpu::CompilationTarget>(format),
48-
StringAttr::get(ctx, object), objectProps));
46+
return wrap(gpu::ObjectAttr::get(
47+
ctx, unwrap(target), static_cast<gpu::CompilationTarget>(format),
48+
StringAttr::get(ctx, object), objectProps, nullptr));
49+
}
50+
51+
MlirAttribute mlirGPUObjectAttrGetWithKernels(MlirContext mlirCtx,
52+
MlirAttribute target,
53+
uint32_t format,
54+
MlirStringRef objectStrRef,
55+
MlirAttribute mlirObjectProps,
56+
MlirAttribute mlirKernelsAttr) {
57+
MLIRContext *ctx = unwrap(mlirCtx);
58+
llvm::StringRef object = unwrap(objectStrRef);
59+
DictionaryAttr objectProps;
60+
if (mlirObjectProps.ptr != nullptr)
61+
objectProps = llvm::cast<DictionaryAttr>(unwrap(mlirObjectProps));
62+
gpu::KernelTableAttr kernels;
63+
if (mlirKernelsAttr.ptr != nullptr)
64+
kernels = llvm::cast<gpu::KernelTableAttr>(unwrap(mlirKernelsAttr));
65+
return wrap(gpu::ObjectAttr::get(
66+
ctx, unwrap(target), static_cast<gpu::CompilationTarget>(format),
67+
StringAttr::get(ctx, object), objectProps, kernels));
4968
}
5069

5170
MlirAttribute mlirGPUObjectAttrGetTarget(MlirAttribute mlirObjectAttr) {
@@ -78,3 +97,15 @@ MlirAttribute mlirGPUObjectAttrGetProperties(MlirAttribute mlirObjectAttr) {
7897
llvm::cast<gpu::ObjectAttr>(unwrap(mlirObjectAttr));
7998
return wrap(objectAttr.getProperties());
8099
}
100+
101+
bool mlirGPUObjectAttrHasKernels(MlirAttribute mlirObjectAttr) {
102+
gpu::ObjectAttr objectAttr =
103+
llvm::cast<gpu::ObjectAttr>(unwrap(mlirObjectAttr));
104+
return objectAttr.getKernels() != nullptr;
105+
}
106+
107+
MlirAttribute mlirGPUObjectAttrGetKernels(MlirAttribute mlirObjectAttr) {
108+
gpu::ObjectAttr objectAttr =
109+
llvm::cast<gpu::ObjectAttr>(unwrap(mlirObjectAttr));
110+
return wrap(objectAttr.getKernels());
111+
}

0 commit comments

Comments
 (0)