[Mlir-commits] [mlir] [mlir][Python] Downcast location returned from diagnostic (PR #201337)
Martin Erhart
llvmlistbot at llvm.org
Wed Jun 3 05:17:44 PDT 2026
https://github.com/maerhart updated https://github.com/llvm/llvm-project/pull/201337
>From 44c8ccc7a5303975c48cc97066008bf8ca0c624e Mon Sep 17 00:00:00 2001
From: Martin Erhart <martin.erhart at sifive.com>
Date: Wed, 3 Jun 2026 13:04:47 +0100
Subject: [PATCH 1/2] [mlir][Python] Downcast location returned from diagnostic
---
mlir/include/mlir/Bindings/Python/IRCore.h | 2 +-
mlir/lib/Bindings/Python/IRCore.cpp | 8 ++++----
mlir/test/python/ir/location.py | 23 ++++++++++++++++++++++
3 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/mlir/include/mlir/Bindings/Python/IRCore.h b/mlir/include/mlir/Bindings/Python/IRCore.h
index 0b758d4061d67..1b8c232b009c0 100644
--- a/mlir/include/mlir/Bindings/Python/IRCore.h
+++ b/mlir/include/mlir/Bindings/Python/IRCore.h
@@ -375,7 +375,7 @@ class MLIR_PYTHON_API_EXPORTED PyDiagnostic {
void invalidate();
bool isValid() { return valid; }
PyDiagnosticSeverity getSeverity();
- PyLocation getLocation();
+ nanobind::typed<nanobind::object, PyLocation> getLocation();
nanobind::str getMessage();
nanobind::typed<nanobind::tuple, PyDiagnostic> getNotes();
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index f92d4c14ceb16..92e9ecf3f2c20 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -754,11 +754,11 @@ PyDiagnosticSeverity PyDiagnostic::getSeverity() {
mlirDiagnosticGetSeverity(diagnostic));
}
-PyLocation PyDiagnostic::getLocation() {
+nb::typed<nb::object, PyLocation> PyDiagnostic::getLocation() {
checkValid();
MlirLocation loc = mlirDiagnosticGetLocation(diagnostic);
MlirContext context = mlirLocationGetContext(loc);
- return PyLocation(PyMlirContext::forContext(context), loc);
+ return PyLocation(PyMlirContext::forContext(context), loc).maybeDownCast();
}
nb::str PyDiagnostic::getMessage() {
@@ -789,8 +789,8 @@ PyDiagnostic::DiagnosticInfo PyDiagnostic::getInfo() {
std::vector<DiagnosticInfo> notes;
for (nb::handle n : getNotes())
notes.emplace_back(nb::cast<PyDiagnostic>(n).getInfo());
- return {getSeverity(), getLocation(), nb::cast<std::string>(getMessage()),
- std::move(notes)};
+ return {getSeverity(), nb::cast<PyLocation>(getLocation()),
+ nb::cast<std::string>(getMessage()), std::move(notes)};
}
//------------------------------------------------------------------------------
diff --git a/mlir/test/python/ir/location.py b/mlir/test/python/ir/location.py
index 33a4ffce48b9c..579aa9fa7c7c3 100644
--- a/mlir/test/python/ir/location.py
+++ b/mlir/test/python/ir/location.py
@@ -292,3 +292,26 @@ def testLocationCapsule():
run(testLocationCapsule)
+
+
+# CHECK-LABEL: TEST: testLocationFromDiagnostic
+def testLocationFromDiagnostic():
+ with Context() as ctx:
+ def callback(d):
+ assert isinstance(d.location, FileLineColLoc)
+ assert isinstance(d.location, Location)
+
+ # CHECK: filename: diagnostic_test.txt
+ print("filename:", d.location.filename)
+ # CHECK: line: 42
+ print("line:", d.location.start_line)
+ # CHECK: col: 7
+ print("col:", d.location.start_col)
+ return True
+
+ ctx.attach_diagnostic_handler(callback)
+ loc = FileLineColLoc.get("diagnostic_test.txt", 42, 7)
+ loc.emit_error("test error message")
+
+
+run(testLocationFromDiagnostic)
>From acd4b2543139f883588c924ac41eccb6a4bfce83 Mon Sep 17 00:00:00 2001
From: Martin Erhart <martin.erhart at sifive.com>
Date: Wed, 3 Jun 2026 13:17:13 +0100
Subject: [PATCH 2/2] formatting
---
mlir/test/python/ir/location.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/mlir/test/python/ir/location.py b/mlir/test/python/ir/location.py
index 579aa9fa7c7c3..3a9d98e1f7214 100644
--- a/mlir/test/python/ir/location.py
+++ b/mlir/test/python/ir/location.py
@@ -297,6 +297,7 @@ def testLocationCapsule():
# CHECK-LABEL: TEST: testLocationFromDiagnostic
def testLocationFromDiagnostic():
with Context() as ctx:
+
def callback(d):
assert isinstance(d.location, FileLineColLoc)
assert isinstance(d.location, Location)
More information about the Mlir-commits
mailing list