Skip to content

Commit 55d2fff

Browse files
authored
[mlir][python]Python Bindings for select edit operations on Block arguments (#94305)
The PR implements MLIR Python Bindings for a few simple edit operations on Block arguments, namely, `add_argument`, `erase_argument`, and `erase_arguments`.
1 parent 53ddc87 commit 55d2fff

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,9 @@ MLIR_CAPI_EXPORTED MlirValue mlirBlockAddArgument(MlirBlock block,
858858
MlirType type,
859859
MlirLocation loc);
860860

861+
/// Erase the argument at 'index' and remove it from the argument list.
862+
MLIR_CAPI_EXPORTED void mlirBlockEraseArgument(MlirBlock block, unsigned index);
863+
861864
/// Inserts an argument of the specified type at a specified index to the block.
862865
/// Returns the newly added argument.
863866
MLIR_CAPI_EXPORTED MlirValue mlirBlockInsertArgument(MlirBlock block,

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,6 +3238,19 @@ void mlir::python::populateIRCore(py::module &m) {
32383238
return PyBlockArgumentList(self.getParentOperation(), self.get());
32393239
},
32403240
"Returns a list of block arguments.")
3241+
.def(
3242+
"add_argument",
3243+
[](PyBlock &self, const PyType &type, const PyLocation &loc) {
3244+
return mlirBlockAddArgument(self.get(), type, loc);
3245+
},
3246+
"Append an argument of the specified type to the block and returns "
3247+
"the newly added argument.")
3248+
.def(
3249+
"erase_argument",
3250+
[](PyBlock &self, unsigned index) {
3251+
return mlirBlockEraseArgument(self.get(), index);
3252+
},
3253+
"Erase the argument at 'index' and remove it from the argument list.")
32413254
.def_property_readonly(
32423255
"operations",
32433256
[](PyBlock &self) {

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,10 @@ MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type,
906906
return wrap(unwrap(block)->addArgument(unwrap(type), unwrap(loc)));
907907
}
908908

909+
void mlirBlockEraseArgument(MlirBlock block, unsigned index) {
910+
return unwrap(block)->eraseArgument(index);
911+
}
912+
909913
MlirValue mlirBlockInsertArgument(MlirBlock block, intptr_t pos, MlirType type,
910914
MlirLocation loc) {
911915
return wrap(unwrap(block)->insertArgument(pos, unwrap(type), unwrap(loc)));

mlir/test/python/ir/blocks.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,35 @@ def testBlockHash():
145145
block1 = Block.create_at_start(dummy.operation.regions[0], [f32])
146146
block2 = Block.create_at_start(dummy.operation.regions[0], [f32])
147147
assert hash(block1) != hash(block2)
148+
149+
150+
# CHECK-LABEL: TEST: testBlockAddArgs
151+
@run
152+
def testBlockAddArgs():
153+
with Context() as ctx, Location.unknown(ctx) as loc:
154+
ctx.allow_unregistered_dialects = True
155+
f32 = F32Type.get()
156+
op = Operation.create("test", regions=1, loc=Location.unknown())
157+
blocks = op.regions[0].blocks
158+
blocks.append()
159+
# CHECK: ^bb0:
160+
op.print(enable_debug_info=True)
161+
blocks[0].add_argument(f32, loc)
162+
# CHECK: ^bb0(%{{.+}}: f32 loc(unknown)):
163+
op.print(enable_debug_info=True)
164+
165+
166+
# CHECK-LABEL: TEST: testBlockEraseArgs
167+
@run
168+
def testBlockEraseArgs():
169+
with Context() as ctx, Location.unknown(ctx) as loc:
170+
ctx.allow_unregistered_dialects = True
171+
f32 = F32Type.get()
172+
op = Operation.create("test", regions=1, loc=Location.unknown())
173+
blocks = op.regions[0].blocks
174+
blocks.append(f32)
175+
# CHECK: ^bb0(%{{.+}}: f32 loc(unknown)):
176+
op.print(enable_debug_info=True)
177+
blocks[0].erase_argument(0)
178+
# CHECK: ^bb0:
179+
op.print(enable_debug_info=True)

0 commit comments

Comments
 (0)