Fix merging of impure assignments in gate optimization (#6629) (#6630)

This commit is contained in:
Geza Lore
2025-11-03 07:29:39 -05:00
committed by GitHub
parent d3ca79368c
commit faaa2db844
5 changed files with 493 additions and 409 deletions
+13 -8
View File
@@ -1097,6 +1097,7 @@ public:
class GateMergeAssignments final {
GateGraph& m_graph;
size_t m_statAssignMerged = 0; // Statistic tracking
std::vector<GateLogicVertex*> m_toRemove; // Logic vertices to delete
// assemble two Sel into one if possible
AstSel* merge(AstSel* prevSelp, AstSel* currSelp) {
@@ -1146,18 +1147,16 @@ class GateMergeAssignments final {
// replace preSel with newSel
prevSelp->replaceWith(newSelp);
VL_DO_DANGLING(prevSelp->deleteTree(), prevSelp);
// create new rhs for pre assignment
AstNode* const newRhsp = new AstConcat{prevAssignp->rhsp()->fileline(),
prevAssignp->rhsp()->cloneTreePure(false),
assignp->rhsp()->cloneTreePure(false)};
AstNode* const prevRhsp = prevAssignp->rhsp();
prevRhsp->replaceWith(newRhsp);
VL_DO_DANGLING(prevRhsp->deleteTree(), prevRhsp);
// Update RHS of the prev assignment, reusing existing parts (might be impure).
prevAssignp->rhsp(new AstConcat{prevAssignp->rhsp()->fileline(),
prevAssignp->rhsp()->unlinkFrBack(),
assignp->rhsp()->unlinkFrBack()});
// Why do we care about the type of an assignment?
prevAssignp->dtypeChgWidthSigned(prevAssignp->width() + assignp->width(),
prevAssignp->width() + assignp->width(),
VSigning::SIGNED);
// Don't need to delete assignp, will be handled
// We will delete the current assignment
m_toRemove.emplace_back(lVtxp);
// Update the graph
while (V3GraphEdge* const iedgep = lVtxp->inEdges().frontp()) {
@@ -1182,6 +1181,12 @@ class GateMergeAssignments final {
for (V3GraphVertex& vtx : graph.vertices()) {
if (GateVarVertex* const vVtxp = vtx.cast<GateVarVertex>()) process(vVtxp);
}
// Delete merged assignments
for (GateLogicVertex* const lVtxp : m_toRemove) {
AstNode* const nodep = lVtxp->nodep();
VL_DO_DANGLING(nodep->unlinkFrBack()->deleteTree(), nodep);
VL_DO_DANGLING(lVtxp->unlinkDelete(&m_graph), lVtxp);
}
}
~GateMergeAssignments() {