[UniformityAnalysis] Detect divergent cycle exits from a header branch #243417
<!--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:
````````````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>
<!--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:
````````````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>
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?
LGTM
jayfoad
PankajDwivedi-25
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