From 4b3b016f8b28739a6bdde5ab33037debe97d2371 Mon Sep 17 00:00:00 2001 From: Schuyler Eldridge Date: Fri, 26 Mar 2021 13:50:21 -0400 Subject: [PATCH] Fix bug in zero-width memory removal (#2153) * Fix bug in zero-width memory removal Correctly remove all extraneous connections to all types of memory ports (read, write, readwrite) for zero-width memories. Previously, only read ports were correctly handled. Signed-off-by: Schuyler Eldridge * fixup! Fix bug in zero-width memory removal (cherry picked from commit 67ce97a10564cfa07829af8cfce562009d60bafb) # Conflicts: # src/main/scala/firrtl/passes/ZeroWidth.scala --- src/main/scala/firrtl/passes/ZeroWidth.scala | 24 +++++++ .../scala/firrtlTests/ZeroWidthTests.scala | 63 +++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/src/main/scala/firrtl/passes/ZeroWidth.scala b/src/main/scala/firrtl/passes/ZeroWidth.scala index 5da39b057c..5bf4d33921 100644 --- a/src/main/scala/firrtl/passes/ZeroWidth.scala +++ b/src/main/scala/firrtl/passes/ZeroWidth.scala @@ -11,6 +11,7 @@ object ZeroWidth extends Transform { def inputForm: CircuitForm = UnknownForm def outputForm: CircuitForm = UnknownForm +<<<<<<< HEAD private def makeEmptyMemBundle(name: String): Field = Field(name, Flip, BundleType(Seq( Field("addr", Default, UIntType(IntWidth(0))), @@ -30,6 +31,29 @@ object ZeroWidth extends Transform { case Some(_) => d } case sx => sx map onEmptyMemStmt +======= + private def makeZero(tpe: ir.Type): ir.Type = tpe match { + case ClockType => UIntType(IntWidth(0)) + case a: UIntType => a.copy(IntWidth(0)) + case a: SIntType => a.copy(IntWidth(0)) + case a: AggregateType => a.map(makeZero) + } + + private def onEmptyMemStmt(s: Statement): Statement = s match { + case d @ DefMemory(info, name, tpe, _, _, _, rs, ws, rws, _) => + removeZero(tpe) match { + case None => + DefWire( + info, + name, + MemPortUtils + .memType(d) + .map(makeZero) + ) + case Some(_) => d + } + case sx => sx.map(onEmptyMemStmt) +>>>>>>> 67ce97a1... Fix bug in zero-width memory removal (#2153) } private def onModuleEmptyMemStmt(m: DefModule): DefModule = { diff --git a/src/test/scala/firrtlTests/ZeroWidthTests.scala b/src/test/scala/firrtlTests/ZeroWidthTests.scala index de8a67baf3..ac747579ec 100644 --- a/src/test/scala/firrtlTests/ZeroWidthTests.scala +++ b/src/test/scala/firrtlTests/ZeroWidthTests.scala @@ -229,6 +229,69 @@ class ZeroWidthTests extends FirrtlFlatSpec { | z <= asUInt(y)""".stripMargin (parse(exec(input))) should be(parse(check)) } + + "Memories with zero-width data-type" should "be fully removed" in { + val input = + """circuit Foo: + | module Foo: + | input clock: Clock + | input rAddr: UInt<4> + | input rEn: UInt<1> + | output rData: UInt<0> + | input wAddr: UInt<4> + | input wEn: UInt<1> + | input wMask: UInt<1> + | input wData: UInt<0> + | input rwEn: UInt<1> + | input rwMode: UInt<1> + | input rwAddr: UInt<1> + | input rwMask: UInt<1> + | input rwDataIn: UInt<0> + | output rwDataOut: UInt<0> + | + | mem memory: + | data-type => UInt<0> + | depth => 16 + | reader => r + | writer => w + | readwriter => rw + | read-latency => 0 + | write-latency => 1 + | read-under-write => undefined + | + | memory.r.clk <= clock + | memory.r.en <= rEn + | memory.r.addr <= rAddr + | rData <= memory.r.data + | memory.w.clk <= clock + | memory.w.en <= wEn + | memory.w.addr <= wAddr + | memory.w.mask <= wMask + | memory.w.data <= wData + | memory.rw.clk <= clock + | memory.rw.en <= rwEn + | memory.rw.addr <= rwAddr + | memory.rw.wmode <= rwMode + | memory.rw.wmask <= rwMask + | memory.rw.wdata <= rwDataIn + | rwDataOut <= memory.rw.rdata""".stripMargin + val check = + s"""circuit Foo: + | module Foo: + | input clock: Clock + | input rAddr: UInt<4> + | input rEn: UInt<1> + | input wAddr: UInt<4> + | input wEn: UInt<1> + | input wMask: UInt<1> + | input rwEn: UInt<1> + | input rwMode: UInt<1> + | input rwAddr: UInt<1> + | input rwMask: UInt<1> + | + |${Seq.tabulate(17)(_ => " skip").mkString("\n")}""".stripMargin + parse(exec(input)) should be(parse(check)) + } } class ZeroWidthVerilog extends FirrtlFlatSpec {