Skip to content

Commit

Permalink
[MLIR] Add LocationAttr to the Python API
Browse files Browse the repository at this point in the history
This is a follow up to D142182, to expose LocationAttrs through Python.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D142522
  • Loading branch information
youngar committed Jan 26, 2023
1 parent e13179d commit 792f3c8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mlir/lib/Bindings/Python/IRCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2546,10 +2546,25 @@ void mlir::python::populateIRCore(py::module &m) {
},
py::arg("name"), py::arg("childLoc") = py::none(),
py::arg("context") = py::none(), kContextGetNameLocationDocString)
.def_static(
"from_attr",
[](PyAttribute &attribute, DefaultingPyMlirContext context) {
return PyLocation(context->getRef(),
mlirLocationFromAttribute(attribute));
},
py::arg("attribute"), py::arg("context") = py::none(),
"Gets a Location from a LocationAttr")
.def_property_readonly(
"context",
[](PyLocation &self) { return self.getContext().getObject(); },
"Context that owns the Location")
.def_property_readonly(
"attr",
[](PyLocation &self) {
return PyAttribute(self.getContext(),
mlirLocationGetAttribute(self));
},
"Get the underlying LocationAttr")
.def(
"emit_error",
[](PyLocation &self, std::string message) {
Expand Down
15 changes: 15 additions & 0 deletions mlir/test/python/ir/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ def testUnknown():
run(testUnknown)


# CHECK-LABEL: TEST: testLocationAttr
def testLocationAttr():
with Context() as ctxt:
loc = Location.unknown()
attr = loc.get_attr()
clone = Location.from_attr(attr)
gc.collect()
# CHECK: loc: loc(unknown)
print("loc:", str(loc))
# CHECK: clone: loc(unknown)
print("clone:", str(clone))
assert loc == clone

run(testLocationAttr)

# CHECK-LABEL: TEST: testFileLineCol
def testFileLineCol():
with Context() as ctx:
Expand Down

0 comments on commit 792f3c8

Please sign in to comment.