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

Bug relating to Arrays of Arrays #226

Closed
RobertBaruch opened this issue Sep 22, 2019 · 1 comment
Closed

Bug relating to Arrays of Arrays #226

RobertBaruch opened this issue Sep 22, 2019 · 1 comment

Comments

@RobertBaruch
Copy link

It seems like this should work, but results in errors. This is using the latest pull: nmigen-0.1.dev664+g1976310.

from nmigen import *
from nmigen.cli import main
from nmigen.hdl.rec import *


class Thing(Elaboratable):
    def __init__(self):
        self.useIX = Signal()
        self.useIY = Signal()
        self.H1 = Signal(8)
        self.L1 = Signal(8)
        self.IX = Signal(8)
        self.IY = Signal(8)

        self.choice = Signal()
        self.input = Signal(8)
        self.output = Signal(8)

    def ports(self):
        return [
            self.useIX, self.useIY, self.H1, self.L1, self.IX, self.IY,
            self.choice, self.input, self.output
        ]

    def elaborate(self, platform):
        """
        If choice is 0, then output H1 or IX depending on whether useIX is 0 or 1.
        If choice is 1, then output L1 or IY depending on whether useIY is 0 or 1.
        """
        m = Module()

        H = Array([self.H1, self.IX])[self.useIX]

        if platform == "bug":
            L = Array([self.L1, self.IY])[self.useIY]
        else:
            L = self.L1

        reg = Array([H, L])[self.choice]

        m.d.comb += self.output.eq(reg)

        return m


if __name__ == "__main__":
    m = Module()
    m.submodules.thing = thing = Thing()

    sync = ClockDomain()

    main(m, ports=[sync.clk, sync.rst] + thing.ports(), platform="bug")
Traceback (most recent call last):
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 702, in on_statement
    super().on_statement(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 219, in on_statement
    new_stmt = self.on_Assign(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 645, in on_Assign
    rhs_sigspec = self.rhs_compiler(stmt.rhs)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 134, in __call__
    return self.on_value(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 402, in on_value
    return super().on_value(self.s.expand(value))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 119, in on_value
    new_value = self.on_ArrayProxy(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 370, in on_ArrayProxy
    raise LegalizeValue(value.index, range(len(value.elems)), value.src_loc)
nmigen.back.rtlil.LegalizeValue: ((sig choice), range(0, 2), ('test_array.py', 39))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 702, in on_statement
    super().on_statement(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 230, in on_statement
    new_stmt = self.on_statements(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 719, in on_statements
    self.on_statement(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 714, in on_statement
    super().on_statement(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 219, in on_statement
    new_stmt = self.on_Assign(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 645, in on_Assign
    rhs_sigspec = self.rhs_compiler(stmt.rhs)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 134, in __call__
    return self.on_value(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 402, in on_value
    return super().on_value(self.s.expand(value))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 119, in on_value
    new_value = self.on_ArrayProxy(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 368, in on_ArrayProxy
    return self.match_shape(elem, *value.shape())
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 464, in match_shape
    return self(ast.Slice(value, 0, new_bits))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 134, in __call__
    return self.on_value(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 402, in on_value
    return super().on_value(self.s.expand(value))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 110, in on_value
    new_value = self.on_Slice(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 351, in on_Slice
    return self(value.value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 134, in __call__
    return self.on_value(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 402, in on_value
    return super().on_value(self.s.expand(value))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 119, in on_value
    new_value = self.on_ArrayProxy(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 370, in on_ArrayProxy
    raise LegalizeValue(value.index, range(len(value.elems)), value.src_loc)
nmigen.back.rtlil.LegalizeValue: ((sig useIX), range(0, 2), ('test_array.py', 32))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 702, in on_statement
    super().on_statement(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 219, in on_statement
    new_stmt = self.on_Assign(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 645, in on_Assign
    rhs_sigspec = self.rhs_compiler(stmt.rhs)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 134, in __call__
    return self.on_value(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 402, in on_value
    return super().on_value(self.s.expand(value))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 119, in on_value
    new_value = self.on_ArrayProxy(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 370, in on_ArrayProxy
    raise LegalizeValue(value.index, range(len(value.elems)), value.src_loc)
nmigen.back.rtlil.LegalizeValue: ((sig choice), range(0, 2), ('test_array.py', 39))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test_array.py", line 52, in <module>
    main(m, ports=[sync.clk, sync.rst] + thing.ports(), platform="bug")
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/cli.py", line 76, in main
    main_runner(parser, parser.parse_args(), *args, **kwargs)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/cli.py", line 58, in main_runner
    output = verilog.convert(fragment, name=name, ports=ports)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/verilog.py", line 67, in convert
    il_text = rtlil.convert(*args, **kwargs)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 959, in convert
    il_text, name_map = convert_fragment(fragment, name)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 953, in convert_fragment
    _convert_fragment(builder, fragment, name_map, hierarchy=(name,))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 810, in _convert_fragment
    hierarchy=hierarchy + (sub_name,))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 854, in _convert_fragment
    stmt_compiler(group_stmts)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 240, in __call__
    return self.on_statement(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 714, in on_statement
    super().on_statement(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 230, in on_statement
    new_stmt = self.on_statements(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 719, in on_statements
    self.on_statement(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 714, in on_statement
    super().on_statement(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 219, in on_statement
    new_stmt = self.on_Assign(stmt)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 645, in on_Assign
    rhs_sigspec = self.rhs_compiler(stmt.rhs)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 134, in __call__
    return self.on_value(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 402, in on_value
    return super().on_value(self.s.expand(value))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 119, in on_value
    new_value = self.on_ArrayProxy(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 368, in on_ArrayProxy
    return self.match_shape(elem, *value.shape())
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 464, in match_shape
    return self(ast.Slice(value, 0, new_bits))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 134, in __call__
    return self.on_value(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 402, in on_value
    return super().on_value(self.s.expand(value))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 110, in on_value
    new_value = self.on_Slice(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 351, in on_Slice
    return self(value.value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 134, in __call__
    return self.on_value(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 402, in on_value
    return super().on_value(self.s.expand(value))
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/hdl/xfrm.py", line 119, in on_value
    new_value = self.on_ArrayProxy(value)
  File "/home/robertbaruch/.local/lib/python3.6/site-packages/nmigen/back/rtlil.py", line 370, in on_ArrayProxy
    raise LegalizeValue(value.index, range(len(value.elems)), value.src_loc)
nmigen.back.rtlil.LegalizeValue: ((sig useIY), range(0, 2), ('test_array.py', 35))
@whitequark whitequark added the bug label Sep 22, 2019
@whitequark whitequark added this to the 0.1 milestone Sep 22, 2019
@RobertBaruch
Copy link
Author

RobertBaruch commented Sep 23, 2019

There is at least a workaround, which is to flatten the array:

        # useIY, useIX, choice
        s = Cat(self.choice, self.useIX, self.useIY)
        reg = Array([
            self.H1, self.L1, self.IX, self.L1, self.H1, self.IY, self.IX,
            self.IY
        ])[s]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants