[llvm] [AMDGPU] Rewrite undef PHIs with a unique constant value (PR #214319)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 7 08:47:04 PDT 2026


https://github.com/robertvirany updated https://github.com/llvm/llvm-project/pull/214319

>From 57757af7b53c75ce3e012ee673acd8e04ed82c0f Mon Sep 17 00:00:00 2001
From: Robert Virany <robertvirany at gmail.com>
Date: Thu, 30 Jul 2026 15:46:35 -0600
Subject: [PATCH 1/2] [AMDGPU] Precommit constant handling in
 RewriteUndefForPHI

---
 .../CodeGen/AMDGPU/rewrite-undef-for-phi.ll   | 22 +++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/llvm/test/CodeGen/AMDGPU/rewrite-undef-for-phi.ll b/llvm/test/CodeGen/AMDGPU/rewrite-undef-for-phi.ll
index f89a5d6f242e6..94f3975d201e6 100644
--- a/llvm/test/CodeGen/AMDGPU/rewrite-undef-for-phi.ll
+++ b/llvm/test/CodeGen/AMDGPU/rewrite-undef-for-phi.ll
@@ -24,6 +24,28 @@ end:
   ret float %c2
 }
 
+define amdgpu_ps i32 @constant_defined_on_non_dominating_path(i32 %x) #0 {
+; OPT-LABEL: @constant_defined_on_non_dominating_path(
+; OPT-NEXT:  entry:
+; OPT-NEXT:    [[CC:%.*]] = icmp slt i32 [[X:%.*]], 0
+; OPT-NEXT:    br i1 [[CC]], label [[IF:%.*]], label [[END:%.*]]
+; OPT:       if:
+; OPT-NEXT:    br label [[END]]
+; OPT:       end:
+; OPT-NEXT:    ret i32 42
+;
+entry:
+  %cc = icmp slt i32 %x, 0
+  br i1 %cc, label %if, label %end
+
+if:
+  br label %end
+
+end:
+  %result = phi i32 [ 42, %if ], [ poison, %entry ]
+  ret i32 %result
+}
+
 define amdgpu_ps float @with_uniform_region_inside(float inreg %c, i32 inreg %d, i32 %x) #0 {
 ; OPT-LABEL: @with_uniform_region_inside(
 ; OPT-NEXT:  entry:

>From 32c2e6f6b288eee195a2cb550c0848c19b9f1513 Mon Sep 17 00:00:00 2001
From: Robert Virany <robertvirany at gmail.com>
Date: Wed, 5 Aug 2026 13:31:02 -0600
Subject: [PATCH 2/2] [AMDGPU] Rewrite constant-valued undef PHIs

---
 .../AMDGPU/AMDGPURewriteUndefForPHI.cpp       | 31 ++++++++++---------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPURewriteUndefForPHI.cpp b/llvm/lib/Target/AMDGPU/AMDGPURewriteUndefForPHI.cpp
index c6757db2d213e..7ba0cb7c180a2 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPURewriteUndefForPHI.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPURewriteUndefForPHI.cpp
@@ -140,23 +140,24 @@ bool rewritePHIs(Function &F, UniformityInfo &UA, DominatorTree *DT) {
       }
       // We only need to replace the undef for the PHI which is merging
       // defined/undefined values from divergent threads.
-      // TODO: We should still be able to replace undef value if the unique
-      // value is a Constant.
-      if (!UniqueDefinedIncoming || Undefs.empty() ||
-          UA.isUniformTerminator(DominateBB->getTerminator()))
+      if (!UniqueDefinedIncoming || Undefs.empty())
         continue;
 
-      // We only replace the undef when DominateBB truly dominates all the
-      // other predecessors with undefined incoming value. Make sure DominateBB
-      // dominates BB so that UniqueDefinedIncoming is available in BB and
-      // afterwards.
-      if (DT->dominates(DominateBB, &BB) && all_of(Undefs, [&](BasicBlock *UD) {
-            return DT->dominates(DominateBB, UD);
-          })) {
-        PHI.replaceAllUsesWith(UniqueDefinedIncoming);
-        ToBeDeleted.push_back(&PHI);
-        Changed = true;
-      }
+      // For non-constant incoming values, require DominateBB to have a
+      // divergent terminator and to dominate BB and every predecessor with an
+      // undefined incoming value. Constants are available everywhere and don't
+      // require these checks.
+      if (!isa<Constant>(UniqueDefinedIncoming) &&
+          (UA.isUniformTerminator(DominateBB->getTerminator()) ||
+           !DT->dominates(DominateBB, &BB) ||
+           any_of(Undefs, [&](BasicBlock *UD) {
+             return !DT->dominates(DominateBB, UD);
+           })))
+        continue;
+
+      PHI.replaceAllUsesWith(UniqueDefinedIncoming);
+      ToBeDeleted.push_back(&PHI);
+      Changed = true;
     }
   }
 



More information about the llvm-commits mailing list