[llvm] [SandboxVec][DAG] Mark UnscheduledSuccs as invalid when vectorized (PR #185519)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 10 16:57:53 PDT 2026


https://github.com/vporpo updated https://github.com/llvm/llvm-project/pull/185519

>From 75e12a5d0e3702da093edbb6a9f4f6e0ac56af5f Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vasileios.porpodas at amd.com>
Date: Mon, 9 Mar 2026 20:56:20 +0000
Subject: [PATCH 1/3] [SandboxVec][DAG] Mark UnscheduledSuccs as invalid when
 vectorized

When a DAG node gets scheduled, it's UnscheduledSuccs variable becomes invalid.
Up until now we had no way in the code of expressing this.

This patch converts UnscheduledSuccs to an std::optional in order to protect its use when not valid.
---
 .../SandboxVectorizer/DependencyGraph.h       | 31 +++++++++++++------
 .../SandboxVectorizer/DependencyGraph.cpp     |  6 ++--
 .../SandboxVectorizer/DependencyGraphTest.cpp | 25 ++++++++++++---
 3 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
index 21457602c36a8..d08e84a537e26 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
@@ -98,9 +98,9 @@ class LLVM_ABI DGNode {
   // TODO: Use a PointerIntPair for SubclassID and I.
   /// For isa/dyn_cast etc.
   DGNodeID SubclassID;
-  /// The number of unscheduled successors. This is meaningless after a node
-  /// gets scheduled.
-  unsigned UnscheduledSuccs = 0;
+  /// The number of unscheduled successors. Optional represents whether the
+  /// value is meaningless, e.g., after a node gets scheduled.
+  std::optional<unsigned> UnscheduledSuccs = 0;
   /// This is true if this node has been scheduled.
   bool Scheduled = false;
   /// The scheduler bundle that this node belongs to.
@@ -121,13 +121,17 @@ class LLVM_ABI DGNode {
   DGNode(const DGNode &Other) = delete;
   virtual ~DGNode();
   /// \Returns the number of unscheduled successors.
-  unsigned getNumUnscheduledSuccs() const { return UnscheduledSuccs; }
+  unsigned getNumUnscheduledSuccs() const { return *UnscheduledSuccs; }
+#ifndef NDEBUG
+  /// \returns true unscheduled successors contains valid data (for testing).
+  bool validUnscheduledSuccs() const { return (bool)UnscheduledSuccs; }
+#endif
   // TODO: Make this private?
   void decrUnscheduledSuccs() {
-    assert(UnscheduledSuccs > 0 && "Counting error!");
-    --UnscheduledSuccs;
+    assert(*UnscheduledSuccs > 0 && "Counting error!");
+    --*UnscheduledSuccs;
   }
-  void incrUnscheduledSuccs() { ++UnscheduledSuccs; }
+  void incrUnscheduledSuccs() { ++*UnscheduledSuccs; }
   void resetScheduleState() {
     UnscheduledSuccs = 0;
     Scheduled = false;
@@ -136,7 +140,12 @@ class LLVM_ABI DGNode {
   bool ready() const { return UnscheduledSuccs == 0; }
   /// \Returns true if this node has been scheduled.
   bool scheduled() const { return Scheduled; }
-  void setScheduled(bool NewVal) { Scheduled = NewVal; }
+  void setScheduled(bool NewVal) {
+    Scheduled = NewVal;
+    if (Scheduled)
+      // UnscheduledSuccs is meaningless from this point on, so prohibit its use
+      UnscheduledSuccs = std::nullopt;
+  }
   /// \Returns the scheduling bundle that this node belongs to, or nullptr.
   SchedBundle *getSchedBundle() const { return SB; }
   /// \Returns true if this is before \p Other in program order.
@@ -279,7 +288,8 @@ class MemDGNode final : public DGNode {
     assert(PredN != this && "Trying to add a dependency to self!");
     PredN->MemSuccs.insert(this);
     if (!Scheduled) {
-      ++PredN->UnscheduledSuccs;
+      if (!PredN->Scheduled)
+        ++*PredN->UnscheduledSuccs;
     }
   }
   /// Removes the memory dependency PredN->this. This also updates the
@@ -288,7 +298,8 @@ class MemDGNode final : public DGNode {
     MemPreds.erase(PredN);
     PredN->MemSuccs.erase(this);
     if (!Scheduled) {
-      PredN->decrUnscheduledSuccs();
+      if (!PredN->Scheduled)
+        PredN->decrUnscheduledSuccs();
     }
   }
   /// \Returns true if there is a memory dependency N->this.
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.cpp
index aa893cd176d1f..ff135d1af592b 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.cpp
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.cpp
@@ -260,7 +260,7 @@ void DependencyGraph::setDefUseUnscheduledSuccs(
       auto *OpN = getNode(OpI);
       if (OpN == nullptr)
         continue;
-      ++OpN->UnscheduledSuccs;
+      ++*OpN->UnscheduledSuccs;
     }
   }
 
@@ -292,7 +292,7 @@ void DependencyGraph::setDefUseUnscheduledSuccs(
         continue;
       if (!TopInterval.contains(OpI))
         continue;
-      ++OpN->UnscheduledSuccs;
+      ++*OpN->UnscheduledSuccs;
     }
   }
 }
@@ -546,7 +546,7 @@ void DependencyGraph::notifySetUse(const Use &U, Value *NewSrc) {
     if (auto *NewSrcN = getNode(NewSrcI)) {
       // If CurrSrcN is scheduled there is no point in updating UnscheduleSuccs.
       if (!NewSrcN->scheduled())
-        ++NewSrcN->UnscheduledSuccs;
+        ++*NewSrcN->UnscheduledSuccs;
     }
   }
 }
diff --git a/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp b/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp
index e2514877fd0ba..d5cc2d3f14414 100644
--- a/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp
+++ b/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp
@@ -1256,14 +1256,31 @@ define void @foo(i8 %v0) {
   auto *Add0N = DAG.getNode(Add0);
   auto *Add1N = DAG.getNode(Add1);
   // Mark Add0N and Add1N as scheduled.
+#ifndef NDEBUG
+  EXPECT_TRUE(Add0N->validUnscheduledSuccs());
+#endif
   Add0N->setScheduled(true);
+#ifndef NDEBUG
+  EXPECT_FALSE(Add0N->validUnscheduledSuccs());
+  EXPECT_DEATH(Add0N->getNumUnscheduledSuccs(), ".*");
+#endif
+
+#ifndef NDEBUG
+  EXPECT_TRUE(Add1N->validUnscheduledSuccs());
+#endif
   Add1N->setScheduled(true);
-  EXPECT_EQ(Add0N->getNumUnscheduledSuccs(), 1u);
-  EXPECT_EQ(Add1N->getNumUnscheduledSuccs(), 0u);
+#ifndef NDEBUG
+  EXPECT_FALSE(Add1N->validUnscheduledSuccs());
+  EXPECT_DEATH(Add1N->getNumUnscheduledSuccs(), ".*");
+#endif
 
   // Change Modify's operand and make sure we won't update Add0N's or Add1N's
   // unscheduled succs.
   Modify->setOperand(0, Add1);
-  EXPECT_EQ(Add0N->getNumUnscheduledSuccs(), 1u);
-  EXPECT_EQ(Add1N->getNumUnscheduledSuccs(), 0u);
+#ifndef NDEBUG
+  EXPECT_FALSE(Add0N->validUnscheduledSuccs());
+  EXPECT_DEATH(Add0N->getNumUnscheduledSuccs(), ".*");
+  EXPECT_FALSE(Add1N->validUnscheduledSuccs());
+  EXPECT_DEATH(Add1N->getNumUnscheduledSuccs(), ".*");
+#endif
 }

>From 4dd0768a59e9b9868aab15d24a065eb2e3a45a30 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vasileios.porpodas at amd.com>
Date: Tue, 10 Mar 2026 18:08:37 +0000
Subject: [PATCH 2/3] fixup! [SandboxVec][DAG] Mark UnscheduledSuccs as invalid
 when vectorized

---
 .../Vectorize/SandboxVectorizer/DependencyGraph.h           | 2 +-
 .../Vectorize/SandboxVectorizer/DependencyGraph.cpp         | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
index d08e84a537e26..26ae067068b7a 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
@@ -289,7 +289,7 @@ class MemDGNode final : public DGNode {
     PredN->MemSuccs.insert(this);
     if (!Scheduled) {
       if (!PredN->Scheduled)
-        ++*PredN->UnscheduledSuccs;
+        PredN->incrUnscheduledSuccs();
     }
   }
   /// Removes the memory dependency PredN->this. This also updates the
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.cpp
index ff135d1af592b..7adebebd52dc6 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.cpp
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.cpp
@@ -260,7 +260,7 @@ void DependencyGraph::setDefUseUnscheduledSuccs(
       auto *OpN = getNode(OpI);
       if (OpN == nullptr)
         continue;
-      ++*OpN->UnscheduledSuccs;
+      OpN->incrUnscheduledSuccs();
     }
   }
 
