Skip to content

Commit 91fe6b3

Browse files
committed
Simplify
1 parent 4dd0768 commit 91fe6b3

File tree

4 files changed

+69
-80
lines changed

4 files changed

+69
-80
lines changed

lld/ELF/BPSectionOrderer.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include "lld/Common/BPSectionOrdererBase.inc"
1515
#include "llvm/Support/Endian.h"
1616

17-
#define DEBUG_TYPE "bp-section-orderer"
18-
1917
using namespace llvm;
2018
using namespace lld::elf;
2119

@@ -86,24 +84,19 @@ DenseMap<const InputSectionBase *, int> lld::elf::runBalancedPartitioning(
8684
DenseSet<const InputSectionBase *> seenSections;
8785

8886
auto addSection = [&](Symbol &sym) {
89-
if (sym.getSize() == 0)
87+
auto *d = dyn_cast<Defined>(&sym);
88+
if (!d || d->size == 0)
9089
return;
91-
if (auto *d = dyn_cast<Defined>(&sym))
92-
if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section))
93-
if (seenSections.insert(sec).second) {
94-
size_t idx = sections.size();
95-
sections.emplace_back(sec);
96-
auto rootName = getRootSymbol(sym.getName());
97-
rootSymbolToSectionIdxs[CachedHashStringRef(rootName)].insert(idx);
98-
if (auto linkageName = BPOrdererELF::getResolvedLinkageName(rootName))
99-
rootSymbolToSectionIdxs[CachedHashStringRef(*linkageName)].insert(
100-
idx);
101-
}
90+
auto *sec = dyn_cast_or_null<InputSectionBase>(d->section);
91+
if (sec && seenSections.insert(sec).second) {
92+
rootSymbolToSectionIdxs[CachedHashStringRef(getRootSymbol(sym.getName()))]
93+
.insert(sections.size());
94+
sections.emplace_back(sec);
95+
}
10296
};
10397

10498
for (Symbol *sym : ctx.symtab->getSymbols())
10599
addSection(*sym);
106-
107100
for (ELFFileBase *file : ctx.objectFiles)
108101
for (Symbol *sym : file->getLocalSymbols())
109102
addSection(*sym);

lld/ELF/Driver.cpp

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,53 @@ static CGProfileSortKind getCGProfileSortKind(Ctx &ctx,
11181118
return CGProfileSortKind::None;
11191119
}
11201120

