xref: /llvm-project/mlir/test/python/ir/exception.py (revision f9008e6366c2496b1ca1785b891d5578174ad63e)
1# RUN: %PYTHON %s | FileCheck %s
2
3import gc
4from mlir.ir import *
5
6
7def run(f):
8    print("\nTEST:", f.__name__)
9    f()
10    gc.collect()
11    assert Context._get_live_count() == 0
12    return f
13
14
15# CHECK-LABEL: TEST: test_exception
16@run
17def test_exception():
18    ctx = Context()
19    ctx.allow_unregistered_dialects = True
20    try:
21        Operation.parse(
22            """
23      func.func @foo() {
24          "test.use"(%0) : (i64) -> ()  loc("use")
25          %0 = "test.def"() : () -> i64 loc("def")
26          return
27      }
28    """,
29            context=ctx,
30        )
31    except MLIRError as e:
32        # CHECK: Exception: <
33        # CHECK:   Unable to parse operation assembly:
34        # CHECK:   error: "use": operand #0 does not dominate this use
35        # CHECK:    note: "use": see current operation: "test.use"(%0) : (i64) -> ()
36        # CHECK:    note: "def": operand defined here (op in the same block)
37        # CHECK: >
38        print(f"Exception: <{e}>")
39
40        # CHECK: message: Unable to parse operation assembly
41        print(f"message: {e.message}")
42
43        # CHECK: error_diagnostics[0]:           loc("use") operand #0 does not dominate this use
44        # CHECK: error_diagnostics[0].notes[0]:  loc("use") see current operation: "test.use"(%0) : (i64) -> ()
45        # CHECK: error_diagnostics[0].notes[1]:  loc("def") operand defined here (op in the same block)
46        print(
47            "error_diagnostics[0]:          ",
48            e.error_diagnostics[0].location,
49            e.error_diagnostics[0].message,
50        )
51        print(
52            "error_diagnostics[0].notes[0]: ",
53            e.error_diagnostics[0].notes[0].location,
54            e.error_diagnostics[0].notes[0].message,
55        )
56        print(
57            "error_diagnostics[0].notes[1]: ",
58            e.error_diagnostics[0].notes[1].location,
59            e.error_diagnostics[0].notes[1].message,
60        )
61
62
63# CHECK-LABEL: test_emit_error_diagnostics
64@run
65def test_emit_error_diagnostics():
66    ctx = Context()
67    loc = Location.unknown(ctx)
68    handler_diags = []
69
70    def handler(d):
71        handler_diags.append(str(d))
72        return True
73
74    ctx.attach_diagnostic_handler(handler)
75
76    try:
77        Attribute.parse("not an attr", ctx)
78    except MLIRError as e:
79        # CHECK: emit_error_diagnostics=False:
80        # CHECK: e.error_diagnostics: ['expected attribute value']
81        # CHECK: handler_diags: []
82        print(f"emit_error_diagnostics=False:")
83        print(f"e.error_diagnostics: {[str(diag) for diag in e.error_diagnostics]}")
84        print(f"handler_diags: {handler_diags}")
85
86    ctx.emit_error_diagnostics = True
87    try:
88        Attribute.parse("not an attr", ctx)
89    except MLIRError as e:
90        # CHECK: emit_error_diagnostics=True:
91        # CHECK: e.error_diagnostics: []
92        # CHECK: handler_diags: ['expected attribute value']
93        print(f"emit_error_diagnostics=True:")
94        print(f"e.error_diagnostics: {[str(diag) for diag in e.error_diagnostics]}")
95        print(f"handler_diags: {handler_diags}")
96