From fc6c84bc3ee75aaeb7a64a9b05823b9443fdca13 Mon Sep 17 00:00:00 2001 From: Jack Koenig Date: Mon, 6 Feb 2017 17:21:48 -0800 Subject: Fix stack overflow from massive MaxWidth chains during width inference (#407) Fixes ucb-bar/chisel3#420--- src/main/scala/firrtl/passes/InferWidths.scala | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main/scala/firrtl/passes/InferWidths.scala b/src/main/scala/firrtl/passes/InferWidths.scala index 36466c09..397a5d91 100644 --- a/src/main/scala/firrtl/passes/InferWidths.scala +++ b/src/main/scala/firrtl/passes/InferWidths.scala @@ -18,13 +18,16 @@ object InferWidths extends Pass { def solve_constraints(l: Seq[WGeq]): ConstraintMap = { def unique(ls: Seq[Width]) : Seq[Width] = (ls map (new WrappedWidth(_))).distinct map (_.w) + // Combines constraints on the same VarWidth into the same constraint def make_unique(ls: Seq[WGeq]): ListMap[String,Width] = { - (ls foldLeft ListMap[String, Width]())((h, g) => g.loc match { - case w: VarWidth => h get w.name match { - case None => h + (w.name -> g.exp) - case Some(p) => h + (w.name -> MaxWidth(Seq(g.exp, p))) + ls.foldLeft(ListMap.empty[String, Width])((acc, wgeq) => wgeq.loc match { + case VarWidth(name) => acc.get(name) match { + case None => acc + (name -> wgeq.exp) + // Avoid constructing massive MaxWidth chains + case Some(MaxWidth(args)) => acc + (name -> MaxWidth(wgeq.exp +: args)) + case Some(width) => acc + (name -> MaxWidth(Seq(wgeq.exp, width))) } - case _ => h + case _ => acc }) } def pullMinMax(w: Width): Width = w map pullMinMax match { -- cgit v1.2.3