[lld] [llvm] [Support] Support nested parallel TaskGroup via work-stealing (PR #189293)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 31 23:22:17 PDT 2026


================
@@ -96,17 +96,47 @@ class ThreadPoolExecutor {
     static void call(void *Ptr) { ((ThreadPoolExecutor *)Ptr)->stop(); }
   };
 
-  void add(std::function<void()> F) {
+  struct WorkItem {
+    std::function<void()> F;
+    std::reference_wrapper<parallel::detail::Latch> L;
+    void operator()() {
+      F();
+      L.get().dec();
+    }
+  };
+
+  void add(std::function<void()> F, parallel::detail::Latch &L) {
     {
       std::lock_guard<std::mutex> Lock(Mutex);
-      WorkStack.push_back(std::move(F));
+      WorkStack.push_back({std::move(F), std::ref(L)});
     }
     Cond.notify_one();
   }
 
+  // Execute tasks from the work queue until the latch reaches zero.
+  // Used by nested TaskGroups (on worker threads) to prevent deadlock:
+  // instead of blocking in sync(), actively help drain the queue.
+  void helpSync(const parallel::detail::Latch &L) {
+    while (L.getCount() != 0) {
+      std::unique_lock<std::mutex> Lock(Mutex);
+      if (WorkStack.empty())
----------------
MaskRay wrote:

Added

https://github.com/llvm/llvm-project/pull/189293


More information about the llvm-commits mailing list