← Back to Pull requests

[AArch64] Fix extern_weak function calls on Windows #219202

Open
trcrsired opened this pull request with 158 lines diff across 4 files
View on GitHub
trcrsired trcrsired commented on Aug 27, 2026 12:44 UTC
Author

On aarch64-windows-gnu/msvc, calls to extern_weak functions were incorrectly going through a GOT/COFFSTUB load (adrp/ldr/blr) instead of using a direct BL instruction. This happened because classifyGlobalFunctionReference delegated to ClassifyGlobalReference for Windows, which returned MO_GOT | MO_COFFSTUB for extern_weak symbols.

The fix adds an early return of MO_NO_FLAG for extern_weak function references on Windows, allowing a direct call. The linker handles extern_weak by resolving to either the real symbol or null, so a direct BL is correct (matching ELF behavior).

issue: #218899

Diff: 158 lines (+151 / -7) across 4 files Reviewer: mstorsjo Review duration: 142470m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.45) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 158 lines diff across 4 files
0.45
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.92 · 5 substantive comment(s)
0.92
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
89%
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.303
Real PR Conversation Timeline
8 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Aug 27, 2026 12:45 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-aarch64

Author: cqwrteur (trcrsired)

<details>
<summary>Changes</summary>

On aarch64-windows-gnu/msvc, calls to extern_weak functions were incorrectly going through a GOT/COFFSTUB load (adrp/ldr/blr) instead of using a direct BL instruction. This happened because classifyGlobalFunctionReference delegated to ClassifyGlobalReference for Windows, which returned MO_GOT | MO_COFFSTUB for extern_weak symbols.

The fix adds an early return of MO_NO_FLAG for extern_weak function references on Windows, allowing a direct call. The linker handles extern_weak by resolving to either the real symbol or null, so a direct BL is correct (matching ELF behavior).

issue: #<!-- -->218899

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

