16 using namespace py::literals;
25 PyPassManager(MlirPassManager passManager) : passManager(passManager) {}
26 PyPassManager(PyPassManager &&other) noexcept
27 : passManager(other.passManager) {
28 other.passManager.ptr =
nullptr;
34 MlirPassManager
get() {
return passManager; }
36 void release() { passManager.ptr =
nullptr; }
37 pybind11::object getCapsule() {
38 return py::reinterpret_steal<py::object>(
42 static pybind11::object createFromCapsule(pybind11::object capsule) {
45 throw py::error_already_set();
46 return py::cast(PyPassManager(rawPm), py::return_value_policy::move);
50 MlirPassManager passManager;
60 py::class_<PyPassManager>(m,
"PassManager", py::module_local())
61 .def(py::init<>([](
const std::string &anchorOp,
66 return new PyPassManager(passManager);
68 "anchor_op"_a = py::str(
"any"),
"context"_a = py::none(),
69 "Create a new PassManager for the current (or provided) Context.")
71 &PyPassManager::getCapsule)
73 .def(
"_testing_release", &PyPassManager::release,
74 "Releases (leaks) the backing pass manager (testing)")
77 [](PyPassManager &passManager,
bool printBeforeAll,
78 bool printAfterAll,
bool printModuleScope,
bool printAfterChange,
79 bool printAfterFailure) {
81 passManager.get(), printBeforeAll, printAfterAll,
82 printModuleScope, printAfterChange, printAfterFailure);
84 "print_before_all"_a =
false,
"print_after_all"_a =
true,
85 "print_module_scope"_a =
false,
"print_after_change"_a =
false,
86 "print_after_failure"_a =
false,
87 "Enable IR printing, default as mlir-print-ir-after-all.")
90 [](PyPassManager &passManager,
bool enable) {
93 "enable"_a,
"Enable / disable verify-each.")
104 throw py::value_error(std::string(errorMsg.
join()));
105 return new PyPassManager(passManager);
107 "pipeline"_a,
"context"_a = py::none(),
108 "Parse a textual pass-pipeline and return a top-level PassManager "
109 "that can be applied on a Module. Throw a ValueError if the pipeline "
113 [](PyPassManager &passManager,
const std::string &pipeline) {
120 throw py::value_error(std::string(errorMsg.
join()));
123 "Add textual pipeline elements to the pass manager. Throws a "
124 "ValueError if the pipeline can't be parsed.")
128 bool invalidateOps) {
137 throw MLIRError(
"Failure while executing pass pipeline",
140 "operation"_a,
"invalidate_ops"_a =
true,
141 "Run the pass manager on the provided operation, raising an "
142 "MLIRError on failure.")
145 [](PyPassManager &
self) {
146 MlirPassManager passManager =
self.get();
151 return printAccum.
join();
153 "Print the textual representation for this PassManager, suitable to "
154 "be passed to `parse` for round-tripping.");
MlirPassManager mlirPassManagerCreate(MlirContext ctx)
Create a new top-level PassManager with the default anchor.
void mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable)
Enable / disable verify-each.
void mlirPassManagerDestroy(MlirPassManager passManager)
Destroy the provided PassManager.
MlirLogicalResult mlirParsePassPipeline(MlirOpPassManager passManager, MlirStringRef pipeline, MlirStringCallback callback, void *userData)
Parse a textual MLIR pass pipeline and assign it to the provided OpPassManager.
MlirOpPassManager mlirPassManagerGetAsOpPassManager(MlirPassManager passManager)
Cast a top-level PassManager to a generic OpPassManager.
MlirLogicalResult mlirPassManagerRunOnOp(MlirPassManager passManager, MlirOperation op)
Run the provided passManager on the given op.
void mlirPrintPassPipeline(MlirOpPassManager passManager, MlirStringCallback callback, void *userData)
Print a textual MLIR pass pipeline by sending chunks of the string representation and forwarding user...
MlirPassManager mlirPassManagerCreateOnOperation(MlirContext ctx, MlirStringRef anchorOp)
Create a new top-level PassManager anchored on anchorOp.
MlirLogicalResult mlirOpPassManagerAddPipeline(MlirOpPassManager passManager, MlirStringRef pipelineElements, MlirStringCallback callback, void *userData)
Parse a sequence of textual MLIR pass pipeline elements and add them to the provided OpPassManager.
void mlirPassManagerEnableIRPrinting(MlirPassManager passManager, bool printBeforeAll, bool printAfterAll, bool printModuleScope, bool printAfterOnlyOnChange, bool printAfterOnlyOnFailure)
Enable IR printing.
#define MLIR_PYTHON_CAPI_PTR_ATTR
Attribute on MLIR Python objects that expose their C-API pointer.
#define MLIR_PYTHON_CAPI_FACTORY_ATTR
Attribute on MLIR Python objects that exposes a factory function for constructing the corresponding P...
static PyObject * mlirPythonPassManagerToCapsule(MlirPassManager pm)
Creates a capsule object encapsulating the raw C-API MlirPassManager.
static MlirPassManager mlirPythonCapsuleToPassManager(PyObject *capsule)
Extracts an MlirPassManager from a capsule as produced from mlirPythonPassManagerToCapsule.
PyMlirContextRef & getContext()
Accesses the context reference.
Used in function arguments when None should resolve to the current context manager set instance.
ReferrentTy * get() const
void clearOperationsInside(PyOperationBase &op)
Clears all operations nested inside the given op using clearOperation(MlirOperation).
Base class for PyOperation and PyOpView which exposes the primary, user visible methods for manipulat...
virtual PyOperation & getOperation()=0
Each must provide access to the raw Operation.
MlirOperation get() const
static bool mlirPassManagerIsNull(MlirPassManager passManager)
Checks if a PassManager is null.
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
static bool mlirLogicalResultIsFailure(MlirLogicalResult res)
Checks if the given logical result represents a failure.
void populatePassManagerSubmodule(pybind11::module &m)
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
A logical result value, essentially a boolean with named states.
Accumulates into a python string from a method that accepts an MlirStringCallback.
MlirStringCallback getCallback()
Custom exception that allows access to error diagnostic information.
RAII object that captures any error diagnostics emitted to the provided context.