Skip to content

Commit 8c556b7

Browse files
committed
[ELF] Change --call-graph-profile-sort to accept an argument
Change the FF form --call-graph-profile-sort to --call-graph-profile-sort={none,hfsort}. This will be extended to support llvm/lib/Transforms/Utils/CodeLayout.cpp. --call-graph-profile-sort is not used in the wild but --no-call-graph-profile-sort is (Chromium). Make --no-call-graph-profile-sort an alias for --call-graph-profile-sort=none. Reviewed By: rahmanl Differential Revision: https://reviews.llvm.org/D159544
1 parent 1db42fa commit 8c556b7

File tree

6 files changed

+46
-10
lines changed

6 files changed

+46
-10
lines changed

lld/ELF/Config.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ enum class BsymbolicKind { None, NonWeakFunctions, Functions, NonWeak, All };
5959
// For --build-id.
6060
enum class BuildIdKind { None, Fast, Md5, Sha1, Hexstring, Uuid };
6161

62+
// For --call-graph-profile-sort={none,hfsort}.
63+
enum class CGProfileSortKind { None, Hfsort };
64+
6265
// For --discard-{all,locals,none}.
6366
enum class DiscardPolicy { Default, All, Locals, None };
6467

@@ -215,7 +218,7 @@ struct Config {
215218
bool asNeeded = false;
216219
bool armBe8 = false;
217220
BsymbolicKind bsymbolic = BsymbolicKind::None;
218-
bool callGraphProfileSort;
221+
CGProfileSortKind callGraphProfileSort;
219222
bool checkSections;
220223
bool checkDynamicRelocs;
221224
llvm::DebugCompressionType compressDebugSections;

lld/ELF/Driver.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,15 @@ static void ltoValidateAllVtablesHaveTypeInfos(opt::InputArgList &args) {
10941094
}
10951095
}
10961096

1097+
static CGProfileSortKind getCGProfileSortKind(opt::InputArgList &args) {
1098+
StringRef s = args.getLastArgValue(OPT_call_graph_profile_sort, "hfsort");
1099+
if (s == "hfsort")
1100+
return CGProfileSortKind::Hfsort;
1101+
if (s != "none")
1102+
error("unknown --call-graph-profile-sort= value: " + s);
1103+
return CGProfileSortKind::None;
1104+
}
1105+
10971106
static DebugCompressionType getCompressionType(StringRef s, StringRef option) {
10981107
DebugCompressionType type = StringSwitch<DebugCompressionType>(s)
10991108
.Case("zlib", DebugCompressionType::Zlib)
@@ -1229,6 +1238,7 @@ static void readConfigs(opt::InputArgList &args) {
12291238
else if (arg->getOption().matches(OPT_Bsymbolic))
12301239
config->bsymbolic = BsymbolicKind::All;
12311240
}
1241+
config->callGraphProfileSort = getCGProfileSortKind(args);
12321242
config->checkSections =
12331243
args.hasFlag(OPT_check_sections, OPT_no_check_sections, true);
12341244
config->chroot = args.getLastArgValue(OPT_chroot);
@@ -1249,8 +1259,6 @@ static void readConfigs(opt::InputArgList &args) {
12491259
args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false);
12501260
config->emitLLVM = args.hasArg(OPT_plugin_opt_emit_llvm, false);
12511261
config->emitRelocs = args.hasArg(OPT_emit_relocs);
1252-
config->callGraphProfileSort = args.hasFlag(
1253-
OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true);
12541262
config->enableNewDtags =
12551263
args.hasFlag(OPT_enable_new_dtags, OPT_disable_new_dtags, true);
12561264
config->entry = args.getLastArgValue(OPT_entry);
@@ -1669,7 +1677,7 @@ static void readConfigs(opt::InputArgList &args) {
16691677
config->symbolOrderingFile = getSymbolOrderingFile(*buffer);
16701678
// Also need to disable CallGraphProfileSort to prevent
16711679
// LLD order symbols with CGProfile
1672-
config->callGraphProfileSort = false;
1680+
config->callGraphProfileSort = CGProfileSortKind::None;
16731681
}
16741682
}
16751683

@@ -3072,7 +3080,7 @@ void LinkerDriver::link(opt::InputArgList &args) {
30723080
}
30733081

