[PATCH] D44969: ELF: Place ordered sections in the middle of the unordered section list on targets with limited-range branches.
Peter Collingbourne via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 27 20:59:53 PDT 2018
pcc created this revision.
pcc added reviewers: ruiu, espindola, peter.smith.
Herald added subscribers: mgrang, arichardson, javed.absar, emaste.
It generally does not matter much where we place sections ordered
by --symbol-ordering-file relative to other sections. But if the
ordered sections are hot (which is the case already for some users
of --symbol-ordering-file, and is increasingly more likely to be
the case once profile-guided section layout lands) and the target
has limited-range branches, it is beneficial to place the ordered
sections in the middle of the output section in order to decrease
the likelihood that a range extension thunk will be required to call
a hot function from a cold function or vice versa.
That is what this patch does. After https://reviews.llvm.org/D44966 it reduces the size of
Chromium for Android's .text section by 60KB.
https://reviews.llvm.org/D44969
Files:
lld/ELF/Writer.cpp
lld/test/ELF/arm-symbol-ordering-file.s
Index: lld/test/ELF/arm-symbol-ordering-file.s
===================================================================
--- /dev/null
+++ lld/test/ELF/arm-symbol-ordering-file.s
@@ -0,0 +1,31 @@
+# RUN: llvm-mc -filetype=obj -triple=armv7-unknown-linux %s -o %t.o
+
+# RUN: echo ordered > %t_order.txt
+# RUN: ld.lld --symbol-ordering-file %t_order.txt %t.o -o %t2.out
+# RUN: llvm-nm -n %t2.out | FileCheck %s
+
+# CHECK: unordered1
+# CHECK-NEXT: unordered2
+# CHECK-NEXT: unordered3
+# CHECK-NEXT: ordered
+# CHECK-NEXT: unordered4
+
+.section .foo,"ax",%progbits,unique,1
+unordered1:
+.zero 1
+
+.section .foo,"ax",%progbits,unique,2
+unordered2:
+.zero 1
+
+.section .foo,"ax",%progbits,unique,3
+unordered3:
+.zero 2
+
+.section .foo,"ax",%progbits,unique,4
+unordered4:
+.zero 4
+
+.section .foo,"ax",%progbits,unique,5
+ordered:
+.zero 1
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -1085,6 +1085,47 @@
return SectionOrder;
}
+static void
+sortISDBySectionOrder(InputSectionDescription *ISD,
+ const DenseMap<const InputSectionBase *, int> &Order) {
+ std::vector<InputSection *> UnorderedSections, OrderedSections;
+ uint64_t UnorderedSize = 0;
+
+ for (InputSection *IS : ISD->Sections) {
+ if (!Order.count(IS)) {
+ UnorderedSections.push_back(IS);
+ UnorderedSize += IS->getSize();
+ continue;
+ }
+ OrderedSections.push_back(IS);
+ }
+ std::sort(OrderedSections.begin(), OrderedSections.end(),
+ [&](InputSection *A, InputSection *B) {
+ return Order.lookup(A) < Order.lookup(B);
+ });
+
+ // Find an insertion point for the ordered section list in the unordered
+ // section list. On targets with limited-range branches, this is the mid-point
+ // of the unordered section list. This decreases the likelihood that a range
+ // extension thunk will be needed to enter or exit the ordered region.
+ size_t UnorderedInsPt = 0;
+ if (Target->ThunkSectionSpacing && !OrderedSections.empty()) {
+ uint64_t UnorderedPos = 0;
+ for (; UnorderedInsPt != UnorderedSections.size(); ++UnorderedInsPt) {
+ UnorderedPos += UnorderedSections[UnorderedInsPt]->getSize();
+ if (UnorderedPos > UnorderedSize / 2)
+ break;
+ }
+ }
+
+ std::copy(UnorderedSections.begin(),
+ UnorderedSections.begin() + UnorderedInsPt, ISD->Sections.begin());
+ std::copy(OrderedSections.begin(), OrderedSections.end(),
+ ISD->Sections.begin() + UnorderedInsPt);
+ std::copy(UnorderedSections.begin() + UnorderedInsPt, UnorderedSections.end(),
+ ISD->Sections.begin() + UnorderedInsPt + OrderedSections.size());
+}
+
static void sortSection(OutputSection *Sec,
const DenseMap<const InputSectionBase *, int> &Order) {
StringRef Name = Sec->Name;
@@ -1111,7 +1152,9 @@
// Sort input sections by priority using the list provided
// by --symbol-ordering-file.
if (!Order.empty())
- Sec->sort([&](InputSectionBase *S) { return Order.lookup(S); });
+ for (BaseCommand *B : Sec->SectionCommands)
+ if (auto *ISD = dyn_cast<InputSectionDescription>(B))
+ sortISDBySectionOrder(ISD, Order);
}
// If no layout was provided by linker script, we want to apply default
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44969.140036.patch
Type: text/x-patch
Size: 3347 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180328/524f7f3f/attachment.bin>
More information about the llvm-commits
mailing list