This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Parse relocations quickly by assuming sorted order
ClosedPublic

Authored by int3 on Jul 4 2021, 1:10 PM.

Details

Summary

clang and gcc both seem to emit relocations in reverse order of
address. That means we can match relocations to their containing
subsections in O(relocs + subsections) rather than the `O(relocs *
log(subsections))` that our previous binary search implementation
required.

Unfortunately, ld -r can still emit unsorted relocations, so we have a
fallback code path for that (less common) case.

Numbers for linking chromium_framework on my 3.2 GHz 16-Core Intel Xeon W:

    N           Min           Max        Median           Avg        Stddev
x  20          4.04          4.11         4.075        4.0775   0.018027756
+  20          3.95          4.02          3.98         3.985   0.020900768
Difference at 95.0% confidence
        -0.0925 +/- 0.0124919
        -2.26855% +/- 0.306361%
        (Student's t, pooled s = 0.0195172)

Diff Detail

Event Timeline

int3 created this revision.Jul 4 2021, 1:10 PM
Herald added a project: Restricted Project. · View Herald Transcript
int3 requested review of this revision.Jul 4 2021, 1:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 4 2021, 1:10 PM
int3 edited the summary of this revision. (Show Details)Jul 4 2021, 1:20 PM
int3 edited the summary of this revision. (Show Details)
thakis accepted this revision.Jul 4 2021, 4:49 PM
thakis added a subscriber: thakis.

Nice!

This revision is now accepted and ready to land.Jul 4 2021, 4:49 PM
int3 added a comment.Jul 4 2021, 6:25 PM

I forgot to test one more input generator: aside from gcc and clang, we also need to worry about ld -r. Turns out ld64 does indeed emit unsorted relocations :(

It's easy enough to handle though, we can easily fall back to binary searching in that case, which should be pretty rare, so this optimization still holds.

ConcatOutputSection will have to be updated as well to handle unsorted, I will look at that in a separate diff.

int3 updated this revision to Diff 356421.Jul 4 2021, 7:14 PM
int3 edited the summary of this revision. (Show Details)

handle unsorted relocations gracefully with binary search fallback