|
2 | 2 | # See https://llvm.org/LICENSE.txt for license information. |
3 | 3 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
4 | 4 |
|
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from collections.abc import Iterable |
| 8 | +from contextlib import contextmanager |
| 9 | + |
5 | 10 | from ._mlir_libs._mlir.ir import * |
6 | 11 | from ._mlir_libs._mlir.ir import _GlobalDebug |
7 | | -from ._mlir_libs._mlir import register_type_caster, register_value_caster |
| 12 | +from ._mlir_libs._mlir import ( |
| 13 | + register_type_caster, |
| 14 | + register_value_caster, |
| 15 | + globals, |
| 16 | +) |
8 | 17 | from ._mlir_libs import ( |
9 | 18 | get_dialect_registry, |
10 | 19 | append_load_on_create_dialect, |
11 | 20 | get_load_on_create_dialects, |
12 | 21 | ) |
13 | 22 |
|
14 | 23 |
|
| 24 | +@contextmanager |
| 25 | +def loc_tracebacks(*, max_depth: int | None = None) -> Iterable[None]: |
| 26 | + """Enables automatic traceback-based locations for MLIR operations. |
| 27 | +
|
| 28 | + Operations created within this context will have their location |
| 29 | + automatically set based on the Python call stack. |
| 30 | +
|
| 31 | + Args: |
| 32 | + max_depth: Maximum number of frames to include in the location. |
| 33 | + If None, the default limit is used. |
| 34 | + """ |
| 35 | + old_enabled = globals.loc_tracebacks_enabled() |
| 36 | + old_limit = globals.loc_tracebacks_frame_limit() |
| 37 | + try: |
| 38 | + globals.set_loc_tracebacks_frame_limit(max_depth) |
| 39 | + if not old_enabled: |
| 40 | + globals.set_loc_tracebacks_enabled(True) |
| 41 | + yield |
| 42 | + finally: |
| 43 | + if not old_enabled: |
| 44 | + globals.set_loc_tracebacks_enabled(False) |
| 45 | + globals.set_loc_tracebacks_frame_limit(old_limit) |
| 46 | + |
| 47 | + |
15 | 48 | # Convenience decorator for registering user-friendly Attribute builders. |
16 | 49 | def register_attribute_builder(kind, replace=False): |
17 | 50 | def decorator_builder(func): |
|
0 commit comments