From 6aa6c45c730b4d0546d2867577a6973897b43021 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Wed, 1 Apr 2026 10:47:35 +0100 Subject: [PATCH] Internals: Add DfgGraph::neighborhood for debugging --- src/V3Dfg.cpp | 28 ++++++++++++++++++++++++++++ src/V3Dfg.h | 3 +++ 2 files changed, 31 insertions(+) diff --git a/src/V3Dfg.cpp b/src/V3Dfg.cpp index cb203d66c..0107b2ace 100644 --- a/src/V3Dfg.cpp +++ b/src/V3Dfg.cpp @@ -585,6 +585,34 @@ DfgGraph::sinkCone(const std::vector& vtxps) const { return dfgGraphCollectCone(vtxps); } +std::unique_ptr> +DfgGraph::neighborhood(const std::vector& vtxps, size_t n) const { + // Neighborhood + std::vector vec = vtxps; + // Set of already visited vertices + std::unordered_set res{vec.begin(), vec.end()}; + // Expand neihborhood by 'n' hops + size_t begin = 0; + size_t end = vec.size(); + for (size_t hops = 1; hops <= n; ++hops) { + for (size_t i = begin; i < end; ++i) { + const DfgVertex* const vtxp = vec[i]; + vtxp->foreachSink([&](const DfgVertex& dst) { + if (res.insert(&dst).second) vec.push_back(&dst); + return false; + }); + vtxp->foreachSource([&](const DfgVertex& src) { + if (res.insert(&src).second) vec.push_back(&src); + return false; + }); + } + begin = end; + end = vec.size(); + } + // Move out the results + return std::make_unique>(std::move(res)); +} + //------------------------------------------------------------------------------ // DfgVertex diff --git a/src/V3Dfg.h b/src/V3Dfg.h index 9e9ab18b9..aff13d8fc 100644 --- a/src/V3Dfg.h +++ b/src/V3Dfg.h @@ -549,6 +549,9 @@ public: // Returns the set of vertices in the downstream cones of the given vertices std::unique_ptr> sinkCone(const std::vector&) const VL_MT_DISABLED; + // Returns the set of vertices within an 'n' hop neighborhood of the given vertices + std::unique_ptr> + neighborhood(const std::vector&, size_t n) const VL_MT_DISABLED; }; namespace V3Dfg {