[Mlir-commits] [mlir] [mlir][python] Propagate error diagnostics when an op couldn't be created. (PR #169499)
Benjamin Chetioui
llvmlistbot at llvm.org
Tue Nov 25 09:04:32 PST 2025
https://github.com/bchetioui updated https://github.com/llvm/llvm-project/pull/169499
>From a6e5231b5072a0638c5e9a02d64f59247524d267 Mon Sep 17 00:00:00 2001
From: Benjamin Chetioui <bchetioui at google.com>
Date: Tue, 25 Nov 2025 14:33:20 +0000
Subject: [PATCH] [mlir][python] Propagate error diagnostics when an op
couldn't be created.
---
mlir/lib/Bindings/Python/IRCore.cpp | 3 ++-
mlir/test/python/ir/operation.py | 16 ++++++++++++++++
2 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index b5720b7ad8b21..fd091eb8501a3 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -1608,9 +1608,10 @@ nb::object PyOperation::create(std::string_view name,
}
// Construct the operation.
+ PyMlirContext::ErrorCapture errors(location.getContext());
MlirOperation operation = mlirOperationCreate(&state);
if (!operation.ptr)
- throw nb::value_error("Operation creation failed");
+ throw MLIRError("Operation creation failed", errors.take());
PyOperationRef created =
PyOperation::createDetached(location->getContext(), operation);
maybeInsertOperation(created, maybeIp);
diff --git a/mlir/test/python/ir/operation.py b/mlir/test/python/ir/operation.py
index b08fe98397fbc..b2a7f1314cc9c 100644
--- a/mlir/test/python/ir/operation.py
+++ b/mlir/test/python/ir/operation.py
@@ -7,6 +7,7 @@
from mlir.ir import *
from mlir.dialects.builtin import ModuleOp
from mlir.dialects import arith
+from mlir.dialects import shape
from mlir.dialects._ods_common import _cext
@@ -746,6 +747,21 @@ def __init__(self, result, value, *, loc=None, ip=None):
print(repr(constant))
+# CHECK-LABEL: TEST: testFailedGenericOperationCreationReportsError
+ at run
+def testFailedGenericOperationCreationReportsError():
+ with Context(), Location.unknown():
+ c0 = shape.const_shape([])
+ c1 = shape.const_shape([1, 2, 3])
+ try:
+ shape.MeetOp.build_generic(operands=[c0, c1])
+ except MLIRError as e:
+ # CHECK: unequal shape cardinality
+ print(e)
+ else:
+ assert False, "Expected exception"
+
+
# CHECK-LABEL: TEST: testSingleResultProperty
@run
def testSingleResultProperty():
More information about the Mlir-commits
mailing list