[PATCH] D148916: [Support][Parallel] Initialize threadIndex and add assertion checking its usage.
Alexey Lapshin via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 21 04:58:35 PDT 2023
avl created this revision.
avl added reviewers: MaskRay, andrewng, dexonsmith.
Herald added subscribers: arphaman, hiraditya, arichardson, emaste.
Herald added a project: All.
avl requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
That patch adds a check for threadIndex being used with only threads
created by ThreadPoolExecutor. This helps catch two types of errors:
1. If a thread is created not by ThreadPoolExecutor its index may clash with the index of another thread. Using threadIndex, in that case, may lead to a data race.
2. Index of the main thread(threadIndex == 0) currently clashes with the index of thread0 in ThreadPoolExecutor threads. That may lead to a data race if main thread and thread0 are executed concurrently.
This patch allows execution tasks on the main thread only in case
parallel::strategy.ThreadsRequested == 1. In all other cases,
assertions check that threadIndex != UINT_MAX(i.e. that task
is executed on a thread created by ThreadPoolExecutor).
Depends on D148728 <https://reviews.llvm.org/D148728>
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D148916
Files:
lld/ELF/Relocations.cpp
llvm/include/llvm/Support/Parallel.h
llvm/lib/Support/Parallel.cpp
Index: llvm/lib/Support/Parallel.cpp
===================================================================
--- llvm/lib/Support/Parallel.cpp
+++ llvm/lib/Support/Parallel.cpp
@@ -24,11 +24,17 @@
#if LLVM_ENABLE_THREADS
#ifdef _WIN32
-static thread_local unsigned threadIndex;
-
-unsigned getThreadIndex() { return threadIndex; }
+static thread_local unsigned threadIndex = UINT_MAX;
+
+unsigned getThreadIndex() {
+ assert(((parallel::strategy.ThreadsRequested == 1) ||
+ (threadIndex != UINT_MAX)) &&
+ "getThreadIndex() must be called from the thread created by "
+ "ThreadPoolExecutor");
+ return threadIndex;
+}
#else
-thread_local unsigned threadIndex;
+thread_local unsigned threadIndex = UINT_MAX;
#endif
namespace detail {
@@ -216,13 +222,9 @@
void llvm::parallelFor(size_t Begin, size_t End,
llvm::function_ref<void(size_t)> Fn) {
- // If we have zero or one items, then do not incur the overhead of spinning up
- // a task group. They are surprisingly expensive, and because they do not
- // support nested parallelism, a single entry task group can block parallel
- // execution underneath them.
#if LLVM_ENABLE_THREADS
- auto NumItems = End - Begin;
- if (NumItems > 1 && parallel::strategy.ThreadsRequested != 1) {
+ if (parallel::strategy.ThreadsRequested != 1) {
+ auto NumItems = End - Begin;
// Limit the number of tasks to MaxTasksPerGroup to limit job scheduling
// overhead on large inputs.
auto TaskSize = NumItems / parallel::detail::MaxTasksPerGroup;
Index: llvm/include/llvm/Support/Parallel.h
===================================================================
--- llvm/include/llvm/Support/Parallel.h
+++ llvm/include/llvm/Support/Parallel.h
@@ -38,7 +38,13 @@
// Don't access this directly, use the getThreadIndex wrapper.
extern thread_local unsigned threadIndex;
-inline unsigned getThreadIndex() { return threadIndex; }
+inline unsigned getThreadIndex() {
+ assert(((parallel::strategy.ThreadsRequested == 1) ||
+ (threadIndex != UINT_MAX)) &&
+ "getThreadIndex() must be called from the thread created by "
+ "ThreadPoolExecutor");
+ return threadIndex;
+}
#endif
#else
inline unsigned getThreadIndex() { return 0; }
Index: lld/ELF/Relocations.cpp
===================================================================
--- lld/ELF/Relocations.cpp
+++ lld/ELF/Relocations.cpp
@@ -1537,9 +1537,6 @@
tg.spawn(fn, serial);
}
- // Both the main thread and thread pool index 0 use getThreadIndex()==0. Be
- // careful that they don't concurrently run scanSections. When serial is
- // true, fn() has finished at this point, so running execute is safe.
tg.spawn([] {
RelocationScanner scanner;
for (Partition &part : partitions) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148916.515682.patch
Type: text/x-patch
Size: 2800 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230421/e3f159aa/attachment.bin>
More information about the llvm-commits
mailing list