Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 93f4358

Browse files
authored
[MLIR] [Python] Added a context manager for enabling traceback-based locations (#157562)
Previously this functionality was not surfaced in the public API.
1 parent d5e5615 commit 93f4358

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

mlir/lib/Bindings/Python/MainModule.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ NB_MODULE(_mlir, m) {
5252
[](PyGlobals &self, bool enabled) {
5353
self.getTracebackLoc().setLocTracebacksEnabled(enabled);
5454
})
55+
.def("loc_tracebacks_frame_limit",
56+
[](PyGlobals &self) {
57+
return self.getTracebackLoc().locTracebackFramesLimit();
58+
})
5559
.def("set_loc_tracebacks_frame_limit",
56-
[](PyGlobals &self, int n) {
57-
self.getTracebackLoc().setLocTracebackFramesLimit(n);
60+
[](PyGlobals &self, std::optional<int> n) {
61+
self.getTracebackLoc().setLocTracebackFramesLimit(
62+
n.value_or(PyGlobals::TracebackLoc::kMaxFrames));
5863
})
5964
.def("register_traceback_file_inclusion",
6065
[](PyGlobals &self, const std::string &filename) {

mlir/python/mlir/ir.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,49 @@
22
# See https://llvm.org/LICENSE.txt for license information.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5+
from __future__ import annotations
6+
7+
from collections.abc import Iterable
8+
from contextlib import contextmanager
9+
510
from ._mlir_libs._mlir.ir import *
611
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+
)
817
from ._mlir_libs import (
918
get_dialect_registry,
1019
append_load_on_create_dialect,
1120
get_load_on_create_dialects,
1221
)
1322

1423

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+
1548
# Convenience decorator for registering user-friendly Attribute builders.
1649
def register_attribute_builder(kind, replace=False):
1750
def decorator_builder(func):

0 commit comments

Comments
 (0)