Fix internal error on dist under implication operator in constraints (#7440) (#7442)

Fixes #7440
This commit is contained in:
Yilou Wang
2026-04-19 08:03:19 +02:00
committed by GitHub
parent 707dcea914
commit 29a93fe5bc
3 changed files with 141 additions and 0 deletions
+36
View File
@@ -3998,6 +3998,23 @@ class RandomizeVisitor final : public VNVisitor {
}
}
// Rewrite a LogIf-of-Dist chain into nested AstConstraintIf. The outermost
// AstLogIf shell is left for the caller's AstConstraintExpr to free; inner
// shells are deleted here once their children are transplanted.
AstConstraintIf* liftLogIfChainToConstraintIf(AstLogIf* logIfp) {
FileLine* const fl = logIfp->fileline();
AstNodeExpr* const condp = logIfp->lhsp()->unlinkFrBack();
AstNodeExpr* const rhsp = logIfp->rhsp()->unlinkFrBack();
AstNode* thenBodyp;
if (AstLogIf* const innerLogIfp = VN_CAST(rhsp, LogIf)) {
thenBodyp = liftLogIfChainToConstraintIf(innerLogIfp);
VL_DO_DANGLING(pushDeletep(innerLogIfp), innerLogIfp);
} else {
thenBodyp = new AstConstraintExpr{fl, rhsp};
}
return new AstConstraintIf{fl, condp, thenBodyp, nullptr};
}
// Replace AstDist with weighted bucket selection via AstConstraintIf chain.
// Supports both constant and variable weight expressions.
void lowerDistConstraints(AstTask* taskp, AstNode* constrItemsp) {
@@ -4019,6 +4036,25 @@ class RandomizeVisitor final : public VNVisitor {
AstConstraintExpr* const constrExprp = VN_CAST(itemp, ConstraintExpr);
if (!constrExprp) continue;
// `cond -> x dist {...}` parses as ConstraintExpr(LogIf(cond, Dist)).
// This pass only scans ConstraintExpr/If/Foreach for Dist, so lift the
// LogIf chain into nested ConstraintIf first. Chains like
// `a -> b -> dist` nest accordingly.
if (AstLogIf* const topLogIfp = VN_CAST(constrExprp->exprp(), LogIf)) {
AstNode* chainEndp = topLogIfp->rhsp();
while (AstLogIf* const innerp = VN_CAST(chainEndp, LogIf)) {
chainEndp = innerp->rhsp();
}
if (VN_IS(chainEndp, Dist)) {
AstConstraintIf* const liftedp = liftLogIfChainToConstraintIf(topLogIfp);
constrExprp->replaceWith(liftedp);
VL_DO_DANGLING(pushDeletep(constrExprp), constrExprp);
lowerDistConstraints(taskp, liftedp->thensp());
continue;
}
}
AstDist* const distp = VN_CAST(constrExprp->exprp(), Dist);
if (!distp) continue;