From f2114d3dc32aa938fef21201c33490de111ee76d Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Sat, 26 Sep 2026 10:20:46 +0100 Subject: [PATCH] Internals: Make node type hashes stable with Ast changes Instead of hashing node types by type id (which can change when adding/removing Ast node types), astgen a stable hash based on node name. This makes output more stable through Ast node additions/deletions. Largely to help with 'make test-diff', should have no user noticeable impact. --- src/V3Hasher.cpp | 4 ++-- src/astgen | 10 ++++++++++ test_regress/t/t_debug_emitv.out | 6 +++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/V3Hasher.cpp b/src/V3Hasher.cpp index e068ac3fc..f010f221f 100644 --- a/src/V3Hasher.cpp +++ b/src/V3Hasher.cpp @@ -59,7 +59,7 @@ class HasherVisitor final : public VNVisitorConst { } else { VL_RESTORER(m_hash); // Reset accumulator - m_hash = V3Hash{nodep->type()}; // Node type + m_hash = V3Hash{nodep->type().hash()}; // Node type f(); // Node-specific hash if (hashDType && nodep != nodep->dtypep()) iterateConstNull(nodep->dtypep()); // Node dtype @@ -77,7 +77,7 @@ class HasherVisitor final : public VNVisitorConst { // Each visitor below contributes to the hash any node-specific content // that is not dependent on either of the following, as these are // included by default by hashNode: - // - Node type (as given by AstNode::type()) + // - Node type (as given by AstNode::type().hash()) // - Node dtype (unless !hashDType) // - child nodes (unless !hashChildren) // diff --git a/src/astgen b/src/astgen index 063e8ec47..ff7a9042b 100755 --- a/src/astgen +++ b/src/astgen @@ -4,6 +4,7 @@ import argparse import glob +import hashlib import os import re import sys @@ -881,6 +882,15 @@ def write_type_enum(prefix, nodeList): fh.write(" return names[m_e];\n") fh.write(" }\n") + fh.write(" uint32_t hash() const VL_MT_SAFE {\n") + fh.write(" static const uint32_t hashes[] = {\n") + for node in sorted(filter(lambda _: _.isLeaf, nodeList), key=lambda _: _.typeId): + digest = hashlib.md5((prefix + node.name).encode("ascii")).hexdigest() + fh.write(" 0x{h}U, // {p}{n}\n".format(h=digest[:8], p=prefix, n=node.name)) + fh.write(" };\n") + fh.write(" return hashes[m_e];\n") + fh.write(" }\n") + def write_type_tests(prefix, nodeList): with open_file("V3{p}__gen_type_tests.h".format(p=prefix)) as fh: diff --git a/test_regress/t/t_debug_emitv.out b/test_regress/t/t_debug_emitv.out index 3b07952ea..62ca88b14 100644 --- a/test_regress/t/t_debug_emitv.out +++ b/test_regress/t/t_debug_emitv.out @@ -56,8 +56,8 @@ module Vt_debug_emitv_t; $write(""); end $display("%p", $_EXPRSTMT( - __Venum_hf6514fb1__0 = (in); - , __Venum_hf6514fb1__0); + __Venum_h99905942__0 = (in); + , __Venum_h99905942__0); ); if ((| $value$plusargs("TEST=%d", i1))) begin $display("value was %d", i1); @@ -1096,7 +1096,7 @@ module Vt_debug_emitv_t; enum logic [2:0] { ZERO = 3'h0, ONE = 3'h1 - } __Venum_hf6514fb1__0; + } __Venum_h99905942__0; endmodule package Vt_debug_emitv___024unit; class Vt_debug_emitv_Cls;