Skip to content

Commit 24ac506

Browse files
authored
[MLIR][NFC] Clean up rewrite CAPI implementation and Python bindings (#162974)
This is a follow-up PR of #162699. In this PR we clean CAPI and Python bindings of MLIR rewrite part by: - remove all manually-defined `wrap`/`unwrap` functions; - remove useless nanobind-defined Python class `RewritePattern`.
1 parent 688cbe4 commit 24ac506

File tree

3 files changed

+10
-46
lines changed

3 files changed

+10
-46
lines changed

mlir/include/mlir/CAPI/Rewrite.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@
1818
#include "mlir-c/Rewrite.h"
1919
#include "mlir/CAPI/Wrap.h"
2020
#include "mlir/IR/PatternMatch.h"
21+
#include "mlir/Rewrite/FrozenRewritePatternSet.h"
2122

2223
DEFINE_C_API_PTR_METHODS(MlirRewriterBase, mlir::RewriterBase)
2324
DEFINE_C_API_PTR_METHODS(MlirRewritePattern, const mlir::RewritePattern)
2425
DEFINE_C_API_PTR_METHODS(MlirRewritePatternSet, mlir::RewritePatternSet)
26+
DEFINE_C_API_PTR_METHODS(MlirFrozenRewritePatternSet,
27+
mlir::FrozenRewritePatternSet)
28+
DEFINE_C_API_PTR_METHODS(MlirPatternRewriter, mlir::PatternRewriter)
29+
30+
#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
31+
DEFINE_C_API_PTR_METHODS(MlirPDLPatternModule, mlir::PDLPatternModule)
32+
DEFINE_C_API_PTR_METHODS(MlirPDLResultList, mlir::PDLResultList)
33+
DEFINE_C_API_PTR_METHODS(MlirPDLValue, const mlir::PDLValue)
34+
#endif // MLIR_ENABLE_PDL_IN_PATTERNMATCH
2535

2636
#endif // MLIR_CAPIREWRITER_H

mlir/lib/Bindings/Python/Rewrite.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ void mlir::python::populateRewriteSubmodule(nb::module_ &m) {
261261
//----------------------------------------------------------------------------
262262
// Mapping of the RewritePatternSet
263263
//----------------------------------------------------------------------------
264-
nb::class_<MlirRewritePattern>(m, "RewritePattern");
265264
nb::class_<PyRewritePatternSet>(m, "RewritePatternSet")
266265
.def(
267266
"__init__",

mlir/lib/CAPI/Transforms/Rewrite.cpp

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,6 @@ void mlirIRRewriterDestroy(MlirRewriterBase rewriter) {
270270
/// RewritePatternSet and FrozenRewritePatternSet API
271271
//===----------------------------------------------------------------------===//
272272

273-
static inline mlir::FrozenRewritePatternSet *
274-
unwrap(MlirFrozenRewritePatternSet module) {
275-
assert(module.ptr && "unexpected null module");
276-
return static_cast<mlir::FrozenRewritePatternSet *>(module.ptr);
277-
}
278-
279-
static inline MlirFrozenRewritePatternSet
280-
wrap(mlir::FrozenRewritePatternSet *module) {
281-
return {module};
282-
}
283-
284273
MlirFrozenRewritePatternSet
285274
mlirFreezeRewritePattern(MlirRewritePatternSet set) {
286275
auto *m = new mlir::FrozenRewritePatternSet(std::move(*unwrap(set)));
@@ -311,15 +300,6 @@ mlirApplyPatternsAndFoldGreedilyWithOp(MlirOperation op,
311300
/// PatternRewriter API
312301
//===----------------------------------------------------------------------===//
313302

314-
inline mlir::PatternRewriter *unwrap(MlirPatternRewriter rewriter) {
315-
assert(rewriter.ptr && "unexpected null rewriter");
316-
return static_cast<mlir::PatternRewriter *>(rewriter.ptr);
317-
}
318-
319-
inline MlirPatternRewriter wrap(mlir::PatternRewriter *rewriter) {
320-
return {rewriter};
321-
}
322-
323303
MlirRewriterBase mlirPatternRewriterAsBase(MlirPatternRewriter rewriter) {
324304
return wrap(static_cast<mlir::RewriterBase *>(unwrap(rewriter)));
325305
}
@@ -400,15 +380,6 @@ void mlirRewritePatternSetAdd(MlirRewritePatternSet set,
400380
//===----------------------------------------------------------------------===//
401381

402382
#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
403-
static inline mlir::PDLPatternModule *unwrap(MlirPDLPatternModule module) {
404-
assert(module.ptr && "unexpected null module");
405-
return static_cast<mlir::PDLPatternModule *>(module.ptr);
406-
}
407-
408-
static inline MlirPDLPatternModule wrap(mlir::PDLPatternModule *module) {
409-
return {module};
410-
}
411-
412383
MlirPDLPatternModule mlirPDLPatternModuleFromModule(MlirModule op) {
413384
return wrap(new mlir::PDLPatternModule(
414385
mlir::OwningOpRef<mlir::ModuleOp>(unwrap(op))));
@@ -426,22 +397,6 @@ mlirRewritePatternSetFromPDLPatternModule(MlirPDLPatternModule op) {
426397
return wrap(m);
427398
}
428399

429-
inline const mlir::PDLValue *unwrap(MlirPDLValue value) {
430-
assert(value.ptr && "unexpected null PDL value");
431-
return static_cast<const mlir::PDLValue *>(value.ptr);
432-
}
433-
434-
inline MlirPDLValue wrap(const mlir::PDLValue *value) { return {value}; }
435-
436-
inline mlir::PDLResultList *unwrap(MlirPDLResultList results) {
437-
assert(results.ptr && "unexpected null PDL results");
438-
return static_cast<mlir::PDLResultList *>(results.ptr);
439-
}
440-
441-
inline MlirPDLResultList wrap(mlir::PDLResultList *results) {
442-
return {results};
443-
}
444-
445400
MlirValue mlirPDLValueAsValue(MlirPDLValue value) {
446401
return wrap(unwrap(value)->dyn_cast<mlir::Value>());
447402
}

0 commit comments

Comments
 (0)