Skip to content

Commit 94d425f

Browse files
authored
Fix invalid references generated by VerilogMemDelays (#2588)
Transformation of mem readwriters whose address contain references to readwriters of mems declared before it would contain invalid references to untransformed memory readwriter, as the connection is not transformed. This commit fixes this issue.
1 parent 84a7db5 commit 94d425f

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/main/scala/firrtl/passes/memlib/VerilogMemDelays.scala

+12-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,18 @@ class MemDelayAndReadwriteTransformer(m: DefModule, passthroughSimpleSyncReadMem
221221
val transformed = m match {
222222
case mod: Module =>
223223
findMemConns(mod.body)
224-
mod.copy(body = Block(transform(mod.body) +: newConns.toSeq))
224+
val bodyx = transform(mod.body)
225+
// Fixup any mem connections being driven by other transformed memories
226+
val newConsx = newConns.map {
227+
case sx if kind(sx.loc) == MemKind =>
228+
val (memRef, _) = Utils.splitRef(sx.loc)
229+
if (passthroughMems(WrappedExpression(memRef)))
230+
sx
231+
else
232+
sx.mapExpr(swapMemRefs)
233+
case sx => sx
234+
}
235+
mod.copy(body = Block(bodyx +: newConsx.toSeq))
225236
case mod => mod
226237
}
227238
}

src/test/scala/firrtlTests/VerilogMemDelaySpec.scala

+58
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,62 @@ class VerilogMemDelaySpec extends LeanTransformSpec(Seq(Dependency(VerilogMemDel
188188
res should include("m.write.clk <= clock")
189189
res should include("reg m_write_data_pipe_0 : UInt<8>, clock")
190190
}
191+
192+
it should "VerilogMemDelays should replace expr in connections of previous mems" in {
193+
val input =
194+
"""
195+
|circuit Test :
196+
| module Test :
197+
| input clock : Clock
198+
| input sel : UInt<1>
199+
| input en : UInt<1>
200+
| output v1 : UInt<1>
201+
| output v2 : UInt<1>
202+
|
203+
| mem m1 :
204+
| data-type => UInt<1>
205+
| depth => 2
206+
| read-latency => 0
207+
| write-latency => 1
208+
| readwriter => rw1
209+
| readwriter => rw2
210+
| read-under-write => undefined
211+
| mem m2 :
212+
| data-type => UInt<1>
213+
| depth => 2
214+
| read-latency => 0
215+
| write-latency => 1
216+
| readwriter => rw1
217+
| readwriter => rw2
218+
| read-under-write => undefined
219+
| v1 <= m1.rw2.rdata
220+
| v2 <= m2.rw2.rdata
221+
| m1.rw1.addr <= UInt<1>("h0")
222+
| m2.rw1.addr <= UInt<1>("h0")
223+
| m1.rw1.en <= UInt<1>("h1")
224+
| m2.rw1.en <= UInt<1>("h1")
225+
| m1.rw1.clk <= clock
226+
| m2.rw1.clk <= clock
227+
| m1.rw1.wmode <= en
228+
| m2.rw1.wmode <= en
229+
| m1.rw1.wdata <= UInt<1>("h1")
230+
| m2.rw1.wdata <= UInt<1>("h0")
231+
| m1.rw1.wmask <= en
232+
| m2.rw1.wmask <= UInt<1>("h0")
233+
| m1.rw2.addr <= m2.rw1.rdata
234+
| m2.rw2.addr <= m2.rw1.rdata
235+
| m1.rw2.en <= UInt<1>("h1")
236+
| m2.rw2.en <= UInt<1>("h1")
237+
| m1.rw2.clk <= clock
238+
| m2.rw2.clk <= clock
239+
| m1.rw2.wmode <= en
240+
| m2.rw2.wmode <= en
241+
| m1.rw2.wdata <= UInt<1>("h0")
242+
| m2.rw2.wdata <= UInt<1>("h0")
243+
| m1.rw2.wmask <= en
244+
| m2.rw2.wmask <= UInt<1>("h0")
245+
""".stripMargin
246+
247+
compileTwice(input)
248+
}
191249
}

0 commit comments

Comments
 (0)