llvm / circt

Circuit IR Compilers and Tools
https://circt.org
Other
1.62k stars 281 forks source link

[FIRRTL] Module with instance sharing the same name creates a bad NLA #2709

Closed youngar closed 2 years ago

youngar commented 2 years ago
circuit Test : %[[
  {
    "class":"fake",
    "target":"~Test|Test/Test:Example"
  }
]]
  module Example :
  module Test :
    inst Test of Example

When trying to run this test, it creates a bad NLA. The @Test FlatSymbolRefAttr should be an hw.innerNameRef<@Test::@Test>.

./bin/firtool --parse-only  ./test.fir
./test.fir:1:1: error: 'firrtl.nla' op the instance path can only contain inner sym reference, only the leaf can refer to a module symbol
circuit Test : %[[
^
./test.fir:1:1: note: see current operation: "firrtl.nla"() {namepath = [@Test, @Example], sym_name = "nla_1"} : () -> ()
darthscsi commented 2 years ago

There was a similar problem with the symCache here: https://github.com/llvm/circt/commit/3257cb6594ffeccafa07ba38843a00580ec2ff55

youngar commented 2 years ago

The issue also hits when ports share the same name as the module:

circuit Test : %[[
  {
    "class":"fake",
    "target":"~Test|Test/other:Other>Other"
  }
]]
  module Test:
    inst other of Other
  module Other :
    input Other: UInt<1>

The port reference is changed into a module reference:

module {
  firrtl.circuit "Test"  {
    firrtl.nla @nla_1 [#hw.innerNameRef<@Test::@other>, @Other]
    firrtl.module @Test() {
      %other_Other = firrtl.instance other sym @other  {annotations = [{circt.nonlocal = @nla_1, class = "circt.nonlocal"}]} @Other(in Other: !firrtl.uint<1>)
    }
    firrtl.module @Other(in %Other: !firrtl.uint<1> sym @Other [{circt.nonlocal = @nla_1, class = "fake"}]) {
    }
  }
}
youngar commented 2 years ago

this is due to this weird assumption that the module name will not be the same as anything in the module itself. https://github.com/llvm/circt/blob/main/lib/Dialect/FIRRTL/Import/FIRAnnotations.cpp#L142