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) {
80 "Enable mlir-print-ir-after-all.")
83 [](PyPassManager &passManager,
bool enable) {
86 "enable"_a,
"Enable / disable verify-each.")
97 throw py::value_error(std::string(errorMsg.
join()));
98 return new PyPassManager(passManager);
100 "pipeline"_a,
"context"_a = py::none(),
101 "Parse a textual pass-pipeline and return a top-level PassManager "
102 "that can be applied on a Module. Throw a ValueError if the pipeline "
106 [](PyPassManager &passManager,
const std::string &pipeline) {
113 throw py::value_error(std::string(errorMsg.
join()));
116 "Add textual pipeline elements to the pass manager. Throws a "
117 "ValueError if the pipeline can't be parsed.")
121 bool invalidateOps) {
123 op.getOperation().
getContext()->clearOperationsInside(op);
128 passManager.get(), op.getOperation().get());
130 throw MLIRError(
"Failure while executing pass pipeline",
133 "operation"_a,
"invalidate_ops"_a =
true,
134 "Run the pass manager on the provided operation, raising an "
135 "MLIRError on failure.")
138 [](PyPassManager &
self) {
139 MlirPassManager passManager =
self.get();
144 return printAccum.
join();
146 "Print the textual representation for this PassManager, suitable to "
147 "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)
Enable mlir-print-ir-after-all.
#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.
MLIRContext * getContext()
Return the context this operation is associated with.
Used in function arguments when None should resolve to the current context manager set instance.
ReferrentTy * get() const
Base class for PyOperation and PyOpView which exposes the primary, user visible methods for manipulat...
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.