[PATCH] D56325: Sort symbols in .bss by size.

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 4 11:10:39 PST 2019


ruiu created this revision.
ruiu added reviewers: grimar, MaskRay.
Herald added subscribers: arichardson, emaste, srhines.
Herald added a reviewer: espindola.

Android Go team (a project to make Android work better on memory and
CPU-contrained devices) found that this heuristics achieves better
memory write locality, and thus saves memory because it reduces the
number of dirty pages at runtime.

IIRC, we used to sort common symbols by size, but the code is now gone.
This patch in some sense resurrects it and extends to allsymbols in .bss.


https://reviews.llvm.org/D56325

Files:
  lld/ELF/Writer.cpp
  lld/test/ELF/bss-sort.s
  lld/test/ELF/common.s


Index: lld/test/ELF/common.s
===================================================================
--- lld/test/ELF/common.s
+++ lld/test/ELF/common.s
@@ -18,7 +18,7 @@
 // CHECK-NEXT: AddressAlignment: 16
 
 // CHECK:      Name: sym1
-// CHECK-NEXT: Value: 0x201000
+// CHECK-NEXT: Value: 0x201014
 // CHECK-NEXT: Size: 8
 // CHECK-NEXT: Binding: Global
 // CHECK-NEXT: Type: Object
@@ -26,7 +26,7 @@
 // CHECK-NEXT: Section: .bss
 
 // CHECK:      Name: sym2
-// CHECK-NEXT: Value: 0x201008
+// CHECK-NEXT: Value: 0x20101C
 // CHECK-NEXT: Size: 8
 // CHECK-NEXT: Binding: Global
 // CHECK-NEXT: Type: Object
@@ -34,7 +34,7 @@
 // CHECK-NEXT: Section: .bss
 
 // CHECK:      Name: sym3
-// CHECK-NEXT: Value: 0x201010
+// CHECK-NEXT: Value: 0x201000
 // CHECK-NEXT: Size: 2
 // CHECK-NEXT: Binding: Global
 // CHECK-NEXT: Type: Object
@@ -42,7 +42,7 @@
 // CHECK-NEXT: Section: .bss
 
 // CHECK:      Name: sym4
-// CHECK-NEXT: Value: 0x201020
+// CHECK-NEXT: Value: 0x201010
 // CHECK-NEXT: Size: 4
 // CHECK-NEXT: Binding: Global
 // CHECK-NEXT: Type: Object
Index: lld/test/ELF/bss-sort.s
===================================================================
--- /dev/null
+++ lld/test/ELF/bss-sort.s
@@ -0,0 +1,19 @@
+// REQUIRES: x86
+// RUN: echo ".globl s4; .size arr, 4; .bss; s4: .zero 4" | \
+// RUN:   llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %t1.o
+// RUN: echo ".globl s8; .size arr, 8; .bss; s8: .zero 8" | \
+// RUN:   llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %t2.o
+// RUN: echo ".globl s1; .size arr, 1; .bss; s1: .zero 1" | \
+// RUN:   llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %t3.o
+
+// RUN: ld.lld -o %t.exe %t1.o %t2.o %t3.o
+// RUN: llvm-readelf -s %t.exe | FileCheck %s
+
+// Test that symbols in .bss are sorted by size.
+// CHECK: 0000000000201001     0 NOTYPE  GLOBAL DEFAULT    2 s4
+// CHECK: 0000000000201005     0 NOTYPE  GLOBAL DEFAULT    2 s8
+// CHECK: 0000000000201000     0 NOTYPE  GLOBAL DEFAULT    2 s1
+
+.global _start
+_start:
+  nop
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -1241,6 +1241,14 @@
   if (Name == ".init" || Name == ".fini")
     return;
 
+  // .bss contents are by default sorted by symbol size for better packing.
+  // According to Android Go project (a project to make Android work better
+  // on memory/CPU-constrained devices) found that this heuristics also
+  // achieves better memory write locality, which reduces number of dirty
+  // pages at runtime.
+  if (Name == ".bss")
+    Sec->sort([](InputSectionBase *S) { return S->getSize(); });
+
   // Sort input sections by priority using the list provided
   // by --symbol-ordering-file.
   if (!Order.empty())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56325.180286.patch
Type: text/x-patch
Size: 2774 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190104/0f93d7cc/attachment.bin>


More information about the llvm-commits mailing list