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

This commit is contained in:
Benjamin Chetioui
2025-11-25 18:41:01 +01:00
committed by GitHub
parent 83d9c636b7
commit 012721d320
2 changed files with 18 additions and 2 deletions

View File

@@ -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);

View File

@@ -5,7 +5,7 @@ import io
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 testKnownOpView():
print(repr(constant))
# CHECK-LABEL: TEST: testFailedGenericOperationCreationReportsError
@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():