Skip to content

Commit

Permalink
[lld] Add a new output section ".text.unknown" for funtions with unkn…
Browse files Browse the repository at this point in the history
…own hotness

For sampleFDO, because the optimized build uses profile generated from previous
release, often we couldn't tell a function without profile was truely cold or
just newly created so we had to treat them conservatively and put them in .text
section instead of .text.unlikely. The result was when we persue the best
performance by locking .text.hot and .text in memory, we wasted a lot of memory
to keep cold functions inside. This problem has been largely solved for regular
sampleFDO using profile-symbol-list (https://reviews.llvm.org/D66374), but for
the case when we use partial profile, we still waste a lot of memory because
of it.

In https://reviews.llvm.org/D62540, we propose to save functions with unknown
hotness information in a special section called ".text.unknown", so that
compiler will treat those functions as luck-warm, but runtime can choose not
to mlock the special section in memory or use other strategy to save memory.
That will solve most of the memory problem even if we use a partial profile.

The patch adds the support in lld for the special section.For sampleFDO,
because the optimized build uses profile generated from previous release,
often we couldn't tell a function without profile was truely cold or just
newly created so we had to treat them conservatively and put them in .text
section instead of .text.unlikely. The result was when we persue the best
performance by locking .text.hot and .text in memory, we wasted a lot of
memory to keep cold functions inside. This problem has been largely solved
for regular sampleFDO using profile-symbol-list
(https://reviews.llvm.org/D66374), but for the case when we use partial
profile, we still waste a lot of memory because of it.

In https://reviews.llvm.org/D62540, we propose to save functions with unknown
hotness information in a special section called ".text.unknown", so that
compiler will treat those functions as luck-warm, but runtime can choose not
to mlock the special section in memory or use other strategy to save memory.
That will solve most of the memory problem even if we use a partial profile.

The patch adds the support in lld for the special section.

Differential Revision: https://reviews.llvm.org/D79590
  • Loading branch information
wmi-11 committed May 8, 2020
1 parent b38d77f commit 538208f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
17 changes: 11 additions & 6 deletions lld/ELF/Writer.cpp
Expand Up @@ -122,13 +122,18 @@ StringRef getOutputSectionName(const InputSectionBase *s) {
// When no SECTIONS is specified, emulate GNU ld's internal linker scripts
// by grouping sections with certain prefixes.

// GNU ld places text sections with prefix ".text.hot.", ".text.unlikely.",
// ".text.startup." or ".text.exit." before others. We provide an option -z
// keep-text-section-prefix to group such sections into separate output
// sections. This is more flexible. See also sortISDBySectionOrder().
// GNU ld places text sections with prefix ".text.hot.", ".text.unknown.",
// ".text.unlikely.", ".text.startup." or ".text.exit." before others.
// We provide an option -z keep-text-section-prefix to group such sections
// into separate output sections. This is more flexible. See also
// sortISDBySectionOrder().
// ".text.unknown" means the hotness of the section is unknown. When
// SampleFDO is used, if a function doesn't have sample, it could be very
// cold or it could be a new function never being sampled. Those functions
// will be kept in the ".text.unknown" section.
if (config->zKeepTextSectionPrefix)
for (StringRef v :
{".text.hot.", ".text.unlikely.", ".text.startup.", ".text.exit."})
for (StringRef v : {".text.hot.", ".text.unknown.", ".text.unlikely.",
".text.startup.", ".text.exit."})
if (isSectionPrefix(v, s->name))
return v.drop_back();

Expand Down
14 changes: 10 additions & 4 deletions lld/test/ELF/text-section-prefix.s
@@ -1,6 +1,7 @@
# REQUIRES: x86
## -z keep-text-section-prefix separates text sections with prefix .text.hot,
## .text.unlikely, .text.startup, or .text.exit, in the absence of a SECTIONS command.
## .text.unknown, .text.unlikely, .text.startup, or .text.exit, in the absence
## of a SECTIONS command.

# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
# RUN: ld.lld %t.o -o %t1
Expand All @@ -13,9 +14,10 @@

# KEEP: [ 1] .text
# KEEP-NEXT: [ 2] .text.hot
# KEEP-NEXT: [ 3] .text.startup
# KEEP-NEXT: [ 4] .text.exit
# KEEP-NEXT: [ 5] .text.unlikely
# KEEP-NEXT: [ 3] .text.unknown
# KEEP-NEXT: [ 4] .text.startup
# KEEP-NEXT: [ 5] .text.exit
# KEEP-NEXT: [ 6] .text.unlikely

# NOKEEP: [ 1] .text
# NOKEEP-NOT: .text
Expand All @@ -29,6 +31,7 @@
# SCRIPT: .text
# SCRIPT-NEXT: .text.f
# SCRIPT-NEXT: .text.hot.f_hot
# SCRIPT-NEXT: .text.unknown.f_unknown
# SCRIPT-NEXT: .text.startup.f_startup
# SCRIPT-NEXT: .text.exit.f_exit
# SCRIPT-NEXT: .text.unlikely.f_unlikely
Expand All @@ -43,6 +46,9 @@ _start:
.section .text.hot.f_hot,"ax"
nop

.section .text.unknown.f_unknown,"ax"
nop

.section .text.startup.f_startup,"ax"
nop

Expand Down

0 comments on commit 538208f

Please sign in to comment.