Skip to content

Commit 4ffc848

Browse files
quic-aregquic-seaswara
authored andcommitted
Respect --thread-count by setting parallel strategy
ELD's --thread-count option wasn't affecting `llvm::parallelFor()` because `llvm::parallel::strategy` was never set, so LLVM always defaulted to hardware concurrency. Set the strategy from --thread-count so `parallelFor()` uses the requested thread limit. Signed-off-by: quic-areg <aregmi@qti.qualcomm.com>
1 parent f1052f6 commit 4ffc848

6 files changed

Lines changed: 20 additions & 10 deletions

File tree

include/eld/Config/LinkerConfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ class LinkerConfig {
199199
GlobalThreadingEnabled = true;
200200
}
201201

202+
bool useThreads() const {
203+
return GenOptions.threadsEnabled() && GenOptions.numThreads() > 1;
204+
}
205+
202206
void addCommandLine(llvm::StringRef Option, bool Flag);
203207

204208
void addCommandLine(llvm::StringRef Option, const char *);

include/eld/Core/Module.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ class Module {
662662
bool isBackendInitialized() const;
663663

664664
private:
665+
void initThreading();
666+
665667
/// Verifies invariants of 'CreatingSections' linker state.
666668
/// Invariants here means the conditions and rules that 'CreatingSections'
667669
/// state expects to be true.
@@ -731,6 +733,7 @@ class Module {
731733
std::mutex Mutex;
732734
// ----------------- Central thread pool for Linker ---------------
733735
llvm::ThreadPoolInterface *LinkerThreadPool = nullptr;
736+
llvm::ThreadPoolStrategy ThreadingStrategy;
734737

735738
llvm::StringMap<MergeableString *> UniqueNonAllocStrings;
736739
llvm::SmallVector<MergeableString *> AllNonAllocStrings;

lib/Core/Module.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "llvm/Support/Allocator.h"
3636
#include "llvm/Support/ErrorOr.h"
3737
#include "llvm/Support/MemoryBuffer.h"
38+
#include "llvm/Support/Parallel.h"
3839
#include "llvm/Support/StringSaver.h"
3940
#include "llvm/Support/ThreadPool.h"
4041

@@ -51,6 +52,7 @@ Module::Module(LinkerScript &CurScript, LinkerConfig &Config,
5152
Config.options().printTimingStats()),
5253
SymbolNamePool(Config, PM) {
5354
State = LinkState::Initializing;
55+
initThreading();
5456
if (Config.options().isLTOCacheEnabled())
5557
UserLinkerScript.setHashingEnabled();
5658
UserLinkerScript.createSectionMap(CurScript, Config, LayoutInfo);
@@ -831,11 +833,17 @@ void Module::addReferencedSymbol(Section &RefencingSection,
831833
llvm::ThreadPoolInterface *Module::getThreadPool() {
832834
if (LinkerThreadPool)
833835
return LinkerThreadPool;
834-
LinkerThreadPool = eld::make<llvm::StdThreadPool>(
835-
llvm::hardware_concurrency(ThisConfig.options().numThreads()));
836+
LinkerThreadPool = eld::make<llvm::StdThreadPool>(ThreadingStrategy);
836837
return LinkerThreadPool;
837838
}
838839

840+
void Module::initThreading() {
841+
unsigned NumThreads =
842+
ThisConfig.useThreads() ? ThisConfig.options().numThreads() : 1;
843+
ThreadingStrategy = llvm::hardware_concurrency(NumThreads);
844+
llvm::parallel::strategy = ThreadingStrategy;
845+
}
846+
839847
bool Module::verifyInvariantsForCreatingSectionsState() const {
840848
if (!getScript().hasPendingSectionOverride(/*LW=*/nullptr))
841849
return true;

lib/LinkerWrapper/LinkerWrapper.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,7 @@ size_t LinkerWrapper::getPluginThreadCount() const {
590590

591591
bool LinkerWrapper::isMultiThreaded() const {
592592
const eld::LinkerConfig &Config = m_Module.getConfig();
593-
if (Config.options().threadsEnabled()) {
594-
uint32_t NumThreads = Config.options().numThreads();
595-
if (NumThreads > 1)
596-
return true;
597-
}
598-
return false;
593+
return Config.useThreads();
599594
}
600595

601596
plugin::LinkerScript LinkerWrapper::getLinkerScript() {

lib/Object/ObjectLinker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ void ObjectLinker::mergeIdenticalStrings() const {
623623
/// between them wrt string merging. When global string merging is enabled,
624624
/// strings would need to be placed in one Module, so threads should
625625
/// not be used.
626-
bool UseThreads = ThisConfig.options().numThreads() > 1;
626+
bool UseThreads = ThisConfig.useThreads();
627627
bool GlobalMerge = ThisConfig.options().shouldGlobalStringMerge();
628628
llvm::ThreadPoolInterface *Pool = ThisModule->getThreadPool();
629629
auto MergeStrings = [&](OutputSectionEntry *O) {

lib/Readers/ArchiveParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ void ArchiveParser::warnRepeatedMembers(const ArchiveFile &archiveFile) const {
600600
std::unordered_map<uint64_t, size_t> memDataHashToMemIdxMap;
601601
const std::vector<Input *> &archiveMembers = archiveFile.getAllMembers();
602602
std::vector<uint64_t> archiveMemberHashes(archiveMembers.size(), 0);
603-
size_t useThreads = config.options().numThreads() > 1;
603+
size_t useThreads = config.useThreads();
604604
llvm::ThreadPoolInterface *Pool = m_Module.getThreadPool();
605605
auto computeAndSetHash = [&](size_t i) {
606606
archiveMemberHashes[i] =

0 commit comments

Comments
 (0)