Skip to content

Commit

Permalink
transformations: (mlir-opt) separate mlir-opt pass errors (#3687)
Browse files Browse the repository at this point in the history
We currently have two statements that can raise an error when calling
out to `mlir-opt`, one for errors the executable raises, the other for
parsing in xDSL, these should be reported separately.
  • Loading branch information
superlopuh authored Jan 6, 2025
1 parent 1954542 commit 87308a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
5 changes: 3 additions & 2 deletions tests/filecheck/mlir-conversion/with-mlir/mlir_opt_fail.mlir
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -10,4 +10,5 @@
}) : () -> ()


// CHECK: Error executing mlir-opt pass
// CHECK-PARSING: Error parsing mlir-opt pass output
// CHECK: Error executing mlir-opt pass
26 changes: 12 additions & 14 deletions xdsl/transforms/mlir_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 87308a1

Please sign in to comment.