From d3fde399d5659fc47b65e9866921c7b32ed5e642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20L=C3=A4ufer?= Date: Mon, 26 Jul 2021 16:28:54 -0700 Subject: [PATCH] ir: make HashCode.str public This will allow chiseltest to save the hash code to disk for the purpose of caching simulation binaries. --- src/main/scala/firrtl/ir/StructuralHash.scala | 5 +++-- src/test/scala/firrtl/ir/StructuralHashSpec.scala | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/scala/firrtl/ir/StructuralHash.scala b/src/main/scala/firrtl/ir/StructuralHash.scala index 20c63e4e1c..250161f5fd 100644 --- a/src/main/scala/firrtl/ir/StructuralHash.scala +++ b/src/main/scala/firrtl/ir/StructuralHash.scala @@ -115,7 +115,8 @@ object StructuralHash { } trait HashCode { - protected val str: String + /** string representation of the hash code */ + def str: String override def hashCode(): Int = str.hashCode override def equals(obj: Any): Boolean = obj match { case hashCode: HashCode => this.str.equals(hashCode.str) @@ -124,7 +125,7 @@ trait HashCode { } private class MDHashCode(code: Array[Byte]) extends HashCode { - protected override val str: String = code.map(b => f"${b.toInt & 0xff}%02x").mkString("") + override val str: String = code.map(b => f"${b.toInt & 0xff}%02x").mkString("") } /** Generic hashing interface which allows us to use different backends to trade of speed and collision resistance */ diff --git a/src/test/scala/firrtl/ir/StructuralHashSpec.scala b/src/test/scala/firrtl/ir/StructuralHashSpec.scala index ffd0312632..659e1172fb 100644 --- a/src/test/scala/firrtl/ir/StructuralHashSpec.scala +++ b/src/test/scala/firrtl/ir/StructuralHashSpec.scala @@ -38,6 +38,16 @@ class StructuralHashSpec extends AnyFlatSpec { assert(hash(b1) != hash(UIntLiteral(1, IntWidth(2)))) } + it should "generate the same hash String if the objects are structurally the same" in { + assert(hash(b0).str == hash(UIntLiteral(0, IntWidth(1))).str) + assert(hash(b0).str != hash(UIntLiteral(1, IntWidth(1))).str) + assert(hash(b0).str != hash(UIntLiteral(1, IntWidth(2))).str) + + assert(hash(b1).str == hash(UIntLiteral(1, IntWidth(1))).str) + assert(hash(b1).str != hash(UIntLiteral(0, IntWidth(1))).str) + assert(hash(b1).str != hash(UIntLiteral(1, IntWidth(2))).str) + } + it should "ignore expression types" in { assert(hash(add) == hash(DoPrim(Add, Seq(b0, b1), Seq(), UnknownType))) assert(hash(add) == hash(DoPrim(Add, Seq(b0, b1), Seq(), UIntType(UnknownWidth))))