[llvm-branch-commits] [llvm] CodeGen: Fix stale live range for undef PHI sources on split edges (PR #228057)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Oct 1 05:16:30 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Matt Arsenault (arsenm)

<details>
<summary>Changes</summary>

SplitCriticalEdge collects the PHI sources coming from the new block so
the trimming loop below does not undo the segment just added for them.
An undef operand gets no segment, but was still added to the set, so a
register that is only an undef PHI operand on the split edge kept the
stale extension of its live range through the new block.

Co-Authored-By: Claude Opus 5 <noreply@<!-- -->anthropic.com>

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


2 Files Affected:

- (modified) llvm/lib/CodeGen/MachineBasicBlock.cpp (+1-1) 
- (added) llvm/test/CodeGen/AMDGPU/phi-elimination-split-critical-edge-undef-phi-source.mir (+36) 


``````````diff
diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp
index 4cd6afed26405..108d5fe842ca2 100644
--- a/llvm/lib/CodeGen/MachineBasicBlock.cpp
+++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp
@@ -1345,9 +1345,9 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(
         if (I->getOperand(ni+1).getMBB() == NMBB) {
           MachineOperand &MO = I->getOperand(ni);
           Register Reg = MO.getReg();
-          PHISrcRegs.insert(Reg);
           if (MO.isUndef())
             continue;
+          PHISrcRegs.insert(Reg);
 
           LiveInterval &LI = LIS->getInterval(Reg);
           VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
diff --git a/llvm/test/CodeGen/AMDGPU/phi-elimination-split-critical-edge-undef-phi-source.mir b/llvm/test/CodeGen/AMDGPU/phi-elimination-split-critical-edge-undef-phi-source.mir
new file mode 100644
index 0000000000000..c9c29eb7b875c
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/phi-elimination-split-critical-edge-undef-phi-source.mir
@@ -0,0 +1,36 @@
+# RUN: llc -mtriple=amdgpu9.00 -passes='require<live-intervals>,phi-node-elimination,print<live-intervals>' -filetype=null %s 2>&1 | FileCheck %s
+
+# %0 is only an undef PHI operand on the critical edge from %bb.1 to
+# %bb.2, so splitting the edge must not leave %0 live through the new
+# block.
+
+# CHECK: ********** INTERVALS **********
+# CHECK-NEXT: %0 [16r,[[SPLIT:[0-9]+]]B:0)
+# CHECK: [[SPLIT]]B{{[[:space:]]+}}bb.5:
+
+---
+name: split_critical_edge_undef_phi_source
+tracksRegLiveness: true
+body: |
+  bb.0:
+    %0:vgpr_32 = V_MOV_B32_e32 0, implicit $exec
+    %1:vgpr_32 = V_MOV_B32_e32 1, implicit $exec
+    $scc = IMPLICIT_DEF
+    S_CBRANCH_SCC1 %bb.1, implicit $scc
+    S_BRANCH %bb.4
+
+  bb.1:
+    $scc = IMPLICIT_DEF
+    S_CBRANCH_SCC1 %bb.3, implicit $scc
+    S_BRANCH %bb.2
+
+  bb.2:
+    %2:vgpr_32 = PHI undef %0, %bb.1, %1, %bb.4
+    S_ENDPGM 0, implicit %2
+
+  bb.3:
+    S_ENDPGM 0, implicit %0
+
+  bb.4:
+    S_BRANCH %bb.2
+...

``````````

</details>


https://github.com/llvm/llvm-project/pull/228057


More information about the llvm-branch-commits mailing list