MLIR  22.0.0git
Rewrite.cpp
Go to the documentation of this file.
1 //===- Rewrite.cpp - Rewrite ----------------------------------------------===//
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 "Rewrite.h"
10 
11 #include "IRModule.h"
12 #include "mlir-c/IR.h"
13 #include "mlir-c/Rewrite.h"
14 #include "mlir-c/Support.h"
15 // clang-format off
17 #include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind.
18 // clang-format on
19 #include "mlir/Config/mlir-config.h"
20 #include "nanobind/nanobind.h"
21 
22 namespace nb = nanobind;
23 using namespace mlir;
24 using namespace nb::literals;
25 using namespace mlir::python;
26 
27 namespace {
28 
29 class PyPatternRewriter {
30 public:
31  PyPatternRewriter(MlirPatternRewriter rewriter)
32  : base(mlirPatternRewriterAsBase(rewriter)),
33  ctx(PyMlirContext::forContext(mlirRewriterBaseGetContext(base))) {}
34 
35  PyInsertionPoint getInsertionPoint() const {
36  MlirBlock block = mlirRewriterBaseGetInsertionBlock(base);
37  MlirOperation op = mlirRewriterBaseGetOperationAfterInsertion(base);
38 
39  if (mlirOperationIsNull(op)) {
40  MlirOperation owner = mlirBlockGetParentOperation(block);
41  auto parent = PyOperation::forOperation(ctx, owner);
42  return PyInsertionPoint(PyBlock(parent, block));
43  }
44 
45  return PyInsertionPoint(PyOperation::forOperation(ctx, op));
46  }
47 
48  void replaceOp(MlirOperation op, MlirOperation newOp) {
50  }
51 
52  void replaceOp(MlirOperation op, const std::vector<MlirValue> &values) {
53  mlirRewriterBaseReplaceOpWithValues(base, op, values.size(), values.data());
54  }
55 
56  void eraseOp(MlirOperation op) { mlirRewriterBaseEraseOp(base, op); }
57 
58 private:
59  MlirRewriterBase base;
60  PyMlirContextRef ctx;
61 };
62 
63 #if MLIR_ENABLE_PDL_IN_PATTERNMATCH
64 static nb::object objectFromPDLValue(MlirPDLValue value) {
65  if (MlirValue v = mlirPDLValueAsValue(value); !mlirValueIsNull(v))
66  return nb::cast(v);
67  if (MlirOperation v = mlirPDLValueAsOperation(value); !mlirOperationIsNull(v))
68  return nb::cast(v);
69  if (MlirAttribute v = mlirPDLValueAsAttribute(value); !mlirAttributeIsNull(v))
70  return nb::cast(v);
71  if (MlirType v = mlirPDLValueAsType(value); !mlirTypeIsNull(v))
72  return nb::cast(v);
73 
74  throw std::runtime_error("unsupported PDL value type");
75 }
76 
77 static std::vector<nb::object> objectsFromPDLValues(size_t nValues,
78  MlirPDLValue *values) {
79  std::vector<nb::object> args;
80  args.reserve(nValues);
81  for (size_t i = 0; i < nValues; ++i)
82  args.push_back(objectFromPDLValue(values[i]));
83  return args;
84 }
85 
86 // Convert the Python object to a boolean.
87 // If it evaluates to False, treat it as success;
88 // otherwise, treat it as failure.
89 // Note that None is considered success.
90 static MlirLogicalResult logicalResultFromObject(const nb::object &obj) {
91  if (obj.is_none())
92  return mlirLogicalResultSuccess();
93 
94  return nb::cast<bool>(obj) ? mlirLogicalResultFailure()
96 }
97 
98 /// Owning Wrapper around a PDLPatternModule.
99 class PyPDLPatternModule {
100 public:
101  PyPDLPatternModule(MlirPDLPatternModule module) : module(module) {}
102  PyPDLPatternModule(PyPDLPatternModule &&other) noexcept
103  : module(other.module) {
104  other.module.ptr = nullptr;
105  }
106  ~PyPDLPatternModule() {
107  if (module.ptr != nullptr)
109  }
110  MlirPDLPatternModule get() { return module; }
111 
112  void registerRewriteFunction(const std::string &name,
113  const nb::callable &fn) {
115  get(), mlirStringRefCreate(name.data(), name.size()),
116  [](MlirPatternRewriter rewriter, MlirPDLResultList results,
117  size_t nValues, MlirPDLValue *values,
118  void *userData) -> MlirLogicalResult {
119  nb::handle f = nb::handle(static_cast<PyObject *>(userData));
120  return logicalResultFromObject(
121  f(PyPatternRewriter(rewriter), results,
122  objectsFromPDLValues(nValues, values)));
123  },
124  fn.ptr());
125  }
126 
127  void registerConstraintFunction(const std::string &name,
128  const nb::callable &fn) {
130  get(), mlirStringRefCreate(name.data(), name.size()),
131  [](MlirPatternRewriter rewriter, MlirPDLResultList results,
132  size_t nValues, MlirPDLValue *values,
133  void *userData) -> MlirLogicalResult {
134  nb::handle f = nb::handle(static_cast<PyObject *>(userData));
135  return logicalResultFromObject(
136  f(PyPatternRewriter(rewriter), results,
137  objectsFromPDLValues(nValues, values)));
138  },
139  fn.ptr());
140  }
141 
142 private:
143  MlirPDLPatternModule module;
144 };
145 #endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH
146 
147 /// Owning Wrapper around a FrozenRewritePatternSet.
148 class PyFrozenRewritePatternSet {
149 public:
150  PyFrozenRewritePatternSet(MlirFrozenRewritePatternSet set) : set(set) {}
151  PyFrozenRewritePatternSet(PyFrozenRewritePatternSet &&other) noexcept
152  : set(other.set) {
153  other.set.ptr = nullptr;
154  }
155  ~PyFrozenRewritePatternSet() {
156  if (set.ptr != nullptr)
158  }
159  MlirFrozenRewritePatternSet get() { return set; }
160 
161  nb::object getCapsule() {
162  return nb::steal<nb::object>(
164  }
165 
166  static nb::object createFromCapsule(const nb::object &capsule) {
167  MlirFrozenRewritePatternSet rawPm =
169  if (rawPm.ptr == nullptr)
170  throw nb::python_error();
171  return nb::cast(PyFrozenRewritePatternSet(rawPm), nb::rv_policy::move);
172  }
173 
174 private:
175  MlirFrozenRewritePatternSet set;
176 };
177 
178 class PyRewritePatternSet {
179 public:
180  PyRewritePatternSet(MlirContext ctx)
181  : set(mlirRewritePatternSetCreate(ctx)), ctx(ctx) {}
182  ~PyRewritePatternSet() {
183  if (set.ptr)
185  }
186 
187  void add(MlirStringRef rootName, unsigned benefit,
188  const nb::callable &matchAndRewrite) {
189  MlirRewritePatternCallbacks callbacks;
190  callbacks.construct = [](void *userData) {
191  nb::handle(static_cast<PyObject *>(userData)).inc_ref();
192  };
193  callbacks.destruct = [](void *userData) {
194  nb::handle(static_cast<PyObject *>(userData)).dec_ref();
195  };
196  callbacks.matchAndRewrite = [](MlirRewritePattern, MlirOperation op,
197  MlirPatternRewriter rewriter,
198  void *userData) -> MlirLogicalResult {
199  nb::handle f(static_cast<PyObject *>(userData));
200 
201  PyMlirContextRef ctx =
202  PyMlirContext::forContext(mlirOperationGetContext(op));
203  nb::object opView = PyOperation::forOperation(ctx, op)->createOpView();
204 
205  nb::object res = f(opView, PyPatternRewriter(rewriter));
206  return logicalResultFromObject(res);
207  };
208  MlirRewritePattern pattern = mlirOpRewritePattenCreate(
209  rootName, benefit, ctx, callbacks, matchAndRewrite.ptr(),
210  /* nGeneratedNames */ 0,
211  /* generatedNames */ nullptr);
212  mlirRewritePatternSetAdd(set, pattern);
213  }
214 
215  PyFrozenRewritePatternSet freeze() {
216  MlirRewritePatternSet s = set;
217  set.ptr = nullptr;
218  return mlirFreezeRewritePattern(s);
219  }
220 
221 private:
222  MlirRewritePatternSet set;
223  MlirContext ctx;
224 };
225 
226 } // namespace
227 
228 /// Create the `mlir.rewrite` here.
229 void mlir::python::populateRewriteSubmodule(nb::module_ &m) {
230  //----------------------------------------------------------------------------
231  // Mapping of the PatternRewriter
232  //----------------------------------------------------------------------------
233  nb::
234  class_<PyPatternRewriter>(m, "PatternRewriter")
235  .def_prop_ro("ip", &PyPatternRewriter::getInsertionPoint,
236  "The current insertion point of the PatternRewriter.")
237  .def(
238  "replace_op",
239  [](PyPatternRewriter &self, MlirOperation op,
240  MlirOperation newOp) { self.replaceOp(op, newOp); },
241  "Replace an operation with a new operation.", nb::arg("op"),
242  nb::arg("new_op"),
243  // clang-format off
244  nb::sig("def replace_op(self, op: " MAKE_MLIR_PYTHON_QUALNAME("ir.Operation") ", new_op: " MAKE_MLIR_PYTHON_QUALNAME("ir.Operation") ") -> None")
245  // clang-format on
246  )
247  .def(
248  "replace_op",
249  [](PyPatternRewriter &self, MlirOperation op,
250  const std::vector<MlirValue> &values) {
251  self.replaceOp(op, values);
252  },
253  "Replace an operation with a list of values.", nb::arg("op"),
254  nb::arg("values"),
255  // clang-format off
256  nb::sig("def replace_op(self, op: " MAKE_MLIR_PYTHON_QUALNAME("ir.Operation") ", values: list[" MAKE_MLIR_PYTHON_QUALNAME("ir.Value") "]) -> None")
257  // clang-format on
258  )
259  .def("erase_op", &PyPatternRewriter::eraseOp, "Erase an operation.",
260  nb::arg("op"),
261  // clang-format off
262  nb::sig("def erase_op(self, op: " MAKE_MLIR_PYTHON_QUALNAME("ir.Operation") ") -> None")
263  // clang-format on
264  );
265 
266  //----------------------------------------------------------------------------
267  // Mapping of the RewritePatternSet
268  //----------------------------------------------------------------------------
269  nb::class_<PyRewritePatternSet>(m, "RewritePatternSet")
270  .def(
271  "__init__",
272  [](PyRewritePatternSet &self, DefaultingPyMlirContext context) {
273  new (&self) PyRewritePatternSet(context.get()->get());
274  },
275  "context"_a = nb::none())
276  .def(
277  "add",
278  [](PyRewritePatternSet &self, nb::handle root, const nb::callable &fn,
279  unsigned benefit) {
280  std::string opName =
281  nb::cast<std::string>(root.attr("OPERATION_NAME"));
282  self.add(mlirStringRefCreate(opName.data(), opName.size()), benefit,
283  fn);
284  },
285  "root"_a, "fn"_a, "benefit"_a = 1,
286  "Add a new rewrite pattern on the given root operation with the "
287  "callable as the matching and rewriting function and the given "
288  "benefit.")
289  .def("freeze", &PyRewritePatternSet::freeze,
290  "Freeze the pattern set into a frozen one.");
291 
292  //----------------------------------------------------------------------------
293  // Mapping of the PDLResultList and PDLModule
294  //----------------------------------------------------------------------------
295 #if MLIR_ENABLE_PDL_IN_PATTERNMATCH
296  nb::class_<MlirPDLResultList>(m, "PDLResultList")
297  .def(
298  "append",
299  [](MlirPDLResultList results, const PyValue &value) {
300  mlirPDLResultListPushBackValue(results, value);
301  },
302  // clang-format off
303  nb::sig("def append(self, value: " MAKE_MLIR_PYTHON_QUALNAME("ir.Value") ")")
304  // clang-format on
305  )
306  .def(
307  "append",
308  [](MlirPDLResultList results, const PyOperation &op) {
310  },
311  // clang-format off
312  nb::sig("def append(self, op: " MAKE_MLIR_PYTHON_QUALNAME("ir.Operation") ")")
313  // clang-format on
314  )
315  .def(
316  "append",
317  [](MlirPDLResultList results, const PyType &type) {
318  mlirPDLResultListPushBackType(results, type);
319  },
320  // clang-format off
321  nb::sig("def append(self, type: " MAKE_MLIR_PYTHON_QUALNAME("ir.Type") ")")
322  // clang-format on
323  )
324  .def(
325  "append",
326  [](MlirPDLResultList results, const PyAttribute &attr) {
327  mlirPDLResultListPushBackAttribute(results, attr);
328  },
329  // clang-format off
330  nb::sig("def append(self, attr: " MAKE_MLIR_PYTHON_QUALNAME("ir.Attribute") ")")
331  // clang-format on
332  );
333  nb::class_<PyPDLPatternModule>(m, "PDLModule")
334  .def(
335  "__init__",
336  [](PyPDLPatternModule &self, MlirModule module) {
337  new (&self)
338  PyPDLPatternModule(mlirPDLPatternModuleFromModule(module));
339  },
340  // clang-format off
341  nb::sig("def __init__(self, module: " MAKE_MLIR_PYTHON_QUALNAME("ir.Module") ") -> None"),
342  // clang-format on
343  "module"_a, "Create a PDL module from the given module.")
344  .def(
345  "__init__",
346  [](PyPDLPatternModule &self, PyModule &module) {
347  new (&self) PyPDLPatternModule(
349  },
350  // clang-format off
351  nb::sig("def __init__(self, module: " MAKE_MLIR_PYTHON_QUALNAME("ir.Module") ") -> None"),
352  // clang-format on
353  "module"_a, "Create a PDL module from the given module.")
354  .def(
355  "freeze",
356  [](PyPDLPatternModule &self) {
357  return PyFrozenRewritePatternSet(mlirFreezeRewritePattern(
359  },
360  nb::keep_alive<0, 1>())
361  .def(
362  "register_rewrite_function",
363  [](PyPDLPatternModule &self, const std::string &name,
364  const nb::callable &fn) {
365  self.registerRewriteFunction(name, fn);
366  },
367  nb::keep_alive<1, 3>())
368  .def(
369  "register_constraint_function",
370  [](PyPDLPatternModule &self, const std::string &name,
371  const nb::callable &fn) {
372  self.registerConstraintFunction(name, fn);
373  },
374  nb::keep_alive<1, 3>());
375 #endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH
376  nb::class_<PyFrozenRewritePatternSet>(m, "FrozenRewritePatternSet")
377  .def_prop_ro(MLIR_PYTHON_CAPI_PTR_ATTR,
378  &PyFrozenRewritePatternSet::getCapsule)
380  &PyFrozenRewritePatternSet::createFromCapsule);
381  m.def(
382  "apply_patterns_and_fold_greedily",
383  [](PyModule &module, PyFrozenRewritePatternSet &set) {
384  auto status =
385  mlirApplyPatternsAndFoldGreedily(module.get(), set.get(), {});
386  if (mlirLogicalResultIsFailure(status))
387  throw std::runtime_error("pattern application failed to converge");
388  },
389  "module"_a, "set"_a,
390  // clang-format off
391  nb::sig("def apply_patterns_and_fold_greedily(module: " MAKE_MLIR_PYTHON_QUALNAME("ir.Module") ", set: FrozenRewritePatternSet) -> None"),
392  // clang-format on
393  "Applys the given patterns to the given module greedily while folding "
394  "results.")
395  .def(
396  "apply_patterns_and_fold_greedily",
397  [](PyModule &module, MlirFrozenRewritePatternSet set) {
398  auto status =
399  mlirApplyPatternsAndFoldGreedily(module.get(), set, {});
400  if (mlirLogicalResultIsFailure(status))
401  throw std::runtime_error(
402  "pattern application failed to converge");
403  },
404  "module"_a, "set"_a,
405  // clang-format off
406  nb::sig("def apply_patterns_and_fold_greedily(module: " MAKE_MLIR_PYTHON_QUALNAME("ir.Module") ", set: FrozenRewritePatternSet) -> None"),
407  // clang-format on
408  "Applys the given patterns to the given module greedily while "
409  "folding "
410  "results.")
411  .def(
412  "apply_patterns_and_fold_greedily",
413  [](PyOperationBase &op, PyFrozenRewritePatternSet &set) {
415  op.getOperation(), set.get(), {});
416  if (mlirLogicalResultIsFailure(status))
417  throw std::runtime_error(
418  "pattern application failed to converge");
419  },
420  "op"_a, "set"_a,
421  // clang-format off
422  nb::sig("def apply_patterns_and_fold_greedily(op: " MAKE_MLIR_PYTHON_QUALNAME("ir._OperationBase") ", set: FrozenRewritePatternSet) -> None"),
423  // clang-format on
424  "Applys the given patterns to the given op greedily while folding "
425  "results.")
426  .def(
427  "apply_patterns_and_fold_greedily",
428  [](PyOperationBase &op, MlirFrozenRewritePatternSet set) {
430  op.getOperation(), set, {});
431  if (mlirLogicalResultIsFailure(status))
432  throw std::runtime_error(
433  "pattern application failed to converge");
434  },
435  "op"_a, "set"_a,
436  // clang-format off
437  nb::sig("def apply_patterns_and_fold_greedily(op: " MAKE_MLIR_PYTHON_QUALNAME("ir._OperationBase") ", set: FrozenRewritePatternSet) -> None"),
438  // clang-format on
439  "Applys the given patterns to the given op greedily while folding "
440  "results.");
441 }
MlirRewritePatternSet mlirRewritePatternSetCreate(MlirContext context)
RewritePatternSet API.
Definition: Rewrite.cpp:362
MlirLogicalResult mlirApplyPatternsAndFoldGreedilyWithOp(MlirOperation op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig)
Definition: Rewrite.cpp:293
void mlirFrozenRewritePatternSetDestroy(MlirFrozenRewritePatternSet set)
Destroy the given MlirFrozenRewritePatternSet.
Definition: Rewrite.cpp:280
MlirLogicalResult mlirApplyPatternsAndFoldGreedily(MlirModule op, MlirFrozenRewritePatternSet patterns, MlirGreedyRewriteDriverConfig)
Definition: Rewrite.cpp:286
MlirRewriterBase mlirPatternRewriterAsBase(MlirPatternRewriter rewriter)
PatternRewriter API.
Definition: Rewrite.cpp:303
MlirContext mlirRewriterBaseGetContext(MlirRewriterBase rewriter)
RewriterBase API inherited from OpBuilder.
Definition: Rewrite.cpp:28
void mlirRewriterBaseReplaceOpWithValues(MlirRewriterBase rewriter, MlirOperation op, intptr_t nValues, MlirValue const *values)
Replace the results of the given (original) operation with the specified list of values (replacements...
Definition: Rewrite.cpp:133
void mlirRewritePatternSetDestroy(MlirRewritePatternSet set)
Destruct the given MlirRewritePatternSet.
Definition: Rewrite.cpp:366
MlirOperation mlirRewriterBaseGetOperationAfterInsertion(MlirRewriterBase rewriter)
Returns the operation right after the current insertion point of the rewriter.
Definition: Rewrite.cpp:74
MlirFrozenRewritePatternSet mlirFreezeRewritePattern(MlirRewritePatternSet set)
RewritePatternSet and FrozenRewritePatternSet API.
Definition: Rewrite.cpp:274
MlirRewritePattern mlirOpRewritePattenCreate(MlirStringRef rootName, unsigned benefit, MlirContext context, MlirRewritePatternCallbacks callbacks, void *userData, size_t nGeneratedNames, MlirStringRef *generatedNames)
Create a rewrite pattern that matches the operation with the given rootName, corresponding to mlir::O...
Definition: Rewrite.cpp:344
void mlirRewriterBaseReplaceOpWithOperation(MlirRewriterBase rewriter, MlirOperation op, MlirOperation newOp)
Replace the results of the given (original) operation with the specified new op (replacement).
Definition: Rewrite.cpp:141
void mlirRewritePatternSetAdd(MlirRewritePatternSet set, MlirRewritePattern pattern)
Add the given MlirRewritePattern into a MlirRewritePatternSet.
Definition: Rewrite.cpp:370
MlirBlock mlirRewriterBaseGetInsertionBlock(MlirRewriterBase rewriter)
Return the block the current insertion point belongs to.
Definition: Rewrite.cpp:65
void mlirRewriterBaseEraseOp(MlirRewriterBase rewriter, MlirOperation op)
Erases an operation that is known to have no uses.
Definition: Rewrite.cpp:147
static MlirFrozenRewritePatternSet mlirPythonCapsuleToFrozenRewritePatternSet(PyObject *capsule)
Extracts an MlirFrozenRewritePatternSet from a capsule as produced from mlirPythonFrozenRewritePatter...
Definition: Interop.h:302
#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
#define MAKE_MLIR_PYTHON_QUALNAME(local)
Definition: Interop.h:57
static PyObject * mlirPythonFrozenRewritePatternSetToCapsule(MlirFrozenRewritePatternSet pm)
Creates a capsule object encapsulating the raw C-API MlirFrozenRewritePatternSet.
Definition: Interop.h:293
Used in function arguments when None should resolve to the current context manager set instance.
Definition: IRModule.h:273
ReferrentTy * get() const
Definition: NanobindUtils.h:60
Wrapper around the generic MlirAttribute.
Definition: IRModule.h:1008
Wrapper around an MlirBlock.
Definition: IRModule.h:813
An insertion point maintains a pointer to a Block and a reference operation.
Definition: IRModule.h:837
MlirContext get()
Accesses the underlying MlirContext.
Definition: IRModule.h:204
MlirModule get()
Gets the backing MlirModule.
Definition: IRModule.h:522
Base class for PyOperation and PyOpView which exposes the primary, user visible methods for manipulat...
Definition: IRModule.h:552
virtual PyOperation & getOperation()=0
Each must provide access to the raw Operation.
MlirOperation get() const
Definition: IRModule.h:638
Wrapper around the generic MlirType.
Definition: IRModule.h:878
Wrapper around the generic MlirValue.
Definition: IRModule.h:1167
MLIR_CAPI_EXPORTED void mlirPDLPatternModuleDestroy(MlirPDLPatternModule op)
MLIR_CAPI_EXPORTED MlirValue mlirPDLValueAsValue(MlirPDLValue value)
Cast the MlirPDLValue to an MlirValue.
MLIR_CAPI_EXPORTED void mlirPDLPatternModuleRegisterRewriteFunction(MlirPDLPatternModule pdlModule, MlirStringRef name, MlirPDLRewriteFunction rewriteFn, void *userData)
Register a rewrite function into the given PDL pattern module.
MLIR_CAPI_EXPORTED MlirPDLPatternModule mlirPDLPatternModuleFromModule(MlirModule op)
MLIR_CAPI_EXPORTED MlirType mlirPDLValueAsType(MlirPDLValue value)
Cast the MlirPDLValue to an MlirType.
MLIR_CAPI_EXPORTED MlirOperation mlirPDLValueAsOperation(MlirPDLValue value)
Cast the MlirPDLValue to an MlirOperation.
MLIR_CAPI_EXPORTED void mlirPDLResultListPushBackAttribute(MlirPDLResultList results, MlirAttribute value)
Push the MlirAttribute into the given MlirPDLResultList.
MLIR_CAPI_EXPORTED void mlirPDLResultListPushBackOperation(MlirPDLResultList results, MlirOperation value)
Push the MlirOperation into the given MlirPDLResultList.
MLIR_CAPI_EXPORTED MlirRewritePatternSet mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op)
MLIR_CAPI_EXPORTED void mlirPDLResultListPushBackType(MlirPDLResultList results, MlirType value)
Push the MlirType into the given MlirPDLResultList.
MLIR_CAPI_EXPORTED MlirAttribute mlirPDLValueAsAttribute(MlirPDLValue value)
Cast the MlirPDLValue to an MlirAttribute.
MLIR_CAPI_EXPORTED void mlirPDLPatternModuleRegisterConstraintFunction(MlirPDLPatternModule pdlModule, MlirStringRef name, MlirPDLConstraintFunction constraintFn, void *userData)
Register a constraint function into the given PDL pattern module.
MLIR_CAPI_EXPORTED void mlirPDLResultListPushBackValue(MlirPDLResultList results, MlirValue value)
Push the MlirValue into the given MlirPDLResultList.
static bool mlirAttributeIsNull(MlirAttribute attr)
Checks whether an attribute is null.
Definition: IR.h:1183
static bool mlirValueIsNull(MlirValue value)
Returns whether the value is null.
Definition: IR.h:1032
static bool mlirTypeIsNull(MlirType type)
Checks whether a type is null.
Definition: IR.h:1148
MLIR_CAPI_EXPORTED MlirContext mlirOperationGetContext(MlirOperation op)
Gets the context this operation is associated with.
Definition: IR.cpp:651
MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetParentOperation(MlirBlock)
Returns the closest surrounding operation that contains this block.
Definition: IR.cpp:981
static bool mlirOperationIsNull(MlirOperation op)
Checks whether the underlying operation is null.
Definition: IR.h:621
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Definition: Support.h:82
static MlirLogicalResult mlirLogicalResultFailure(void)
Creates a logical result representing a failure.
Definition: Support.h:138
static MlirLogicalResult mlirLogicalResultSuccess(void)
Creates a logical result representing a success.
Definition: Support.h:132
static bool mlirLogicalResultIsFailure(MlirLogicalResult res)
Checks if the given logical result represents a failure.
Definition: Support.h:127
void populateRewriteSubmodule(nanobind::module_ &m)
detail::LazyTextBuild add(const char *fmt, Ts &&...ts)
Create a Remark with llvm::formatv formatting.
Definition: Remarks.h:463
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
RewritePattern API.
Definition: Rewrite.h:337
MlirLogicalResult(* matchAndRewrite)(MlirRewritePattern pattern, MlirOperation op, MlirPatternRewriter rewriter, void *userData)
The callback function to match against code rooted at the specified operation, and perform the rewrit...
Definition: Rewrite.h:347
void(* construct)(void *userData)
Optional constructor for the user data.
Definition: Rewrite.h:340
void(* destruct)(void *userData)
Optional destructor for the user data.
Definition: Rewrite.h:343
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition: Support.h:73