Skip to content

Commit a77250f

Browse files
authored
[mlir] Add C and Python interface for file range (#123276)
Plumbs through creating file ranges to C and Python.
1 parent 4e9d5a3 commit a77250f

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ mlirLocationFromAttribute(MlirAttribute attribute);
256256
MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet(
257257
MlirContext context, MlirStringRef filename, unsigned line, unsigned col);
258258

259+
/// Creates an File/Line/Column range location owned by the given context.
260+
MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColRangeGet(
261+
MlirContext context, MlirStringRef filename, unsigned start_line,
262+
unsigned start_col, unsigned end_line, unsigned end_col);
263+
259264
/// Creates a call site location with a callee and a caller.
260265
MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee,
261266
MlirLocation caller);

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ static const char kContextGetCallSiteLocationDocstring[] =
5050
static const char kContextGetFileLocationDocstring[] =
5151
R"(Gets a Location representing a file, line and column)";
5252

53+
static const char kContextGetFileRangeDocstring[] =
54+
R"(Gets a Location representing a file, line and column range)";
55+
5356
static const char kContextGetFusedLocationDocstring[] =
5457
R"(Gets a Location representing a fused location with optional metadata)";
5558

@@ -2917,6 +2920,18 @@ void mlir::python::populateIRCore(nb::module_ &m) {
29172920
nb::arg("filename"), nb::arg("line"), nb::arg("col"),
29182921
nb::arg("context").none() = nb::none(),
29192922
kContextGetFileLocationDocstring)
2923+
.def_static(
2924+
"file",
2925+
[](std::string filename, int startLine, int startCol, int endLine,
2926+
int endCol, DefaultingPyMlirContext context) {
2927+
return PyLocation(context->getRef(),
2928+
mlirLocationFileLineColRangeGet(
2929+
context->get(), toMlirStringRef(filename),
2930+
startLine, startCol, endLine, endCol));
2931+
},
2932+
nb::arg("filename"), nb::arg("start_line"), nb::arg("start_col"),
2933+
nb::arg("end_line"), nb::arg("end_col"),
2934+
nb::arg("context").none() = nb::none(), kContextGetFileRangeDocstring)
29202935
.def_static(
29212936
"fused",
29222937
[](const std::vector<PyLocation> &pyLocations,

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@ MlirLocation mlirLocationFileLineColGet(MlirContext context,
264264
FileLineColLoc::get(unwrap(context), unwrap(filename), line, col)));
265265
}
266266

267+
MlirLocation
268+
mlirLocationFileLineColRangeGet(MlirContext context, MlirStringRef filename,
269+
unsigned startLine, unsigned startCol,
270+
unsigned endLine, unsigned endCol) {
271+
return wrap(
272+
Location(FileLineColRange::get(unwrap(context), unwrap(filename),
273+
startLine, startCol, endLine, endCol)));
274+
}
275+
267276
MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
268277
return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
269278
}

mlir/test/CAPI/ir.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,6 +2389,9 @@ void testDiagnostics(void) {
23892389
MlirLocation fileLineColLoc = mlirLocationFileLineColGet(
23902390
ctx, mlirStringRefCreateFromCString("file.c"), 1, 2);
23912391
mlirEmitError(fileLineColLoc, "test diagnostics");
2392+
MlirLocation fileLineColRange = mlirLocationFileLineColRangeGet(
2393+
ctx, mlirStringRefCreateFromCString("other-file.c"), 1, 2, 3, 4);
2394+
mlirEmitError(fileLineColRange, "test diagnostics");
23922395
MlirLocation callSiteLoc = mlirLocationCallSiteGet(
23932396
mlirLocationFileLineColGet(
23942397
ctx, mlirStringRefCreateFromCString("other-file.c"), 2, 3),
@@ -2418,6 +2421,10 @@ void testDiagnostics(void) {
24182421
// CHECK: >> end of diagnostic (userData: 42)
24192422
// CHECK: processing diagnostic (userData: 42) <<
24202423
// CHECK: test diagnostics
2424+
// CHECK: loc("other-file.c":1:2 to 3:4)
2425+
// CHECK: >> end of diagnostic (userData: 42)
2426+
// CHECK: processing diagnostic (userData: 42) <<
2427+
// CHECK: test diagnostics
24212428
// CHECK: loc(callsite("other-file.c":2:3 at "file.c":1:2))
24222429
// CHECK: >> end of diagnostic (userData: 42)
24232430
// CHECK: processing diagnostic (userData: 42) <<

mlir/test/python/ir/location.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,17 @@ def testLocationAttr():
4747
def testFileLineCol():
4848
with Context() as ctx:
4949
loc = Location.file("foo.txt", 123, 56)
50+
range = Location.file("foo.txt", 123, 56, 123, 100)
5051
ctx = None
5152
gc.collect()
5253
# CHECK: file str: loc("foo.txt":123:56)
5354
print("file str:", str(loc))
5455
# CHECK: file repr: loc("foo.txt":123:56)
5556
print("file repr:", repr(loc))
57+
# CHECK: file range str: loc("foo.txt":123:56 to :100)
58+
print("file range str:", str(range))
59+
# CHECK: file range repr: loc("foo.txt":123:56 to :100)
60+
print("file range repr:", repr(range))
5661

5762

5863
run(testFileLineCol)

0 commit comments

Comments
 (0)