@@ -292,7 +292,7 @@ void DependencyGraph::setDefUseUnscheduledSuccs(
         continue;
       if (!TopInterval.contains(OpI))
         continue;
-      ++*OpN->UnscheduledSuccs;
+      OpN->incrUnscheduledSuccs();
     }
   }
 }
@@ -546,7 +546,7 @@ void DependencyGraph::notifySetUse(const Use &U, Value *NewSrc) {
     if (auto *NewSrcN = getNode(NewSrcI)) {
       // If CurrSrcN is scheduled there is no point in updating UnscheduleSuccs.
       if (!NewSrcN->scheduled())
-        ++*NewSrcN->UnscheduledSuccs;
+        NewSrcN->incrUnscheduledSuccs();
     }
   }
 }

>From 6a20496e5084215c535f0987100167e064906e3e Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vasileios.porpodas at amd.com>
Date: Tue, 10 Mar 2026 23:47:23 +0000
Subject: [PATCH 3/3] fixup! fixup! [SandboxVec][DAG] Mark UnscheduledSuccs as
 invalid when vectorized

---
 .../Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
index 26ae067068b7a..221b099a127be 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
@@ -121,7 +121,10 @@ class LLVM_ABI DGNode {
   DGNode(const DGNode &Other) = delete;
   virtual ~DGNode();
   /// \Returns the number of unscheduled successors.
-  unsigned getNumUnscheduledSuccs() const { return *UnscheduledSuccs; }
+  unsigned getNumUnscheduledSuccs() const {
+    assert((bool)UnscheduledSuccs && "Invalid UnscheduledSuccs!");
+    return *UnscheduledSuccs;
+  }
 #ifndef NDEBUG
   /// \returns true unscheduled successors contains valid data (for testing).
   bool validUnscheduledSuccs() const { return (bool)UnscheduledSuccs; }



More information about the llvm-commits mailing list