[Support][Parallel] Initialize threadIndex and add assertion checking its usage.
authorAlexey Lapshin <a.v.lapshin@mail.ru>
Thu, 20 Apr 2023 21:03:21 +0000 (23:03 +0200)
committerAlexey Lapshin <a.v.lapshin@mail.ru>
Tue, 2 May 2023 16:44:15 +0000 (18:44 +0200)
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).

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

lld/ELF/Relocations.cpp
llvm/include/llvm/Support/Parallel.h
llvm/lib/Support/Parallel.cpp

index bda979c..2f7fcd6 100644 (file)
@@ -1537,9 +1537,6 @@ template <class ELFT> void elf::scanRelocations() {
     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) {
index c9bcad6..2e5476a 100644 (file)
@@ -30,6 +30,14 @@ namespace parallel {
 extern ThreadPoolStrategy strategy;
 
 #if LLVM_ENABLE_THREADS
+#define GET_THREAD_INDEX_IMPL                                                  \
+  if (parallel::strategy.ThreadsRequested == 1)                                \
+    return 0;                                                                  \
+  assert((threadIndex != UINT_MAX) &&                                          \
+         "getThreadIndex() must be called from a thread created by "           \
+         "ThreadPoolExecutor");                                                \
+  return threadIndex;
+
 #ifdef _WIN32
 // Direct access to thread_local variables from a different DLL isn't
 // possible with Windows Native TLS.
@@ -38,7 +46,7 @@ unsigned getThreadIndex();
 // Don't access this directly, use the getThreadIndex wrapper.
 extern thread_local unsigned threadIndex;
 
-inline unsigned getThreadIndex() { return threadIndex; }
+inline unsigned getThreadIndex() { GET_THREAD_INDEX_IMPL; }
 #endif
 #else
 inline unsigned getThreadIndex() { return 0; }
index df292eb..95956bb 100644 (file)
@@ -24,11 +24,11 @@ namespace parallel {
 #if LLVM_ENABLE_THREADS
 
 #ifdef _WIN32
-static thread_local unsigned threadIndex;
+static thread_local unsigned threadIndex = UINT_MAX;
 
-unsigned getThreadIndex() { return threadIndex; }
+unsigned getThreadIndex() { GET_THREAD_INDEX_IMPL; }
 #else
-thread_local unsigned threadIndex;
+thread_local unsigned threadIndex = UINT_MAX;
 #endif
 
 namespace detail {
@@ -99,10 +99,13 @@ public:
 
   void add(std::function<void()> F, bool Sequential = false) override {
     {
-      bool UseSequentialQueue =
-          Sequential || parallel::strategy.ThreadsRequested == 1;
+      if (parallel::strategy.ThreadsRequested == 1) {
+        F();
+        return;
+      }
+
       std::lock_guard<std::mutex> Lock(Mutex);
-      if (UseSequentialQueue)
+      if (Sequential)
         WorkQueueSequential.emplace_front(std::move(F));
       else
         WorkQueue.emplace_back(std::move(F));
@@ -217,13 +220,9 @@ void TaskGroup::spawn(std::function<void()> F, bool Sequential) {
 
 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;