← Back to Pull requests

[ELF] Run the branch-to-branch optimization in parallel #246843

Open
GiuseppeCesarano opened this pull request with 18 lines diff across 1 files
View on GitHub
GiuseppeCesarano GiuseppeCesarano commented on Nov 28, 2026 12:08 UTC
Author

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% |

Diff: 18 lines (+13 / -5) across 1 files Reviewer: smithp35 Review duration: 8285m
All Vouch review confidence checks passed
Residual risk 0.02 = Change Risk (0.21) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.21 · 18 lines diff across 1 files
0.21
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.88 · 4 substantive comment(s)
0.88
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
88%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.176
Real PR Conversation Timeline
5 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 28, 2026 12:09 UTC
Bot

<!--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:

  • (modified) lld/ELF/Arch/TargetImpl.h (+13-5)
  • ``````

    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>

    smithp35 smithp35 commented on code on Nov 30, 2026 10:11 UTC
    Code Review
    lld/ELF/Arch/TargetImpl.h:1
    @@ -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 {

    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.

    smithp35 smithp35 commented on code on Nov 30, 2026 10:12 UTC
    Code Review
    lld/ELF/Arch/TargetImpl.h:1
    @@ -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)

    Could this be sharded per object file?

    smithp35 smithp35 commented on Nov 30, 2026 10:13 UTC
    Contributor

    Added MaskRay as a reviewer as he has been doing a lot of similar work recently.

    Reviewers
    Assignees
    GiuseppeCesarano GiuseppeCesarano
    Labels
    risk: low confidence: 88.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 20.6% (LOW) · Higher = larger blast radius
    20.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 88.5% · Lower = rushed or shallow review
    88.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2.4% · Safe to merge
    2.4%