Skip to content

Commit 0764636

Browse files
avl-llvmtstellar
authored andcommitted
[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 10a796a)
1 parent 5cc5d71 commit 0764636

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

llvm/lib/Support/Parallel.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,12 @@ void llvm::parallelFor(size_t Begin, size_t End,
214214
Fn(I);
215215
});
216216
}
217-
for (; Begin != End; ++Begin)
218-
Fn(Begin);
217+
if (Begin != End) {
218+
TG.spawn([=, &Fn] {
219+
for (size_t I = Begin; I != End; ++I)
220+
Fn(I);
221+
});
222+
}
219223
return;
220224
}
221225
#endif

0 commit comments

Comments
 (0)