From 0b1391a623ee0769dc51fce15e0e0f5516225d5d Mon Sep 17 00:00:00 2001 From: Kevin Laeufer Date: Fri, 28 Aug 2020 13:15:56 -0700 Subject: FlattenSpec: flattening a module with no instaces should be a no-op (#1868) * FlattenSpec: flattening a module with no instaces should be a no-op * Fix problem when flattening/inlining a lone module Fix an edge case bug in InlineInstances where a circuit containing a lone module is flattened/inlined. This now properly special cases the situation of an empty indexMap which before had to be of length >= 1. Signed-off-by: Schuyler Eldridge * Simplify rename logic in InlineInstances Co-authored-by: Jack Koenig Co-authored-by: Albert Magyar Signed-off-by: Schuyler Eldridge * Mea culpa Signed-off-by: Schuyler Eldridge Co-authored-by: Schuyler Eldridge Co-authored-by: Jack Koenig Co-authored-by: Albert Magyar --- src/main/scala/firrtl/passes/Inline.scala | 17 +++++++++++------ src/test/scala/firrtlTests/FlattenTests.scala | 12 ++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/main/scala/firrtl/passes/Inline.scala b/src/main/scala/firrtl/passes/Inline.scala index ad9c108f..00cda739 100644 --- a/src/main/scala/firrtl/passes/Inline.scala +++ b/src/main/scala/firrtl/passes/Inline.scala @@ -264,10 +264,15 @@ class InlineInstances extends Transform with DependencyAPIMigration with Registe } } - val maxIdx = indexMap.values.max - val resultSeq = Seq.fill(maxIdx + 1)(RenameMap()) - val resultMap = indexMap.mapValues(idx => resultSeq(maxIdx - idx)) - (resultMap, resultSeq) + indexMap match { + case a if a.isEmpty => + (Map.empty[(OfModule, Instance), RenameMap], Seq.empty[RenameMap]) + case a => + val maxIdx = indexMap.values.max + val resultSeq = Seq.fill(maxIdx + 1)(RenameMap()) + val resultMap = indexMap.mapValues(idx => resultSeq(maxIdx - idx)) + (resultMap, resultSeq) + } } def fixupRefs( @@ -353,8 +358,8 @@ class InlineInstances extends Transform with DependencyAPIMigration with Registe Some(m.map(onStmt(ModuleName(m.name, CircuitName(c.main))))) }) - val renames = renamesSeq.tail.foldLeft(renamesSeq.head)(_ andThen _) + val renames = renamesSeq.reduceLeftOption(_ andThen _) - CircuitState(flatCircuit, LowForm, annos, Some(renames)) + CircuitState(flatCircuit, LowForm, annos, renames) } } diff --git a/src/test/scala/firrtlTests/FlattenTests.scala b/src/test/scala/firrtlTests/FlattenTests.scala index 53604ee5..ef555eaa 100644 --- a/src/test/scala/firrtlTests/FlattenTests.scala +++ b/src/test/scala/firrtlTests/FlattenTests.scala @@ -272,4 +272,16 @@ class FlattenTests extends LowTransformSpec { """.stripMargin execute(input, check, Seq(flatten("Top"))) } + + "The Flatten transform" should "work on modules with no instances" in { + val input = """ + |circuit Top : + | module Top : + | input a : UInt<32> + | output b : UInt<32> + | b <= a + """.stripMargin + val check = input + execute(input, check, Seq(flatten("Top"))) + } } -- cgit v1.2.3