-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[ELF] Add BPSectionOrderer options #120514
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
Show all changes
14 commits
Select commit
Hold shift + click to select a range
14658cc
Add new ELF linker options for order section layout
Colibrow 5569078
try to refine symbol/section
Colibrow 95a36a5
change content hash as #121729
Colibrow 59d0783
remove useless header
Colibrow 0236c70
omit -o /dev/null
Colibrow ae0c698
rewrite BPSectionOrder in ELF following #124482
Colibrow 99ddffe
fix: address code review feedback
Colibrow 17e0962
[cmake] Add ProfileData to fix BUILD_SHARED_LIBS=on builds
MaskRay 4f9853b
Simplify
MaskRay 78ed4a6
Ignore callGraphProfile when BP orderer is used, update test
MaskRay 123c8a7
Move getResolvedLinkageName to .inc
MaskRay 0d30017
Replace Symbol with Defined
MaskRay fcf877e
Optimize getSymbols
MaskRay be9c54b
improve test
MaskRay 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 & llvm::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, llvm::SmallVectorImpl<uint64_t> &hashes, | ||
const llvm::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(llvm::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, DenseSet<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()) | ||
Colibrow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ add_lld_library(lldELF | |
Arch/X86.cpp | ||
Arch/X86_64.cpp | ||
ARMErrataFix.cpp | ||
BPSectionOrderer.cpp | ||
CallGraphSort.cpp | ||
DWARF.cpp | ||
Driver.cpp | ||
|
@@ -72,6 +73,7 @@ add_lld_library(lldELF | |
Object | ||
Option | ||
Passes | ||
ProfileData | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed to fix BUILD_SHARED_LIBS=on builds |
||
Support | ||
TargetParser | ||
TransformUtils | ||
|
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.
The previous getSymbols iterated over all symbols in sec.file, which could be very slow. I rewrote this to use a member variable.