Skip to content

[DebugInfo][LoopStrengthReduce] Salvage the debug value of the dead cmp instruction #147241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Apochens
Copy link
Contributor

@Apochens Apochens commented Jul 7, 2025

Fix #147238

@llvmbot
Copy link
Member

llvmbot commented Jul 7, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Shan Huang (Apochens)

Changes

Fix #147238


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp (+3-1)
  • (modified) llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll (+9)
diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index 786e4516ace05..7b631dbb16571 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -2559,8 +2559,10 @@ ICmpInst *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse) {
   Instruction *Cmp = cast<Instruction>(Sel->getOperand(0));
   Cond->eraseFromParent();
   Sel->eraseFromParent();
-  if (Cmp->use_empty())
+  if (Cmp->use_empty()) {
+    salvageDebugInfo(*Cmp);
     Cmp->eraseFromParent();
+  }
   return NewCond;
 }
 
diff --git a/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll b/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll
index d2d5550c3d68f..eabcb0e1cb81c 100644
--- a/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll
+++ b/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll
@@ -1,7 +1,16 @@
 ; RUN: opt < %s -loop-reduce -S 2>&1 | FileCheck %s
+
 ;; This test case checks that whether the new icmp instruction preserves
 ;; the debug location of the original instruction for %exitcond
+
+;; This test case also checks that the debug value of the dead icmp
+;; instruction is salvaged.
+
+; CHECK:   #dbg_value(i32 %n, ![[META1:[0-9]+]], !DIExpression(DW_OP_lit0, DW_OP_eq, DW_OP_stack_value), ![[META2:[0-9]+]])
 ; CHECK: icmp uge i32 %indvar.next, %n, !dbg ![[DBGLOC:[0-9]+]]
+
+; CHECK: ![[META1]] = !DILocalVariable(name: "1",
+; CHECK: ![[META2]] = !DILocation(line: 1, column: 1,
 ; CHECK: ![[DBGLOC]] = !DILocation(line: 6, column: 1, scope
 
 ; ModuleID = 'simplified-dbg.bc'

@llvmbot
Copy link
Member

llvmbot commented Jul 7, 2025

@llvm/pr-subscribers-debuginfo

Author: Shan Huang (Apochens)

Changes

Fix #147238


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp (+3-1)
  • (modified) llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll (+9)
diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index 786e4516ace05..7b631dbb16571 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -2559,8 +2559,10 @@ ICmpInst *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse) {
   Instruction *Cmp = cast<Instruction>(Sel->getOperand(0));
   Cond->eraseFromParent();
   Sel->eraseFromParent();
-  if (Cmp->use_empty())
+  if (Cmp->use_empty()) {
+    salvageDebugInfo(*Cmp);
     Cmp->eraseFromParent();
+  }
   return NewCond;
 }
 
diff --git a/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll b/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll
index d2d5550c3d68f..eabcb0e1cb81c 100644
--- a/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll
+++ b/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll
@@ -1,7 +1,16 @@
 ; RUN: opt < %s -loop-reduce -S 2>&1 | FileCheck %s
+
 ;; This test case checks that whether the new icmp instruction preserves
 ;; the debug location of the original instruction for %exitcond
+
+;; This test case also checks that the debug value of the dead icmp
+;; instruction is salvaged.
+
+; CHECK:   #dbg_value(i32 %n, ![[META1:[0-9]+]], !DIExpression(DW_OP_lit0, DW_OP_eq, DW_OP_stack_value), ![[META2:[0-9]+]])
 ; CHECK: icmp uge i32 %indvar.next, %n, !dbg ![[DBGLOC:[0-9]+]]
+
+; CHECK: ![[META1]] = !DILocalVariable(name: "1",
+; CHECK: ![[META2]] = !DILocation(line: 1, column: 1,
 ; CHECK: ![[DBGLOC]] = !DILocation(line: 6, column: 1, scope
 
 ; ModuleID = 'simplified-dbg.bc'

Copy link
Contributor

@SLTozer SLTozer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one small nit, thanks for fixing this!

;; This test case also checks that the debug value of the dead icmp
;; instruction is salvaged.

; CHECK: #dbg_value(i32 %n, ![[META1:[0-9]+]], !DIExpression(DW_OP_lit0, DW_OP_eq, DW_OP_stack_value), ![[META2:[0-9]+]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for legibility's sake, add some CHECK-LABELs for the basic blocks here? Just to make the structure of the output slightly clearer.

Suggested change
; CHECK: #dbg_value(i32 %n, ![[META1:[0-9]+]], !DIExpression(DW_OP_lit0, DW_OP_eq, DW_OP_stack_value), ![[META2:[0-9]+]])
; CHECK-LABEL: bb.nph:
; CHECK: #dbg_value(i32 %n, ![[META1:[0-9]+]], !DIExpression(DW_OP_lit0, DW_OP_eq, DW_OP_stack_value), ![[META2:[0-9]+]])
; CHECK-LABEL: bb:

Copy link
Contributor

@SLTozer SLTozer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DebugInfo][LoopStrengthReduce] Delete the cmp instruction without salvaging its debug value
3 participants