Fix inherited rand array with .size + foreach constraint (#7650)

Fixes #7558.
This commit is contained in:
Yilou Wang
2026-05-24 09:02:01 -04:00
committed by GitHub
parent f93b4bbd05
commit 6c96ce4b40
3 changed files with 130 additions and 4 deletions
+16 -4
View File
@@ -4818,7 +4818,12 @@ class RandomizeVisitor final : public VNVisitor {
pinp->addPinsp(namep);
pinp->addPinsp(new AstConst{fl, AstConst::Unsized64{},
static_cast<uint64_t>(sizeVarp->width())});
pinp->addPinsp(new AstVarRef{fl, sizeVarp, VAccess::READ});
// sizeVarp may live in a base class when the constrained
// array is inherited; route VarRef through its declaring
// class so V3Scope can resolve it.
AstVarRef* const sizeVarRefp = new AstVarRef{fl, sizeVarp, VAccess::READ};
sizeVarRefp->classOrPackagep(VN_AS(sizeVarp->user2p(), NodeModule));
pinp->addPinsp(sizeVarRefp);
randomizep->addStmtsp(pinp->makeStmt());
}
@@ -5265,12 +5270,18 @@ class RandomizeVisitor final : public VNVisitor {
bool wasCreated = false;
AstVar* const sizeVarp
= createOrGetSizeVar(classp, arrVarp, fl, methodp->findIntDType(), wasCreated);
// arrVarp and sizeVarp may live in a base class when the
// array is inherited; route VarRefs through their declaring
// class so V3Scope can resolve them.
AstNodeModule* const arrClassp = VN_AS(arrVarp->user2p(), NodeModule);
AstNodeModule* const sizeClassp = VN_AS(sizeVarp->user2p(), NodeModule);
if (wasCreated) {
// Generate resize for dynamic arrays/queues (not assoc arrays)
if (!VN_IS(arrVarp->dtypep()->skipRefp(), AssocArrayDType)) {
AstCMethodHard* const resizep = new AstCMethodHard{
fl, new AstVarRef{fl, classp, arrVarp, VAccess::READWRITE},
VCMethod::DYN_RESIZE, new AstVarRef{fl, sizeVarp, VAccess::READ}};
fl, new AstVarRef{fl, arrClassp, arrVarp, VAccess::READWRITE},
VCMethod::DYN_RESIZE,
new AstVarRef{fl, sizeClassp, sizeVarp, VAccess::READ}};
resizep->dtypep(methodp->findVoidDType());
inlineResizeStmtsp
= AstNode::addNext(inlineResizeStmtsp, new AstStmtExpr{fl, resizep});
@@ -5279,7 +5290,8 @@ class RandomizeVisitor final : public VNVisitor {
// Append size >= 0 constraint so ConstraintExprVisitor processes it
capturedTreep->addNext(createSizeGteZeroConstraint(fl, sizeVarp));
}
AstVarRef* const sizeVarRefp = new AstVarRef{fl, sizeVarp, VAccess::READ};
AstVarRef* const sizeVarRefp
= new AstVarRef{fl, sizeClassp, sizeVarp, VAccess::READ};
sizeVarRefp->user1(true);
methodp->replaceWith(sizeVarRefp);
VL_DO_DANGLING(methodp->deleteTree(), methodp);