2015-06-25 04:40:03 +08:00
|
|
|
//===- ICF.cpp ------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-12-02 16:03:58 +08:00
|
|
|
// ICF is short for Identical Code Folding. That is a size optimization to
|
|
|
|
// identify and merge two or more read-only sections (typically functions)
|
|
|
|
// that happened to have the same contents. It usually reduces output size
|
|
|
|
// by a few percent.
|
2015-09-11 12:29:03 +08:00
|
|
|
//
|
2016-12-02 16:03:58 +08:00
|
|
|
// On Windows, ICF is enabled by default.
|
2015-09-11 12:29:03 +08:00
|
|
|
//
|
2016-12-02 16:03:58 +08:00
|
|
|
// See ELF/ICF.cpp for the details about the algortihm.
|
2015-06-25 04:40:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Chunks.h"
|
2016-12-02 16:03:58 +08:00
|
|
|
#include "Error.h"
|
2015-07-31 06:57:21 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
#include "llvm/ADT/Hashing.h"
|
2015-09-11 12:29:03 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2017-05-11 08:03:52 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2015-09-11 12:29:03 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
2015-09-19 06:31:15 +08:00
|
|
|
#include <atomic>
|
2015-06-25 04:40:03 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2015-07-31 06:57:21 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2015-06-25 04:40:03 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
2015-09-11 12:29:03 +08:00
|
|
|
|
2015-09-16 22:19:10 +08:00
|
|
|
class ICF {
|
|
|
|
public:
|
2015-09-19 05:17:44 +08:00
|
|
|
void run(const std::vector<Chunk *> &V);
|
2015-09-16 22:19:10 +08:00
|
|
|
|
|
|
|
private:
|
2016-12-02 16:03:58 +08:00
|
|
|
void segregate(size_t Begin, size_t End, bool Constant);
|
2015-09-16 22:19:10 +08:00
|
|
|
|
2016-12-02 16:03:58 +08:00
|
|
|
bool equalsConstant(const SectionChunk *A, const SectionChunk *B);
|
|
|
|
bool equalsVariable(const SectionChunk *A, const SectionChunk *B);
|
2015-09-16 22:19:10 +08:00
|
|
|
|
2016-12-02 16:03:58 +08:00
|
|
|
uint32_t getHash(SectionChunk *C);
|
|
|
|
bool isEligible(SectionChunk *C);
|
|
|
|
|
|
|
|
size_t findBoundary(size_t Begin, size_t End);
|
|
|
|
|
2017-05-06 07:52:24 +08:00
|
|
|
void forEachClassRange(size_t Begin, size_t End,
|
2016-12-02 16:03:58 +08:00
|
|
|
std::function<void(size_t, size_t)> Fn);
|
|
|
|
|
2017-05-06 07:52:24 +08:00
|
|
|
void forEachClass(std::function<void(size_t, size_t)> Fn);
|
2016-12-02 16:03:58 +08:00
|
|
|
|
|
|
|
std::vector<SectionChunk *> Chunks;
|
|
|
|
int Cnt = 0;
|
|
|
|
std::atomic<bool> Repeat = {false};
|
|
|
|
};
|
2015-07-31 06:57:21 +08:00
|
|
|
|
2016-12-02 16:03:58 +08:00
|
|
|
// Returns a hash value for S.
|
|
|
|
uint32_t ICF::getHash(SectionChunk *C) {
|
2015-09-16 22:19:10 +08:00
|
|
|
return hash_combine(C->getPermissions(),
|
|
|
|
hash_value(C->SectionName),
|
|
|
|
C->NumRelocs,
|
2015-09-26 00:50:12 +08:00
|
|
|
C->getAlign(),
|
2015-09-16 22:19:10 +08:00
|
|
|
uint32_t(C->Header->SizeOfRawData),
|
|
|
|
C->Checksum);
|
2015-09-16 11:26:31 +08:00
|
|
|
}
|
2015-09-11 12:29:03 +08:00
|
|
|
|
2016-12-02 16:03:58 +08:00
|
|
|
// Returns true if section S is subject of ICF.
|
2017-04-28 07:03:22 +08:00
|
|
|
//
|
|
|
|
// Microsoft's documentation
|
|
|
|
// (https://msdn.microsoft.com/en-us/library/bxwfs976.aspx; visited April
|
|
|
|
// 2017) says that /opt:icf folds both functions and read-only data.
|
|
|
|
// Despite that, the MSVC linker folds only functions. We found
|
|
|
|
// a few instances of programs that are not safe for data merging.
|
|
|
|
// Therefore, we merge only functions just like the MSVC tool.
|
2016-12-02 16:03:58 +08:00
|
|
|
bool ICF::isEligible(SectionChunk *C) {
|
|
|
|
bool Global = C->Sym && C->Sym->isExternal();
|
2017-04-28 07:03:22 +08:00
|
|
|
bool Executable = C->getPermissions() & llvm::COFF::IMAGE_SCN_MEM_EXECUTE;
|
2016-12-02 16:03:58 +08:00
|
|
|
bool Writable = C->getPermissions() & llvm::COFF::IMAGE_SCN_MEM_WRITE;
|
2017-04-28 07:03:22 +08:00
|
|
|
return C->isCOMDAT() && C->isLive() && Global && Executable && !Writable;
|
2016-12-02 16:03:58 +08:00
|
|
|
}
|
|
|
|
|
2017-05-06 07:52:24 +08:00
|
|
|
// Split an equivalence class into smaller classes.
|
2016-12-02 16:03:58 +08:00
|
|
|
void ICF::segregate(size_t Begin, size_t End, bool Constant) {
|
|
|
|
while (Begin < End) {
|
|
|
|
// Divide [Begin, End) into two. Let Mid be the start index of the
|
|
|
|
// second group.
|
|
|
|
auto Bound = std::stable_partition(
|
|
|
|
Chunks.begin() + Begin + 1, Chunks.begin() + End, [&](SectionChunk *S) {
|
|
|
|
if (Constant)
|
|
|
|
return equalsConstant(Chunks[Begin], S);
|
|
|
|
return equalsVariable(Chunks[Begin], S);
|
|
|
|
});
|
|
|
|
size_t Mid = Bound - Chunks.begin();
|
|
|
|
|
2017-05-25 03:56:29 +08:00
|
|
|
// Split [Begin, End) into [Begin, Mid) and [Mid, End). We use Mid as an
|
|
|
|
// equivalence class ID because every group ends with a unique index.
|
2016-12-02 16:03:58 +08:00
|
|
|
for (size_t I = Begin; I < Mid; ++I)
|
2017-05-25 03:56:29 +08:00
|
|
|
Chunks[I]->Class[(Cnt + 1) % 2] = Mid;
|
2016-12-02 16:03:58 +08:00
|
|
|
|
|
|
|
// If we created a group, we need to iterate the main loop again.
|
|
|
|
if (Mid != End)
|
|
|
|
Repeat = true;
|
|
|
|
|
|
|
|
Begin = Mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare "non-moving" part of two sections, namely everything
|
|
|
|
// except relocation targets.
|
2015-09-16 22:19:10 +08:00
|
|
|
bool ICF::equalsConstant(const SectionChunk *A, const SectionChunk *B) {
|
2016-12-02 16:03:58 +08:00
|
|
|
if (A->NumRelocs != B->NumRelocs)
|
2015-07-31 06:57:21 +08:00
|
|
|
return false;
|
|
|
|
|
COFF: Optimize ICF by comparing relocations before section contents.
equalsConstants() is the heaviest function in ICF, and that consumes
more than half of total ICF execution time. Of which, section content
comparison accounts for roughly one third.
Previously, we compared section contents at the beginning of the
function after comparing their checksums. The comparison is very
likely to succeed because when the control reaches that comparison,
their checksums are always equal. And because checksums are 64-bit
CRC, they are unlikely to collide.
We compared relocations and associative sections after that.
If they are different, the time we spent on byte-by-byte comparison
of section contents were wasted.
This patch moves the comparison at the end of function. If the
comparison fails, the time we spent on relocation comparison are
wasted, but as I wrote it's very unlikely to happen.
LLD took 1198 ms to link itself to produce a 27.11 MB executable.
Of which, ICF accounted for 536 ms. This patch cuts it by 90 ms,
which is 17% speedup of ICF and 7.5% speedup overall. All numbers
are median of ten runs.
llvm-svn: 247961
2015-09-18 09:30:56 +08:00
|
|
|
// Compare relocations.
|
2015-07-31 06:57:21 +08:00
|
|
|
auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
|
2015-09-16 11:26:31 +08:00
|
|
|
if (R1.Type != R2.Type ||
|
|
|
|
R1.VirtualAddress != R2.VirtualAddress) {
|
2015-07-31 06:57:21 +08:00
|
|
|
return false;
|
2015-09-16 11:26:31 +08:00
|
|
|
}
|
2016-12-10 05:55:24 +08:00
|
|
|
SymbolBody *B1 = A->File->getSymbolBody(R1.SymbolTableIndex);
|
|
|
|
SymbolBody *B2 = B->File->getSymbolBody(R2.SymbolTableIndex);
|
2015-07-31 06:57:21 +08:00
|
|
|
if (B1 == B2)
|
|
|
|
return true;
|
2015-09-18 10:40:54 +08:00
|
|
|
if (auto *D1 = dyn_cast<DefinedRegular>(B1))
|
|
|
|
if (auto *D2 = dyn_cast<DefinedRegular>(B2))
|
|
|
|
return D1->getValue() == D2->getValue() &&
|
2017-05-06 07:52:24 +08:00
|
|
|
D1->getChunk()->Class[Cnt % 2] == D2->getChunk()->Class[Cnt % 2];
|
2015-09-18 10:40:54 +08:00
|
|
|
return false;
|
2015-07-31 06:57:21 +08:00
|
|
|
};
|
COFF: Optimize ICF by comparing relocations before section contents.
equalsConstants() is the heaviest function in ICF, and that consumes
more than half of total ICF execution time. Of which, section content
comparison accounts for roughly one third.
Previously, we compared section contents at the beginning of the
function after comparing their checksums. The comparison is very
likely to succeed because when the control reaches that comparison,
their checksums are always equal. And because checksums are 64-bit
CRC, they are unlikely to collide.
We compared relocations and associative sections after that.
If they are different, the time we spent on byte-by-byte comparison
of section contents were wasted.
This patch moves the comparison at the end of function. If the
comparison fails, the time we spent on relocation comparison are
wasted, but as I wrote it's very unlikely to happen.
LLD took 1198 ms to link itself to produce a 27.11 MB executable.
Of which, ICF accounted for 536 ms. This patch cuts it by 90 ms,
which is 17% speedup of ICF and 7.5% speedup overall. All numbers
are median of ten runs.
llvm-svn: 247961
2015-09-18 09:30:56 +08:00
|
|
|
if (!std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq))
|
|
|
|
return false;
|
|
|
|
|
2015-09-18 10:40:54 +08:00
|
|
|
// Compare section attributes and contents.
|
|
|
|
return A->getPermissions() == B->getPermissions() &&
|
|
|
|
A->SectionName == B->SectionName &&
|
2015-09-26 00:50:12 +08:00
|
|
|
A->getAlign() == B->getAlign() &&
|
2015-09-18 10:40:54 +08:00
|
|
|
A->Header->SizeOfRawData == B->Header->SizeOfRawData &&
|
|
|
|
A->Checksum == B->Checksum &&
|
|
|
|
A->getContents() == B->getContents();
|
2015-09-16 11:26:31 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 16:03:58 +08:00
|
|
|
// Compare "moving" part of two sections, namely relocation targets.
|
2015-09-16 22:19:10 +08:00
|
|
|
bool ICF::equalsVariable(const SectionChunk *A, const SectionChunk *B) {
|
2015-09-18 09:51:37 +08:00
|
|
|
// Compare relocations.
|
|
|
|
auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
|
2016-12-10 05:55:24 +08:00
|
|
|
SymbolBody *B1 = A->File->getSymbolBody(R1.SymbolTableIndex);
|
|
|
|
SymbolBody *B2 = B->File->getSymbolBody(R2.SymbolTableIndex);
|
2015-09-21 04:19:12 +08:00
|
|
|
if (B1 == B2)
|
|
|
|
return true;
|
|
|
|
if (auto *D1 = dyn_cast<DefinedRegular>(B1))
|
2015-09-19 05:17:44 +08:00
|
|
|
if (auto *D2 = dyn_cast<DefinedRegular>(B2))
|
2017-05-06 07:52:24 +08:00
|
|
|
return D1->getChunk()->Class[Cnt % 2] == D2->getChunk()->Class[Cnt % 2];
|
2015-09-19 05:17:44 +08:00
|
|
|
return false;
|
2015-09-18 09:51:37 +08:00
|
|
|
};
|
|
|
|
return std::equal(A->Relocs.begin(), A->Relocs.end(), B->Relocs.begin(), Eq);
|
2015-09-16 11:26:31 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 16:03:58 +08:00
|
|
|
size_t ICF::findBoundary(size_t Begin, size_t End) {
|
|
|
|
for (size_t I = Begin + 1; I < End; ++I)
|
2017-05-06 07:52:24 +08:00
|
|
|
if (Chunks[Begin]->Class[Cnt % 2] != Chunks[I]->Class[Cnt % 2])
|
2016-12-02 16:03:58 +08:00
|
|
|
return I;
|
|
|
|
return End;
|
|
|
|
}
|
|
|
|
|
2017-05-06 07:52:24 +08:00
|
|
|
void ICF::forEachClassRange(size_t Begin, size_t End,
|
2016-12-02 16:03:58 +08:00
|
|
|
std::function<void(size_t, size_t)> Fn) {
|
|
|
|
if (Begin > 0)
|
|
|
|
Begin = findBoundary(Begin - 1, End);
|
|
|
|
|
|
|
|
while (Begin < End) {
|
|
|
|
size_t Mid = findBoundary(Begin, Chunks.size());
|
|
|
|
Fn(Begin, Mid);
|
|
|
|
Begin = Mid;
|
2015-09-16 11:26:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 07:52:24 +08:00
|
|
|
// Call Fn on each class group.
|
|
|
|
void ICF::forEachClass(std::function<void(size_t, size_t)> Fn) {
|
2016-12-02 16:03:58 +08:00
|
|
|
// If the number of sections are too small to use threading,
|
|
|
|
// call Fn sequentially.
|
|
|
|
if (Chunks.size() < 1024) {
|
2017-05-06 07:52:24 +08:00
|
|
|
forEachClassRange(0, Chunks.size(), Fn);
|
2017-05-25 03:56:29 +08:00
|
|
|
++Cnt;
|
2016-12-02 16:03:58 +08:00
|
|
|
return;
|
2015-09-16 11:26:31 +08:00
|
|
|
}
|
2016-12-02 16:03:58 +08:00
|
|
|
|
|
|
|
// Split sections into 256 shards and call Fn in parallel.
|
|
|
|
size_t NumShards = 256;
|
|
|
|
size_t Step = Chunks.size() / NumShards;
|
2017-05-10 09:16:22 +08:00
|
|
|
for_each_n(parallel::par, size_t(0), NumShards, [&](size_t I) {
|
2017-05-25 03:22:34 +08:00
|
|
|
size_t End = (I == NumShards - 1) ? Chunks.size() : (I + 1) * Step;
|
|
|
|
forEachClassRange(I * Step, End, Fn);
|
2016-12-02 16:03:58 +08:00
|
|
|
});
|
2017-05-25 03:56:29 +08:00
|
|
|
++Cnt;
|
2015-07-31 06:57:21 +08:00
|
|
|
}
|
|
|
|
|
2015-06-25 04:40:03 +08:00
|
|
|
// Merge identical COMDAT sections.
|
2015-09-05 05:35:54 +08:00
|
|
|
// Two sections are considered the same if their section headers,
|
2015-06-25 04:40:03 +08:00
|
|
|
// contents and relocations are all the same.
|
2015-09-19 05:17:44 +08:00
|
|
|
void ICF::run(const std::vector<Chunk *> &Vec) {
|
2015-09-16 22:19:10 +08:00
|
|
|
// Collect only mergeable sections and group by hash value.
|
2017-05-25 04:32:23 +08:00
|
|
|
uint32_t NextId = 1;
|
|
|
|
for (Chunk *C : Vec) {
|
|
|
|
if (auto *SC = dyn_cast<SectionChunk>(C)) {
|
2017-05-25 03:56:29 +08:00
|
|
|
if (isEligible(SC))
|
|
|
|
Chunks.push_back(SC);
|
2017-05-25 04:32:23 +08:00
|
|
|
else
|
|
|
|
SC->Class[0] = NextId++;
|
|
|
|
}
|
|
|
|
}
|
2015-09-05 05:35:54 +08:00
|
|
|
|
2017-05-25 03:56:29 +08:00
|
|
|
// Initially, we use hash values to partition sections.
|
|
|
|
for (SectionChunk *SC : Chunks)
|
|
|
|
// Set MSB to 1 to avoid collisions with non-hash classs.
|
|
|
|
SC->Class[0] = getHash(SC) | (1 << 31);
|
2016-12-02 16:03:58 +08:00
|
|
|
|
2015-09-19 05:06:34 +08:00
|
|
|
// From now on, sections in Chunks are ordered so that sections in
|
2015-09-16 22:19:10 +08:00
|
|
|
// the same group are consecutive in the vector.
|
2016-12-02 16:03:58 +08:00
|
|
|
std::stable_sort(Chunks.begin(), Chunks.end(),
|
|
|
|
[](SectionChunk *A, SectionChunk *B) {
|
2017-05-06 07:52:24 +08:00
|
|
|
return A->Class[0] < B->Class[0];
|
2016-12-02 16:03:58 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Compare static contents and assign unique IDs for each static content.
|
2017-05-06 07:52:24 +08:00
|
|
|
forEachClass([&](size_t Begin, size_t End) { segregate(Begin, End, true); });
|
2016-12-02 16:03:58 +08:00
|
|
|
|
|
|
|
// Split groups by comparing relocations until convergence is obtained.
|
|
|
|
do {
|
|
|
|
Repeat = false;
|
2017-05-06 07:52:24 +08:00
|
|
|
forEachClass(
|
2016-12-02 16:03:58 +08:00
|
|
|
[&](size_t Begin, size_t End) { segregate(Begin, End, false); });
|
|
|
|
} while (Repeat);
|
|
|
|
|
2017-02-22 07:22:56 +08:00
|
|
|
log("ICF needed " + Twine(Cnt) + " iterations");
|
2016-12-02 16:03:58 +08:00
|
|
|
|
2017-05-06 07:52:24 +08:00
|
|
|
// Merge sections in the same classs.
|
|
|
|
forEachClass([&](size_t Begin, size_t End) {
|
2016-12-02 16:03:58 +08:00
|
|
|
if (End - Begin == 1)
|
|
|
|
return;
|
|
|
|
|
2017-02-22 07:22:56 +08:00
|
|
|
log("Selected " + Chunks[Begin]->getDebugName());
|
2016-12-02 16:03:58 +08:00
|
|
|
for (size_t I = Begin + 1; I < End; ++I) {
|
2017-02-22 07:22:56 +08:00
|
|
|
log(" Removed " + Chunks[I]->getDebugName());
|
2016-12-02 16:03:58 +08:00
|
|
|
Chunks[Begin]->replace(Chunks[I]);
|
2015-09-11 12:29:03 +08:00
|
|
|
}
|
2016-12-02 16:03:58 +08:00
|
|
|
});
|
2015-06-25 04:40:03 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 16:03:58 +08:00
|
|
|
// Entry point to ICF.
|
|
|
|
void doICF(const std::vector<Chunk *> &Chunks) { ICF().run(Chunks); }
|
|
|
|
|
2015-06-25 04:40:03 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|