From e30c20d10ba47b11e06416e912ed89b6b6ce8e7b Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Thu, 23 Jul 2020 14:31:11 -0700 Subject: fix reduction op bug ConstantPropagation (#1746) * add const prop bitwise reduction equivalence test * mask negative literals when propagating reduction * change widths * get rid of unnecessary if * add BigInt mask utility--- src/main/scala/firrtl/Utils.scala | 9 +++++++++ src/main/scala/firrtl/transforms/ConstantPropagation.scala | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/scala/firrtl/Utils.scala b/src/main/scala/firrtl/Utils.scala index 2264a0e4..0a067c04 100644 --- a/src/main/scala/firrtl/Utils.scala +++ b/src/main/scala/firrtl/Utils.scala @@ -764,6 +764,15 @@ object Utils extends LazyLogging { .toSeq .foldLeft(Seq[String]()){ case (seq, id) => seq :+ name.splitAt(id)._1 } } + + /** Returns the value masked with the width. + * + * This supports truncating negative values as well as values that are too + * wide for the width + */ + def maskBigInt(value: BigInt, width: Int): BigInt = { + value & ((BigInt(1) << width) - 1) + } } object MemoizedHash { diff --git a/src/main/scala/firrtl/transforms/ConstantPropagation.scala b/src/main/scala/firrtl/transforms/ConstantPropagation.scala index 8ad3489f..0ec4fe0b 100644 --- a/src/main/scala/firrtl/transforms/ConstantPropagation.scala +++ b/src/main/scala/firrtl/transforms/ConstantPropagation.scala @@ -160,7 +160,8 @@ class ConstantPropagation extends Transform with DependencyAPIMigration with Res case IntWidth(b) => b } - val v: Seq[Boolean] = s"%${w}s".format(a.value.toString(2)).map(_ == '1') + val maskedValue = Utils.maskBigInt(a.value, w.toInt) + val v: Seq[Boolean] = s"%${w}s".format(maskedValue.toString(2)).map(_ == '1') (BigInt(0) until w).zip(v).foldLeft(identityValue) { case (acc, (_, x)) => reduce(acc, x) -- cgit v1.2.3