2 Files Affected:

  • (modified) llvm/lib/Target/AArch64/AArch64Subtarget.cpp (+6)

  • (modified) llvm/test/CodeGen/AArch64/windows-extern-weak.ll (+23-4)
  • ``````

    diff --git a/llvm/lib/Target/AArch64/AArch64Subtarget.cpp b/llvm/lib/Target/AArch64/AArch64Subtarget.cpp
    index 4fef12ab37ad8..19486ebc36cae 100644
    --- a/llvm/lib/Target/AArch64/AArch64Subtarget.cpp
    +++ b/llvm/lib/Target/AArch64/AArch64Subtarget.cpp
    @@ -503,6 +503,12 @@ unsigned AArch64Subtarget::classifyGlobalFunctionReference(
           }
         }
     
    +    // extern_weak function references on Windows should not go through
    +    // GOT/COFFSTUB. The linker resolves extern_weak to either the real
    +    // symbol or null, so a direct call is correct.
    +    if (GV->hasExternalWeakLinkage())
    +      return AArch64II::MO_NO_FLAG;
    +
         // Use ClassifyGlobalReference for setting MO_DLLIMPORT/MO_COFFSTUB.
         return ClassifyGlobalReference(GV, TM);
       }
    diff --git a/llvm/test/CodeGen/AArch64/windows-extern-weak.ll b/llvm/test/CodeGen/AArch64/windows-extern-weak.ll
    index ecdb802222935..edc5874c4ff39 100644
    --- a/llvm/test/CodeGen/AArch64/windows-extern-weak.ll
    +++ b/llvm/test/CodeGen/AArch64/windows-extern-weak.ll
    @@ -11,7 +11,7 @@ define void @func() nounwind {
     ; CHECK-NEXT:    ldr x8, [x8, :lo12:.refptr.weakfunc]
     ; CHECK-NEXT:    cbz x8, .LBB0_2
     ; CHECK-NEXT:  // %bb.1:
    -; CHECK-NEXT:    blr x8
    +; CHECK-NEXT:    bl weakfunc
     ; CHECK-NEXT:  .LBB0_2:
     ; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
     ; CHECK-NEXT:    ret
    @@ -23,9 +23,7 @@ define void @func() nounwind {
     ; FISEL-NEXT:    ldr x8, [x8, :lo12:.refptr.weakfunc]
     ; FISEL-NEXT:    cbz x8, .LBB0_2
     ; FISEL-NEXT:  // %bb.1:
    -; FISEL-NEXT:    adrp x8, .refptr.weakfunc
    -; FISEL-NEXT:    ldr x8, [x8, :lo12:.refptr.weakfunc]
    -; FISEL-NEXT:    blr x8
    +; FISEL-NEXT:    bl weakfunc
     ; FISEL-NEXT:  .LBB0_2:
     ; FISEL-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
     ; FISEL-NEXT:    ret
    @@ -41,3 +39,24 @@ define void @func() nounwind {
     }
     
     declare extern_weak void @weakfunc()
    +
    +define ptr @use() {
    +; CHECK-LABEL: use:
    +; CHECK:       // %bb.0: // %entry
    +; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
    +; CHECK:    bl foo
    +; CHECK:    ldr x30, [sp], #16 // 8-byte Folded Reload
    +; CHECK:    ret
    +;
    +; FISEL-LABEL: use:
    +; FISEL:       // %bb.0: // %entry
    +; FISEL-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
    +; FISEL:    bl foo
    +; FISEL:    ldr x30, [sp], #16 // 8-byte Folded Reload
    +; FISEL:    ret
    +entry:
    +  %r = call ptr @foo()
    +  ret ptr %r
    +}
    +
    +declare extern_weak dso_local ptr @foo()
    
    
    ``````

    </details>

    mstorsjo mstorsjo commented on code on Aug 27, 2026 12:56 UTC
    Code Review
    llvm/test/CodeGen/AArch64/windows-extern-weak.ll:1
    @@ -11,7 +11,7 @@ define void @func() nounwind {
     ; CHECK-NEXT:    ldr x8, [x8, :lo12:.refptr.weakfunc]
     ; CHECK-NEXT:    cbz x8, .LBB0_2
     ; CHECK-NEXT:  // %bb.1:
    -; CHECK-NEXT:    blr x8
    +; CHECK-NEXT:    bl weakfunc

    This is wrong. The previous indirection through a .refptr is essential here.

    If we have an extern weak, there's a chance that this resolves to absolute null, if there's no actual definition of the symbol. A bl instruction encodes a PC relative offset. If the target symbol is absolute null, this doesn't work.

    So the existing case, of loading the pointer through .refptr, is correct in general.

    If the symbol is marked dso_local then there's no .refptr generated. Only then we may be able to deduce that the symbol won't resolve to absolute null, and the .refptr indirection isn't needed. But for the existin cases where .refptr is used, we must not change the generated code.

    trcrsired trcrsired commented on code on Aug 27, 2026 14:09 UTC
    Code Review
    llvm/test/CodeGen/AArch64/windows-extern-weak.ll:1
    @@ -11,7 +11,7 @@ define void @func() nounwind {
     ; CHECK-NEXT:    ldr x8, [x8, :lo12:.refptr.weakfunc]
     ; CHECK-NEXT:    cbz x8, .LBB0_2
     ; CHECK-NEXT:  // %bb.1:
    -; CHECK-NEXT:    blr x8
    +; CHECK-NEXT:    bl weakfunc

    Manually added with isDSOLocal() check. Should be ok right?

    trcrsired trcrsired commented on code on Aug 27, 2026 14:09 UTC
    Code Review
    llvm/test/CodeGen/AArch64/windows-extern-weak.ll:1
    @@ -11,7 +11,7 @@ define void @func() nounwind {
     ; CHECK-NEXT:    ldr x8, [x8, :lo12:.refptr.weakfunc]
     ; CHECK-NEXT:    cbz x8, .LBB0_2
     ; CHECK-NEXT:  // %bb.1:
    -; CHECK-NEXT:    blr x8
    +; CHECK-NEXT:    bl weakfunc

    Can you review it again?

    trcrsired trcrsired commented on Aug 27, 2026 14:56 UTC
    Author

    @mstorsjo

    trcrsired trcrsired commented on Aug 27, 2026 16:42 UTC
    Author

    CI passed everyone. :) If we found no issues we can merge it and wait several days to see whether we have new regressions.

    mstorsjo mstorsjo commented on Aug 30, 2026 20:59 UTC
    Reviewer

    The end result in the generated code looks reasoanble.

    However I'm hesitant about whether the code change itself is the right way to do it. Most of this handling is similar between X86/ARM/AArch64, and I don't think either of the others have such a special case here. And the fact that we previously generated incorrect code means that there's some other part of the AArch64 backend that disregards the returned flags, so we may want to investigate that too.

    This happened because classifyGlobalFunctionReference delegated to ClassifyGlobalReference for Windows, which returned MO_GOT | MO_COFFSTUB for extern_weak symbols.

    If we returned MO_GOT | MO_COFFSTUB, then we _should_ reference .refptr.foo rather than .foo. But apparently we don't. Can you pinpoint which part ends up ignoring these flags in this case? I guess there's some part that looks at the dso_local flag early, and picks different code generation based on that.

    Then again, on X86/ARM, we don't use .refptr here. Can you look into what happens on X86/ARM, what corresponding flags get returned there, and what differs in the generic logic in ClassifyGlobalReference there that makes it return MO_COFFSTUB here but apparently not on X86/ARM? The general direction of the proper fix would be to find the existing cases and tweak it closer to X86/ARM, not add new special case conditions.

    Reviewers
    Assignees
    trcrsired trcrsired
    Labels
    risk: low confidence: 89.7% 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: 44.7% (MEDIUM) · Higher = larger blast radius
    44.7%
    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: 89.7% · Lower = rushed or shallow review
    89.7%
    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: 4.6% · Safe to merge
    4.6%