20using namespace nb::literals;
31 PyPassManager(MlirPassManager passManager) : passManager(passManager) {}
33 : passManager(other.passManager) {
34 other.passManager.ptr =
nullptr;
40 MlirPassManager
get() {
return passManager; }
42 void release() { passManager.ptr =
nullptr; }
50 throw nb::python_error();
55 MlirPassManager passManager;
70 nb::enum_<PyMlirPassDisplayMode>(m,
"PassDisplayMode")
77 nb::class_<PyMlirExternalPass>(m,
"ExternalPass")
85 nb::class_<PyPassManager>(m,
"PassManager")
95 "anchor_op"_a = nb::str(
"any"),
"context"_a = nb::none(),
97 nb::sig(
"def __init__(self, anchor_op: str = 'any', context: " MAKE_MLIR_PYTHON_QUALNAME(
"ir.Context")
" | None = None) -> None"),
99 "Create a new PassManager for the current (or provided) Context.")
103 "Releases (leaks) the backing pass manager (testing)")
105 "enable_ir_printing",
107 bool printAfterAll,
bool printModuleScope,
bool printAfterChange,
108 bool printAfterFailure, std::optional<int64_t> largeElementsLimit,
109 std::optional<int64_t> largeResourceLimit,
bool enableDebugInfo,
110 bool printGenericOpForm,
111 std::optional<std::string> optionalTreePrintingPath) {
113 if (largeElementsLimit) {
115 *largeElementsLimit);
117 *largeElementsLimit);
119 if (largeResourceLimit)
121 *largeResourceLimit);
125 if (printGenericOpForm)
127 std::string treePrintingPath =
"";
128 if (optionalTreePrintingPath.has_value())
129 treePrintingPath = optionalTreePrintingPath.value();
131 passManager.
get(), printBeforeAll, printAfterAll,
132 printModuleScope, printAfterChange, printAfterFailure, flags,
134 treePrintingPath.size()));
137 "print_before_all"_a =
false,
"print_after_all"_a =
true,
138 "print_module_scope"_a =
false,
"print_after_change"_a =
false,
139 "print_after_failure"_a =
false,
140 "large_elements_limit"_a = nb::none(),
141 "large_resource_limit"_a = nb::none(),
"enable_debug_info"_a =
false,
142 "print_generic_op_form"_a =
false,
143 "tree_printing_dir_path"_a = nb::none(),
144 "Enable IR printing, default as mlir-print-ir-after-all.")
150 "enable"_a,
"Enable / disable verify-each.")
156 "Enable pass timing.")
165 "Enable pass statistics.")
176 throw nb::value_error(errorMsg.
join().c_str());
179 "pipeline"_a,
"context"_a = nb::none(),
183 "Parse a textual pass-pipeline and return a top-level PassManager "
184 "that can be applied on a Module. Throw a ValueError if the pipeline "
188 [](
PyPassManager &passManager,
const std::string &pipeline) {
195 throw nb::value_error(errorMsg.
join().c_str());
198 "Add textual pipeline elements to the pass manager. Throws a "
199 "ValueError if the pipeline can't be parsed.")
203 std::optional<std::string> &name,
const std::string &argument,
204 const std::string &description,
const std::string &opName) {
205 if (!name.has_value()) {
206 name = nb::cast<std::string>(
207 nb::borrow<nb::str>(run.attr(
"__name__")));
210 MlirExternalPassCallbacks callbacks;
211 callbacks.construct = [](
void *obj) {
212 (
void)nb::handle(
static_cast<PyObject *
>(obj)).inc_ref();
214 callbacks.destruct = [](
void *obj) {
215 (
void)nb::handle(
static_cast<PyObject *
>(obj)).dec_ref();
217 callbacks.initialize =
nullptr;
218 callbacks.clone = [](
void *) ->
void * {
219 throw std::runtime_error(
"Cloning Python passes not supported");
223 nb::handle(
static_cast<PyObject *
>(userData))(
232 callbacks, run.ptr());
235 "run"_a,
"name"_a.none() = nb::none(),
"argument"_a.none() =
"",
236 "description"_a.none() =
"",
"op_name"_a.none() =
"",
238 Add a python-defined pass to the current pipeline of the pass manager.
241 run: A callable with signature ``(op: ir.Operation, pass_: ExternalPass) -> None``.
242 Called when the pass executes. It receives the operation to be processed and
243 the current ``ExternalPass`` instance.
244 Use ``pass_.signal_pass_failure()`` to signal failure.
245 name: The name of the pass. Defaults to ``run.__name__``.
246 argument: The command-line argument for the pass. Defaults to empty.
247 description: The description of the pass. Defaults to empty.
248 op_name: The name of the operation this pass operates on.
249 It will be a generic operation pass if not specified.)")
258 throw MLIRError(
"Failure while executing pass pipeline",
265 "Run the pass manager on the provided operation, raising an "
266 "MLIRError on failure.")
270 MlirPassManager passManager = self.
get();
275 return printAccum.
join();
277 "Print the textual representation for this PassManager, suitable to "
278 "be passed to `parse` for round-tripping.");
MlirPass mlirCreateExternalPass(MlirTypeID passID, MlirStringRef name, MlirStringRef argument, MlirStringRef description, MlirStringRef opName, intptr_t nDependentDialects, MlirDialectHandle *dependentDialects, MlirExternalPassCallbacks callbacks, void *userData)
MlirLogicalResult mlirParsePassPipeline(MlirOpPassManager passManager, MlirStringRef pipeline, MlirStringCallback callback, void *userData)
void mlirPrintPassPipeline(MlirOpPassManager passManager, MlirStringCallback callback, void *userData)
MlirOpPrintingFlags mlirOpPrintingFlagsCreate()
void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags, intptr_t largeElementLimit)
void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable, bool prettyForm)
void mlirOpPrintingFlagsElideLargeResourceString(MlirOpPrintingFlags flags, intptr_t largeResourceLimit)
void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags)
void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags)
#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...
#define MAKE_MLIR_PYTHON_QUALNAME(local)
static MlirPassManager mlirPythonCapsuleToPassManager(PyObject *capsule)
Extracts an MlirPassManager from a capsule as produced from mlirPythonPassManagerToCapsule.
static PyObject * mlirPythonPassManagerToCapsule(MlirPassManager pm)
Creates a capsule object encapsulating the raw C-API MlirPassManager.
ReferrentTy * get() const
PyMlirContextRef & getContext()
Accesses the context reference.
Used in function arguments when None should resolve to the current context manager set instance.
MlirTypeID allocateTypeID()
static PyGlobals & get()
Most code should get the globals via this static accessor.
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
Owning Wrapper around a PassManager.
PyPassManager(MlirPassManager passManager)
PyPassManager(PyPassManager &&other) noexcept
static nb::object createFromCapsule(const nb::object &capsule)
MLIR_CAPI_EXPORTED void mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable)
Enable / disable verify-each.
MLIR_CAPI_EXPORTED void mlirExternalPassSignalFailure(MlirExternalPass pass)
Print a textual MLIR pass pipeline by sending chunks of the string representation and forwarding user...
MLIR_CAPI_EXPORTED void mlirPassManagerEnableStatistics(MlirPassManager passManager, MlirPassDisplayMode displayMode)
Enable pass statistics.
MLIR_CAPI_EXPORTED MlirOpPassManager mlirPassManagerGetAsOpPassManager(MlirPassManager passManager)
Cast a top-level PassManager to a generic OpPassManager.
MLIR_CAPI_EXPORTED MlirLogicalResult mlirPassManagerRunOnOp(MlirPassManager passManager, MlirOperation op)
Run the provided passManager on the given op.
MLIR_CAPI_EXPORTED void mlirPassManagerEnableTiming(MlirPassManager passManager)
Enable pass timing.
MLIR_CAPI_EXPORTED MlirPassManager mlirPassManagerCreate(MlirContext ctx)
Create a new top-level PassManager with the default anchor.
MLIR_CAPI_EXPORTED void mlirPassManagerEnableIRPrinting(MlirPassManager passManager, bool printBeforeAll, bool printAfterAll, bool printModuleScope, bool printAfterOnlyOnChange, bool printAfterOnlyOnFailure, MlirOpPrintingFlags flags, MlirStringRef treePrintingPath)
Enable IR printing.
MLIR_CAPI_EXPORTED 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.
MlirPassDisplayMode
Enumerated type of pass display modes.
MLIR_CAPI_EXPORTED void mlirPassManagerAddOwnedPass(MlirPassManager passManager, MlirPass pass)
Add a pass and transfer ownership to the provided top-level mlirPassManager.
static bool mlirPassManagerIsNull(MlirPassManager passManager)
Checks if a PassManager is null.
MLIR_CAPI_EXPORTED MlirPassManager mlirPassManagerCreateOnOperation(MlirContext ctx, MlirStringRef anchorOp)
Create a new top-level PassManager anchored on anchorOp.
MLIR_CAPI_EXPORTED void mlirPassManagerDestroy(MlirPassManager passManager)
Destroy the provided PassManager.
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.
@ MLIR_PASS_DISPLAY_MODE_PIPELINE
@ MLIR_PASS_DISPLAY_MODE_LIST
void populatePassManagerSubmodule(nb::module_ &m)
Create the mlir.passmanager here.
Include the generated interface declarations.
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.
std::vector< PyDiagnostic::DiagnosticInfo > take()