[PATCH] D133759: [Support] Access threadIndex via a wrapper function

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 13 01:22:56 PDT 2022


mstorsjo created this revision.
mstorsjo added reviewers: MaskRay, andrewng.
Herald added subscribers: StephenFan, arphaman, hiraditya, arichardson, emaste.
Herald added a project: All.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133759

Files:
  lld/ELF/Relocations.cpp
  lld/ELF/SyntheticSections.h
  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
@@ -18,7 +18,13 @@
 #include <vector>
 
 llvm::ThreadPoolStrategy llvm::parallel::strategy;
+#ifdef _WIN32
+static thread_local unsigned threadIndex;
+
+unsigned llvm::parallel::getThreadIndex() { return threadIndex; }
+#else
 thread_local unsigned llvm::parallel::threadIndex;
+#endif
 
 namespace llvm {
 namespace parallel {
Index: llvm/include/llvm/Support/Parallel.h
===================================================================
--- llvm/include/llvm/Support/Parallel.h
+++ llvm/include/llvm/Support/Parallel.h
@@ -28,8 +28,17 @@
 // this file. It defaults to using all hardware threads and should be
 // initialized before the first use of parallel routines.
 extern ThreadPoolStrategy strategy;
+#ifdef _WIN32
+// Direct access to thread_local variables from a different 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
+
 namespace detail {
 class Latch {
   uint32_t Count;
Index: lld/ELF/SyntheticSections.h
===================================================================
--- lld/ELF/SyntheticSections.h
+++ lld/ELF/SyntheticSections.h
@@ -557,7 +557,7 @@
 
 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>
Index: lld/ELF/Relocations.cpp
===================================================================
--- lld/ELF/Relocations.cpp
+++ lld/ELF/Relocations.cpp
@@ -887,7 +887,7 @@
   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 @@
       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([] {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133759.459674.patch
Type: text/x-patch
Size: 2654 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220913/b34d44cf/attachment.bin>


More information about the llvm-commits mailing list