1121+
static void parseBPOrdererOptions(Ctx &ctx, opt::InputArgList &args) {
1122+
if (auto *arg = args.getLastArg(OPT_bp_compression_sort)) {
1123+
StringRef s = arg->getValue();
1124+
if (s == "function") {
1125+
ctx.arg.bpFunctionOrderForCompression = true;
1126+
} else if (s == "data") {
1127+
ctx.arg.bpDataOrderForCompression = true;
1128+
} else if (s == "both") {
1129+
ctx.arg.bpFunctionOrderForCompression = true;
1130+
ctx.arg.bpDataOrderForCompression = true;
1131+
} else if (s != "none") {
1132+
ErrAlways(ctx) << arg->getSpelling()
1133+
<< ": expected [none|function|data|both]";
1134+
}
1135+
if (s != "none" && args.hasArg(OPT_call_graph_ordering_file))
1136+
ErrAlways(ctx) << "--bp-compression-sort is incompatible with "
1137+
"--call-graph-ordering-file";
1138+
}
1139+
if (auto *arg = args.getLastArg(OPT_bp_startup_sort)) {
1140+
StringRef s = arg->getValue();
1141+
if (s == "function") {
1142+
ctx.arg.bpStartupFunctionSort = true;
1143+
} else if (s != "none") {
1144+
ErrAlways(ctx) << arg->getSpelling() << ": expected [none|function]";
1145+
}
1146+
if (s != "none" && args.hasArg(OPT_call_graph_ordering_file))
1147+
ErrAlways(ctx) << "--bp-startup-sort=function is incompatible with "
1148+
"--call-graph-ordering-file";
1149+
}
1150+
1151+
ctx.arg.bpCompressionSortStartupFunctions =
1152+
args.hasFlag(OPT_bp_compression_sort_startup_functions,
1153+
OPT_no_bp_compression_sort_startup_functions, false);
1154+
ctx.arg.bpVerboseSectionOrderer = args.hasArg(OPT_verbose_bp_section_orderer);
1155+
1156+
ctx.arg.irpgoProfilePath = args.getLastArgValue(OPT_irpgo_profile);
1157+
if (ctx.arg.irpgoProfilePath.empty()) {
1158+
if (ctx.arg.bpStartupFunctionSort)
1159+
ErrAlways(ctx) << "--bp-startup-sort=function must be used with "
1160+
"--irpgo-profile";
1161+
if (ctx.arg.bpCompressionSortStartupFunctions)
1162+
ErrAlways(ctx)
1163+
<< "--bp-compression-sort-startup-functions must be used with "
1164+
"--irpgo-profile";
1165+
}
1166+
}
1167+
11211168
static DebugCompressionType getCompressionType(Ctx &ctx, StringRef s,
11221169
StringRef option) {
11231170
DebugCompressionType type = StringSwitch<DebugCompressionType>(s)
@@ -1259,55 +1306,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
12591306
ctx.arg.bsymbolic = BsymbolicKind::All;
12601307
}
12611308
ctx.arg.callGraphProfileSort = getCGProfileSortKind(ctx, args);
1262-
ctx.arg.irpgoProfilePath = args.getLastArgValue(OPT_irpgo_profile);
1263-
ctx.arg.bpCompressionSortStartupFunctions =
1264-
args.hasFlag(OPT_bp_compression_sort_startup_functions,
1265-
OPT_no_bp_compression_sort_startup_functions, false);
1266-
if (auto *arg = args.getLastArg(OPT_bp_startup_sort)) {
1267-
StringRef startupSortStr = arg->getValue();
1268-
if (startupSortStr == "function") {
1269-
ctx.arg.bpStartupFunctionSort = true;
1270-
} else if (startupSortStr != "none") {
1271-
ErrAlways(ctx) << "unknown value '" + startupSortStr + "' for " +
1272-
arg->getSpelling();
1273-
}
1274-
if (startupSortStr != "none")
1275-
if (args.hasArg(OPT_call_graph_ordering_file))
1276-
ErrAlways(ctx) << "--bp-startup-sort=function is incompatible with "
1277-
"--call-graph-ordering-file";
1278-
}
1279-
if (ctx.arg.irpgoProfilePath.empty()) {
1280-
if (ctx.arg.bpStartupFunctionSort)
1281-
ErrAlways(ctx) << "--bp-startup-sort=function must be used with "
1282-
"--irpgo-profile";
1283-
if (ctx.arg.bpCompressionSortStartupFunctions)
1284-
ErrAlways(ctx)
1285-
<< "--bp-compression-sort-startup-functions must be used with "
1286-
"--irpgo-profile";
1287-
}
1288-
1289-
if (auto *arg = args.getLastArg(OPT_bp_compression_sort)) {
1290-
StringRef compressionSortStr = arg->getValue();
1291-
if (compressionSortStr == "function") {
1292-
ctx.arg.bpFunctionOrderForCompression = true;
1293-
} else if (compressionSortStr == "data") {
1294-
ctx.arg.bpDataOrderForCompression = true;
1295-
} else if (compressionSortStr == "both") {
1296-
ctx.arg.bpFunctionOrderForCompression = true;
1297-
ctx.arg.bpDataOrderForCompression = true;
1298-
} else if (compressionSortStr != "none") {
1299-
ErrAlways(ctx) << "unknown value '" + compressionSortStr + "' for " +
1300-
arg->getSpelling();
1301-
}
1302-
if (ctx.arg.bpDataOrderForCompression ||
1303-
ctx.arg.bpFunctionOrderForCompression) {
1304-
if (args.getLastArg(OPT_call_graph_ordering_file) != nullptr) {
1305-
ErrAlways(ctx) << "--bp-compression-sort is incompatible with "
1306-
"--call-graph-ordering-file";
1307-
}
1308-
}
1309-
}
1310-
ctx.arg.bpVerboseSectionOrderer = args.hasArg(OPT_verbose_bp_section_orderer);
1309+
parseBPOrdererOptions(ctx, args);
13111310
ctx.arg.checkSections =
13121311
args.hasFlag(OPT_check_sections, OPT_no_check_sections, true);
13131312
ctx.arg.chroot = args.getLastArgValue(OPT_chroot);

