Skip to content

Commit fea2cca

Browse files
authored
[MLIR][Python] expose Operation::setLoc (#161594)
1 parent a88c839 commit fea2cca

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,10 @@ MLIR_CAPI_EXPORTED MlirContext mlirOperationGetContext(MlirOperation op);
634634
/// Gets the location of the operation.
635635
MLIR_CAPI_EXPORTED MlirLocation mlirOperationGetLocation(MlirOperation op);
636636

637+
/// Sets the location of the operation.
638+
MLIR_CAPI_EXPORTED void mlirOperationSetLocation(MlirOperation op,
639+
MlirLocation loc);
640+
637641
/// Gets the type id of the operation.
638642
/// Returns null if the operation does not have a registered operation
639643
/// description.

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,15 +3485,21 @@ void mlir::python::populateIRCore(nb::module_ &m) {
34853485
},
34863486
"Shortcut to get an op result if it has only one (throws an error "
34873487
"otherwise).")
3488-
.def_prop_ro(
3488+
.def_prop_rw(
34893489
"location",
34903490
[](PyOperationBase &self) {
34913491
PyOperation &operation = self.getOperation();
34923492
return PyLocation(operation.getContext(),
34933493
mlirOperationGetLocation(operation.get()));
34943494
},
3495-
"Returns the source location the operation was defined or derived "
3496-
"from.")
3495+
[](PyOperationBase &self, const PyLocation &location) {
3496+
PyOperation &operation = self.getOperation();
3497+
mlirOperationSetLocation(operation.get(), location.get());
3498+
},
3499+
nb::for_getter("Returns the source location the operation was "
3500+
"defined or derived from."),
3501+
nb::for_setter("Sets the source location the operation was defined "
3502+
"or derived from."))
34973503
.def_prop_ro("parent",
34983504
[](PyOperationBase &self)
34993505
-> std::optional<nb::typed<nb::object, PyOperation>> {

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,10 @@ MlirLocation mlirOperationGetLocation(MlirOperation op) {
656656
return wrap(unwrap(op)->getLoc());
657657
}
658658

659+
void mlirOperationSetLocation(MlirOperation op, MlirLocation loc) {
660+
unwrap(op)->setLoc(unwrap(loc));
661+
}
662+
659663
MlirTypeID mlirOperationGetTypeID(MlirOperation op) {
660664
if (auto info = unwrap(op)->getRegisteredInfo())
661665
return wrap(info->getTypeID());

mlir/test/python/ir/operation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ def testOperationPrint():
696696
# CHECK: resource1: "0x08
697697
module.operation.print(large_elements_limit=2)
698698

699+
699700
# CHECK-LABEL: TEST: testKnownOpView
700701
@run
701702
def testKnownOpView():
@@ -969,6 +970,13 @@ def testOperationLoc():
969970
assert op.location == loc
970971
assert op.operation.location == loc
971972

973+
another_loc = Location.name("another_loc")
974+
op.location = another_loc
975+
assert op.location == another_loc
976+
assert op.operation.location == another_loc
977+
# CHECK: loc("another_loc")
978+
print(op.location)
979+
972980

973981
# CHECK-LABEL: TEST: testModuleMerge
974982
@run

0 commit comments

Comments
 (0)