From 34e37d7bd08758a62a7e4750919fd58c1ac2a5d9 Mon Sep 17 00:00:00 2001 From: Krzysztof Bieganski Date: Mon, 15 Jul 2024 16:42:41 +0200 Subject: [PATCH] Keep one member map for randomize method creation (#5263) No functional change intended. Signed-off-by: Krzysztof Bieganski --- src/V3Randomize.cpp | 20 +++++++++++--------- src/V3Randomize.h | 6 ++++-- src/V3Width.cpp | 11 ++++------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index 454cd3176..d7c727868 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -710,7 +710,7 @@ class RandomizeVisitor final : public VNVisitor { iterateChildren(nodep); if (!nodep->user1()) return; // Doesn't need randomize, or already processed UINFO(9, "Define randomize() for " << nodep << endl); - AstFunc* const funcp = V3Randomize::newRandomizeFunc(nodep); + AstFunc* const funcp = V3Randomize::newRandomizeFunc(m_memberMap, nodep); nodep->user3p(funcp); AstVar* const fvarp = VN_AS(funcp->fvarp(), Var); addPrePostCall(nodep, funcp, "pre_randomize"); @@ -758,7 +758,8 @@ class RandomizeVisitor final : public VNVisitor { "Unsupported: random member variable with type of a current class"); continue; } - AstFunc* const memberFuncp = V3Randomize::newRandomizeFunc(classRefp->classp()); + AstFunc* const memberFuncp + = V3Randomize::newRandomizeFunc(m_memberMap, classRefp->classp()); AstMethodCall* const callp = new AstMethodCall{ fl, new AstVarRef{fl, memberVarp, VAccess::WRITE}, "randomize", nullptr}; callp->taskp(memberFuncp); @@ -784,7 +785,8 @@ class RandomizeVisitor final : public VNVisitor { if (nodep->user2p()) return; // Already visited AstNodeFTask* const newp = VN_AS(m_memberMap.findMember(m_modp, "new"), NodeFTask); UASSERT_OBJ(newp, m_modp, "No new() in class"); - AstFunc* const randomizep = V3Randomize::newRandomizeFunc(VN_AS(m_modp, Class)); + AstFunc* const randomizep + = V3Randomize::newRandomizeFunc(m_memberMap, VN_AS(m_modp, Class)); AstTask* const taskp = newSetupConstraintTask(VN_AS(m_modp, Class), nodep->name()); nodep->user2p(taskp); AstTaskRef* const setupTaskRefp @@ -902,7 +904,7 @@ class RandomizeVisitor final : public VNVisitor { localGenp->funcLocal(true); AstFunc* const randomizeFuncp - = V3Randomize::newRandomizeFunc(classp, m_inlineUniqueNames.get(nodep)); + = V3Randomize::newRandomizeFunc(m_memberMap, classp, m_inlineUniqueNames.get(nodep)); // Detach the expression and prepare variable copies const CaptureFrame captured{withp->exprp(), classp, false}; @@ -994,10 +996,9 @@ void V3Randomize::randomizeNetlist(AstNetlist* nodep) { V3Global::dumpCheckGlobalTree("randomize", 0, dumpTreeEitherLevel() >= 3); } -AstFunc* V3Randomize::newRandomizeFunc(AstClass* nodep, const std::string& name) { - VMemberMap memberMap; +AstFunc* V3Randomize::newRandomizeFunc(VMemberMap& memberMap, AstClass* nodep, + const std::string& name) { AstFunc* funcp = VN_AS(memberMap.findMember(nodep, name), Func); - if (!funcp) { v3Global.useRandomizeMethods(true); AstNodeDType* const dtypep @@ -1013,14 +1014,14 @@ AstFunc* V3Randomize::newRandomizeFunc(AstClass* nodep, const std::string& name) funcp->classMethod(true); funcp->isVirtual(nodep->isExtended()); nodep->addMembersp(funcp); + memberMap.insert(nodep, funcp); AstClass* const basep = nodep->baseMostClassp(); basep->needRNG(true); } return funcp; } -AstFunc* V3Randomize::newSRandomFunc(AstClass* nodep) { - VMemberMap memberMap; +AstFunc* V3Randomize::newSRandomFunc(VMemberMap& memberMap, AstClass* nodep) { AstClass* const basep = nodep->baseMostClassp(); AstFunc* funcp = VN_AS(memberMap.findMember(basep, "srandom"), Func); if (!funcp) { @@ -1036,6 +1037,7 @@ AstFunc* V3Randomize::newSRandomFunc(AstClass* nodep) { funcp->classMethod(true); funcp->isVirtual(false); basep->addMembersp(funcp); + memberMap.insert(nodep, funcp); funcp->addStmtsp(new AstCStmt{basep->fileline(), "__Vm_rng.srandom(seed);\n"}); basep->needRNG(true); } diff --git a/src/V3Randomize.h b/src/V3Randomize.h index e7359a95d..eeb2077b0 100644 --- a/src/V3Randomize.h +++ b/src/V3Randomize.h @@ -26,13 +26,15 @@ class AstClass; class AstFunc; class AstNetlist; +class VMemberMap; + class V3Randomize final { public: static void randomizeNetlist(AstNetlist* nodep) VL_MT_DISABLED; - static AstFunc* newRandomizeFunc(AstClass* nodep, + static AstFunc* newRandomizeFunc(VMemberMap& memberMap, AstClass* nodep, const std::string& name = "randomize") VL_MT_DISABLED; - static AstFunc* newSRandomFunc(AstClass* nodep) VL_MT_DISABLED; + static AstFunc* newSRandomFunc(VMemberMap& memberMap, AstClass* nodep) VL_MT_DISABLED; }; #endif // Guard diff --git a/src/V3Width.cpp b/src/V3Width.cpp index 9e5320d85..ce4a8ae91 100644 --- a/src/V3Width.cpp +++ b/src/V3Width.cpp @@ -3716,13 +3716,11 @@ class WidthVisitor final : public VNVisitor { adtypep->findBitDType(), adtypep); methodOkArguments(nodep, 0, 0); methodCallLValueRecurse(nodep, nodep->fromp(), VAccess::WRITE); - V3Randomize::newRandomizeFunc(first_classp); - m_memberMap.clear(); + V3Randomize::newRandomizeFunc(m_memberMap, first_classp); } else if (nodep->name() == "srandom") { methodOkArguments(nodep, 1, 1); methodCallLValueRecurse(nodep, nodep->fromp(), VAccess::WRITE); - V3Randomize::newSRandomFunc(first_classp); - m_memberMap.clear(); + V3Randomize::newSRandomFunc(m_memberMap, first_classp); } UASSERT_OBJ(first_classp, nodep, "Unlinked"); for (AstClass* classp = first_classp; classp;) { @@ -5956,10 +5954,9 @@ class WidthVisitor final : public VNVisitor { AstClass* const classp = VN_CAST(nodep->classOrPackagep(), Class); UASSERT_OBJ(classp, nodep, "Should have failed in V3LinkDot"); if (nodep->name() == "randomize") { - nodep->taskp(V3Randomize::newRandomizeFunc(classp)); - m_memberMap.clear(); + nodep->taskp(V3Randomize::newRandomizeFunc(m_memberMap, classp)); } else if (nodep->name() == "srandom") { - nodep->taskp(V3Randomize::newSRandomFunc(classp)); + nodep->taskp(V3Randomize::newSRandomFunc(m_memberMap, classp)); m_memberMap.clear(); } else if (nodep->name() == "get_randstate") { methodOkArguments(nodep, 0, 0);