MLIR  20.0.0git
TransformInterpreter.cpp
Go to the documentation of this file.
1 //===- TransformInterpreter.cpp -------------------------------------------===//
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 // Pybind classes for the transform dialect interpreter.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "mlir-c/IR.h"
15 #include "mlir-c/Support.h"
17 
18 #include <pybind11/detail/common.h>
19 #include <pybind11/pybind11.h>
20 
21 namespace py = pybind11;
22 
23 namespace {
24 struct PyMlirTransformOptions {
25  PyMlirTransformOptions() { options = mlirTransformOptionsCreate(); };
26  PyMlirTransformOptions(PyMlirTransformOptions &&other) {
27  options = other.options;
28  other.options.ptr = nullptr;
29  }
30  PyMlirTransformOptions(const PyMlirTransformOptions &) = delete;
31 
32  ~PyMlirTransformOptions() { mlirTransformOptionsDestroy(options); }
33 
34  MlirTransformOptions options;
35 };
36 } // namespace
37 
38 static void populateTransformInterpreterSubmodule(py::module &m) {
39  py::class_<PyMlirTransformOptions>(m, "TransformOptions", py::module_local())
40  .def(py::init())
41  .def_property(
42  "expensive_checks",
43  [](const PyMlirTransformOptions &self) {
45  },
46  [](PyMlirTransformOptions &self, bool value) {
48  })
49  .def_property(
50  "enforce_single_top_level_transform_op",
51  [](const PyMlirTransformOptions &self) {
53  self.options);
54  },
55  [](PyMlirTransformOptions &self, bool value) {
57  value);
58  });
59 
60  m.def(
61  "apply_named_sequence",
62  [](MlirOperation payloadRoot, MlirOperation transformRoot,
63  MlirOperation transformModule, const PyMlirTransformOptions &options) {
65  mlirOperationGetContext(transformRoot));
66 
67  // Calling back into Python to invalidate everything under the payload
68  // root. This is awkward, but we don't have access to PyMlirContext
69  // object here otherwise.
70  py::object obj = py::cast(payloadRoot);
71  obj.attr("context").attr("_clear_live_operations_inside")(payloadRoot);
72 
74  payloadRoot, transformRoot, transformModule, options.options);
75  if (mlirLogicalResultIsSuccess(result))
76  return;
77 
78  throw py::value_error(
79  "Failed to apply named transform sequence.\nDiagnostic message " +
80  scope.takeMessage());
81  },
82  py::arg("payload_root"), py::arg("transform_root"),
83  py::arg("transform_module"),
84  py::arg("transform_options") = PyMlirTransformOptions());
85 
86  m.def(
87  "copy_symbols_and_merge_into",
88  [](MlirOperation target, MlirOperation other) {
90  mlirOperationGetContext(target));
91 
92  MlirLogicalResult result = mlirMergeSymbolsIntoFromClone(target, other);
93  if (mlirLogicalResultIsFailure(result)) {
94  throw py::value_error(
95  "Failed to merge symbols.\nDiagnostic message " +
96  scope.takeMessage());
97  }
98  },
99  py::arg("target"), py::arg("other"));
100 }
101 
102 PYBIND11_MODULE(_mlirTransformInterpreter, m) {
103  m.doc() = "MLIR Transform dialect interpreter functionality.";
105 }
static void populateTransformInterpreterSubmodule(py::module &m)
PYBIND11_MODULE(_mlirTransformInterpreter, m)
void mlirTransformOptionsEnableExpensiveChecks(MlirTransformOptions transformOptions, bool enable)
Enables or disables expensive checks in transform options.
MlirLogicalResult mlirMergeSymbolsIntoFromClone(MlirOperation target, MlirOperation other)
Merge the symbols from other into target, potentially renaming them to avoid conflicts.
MlirLogicalResult mlirTransformApplyNamedSequence(MlirOperation payload, MlirOperation transformRoot, MlirOperation transformModule, MlirTransformOptions transformOptions)
Applies the transformation script starting at the given transform root operation to the given payload...
void mlirTransformOptionsDestroy(MlirTransformOptions transformOptions)
Destroys a transform options object previously created by mlirTransformOptionsCreate.
bool mlirTransformOptionsGetExpensiveChecksEnabled(MlirTransformOptions transformOptions)
Returns true if expensive checks are enabled in transform options.
void mlirTransformOptionsEnforceSingleTopLevelTransformOp(MlirTransformOptions transformOptions, bool enable)
Enables or disables the enforcement of the top-level transform op being single in transform options.
MlirTransformOptions mlirTransformOptionsCreate()
Creates a default-initialized transform options object.
bool mlirTransformOptionsGetEnforceSingleTopLevelTransformOp(MlirTransformOptions transformOptions)
Returns true if the enforcement of the top-level transform op being single is enabled in transform op...
static llvm::ManagedStatic< PassManagerOptions > options
RAII scope intercepting all diagnostics into a string.
MLIR_CAPI_EXPORTED MlirContext mlirOperationGetContext(MlirOperation op)
Gets the context this operation is associated with.
Definition: IR.cpp:514
static bool mlirLogicalResultIsSuccess(MlirLogicalResult res)
Checks if the given logical result represents a success.
Definition: Support.h:122
static bool mlirLogicalResultIsFailure(MlirLogicalResult res)
Checks if the given logical result represents a failure.
Definition: Support.h:127
A logical result value, essentially a boolean with named states.
Definition: Support.h:116