[llvm] [DAGCombiner] Loop unfreeze step in visitFREEZE freezeOtherUses (PR #200120)
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Thu May 28 07:36:39 PDT 2026
https://github.com/RKSimon updated https://github.com/llvm/llvm-project/pull/200120
>From 974b79392dad06b49b786834f34ec2f5579eda8c Mon Sep 17 00:00:00 2001
From: Li Jinpei <jinpli at amd.com>
Date: Thu, 28 May 2026 19:49:22 +0800
Subject: [PATCH] [DAGCombiner] Loop unfreeze step in visitFREEZE
freezeOtherUses (#198094)
For a freeze chain of depth 3:
t1 = freeze(t0)
t2 = freeze(t1)
t3 = freeze(t2)
After a single ReplaceAllUsesOfValueWith(t1, t0):
t1 = freeze(t0)
t2 = freeze(t0) -> CSE'd to t1
t3 = freeze(t1) <-- newly created use that the same RAUW doesn't see
CSE prepends the new freeze(t1) to t1's use-list mid-iteration, which
RAUW deliberately skips. The next RAUW step then trips
"Node is not in map!" in RemoveNodeFromCSEMaps.
Loop the first RAUW until N's use-list is drained, so all such
prepended users are folded before the second RAUW.
Fixes #198094.
---
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 6 ++-
.../AMDGPU/freeze-other-uses-issue-198094.ll | 37 +++++++++++++++++++
2 files changed, 41 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/freeze-other-uses-issue-198094.ll
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 2a9ecebeb1508..778153496ca71 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -17970,8 +17970,10 @@ SDValue DAGCombiner::visitFREEZE(SDNode *N) {
// If we have frozen and unfrozen users of N0, update so everything uses N.
if (!N0.isUndef() && !N0.hasOneUse()) {
SDValue FrozenN0(N, 0);
- // Unfreeze all uses of N to avoid double deleting N from the CSE map.
- DAG.ReplaceAllUsesOfValueWith(FrozenN0, N0);
+ // Unfreeze all (possibly nested) uses of N to avoid double deleting N from
+ // the CSE map.
+ while (!N->use_empty())
+ DAG.ReplaceAllUsesOfValueWith(FrozenN0, N0);
DAG.ReplaceAllUsesOfValueWith(N0, FrozenN0);
// ReplaceAllUsesOfValueWith will have also updated the use in N, thus
// creating a cycle in a DAG. Let's undo that by mutating the freeze.
diff --git a/llvm/test/CodeGen/AMDGPU/freeze-other-uses-issue-198094.ll b/llvm/test/CodeGen/AMDGPU/freeze-other-uses-issue-198094.ll
new file mode 100644
index 0000000000000..4ceaea857b020
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/freeze-other-uses-issue-198094.ll
@@ -0,0 +1,37 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=amdgcn < %s | FileCheck %s
+
+define void @repro(<6 x i32> %a, i32 %i) {
+; CHECK-LABEL: repro:
+; CHECK: ; %bb.0:
+; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; CHECK-NEXT: v_cmp_eq_u32_e32 vcc, 1, v6
+; CHECK-NEXT: v_cndmask_b32_e32 v7, v0, v1, vcc
+; CHECK-NEXT: v_cmp_eq_u32_e32 vcc, 2, v6
+; CHECK-NEXT: v_cndmask_b32_e32 v2, v7, v2, vcc
+; CHECK-NEXT: v_cmp_eq_u32_e32 vcc, 3, v6
+; CHECK-NEXT: v_cndmask_b32_e32 v2, v2, v3, vcc
+; CHECK-NEXT: v_cmp_eq_u32_e32 vcc, 4, v6
+; CHECK-NEXT: v_cndmask_b32_e32 v2, v2, v4, vcc
+; CHECK-NEXT: v_cmp_eq_u32_e32 vcc, 5, v6
+; CHECK-NEXT: v_cndmask_b32_e32 v2, v2, v5, vcc
+; CHECK-NEXT: v_xor_b32_e32 v1, v2, v1
+; CHECK-NEXT: v_xor_b32_e32 v0, v2, v0
+; CHECK-NEXT: v_cndmask_b32_e32 v0, v0, v1, vcc
+; CHECK-NEXT: v_cmp_ne_u32_e32 vcc, 0, v0
+; CHECK-NEXT: s_and_saveexec_b64 s[4:5], vcc
+; CHECK-NEXT: s_or_b64 exec, exec, s[4:5]
+; CHECK-NEXT: s_setpc_b64 s[30:31]
+ %r0 = shufflevector <6 x i32> %a, <6 x i32> poison, <6 x i32> <i32 0, i32 0, i32 poison, i32 0, i32 0, i32 1>
+ %l0 = extractelement <6 x i32> %a, i32 %i
+ %s = insertelement <6 x i32> poison, i32 %l0, i32 %i
+ %r2 = shufflevector <6 x i32> %s, <6 x i32> poison, <6 x i32> <i32 poison, i32 0, i32 0, i32 poison, i32 0, i32 0>
+ %d = xor <6 x i32> %r2, %r0
+ %l = extractelement <6 x i32> %d, i32 %i
+ %c = icmp ne i32 %l, 0
+ br i1 %c, label %t, label %r
+t:
+ switch i32 0, label %r []
+r:
+ ret void
+}
More information about the llvm-commits
mailing list