Fix --x-initial and --x-assign random stability (#2662) (#5958)

This commit is contained in:
Todd Strader
2025-05-16 13:54:51 -04:00
committed by GitHub
parent 73b3648262
commit 4581023805
35 changed files with 674 additions and 45 deletions
+34 -5
View File
@@ -752,7 +752,9 @@ string EmitCFunc::emitVarResetRecurse(const AstVar* varp, bool constructing,
|| varp->isFuncLocal() // Randomization too slow
|| (basicp && basicp->isZeroInit())
|| (v3Global.opt.underlineZero() && !varp->name().empty() && varp->name()[0] == '_')
|| (v3Global.opt.xInitial() == "fast" || v3Global.opt.xInitial() == "0"));
|| (varp->isXTemp()
? (v3Global.opt.xAssign() != "unique")
: (v3Global.opt.xInitial() == "fast" || v3Global.opt.xInitial() == "0")));
const bool slow = !varp->isFuncLocal() && !varp->isClassMember();
splitSizeInc(1);
if (dtypep->isWide()) { // Handle unpacked; not basicp->isWide
@@ -765,9 +767,20 @@ string EmitCFunc::emitVarResetRecurse(const AstVar* varp, bool constructing,
out += cvtToStr(constp->num().edataWord(w)) + "U;\n";
}
} else {
out += zeroit ? (slow ? "VL_ZERO_RESET_W(" : "VL_ZERO_W(") : "VL_RAND_RESET_W(";
out += zeroit ? (slow ? "VL_ZERO_RESET_W(" : "VL_ZERO_W(")
: "VL_SCOPED_RAND_RESET_W(";
out += cvtToStr(dtypep->widthMin());
out += ", " + varNameProtected + suffix + ");\n";
out += ", " + varNameProtected + suffix;
if (!zeroit) {
emitVarResetScopeHash();
const uint64_t salt = VString::hashMurmur(varp->prettyName());
out += ", ";
out += m_classOrPackage ? m_classOrPackageHash : "__VscopeHash";
out += ", ";
out += std::to_string(salt);
out += "ull";
}
out += ");\n";
}
return out;
} else {
@@ -780,9 +793,13 @@ string EmitCFunc::emitVarResetRecurse(const AstVar* varp, bool constructing,
if (zeroit || (v3Global.opt.xInitialEdge() && varp->isUsedClock())) {
out += " = 0;\n";
} else {
out += " = VL_RAND_RESET_";
emitVarResetScopeHash();
const uint64_t salt = VString::hashMurmur(varp->prettyName());
out += " = VL_SCOPED_RAND_RESET_";
out += dtypep->charIQWN();
out += "(" + cvtToStr(dtypep->widthMin()) + ");\n";
out += "(" + cvtToStr(dtypep->widthMin()) + ", "
+ (m_classOrPackage ? m_classOrPackageHash : "__VscopeHash") + ", "
+ std::to_string(salt) + "ull);\n";
}
return out;
}
@@ -791,3 +808,15 @@ string EmitCFunc::emitVarResetRecurse(const AstVar* varp, bool constructing,
}
return "";
}
void EmitCFunc::emitVarResetScopeHash() {
if (VL_LIKELY(m_createdScopeHash)) { return; }
if (m_classOrPackage) {
m_classOrPackageHash
= std::to_string(VString::hashMurmur(m_classOrPackage->name())) + "ULL";
} else {
puts(string("const uint64_t __VscopeHash = VL_HASH(")
+ (m_useSelfForThis ? "vlSelf" : "this") + "->name());\n");
}
m_createdScopeHash = true;
}