[llvm] [ScheduleDAG](NFC) Allocate worklist once in `ScheduleDAGTopologicalSort` (PR #179256)

Nathan Corbyn via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 08:03:16 PDT 2026


https://github.com/cofibrant updated https://github.com/llvm/llvm-project/pull/179256

>From b7e3dacdd78aa9e1b05b5ca882b09ae885fd04dc Mon Sep 17 00:00:00 2001
From: Nathan Corbyn <n_corbyn at apple.com>
Date: Tue, 6 Oct 2026 13:48:21 +0100
Subject: [PATCH 1/2] [ScheduleDAG](NFC) Allocate worklist once

---
 llvm/include/llvm/CodeGen/ScheduleDAG.h |  2 ++
 llvm/lib/CodeGen/ScheduleDAG.cpp        | 13 ++++++-------
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/ScheduleDAG.h b/llvm/include/llvm/CodeGen/ScheduleDAG.h
index 0617ef3c954b7..e4e7f25aa0bba 100644
--- a/llvm/include/llvm/CodeGen/ScheduleDAG.h
+++ b/llvm/include/llvm/CodeGen/ScheduleDAG.h
@@ -749,6 +749,8 @@ class TargetRegisterInfo;
     std::vector<int> Node2Index;
     /// a set of nodes visited during a DFS traversal.
     BitVector Visited;
+    /// a worklist for use during traversals.
+    std::vector<const SUnit *> WorkList;
     /// Cache of reachability queries. {A, B} -> true if B is reachable from A.
     /// The keys are SUnit NodeNums.
     DenseMap<std::pair<int, int>, bool> Reachable;
diff --git a/llvm/lib/CodeGen/ScheduleDAG.cpp b/llvm/lib/CodeGen/ScheduleDAG.cpp
index 7ac5a816b1368..c1ed0db8de09d 100644
--- a/llvm/lib/CodeGen/ScheduleDAG.cpp
+++ b/llvm/lib/CodeGen/ScheduleDAG.cpp
@@ -476,12 +476,13 @@ void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
   Reachable.clear();
 
   unsigned DAGSize = SUnits.size();
-  std::vector<SUnit*> WorkList;
-  WorkList.reserve(DAGSize);
 
+  WorkList.reserve(DAGSize);
   Index2Node.resize(DAGSize);
   Node2Index.resize(DAGSize);
 
+  WorkList.clear();
+
   // Initialize the data structures.
   if (ExitSU)
     WorkList.push_back(ExitSU);
@@ -501,7 +502,7 @@ void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
 
   int Id = DAGSize;
   while (!WorkList.empty()) {
-    SUnit *SU = WorkList.back();
+    const SUnit *SU = WorkList.back();
     WorkList.pop_back();
     if (SU->NodeNum < DAGSize)
       Allocate(SU->NodeNum, --Id);
@@ -578,8 +579,7 @@ void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
 
 void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
                                      bool &HasLoop) {
-  std::vector<const SUnit*> WorkList;
-  WorkList.reserve(SUnits.size());
+  WorkList.clear();
 
   WorkList.push_back(SU);
   do {
@@ -606,7 +606,6 @@ void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
 std::vector<int> ScheduleDAGTopologicalSort::GetSubGraph(const SUnit &StartSU,
                                                          const SUnit &TargetSU,
                                                          bool &Success) {
-  std::vector<const SUnit*> WorkList;
   int LowerBound = Node2Index[StartSU.NodeNum];
   int UpperBound = Node2Index[TargetSU.NodeNum];
   bool Found = false;
@@ -618,7 +617,7 @@ std::vector<int> ScheduleDAGTopologicalSort::GetSubGraph(const SUnit &StartSU,
     return Nodes;
   }
 
-  WorkList.reserve(SUnits.size());
+  WorkList.clear();
   Visited.reset();
 
   // Starting from StartSU, visit all successors up

>From 3d429c5a71ef172504be0a4d73bc76ea8a4d8800 Mon Sep 17 00:00:00 2001
From: Nathan Corbyn <n_corbyn at apple.com>
Date: Tue, 6 Oct 2026 16:02:46 +0100
Subject: [PATCH 2/2] Address review comments

---
 llvm/include/llvm/CodeGen/ScheduleDAG.h | 3 ++-
 llvm/lib/CodeGen/ScheduleDAG.cpp        | 6 +++---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/ScheduleDAG.h b/llvm/include/llvm/CodeGen/ScheduleDAG.h
index e4e7f25aa0bba..aab01ce42e7f3 100644
--- a/llvm/include/llvm/CodeGen/ScheduleDAG.h
+++ b/llvm/include/llvm/CodeGen/ScheduleDAG.h
@@ -749,7 +749,8 @@ class TargetRegisterInfo;
     std::vector<int> Node2Index;
     /// a set of nodes visited during a DFS traversal.
     BitVector Visited;
-    /// a worklist for use during traversals.
+    /// A worklist for use during traversals. Retained after traversals so must
+    /// be cleared before use.
     std::vector<const SUnit *> WorkList;
     /// Cache of reachability queries. {A, B} -> true if B is reachable from A.
     /// The keys are SUnit NodeNums.
diff --git a/llvm/lib/CodeGen/ScheduleDAG.cpp b/llvm/lib/CodeGen/ScheduleDAG.cpp
index c1ed0db8de09d..14ede38821d27 100644
--- a/llvm/lib/CodeGen/ScheduleDAG.cpp
+++ b/llvm/lib/CodeGen/ScheduleDAG.cpp
@@ -477,10 +477,10 @@ void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
 
   unsigned DAGSize = SUnits.size();
 
-  WorkList.reserve(DAGSize);
-  Index2Node.resize(DAGSize);
-  Node2Index.resize(DAGSize);
+ Index2Node.resize(DAGSize);
+ Node2Index.resize(DAGSize);
 
+  WorkList.reserve(DAGSize);
   WorkList.clear();
 
   // Initialize the data structures.



More information about the llvm-commits mailing list