diff --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h index 0a515bbea3b50..7d2fd89e8560f 100644 --- a/mlir/include/mlir-c/IR.h +++ b/mlir/include/mlir-c/IR.h @@ -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); diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp index 53806ca9f04a4..d6c2fdd5eac98 100644 --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -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)"; @@ -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 &pyLocations, diff --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp index 24dc885404853..f27af0ca9a2c7 100644 --- a/mlir/lib/CAPI/IR/IR.cpp +++ b/mlir/lib/CAPI/IR/IR.cpp @@ -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)))); } diff --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c index 15a3a1fb50dc9..68da79f69cc0a 100644 --- a/mlir/test/CAPI/ir.c +++ b/mlir/test/CAPI/ir.c @@ -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), @@ -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) << diff --git a/mlir/test/python/ir/location.py b/mlir/test/python/ir/location.py index f66d6c501dcf5..59d8a89e770dd 100644 --- a/mlir/test/python/ir/location.py +++ b/mlir/test/python/ir/location.py @@ -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)