From cc51966ad1bb4acb3ce9e7024dd62b8bae3cf134 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Fri, 30 Sep 2022 11:35:03 +0100 Subject: [PATCH] DFG: Remove unconneced variables early --- src/V3Dfg.cpp | 14 ++++++++++---- src/V3DfgAstToDfg.cpp | 22 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/V3Dfg.cpp b/src/V3Dfg.cpp index a6bdb2411..fb2c74d44 100644 --- a/src/V3Dfg.cpp +++ b/src/V3Dfg.cpp @@ -410,12 +410,18 @@ class ExtractCyclicComponents final { static void packSources(DfgGraph& dfg) { // Remove undriven variable sources dfg.forEachVertex([&](DfgVertex& vtx) { - if (DfgVarPacked* const vtxp = vtx.cast()) { - vtxp->packSources(); + if (DfgVarPacked* const varp = vtx.cast()) { + varp->packSources(); + if (!varp->hasSinks() && varp->arity() == 0) { + VL_DO_DANGLING(varp->unlinkDelete(dfg), varp); + } return; } - if (DfgVarArray* const vtxp = vtx.cast()) { - vtxp->packSources(); + if (DfgVarArray* const varp = vtx.cast()) { + varp->packSources(); + if (!varp->hasSinks() && varp->arity() == 0) { + VL_DO_DANGLING(varp->unlinkDelete(dfg), varp); + } return; } }); diff --git a/src/V3DfgAstToDfg.cpp b/src/V3DfgAstToDfg.cpp index 763a1656d..db81c7d11 100644 --- a/src/V3DfgAstToDfg.cpp +++ b/src/V3DfgAstToDfg.cpp @@ -110,8 +110,7 @@ class AstToDfgVisitor final : public VNVisitor { // Note DfgVertexLValue vertices are not added to m_uncommittedVertices, because we // want to hold onto them via AstVar::user1p, and the AstVar might be referenced via // multiple AstVarRef instances, so we will never revert a DfgVertexLValue once - // created. This means we can end up with DfgVertexLValue vertices in the graph which - // have no connections at all (which is fine for later processing). + // created. We will delete unconnected variable vertices at the end. if (VN_IS(varp->dtypep()->skipRefp(), UnpackArrayDType)) { DfgVarArray* const vtxp = new DfgVarArray{*m_dfgp, varp}; varp->user1p(); @@ -264,6 +263,13 @@ class AstToDfgVisitor final : public VNVisitor { // Canonicalize packed variables void canonicalizePacked() { for (DfgVarPacked* const varp : m_varPackedps) { + // Delete variables with no sinks nor sources (this can happen due to reverting + // uncommitted vertices, which does not remove variables) + if (!varp->hasSinks() && varp->arity() == 0) { + VL_DO_DANGLING(varp->unlinkDelete(*m_dfgp), varp); + continue; + } + // Gather (and unlink) all drivers struct Driver { FileLine* flp; @@ -328,6 +334,17 @@ class AstToDfgVisitor final : public VNVisitor { } } + // Canonicalize array variables + void canonicalizeArray() { + for (DfgVarArray* const varp : m_varArrayps) { + // Delete variables with no sinks nor sources (this can happen due to reverting + // uncommitted vertices, which does not remove variables) + if (!varp->hasSinks() && varp->arity() == 0) { + VL_DO_DANGLING(varp->unlinkDelete(*m_dfgp), varp); + } + } + } + // VISITORS void visit(AstNode* nodep) override { // Conservatively treat this node as unhandled @@ -406,6 +423,7 @@ class AstToDfgVisitor final : public VNVisitor { // Canonicalize variables canonicalizePacked(); + canonicalizeArray(); } public: