Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 0772e08

Browse files
authored
[mlir] Add C and Python interface for file range (#123276)
Plumbs through creating file ranges to C and Python.
1 parent 96a8a80 commit 0772e08

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-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
}

0 commit comments

Comments
 (0)