Skip to content

Commit 3736d08

Browse files
committed
[ELF] Optimize -z combreloc
Sorting dynamic relocations is a bottleneck. Simplifying the comparator improves performance. Linking clang is 4~5% faster with --threads=8. This change may shuffle R_MIPS_REL32 for Mips and is a NFC for non-Mips.
1 parent 89e968f commit 3736d08

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

lld/ELF/SyntheticSections.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,9 +1709,14 @@ template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
17091709
// is to make results easier to read.
17101710
if (sort) {
17111711
const RelType relativeRel = target->relativeRel;
1712-
parallelSort(relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1713-
return std::make_tuple(a.type != relativeRel, a.r_sym, a.r_offset) <
1714-
std::make_tuple(b.type != relativeRel, b.r_sym, b.r_offset);
1712+
auto nonRelative =
1713+
std::stable_partition(relocs.begin(), relocs.end(),
1714+
[=](auto &r) { return r.type == relativeRel; });
1715+
parallelSort(relocs.begin(), nonRelative,
1716+
[&](auto &a, auto &b) { return a.r_offset < b.r_offset; });
1717+
// Non-relative relocations are few, so don't bother with parallelSort.
1718+
std::sort(nonRelative, relocs.end(), [&](auto &a, auto &b) {
1719+
return std::tie(a.r_sym, a.r_offset) < std::tie(b.r_sym, b.r_offset);
17151720
});
17161721
}
17171722

lld/test/ELF/mips-32.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ v2:
4141
# REL-NOT: (RELCOUNT)
4242

4343
# REL: Relocation section
44-
# REL: {{.*}} R_MIPS_REL32
45-
# REL-NEXT: {{.*}} R_MIPS_REL32 [[V2:[0-9a-f]+]]
44+
# REL: {{.*}} R_MIPS_REL32 [[V2:[0-9a-f]+]]
45+
# REL-NEXT: {{.*}} R_MIPS_REL32 {{$}}
4646

4747
# REL: Symbol table
4848
# REL: {{.*}}: [[V2]] {{.*}} v2

lld/test/ELF/mips-64.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ v2:
3232
# CHECK: (RELENT) 16 (bytes)
3333

3434
# CHECK: Relocation section
35-
# CHECK: [[V2:[0-9a-f]+]] {{.*}} R_MIPS_REL32/R_MIPS_64/R_MIPS_NONE
36-
# CHECK: [[V1:[0-9a-f]+]] {{.*}} R_MIPS_REL32/R_MIPS_64/R_MIPS_NONE [[V2]] v2
35+
# CHECK: [[V1:[0-9a-f]+]] {{.*}} R_MIPS_REL32/R_MIPS_64/R_MIPS_NONE [[V2:[0-9a-f]+]] v2
36+
# CHECK-NEXT: [[V2]] {{.*}} R_MIPS_REL32/R_MIPS_64/R_MIPS_NONE {{$}}
3737

3838
# CHECK: Symbol table '.symtab'
3939
# CHECK: {{.*}}: [[V1]] {{.*}} v1

0 commit comments

Comments
 (0)