diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -43,6 +43,9 @@ 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 @@ }, py::arg("filename"), py::arg("line"), py::arg("col"), py::arg("context") = py::none(), kContextGetFileLocationDocstring) + .def_static( + "name", + [](std::string name, llvm::Optional 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(); }, diff --git a/mlir/test/python/ir/location.py b/mlir/test/python/ir/location.py --- a/mlir/test/python/ir/location.py +++ b/mlir/test/python/ir/location.py @@ -39,6 +39,25 @@ 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: