Skip to content

Commit ae75929

Browse files
[MLIR][Python] Expose the insertion point of pattern rewriter (#161001)
In [#160520](llvm/llvm-project#160520), we discussed the current limitations of PDL rewriting in Python (see [this comment](llvm/llvm-project#160520 (comment))). At the moment, we cannot create new operations in PDL native (python) rewrite functions because the `PatternRewriter` APIs are not exposed. This PR introduces bindings to retrieve the insertion point of the `PatternRewriter`, enabling users to create new operations within Python rewrite functions. With this capability, more complex rewrites e.g. with branching and loops that involve op creations become possible. --------- Co-authored-by: Maksim Levental <[email protected]>
1 parent a374b25 commit ae75929

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

mlir/include/mlir-c/Rewrite.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ mlirRewriterBaseGetInsertionBlock(MlirRewriterBase rewriter);
101101
MLIR_CAPI_EXPORTED MlirBlock
102102
mlirRewriterBaseGetBlock(MlirRewriterBase rewriter);
103103

104+
/// Returns the operation right after the current insertion point
105+
/// of the rewriter. A null MlirOperation will be returned
106+
// if the current insertion point is at the end of the block.
107+
MLIR_CAPI_EXPORTED MlirOperation
108+
mlirRewriterBaseGetOperationAfterInsertion(MlirRewriterBase rewriter);
109+
104110
//===----------------------------------------------------------------------===//
105111
/// Block and operation creation/insertion/cloning
106112
//===----------------------------------------------------------------------===//
@@ -310,6 +316,14 @@ MLIR_CAPI_EXPORTED MlirLogicalResult mlirApplyPatternsAndFoldGreedily(
310316
MlirModule op, MlirFrozenRewritePatternSet patterns,
311317
MlirGreedyRewriteDriverConfig);
312318

319+
//===----------------------------------------------------------------------===//
320+
/// PatternRewriter API
321+
//===----------------------------------------------------------------------===//
322+
323+
/// Cast the PatternRewriter to a RewriterBase
324+
MLIR_CAPI_EXPORTED MlirRewriterBase
325+
mlirPatternRewriterAsBase(MlirPatternRewriter rewriter);
326+
313327
//===----------------------------------------------------------------------===//
314328
/// PDLPatternModule API
315329
//===----------------------------------------------------------------------===//

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,9 @@ PyInsertionPoint::PyInsertionPoint(PyOperationBase &beforeOperationBase)
20462046
: refOperation(beforeOperationBase.getOperation().getRef()),
20472047
block((*refOperation)->getBlock()) {}
20482048

2049+
PyInsertionPoint::PyInsertionPoint(PyOperationRef beforeOperationRef)
2050+
: refOperation(beforeOperationRef), block((*refOperation)->getBlock()) {}
2051+
20492052
void PyInsertionPoint::insert(PyOperationBase &operationBase) {
20502053
PyOperation &operation = operationBase.getOperation();
20512054
if (operation.isAttached())

mlir/lib/Bindings/Python/IRModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ class PyInsertionPoint {
841841
PyInsertionPoint(const PyBlock &block);
842842
/// Creates an insertion point positioned before a reference operation.
843843
PyInsertionPoint(PyOperationBase &beforeOperationBase);
844+
/// Creates an insertion point positioned before a reference operation.
845+
PyInsertionPoint(PyOperationRef beforeOperationRef);
844846

845847
/// Shortcut to create an insertion point at the beginning of the block.
846848
static PyInsertionPoint atBlockBegin(PyBlock &block);

mlir/lib/Bindings/Python/Rewrite.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@ using namespace mlir::python;
2626

2727
namespace {
2828

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+
private:
49+
MlirRewriterBase base;
50+
PyMlirContextRef ctx;
51+
};
52+
2953
#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
3054
static nb::object objectFromPDLValue(MlirPDLValue value) {
3155
if (MlirValue v = mlirPDLValueAsValue(value); !mlirValueIsNull(v))
@@ -84,7 +108,8 @@ class PyPDLPatternModule {
84108
void *userData) -> MlirLogicalResult {
85109
nb::handle f = nb::handle(static_cast<PyObject *>(userData));
86110
return logicalResultFromObject(
87-
f(rewriter, results, objectsFromPDLValues(nValues, values)));
111+
f(PyPatternRewriter(rewriter), results,
112+
objectsFromPDLValues(nValues, values)));
88113
},
89114
fn.ptr());
90115
}
@@ -98,7 +123,8 @@ class PyPDLPatternModule {
98123
void *userData) -> MlirLogicalResult {
99124
nb::handle f = nb::handle(static_cast<PyObject *>(userData));
100125
return logicalResultFromObject(
101-
f(rewriter, results, objectsFromPDLValues(nValues, values)));
126+
f(PyPatternRewriter(rewriter), results,
127+
objectsFromPDLValues(nValues, values)));
102128
},
103129
fn.ptr());
104130
}
@@ -143,7 +169,9 @@ class PyFrozenRewritePatternSet {
143169

144170
/// Create the `mlir.rewrite` here.
145171
void mlir::python::populateRewriteSubmodule(nb::module_ &m) {
146-
nb::class_<MlirPatternRewriter>(m, "PatternRewriter");
172+
nb::class_<PyPatternRewriter>(m, "PatternRewriter")
173+
.def_prop_ro("ip", &PyPatternRewriter::getInsertionPoint,
174+
"The current insertion point of the PatternRewriter.");
147175
//----------------------------------------------------------------------------
148176
// Mapping of the PDLResultList and PDLModule
149177
//----------------------------------------------------------------------------

mlir/lib/CAPI/Transforms/Rewrite.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ MlirBlock mlirRewriterBaseGetBlock(MlirRewriterBase rewriter) {
7070
return wrap(unwrap(rewriter)->getBlock());
7171
}
7272

73+
MlirOperation
74+
mlirRewriterBaseGetOperationAfterInsertion(MlirRewriterBase rewriter) {
75+
mlir::RewriterBase *base = unwrap(rewriter);
76+
mlir::Block *block = base->getInsertionBlock();
77+
mlir::Block::iterator it = base->getInsertionPoint();
78+
if (it == block->end())
79+
return {nullptr};
80+
81+
return wrap(std::addressof(*it));
82+
}
83+
7384
//===----------------------------------------------------------------------===//
7485
/// Block and operation creation/insertion/cloning
7586
//===----------------------------------------------------------------------===//
@@ -317,6 +328,10 @@ inline MlirPatternRewriter wrap(mlir::PatternRewriter *rewriter) {
317328
return {rewriter};
318329
}
319330

331+
MlirRewriterBase mlirPatternRewriterAsBase(MlirPatternRewriter rewriter) {
332+
return wrap(static_cast<mlir::RewriterBase *>(unwrap(rewriter)));
333+
}
334+
320335
//===----------------------------------------------------------------------===//
321336
/// PDLPatternModule API
322337
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)