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

Memoize first clock type of defn #685

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 13 additions & 6 deletions magma/clock.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
from .t import Direction, In
from .digital import DigitalMeta, Digital
from .wire import wire
Expand Down Expand Up @@ -126,16 +127,22 @@ def _get_first_clock(port, clocktype):
return None


def wireclocktype(defn, inst, clocktype):
def _get_first_clock_of_defn(defn, clocktype):
# Check common case: top level clock port
clks = (port if isinstance(port, clocktype) else None
for port in defn.interface.ports.values())
defnclk = _first(clks)
if defnclk is None:
# Check recursive types
clks = (_get_first_clock(port, clocktype)
for port in defn.interface.ports.values())
defnclk = _first(clks)
if defnclk is not None:
return defnclk
# Check recursive types
clks = (_get_first_clock(port, clocktype)
for port in defn.interface.ports.values())
return _first(clks)


@functools.lru_cache(maxsize=None)
def wireclocktype(defn, inst, clocktype):
defnclk = _get_first_clock_of_defn(defn, clocktype)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to memoize the _get_first_clock function right? But I'm not sure if this is really where the best savings will occur, since for one definition, we'll only need to get the first clock once.

I think where it becomes useful is in the wiring of instances. We may have a large number of instances, and we have to check each of their interfaces to find the first clock. If we can memoize the first clock for a definition, then for each instance we can simply lookup the default clock rather than traversing the interface. I suspect most of the savings will come here, since will be repeating the clock lookup logic for instances of the same definition, while definitions only need to lookup their internal clock once

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, wireclock will repeat this logic for each clock type. I think it would be more efficient to wire a set of clock types and handle them all as we're traversing the interface once (rather than traversing the interface each time for each clock type).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(a) yes, oops, sorry put the decorator on the wrong function.

(b) with the decorator on the right function, we will only traverse the interface once per (definition, type) pair. If I understand correctly, you're suggesting we simultaneously do the traversal for all the types?

(c) yes, good point about instances. Even if we find we can find the first clock of a defn fast, we still find all the clocks for each instance, and this is repeated even for multiple instances of the same defn. Will try to implement this.

I think the best way to do this is to have an acceleration data structure associated with each defn which is Map[Type, List[String]], where for each type we have the name of all the ports which match that type. We can make this a query and then cache the result per definition. I think it would also be prudent to have an option to stop on the first one if needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leonardt I can try to implement the above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I realize (c) is more complicated for nested types. If for example, defn.x[0].y[1] happens to be a clock type, re-targetting that reference to an instance is non-trivial (we basically need to capture all the clock types "by reference"). Will think about the best way to do this.

If we avoid nested types this becomes much easier as all we have to do is save the port name and do getattr on instances. But it sounds like nested clocks are definitely important.

if defnclk is None:
return
for port in inst.interface.inputs(include_clocks=True):
Expand Down