Index: llvm/include/llvm/Support/Threading.h =================================================================== --- llvm/include/llvm/Support/Threading.h +++ llvm/include/llvm/Support/Threading.h @@ -157,6 +157,9 @@ // std::thread per core. bool UseHyperThreads = true; + // Sets an upper bound to the number of hardware threads, or hardware cores. + bool Limit = false; + /// Retrieves the max available threads for the current strategy. This /// accounts for affinity masks and takes advantage of all CPU sockets. unsigned compute_thread_count() const; Index: llvm/lib/Support/Threading.cpp =================================================================== --- llvm/lib/Support/Threading.cpp +++ llvm/lib/Support/Threading.cpp @@ -84,14 +84,16 @@ int computeHostNumHardwareThreads(); unsigned llvm::ThreadPoolStrategy::compute_thread_count() const { - if (ThreadsRequested > 0) - return ThreadsRequested; - int MaxThreadCount = UseHyperThreads ? computeHostNumHardwareThreads() : sys::getHostNumPhysicalCores(); if (MaxThreadCount <= 0) MaxThreadCount = 1; - return MaxThreadCount; + + if (ThreadsRequested == 0) + return MaxThreadCount; + + return Limit ? std::min((unsigned)MaxThreadCount, ThreadsRequested) + : ThreadsRequested; } namespace { Index: llvm/tools/llvm-cov/CodeCoverage.cpp =================================================================== --- llvm/tools/llvm-cov/CodeCoverage.cpp +++ llvm/tools/llvm-cov/CodeCoverage.cpp @@ -946,8 +946,13 @@ auto NumThreads = ViewOpts.NumThreads; // If NumThreads is not specified, auto-detect a good default. - if (NumThreads == 0) - NumThreads = SourceFiles.size(); + ThreadPoolStrategy S; + if (NumThreads > 0) + S = hardware_concurrency(NumThreads); // no upper limit on the # of threads + else { + S = heavyweight_hardware_concurrency(SourceFiles.size()); + S.Limit = true; // limit to max hardware cores + } if (!ViewOpts.hasOutputDirectory() || NumThreads == 1) { for (const std::string &SourceFile : SourceFiles) @@ -955,7 +960,7 @@ ShowFilenames); } else { // In -output-dir mode, it's safe to use multiple threads to print files. - ThreadPool Pool(heavyweight_hardware_concurrency(NumThreads)); + ThreadPool Pool(S); for (const std::string &SourceFile : SourceFiles) Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile, Coverage.get(), Printer.get(), ShowFilenames);