This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Add RelocationScanner. NFC
ClosedPublic

Authored by MaskRay on Jan 9 2022, 12:43 AM.

Details

Summary

Currently the way some relocation-related static functions pass around
states is clumsy. Add a Resolver class to store some states as member
variables.

Advantages:

  • Avoid the parameter InputSectionBase &sec (this offsets the cost passing around this paramemter)
  • Avoid the parameter end (Mips and PowerPC hacks)
  • config and target can be cached as member variables to reduce global state accesses. (potential speedup because the compiler didn't know config/target were not changed across function calls)
  • If we ever want to reduce if-else costs (e.g. config->emachine==EM_MIPS for non-Mips) or introduce parallel relocation scan not handling some tricky arches (PPC/Mips)[1], we can templatize Resolver

target isn't used as much as config, so I change it to a const reference
during the migration.

There is a minor performance inprovement for elf::scanRelocations.

[1]: https://maskray.me/blog/2021-12-19-why-isnt-ld.lld-faster "Scan relocations"

Diff Detail

Event Timeline

MaskRay created this revision.Jan 9 2022, 12:43 AM
MaskRay requested review of this revision.Jan 9 2022, 12:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 9 2022, 12:43 AM
ikudrin added inline comments.Jan 10 2022, 2:19 AM
lld/ELF/Relocations.cpp
434

i should be changed to something more self-explanatory. Or, maybe, OffsetGetter should be preserved as a separate class because it implements isolated functionality.

1294

Why config->isMips64EL is changed to 0?

In principle I'm in favour of having a class with a single public entry point. I agree with ikudrin's comments.

lld/ELF/Relocations.cpp
407

I normally think of resolving a relocation when the final value is written. Given that the only public member function is called scanRelocs. Perhaps call the class Scanner or RelocationScanner the latter is more verbose but we could use scan instead of scanRelocs.

Regardless of what the name is, it will be good to have a comment to describe this large class.

MaskRay updated this revision to Diff 398825.EditedJan 10 2022, 8:21 PM
MaskRay marked 2 inline comments as done.
MaskRay retitled this revision from [ELF] Add Resolver class to relocation scan pass. NFC to [ELF] Add RelocationScanner. NFC.
MaskRay edited the summary of this revision. (Show Details)

Rename to RelocationScanner. Rebase.

The instructions are fewer. Possible minor speed-up:

% hyperfine --warmup 2 --min-runs 40 "numactl -C 20-23 "{/tmp/c/0,/tmp/c/1}" -flavor gnu @response.txt --threads=4 -o lld.8"
Benchmark 1: numactl -C 20-23 /tmp/c/0 -flavor gnu @response.txt --threads=4 -o lld.8
  Time (mean ± σ):      1.311 s ±  0.015 s    [User: 1.360 s, System: 0.502 s]
  Range (min … max):    1.281 s …  1.339 s    40 runs
 
Benchmark 2: numactl -C 20-23 /tmp/c/1 -flavor gnu @response.txt --threads=4 -o lld.8
  Time (mean ± σ):      1.297 s ±  0.012 s    [User: 1.347 s, System: 0.497 s]
  Range (min … max):    1.279 s …  1.333 s    40 runs
 
Summary
  'numactl -C 20-23 /tmp/c/1 -flavor gnu @response.txt --threads=4 -o lld.8' ran
    1.01 ± 0.01 times faster than 'numactl -C 20-23 /tmp/c/0 -flavor gnu @response.txt --threads=4 -o lld.8'
MaskRay added inline comments.Jan 10 2022, 8:35 PM
lld/ELF/Relocations.cpp
407

Changed to RelocationScanner. I do not remember how I picked Resolver...

1294

Fixed.

It was unintentional - I was testing how mips checks affect the performance.

@atanasyan There is missing coverage for mips64el.

This revision is now accepted and ready to land.Jan 10 2022, 11:09 PM

LGTM too, thanks for the updates.

This revision was automatically updated to reflect the committed changes.