Skip to content

Commit d5429a1

Browse files
rkayaithftynse
authored andcommitted
[mlir][python] Add 'loc' property to ops
Add a read-only `loc` property to Operation and OpView Reviewed By: ftynse Differential Revision: https://reviews.llvm.org/D111972
1 parent 366fb53 commit d5429a1

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ MLIR_CAPI_EXPORTED bool mlirOperationEqual(MlirOperation op,
357357
/// Gets the context this operation is associated with
358358
MLIR_CAPI_EXPORTED MlirContext mlirOperationGetContext(MlirOperation op);
359359

360+
/// Gets the location of the operation.
361+
MLIR_CAPI_EXPORTED MlirLocation mlirOperationGetLocation(MlirOperation op);
362+
360363
/// Gets the type id of the operation.
361364
/// Returns null if the operation does not have a registered operation
362365
/// description.

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,15 @@ void mlir::python::populateIRCore(py::module &m) {
21432143
},
21442144
"Shortcut to get an op result if it has only one (throws an error "
21452145
"otherwise).")
2146+
.def_property_readonly(
2147+
"location",
2148+
[](PyOperationBase &self) {
2149+
PyOperation &operation = self.getOperation();
2150+
return PyLocation(operation.getContext(),
2151+
mlirOperationGetLocation(operation.get()));
2152+
},
2153+
"Returns the source location the operation was defined or derived "
2154+
"from.")
21462155
.def("__iter__",
21472156
[](PyOperationBase &self) {
21482157
return PyRegionIterator(self.getOperation().getRef());

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ MlirContext mlirOperationGetContext(MlirOperation op) {
346346
return wrap(unwrap(op)->getContext());
347347
}
348348

349+
MlirLocation mlirOperationGetLocation(MlirOperation op) {
350+
return wrap(unwrap(op)->getLoc());
351+
}
352+
349353
MlirTypeID mlirOperationGetTypeID(MlirOperation op) {
350354
if (const auto *abstractOp = unwrap(op)->getAbstractOperation()) {
351355
return wrap(abstractOp->typeID);

mlir/test/python/ir/operation.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,3 +728,15 @@ def testOperationErase():
728728

729729
# Ensure we can create another operation
730730
Operation.create("custom.op2")
731+
732+
733+
# CHECK-LABEL: TEST: testOperationLoc
734+
@run
735+
def testOperationLoc():
736+
ctx = Context()
737+
ctx.allow_unregistered_dialects = True
738+
with ctx:
739+
loc = Location.name("loc")
740+
op = Operation.create("custom.op", loc=loc)
741+
assert op.location == loc
742+
assert op.operation.location == loc

0 commit comments

Comments
 (0)