30743082
// Read the callgraph now that we know what was gced or icfed
3075-
if (config->callGraphProfileSort) {
3083+
if (config->callGraphProfileSort != CGProfileSortKind::None) {
30763084
if (auto *arg = args.getLastArg(OPT_call_graph_ordering_file))
30773085
if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
30783086
readCallGraph(*buffer);

lld/ELF/Options.td

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,12 @@ defm as_needed: B<"as-needed",
128128
defm call_graph_ordering_file:
129129
Eq<"call-graph-ordering-file", "Layout sections to optimize the given callgraph">;
130130

131-
defm call_graph_profile_sort: BB<"call-graph-profile-sort",
132-
"Reorder sections with call graph profile (default)",
133-
"Do not reorder sections with call graph profile">;
131+
def call_graph_profile_sort: JJ<"call-graph-profile-sort=">,
132+
HelpText<"Reorder input sections with call graph profile using the specified algorithm (default: hfsort)">,
133+
MetaVarName<"[none,hfsort]">,
134+
Values<"none,hfsort">;
135+
def : FF<"no-call-graph-profile-sort">, Alias<call_graph_profile_sort>, AliasArgs<["none"]>,
136+
Flags<[HelpHidden]>;
134137

135138
// --chroot doesn't have a help text because it is an internal option.
136139
def chroot: Separate<["--"], "chroot">;

lld/docs/ld.lld.1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ is not intended to be cryptographically secure.
120120
.It Fl -build-id
121121
Synonym for
122122
.Fl -build-id Ns = Ns Cm fast .
123+
.It Fl -call-graph-profile-sort Ns = Ns Ar algorithm
124+
.Ar algorithm
125+
may be:
126+
.Pp
127+
.Bl -tag -width 2n -compact
128+
.It Cm none
129+
Ignore call graph profile.
130+
.It Cm hfsort
131+
Use hfsort (default).
132+
.El
133+
.Pp
123134
.It Fl -color-diagnostics Ns = Ns Ar value
124135
Use colors in diagnostics.
125136
.Ar value

lld/test/ELF/cgprofile-obj.s

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
44
# RUN: ld.lld -e A %t.o -o %t
55
# RUN: llvm-nm --no-sort %t | FileCheck %s
6-
# RUN: ld.lld --no-call-graph-profile-sort -e A %t.o -o %t
6+
# RUN: ld.lld --call-graph-profile-sort=none -e A %t.o -o %t
77
# RUN: llvm-nm --no-sort %t | FileCheck %s --check-prefix=NO-CG
8+
## --no-call-graph-profile-sort is an alias for --call-graph-profile-sort=none.
9+
# RUN: ld.lld --no-call-graph-profile-sort -e A %t.o -o %t1
10+
# RUN: cmp %t %t1
811

912
.section .text.D,"ax",@progbits
1013
D:

lld/test/ELF/cgprofile-txt.s

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@
2424
# RUN: echo "TooManyPreds8 TooManyPreds 10" >> %t.call_graph
2525
# RUN: echo "TooManyPreds9 TooManyPreds 10" >> %t.call_graph
2626
# RUN: echo "TooManyPreds10 TooManyPreds 11" >> %t.call_graph
27-
# RUN: ld.lld -e A %t --call-graph-ordering-file %t.call_graph -o %t2
27+
# RUN: ld.lld -e A %t --call-graph-ordering-file %t.call_graph --call-graph-profile-sort=hfsort -o %t2
2828
# RUN: llvm-readobj --symbols %t2 | FileCheck %s
29+
## --call-graph-profile-sort=hfsort is the default.
30+
# RUN: ld.lld -e A %t --call-graph-ordering-file %t.call_graph -o %t2b
31+
# RUN: cmp %t2 %t2b
32+
33+
# RUN: not ld.lld -e A %t --call-graph-ordering-file %t.call_graph --call-graph-profile-sort=sort \
34+
# RUN: -o /dev/null 2>&1 | FileCheck %s --check-prefix=UNKNOWN
35+
36+
# UNKNOWN: error: unknown --call-graph-profile-sort= value: sort
2937

3038
.section .text.D,"ax",@progbits
3139
D:

0 commit comments

Comments
 (0)