[mlir][python] Add nameloc getter

Expose the nameloc getter to Python API.

Differential Revision: https://reviews.llvm.org/D111663
This commit is contained in:
Jacques Pienaar 2021-10-12 12:45:57 -07:00
parent 357b8d7ddb
commit 04d76d3694
2 changed files with 35 additions and 0 deletions

View File

@ -43,6 +43,9 @@ See also: https://mlir.llvm.org/docs/LangRef/#type-system
static const char kContextGetFileLocationDocstring[] =
R"(Gets a Location representing a file, line and column)";
static const char kContextGetNameLocationDocString[] =
R"(Gets a Location representing a named location with optional child location)";
static const char kModuleParseDocstring[] =
R"(Parses a module's assembly format from a string.
@ -1970,6 +1973,19 @@ void mlir::python::populateIRCore(py::module &m) {
},
py::arg("filename"), py::arg("line"), py::arg("col"),
py::arg("context") = py::none(), kContextGetFileLocationDocstring)
.def_static(
"name",
[](std::string name, llvm::Optional<PyLocation> childLoc,
DefaultingPyMlirContext context) {
return PyLocation(
context->getRef(),
mlirLocationNameGet(
context->get(), toMlirStringRef(name),
childLoc ? childLoc->get()
: mlirLocationUnknownGet(context->get())));
},
py::arg("name"), py::arg("childLoc") = py::none(),
py::arg("context") = py::none(), kContextGetNameLocationDocString)
.def_property_readonly(
"context",
[](PyLocation &self) { return self.getContext().getObject(); },

View File

@ -39,6 +39,25 @@ def testFileLineCol():
run(testFileLineCol)
# CHECK-LABEL: TEST: testName
def testName():
with Context() as ctx:
loc = Location.name("nombre")
locWithChildLoc = Location.name("naam", loc)
ctx = None
gc.collect()
# CHECK: file str: loc("nombre")
print("file str:", str(loc))
# CHECK: file repr: loc("nombre")
print("file repr:", repr(loc))
# CHECK: file str: loc("naam"("nombre"))
print("file str:", str(locWithChildLoc))
# CHECK: file repr: loc("naam"("nombre"))
print("file repr:", repr(locWithChildLoc))
run(testName)
# CHECK-LABEL: TEST: testLocationCapsule
def testLocationCapsule():
with Context() as ctx: