[all-commits] [llvm/llvm-project] 3ea4c5: [mlir][python] Capture error diagnostics in except...

rk via All-commits all-commits at lists.llvm.org
Tue Mar 7 11:59:36 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 3ea4c5014da3a18b56fea3579bed72c649357f47
      https://github.com/llvm/llvm-project/commit/3ea4c5014da3a18b56fea3579bed72c649357f47
  Author: Rahul Kayaith <rkayaith at gmail.com>
  Date:   2023-03-07 (Tue, 07 Mar 2023)

  Changed paths:
    M mlir/lib/Bindings/Python/IRAttributes.cpp
    M mlir/lib/Bindings/Python/IRCore.cpp
    M mlir/lib/Bindings/Python/IRModule.h
    M mlir/lib/Bindings/Python/IRTypes.cpp
    M mlir/lib/Bindings/Python/Pass.cpp
    M mlir/python/mlir/_mlir_libs/__init__.py
    M mlir/test/python/ir/attributes.py
    M mlir/test/python/ir/builtin_types.py
    M mlir/test/python/ir/diagnostic_handler.py
    A mlir/test/python/ir/exception.py
    M mlir/test/python/ir/module.py
    M mlir/test/python/ir/operation.py
    M mlir/test/python/pass_manager.py

  Log Message:
  -----------
  [mlir][python] Capture error diagnostics in exceptions

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

Reviewed By: stellaraccident

Differential Revision: https://reviews.llvm.org/D143869




More information about the All-commits mailing list