Skip to content

Commit

Permalink
[ELF] Optimize -z combreloc
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MaskRay committed Jan 16, 2022
1 parent 89e968f commit 3736d08
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
11 changes: 8 additions & 3 deletions lld/ELF/SyntheticSections.cpp
Expand Up @@ -1709,9 +1709,14 @@ template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
// is to make results easier to read.
if (sort) {
const RelType relativeRel = target->relativeRel;
parallelSort(relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
return std::make_tuple(a.type != relativeRel, a.r_sym, a.r_offset) <
std::make_tuple(b.type != relativeRel, b.r_sym, b.r_offset);
auto nonRelative =
std::stable_partition(relocs.begin(), relocs.end(),
[=](auto &r) { return r.type == relativeRel; });
parallelSort(relocs.begin(), nonRelative,
[&](auto &a, auto &b) { return a.r_offset < b.r_offset; });
// Non-relative relocations are few, so don't bother with parallelSort.
std::sort(nonRelative, relocs.end(), [&](auto &a, auto &b) {
return std::tie(a.r_sym, a.r_offset) < std::tie(b.r_sym, b.r_offset);
});
}

Expand Down
4 changes: 2 additions & 2 deletions lld/test/ELF/mips-32.s
Expand Up @@ -41,8 +41,8 @@ v2:
# REL-NOT: (RELCOUNT)

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

# REL: Symbol table
# REL: {{.*}}: [[V2]] {{.*}} v2
Expand Down
4 changes: 2 additions & 2 deletions lld/test/ELF/mips-64.s
Expand Up @@ -32,8 +32,8 @@ v2:
# CHECK: (RELENT) 16 (bytes)

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

# CHECK: Symbol table '.symtab'
# CHECK: {{.*}}: [[V1]] {{.*}} v1
Expand Down

0 comments on commit 3736d08

Please sign in to comment.