Index: COFF/Driver.cpp =================================================================== --- COFF/Driver.cpp +++ COFF/Driver.cpp @@ -19,6 +19,7 @@ #include "lld/Common/Driver.h" #include "lld/Common/ErrorHandler.h" #include "lld/Common/Memory.h" +#include "lld/Common/Threads.h" #include "lld/Common/Timer.h" #include "lld/Common/Version.h" #include "llvm/ADT/Optional.h" @@ -987,6 +988,8 @@ return; } + lld::ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_threads_no, true); + if (Args.hasArg(OPT_show_timing)) Config->ShowTiming = true; Index: COFF/MapFile.cpp =================================================================== --- COFF/MapFile.cpp +++ COFF/MapFile.cpp @@ -23,7 +23,7 @@ #include "Symbols.h" #include "Writer.h" #include "lld/Common/ErrorHandler.h" -#include "llvm/Support/Parallel.h" +#include "lld/Common/Threads.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -75,7 +75,7 @@ static DenseMap getSymbolStrings(ArrayRef Syms) { std::vector Str(Syms.size()); - for_each_n(parallel::par, (size_t)0, Syms.size(), [&](size_t I) { + parallelForEachN((size_t)0, Syms.size(), [&](size_t I) { raw_string_ostream OS(Str[I]); writeHeader(OS, Syms[I]->getRVA(), 0, 0); OS << Indent16 << toString(*Syms[I]); Index: COFF/Options.td =================================================================== --- COFF/Options.td +++ COFF/Options.td @@ -163,6 +163,9 @@ HelpText<"Quoting style for response files, 'windows' (default) or 'posix'">; def dash_dash_version : Flag<["--"], "version">, HelpText<"Print version information">; +defm threads: B<"threads", + "Run the linker multi-threaded (default)", + "Do not run the linker multi-threaded">; // Flags for debugging def lldmap : F<"lldmap">; Index: COFF/PDB.cpp =================================================================== --- COFF/PDB.cpp +++ COFF/PDB.cpp @@ -15,6 +15,7 @@ #include "Writer.h" #include "lld/Common/ErrorHandler.h" #include "lld/Common/Timer.h" +#include "lld/Common/Threads.h" #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h" @@ -52,7 +53,6 @@ #include "llvm/Support/Errc.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/JamCRC.h" -#include "llvm/Support/Parallel.h" #include "llvm/Support/Path.h" #include "llvm/Support/ScopedPrinter.h" #include @@ -1340,10 +1340,9 @@ if (!Publics.empty()) { // Sort the public symbols and add them to the stream. - sort(parallel::par, Publics.begin(), Publics.end(), - [](const PublicSym32 &L, const PublicSym32 &R) { - return L.Name < R.Name; - }); + parallelSort(Publics, [](const PublicSym32 &L, const PublicSym32 &R) { + return L.Name < R.Name; + }); for (const PublicSym32 &Pub : Publics) GsiBuilder.addPublicSymbol(Pub); } Index: COFF/Writer.cpp =================================================================== --- COFF/Writer.cpp +++ COFF/Writer.cpp @@ -16,6 +16,7 @@ #include "Symbols.h" #include "lld/Common/ErrorHandler.h" #include "lld/Common/Memory.h" +#include "lld/Common/Threads.h" #include "lld/Common/Timer.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/STLExtras.h" @@ -1096,8 +1097,7 @@ // Visits all sections to initialize their relocation targets. void Writer::readRelocTargets() { for (OutputSection *Sec : OutputSections) - for_each(parallel::par, Sec->Chunks.begin(), Sec->Chunks.end(), - [&](Chunk *C) { C->readRelocTargets(); }); + parallelForEach(Sec->Chunks, [&](Chunk *C) { C->readRelocTargets(); }); } // Visits all sections to assign incremental, non-overlapping RVAs and @@ -1613,8 +1613,7 @@ // ADD instructions). if (Sec->Header.Characteristics & IMAGE_SCN_CNT_CODE) memset(SecBuf, 0xCC, Sec->getRawSize()); - for_each(parallel::par, Sec->Chunks.begin(), Sec->Chunks.end(), - [&](Chunk *C) { C->writeTo(SecBuf); }); + parallelForEach(Sec->Chunks, [&](Chunk *C) { C->writeTo(SecBuf); }); } } @@ -1682,14 +1681,16 @@ uint8_t *End = BufAddr(LastPdata) + LastPdata->getSize(); if (Config->Machine == AMD64) { struct Entry { ulittle32_t Begin, End, Unwind; }; - sort(parallel::par, (Entry *)Begin, (Entry *)End, - [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; }); + parallelSort( + MutableArrayRef((Entry *)Begin, (Entry *)End), + [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; }); return; } if (Config->Machine == ARMNT || Config->Machine == ARM64) { struct Entry { ulittle32_t Begin, Unwind; }; - sort(parallel::par, (Entry *)Begin, (Entry *)End, - [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; }); + parallelSort( + MutableArrayRef((Entry *)Begin, (Entry *)End), + [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; }); return; } errs() << "warning: don't know how to handle .pdata.\n"; Index: include/lld/Common/Threads.h =================================================================== --- include/lld/Common/Threads.h +++ include/lld/Common/Threads.h @@ -80,6 +80,13 @@ for_each_n(llvm::parallel::seq, Begin, End, Fn); } +template void parallelSort(R &&Range, FuncTy Fn) { + if (ThreadsEnabled) + sort(llvm::parallel::par, std::begin(Range), std::end(Range), Fn); + else + sort(llvm::parallel::seq, std::begin(Range), std::end(Range), Fn); +} + } // namespace lld #endif