[lld] r288599 - Factor out common code to a header.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 3 13:24:51 PST 2016


Author: ruiu
Date: Sat Dec  3 15:24:51 2016
New Revision: 288599

URL: http://llvm.org/viewvc/llvm-project?rev=288599&view=rev
Log:
Factor out common code to a header.

Added:
    lld/trunk/ELF/Threads.h
Modified:
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/ICF.cpp
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/SyntheticSections.cpp

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=288599&r1=288598&r2=288599&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Sat Dec  3 15:24:51 2016
@@ -18,9 +18,9 @@
 #include "Strings.h"
 #include "SymbolTable.h"
 #include "Target.h"
+#include "Threads.h"
 #include "Writer.h"
 #include "lld/Config/Version.h"
-#include "lld/Core/Parallel.h"
 #include "lld/Driver/Driver.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSwitch.h"
@@ -830,18 +830,15 @@ template <class ELFT> void LinkerDriver:
 
   // MergeInputSection::splitIntoPieces needs to be called before
   // any call of MergeInputSection::getOffset. Do that.
-  auto Fn = [](InputSectionBase<ELFT> *S) {
-    if (!S->Live)
-      return;
-    if (S->Compressed)
-      S->uncompress();
-    if (auto *MS = dyn_cast<MergeInputSection<ELFT>>(S))
-      MS->splitIntoPieces();
-  };
-  if (Config->Threads)
-    parallel_for_each(Symtab.Sections.begin(), Symtab.Sections.end(), Fn);
-  else
-    std::for_each(Symtab.Sections.begin(), Symtab.Sections.end(), Fn);
+  forEach(Symtab.Sections.begin(), Symtab.Sections.end(),
+          [](InputSectionBase<ELFT> *S) {
+            if (!S->Live)
+              return;
+            if (S->Compressed)
+              S->uncompress();
+            if (auto *MS = dyn_cast<MergeInputSection<ELFT>>(S))
+              MS->splitIntoPieces();
+          });
 
   // Write the result to the file.
   writeResult<ELFT>();

Modified: lld/trunk/ELF/ICF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ICF.cpp?rev=288599&r1=288598&r2=288599&view=diff
==============================================================================
--- lld/trunk/ELF/ICF.cpp (original)
+++ lld/trunk/ELF/ICF.cpp Sat Dec  3 15:24:51 2016
@@ -69,8 +69,8 @@
 #include "ICF.h"
 #include "Config.h"
 #include "SymbolTable.h"
+#include "Threads.h"
 
-#include "lld/Core/Parallel.h"
 #include "llvm/ADT/Hashing.h"
 #include "llvm/Object/ELF.h"
 #include "llvm/Support/ELF.h"
@@ -295,9 +295,8 @@ void ICF<ELFT>::forEachColor(std::functi
   // Split sections into 256 shards and call Fn in parallel.
   size_t NumShards = 256;
   size_t Step = Sections.size() / NumShards;
-  parallel_for(size_t(0), NumShards, [&](size_t I) {
-    forEachColorRange(I * Step, (I + 1) * Step, Fn);
-  });
+  forLoop(0, NumShards,
+          [&](size_t I) { forEachColorRange(I * Step, (I + 1) * Step, Fn); });
   forEachColorRange(Step * NumShards, Sections.size(), Fn);
 }
 

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=288599&r1=288598&r2=288599&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Sat Dec  3 15:24:51 2016
@@ -16,7 +16,7 @@
 #include "SymbolTable.h"
 #include "SyntheticSections.h"
 #include "Target.h"
-#include "lld/Core/Parallel.h"
+#include "Threads.h"
 #include "llvm/Support/Dwarf.h"
 #include "llvm/Support/MD5.h"
 #include "llvm/Support/MathExtras.h"
@@ -254,10 +254,7 @@ template <class ELFT> void OutputSection
     fill(Buf, this->Size, Filler);
 
   auto Fn = [=](InputSection<ELFT> *IS) { IS->writeTo(Buf); };
-  if (Config->Threads)
-    parallel_for_each(Sections.begin(), Sections.end(), Fn);
-  else
-    std::for_each(Sections.begin(), Sections.end(), Fn);
+  forEach(Sections.begin(), Sections.end(), Fn);
 
   // Linker scripts may have BYTE()-family commands with which you
   // can write arbitrary bytes to the output. Process them if any.

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=288599&r1=288598&r2=288599&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Sat Dec  3 15:24:51 2016
@@ -24,10 +24,10 @@
 #include "Strings.h"
 #include "SymbolTable.h"
 #include "Target.h"
+#include "Threads.h"
 #include "Writer.h"
 
 #include "lld/Config/Version.h"
-#include "lld/Core/Parallel.h"
 #include "llvm/Support/Dwarf.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/MD5.h"
@@ -334,14 +334,9 @@ void BuildIdSection<ELFT>::computeHash(
   std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024);
   std::vector<uint8_t> Hashes(Chunks.size() * HashSize);
 
-  auto Fn = [&](size_t I) { HashFn(Hashes.data() + I * HashSize, Chunks[I]); };
-
   // Compute hash values.
-  if (Config->Threads)
-    parallel_for(size_t(0), Chunks.size(), Fn);
-  else
-    for (size_t I = 0, E = Chunks.size(); I != E; ++I)
-      Fn(I);
+  forLoop(0, Chunks.size(),
+          [&](size_t I) { HashFn(Hashes.data() + I * HashSize, Chunks[I]); });
 
   // Write to the final output buffer.
   HashFn(HashBuf, Hashes);

Added: lld/trunk/ELF/Threads.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Threads.h?rev=288599&view=auto
==============================================================================
--- lld/trunk/ELF/Threads.h (added)
+++ lld/trunk/ELF/Threads.h Sat Dec  3 15:24:51 2016
@@ -0,0 +1,41 @@
+//===- Threads.h ------------------------------------------------*- C++ -*-===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_ELF_THREADS_H
+#define LLD_ELF_THREADS_H
+
+#include "Config.h"
+
+#include "lld/Core/Parallel.h"
+#include <algorithm>
+#include <functional>
+
+namespace lld {
+namespace elf {
+
+template <class IterTy, class FuncTy>
+void forEach(IterTy Begin, IterTy End, FuncTy Fn) {
+  if (Config->Threads)
+    parallel_for_each(Begin, End, Fn);
+  else
+    std::for_each(Begin, End, Fn);
+}
+
+inline void forLoop(size_t Begin, size_t End, std::function<void(size_t)> Fn) {
+  if (Config->Threads) {
+    parallel_for(Begin, End, Fn);
+  } else {
+    for (size_t I = Begin; I < End; ++I)
+      Fn(I);
+  }
+}
+}
+}
+
+#endif




More information about the llvm-commits mailing list