← Back to Pull requests

[UniformityAnalysis] Detect divergent cycle exits from a header branch #243417

Open
PankajDwivedi-25 opened this pull request with 106 lines diff across 2 files
View on GitHub
PankajDwivedi-25 PankajDwivedi-25 commented on Nov 17, 2026 10:32 UTC
Author

If the divergent branch of a reducible cycle is the header's terminator and its paths leave the cycle through other exiting blocks, the cycle was not recorded as having a divergent exit. Instructions outside the cycle that use values defined inside it were then reported uniform, although threads leave the cycle in different iterations. This includes an exit phi whose incoming values are all the same value, which taintAndPushPhiNodes() skips even if the exit is a join.

Label propagation records the divergent exits of a reducible cycle when it visits the header, but it never visits DivTermBlock. The final check, which compares each exit's label with the header's label, only ran for irreducible cycles. Run it also for a reducible cycle whose header is DivTermBlock.

Because of the missed exit, AMDGPUUniformIntrinsicCombine folded readlane(x, 0) to x after such a loop, with x computed inside it, which miscompiled a HIP kernel at -O3 on gfx90a.

Assisted-by: Claude Opus 5.5

Diff: 106 lines (+103 / -3) across 2 files Reviewer: jayfoad Review duration: 19448m
All Vouch review confidence checks passed
Residual risk 0.11 = Change Risk (0.37) × (1 − Review Confidence (0.71)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 106 lines diff across 2 files
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.56 · 3 substantive comment(s)
0.56
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
70%
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.280
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
5 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 17, 2026 10:33 UTC
Bot

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

@llvm/pr-subscribers-llvm-analysis

Author: Pankaj Dwivedi (PankajDwivedi-25)

<details>
<summary>Changes</summary>

If the divergent branch of a reducible cycle is the header's terminator and its paths leave the cycle through other exiting blocks, the cycle was not recorded as having a divergent exit. Instructions outside the cycle that use values defined inside it were then reported uniform, although threads leave the cycle in different iterations. This includes an exit phi whose incoming values are all the same value, which taintAndPushPhiNodes() skips even if the exit is a join.

Label propagation records the divergent exits of a reducible cycle when it visits the header, but it never visits DivTermBlock. The final check, which compares each exit's label with the header's label, only ran for irreducible cycles. Run it also for a reducible cycle whose header is DivTermBlock.

Because of the missed exit, AMDGPUUniformIntrinsicCombine folded readlane(x, 0) to x after such a loop, with x computed inside it, which miscompiled a HIP kernel at -O3 on gfx90a.

Assisted-by: Claude Opus 5.5

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

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/GenericUniformityImpl.h (+3-3)

  • (modified) llvm/test/Analysis/UniformityAnalysis/AMDGPU/divergent-cycle-exit-phi.ll (+100)
  • ``````

    diff --git a/llvm/include/llvm/ADT/GenericUniformityImpl.h b/llvm/include/llvm/ADT/GenericUniformityImpl.h
    index c6f0bba051b609..edd0a8064fd0e1 100644
    --- a/llvm/include/llvm/ADT/GenericUniformityImpl.h
    +++ b/llvm/include/llvm/ADT/GenericUniformityImpl.h
    @@ -747,9 +747,9 @@ template <typename ContextT> class DivergencePropagator {
         // A cycle has exit divergence if the label of an exit block does
         // not match the label of its header.
         for (auto C = CI.getCycle(&DivTermBlock); C; C = CI.getParentCycle(C)) {
    -      if (CI.isReducible(C)) {
    -        // The exit divergence of a reducible cycle is recorded while
    -        // propagating labels.
    +      if (CI.isReducible(C) && CI.getHeader(C) != &DivTermBlock) {
    +        // The exit divergence of a reducible cycle is recorded when label
    +        // propagation visits its header, which never happens for DivTermBlock.
             continue;
           }
           SmallVector<BlockT *> Exits;
    diff --git a/llvm/test/Analysis/UniformityAnalysis/AMDGPU/divergent-cycle-exit-phi.ll b/llvm/test/Analysis/UniformityAnalysis/AMDGPU/divergent-cycle-exit-phi.ll
    index 945c1df09d14d1..cbe5b6966decc0 100644
    --- a/llvm/test/Analysis/UniformityAnalysis/AMDGPU/divergent-cycle-exit-phi.ll
    +++ b/llvm/test/Analysis/UniformityAnalysis/AMDGPU/divergent-cycle-exit-phi.ll
    @@ -110,3 +110,103 @@ exit.uniform:
       %acc = phi i32 [ 0, %loop ], [ 1, %mid ]
       ret void
     }
    +
    +; The divergent branch is the header's terminator, and its paths leave the cycle
    +; through different exiting blocks. Threads leave in different iterations, so
    +; the exit phi is divergent although its incoming values are identical, and so
    +; is the use of %i outside the cycle.
    +define amdgpu_kernel void @header_divergent_cycle_exit_phi(i32 %n, i32 %m) {
    +; CHECK-LABEL: UniformityInfo for function 'header_divergent_cycle_exit_phi':
    +; CHECK: DIVERGENT:   %i.lcssa
    +; CHECK: DIVERGENT:   %r
    +entry:
    +  %tid = call i32 @llvm.amdgcn.workitem.id.x()
    +  %div.cond = icmp ult i32 %tid, 7
    +  br label %loop
    +
    +loop:
    +  %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]
    +  br i1 %div.cond, label %early, label %latch
    +
    +early:
    +  %stop = icmp eq i32 %i, %m
    +  br i1 %stop, label %exit, label %latch
    +
    +latch:
    +  %i.next = add i32 %i, 1
    +  %exit.cond = icmp eq i32 %i.next, %n
    +  br i1 %exit.cond, label %exit, label %loop
    +
    +exit:
    +  %i.lcssa = phi i32 [ %i, %early ], [ %i, %latch ]
    +  %r = add i32 %i, 1000
    +  ret void
    +}
    +
    +; The divergent branch is the header's terminator, but its paths join inside the
    +; cycle, and all threads leave the cycle in the same iteration.
    +define amdgpu_kernel void @header_divergent_uniform_cycle_exit_phi(i32 %n) {
    +; CHECK-LABEL: UniformityInfo for function 'header_divergent_uniform_cycle_exit_phi':
    +; CHECK-NOT: DIVERGENT:   %i.lcssa
    +; CHECK-NOT: DIVERGENT:   %r
    +entry:
    +  %tid = call i32 @llvm.amdgcn.workitem.id.x()
    +  %div.cond = icmp ult i32 %tid, 7
    +  br label %loop
    +
    +loop:
    +  %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]
    +  br i1 %div.cond, label %then, label %latch
    +
    +then:
    +  br label %latch
    +
    +latch:
    +  %i.next = add i32 %i, 1
    +  %exit.cond = icmp eq i32 %i.next, %n
    +  br i1 %exit.cond, label %exit, label %loop
    +
    +exit:
    +  %i.lcssa = phi i32 [ %i, %latch ]
    +  %r = add i32 %i, 1000
    +  ret void
    +}
    +
    +; Nested cycles: the divergent branch is the inner header's terminator, and its
    +; paths leave the inner cycle, but not the outer one, through different exiting
    +; blocks.
    +define amdgpu_kernel void @nested_header_divergent_cycle_exit_phi(i32 %n, i32 %m) {
    +; CHECK-LABEL: UniformityInfo for function 'nested_header_divergent_cycle_exit_phi':
    +; CHECK: DIVERGENT:   %i.lcssa
    +; CHECK-NOT: DIVERGENT:   %o.next
    +entry:
    +  %tid = call i32 @llvm.amdgcn.workitem.id.x()
    +  %div.cond = icmp ult i32 %tid, 7
    +  br label %outer.header
    +
    +outer.header:
    +  %o = phi i32 [ 0, %entry ], [ %o.next, %outer.latch ]
    +  br label %inner.header
    +
    +inner.header:
    +  %i = phi i32 [ 0, %outer.header ], [ %i.next, %inner.latch ]
    +  br i1 %div.cond, label %inner.early, label %inner.latch
    +
    +inner.early:
    +  %stop = icmp eq i32 %i, %m
    +  br i1 %stop, label %outer.latch, label %inner.latch
    +
    +inner.latch:
    +  %i.next = add i32 %i, 1
    +  %inner.cond = icmp eq i32 %i.next, %n
    +  br i1 %inner.cond, label %outer.latch, label %inner.header
    +
    +outer.latch:
    +  %i.lcssa = phi i32 [ %i, %inner.early ], [ %i, %inner.latch ]
    +  %o.next = add i32 %o, 1
    +  %outer.cond = icmp eq i32 %o.next, 4
    +  br i1 %outer.cond, label %exit, label %outer.header
    +
    +exit:
    +  ret void
    +}
    
    
    ``````

    </details>

    llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 17, 2026 10:33 UTC
    Bot

    <!--LLVM PR SUMMARY COMMENT-->

    @llvm/pr-subscribers-backend-amdgpu

    Author: Pankaj Dwivedi (PankajDwivedi-25)

    <details>
    <summary>Changes</summary>

    If the divergent branch of a reducible cycle is the header's terminator and its paths leave the cycle through other exiting blocks, the cycle was not recorded as having a divergent exit. Instructions outside the cycle that use values defined inside it were then reported uniform, although threads leave the cycle in different iterations. This includes an exit phi whose incoming values are all the same value, which taintAndPushPhiNodes() skips even if the exit is a join.

    Label propagation records the divergent exits of a reducible cycle when it visits the header, but it never visits DivTermBlock. The final check, which compares each exit's label with the header's label, only ran for irreducible cycles. Run it also for a reducible cycle whose header is DivTermBlock.

    Because of the missed exit, AMDGPUUniformIntrinsicCombine folded readlane(x, 0) to x after such a loop, with x computed inside it, which miscompiled a HIP kernel at -O3 on gfx90a.

    Assisted-by: Claude Opus 5.5

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

    2 Files Affected:

  • (modified) llvm/include/llvm/ADT/GenericUniformityImpl.h (+3-3)

  • (modified) llvm/test/Analysis/UniformityAnalysis/AMDGPU/divergent-cycle-exit-phi.ll (+100)
  • ``````

    diff --git a/llvm/include/llvm/ADT/GenericUniformityImpl.h b/llvm/include/llvm/ADT/GenericUniformityImpl.h
    index c6f0bba051b609..edd0a8064fd0e1 100644
    --- a/llvm/include/llvm/ADT/GenericUniformityImpl.h
    +++ b/llvm/include/llvm/ADT/GenericUniformityImpl.h
    @@ -747,9 +747,9 @@ template <typename ContextT> class DivergencePropagator {
         // A cycle has exit divergence if the label of an exit block does
         // not match the label of its header.
         for (auto C = CI.getCycle(&DivTermBlock); C; C = CI.getParentCycle(C)) {
    -      if (CI.isReducible(C)) {
    -        // The exit divergence of a reducible cycle is recorded while
    -        // propagating labels.
    +      if (CI.isReducible(C) && CI.getHeader(C) != &DivTermBlock) {
    +        // The exit divergence of a reducible cycle is recorded when label
    +        // propagation visits its header, which never happens for DivTermBlock.
             continue;
           }
           SmallVector<BlockT *> Exits;
    diff --git a/llvm/test/Analysis/UniformityAnalysis/AMDGPU/divergent-cycle-exit-phi.ll b/llvm/test/Analysis/UniformityAnalysis/AMDGPU/divergent-cycle-exit-phi.ll
    index 945c1df09d14d1..cbe5b6966decc0 100644
    --- a/llvm/test/Analysis/UniformityAnalysis/AMDGPU/divergent-cycle-exit-phi.ll
    +++ b/llvm/test/Analysis/UniformityAnalysis/AMDGPU/divergent-cycle-exit-phi.ll
    @@ -110,3 +110,103 @@ exit.uniform:
       %acc = phi i32 [ 0, %loop ], [ 1, %mid ]
       ret void
     }
    +
    +; The divergent branch is the header's terminator, and its paths leave the cycle
    +; through different exiting blocks. Threads leave in different iterations, so
    +; the exit phi is divergent although its incoming values are identical, and so
    +; is the use of %i outside the cycle.
    +define amdgpu_kernel void @header_divergent_cycle_exit_phi(i32 %n, i32 %m) {
    +; CHECK-LABEL: UniformityInfo for function 'header_divergent_cycle_exit_phi':
    +; CHECK: DIVERGENT:   %i.lcssa
    +; CHECK: DIVERGENT:   %r
    +entry:
    +  %tid = call i32 @llvm.amdgcn.workitem.id.x()
    +  %div.cond = icmp ult i32 %tid, 7
    +  br label %loop
    +
    +loop:
    +  %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]
    +  br i1 %div.cond, label %early, label %latch
    +
    +early:
    +  %stop = icmp eq i32 %i, %m
    +  br i1 %stop, label %exit, label %latch
    +
    +latch:
    +  %i.next = add i32 %i, 1
    +  %exit.cond = icmp eq i32 %i.next, %n
    +  br i1 %exit.cond, label %exit, label %loop
    +
    +exit:
    +  %i.lcssa = phi i32 [ %i, %early ], [ %i, %latch ]
    +  %r = add i32 %i, 1000
    +  ret void
    +}
    +
    +; The divergent branch is the header's terminator, but its paths join inside the
    +; cycle, and all threads leave the cycle in the same iteration.
    +define amdgpu_kernel void @header_divergent_uniform_cycle_exit_phi(i32 %n) {
    +; CHECK-LABEL: UniformityInfo for function 'header_divergent_uniform_cycle_exit_phi':
    +; CHECK-NOT: DIVERGENT:   %i.lcssa
    +; CHECK-NOT: DIVERGENT:   %r
    +entry:
    +  %tid = call i32 @llvm.amdgcn.workitem.id.x()
    +  %div.cond = icmp ult i32 %tid, 7
    +  br label %loop
    +
    +loop:
    +  %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]
    +  br i1 %div.cond, label %then, label %latch
    +
    +then:
    +  br label %latch
    +
    +latch:
    +  %i.next = add i32 %i, 1
    +  %exit.cond = icmp eq i32 %i.next, %n
    +  br i1 %exit.cond, label %exit, label %loop
    +
    +exit:
    +  %i.lcssa = phi i32 [ %i, %latch ]
    +  %r = add i32 %i, 1000
    +  ret void
    +}
    +
    +; Nested cycles: the divergent branch is the inner header's terminator, and its
    +; paths leave the inner cycle, but not the outer one, through different exiting
    +; blocks.
    +define amdgpu_kernel void @nested_header_divergent_cycle_exit_phi(i32 %n, i32 %m) {
    +; CHECK-LABEL: UniformityInfo for function 'nested_header_divergent_cycle_exit_phi':
    +; CHECK: DIVERGENT:   %i.lcssa
    +; CHECK-NOT: DIVERGENT:   %o.next
    +entry:
    +  %tid = call i32 @llvm.amdgcn.workitem.id.x()
    +  %div.cond = icmp ult i32 %tid, 7
    +  br label %outer.header
    +
    +outer.header:
    +  %o = phi i32 [ 0, %entry ], [ %o.next, %outer.latch ]
    +  br label %inner.header
    +
    +inner.header:
    +  %i = phi i32 [ 0, %outer.header ], [ %i.next, %inner.latch ]
    +  br i1 %div.cond, label %inner.early, label %inner.latch
    +
    +inner.early:
    +  %stop = icmp eq i32 %i, %m
    +  br i1 %stop, label %outer.latch, label %inner.latch
    +
    +inner.latch:
    +  %i.next = add i32 %i, 1
    +  %inner.cond = icmp eq i32 %i.next, %n
    +  br i1 %inner.cond, label %outer.latch, label %inner.header
    +
    +outer.latch:
    +  %i.lcssa = phi i32 [ %i, %inner.early ], [ %i, %inner.latch ]
    +  %o.next = add i32 %o, 1
    +  %outer.cond = icmp eq i32 %o.next, 4
    +  br i1 %outer.cond, label %exit, label %outer.header
    +
    +exit:
    +  ret void
    +}
    
    
    ``````

    </details>

    jayfoad jayfoad commented on Nov 18, 2026 16:49 UTC
    Contributor

    Because of the missed exit, AMDGPUUniformIntrinsicCombine folded readlane(x, 0) to x after such a loop, with x computed inside it, which miscompiled a HIP kernel at -O3 on gfx90a.

    Could add a regression test for that AMDGPUUniformIntrinsicCombine miscompile?

    jayfoad jayfoad approved these changes on Nov 18, 2026 16:49 UTC
    Approved Reviewer

    LGTM

    Reviewers
    Assignees
    PankajDwivedi-25 PankajDwivedi-25
    Labels
    risk: low confidence: 70.9% 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: 37.4% (MEDIUM) · Higher = larger blast radius
    37.4%
    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: 70.9% · Lower = rushed or shallow review
    70.9%
    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: 10.9% · Safe to merge
    10.9%