Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 2fd9e83

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 1f1e02f commit 2fd9e83

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
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/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)