[llvm-branch-commits] [llvm] 0764636 - [Support] Avoid using main thread for llvm::parallelFor().

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Jan 27 20:42:57 PST 2023


Author: Alexey Lapshin
Date: 2023-01-27T20:40:41-08:00
New Revision: 0764636b95f1b6f523202a291b9534eb826dd715

URL: https://github.com/llvm/llvm-project/commit/0764636b95f1b6f523202a291b9534eb826dd715
DIFF: https://github.com/llvm/llvm-project/commit/0764636b95f1b6f523202a291b9534eb826dd715.diff

LOG: [Support] Avoid using main thread for llvm::parallelFor().

The llvm::parallelFor() uses threads created by ThreadPoolExecutor as well as main thread.
The index for the main thread matches with the index for the first thread created by ThreadPoolExecutor.
It results in that getThreadIndex returns the same value for different threads.
To avoid thread index clashing - do not use main thread for llvm::parallelFor():

parallel::TaskGroup TG;
for (; Begin + TaskSize < End; Begin += TaskSize) {
  TG.spawn([=, &Fn] {
    for (size_t I = Begin, E = Begin + TaskSize; I != E; ++I)
      Fn(I);
  });
}
for (; Begin != End; ++Begin)    <<<< executed by main thread.
  Fn(Begin);                     <<<<
return;                          <<<<

Differential Revision: https://reviews.llvm.org/D142317

(cherry picked from commit 10a796a0beb22ec604b3038b544cc9058facea79)

Added: 
    

Modified: 
    llvm/lib/Support/Parallel.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/Parallel.cpp b/llvm/lib/Support/Parallel.cpp
index 23ed9d813548e..c256d256be4fa 100644
--- a/llvm/lib/Support/Parallel.cpp
+++ b/llvm/lib/Support/Parallel.cpp
@@ -214,8 +214,12 @@ void llvm::parallelFor(size_t Begin, size_t End,
           Fn(I);
       });
     }
-    for (; Begin != End; ++Begin)
-      Fn(Begin);
+    if (Begin != End) {
+      TG.spawn([=, &Fn] {
+        for (size_t I = Begin; I != End; ++I)
+          Fn(I);
+      });
+    }
     return;
   }
 #endif


        


More information about the llvm-branch-commits mailing list