[Mlir-commits] [mlir] [MLIR][Python] Add bindings for PDL native rewrite function registering (PR #159926)
Maksim Levental
llvmlistbot at llvm.org
Sat Sep 20 10:47:03 PDT 2025
================
@@ -36,6 +41,43 @@ class PyPDLPatternModule {
}
MlirPDLPatternModule get() { return module; }
+ static nb::object fromPDLValue(MlirPDLValue value) {
+ if (mlirPDLValueIsValue(value)) {
+ return nb::cast(mlirPDLValueAsValue(value));
+ }
+ if (mlirPDLValueIsOperation(value)) {
+ return nb::cast(mlirPDLValueAsOperation(value));
+ }
+ if (mlirPDLValueIsAttribute(value)) {
+ return nb::cast(mlirPDLValueAsAttribute(value));
+ }
+ if (mlirPDLValueIsType(value)) {
+ return nb::cast(mlirPDLValueAsType(value));
+ }
+
+ throw std::runtime_error("unsupported PDL value type");
+ }
+
+ void registerRewriteFunction(const std::string &name,
+ const nb::callable &fn) {
+ mlirPDLPatternModuleRegisterRewriteFunction(
+ get(), mlirStringRefCreate(name.data(), name.size()),
+ [](MlirPatternRewriter rewriter, MlirPDLResultList results,
+ size_t nValues, MlirPDLValue *values,
+ void *userData) -> MlirLogicalResult {
+ auto f = nb::handle(static_cast<PyObject *>(userData));
+ std::vector<nb::object> args;
+ args.reserve(nValues);
+ for (size_t i = 0; i < nValues; ++i) {
+ args.push_back(fromPDLValue(values[i]));
+ }
----------------
makslevental wrote:
nit:
```suggestion
for (size_t i = 0; i < nValues; ++i)
args.push_back(fromPDLValue(values[i]));
```
https://github.com/llvm/llvm-project/pull/159926
More information about the Mlir-commits
mailing list