← Back to Pull requests

[LLD][COFF] Avoid repeated vector insertion during thunk creation (up to 10x improvement for large links) #241059

Merged
kfarnung merged 1 files (17 lines) on Nov 17, 2026 22:07 UTC
View on GitHub
kfarnung kfarnung commented on Nov 10, 2026 23:51 UTC
Author

Inserting each range-extension thunk into OutputSection::chunks shifts
the remaining chunk pointers. When many chunks need thunks, this makes
chunk-list construction quadratic.

Append the original chunks and their thunks to a new vector and swap it
into the output section, preserving their order and the existing thunk
placement, reuse, relocation rewriting, and retry policy. Newly created
range-extension thunks need no additional visit because they inherit the
no-op extendRanges implementation.

On my x64 Windows machine it reduces a large ARM64 debug link from 43
minutes down to 2 minutes.

Tested with ninja -C build check-lld-coff: 559 passed, 6 unsupported.

Assisted-by: GitHub Copilot CLI (GPT-6.1 Sol)

Diff: 17 lines (+9 / -8) across 1 files Reviewer: mstorsjo Review duration: 9976m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.25) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.25 · 17 lines diff across 1 files
0.25
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 3 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
90%
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.173
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
5 items
github-actions[bot] github-actions[bot] commented on Nov 10, 2026 23:51 UTC
Bot

<!--LLVM NEW CONTRIBUTOR COMMENT-->

Hello @kfarnung :wave:

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.

  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.
  • Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.

    ---

    Frequently asked questions

    How do I add reviewers?

    This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

    You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

    What if there are no comments?

    If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

    Are any special GitHub settings required to contribute to LLVM?

    We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.

    ---

    If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

    Thank you,
    The LLVM Community

    llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 11, 2026 00:14 UTC
    Bot

    <!--LLVM PR SUMMARY COMMENT-->
    @llvm/pr-subscribers-lld-coff

    @llvm/pr-subscribers-lld

    Author: Kyle Farnung (kfarnung)

    <details>
    <summary>Changes</summary>

    Inserting each range-extension thunk into OutputSection::chunks shifts
    the remaining chunk pointers. When many chunks need thunks, this makes
    chunk-list construction quadratic.

    Append the original chunks and their thunks to a new vector and swap it
    into the output section, preserving their order and the existing thunk
    placement, reuse, relocation rewriting, and retry policy. Newly created
    range-extension thunks need no additional visit because they inherit the
    no-op extendRanges implementation.

    Tested with ninja -C build check-lld-coff: 559 passed, 6 unsupported.

    Assisted-by: GitHub Copilot CLI (GPT-6.1 Sol)

    ---
    Full diff: https://github.com/llvm/llvm-project/pull/241059.diff

    1 Files Affected:

  • (modified) lld/COFF/Writer.cpp (+9-8)
  • ``````

    diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
    index 3cab426df55cf..c66b44c3597d0 100644
    --- a/lld/COFF/Writer.cpp
    +++ b/lld/COFF/Writer.cpp
    @@ -491,12 +491,14 @@ bool Writer::createThunks(OutputSection *os, int margin) {
       DenseMap<uint64_t, Defined *> lastThunks;
       DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> thunkSymtabIndices;
       size_t thunksSize = 0;
    -  // Recheck Chunks.size() each iteration, since we can insert more
    -  // elements into it.
    -  for (size_t i = 0; i != os->chunks.size(); ++i) {
    -    SectionChunk *sc = dyn_cast<SectionChunk>(os->chunks[i]);
    +  // Append thunks without repeatedly shifting the remaining chunks.
    +  std::vector<Chunk *> newChunks;
    +  newChunks.reserve(os->chunks.size());
    +  for (Chunk *c : os->chunks) {
    +    newChunks.push_back(c);
    +    SectionChunk *sc = dyn_cast<SectionChunk>(c);
         if (!sc) {
    -      auto chunk = cast<NonSectionChunk>(os->chunks[i]);
    +      auto chunk = cast<NonSectionChunk>(c);
           if (uint32_t size = chunk->extendRanges()) {
             thunksSize += size;
             addressesChanged = true;
    @@ -504,7 +506,6 @@ bool Writer::createThunks(OutputSection *os, int margin) {
           continue;
         }
         MachineTypes machine = sc->getMachine();
    -    size_t thunkInsertionSpot = i + 1;
     
         // Try to get a good enough estimate of where new thunks will be placed.
         // Offset this by the size of the new thunks added so far, to make the
    @@ -540,8 +541,7 @@ bool Writer::createThunks(OutputSection *os, int margin) {
             Chunk *thunkChunk = thunk->getChunk();
             thunkChunk->setRVA(
                 thunkInsertionRVA); // Estimate of where it will be located.
    -        os->chunks.insert(os->chunks.begin() + thunkInsertionSpot, thunkChunk);
    -        thunkInsertionSpot++;
    +        newChunks.push_back(thunkChunk);
             thunksSize += thunkChunk->getSize();
             thunkInsertionRVA += thunkChunk->getSize();
             addressesChanged = true;
    @@ -586,6 +586,7 @@ bool Writer::createThunks(OutputSection *os, int margin) {
     
         sc->setRelocs(newRelocs);
       }
    +  os->chunks.swap(newChunks);
       return addressesChanged;
     }
     
    
    
    ``````

    </details>

    mstorsjo mstorsjo approved these changes on Nov 17, 2026 13:36 UTC
    Approved Reviewer

    LGTM, thanks!

    github-actions[bot] github-actions[bot] commented on Nov 17, 2026 22:08 UTC
    Bot

    <!--LLVM BUILDBOT INFORMATION COMMENT-->

    @kfarnung Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

    Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

    Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

    How to do this, and the rest of the post-merge process, is covered in detail here.

    If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

    If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

    Reviewers
    Assignees
    kfarnung kfarnung
    Labels
    risk: low confidence: 90% 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: 25.3% (LOW) · Higher = larger blast radius
    25.3%
    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: 90% · Lower = rushed or shallow review
    90%
    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.5% · Safe to merge
    2.5%