[lld] r355029 - [LLD][COFF] Support /threads[:no] like the ELF driver

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 27 12:53:50 PST 2019


Author: aganea
Date: Wed Feb 27 12:53:50 2019
New Revision: 355029

URL: http://llvm.org/viewvc/llvm-project?rev=355029&view=rev
Log:
[LLD][COFF] Support /threads[:no] like the ELF driver

Differential review: https://reviews.llvm.org/D58594

Modified:
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/MapFile.cpp
    lld/trunk/COFF/Options.td
    lld/trunk/COFF/PDB.cpp
    lld/trunk/COFF/Writer.cpp
    lld/trunk/include/lld/Common/Threads.h
    lld/trunk/test/COFF/pdb-globals.test

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=355029&r1=355028&r2=355029&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Wed Feb 27 12:53:50 2019
@@ -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 @@ void LinkerDriver::link(ArrayRef<const c
     return;
   }
 
+  lld::ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_threads_no, true);
+
   if (Args.hasArg(OPT_show_timing))
     Config->ShowTiming = true;
 

Modified: lld/trunk/COFF/MapFile.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/MapFile.cpp?rev=355029&r1=355028&r2=355029&view=diff
==============================================================================
--- lld/trunk/COFF/MapFile.cpp (original)
+++ lld/trunk/COFF/MapFile.cpp Wed Feb 27 12:53:50 2019
@@ -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 SymbolMapTy getSectionSyms(ArrayR
 static DenseMap<DefinedRegular *, std::string>
 getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
   std::vector<std::string> 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]);

Modified: lld/trunk/COFF/Options.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Options.td?rev=355029&r1=355028&r2=355029&view=diff
==============================================================================
--- lld/trunk/COFF/Options.td (original)
+++ lld/trunk/COFF/Options.td Wed Feb 27 12:53:50 2019
@@ -163,6 +163,9 @@ def rsp_quoting : Joined<["--"], "rsp-qu
   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">;

Modified: lld/trunk/COFF/PDB.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/PDB.cpp?rev=355029&r1=355028&r2=355029&view=diff
==============================================================================
--- lld/trunk/COFF/PDB.cpp (original)
+++ lld/trunk/COFF/PDB.cpp Wed Feb 27 12:53:50 2019
@@ -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 <memory>
@@ -1340,10 +1340,9 @@ void PDBLinker::addObjectsToPDB() {
 
   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);
   }

Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=355029&r1=355028&r2=355029&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Wed Feb 27 12:53:50 2019
@@ -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 @@ void Writer::mergeSections() {
 // 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 @@ void Writer::writeSections() {
     // 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 @@ void Writer::sortExceptionTable() {
   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>((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>((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";

Modified: lld/trunk/include/lld/Common/Threads.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Common/Threads.h?rev=355029&r1=355028&r2=355029&view=diff
==============================================================================
--- lld/trunk/include/lld/Common/Threads.h (original)
+++ lld/trunk/include/lld/Common/Threads.h Wed Feb 27 12:53:50 2019
@@ -80,6 +80,13 @@ inline void parallelForEachN(size_t Begi
     for_each_n(llvm::parallel::seq, Begin, End, Fn);
 }
 
+template <typename R, class FuncTy> 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

Modified: lld/trunk/test/COFF/pdb-globals.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/pdb-globals.test?rev=355029&r1=355028&r2=355029&view=diff
==============================================================================
--- lld/trunk/test/COFF/pdb-globals.test (original)
+++ lld/trunk/test/COFF/pdb-globals.test Wed Feb 27 12:53:50 2019
@@ -2,6 +2,11 @@ RUN: yaml2obj %S/Inputs/pdb-globals.yaml
 RUN: lld-link /debug /nodefaultlib /entry:main /out:%t.exe /pdb:%t.pdb %t.obj
 RUN: llvm-pdbutil dump -symbols -globals %t.pdb | FileCheck %s
 
+RUN: lld-link /debug /nodefaultlib /entry:main /out:%t.exe /pdb:%t.pdb %t.obj /threads
+RUN: llvm-pdbutil dump -symbols -globals %t.pdb | FileCheck %s
+RUN: lld-link /debug /nodefaultlib /entry:main /out:%t.exe /pdb:%t.pdb %t.obj /threads:no
+RUN: llvm-pdbutil dump -symbols -globals %t.pdb | FileCheck %s
+
 # Test that we correctly distribute symbols between the globals and module
 # symbol streams.  Specifically:
 #  * S_UDT, S_GDATA32, and S_CONSTANT end up in the globals stream, and are




More information about the llvm-commits mailing list