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,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; 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,7 +18,13 @@ #include 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 {