[lld] e280940 - [Support] Access threadIndex via a wrapper function

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 13 23:29:57 PDT 2022


Author: Martin Storsjö
Date: 2022-09-14T09:19:27+03:00
New Revision: e280940bfb33eb21925b0b13f04e3b87289b9dea

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

LOG: [Support] Access threadIndex via a wrapper function

On Unix platforms, this wrapper function is inline, so it should
expand to the same direct access to the thread local variable. On
Windows, it's a non-inline function within Parallel.cpp, allowing
making the thread_local variable static.

Windows Native TLS doesn't support direct access to thread local
variables in a different DLL, and GCC/binutils on Windows occasionally
has problems with non-static thread local variables too.

This fixes mingw dylib builds with native TLS after
e6aebff67426fa0f9779a0c19d6188a043bf15e7.

At the same time, move the whole thread local variable within
    #if LLVM_ENABLE_THREADS
to fix builds without threading support.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 36166c6f91383..ecb0c48cee140 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -887,7 +887,7 @@ static void addRelativeReloc(InputSectionBase &isec, uint64_t offsetInSec,
   if (part.relrDyn && isec.alignment >= 2 && offsetInSec % 2 == 0) {
     isec.relocations.push_back({expr, type, offsetInSec, addend, &sym});
     if (shard)
-      part.relrDyn->relocsVec[parallel::threadIndex].push_back(
+      part.relrDyn->relocsVec[parallel::getThreadIndex()].push_back(
           {&isec, offsetInSec});
     else
       part.relrDyn->relocs.push_back({&isec, offsetInSec});
@@ -1562,7 +1562,7 @@ template <class ELFT> void elf::scanRelocations() {
       tg.execute(fn);
   }
 
-  // Both the main thread and thread pool index 0 use threadIndex==0. Be
+  // 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.execute([] {

diff  --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h
index fd2296a4cfca5..a09477e1a4116 100644
--- a/lld/ELF/SyntheticSections.h
+++ b/lld/ELF/SyntheticSections.h
@@ -557,7 +557,7 @@ class RelocationBaseSection : public SyntheticSection {
 
 template <>
 inline void RelocationBaseSection::addReloc<true>(const DynamicReloc &reloc) {
-  relocsVec[llvm::parallel::threadIndex].push_back(reloc);
+  relocsVec[llvm::parallel::getThreadIndex()].push_back(reloc);
 }
 
 template <class ELFT>

diff  --git a/llvm/include/llvm/Support/Parallel.h b/llvm/include/llvm/Support/Parallel.h
index 918edc07d96ae..4500b93b130b8 100644
--- a/llvm/include/llvm/Support/Parallel.h
+++ b/llvm/include/llvm/Support/Parallel.h
@@ -28,8 +28,22 @@ namespace parallel {
 // this file. It defaults to using all hardware threads and should be
 // initialized before the first use of parallel routines.
 extern ThreadPoolStrategy strategy;
+
+#if LLVM_ENABLE_THREADS
+#ifdef _WIN32
+// Direct access to thread_local variables from a 
diff erent DLL isn't
+// possible with Windows Native TLS.
+unsigned getThreadIndex();
+#else
+// Don't access this directly, use the getThreadIndex wrapper.
 extern thread_local unsigned threadIndex;
 
+inline unsigned getThreadIndex() { return threadIndex; }
+#endif
+#else
+inline unsigned getThreadIndex() { return 0; }
+#endif
+
 namespace detail {
 class Latch {
   uint32_t Count;

diff  --git a/llvm/lib/Support/Parallel.cpp b/llvm/lib/Support/Parallel.cpp
index 71c41c24817fe..23ed9d813548e 100644
--- a/llvm/lib/Support/Parallel.cpp
+++ b/llvm/lib/Support/Parallel.cpp
@@ -18,11 +18,19 @@
 #include <vector>
 
 llvm::ThreadPoolStrategy llvm::parallel::strategy;
-thread_local unsigned llvm::parallel::threadIndex;
 
 namespace llvm {
 namespace parallel {
 #if LLVM_ENABLE_THREADS
+
+#ifdef _WIN32
+static thread_local unsigned threadIndex;
+
+unsigned getThreadIndex() { return threadIndex; }
+#else
+thread_local unsigned threadIndex;
+#endif
+
 namespace detail {
 
 namespace {


        


More information about the llvm-commits mailing list