Skip to content

Commit

Permalink
Add error handling to width inference for invoke (#2142)
Browse files Browse the repository at this point in the history
* Add error handling to width inference for invoke

* Typo

* Add more helpful error message

* Tweak error message a tad

---------

Co-authored-by: Anshuman Mohan <[email protected]>
  • Loading branch information
polybeandip and anshumanmohan authored Jun 12, 2024
1 parent cb3fad3 commit 250a6b2
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions calyx-py/calyx/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,10 @@ def comb_mem_d2(
"""Generate a StdMemD2 cell."""
self.prog.import_("primitives/memories/comb.futil")
return self.cell(
name, ast.Stdlib.comb_mem_d2(bitwidth, len0, len1, idx_size0, idx_size1), is_external, is_ref
name,
ast.Stdlib.comb_mem_d2(bitwidth, len0, len1, idx_size0, idx_size1),
is_external,
is_ref,
)

def seq_mem_d1(
Expand Down Expand Up @@ -440,7 +443,10 @@ def seq_mem_d2(
"""Generate a SeqMemD2 cell."""
self.prog.import_("primitives/memories/seq.futil")
return self.cell(
name, ast.Stdlib.seq_mem_d2(bitwidth, len0, len1, idx_size0, idx_size1), is_external, is_ref
name,
ast.Stdlib.seq_mem_d2(bitwidth, len0, len1, idx_size0, idx_size1),
is_external,
is_ref,
)

def binary(
Expand Down Expand Up @@ -787,7 +793,7 @@ def reg_store(self, reg, val, groupname=None):

def mem_load_d1(self, mem, i, reg, groupname):
"""Inserts wiring into `self` to perform `reg := mem[i]`,
where `mem` is a seq_d1 memory or a comb_mem_d1 memory
where `mem` is a seq_d1 memory or a comb_mem_d1 memory
"""
assert mem.is_seq_mem_d1() or mem.is_comb_mem_d1()
is_comb = mem.is_comb_mem_d1()
Expand All @@ -805,7 +811,7 @@ def mem_load_d1(self, mem, i, reg, groupname):

def mem_load_d2(self, mem, i, j, reg, groupname):
"""Inserts wiring into `self` to perform `reg := mem[i]`,
where `mem` is a seq_d2 memory or a comb_mem_d2 memory
where `mem` is a seq_d2 memory or a comb_mem_d2 memory
"""
assert mem.is_seq_mem_d2() or mem.is_comb_mem_d2()
is_comb = mem.is_comb_mem_d2()
Expand All @@ -824,7 +830,7 @@ def mem_load_d2(self, mem, i, j, reg, groupname):

def mem_store_d1(self, mem, i, val, groupname):
"""Inserts wiring into `self` to perform `mem[i] := val`,
where `mem` is a seq_d1 memory or a comb_mem_d1 memory
where `mem` is a seq_d1 memory or a comb_mem_d1 memory
"""
assert mem.is_seq_mem_d1() or mem.is_comb_mem_d1()
is_comb = mem.is_comb_mem_d1()
Expand All @@ -839,7 +845,7 @@ def mem_store_d1(self, mem, i, val, groupname):

def mem_store_d2(self, mem, i, j, val, groupname):
"""Inserts wiring into `self` to perform `mem[i] := val`,
where `mem` is a seq_d2 memory or a comb_mem_d2 memory
where `mem` is a seq_d2 memory or a comb_mem_d2 memory
"""
assert mem.is_seq_mem_d2() or mem.is_comb_mem_d2()
is_comb = mem.is_comb_mem_d2()
Expand Down Expand Up @@ -1092,14 +1098,24 @@ def invoke(cell: CellBuilder, **kwargs) -> ast.Invoke:
The keyword arguments should have the form `in_*`, `out_*`, or `ref_*`, where
`*` is the name of an input port, output port, or ref cell on the invoked cell.
"""

def try_infer_width(x):
width = cell.infer_width(x)
if not width:
raise WidthInferenceError(
f"Could not infer width of input '{x}' when invoking cell '{cell.name}'. "
"Consider using `const(width, value)` instead of `value`."
)
return width

return ast.Invoke(
cell._cell.id,
[
(
k[3:],
(
(
const(cell.infer_width(k[3:]), v).expr
const(try_infer_width(k[3:]), v).expr
if isinstance(v, int)
else ExprBuilder.unwrap(v)
)
Expand Down

0 comments on commit 250a6b2

Please sign in to comment.