Skip to content

Commit 1fee111

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

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-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());

0 commit comments

Comments
 (0)