Factor out common code to a header.

llvm-svn: 288599
This commit is contained in:
Rui Ueyama 2016-12-03 21:24:51 +00:00
parent 30545b9058
commit 244a435ae3
5 changed files with 59 additions and 30 deletions

View File

@ -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::link(opt::InputArgList &Args) {
// 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>();

View File

@ -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::function<void(size_t, size_t)> Fn) {
// 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);
}

View File

@ -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<ELFT>::writeTo(uint8_t *Buf) {
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.

View File

@ -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);

41
lld/ELF/Threads.h Normal file
View File

@ -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