This is an archive of the discontinued LLVM Phabricator instance.

[mlir][python] Capture error diagnostics in exceptions
ClosedPublic

Authored by rkayaith on Feb 12 2023, 4:38 PM.

Details

Summary

This updates most (all?) error-diagnostic-emitting python APIs to
capture error diagnostics and include them in the raised exception's
message:

>>> Operation.parse('"arith.addi"() : () -> ()'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
mlir._mlir_libs.MLIRError: Unable to parse operation assembly:
error: "-":1:1: 'arith.addi' op requires one result
 note: "-":1:1: see current operation: "arith.addi"() : () -> ()

The diagnostic information is available on the exception for users who
may want to customize the error message:

>>> try:
...   Operation.parse('"arith.addi"() : () -> ()')
... except MLIRError as e:
...   print(e.message)
...   print(e.error_diagnostics)
...   print(e.error_diagnostics[0].message)
... 
Unable to parse operation assembly
[<mlir._mlir_libs._mlir.ir.DiagnosticInfo object at 0x7fed32bd6b70>]
'arith.addi' op requires one result

Error diagnostics captured in exceptions aren't propagated to diagnostic
handlers, to avoid double-reporting of errors. The context-level
emit_error_diagnostics option can be used to revert to the old
behaviour, causing error diagnostics to be reported to handlers instead
of as part of exceptions.

API changes:

  • Operation.verify now raises an exception on verification failure, instead of returning false
  • The exception raised by the following methods has been changed to MLIRError:
    • PassManager.run
    • {Module,Operation,Type,Attribute}.parse
    • {RankedTensorType,UnrankedTensorType}.get
    • {MemRefType,UnrankedMemRefType}.get
    • VectorType.get
    • FloatAttr.get

closes #60595

depends on D144804, D143830

Diff Detail

Event Timeline

rkayaith created this revision.Feb 12 2023, 4:38 PM
Herald added a project: Restricted Project. · View Herald Transcript
rkayaith added inline comments.Feb 13 2023, 11:55 AM
mlir/lib/Bindings/Python/IRCore.cpp
3371–3372

I couldn't find a way to define an exception with custom __init__ through pybind, so it's defined in python + imported here. Another option would be to use the C API.

rkayaith updated this revision to Diff 500656.Feb 26 2023, 8:35 PM
rkayaith retitled this revision from [WIP][mlir][python] Capture error diagnostics to [mlir][python] Capture error diagnostics in exceptions.
rkayaith edited the summary of this revision. (Show Details)

update

rkayaith updated this revision to Diff 500661.Feb 26 2023, 10:07 PM
rkayaith edited the summary of this revision. (Show Details)

missed some methods

Since I only changed error-emitting APIs to raise MLIRError, the exception type used is somewhat inconsistent, e.g. ComplexType.get still raises ValueError. Not sure what to do about those (raise MLIRError everywhere even if there's no diagnostics?)

rkayaith published this revision for review.Feb 26 2023, 10:19 PM
stellaraccident accepted this revision.Feb 26 2023, 10:38 PM

Nice, thanks! Fine to land as a stretch improvement but I think we need to further sugar the new exception class to avoid exposing implementation details.

mlir/python/mlir/_mlir_libs/__init__.py
109

I think we may want to further typify this vs having deeply nested tuples. Can be fine in a follow-up to sugar. Would make string printing easier and would leak fewer details to callers who want to extract the diagnostics.

This revision is now accepted and ready to land.Feb 26 2023, 10:38 PM
rkayaith updated this revision to Diff 501653.Mar 1 2023, 1:32 PM
rkayaith marked an inline comment as done.

Replace diagnostic tuples with a DiagnosticInfo class, which just mirrors the
main Diagnostic class but is safe to access outside handlers.

mlir/python/mlir/_mlir_libs/__init__.py
109

Took a stab at this, let me know if this is what you had in mind.

rkayaith updated this revision to Diff 501880.Mar 2 2023, 8:21 AM
rkayaith edited the summary of this revision. (Show Details)

rebase

rkayaith edited the summary of this revision. (Show Details)Mar 2 2023, 8:24 AM
rkayaith edited the summary of this revision. (Show Details)Mar 7 2023, 11:58 AM
This revision was automatically updated to reflect the committed changes.