[PATCH] D128382: [LLD] Two tweaks to symbol ordering scheme

YongKang Zhu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 22 14:44:23 PDT 2022


yozhu created this revision.
Herald added subscribers: arichardson, emaste.
Herald added a reviewer: MaskRay.
Herald added a project: All.
yozhu added a reviewer: smeenai.
Herald added a subscriber: StephenFan.
yozhu published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The linker today will always put hot contributions in the middle of cold ones when targeting
RISC machine, so to minimize the chances of generating branch thunks for hot code calling
into cold code. This is not necessary if less than two input sections contain anything that
is executable, for example, when user specifies an ordering of read-only data (instead of
function) symbols. It is also not necessary if total size of output section is small such
that no branch thunk would ever be required, which is common for mobile apps. For example,
among all the native shared libraries in Facebook Instagram App for Android, 80% of them
have text section smaller than 64KB and the largest text section seen is less than 8MB, well
below the distance a BRANCH26 can reach.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128382

Files:
  lld/ELF/Writer.cpp
  lld/test/ELF/arm-symbol-ordering-file.s


Index: lld/test/ELF/arm-symbol-ordering-file.s
===================================================================
--- lld/test/ELF/arm-symbol-ordering-file.s
+++ lld/test/ELF/arm-symbol-ordering-file.s
@@ -5,10 +5,10 @@
 # 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: ordered
+# CHECK-NEXT: unordered1
 # CHECK-NEXT: unordered2
 # CHECK-NEXT: unordered3
-# CHECK-NEXT: ordered
 # CHECK-NEXT: unordered4
 
 .section .foo,"ax",%progbits,unique,1
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -1316,8 +1316,13 @@
   SmallVector<InputSection *, 0> unorderedSections;
   SmallVector<std::pair<InputSection *, int>, 0> orderedSections;
   uint64_t unorderedSize = 0;
+  uint64_t totalSize = 0;
+  uint64_t executableInputSections = 0;
 
   for (InputSection *isec : isd->sections) {
+    totalSize += isec->getSize();
+    if (isec->flags & SHF_EXECINSTR)
+      executableInputSections++;
     auto i = order.find(isec);
     if (i == order.end()) {
       unorderedSections.push_back(isec);
@@ -1356,7 +1361,9 @@
   // we effectively double the amount of code that could potentially call into
   // the hot code without a thunk.
   size_t insPt = 0;
-  if (target->getThunkSectionSpacing() && !orderedSections.empty()) {
+  if (executableInputSections > 1 && target->getThunkSectionSpacing() &&
+      !orderedSections.empty() &&
+      totalSize >= target->getThunkSectionSpacing()) {
     uint64_t unorderedPos = 0;
     for (; insPt != unorderedSections.size(); ++insPt) {
       unorderedPos += unorderedSections[insPt]->getSize();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128382.439162.patch
Type: text/x-patch
Size: 1724 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220622/699f100d/attachment.bin>


More information about the llvm-commits mailing list