Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generator debug_info generation #973

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions magma/generator.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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__
Expand All @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions magma/placer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_generator2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)