Fix loop initialization visibility outside loop (#4237).

This commit is contained in:
Wilson Snyder
2025-07-27 03:42:41 -04:00
parent d93abd002f
commit 1725ee9c52
7 changed files with 111 additions and 4 deletions
+11
View File
@@ -297,6 +297,17 @@ class UnrollVisitor final : public VNVisitor {
++m_statLoops;
AstNode* newbodysp = nullptr;
if (initp && !m_generate) { // Set variable to initial value (may optimize away later)
AstNode* clonep = initp->cloneTree(true);
AstConst* varValuep = new AstConst{nodep->fileline(), loopValue};
// Iteration requires a back, so put under temporary node
AstBegin* tempp = new AstBegin{nodep->fileline(), "[EditWrapper]", clonep};
replaceVarRef(clonep, varValuep);
clonep = tempp->stmtsp()->unlinkFrBackWithNext();
VL_DO_CLEAR(tempp->deleteTree(), tempp = nullptr);
VL_DO_DANGLING(pushDeletep(varValuep), varValuep);
newbodysp = clonep;
}
if (stmtsp) {
int times = 0;
while (true) {