Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mlir/include/mlir-c/IR.h
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,10 @@ MLIR_CAPI_EXPORTED MlirContext mlirOperationGetContext(MlirOperation op);
/// Gets the location of the operation.
MLIR_CAPI_EXPORTED MlirLocation mlirOperationGetLocation(MlirOperation op);

/// Sets the location of the operation.
MLIR_CAPI_EXPORTED void mlirOperationSetLocation(MlirOperation op,
MlirLocation loc);

/// Gets the type id of the operation.
/// Returns null if the operation does not have a registered operation
/// description.
Expand Down
12 changes: 9 additions & 3 deletions mlir/lib/Bindings/Python/IRCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3485,15 +3485,21 @@ void mlir::python::populateIRCore(nb::module_ &m) {
},
"Shortcut to get an op result if it has only one (throws an error "
"otherwise).")
.def_prop_ro(
.def_prop_rw(
"location",
[](PyOperationBase &self) {
PyOperation &operation = self.getOperation();
return PyLocation(operation.getContext(),
mlirOperationGetLocation(operation.get()));
},
"Returns the source location the operation was defined or derived "
"from.")
[](PyOperationBase &self, const PyLocation &location) {
PyOperation &operation = self.getOperation();
mlirOperationSetLocation(operation.get(), location.get());
},
nb::for_getter("Returns the source location the operation was "
"defined or derived from."),
nb::for_setter("Sets the source location the operation was defined "
"or derived from."))
.def_prop_ro("parent",
[](PyOperationBase &self)
-> std::optional<nb::typed<nb::object, PyOperation>> {
Expand Down
4 changes: 4 additions & 0 deletions mlir/lib/CAPI/IR/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,10 @@ MlirLocation mlirOperationGetLocation(MlirOperation op) {
return wrap(unwrap(op)->getLoc());
}

void mlirOperationSetLocation(MlirOperation op, MlirLocation loc) {
unwrap(op)->setLoc(unwrap(loc));
}

MlirTypeID mlirOperationGetTypeID(MlirOperation op) {
if (auto info = unwrap(op)->getRegisteredInfo())
return wrap(info->getTypeID());
Expand Down
8 changes: 8 additions & 0 deletions mlir/test/python/ir/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ def testOperationPrint():
# CHECK: resource1: "0x08
module.operation.print(large_elements_limit=2)


# CHECK-LABEL: TEST: testKnownOpView
@run
def testKnownOpView():
Expand Down Expand Up @@ -969,6 +970,13 @@ def testOperationLoc():
assert op.location == loc
assert op.operation.location == loc

another_loc = Location.name("another_loc")
op.location = another_loc
assert op.location == another_loc
assert op.operation.location == another_loc
# CHECK: loc("another_loc")
print(op.location)


# CHECK-LABEL: TEST: testModuleMerge
@run
Expand Down