Skip to content

[mlir] Add C and Python interface for file range #123276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 22, 2025
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
5 changes: 5 additions & 0 deletions mlir/include/mlir-c/IR.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ mlirLocationFromAttribute(MlirAttribute attribute);
MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet(
MlirContext context, MlirStringRef filename, unsigned line, unsigned col);

/// Creates an File/Line/Column range location owned by the given context.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColRangeGet(
MlirContext context, MlirStringRef filename, unsigned start_line,
unsigned start_col, unsigned end_line, unsigned end_col);

/// Creates a call site location with a callee and a caller.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee,
MlirLocation caller);
Expand Down
15 changes: 15 additions & 0 deletions mlir/lib/Bindings/Python/IRCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ static const char kContextGetCallSiteLocationDocstring[] =
static const char kContextGetFileLocationDocstring[] =
R"(Gets a Location representing a file, line and column)";

static const char kContextGetFileRangeDocstring[] =
R"(Gets a Location representing a file, line and column range)";

static const char kContextGetFusedLocationDocstring[] =
R"(Gets a Location representing a fused location with optional metadata)";

Expand Down Expand Up @@ -2902,6 +2905,18 @@ void mlir::python::populateIRCore(nb::module_ &m) {
nb::arg("filename"), nb::arg("line"), nb::arg("col"),
nb::arg("context").none() = nb::none(),
kContextGetFileLocationDocstring)
.def_static(
"file",
[](std::string filename, int startLine, int startCol, int endLine,
int endCol, DefaultingPyMlirContext context) {
return PyLocation(context->getRef(),
mlirLocationFileLineColRangeGet(
context->get(), toMlirStringRef(filename),
startLine, startCol, endLine, endCol));
},
nb::arg("filename"), nb::arg("start_line"), nb::arg("start_col"),
nb::arg("end_line"), nb::arg("end_col"),
nb::arg("context").none() = nb::none(), kContextGetFileRangeDocstring)
.def_static(
"fused",
[](const std::vector<PyLocation> &pyLocations,
Expand Down
9 changes: 9 additions & 0 deletions mlir/lib/CAPI/IR/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ MlirLocation mlirLocationFileLineColGet(MlirContext context,
FileLineColLoc::get(unwrap(context), unwrap(filename), line, col)));
}

MlirLocation
mlirLocationFileLineColRangeGet(MlirContext context, MlirStringRef filename,
unsigned startLine, unsigned startCol,
unsigned endLine, unsigned endCol) {
return wrap(
Location(FileLineColRange::get(unwrap(context), unwrap(filename),
startLine, startCol, endLine, endCol)));
}

MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
}
Expand Down
7 changes: 7 additions & 0 deletions mlir/test/CAPI/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -2389,6 +2389,9 @@ void testDiagnostics(void) {
MlirLocation fileLineColLoc = mlirLocationFileLineColGet(
ctx, mlirStringRefCreateFromCString("file.c"), 1, 2);
mlirEmitError(fileLineColLoc, "test diagnostics");
MlirLocation fileLineColRange = mlirLocationFileLineColRangeGet(
ctx, mlirStringRefCreateFromCString("other-file.c"), 1, 2, 3, 4);
mlirEmitError(fileLineColRange, "test diagnostics");
MlirLocation callSiteLoc = mlirLocationCallSiteGet(
mlirLocationFileLineColGet(
ctx, mlirStringRefCreateFromCString("other-file.c"), 2, 3),
Expand Down Expand Up @@ -2418,6 +2421,10 @@ void testDiagnostics(void) {
// CHECK: >> end of diagnostic (userData: 42)
// CHECK: processing diagnostic (userData: 42) <<
// CHECK: test diagnostics
// CHECK: loc("other-file.c":1:2 to 3:4)
// CHECK: >> end of diagnostic (userData: 42)
// CHECK: processing diagnostic (userData: 42) <<
// CHECK: test diagnostics
// CHECK: loc(callsite("other-file.c":2:3 at "file.c":1:2))
// CHECK: >> end of diagnostic (userData: 42)
// CHECK: processing diagnostic (userData: 42) <<
Expand Down
5 changes: 5 additions & 0 deletions mlir/test/python/ir/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ def testLocationAttr():
def testFileLineCol():
with Context() as ctx:
loc = Location.file("foo.txt", 123, 56)
range = Location.file("foo.txt", 123, 56, 123, 100)
ctx = None
gc.collect()
# CHECK: file str: loc("foo.txt":123:56)
print("file str:", str(loc))
# CHECK: file repr: loc("foo.txt":123:56)
print("file repr:", repr(loc))
# CHECK: file range str: loc("foo.txt":123:56 to :100)
print("file range str:", str(range))
# CHECK: file range repr: loc("foo.txt":123:56 to :100)
print("file range repr:", repr(range))


run(testFileLineCol)
Expand Down
Loading