diff --git a/tests/filecheck/mlir-conversion/with-mlir/mlir_opt_fail.mlir b/tests/filecheck/mlir-conversion/with-mlir/mlir_opt_fail.mlir index 6b05b4c426..1762948713 100644 --- a/tests/filecheck/mlir-conversion/with-mlir/mlir_opt_fail.mlir +++ b/tests/filecheck/mlir-conversion/with-mlir/mlir_opt_fail.mlir @@ -1,4 +1,4 @@ -// RUN: xdsl-opt %s -p mlir-opt{arguments='--hello','--mlir-print-op-generic'} --print-op-generic --verify-diagnostics | filecheck %s +// RUN: xdsl-opt %s -p mlir-opt{arguments='--hello','--mlir-print-op-generic'} --print-op-generic --verify-diagnostics | filecheck %s --check-prefix=CHECK-PARSING // RUN: xdsl-opt %s -p mlir-opt[this-probably-will-never-be-an-MLIR-pass-name] --print-op-generic --verify-diagnostics | filecheck %s // RUN: xdsl-opt %s -p mlir-opt{executable='"false"'} --print-op-generic --verify-diagnostics | filecheck %s @@ -10,4 +10,5 @@ }) : () -> () -// CHECK: Error executing mlir-opt pass +// CHECK-PARSING: Error parsing mlir-opt pass output +// CHECK: Error executing mlir-opt pass diff --git a/xdsl/transforms/mlir_opt.py b/xdsl/transforms/mlir_opt.py index 1abce10ed7..736fbf8cb6 100644 --- a/xdsl/transforms/mlir_opt.py +++ b/xdsl/transforms/mlir_opt.py @@ -9,7 +9,7 @@ from xdsl.passes import ModulePass from xdsl.printer import Printer from xdsl.rewriter import Rewriter -from xdsl.utils.exceptions import DiagnosticException +from xdsl.utils.exceptions import DiagnosticException, ParseError @dataclass(frozen=True) @@ -44,20 +44,18 @@ def apply(self, ctx: MLContext, op: ModuleOp) -> None: try: completed_process.check_returncode() + except subprocess.CalledProcessError as e: + raise DiagnosticException("Error executing mlir-opt pass") from e - # Get the stdout output - stdout_output = completed_process.stdout - - parser = Parser(ctx, stdout_output) + # Get the stdout output + stdout_output = completed_process.stdout + parser = Parser(ctx, stdout_output) + try: new_module = parser.parse_module() + except ParseError as e: + raise DiagnosticException("Error parsing mlir-opt pass output") from e - op.detach_region(op.body) - op.add_region( - Rewriter().move_region_contents_to_new_regions(new_module.body) - ) - op.attributes = new_module.attributes - except Exception as e: - raise DiagnosticException( - "Error executing mlir-opt pass:", completed_process.stderr - ) from e + op.detach_region(op.body) + op.add_region(Rewriter().move_region_contents_to_new_regions(new_module.body)) + op.attributes = new_module.attributes