Skip to content

Commit 012721d

Browse files
authored
[mlir][python] Propagate error diagnostics when an op couldn't be created. (#169499)
1 parent 83d9c63 commit 012721d

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,9 +1411,10 @@ nb::object PyOperation::create(std::string_view name,
14111411
}
14121412

14131413
// Construct the operation.
1414+
PyMlirContext::ErrorCapture errors(location.getContext());
14141415
MlirOperation operation = mlirOperationCreate(&state);
14151416
if (!operation.ptr)
1416-
throw nb::value_error("Operation creation failed");
1417+
throw MLIRError("Operation creation failed", errors.take());
14171418
PyOperationRef created =
14181419
PyOperation::createDetached(location.getContext(), operation);
14191420
maybeInsertOperation(created, maybeIp);

mlir/test/python/ir/operation.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from tempfile import NamedTemporaryFile
66
from mlir.ir import *
77
from mlir.dialects.builtin import ModuleOp
8-
from mlir.dialects import arith, func, scf
8+
from mlir.dialects import arith, func, scf, shape
99
from mlir.dialects._ods_common import _cext
1010
from mlir.extras import types as T
1111

@@ -774,6 +774,21 @@ def __init__(self, result, value, *, loc=None, ip=None):
774774
print(repr(constant))
775775

776776

777+
# CHECK-LABEL: TEST: testFailedGenericOperationCreationReportsError
778+
@run
779+
def testFailedGenericOperationCreationReportsError():
780+
with Context(), Location.unknown():
781+
c0 = shape.const_shape([])
782+
c1 = shape.const_shape([1, 2, 3])
783+
try:
784+
shape.MeetOp.build_generic(operands=[c0, c1])
785+
except MLIRError as e:
786+
# CHECK: unequal shape cardinality
787+
print(e)
788+
else:
789+
assert False, "Expected exception"
790+
791+
777792
# CHECK-LABEL: TEST: testSingleResultProperty
778793
@run
779794
def testSingleResultProperty():

0 commit comments

Comments
 (0)