17 using namespace nb::literals;
26 PyPassManager(MlirPassManager passManager) : passManager(passManager) {}
27 PyPassManager(PyPassManager &&other) noexcept
28 : passManager(other.passManager) {
29 other.passManager.ptr =
nullptr;
35 MlirPassManager
get() {
return passManager; }
37 void release() { passManager.ptr =
nullptr; }
38 nb::object getCapsule() {
42 static nb::object createFromCapsule(nb::object capsule) {
45 throw nb::python_error();
46 return nb::cast(PyPassManager(rawPm), nb::rv_policy::move);
50 MlirPassManager passManager;
60 nb::class_<PyPassManager>(m,
"PassManager")
63 [](PyPassManager &
self,
const std::string &anchorOp,
68 new (&
self) PyPassManager(passManager);
70 "anchor_op"_a = nb::str(
"any"),
"context"_a.none() = nb::none(),
71 "Create a new PassManager for the current (or provided) Context.")
74 .def(
"_testing_release", &PyPassManager::release,
75 "Releases (leaks) the backing pass manager (testing)")
78 [](PyPassManager &passManager,
bool printBeforeAll,
79 bool printAfterAll,
bool printModuleScope,
bool printAfterChange,
80 bool printAfterFailure, std::optional<int64_t> largeElementsLimit,
81 bool enableDebugInfo,
bool printGenericOpForm,
82 std::optional<std::string> optionalTreePrintingPath) {
84 if (largeElementsLimit)
90 if (printGenericOpForm)
92 std::string treePrintingPath =
"";
93 if (optionalTreePrintingPath.has_value())
94 treePrintingPath = optionalTreePrintingPath.value();
96 passManager.get(), printBeforeAll, printAfterAll,
97 printModuleScope, printAfterChange, printAfterFailure, flags,
99 treePrintingPath.size()));
102 "print_before_all"_a =
false,
"print_after_all"_a =
true,
103 "print_module_scope"_a =
false,
"print_after_change"_a =
false,
104 "print_after_failure"_a =
false,
105 "large_elements_limit"_a.none() = nb::none(),
106 "enable_debug_info"_a =
false,
"print_generic_op_form"_a =
false,
107 "tree_printing_dir_path"_a.none() = nb::none(),
108 "Enable IR printing, default as mlir-print-ir-after-all.")
111 [](PyPassManager &passManager,
bool enable) {
114 "enable"_a,
"Enable / disable verify-each.")
125 throw nb::value_error(errorMsg.
join().c_str());
126 return new PyPassManager(passManager);
128 "pipeline"_a,
"context"_a.none() = nb::none(),
129 "Parse a textual pass-pipeline and return a top-level PassManager "
130 "that can be applied on a Module. Throw a ValueError if the pipeline "
134 [](PyPassManager &passManager,
const std::string &pipeline) {
141 throw nb::value_error(errorMsg.
join().c_str());
144 "Add textual pipeline elements to the pass manager. Throws a "
145 "ValueError if the pipeline can't be parsed.")
149 bool invalidateOps) {
158 throw MLIRError(
"Failure while executing pass pipeline",
161 "operation"_a,
"invalidate_ops"_a =
true,
162 "Run the pass manager on the provided operation, raising an "
163 "MLIRError on failure.")
166 [](PyPassManager &
self) {
167 MlirPassManager passManager =
self.get();
172 return printAccum.
join();
174 "Print the textual representation for this PassManager, suitable to "
175 "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, MlirOpPrintingFlags flags, MlirStringRef treePrintingPath)
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.
MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags)
Always print operations in the generic form.
MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags, intptr_t largeElementLimit)
Enables the elision of large elements attributes by printing a lexically valid but otherwise meaningl...
MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags)
Destroys printing flags created with mlirOpPrintingFlagsCreate.
MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable, bool prettyForm)
Enable or disable printing of debug information (based on enable).
MLIR_CAPI_EXPORTED MlirOpPrintingFlags mlirOpPrintingFlagsCreate(void)
Creates new printing flags with defaults, intended for customization.
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(nanobind::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.