diff --git a/magma/generator.py b/magma/generator.py index 556d008d5..39ef16e5f 100644 --- a/magma/generator.py +++ b/magma/generator.py @@ -1,10 +1,14 @@ from abc import abstractmethod, ABCMeta import collections import functools +import inspect import weakref -from .circuit import DefineCircuitKind, Circuit -from . import cache_definition + +from magma import cache_definition +from magma.circuit import DefineCircuitKind, Circuit from magma.common import ParamDict +from magma.config import get_debug_mode +from magma.debug import debug_info class GeneratorMeta(type): @@ -60,6 +64,18 @@ def _make_key(cls, *args, **kwargs): return (cls, ParamDict(dct)) +def _maybe_add_debug_info(dct, cls): + if "debug_info" in dct: + return + if not get_debug_mode(): + dct["debug_info"] = None + return + _, lineno = inspect.getsourcelines(cls) + filename = inspect.getsourcefile(cls) + module = inspect.getmodule(cls) + dct["debug_info"] = debug_info(filename, lineno, module) + + def _make_type(cls, *args, **kwargs): dummy = type.__new__(cls, "", (), {}) name = cls.__name__ @@ -70,6 +86,7 @@ def _make_type(cls, *args, **kwargs): # NOTE(leonardt): We need to override the Generator2 classmethod bind with # DefineCircuitKind.bind for generator instances (circuits). dct["bind"] = classmethod(DefineCircuitKind.bind) + _maybe_add_debug_info(dct, cls) ckt = DefineCircuitKind.__new__(cls, name, bases, dct) for gen in cls.bind_generators: gen.generate_bind(ckt, *args, **kwargs) diff --git a/magma/placer.py b/magma/placer.py index 1339c353a..a82f07411 100644 --- a/magma/placer.py +++ b/magma/placer.py @@ -3,9 +3,13 @@ from collections import Counter import inspect import textwrap -from .config import get_debug_mode -from .ref import LazyCircuit -from .view import InstView + +from magma.config import get_debug_mode, config, RuntimeConfig +from magma.ref import LazyCircuit +from magma.view import InstView + + +config._register(disable_smart_naming=RuntimeConfig(False)) def _parse_name(inst): @@ -83,7 +87,7 @@ def _debug_name(self, inst) -> bool: name of @inst and returns True if available. Otherwise (or if debug mode is disabled), returns False. """ - if not get_debug_mode(): + if not get_debug_mode() or config.disable_smart_naming: return False basename = _parse_name(inst) if not basename: diff --git a/tests/test_generator2.py b/tests/test_generator2.py index b3b459bd2..2571c2608 100644 --- a/tests/test_generator2.py +++ b/tests/test_generator2.py @@ -92,3 +92,31 @@ def __init__(self): my_gen_other = _MyGen() assert my_gen is not my_gen_other assert repr(my_gen) == repr(my_gen_other) + + +def test_debug_info(): + m.config.set_debug_mode(True) + + class Cell(m.Circuit): + io = m.IO(I=m.In(m.Bit), O=m.Out(m.Bit)) + + class _MyNewGen(m.Generator2): + def __init__(self): + self.io = io = m.IO(I=m.In(m.Bit), O=m.Out(m.Bit)) + io.O @= Cell()(io.I) + + _MyNewGenDef = _MyNewGen() + + # Sanity checks. + assert Cell.debug_info[0] == __file__ + assert Cell.debug_info[1] == 100 + + # Check generator debug info. + cell = _MyNewGenDef.instances[0] + assert cell.debug_info[0] == __file__ + assert cell.debug_info[1] == 106 + print (_MyNewGenDef.debug_info) + assert _MyNewGenDef.debug_info[0] == __file__ + assert _MyNewGenDef.debug_info[1] == 103 + + m.config.set_debug_mode(False)