[llvm] [AMDGPU] Ignore debug uses when folding REG_SEQUENCE copies (PR #229299)
Yongqiang Tian via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 6 03:07:07 PDT 2026
https://github.com/yqtian-se updated https://github.com/llvm/llvm-project/pull/229299
>From b1efad1e1087a31bb23d3c7b5bf416be704b65b5 Mon Sep 17 00:00:00 2001
From: Yongqiang Tian <yqtian668 at gmail.com>
Date: Tue, 6 Oct 2026 03:24:01 +0000
Subject: [PATCH 1/2] [AMDGPU] Ignore debug uses when folding REG_SEQUENCE
copies
SIFixSGPRCopies only folds an SGPR REG_SEQUENCE into its sole VGPR copy. The current single-use check counts debug uses, so a DBG_VALUE prevents the fold and makes -g change generated code and register usage.
Use non-debug use queries to find the copy, then replace the REG_SEQUENCE result with the copy destination so debug users follow the new definition. Add MIR coverage for equivalent functions with and without a debug use.
Fixes #229046
Assisted-by: OpenAI Codex 5.6 Sol
Assisted-by: Claude (review)
---
llvm/lib/Target/AMDGPU/SIFixSGPRCopies.cpp | 7 +--
.../AMDGPU/fix-sgpr-copies-debug-use.mir | 63 +++++++++++++++++++
2 files changed, 66 insertions(+), 4 deletions(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/fix-sgpr-copies-debug-use.mir
diff --git a/llvm/lib/Target/AMDGPU/SIFixSGPRCopies.cpp b/llvm/lib/Target/AMDGPU/SIFixSGPRCopies.cpp
index e4ff116f3fcfb..4651a25363e90 100644
--- a/llvm/lib/Target/AMDGPU/SIFixSGPRCopies.cpp
+++ b/llvm/lib/Target/AMDGPU/SIFixSGPRCopies.cpp
@@ -291,10 +291,10 @@ static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI,
if (!TRI->isSGPRClass(MRI.getRegClass(DstReg)))
return false;
- if (!MRI.hasOneUse(DstReg))
+ if (!MRI.hasOneNonDBGUse(DstReg))
return false;
- MachineInstr &CopyUse = *MRI.use_instr_begin(DstReg);
+ MachineInstr &CopyUse = *MRI.use_instr_nodbg_begin(DstReg);
if (!CopyUse.isCopy())
return false;
@@ -316,7 +316,7 @@ static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI,
if (SubReg != AMDGPU::NoSubRegister)
return false;
- MRI.setRegClass(DstReg, DstRC);
+ MRI.replaceRegWith(DstReg, CopyUse.getOperand(0).getReg());
// SGPRx = ...
// SGPRy = REG_SEQUENCE SGPRx, sub0 ...
@@ -326,7 +326,6 @@ static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI,
// VGPRx = COPY SGPRx
// VGPRz = REG_SEQUENCE VGPRx, sub0
- MI.getOperand(0).setReg(CopyUse.getOperand(0).getReg());
bool IsAGPR = TRI->isAGPRClass(DstRC);
for (unsigned I = 1, N = MI.getNumOperands(); I != N; I += 2) {
diff --git a/llvm/test/CodeGen/AMDGPU/fix-sgpr-copies-debug-use.mir b/llvm/test/CodeGen/AMDGPU/fix-sgpr-copies-debug-use.mir
new file mode 100644
index 0000000000000..e946a7029da6b
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/fix-sgpr-copies-debug-use.mir
@@ -0,0 +1,63 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=amdgpu6.00 -run-pass=si-fix-sgpr-copies -verify-machineinstrs -o - %s | FileCheck %s
+
+--- |
+ define void @reg_sequence_copy_without_debug_use() {
+ ret void
+ }
+
+ define void @reg_sequence_copy_with_debug_use() !dbg !3 {
+ ret void
+ }
+
+ !llvm.dbg.cu = !{!0}
+ !llvm.module.flags = !{!2}
+
+ !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+ !1 = !DIFile(filename: "fix-sgpr-copies-debug-use.mir", directory: "/")
+ !2 = !{i32 2, !"Debug Info Version", i32 3}
+ !3 = distinct !DISubprogram(name: "reg_sequence_copy_with_debug_use", linkageName: "reg_sequence_copy_with_debug_use", scope: null, file: !1, line: 1, type: !4, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !5)
+ !4 = !DISubroutineType(types: !5)
+ !5 = !{}
+ !6 = !DIBasicType(name: "ty128", size: 128, encoding: DW_ATE_unsigned)
+ !7 = !DILocalVariable(name: "value", scope: !3, file: !1, line: 1, type: !6)
+ !8 = !DILocation(line: 1, column: 1, scope: !3)
+...
+
+---
+name: reg_sequence_copy_without_debug_use
+body: |
+ bb.0:
+ ; CHECK-LABEL: name: reg_sequence_copy_without_debug_use
+ ; CHECK: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:vgpr_32 = COPY [[S_MOV_B32_]]
+ ; CHECK-NEXT: [[COPY1:%[0-9]+]]:vgpr_32 = COPY [[S_MOV_B32_]]
+ ; CHECK-NEXT: [[COPY2:%[0-9]+]]:vgpr_32 = COPY [[S_MOV_B32_]]
+ ; CHECK-NEXT: [[COPY3:%[0-9]+]]:vgpr_32 = COPY [[S_MOV_B32_]]
+ ; CHECK-NEXT: [[REG_SEQUENCE:%[0-9]+]]:vreg_128 = REG_SEQUENCE [[COPY]], %subreg.sub0, [[COPY1]], %subreg.sub1, [[COPY2]], %subreg.sub2, [[COPY3]], %subreg.sub3
+ ; CHECK-NEXT: S_ENDPGM 0, implicit [[REG_SEQUENCE]]
+ %0:sreg_32 = S_MOV_B32 0
+ %1:sgpr_128 = REG_SEQUENCE %0, %subreg.sub0, %0, %subreg.sub1, %0, %subreg.sub2, %0, %subreg.sub3
+ %2:vreg_128 = COPY %1
+ S_ENDPGM 0, implicit %2
+...
+
+---
+name: reg_sequence_copy_with_debug_use
+body: |
+ bb.0:
+ ; CHECK-LABEL: name: reg_sequence_copy_with_debug_use
+ ; CHECK: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:vgpr_32 = COPY [[S_MOV_B32_]]
+ ; CHECK-NEXT: [[COPY1:%[0-9]+]]:vgpr_32 = COPY [[S_MOV_B32_]]
+ ; CHECK-NEXT: [[COPY2:%[0-9]+]]:vgpr_32 = COPY [[S_MOV_B32_]]
+ ; CHECK-NEXT: [[COPY3:%[0-9]+]]:vgpr_32 = COPY [[S_MOV_B32_]]
+ ; CHECK-NEXT: [[REG_SEQUENCE:%[0-9]+]]:vreg_128 = REG_SEQUENCE [[COPY]], %subreg.sub0, [[COPY1]], %subreg.sub1, [[COPY2]], %subreg.sub2, [[COPY3]], %subreg.sub3
+ ; CHECK-NEXT: DBG_VALUE [[REG_SEQUENCE]], $noreg, !6, !DIExpression(), debug-location !DILocation(line: 1, column: 1, scope: !3)
+ ; CHECK-NEXT: S_ENDPGM 0, implicit [[REG_SEQUENCE]]
+ %0:sreg_32 = S_MOV_B32 0
+ %1:sgpr_128 = REG_SEQUENCE %0, %subreg.sub0, %0, %subreg.sub1, %0, %subreg.sub2, %0, %subreg.sub3
+ DBG_VALUE %1, $noreg, !7, !DIExpression(), debug-location !8
+ %2:vreg_128 = COPY %1
+ S_ENDPGM 0, implicit %2
+...
>From ea6e16ad72d3bb7d55b7fb506b9fc0a9fcc3276a Mon Sep 17 00:00:00 2001
From: Yongqiang Tian <yqtian668 at gmail.com>
Date: Tue, 6 Oct 2026 10:06:40 +0000
Subject: [PATCH 2/2] [AMDGPU] Preserve the folded REG_SEQUENCE register class
Keep the existing register class update before replacing the REG_SEQUENCE result. This preserves the register-class state while still redirecting debug uses to the copy destination.
Remove -verify-machineinstrs from the MIR test, as requested in review.
Assisted-by: OpenAI Codex 5.6 Sol
---
llvm/lib/Target/AMDGPU/SIFixSGPRCopies.cpp | 1 +
llvm/test/CodeGen/AMDGPU/fix-sgpr-copies-debug-use.mir | 2 +-
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Target/AMDGPU/SIFixSGPRCopies.cpp b/llvm/lib/Target/AMDGPU/SIFixSGPRCopies.cpp
index 4651a25363e90..c97925ec59437 100644
--- a/llvm/lib/Target/AMDGPU/SIFixSGPRCopies.cpp
+++ b/llvm/lib/Target/AMDGPU/SIFixSGPRCopies.cpp
@@ -316,6 +316,7 @@ static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI,
if (SubReg != AMDGPU::NoSubRegister)
return false;
+ MRI.setRegClass(DstReg, DstRC);
MRI.replaceRegWith(DstReg, CopyUse.getOperand(0).getReg());
// SGPRx = ...
diff --git a/llvm/test/CodeGen/AMDGPU/fix-sgpr-copies-debug-use.mir b/llvm/test/CodeGen/AMDGPU/fix-sgpr-copies-debug-use.mir
index e946a7029da6b..55d3fc480872f 100644
--- a/llvm/test/CodeGen/AMDGPU/fix-sgpr-copies-debug-use.mir
+++ b/llvm/test/CodeGen/AMDGPU/fix-sgpr-copies-debug-use.mir
@@ -1,5 +1,5 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
-# RUN: llc -mtriple=amdgpu6.00 -run-pass=si-fix-sgpr-copies -verify-machineinstrs -o - %s | FileCheck %s
+# RUN: llc -mtriple=amdgpu6.00 -run-pass=si-fix-sgpr-copies -o - %s | FileCheck %s
--- |
define void @reg_sequence_copy_without_debug_use() {
More information about the llvm-commits
mailing list