-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[ELF] Add BPSectionOrderer options (#120514) #125559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//===- BPSectionOrderer.cpp -----------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "BPSectionOrderer.h" | ||
#include "InputFiles.h" | ||
#include "InputSection.h" | ||
#include "SymbolTable.h" | ||
#include "Symbols.h" | ||
#include "lld/Common/BPSectionOrdererBase.inc" | ||
#include "llvm/Support/Endian.h" | ||
|
||
using namespace llvm; | ||
using namespace lld::elf; | ||
|
||
namespace { | ||
struct BPOrdererELF; | ||
} | ||
template <> struct lld::BPOrdererTraits<struct BPOrdererELF> { | ||
using Section = elf::InputSectionBase; | ||
using Defined = elf::Defined; | ||
}; | ||
namespace { | ||
struct BPOrdererELF : lld::BPOrderer<BPOrdererELF> { | ||
DenseMap<const InputSectionBase *, Defined *> secToSym; | ||
|
||
static uint64_t getSize(const Section &sec) { return sec.getSize(); } | ||
static bool isCodeSection(const Section &sec) { | ||
return sec.flags & ELF::SHF_EXECINSTR; | ||
} | ||
ArrayRef<Defined *> getSymbols(const Section &sec) { | ||
auto it = secToSym.find(&sec); | ||
if (it == secToSym.end()) | ||
return {}; | ||
return ArrayRef(it->second); | ||
} | ||
|
||
static void | ||
getSectionHashes(const Section &sec, SmallVectorImpl<uint64_t> &hashes, | ||
const DenseMap<const void *, uint64_t> §ionToIdx) { | ||
constexpr unsigned windowSize = 4; | ||
|
||
// Calculate content hashes: k-mers and the last k-1 bytes. | ||
ArrayRef<uint8_t> data = sec.content(); | ||
if (data.size() >= windowSize) | ||
for (size_t i = 0; i <= data.size() - windowSize; ++i) | ||
hashes.push_back(support::endian::read32le(data.data() + i)); | ||
for (uint8_t byte : data.take_back(windowSize - 1)) | ||
hashes.push_back(byte); | ||
|
||
llvm::sort(hashes); | ||
hashes.erase(std::unique(hashes.begin(), hashes.end()), hashes.end()); | ||
} | ||
|
||
static StringRef getSymName(const Defined &sym) { return sym.getName(); } | ||
static uint64_t getSymValue(const Defined &sym) { return sym.value; } | ||
static uint64_t getSymSize(const Defined &sym) { return sym.size; } | ||
}; | ||
} // namespace | ||
|
||
DenseMap<const InputSectionBase *, int> elf::runBalancedPartitioning( | ||
Ctx &ctx, StringRef profilePath, bool forFunctionCompression, | ||
bool forDataCompression, bool compressionSortStartupFunctions, | ||
bool verbose) { | ||
// Collect candidate sections and associated symbols. | ||
SmallVector<InputSectionBase *> sections; | ||
DenseMap<CachedHashStringRef, std::set<unsigned>> rootSymbolToSectionIdxs; | ||
BPOrdererELF orderer; | ||
|
||
auto addSection = [&](Symbol &sym) { | ||
auto *d = dyn_cast<Defined>(&sym); | ||
if (!d) | ||
return; | ||
auto *sec = dyn_cast_or_null<InputSectionBase>(d->section); | ||
if (!sec || sec->size == 0 || !orderer.secToSym.try_emplace(sec, d).second) | ||
return; | ||
rootSymbolToSectionIdxs[CachedHashStringRef(getRootSymbol(sym.getName()))] | ||
.insert(sections.size()); | ||
sections.emplace_back(sec); | ||
}; | ||
|
||
for (Symbol *sym : ctx.symtab->getSymbols()) | ||
addSection(*sym); | ||
for (ELFFileBase *file : ctx.objectFiles) | ||
for (Symbol *sym : file->getLocalSymbols()) | ||
addSection(*sym); | ||
return orderer.computeOrder(profilePath, forFunctionCompression, | ||
forDataCompression, | ||
compressionSortStartupFunctions, verbose, | ||
sections, rootSymbolToSectionIdxs); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===- BPSectionOrderer.h -------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// This file uses Balanced Partitioning to order sections to improve startup | ||
/// time and compressed size. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLD_ELF_BPSECTION_ORDERER_H | ||
#define LLD_ELF_BPSECTION_ORDERER_H | ||
|
||
#include "llvm/ADT/DenseMap.h" | ||
#include "llvm/ADT/StringRef.h" | ||
|
||
namespace lld::elf { | ||
struct Ctx; | ||
class InputSectionBase; | ||
|
||
/// Run Balanced Partitioning to find the optimal function and data order to | ||
/// improve startup time and compressed size. | ||
/// | ||
/// It is important that -ffunction-sections and -fdata-sections compiler flags | ||
/// are used to ensure functions and data are in their own sections and thus | ||
/// can be reordered. | ||
llvm::DenseMap<const InputSectionBase *, int> | ||
runBalancedPartitioning(Ctx &ctx, llvm::StringRef profilePath, | ||
bool forFunctionCompression, bool forDataCompression, | ||
bool compressionSortStartupFunctions, bool verbose); | ||
|
||
} // namespace lld::elf | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ellishg With the existing threshold 5, wholeHash is almost always 0 for smaller applications. I guess 5 was picked to improve the performance of larger applications.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, 5 was picked because we saw good results for large binaries. If we wanted to generalize this we could compute something like the P90 of the hash frequency, which would be the set of hashes that are repeated more than 90% of the hashes. But I would like to experiment with this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's important for tests to show that
--bp-compression-sort=data
actually does something. Therefore, the threshold has to be lowered so that data sections will not all be considered duplicates....I think
sectionHashes.size() > 10000
will work for large applications like yours.(In the test, --bp-compression-sort=both and --bp-compression-sort=data exhibit the same behavior. Ideally 'both' should actually do something with functions. Reserved for a future patch.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I built some large binaries with this change and I didn't see any large differences, so I think this is ok. I also realized that there is a bug. If
wholeHash
is zero, we should not insert intoduplicateSectionIdxs
because they have no hashes in common.The more I think about this the more I realize this might be able to be improved quite a bit. I can imagine a set of small outlined functions that differ in one instructions. Ideally these should be in
duplicateSectionIdxs
, but this code won't catch that case.