MLIR  22.0.0git
Pass.cpp
Go to the documentation of this file.
1 //===- Pass.cpp - Pass Management -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Pass.h"
10 
11 #include "IRModule.h"
12 #include "mlir-c/Pass.h"
14 #include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind.
15 
16 namespace nb = nanobind;
17 using namespace nb::literals;
18 using namespace mlir;
19 using namespace mlir::python;
20 
21 namespace {
22 
23 /// Owning Wrapper around a PassManager.
24 class PyPassManager {
25 public:
26  PyPassManager(MlirPassManager passManager) : passManager(passManager) {}
27  PyPassManager(PyPassManager &&other) noexcept
28  : passManager(other.passManager) {
29  other.passManager.ptr = nullptr;
30  }
31  ~PyPassManager() {
32  if (!mlirPassManagerIsNull(passManager))
33  mlirPassManagerDestroy(passManager);
34  }
35  MlirPassManager get() { return passManager; }
36 
37  void release() { passManager.ptr = nullptr; }
38  nb::object getCapsule() {
39  return nb::steal<nb::object>(mlirPythonPassManagerToCapsule(get()));
40  }
41 
42  static nb::object createFromCapsule(const nb::object &capsule) {
43  MlirPassManager rawPm = mlirPythonCapsuleToPassManager(capsule.ptr());
44  if (mlirPassManagerIsNull(rawPm))
45  throw nb::python_error();
46  return nb::cast(PyPassManager(rawPm), nb::rv_policy::move);
47  }
48 
49 private:
50  MlirPassManager passManager;
51 };
52 
53 } // namespace
54 
55 /// Create the `mlir.passmanager` here.
56 void mlir::python::populatePassManagerSubmodule(nb::module_ &m) {
57  //----------------------------------------------------------------------------
58  // Mapping of the top-level PassManager
59  //----------------------------------------------------------------------------
60  nb::class_<PyPassManager>(m, "PassManager")
61  .def(
62  "__init__",
63  [](PyPassManager &self, const std::string &anchorOp,
64  DefaultingPyMlirContext context) {
65  MlirPassManager passManager = mlirPassManagerCreateOnOperation(
66  context->get(),
67  mlirStringRefCreate(anchorOp.data(), anchorOp.size()));
68  new (&self) PyPassManager(passManager);
69  },
70  "anchor_op"_a = nb::str("any"), "context"_a.none() = nb::none(),
71  "Create a new PassManager for the current (or provided) Context.")
72  .def_prop_ro(MLIR_PYTHON_CAPI_PTR_ATTR, &PyPassManager::getCapsule)
73  .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyPassManager::createFromCapsule)
74  .def("_testing_release", &PyPassManager::release,
75  "Releases (leaks) the backing pass manager (testing)")
76  .def(
77  "enable_ir_printing",
78  [](PyPassManager &passManager, bool printBeforeAll,
79  bool printAfterAll, bool printModuleScope, bool printAfterChange,
80  bool printAfterFailure, std::optional<int64_t> largeElementsLimit,
81  std::optional<int64_t> largeResourceLimit, bool enableDebugInfo,
82  bool printGenericOpForm,
83  std::optional<std::string> optionalTreePrintingPath) {
84  MlirOpPrintingFlags flags = mlirOpPrintingFlagsCreate();
85  if (largeElementsLimit) {
87  *largeElementsLimit);
89  *largeElementsLimit);
90  }
91  if (largeResourceLimit)
93  *largeResourceLimit);
94  if (enableDebugInfo)
95  mlirOpPrintingFlagsEnableDebugInfo(flags, /*enable=*/true,
96  /*prettyForm=*/false);
97  if (printGenericOpForm)
99  std::string treePrintingPath = "";
100  if (optionalTreePrintingPath.has_value())
101  treePrintingPath = optionalTreePrintingPath.value();
103  passManager.get(), printBeforeAll, printAfterAll,
104  printModuleScope, printAfterChange, printAfterFailure, flags,
105  mlirStringRefCreate(treePrintingPath.data(),
106  treePrintingPath.size()));
108  },
109  "print_before_all"_a = false, "print_after_all"_a = true,
110  "print_module_scope"_a = false, "print_after_change"_a = false,
111  "print_after_failure"_a = false,
112  "large_elements_limit"_a.none() = nb::none(),
113  "large_resource_limit"_a.none() = nb::none(),
114  "enable_debug_info"_a = false, "print_generic_op_form"_a = false,
115  "tree_printing_dir_path"_a.none() = nb::none(),
116  "Enable IR printing, default as mlir-print-ir-after-all.")
117  .def(
118  "enable_verifier",
119  [](PyPassManager &passManager, bool enable) {
120  mlirPassManagerEnableVerifier(passManager.get(), enable);
121  },
122  "enable"_a, "Enable / disable verify-each.")
123  .def(
124  "enable_timing",
125  [](PyPassManager &passManager) {
126  mlirPassManagerEnableTiming(passManager.get());
127  },
128  "Enable pass timing.")
129  .def_static(
130  "parse",
131  [](const std::string &pipeline, DefaultingPyMlirContext context) {
132  MlirPassManager passManager = mlirPassManagerCreate(context->get());
133  PyPrintAccumulator errorMsg;
136  mlirStringRefCreate(pipeline.data(), pipeline.size()),
137  errorMsg.getCallback(), errorMsg.getUserData());
138  if (mlirLogicalResultIsFailure(status))
139  throw nb::value_error(errorMsg.join().c_str());
140  return new PyPassManager(passManager);
141  },
142  "pipeline"_a, "context"_a.none() = nb::none(),
143  "Parse a textual pass-pipeline and return a top-level PassManager "
144  "that can be applied on a Module. Throw a ValueError if the pipeline "
145  "can't be parsed")
146  .def(
147  "add",
148  [](PyPassManager &passManager, const std::string &pipeline) {
149  PyPrintAccumulator errorMsg;
151  mlirPassManagerGetAsOpPassManager(passManager.get()),
152  mlirStringRefCreate(pipeline.data(), pipeline.size()),
153  errorMsg.getCallback(), errorMsg.getUserData());
154  if (mlirLogicalResultIsFailure(status))
155  throw nb::value_error(errorMsg.join().c_str());
156  },
157  "pipeline"_a,
158  "Add textual pipeline elements to the pass manager. Throws a "
159  "ValueError if the pipeline can't be parsed.")
160  .def(
161  "run",
162  [](PyPassManager &passManager, PyOperationBase &op) {
163  // Actually run the pass manager.
166  passManager.get(), op.getOperation().get());
167  if (mlirLogicalResultIsFailure(status))
168  throw MLIRError("Failure while executing pass pipeline",
169  errors.take());
170  },
171  "operation"_a,
172  "Run the pass manager on the provided operation, raising an "
173  "MLIRError on failure.")
174  .def(
175  "__str__",
176  [](PyPassManager &self) {
177  MlirPassManager passManager = self.get();
178  PyPrintAccumulator printAccum;
181  printAccum.getCallback(), printAccum.getUserData());
182  return printAccum.join();
183  },
184  "Print the textual representation for this PassManager, suitable to "
185  "be passed to `parse` for round-tripping.");
186 }
MlirPassManager mlirPassManagerCreate(MlirContext ctx)
Create a new top-level PassManager with the default anchor.
Definition: Pass.cpp:24
void mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable)
Enable / disable verify-each.
Definition: Pass.cpp:74
void mlirPassManagerEnableTiming(MlirPassManager passManager)
Enable pass timing.
Definition: Pass.cpp:78
void mlirPassManagerDestroy(MlirPassManager passManager)
Destroy the provided PassManager.
Definition: Pass.cpp:33
MlirLogicalResult mlirParsePassPipeline(MlirOpPassManager passManager, MlirStringRef pipeline, MlirStringCallback callback, void *userData)
Parse a textual MLIR pass pipeline and assign it to the provided OpPassManager.
Definition: Pass.cpp:116
MlirOpPassManager mlirPassManagerGetAsOpPassManager(MlirPassManager passManager)
Cast a top-level PassManager to a generic OpPassManager.
Definition: Pass.cpp:38
MlirLogicalResult mlirPassManagerRunOnOp(MlirPassManager passManager, MlirOperation op)
Run the provided passManager on the given op.
Definition: Pass.cpp:42
void mlirPrintPassPipeline(MlirOpPassManager passManager, MlirStringCallback callback, void *userData)
Print a textual MLIR pass pipeline by sending chunks of the string representation and forwarding user...
Definition: Pass.cpp:110
MlirPassManager mlirPassManagerCreateOnOperation(MlirContext ctx, MlirStringRef anchorOp)
Create a new top-level PassManager anchored on anchorOp.
Definition: Pass.cpp:28
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.
Definition: Pass.cpp:101
void mlirPassManagerEnableIRPrinting(MlirPassManager passManager, bool printBeforeAll, bool printAfterAll, bool printModuleScope, bool printAfterOnlyOnChange, bool printAfterOnlyOnFailure, MlirOpPrintingFlags flags, MlirStringRef treePrintingPath)
Enable IR printing.
Definition: Pass.cpp:47
#define MLIR_PYTHON_CAPI_PTR_ATTR
Attribute on MLIR Python objects that expose their C-API pointer.
Definition: Interop.h:97
#define MLIR_PYTHON_CAPI_FACTORY_ATTR
Attribute on MLIR Python objects that exposes a factory function for constructing the corresponding P...
Definition: Interop.h:110
static PyObject * mlirPythonPassManagerToCapsule(MlirPassManager pm)
Creates a capsule object encapsulating the raw C-API MlirPassManager.
Definition: Interop.h:311
static MlirPassManager mlirPythonCapsuleToPassManager(PyObject *capsule)
Extracts an MlirPassManager from a capsule as produced from mlirPythonPassManagerToCapsule.
Definition: Interop.h:320
PyMlirContextRef & getContext()
Accesses the context reference.
Definition: IRModule.h:276
Used in function arguments when None should resolve to the current context manager set instance.
Definition: IRModule.h:257
ReferrentTy * get() const
Definition: NanobindUtils.h:60
Base class for PyOperation and PyOpView which exposes the primary, user visible methods for manipulat...
Definition: IRModule.h:536
virtual PyOperation & getOperation()=0
Each must provide access to the raw Operation.
MlirOperation get() const
Definition: IRModule.h:621
static bool mlirPassManagerIsNull(MlirPassManager passManager)
Checks if a PassManager is null.
Definition: Pass.h:65
MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsElideLargeResourceString(MlirOpPrintingFlags flags, intptr_t largeResourceLimit)
Enables the elision of large resources strings by omitting them from the dialect_resources section.
Definition: IR.cpp:215
MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags)
Always print operations in the generic form.
Definition: IR.cpp:225
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...
Definition: IR.cpp:210
MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags)
Destroys printing flags created with mlirOpPrintingFlagsCreate.
Definition: IR.cpp:206
MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable, bool prettyForm)
Enable or disable printing of debug information (based on enable).
Definition: IR.cpp:220
MLIR_CAPI_EXPORTED MlirOpPrintingFlags mlirOpPrintingFlagsCreate(void)
Creates new printing flags with defaults, intended for customization.
Definition: IR.cpp:202
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Definition: Support.h:82
static bool mlirLogicalResultIsFailure(MlirLogicalResult res)
Checks if the given logical result represents a failure.
Definition: Support.h:127
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.
Definition: Support.h:116
Accumulates into a python string from a method that accepts an MlirStringCallback.
MlirStringCallback getCallback()
Custom exception that allows access to error diagnostic information.
Definition: IRModule.h:1273
RAII object that captures any error diagnostics emitted to the provided context.
Definition: IRModule.h:392