1111#include " IRModule.h"
1212#include " mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind.
1313#include " mlir-c/Pass.h"
14+ #include " mlir-c/Support.h"
1415#include " mlir/Bindings/Python/Nanobind.h"
1516#include " nanobind/trampoline.h"
1617#include " llvm/Support/ErrorHandling.h"
@@ -32,7 +33,10 @@ namespace {
3233// ```
3334class PyPassBase {
3435public:
35- PyPassBase () : callbacks{} {
36+ PyPassBase (std::string name, std::string argument, std::string description,
37+ std::string opName)
38+ : callbacks{}, name(std::move(name)), argument(std::move(argument)),
39+ description (std::move(description)), opName(std::move(opName)) {
3640 callbacks.construct = [](void *) {};
3741 callbacks.destruct = [](void *) {};
3842 callbacks.run = [](MlirOperation op, MlirExternalPass, void *obj) {
@@ -58,15 +62,25 @@ class PyPassBase {
5862 // Also, `*this` must remain alive as long as the returned object is alive.
5963 MlirPass make () {
6064 return mlirCreateExternalPass (
61- mlirTypeIDCreate (this ),
62- mlirStringRefCreateFromCString ( " python-example-pass " ),
63- mlirStringRefCreateFromCString ( " " ),
64- mlirStringRefCreateFromCString ( " Python Example Pass " ) ,
65- mlirStringRefCreateFromCString ( " " ), 0 , nullptr , callbacks, this );
65+ mlirTypeIDCreate (this ), mlirStringRefCreate (name. data (), name. length ()),
66+ mlirStringRefCreate (argument. data (), argument. length () ),
67+ mlirStringRefCreate (description. data (), description. length () ),
68+ mlirStringRefCreate (opName. data (), opName. size ()), 0 , nullptr ,
69+ callbacks, this );
6670 }
6771
72+ const std::string &getName () const { return name; }
73+ const std::string &getArgument () const { return argument; }
74+ const std::string &getDescription () const { return description; }
75+ const std::string &getOpName () const { return opName; }
76+
6877private:
6978 MlirExternalPassCallbacks callbacks;
79+
80+ std::string name;
81+ std::string argument;
82+ std::string description;
83+ std::string opName;
7084};
7185
7286// A trampoline class upon PyPassBase.
@@ -116,9 +130,19 @@ void mlir::python::populatePassSubmodule(nanobind::module_ &m) {
116130 // Mapping of the Python-defined Pass interface
117131 // ----------------------------------------------------------------------------
118132 nb::class_<PyPassBase, PyPass>(m, " Pass" )
119- .def (nb::init<>(), " Create a new Pass." )
133+ .def (nb::init<std::string, std::string, std::string, std::string>(),
134+ " name" _a, nb::kw_only (), " argument" _a = " " , " description" _a = " " ,
135+ " op_name" _a = " " , " Create a new Pass." )
120136 .def (" run" , &PyPassBase::run, " operation" _a,
121- " Run the pass on the provided operation." );
137+ " Run the pass on the provided operation." )
138+ .def_prop_ro (" name" ,
139+ [](const PyPassBase &self) { return self.getName (); })
140+ .def_prop_ro (" argument" ,
141+ [](const PyPassBase &self) { return self.getArgument (); })
142+ .def_prop_ro (" description" ,
143+ [](const PyPassBase &self) { return self.getDescription (); })
144+ .def_prop_ro (" op_name" ,
145+ [](const PyPassBase &self) { return self.getOpName (); });
122146}
123147
124148// / Create the `mlir.passmanager` here.
0 commit comments