[Mlir-commits] [mlir] 012721d - [mlir][python] Propagate error diagnostics when an op couldn't be created. (#169499)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Nov 25 09:41:05 PST 2025


Author: Benjamin Chetioui
Date: 2025-11-25T17:41:01Z
New Revision: 012721d3200ceed635495394fe96b17bbaa8653e

URL: https://github.com/llvm/llvm-project/commit/012721d3200ceed635495394fe96b17bbaa8653e
DIFF: https://github.com/llvm/llvm-project/commit/012721d3200ceed635495394fe96b17bbaa8653e.diff

LOG: [mlir][python] Propagate error diagnostics when an op couldn't be created. (#169499)

Added: 
    

Modified: 
    mlir/lib/Bindings/Python/IRCore.cpp
    mlir/test/python/ir/operation.py

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 03b540de97d4f..2e0c2b895216f 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -1411,9 +1411,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 66ba5d28e49b2..ca99c2a985242 100644
--- a/mlir/test/python/ir/operation.py
+++ b/mlir/test/python/ir/operation.py
@@ -5,7 +5,7 @@
 from tempfile import NamedTemporaryFile
 from mlir.ir import *
 from mlir.dialects.builtin import ModuleOp
-from mlir.dialects import arith, func, scf
+from mlir.dialects import arith, func, scf, shape
 from mlir.dialects._ods_common import _cext
 from mlir.extras import types as T
 
@@ -774,6 +774,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