[ELF] Run the branch-to-branch optimization in parallel #246843
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld
@llvm/pr-subscribers-lld-elf
Author: Giuseppe Cesarano (GiuseppeCesarano)
<details>
<summary>Changes</summary>
Collects branch-to-branch redirects per object file with parallelFor, then applies them serially. The output is byte-identical at any --threads value.
hyperfine -N --warmup 5 --runs 40 \
'ld.lld-base --threads=8 @<!-- -->clang.rsp -O2 -o clang' \
'ld.lld-patched --threads=8 @<!-- -->clang.rsp -O2 -o clang'
Linking clang on an i7-7700K (4C/8T):
| | base | patched | |
|---|---|---|---|
| -O3 | 332.6 ms ± 2.9 ms | 320.5 ms ± 3.4 ms | -3.6% |
| -O2 | 339.2 ms ± 3.8 ms | 323.4 ms ± 6.2 ms | -4.6% |
---
Full diff: https://github.com/llvm/llvm-project/pull/246843.diff
1 Files Affected:
````````````diff --git a/lld/ELF/Arch/TargetImpl.h b/lld/ELF/Arch/TargetImpl.h
index 0d4793afbb2eda..6558314eee2a3f 100644
--- a/lld/ELF/Arch/TargetImpl.h
+++ b/lld/ELF/Arch/TargetImpl.h
@@ -14,6 +14,7 @@
#include "Relocations.h"
#include "Symbols.h"
#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/Support/Parallel.h"
namespace lld::elf {
@@ -38,9 +39,13 @@ inline void applyBranchToBranchOptImpl(
Ctx &ctx, GetControlTransferAddend getControlTransferAddend,
GetBranchInfoAtTarget getBranchInfoAtTarget,
RedirectControlTransferRelocations redirectControlTransferRelocations) {
- // Needs to run serially because it writes to the relocations array as well as
- // reading relocations of other sections.
- for (ELFFileBase *f : ctx.objectFiles) {
+ struct Redirect {
+ Relocation *from;
+ const Relocation *to;
+ };
+ SmallVector<SmallVector<Redirect, 0>, 0> redirects(ctx.objectFiles.size());
+ parallelFor(0, ctx.objectFiles.size(), [&](size_t fileIndex) {
+ ELFFileBase *f = ctx.objectFiles[fileIndex];
auto getRelocBranchInfo =
[&getBranchInfoAtTarget](
Relocation &r,
@@ -82,10 +87,13 @@ inline void applyBranchToBranchOptImpl(
break;
targetAndAddend = nextTargetAndAddend;
}
- redirectControlTransferRelocations(r, *targetAndAddend.first);
+ redirects[fileIndex].push_back({&r, targetAndAddend.first});
}
}
- }
+ });
+ for (SmallVector<Redirect, 0> &fileRedirects : redirects)
+ for (const Redirect &redirect : fileRedirects)
+ redirectControlTransferRelocations(*redirect.from, *redirect.to);
}
} // namespace lld::elf
</details>
Would like to see a comment on the parallelisation strategy that has been chosen as well as an argument as to why it is safe.
In the serial case the relocations are being updated as we go. In theory relocations from other objects may reach relocations that have been updated. In the parallel case these updates are being made in series at the end.
I think it is likely that both strategies will converge to the same results but would be useful to see this reasoned through.
Could this be sharded per object file?
Added MaskRay as a reviewer as he has been doing a lot of similar work recently.
smithp35
GiuseppeCesarano
Collects branch-to-branch redirects per object file with
parallelFor, then applies them serially. The output is byte-identical at any--threadsvalue.Linking clang on an i7-7700K (4C/8T):
| | base | patched | |
|---|---|---|---|
|
-O3| 332.6 ms ± 2.9 ms | 320.5 ms ± 3.4 ms | -3.6% ||
-O2| 339.2 ms ± 3.8 ms | 323.4 ms ± 6.2 ms | -4.6% |