Fix randc on extended class (#6852).

This commit is contained in:
Wilson Snyder
2025-12-21 19:56:55 -05:00
parent 1d7f5ed33e
commit f6b966ed16
6 changed files with 73 additions and 11 deletions
+12 -7
View File
@@ -1979,11 +1979,14 @@ class RandomizeVisitor final : public VNVisitor {
return pair.first->second;
}
AstVar* newRandcVarsp(AstVar* const varp) {
AstVar* newRandcVarsp(AstVar* const varp, AstClass* const classp) {
// Might be called multiple times on same var, if var is referenced in class extends,
// If so, each is randomized separately, so the select lands in the extending class
// If a randc, make a VlRandC object to hold the state
if (!varp->isRandC()) return nullptr;
uint64_t items = 0;
uint64_t items = 0;
if (AstEnumDType* const enumDtp = VN_CAST(varp->dtypep()->skipRefToEnump(), EnumDType)) {
items = static_cast<uint64_t>(enumDtp->itemCount());
} else if (AstBasicDType* const basicp = varp->dtypep()->skipRefp()->basicp()) {
@@ -2002,11 +2005,11 @@ class RandomizeVisitor final : public VNVisitor {
} else {
varp->v3fatalSrc("Unexpected randc variable dtype");
}
AstCDType* newdtp = findVlRandCDType(varp->fileline(), items);
AstVar* newp
AstCDType* const newdtp = findVlRandCDType(varp->fileline(), items);
AstVar* const newp
= new AstVar{varp->fileline(), VVarType::MEMBER, varp->name() + "__Vrandc", newdtp};
newp->isInternal(true);
varp->addNextHere(newp);
classp->addStmtsp(newp);
UINFO(9, "created " << varp);
return newp;
}
@@ -2285,6 +2288,7 @@ class RandomizeVisitor final : public VNVisitor {
void addBasicRandomizeBody(AstFunc* const basicRandomizep, AstClass* const nodep,
AstVar* randModeVarp) {
UINFO(9, "addBasicRTB " << nodep);
FileLine* const fl = nodep->fileline();
AstVar* const basicFvarp = VN_AS(basicRandomizep->fvarp(), Var);
AstVarRef* const basicFvarRefp = new AstVarRef{fl, basicFvarp, VAccess::WRITE};
@@ -2340,8 +2344,9 @@ class RandomizeVisitor final : public VNVisitor {
new AstAnd{fl, basicFvarRefReadp, callp}}};
basicRandomizep->addStmtsp(wrapIfRandMode(nodep, memberVarp, assignIfNotNullp));
} else {
AstVar* const randcVarp = newRandcVarsp(memberVarp);
AstVarRef* const refp = new AstVarRef{fl, classp, memberVarp, VAccess::WRITE};
AstVar* const randcVarp = newRandcVarsp(memberVarp, nodep);
AstVarRef* const refp
= new AstVarRef{memberVarp->fileline(), classp, memberVarp, VAccess::WRITE};
AstNodeStmt* const stmtp = newRandStmtsp(fl, refp, randcVarp, basicFvarp);
if (!refp->backp()) VL_DO_DANGLING(refp->deleteTree(), refp);
basicRandomizep->addStmtsp(new AstBegin{fl, "", stmtp, false});