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

Piezo, PIFOTree: static option! #1783

Merged
merged 17 commits into from
Nov 20, 2023
Merged
Prev Previous commit
Next Next commit
Neater incr
  • Loading branch information
anshumanmohan committed Nov 20, 2023
commit 7be44bfc3e87a34da293ce83d1f3ff6bb7d381fd
24 changes: 9 additions & 15 deletions calyx-py/calyx/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,29 +523,23 @@ def bitwise_flip_reg(self, reg, cellname=None):
not_group.done = reg.done
return not_group

def incr(self, reg, val=1, signed=False, cellname=None):
def incr(self, reg, val=1, signed=False, cellname=None, static=False):
"""Inserts wiring into `self` to perform `reg := reg + val`."""
cellname = cellname or f"{reg.name}_incr"
width = reg.infer_width_reg()
add_cell = self.add(width, cellname, signed)
with self.group(f"{cellname}_group") as incr_group:
add_cell.left = reg.out
add_cell.right = const(width, val)
reg.write_en = 1
reg.in_ = add_cell.out
incr_group.done = reg.done
return incr_group

def incr_static(self, reg, val=1, signed=False, cellname=None):
"""Inserts wiring into `self` to perform `reg := reg + val` statically."""
cellname = cellname or f"{reg.name}_incr"
width = reg.infer_width_reg()
add_cell = self.add(width, cellname, signed)
with self.static_group(f"{cellname}_group", 1) as incr_group:
group = (
self.static_group(f"{cellname}_group", 1)
if static
else self.group(f"{cellname}_group")
)
with group as incr_group:
add_cell.left = reg.out
add_cell.right = const(width, val)
reg.write_en = 1
reg.in_ = add_cell.out
Copy link
Contributor Author

@anshumanmohan anshumanmohan Nov 19, 2023

Choose a reason for hiding this comment

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

It would have been nice to have reused the existing incr with a static flag as an argument. I got a good way through it, but then the issue is that the dynamic version needs a line incr_group.done = ... but the static version does not.

I'd usually do that with the Python idiom of X if b else Y, but that doesn't tango super well with the eDSL's own with foo as bar: baz idiom.

Copy link
Contributor Author

@anshumanmohan anshumanmohan Nov 19, 2023

Choose a reason for hiding this comment

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

Just paging @sampsyo in case he knows a neat way! Like basically is there some Y such that I could do:

with foo as bar:
  ...
  ...
  X if not static else Y

and the Y would be a no-op, ignored when generating Calyx code?

If I had a nice way of doing this, I could go through and do it for lots of helpers in the eDSL!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the sync chat; it worked like a charm!

if not static:
incr_group.done = reg.done
return incr_group

def decr(self, reg, val=1, signed=False, cellname=None):
Expand Down
4 changes: 2 additions & 2 deletions calyx-py/test/correctness/sdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def insert_stats(prog, name, static=False):

# Wiring to increment the appropriate register.
if static:
count_0_incr = stats.incr_static(count_0_sto)
count_1_incr = stats.incr_static(count_1_sto)
count_0_incr = stats.incr(count_0_sto, static=static)
count_1_incr = stats.incr(count_1_sto, static=static)
else:
count_0_incr = stats.incr(count_0_sto)
count_1_incr = stats.incr(count_1_sto)
Expand Down