diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp --- a/lld/ELF/Relocations.cpp +++ b/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([] { diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -557,7 +557,7 @@ template <> inline void RelocationBaseSection::addReloc(const DynamicReloc &reloc) { - relocsVec[llvm::parallel::threadIndex].push_back(reloc); + relocsVec[llvm::parallel::getThreadIndex()].push_back(reloc); } template diff --git a/llvm/include/llvm/Support/Parallel.h b/llvm/include/llvm/Support/Parallel.h --- a/llvm/include/llvm/Support/Parallel.h +++ b/llvm/include/llvm/Support/Parallel.h @@ -28,8 +28,22 @@ // 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 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 +#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 --- a/llvm/lib/Support/Parallel.cpp +++ b/llvm/lib/Support/Parallel.cpp @@ -18,11 +18,19 @@ #include 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 {