From 12607f57da9aa990eb9ec3f671214058d86ab1b1 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Mon, 12 Sep 2022 19:09:08 -0700 Subject: [PATCH] [ELF] Cache compute_thread_count. NFC --- lld/ELF/Config.h | 2 ++ lld/ELF/Driver.cpp | 1 + lld/ELF/SyntheticSections.cpp | 12 ++++-------- lld/ELF/Writer.cpp | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 26a6d63..fcba921 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -364,6 +364,8 @@ struct Configuration { // this means to map the primary and thread stacks as PROT_MTE. Note: This is // not supported on Android 11 & 12. bool androidMemtagStack; + + unsigned threadCount; }; // The only instance of Configuration struct. diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 369ef02..85d1fb4 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1363,6 +1363,7 @@ static void readConfigs(opt::InputArgList &args) { } if (auto *arg = args.getLastArg(OPT_thinlto_jobs)) config->thinLTOJobs = arg->getValue(); + config->threadCount = parallel::strategy.compute_thread_count(); if (config->ltoo > 3) error("invalid optimization level for LTO: " + Twine(config->ltoo)); diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 291c925..ce041c2 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -2769,10 +2769,8 @@ createSymbols( // of millions for very large executables, so we use multi-threading to // speed it up. constexpr size_t numShards = 32; - size_t concurrency = PowerOf2Floor( - std::min(hardware_concurrency(parallel::strategy.ThreadsRequested) - .compute_thread_count(), - numShards)); + const size_t concurrency = + PowerOf2Floor(std::min(config->threadCount, numShards)); // A sharded map to uniquify symbols by name. auto map = @@ -3257,10 +3255,8 @@ void MergeNoTailSection::finalizeContents() { // Concurrency level. Must be a power of 2 to avoid expensive modulo // operations in the following tight loop. - size_t concurrency = PowerOf2Floor( - std::min(hardware_concurrency(parallel::strategy.ThreadsRequested) - .compute_thread_count(), - numShards)); + const size_t concurrency = + PowerOf2Floor(std::min(config->threadCount, numShards)); // Add section pieces to the builders. parallelFor(0, concurrency, [&](size_t threadId) { diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 9124961..cc8742e 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -317,7 +317,7 @@ template void elf::createSyntheticSections() { StringRef relaDynName = config->isRela ? ".rela.dyn" : ".rel.dyn"; - const unsigned threadCount = parallel::strategy.compute_thread_count(); + const unsigned threadCount = config->threadCount; for (Partition &part : partitions) { auto add = [&](SyntheticSection &sec) { sec.partition = part.getNumber(); -- 2.7.4