MLIR 22.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"
19
20namespace nb = nanobind;
21
22namespace {
23struct PyMlirTransformOptions {
24 PyMlirTransformOptions() { options = mlirTransformOptionsCreate(); };
25 PyMlirTransformOptions(PyMlirTransformOptions &&other) {
26 options = other.options;
27 other.options.ptr = nullptr;
28 }
29 PyMlirTransformOptions(const PyMlirTransformOptions &) = delete;
30
31 ~PyMlirTransformOptions() { mlirTransformOptionsDestroy(options); }
32
33 MlirTransformOptions options;
34};
35} // namespace
36
37static void populateTransformInterpreterSubmodule(nb::module_ &m) {
38 nb::class_<PyMlirTransformOptions>(m, "TransformOptions")
39 .def(nb::init<>())
40 .def_prop_rw(
41 "expensive_checks",
42 [](const PyMlirTransformOptions &self) {
44 },
45 [](PyMlirTransformOptions &self, bool value) {
47 })
48 .def_prop_rw(
49 "enforce_single_top_level_transform_op",
50 [](const PyMlirTransformOptions &self) {
52 self.options);
53 },
54 [](PyMlirTransformOptions &self, bool value) {
56 value);
57 });
58
59 m.def(
60 "apply_named_sequence",
61 [](MlirOperation payloadRoot, MlirOperation transformRoot,
62 MlirOperation transformModule, const PyMlirTransformOptions &options) {
64 mlirOperationGetContext(transformRoot));
65
66 // Calling back into Python to invalidate everything under the payload
67 // root. This is awkward, but we don't have access to PyMlirContext
68 // object here otherwise.
69 nb::object obj = nb::cast(payloadRoot);
70
72 payloadRoot, transformRoot, transformModule, options.options);
74 return;
75
76 throw nb::value_error(
77 ("Failed to apply named transform sequence.\nDiagnostic message " +
78 scope.takeMessage())
79 .c_str());
80 },
81 nb::arg("payload_root"), nb::arg("transform_root"),
82 nb::arg("transform_module"),
83 nb::arg("transform_options") = PyMlirTransformOptions());
84
85 m.def(
86 "copy_symbols_and_merge_into",
87 [](MlirOperation target, MlirOperation other) {
90
93 throw nb::value_error(
94 ("Failed to merge symbols.\nDiagnostic message " +
95 scope.takeMessage())
96 .c_str());
97 }
98 },
99 nb::arg("target"), nb::arg("other"));
100}
101
102NB_MODULE(_mlirTransformInterpreter, m) {
103 m.doc() = "MLIR Transform dialect interpreter functionality.";
105}
static void populateTransformInterpreterSubmodule(nb::module_ &m)
NB_MODULE(_mlirTransformInterpreter, m)
MlirContext mlirOperationGetContext(MlirOperation op)
Definition IR.cpp:651
MLIR_CAPI_EXPORTED 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...
MLIR_CAPI_EXPORTED bool mlirTransformOptionsGetEnforceSingleTopLevelTransformOp(MlirTransformOptions transformOptions)
Returns true if the enforcement of the top-level transform op being single is enabled in transform op...
MLIR_CAPI_EXPORTED void mlirTransformOptionsEnableExpensiveChecks(MlirTransformOptions transformOptions, bool enable)
Enables or disables expensive checks in transform options.
MLIR_CAPI_EXPORTED void mlirTransformOptionsDestroy(MlirTransformOptions transformOptions)
Destroys a transform options object previously created by mlirTransformOptionsCreate.
MLIR_CAPI_EXPORTED void mlirTransformOptionsEnforceSingleTopLevelTransformOp(MlirTransformOptions transformOptions, bool enable)
Enables or disables the enforcement of the top-level transform op being single in transform options.
MLIR_CAPI_EXPORTED MlirTransformOptions mlirTransformOptionsCreate(void)
Creates a default-initialized transform options object.
MLIR_CAPI_EXPORTED MlirLogicalResult mlirMergeSymbolsIntoFromClone(MlirOperation target, MlirOperation other)
Merge the symbols from other into target, potentially renaming them to avoid conflicts.
MLIR_CAPI_EXPORTED bool mlirTransformOptionsGetExpensiveChecksEnabled(MlirTransformOptions transformOptions)
Returns true if expensive checks are enabled in transform options.
static llvm::ManagedStatic< PassManagerOptions > options
RAII scope intercepting all diagnostics into a string.
Definition Diagnostics.h:25
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