13ea4c501SRahul Kayaith# RUN: %PYTHON %s | FileCheck %s 23ea4c501SRahul Kayaith 33ea4c501SRahul Kayaithimport gc 43ea4c501SRahul Kayaithfrom mlir.ir import * 53ea4c501SRahul Kayaith 6*f9008e63STobias Hieta 73ea4c501SRahul Kayaithdef run(f): 83ea4c501SRahul Kayaith print("\nTEST:", f.__name__) 93ea4c501SRahul Kayaith f() 103ea4c501SRahul Kayaith gc.collect() 113ea4c501SRahul Kayaith assert Context._get_live_count() == 0 123ea4c501SRahul Kayaith return f 133ea4c501SRahul Kayaith 143ea4c501SRahul Kayaith 153ea4c501SRahul Kayaith# CHECK-LABEL: TEST: test_exception 163ea4c501SRahul Kayaith@run 173ea4c501SRahul Kayaithdef test_exception(): 183ea4c501SRahul Kayaith ctx = Context() 193ea4c501SRahul Kayaith ctx.allow_unregistered_dialects = True 203ea4c501SRahul Kayaith try: 21*f9008e63STobias Hieta Operation.parse( 22*f9008e63STobias Hieta """ 233ea4c501SRahul Kayaith func.func @foo() { 243ea4c501SRahul Kayaith "test.use"(%0) : (i64) -> () loc("use") 253ea4c501SRahul Kayaith %0 = "test.def"() : () -> i64 loc("def") 263ea4c501SRahul Kayaith return 273ea4c501SRahul Kayaith } 28*f9008e63STobias Hieta """, 29*f9008e63STobias Hieta context=ctx, 30*f9008e63STobias Hieta ) 313ea4c501SRahul Kayaith except MLIRError as e: 323ea4c501SRahul Kayaith # CHECK: Exception: < 333ea4c501SRahul Kayaith # CHECK: Unable to parse operation assembly: 343ea4c501SRahul Kayaith # CHECK: error: "use": operand #0 does not dominate this use 353ea4c501SRahul Kayaith # CHECK: note: "use": see current operation: "test.use"(%0) : (i64) -> () 363ea4c501SRahul Kayaith # CHECK: note: "def": operand defined here (op in the same block) 373ea4c501SRahul Kayaith # CHECK: > 383ea4c501SRahul Kayaith print(f"Exception: <{e}>") 393ea4c501SRahul Kayaith 403ea4c501SRahul Kayaith # CHECK: message: Unable to parse operation assembly 413ea4c501SRahul Kayaith print(f"message: {e.message}") 423ea4c501SRahul Kayaith 433ea4c501SRahul Kayaith # CHECK: error_diagnostics[0]: loc("use") operand #0 does not dominate this use 443ea4c501SRahul Kayaith # CHECK: error_diagnostics[0].notes[0]: loc("use") see current operation: "test.use"(%0) : (i64) -> () 453ea4c501SRahul Kayaith # CHECK: error_diagnostics[0].notes[1]: loc("def") operand defined here (op in the same block) 46*f9008e63STobias Hieta print( 47*f9008e63STobias Hieta "error_diagnostics[0]: ", 48*f9008e63STobias Hieta e.error_diagnostics[0].location, 49*f9008e63STobias Hieta e.error_diagnostics[0].message, 50*f9008e63STobias Hieta ) 51*f9008e63STobias Hieta print( 52*f9008e63STobias Hieta "error_diagnostics[0].notes[0]: ", 53*f9008e63STobias Hieta e.error_diagnostics[0].notes[0].location, 54*f9008e63STobias Hieta e.error_diagnostics[0].notes[0].message, 55*f9008e63STobias Hieta ) 56*f9008e63STobias Hieta print( 57*f9008e63STobias Hieta "error_diagnostics[0].notes[1]: ", 58*f9008e63STobias Hieta e.error_diagnostics[0].notes[1].location, 59*f9008e63STobias Hieta e.error_diagnostics[0].notes[1].message, 60*f9008e63STobias Hieta ) 613ea4c501SRahul Kayaith 623ea4c501SRahul Kayaith 633ea4c501SRahul Kayaith# CHECK-LABEL: test_emit_error_diagnostics 643ea4c501SRahul Kayaith@run 653ea4c501SRahul Kayaithdef test_emit_error_diagnostics(): 663ea4c501SRahul Kayaith ctx = Context() 673ea4c501SRahul Kayaith loc = Location.unknown(ctx) 683ea4c501SRahul Kayaith handler_diags = [] 69*f9008e63STobias Hieta 703ea4c501SRahul Kayaith def handler(d): 713ea4c501SRahul Kayaith handler_diags.append(str(d)) 723ea4c501SRahul Kayaith return True 73*f9008e63STobias Hieta 743ea4c501SRahul Kayaith ctx.attach_diagnostic_handler(handler) 753ea4c501SRahul Kayaith 763ea4c501SRahul Kayaith try: 773ea4c501SRahul Kayaith Attribute.parse("not an attr", ctx) 783ea4c501SRahul Kayaith except MLIRError as e: 793ea4c501SRahul Kayaith # CHECK: emit_error_diagnostics=False: 803ea4c501SRahul Kayaith # CHECK: e.error_diagnostics: ['expected attribute value'] 813ea4c501SRahul Kayaith # CHECK: handler_diags: [] 823ea4c501SRahul Kayaith print(f"emit_error_diagnostics=False:") 833ea4c501SRahul Kayaith print(f"e.error_diagnostics: {[str(diag) for diag in e.error_diagnostics]}") 843ea4c501SRahul Kayaith print(f"handler_diags: {handler_diags}") 853ea4c501SRahul Kayaith 863ea4c501SRahul Kayaith ctx.emit_error_diagnostics = True 873ea4c501SRahul Kayaith try: 883ea4c501SRahul Kayaith Attribute.parse("not an attr", ctx) 893ea4c501SRahul Kayaith except MLIRError as e: 903ea4c501SRahul Kayaith # CHECK: emit_error_diagnostics=True: 913ea4c501SRahul Kayaith # CHECK: e.error_diagnostics: [] 923ea4c501SRahul Kayaith # CHECK: handler_diags: ['expected attribute value'] 933ea4c501SRahul Kayaith print(f"emit_error_diagnostics=True:") 943ea4c501SRahul Kayaith print(f"e.error_diagnostics: {[str(diag) for diag in e.error_diagnostics]}") 953ea4c501SRahul Kayaith print(f"handler_diags: {handler_diags}") 96