MLIR 23.0.0git
DialectGPU.cpp
Go to the documentation of this file.
1//===- DialectGPU.cpp - Pybind module for the GPU passes ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===---------------------------------------------------------------------===//
8
10#include "mlir-c/IR.h"
11#include "mlir-c/Support.h"
16
17namespace nb = nanobind;
18using namespace nanobind::literals;
20
21namespace mlir {
22namespace python {
24namespace gpu {
25// -----------------------------------------------------------------------------
26// AsyncTokenType
27// -----------------------------------------------------------------------------
28
29struct AsyncTokenType : PyConcreteType<AsyncTokenType> {
31 static constexpr const char *pyClassName = "AsyncTokenType";
33 using Base::Base;
34
35 static void bindDerived(ClassTy &c) {
36 c.def_static(
37 "get",
38 [](DefaultingPyMlirContext context) {
39 return AsyncTokenType(context->getRef(),
40 mlirGPUAsyncTokenTypeGet(context.get()->get()));
41 },
42 "Gets an instance of AsyncTokenType in the same context",
43 nb::arg("context").none() = nb::none());
44 }
45};
46
47//===-------------------------------------------------------------------===//
48// ObjectAttr
49//===-------------------------------------------------------------------===//
50
51struct ObjectAttr : PyConcreteAttribute<ObjectAttr> {
53 static constexpr const char *pyClassName = "ObjectAttr";
55 using Base::Base;
56
57 static void bindDerived(ClassTy &c) {
58 c.def_static(
59 "get",
60 [](const PyAttribute &target, uint32_t format, const nb::bytes &object,
61 std::optional<PyDictAttribute> mlirObjectProps,
62 std::optional<PyAttribute> mlirKernelsAttr,
65 static_cast<char *>(const_cast<void *>(object.data())),
66 object.size());
67 return ObjectAttr(
68 context->getRef(),
70 mlirAttributeGetContext(target), target, format, objectStrRef,
71 mlirObjectProps.has_value() ? *mlirObjectProps
72 : MlirAttribute{nullptr},
73 mlirKernelsAttr.has_value() ? *mlirKernelsAttr
74 : MlirAttribute{nullptr}));
75 },
76 "target"_a, "format"_a, "object"_a, "properties"_a = nb::none(),
77 "kernels"_a = nb::none(), "context"_a = nb::none(),
78 "Gets a gpu.object from parameters.");
79
80 c.def_prop_ro("target", [](ObjectAttr &self) {
82 .maybeDownCast();
83 });
84 c.def_prop_ro("format", [](const ObjectAttr &self) {
85 return mlirGPUObjectAttrGetFormat(self);
86 });
87 c.def_prop_ro("object", [](const ObjectAttr &self) {
89 return nb::bytes(stringRef.data, stringRef.length);
90 });
91 c.def_prop_ro(
92 "properties", [](ObjectAttr &self) -> std::optional<PyDictAttribute> {
94 return PyDictAttribute(self.getContext(),
96 return std::nullopt;
97 });
98 c.def_prop_ro("kernels",
99 [](ObjectAttr &self)
100 -> std::optional<nb::typed<nb::object, PyAttribute>> {
102 return PyAttribute(self.getContext(),
104 .maybeDownCast();
105 return std::nullopt;
106 });
107 }
108};
109} // namespace gpu
110} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
111} // namespace python
112} // namespace mlir
113
114// -----------------------------------------------------------------------------
115// Module initialization.
116// -----------------------------------------------------------------------------
117
NB_MODULE(_mlirDialectsGPU, m)
MLIR_CAPI_EXPORTED bool mlirAttributeIsAGPUObjectAttr(MlirAttribute attr)
Definition GPU.cpp:38
MLIR_CAPI_EXPORTED MlirAttribute mlirGPUObjectAttrGetTarget(MlirAttribute mlirObjectAttr)
Definition GPU.cpp:78
MLIR_CAPI_EXPORTED MlirAttribute mlirGPUObjectAttrGetProperties(MlirAttribute mlirObjectAttr)
Definition GPU.cpp:103
MLIR_CAPI_EXPORTED bool mlirGPUObjectAttrHasProperties(MlirAttribute mlirObjectAttr)
Definition GPU.cpp:97
MLIR_CAPI_EXPORTED MlirAttribute mlirGPUObjectAttrGetKernels(MlirAttribute mlirObjectAttr)
Definition GPU.cpp:115
MLIR_CAPI_EXPORTED MlirAttribute mlirGPUObjectAttrGetWithKernels(MlirContext mlirCtx, MlirAttribute target, uint32_t format, MlirStringRef objectStrRef, MlirAttribute mlirObjectProps, MlirAttribute mlirKernelsAttr)
Definition GPU.cpp:59
MLIR_CAPI_EXPORTED bool mlirGPUObjectAttrHasKernels(MlirAttribute mlirObjectAttr)
Definition GPU.cpp:109
MLIR_CAPI_EXPORTED MlirType mlirGPUAsyncTokenTypeGet(MlirContext ctx)
Definition GPU.cpp:26
MLIR_CAPI_EXPORTED MlirStringRef mlirGPUObjectAttrGetName(void)
Definition GPU.cpp:55
MLIR_CAPI_EXPORTED MlirStringRef mlirGPUAsyncTokenTypeGetName(void)
Definition GPU.cpp:30
MLIR_CAPI_EXPORTED bool mlirTypeIsAGPUAsyncTokenType(MlirType type)
Definition GPU.cpp:22
MLIR_CAPI_EXPORTED uint32_t mlirGPUObjectAttrGetFormat(MlirAttribute mlirObjectAttr)
Definition GPU.cpp:84
MLIR_CAPI_EXPORTED MlirStringRef mlirGPUObjectAttrGetObject(MlirAttribute mlirObjectAttr)
Definition GPU.cpp:90
MlirContext mlirAttributeGetContext(MlirAttribute attribute)
Definition IR.cpp:1281
ReferrentTy * get() const
PyMlirContextRef & getContext()
Accesses the context reference.
Definition IRCore.h:299
Used in function arguments when None should resolve to the current context manager set instance.
Definition IRCore.h:280
PyAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
Definition IRCore.h:1011
static void bind(nanobind::module_ &m, PyType_Slot *slots=nullptr)
Definition IRCore.h:1092
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Definition Support.h:84
Include the generated interface declarations.
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:75
const char * data
Pointer to the first symbol.
Definition Support.h:76
size_t length
Length of the fragment.
Definition Support.h:77