[Mlir-commits] [mlir] [MLIR][Python] Add `GreedyRewriteDriverConfig` parameter to `apply_patterns_and_fold_greedily` (PR #174785)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Jan 7 08:25:01 PST 2026


================
@@ -509,26 +509,42 @@ void populateRewriteSubmodule(nb::module_ &m) {
            &PyFrozenRewritePatternSet::createFromCapsule);
   m.def(
        "apply_patterns_and_fold_greedily",
-       [](PyModule &module, PyFrozenRewritePatternSet &set) {
+       [](PyModule &module, PyFrozenRewritePatternSet &set, nb::object config) {
+         if (config.is_none()) {
+           config = nb::cast(PyGreedyRewriteDriverConfig());
+         }
+
          auto status = mlirApplyPatternsAndFoldGreedily(
-             module.get(), set.get(), mlirGreedyRewriteDriverConfigCreate());
+             module.get(), set.get(),
+             nb::cast<PyGreedyRewriteDriverConfig &>(config).get());
          if (mlirLogicalResultIsFailure(status))
            throw std::runtime_error("pattern application failed to converge");
        },
-       "module"_a, "set"_a,
+       "module"_a, "set"_a, "config"_a = nb::none(),
----------------
PragmaTwice wrote:

hmm it seems that this requires that `PyGreedyRewriteDriverConfig` can be copy-constructed (`T(const T&)`), but now it can only be move-constructed (`T(T&&)`) instead. 🤔 

```
In file included from /llvm-project/mlir/lib/Bindings/Python/Rewrite.cpp:9:
In file included from /llvm-project/mlir/lib/Bindings/Python/Rewrite.h:12:
In file included from /llvm-project/mlir/include/mlir/Bindings/Python/NanobindUtils.h:14:
In file included from /llvm-project/mlir/include/mlir/Bindings/Python/Nanobind.h:27:
In file included from /python-env/lib/python3.11/site-packages/nanobind/include/nanobind/stl/optional.h:12:
/python-env/lib/python3.11/site-packages/nanobind/include/nanobind/stl/detail/nb_optional.h:34:15: error: no matching member function for call to 'emplace'
   34 |         value.emplace(caster.operator cast_t<T>());
      |         ~~~~~~^~~~~~~
/python-env/lib/python3.11/site-packages/nanobind/include/nanobind/nb_func.h:254:41: note: in instantiation of member function 'nanobind::detail::optional_caster<std::optional<mlir::python::mlir::PyGreedyRewriteDriverConfig>>::from_python' requested here
  254 |             if ((!in.template get<Is>().from_python(args[Is], args_flags[Is],
      |                                         ^
/python-env/lib/python3.11/site-packages/nanobind/include/nanobind/nb_func.h:352:13: note: in instantiation of function template specialization 'nanobind::detail::func_create<false, true, (lambda at /llvm-project/mlir/lib/Bindings/Python/Rewrite.cpp:512:8), void, mlir::python::mlir::PyModule &, mlir::python::mlir::PyFrozenRewritePatternSet &, std::optional<mlir::python::mlir::PyGreedyRewriteDriverConfig> &&, 0UL, 1UL, 2UL, nanobind::scope, nanobind::name, nanobind::arg, nanobind::arg, nanobind::arg_v, char[78]>' requested here
  352 |     detail::func_create<false, true>(
      |             ^
/python-env/lib/python3.11/site-packages/nanobind/include/nanobind/nb_func.h:409:5: note: in instantiation of function template specialization 'nanobind::cpp_function_def<void, (lambda at /llvm-project/mlir/lib/Bindings/Python/Rewrite.cpp:512:8), nanobind::scope, nanobind::name, nanobind::arg, nanobind::arg, nanobind::arg_v, char[78], 0>' requested here
  409 |     cpp_function_def((detail::forward_t<Func>) f, scope(*this),
      |     ^
/llvm-project/mlir/lib/Bindings/Python/Rewrite.cpp:510:5: note: in instantiation of function template specialization 'nanobind::module_::def<(lambda at /llvm-project/mlir/lib/Bindings/Python/Rewrite.cpp:512:8), nanobind::arg, nanobind::arg, nanobind::arg_v, char[78]>' requested here
  510 |   m.def(
      |     ^
/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/optional:914:2: note: candidate template ignored: requirement 'is_constructible_v<mlir::python::mlir::PyGreedyRewriteDriverConfig, mlir::python::mlir::PyGreedyRewriteDriverConfig &>' was not satisfied [with _Args = <Type &>]
  914 |         emplace(_Args&&... __args)
      |         ^
/usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/optional:926:2: note: candidate template ignored: could not match 'initializer_list<_Up>' against 'Type' (aka 'mlir::python::mlir::PyGreedyRewriteDriverConfig')
  926 |         emplace(initializer_list<_Up> __il, _Args&&... __args)
      |         ^
1 error generated.
ninja: build stopped: subcommand failed.
```

code:
```c++
  m.def(
       "apply_patterns_and_fold_greedily",
       [](PyModule &module, PyFrozenRewritePatternSet &set,
          std::optional<PyGreedyRewriteDriverConfig> config) {
         if (!config) {
           config.emplace(PyGreedyRewriteDriverConfig());
         }

         auto status = mlirApplyPatternsAndFoldGreedily(module.get(), set.get(),
                                                        config->get());
         if (mlirLogicalResultIsFailure(status))
           throw std::runtime_error("pattern application failed to converge");
       },
       "module"_a, "set"_a, "config"_a = nb::none(),
       "Applys the given patterns to the given module greedily while folding "
       "results.")
```

https://github.com/llvm/llvm-project/pull/174785


More information about the Mlir-commits mailing list