lld/ELF/Options.td

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,18 @@ def : FF<"no-call-graph-profile-sort">, Alias<call_graph_profile_sort>, AliasArg
142142
Flags<[HelpHidden]>;
143143

144144
defm irpgo_profile: EEq<"irpgo-profile",
145-
"Read the IRPGO profile for use with -bp-startup-sort and other profile-guided optimizations">;
146-
147-
def bp_startup_sort: JJ<"bp-startup-sort=">,
148-
MetaVarName<"[none,function]">,
149-
HelpText<"Order sections based on profile data to improve startup time">;
145+
"Read a temporary profile file for use with --bp-startup-sort=">;
146+
def bp_compression_sort: JJ<"bp-compression-sort=">, MetaVarName<"[none,function,data,both]">,
147+
HelpText<"Improve Lempel-Ziv compression by grouping similar sections together, resulting in a smaller compressed app size">;
148+
def bp_startup_sort: JJ<"bp-startup-sort=">, MetaVarName<"[none,function]">,
149+
HelpText<"Utilize a temporal profile file to reduce page faults during program startup">;
150150

151+
// Auxiliary options related to balanced partition
151152
defm bp_compression_sort_startup_functions: BB<"bp-compression-sort-startup-functions",
152-
"Order startup functions by balanced partition to improve compressed size in addition to startup time",
153-
"Do not order startup function for compression">;
154-
155-
def bp_compression_sort: JJ<"bp-compression-sort=">,
156-
MetaVarName<"[none,function,data,both]">,
157-
HelpText<"Order sections by balanced partition to improve compressed size">;
158-
153+
"When --irpgo-profile is pecified, prioritize function similarity for compression in addition to startup time", "">;
159154
def verbose_bp_section_orderer: FF<"verbose-bp-section-orderer">,
160-
HelpText<"Print information on how many sections were ordered by balanced partitioning and a measure of the expected number of page faults">;
161-
155+
HelpText<"Print information on balanced partitioning">;
156+
162157
// --chroot doesn't have a help text because it is an internal option.
163158
def chroot: Separate<["--"], "chroot">;
164159

lld/test/ELF/bp-section-orderer.s

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
# RUN: not ld.lld --bp-compression-sort=function --call-graph-ordering-file /dev/null 2>&1 | FileCheck %s --check-prefix=BP-COMPRESSION-CALLGRAPH-ERR
88
# RUN: not ld.lld --bp-startup-sort=function 2>&1 | FileCheck %s --check-prefix=BP-STARTUP-ERR
99
# RUN: not ld.lld --bp-compression-sort-startup-functions 2>&1 | FileCheck %s --check-prefix=BP-STARTUP-COMPRESSION-ERR
10-
# RUN: not ld.lld --bp-compression-sort=malformed 2>&1 | FileCheck %s --check-prefix=BP-COMPRESSION-MALFORM
10+
# RUN: not ld.lld --bp-startup-sort=invalid --bp-compression-sort=invalid 2>&1 | FileCheck %s --check-prefix=BP-INVALID
1111

1212
# BP-STARTUP-CALLGRAPH-ERR: error: --bp-startup-sort=function is incompatible with --call-graph-ordering-file
1313
# BP-COMPRESSION-CALLGRAPH-ERR: error: --bp-compression-sort is incompatible with --call-graph-ordering-file
1414
# BP-STARTUP-ERR: error: --bp-startup-sort=function must be used with --irpgo-profile
1515
# BP-STARTUP-COMPRESSION-ERR: error: --bp-compression-sort-startup-functions must be used with --irpgo-profile
16-
# BP-COMPRESSION-MALFORM: error: unknown value 'malformed' for --bp-compression-sort=
16+
17+
# BP-INVALID: error: --bp-compression-sort=: expected [none|function|data|both]
18+
# BP-INVALID: error: --bp-startup-sort=: expected [none|function]
1719

1820
# RUN: llvm-mc -filetype=obj -triple=aarch64 a.s -o a.o
1921
# RUN: llvm-profdata merge a.proftext -o a.profdata

0 commit comments

Comments
 (0)