Skip to content

[DebugInfo][TailCallElim] Drop the debug location of AccRecInstrNew #95742

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

Merged

Conversation

Apochens
Copy link
Contributor

Fix #95731 .

@llvmbot
Copy link
Member

llvmbot commented Jun 17, 2024

@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-llvm-transforms

Author: Shan Huang (Apochens)

Changes

Fix #95731 .


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp (+2)
  • (added) llvm/test/Transforms/TailCallElim/dropping_debugloc_acc_rec_inst_rnew.ll (+109)
diff --git a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
index bc4b6de2f07f1..f413e4e1c15ac 100644
--- a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
@@ -779,6 +779,7 @@ void TailRecursionEliminator::cleanupAndFinalize() {
           AccRecInstrNew->setOperand(AccRecInstr->getOperand(0) == AccPN,
                                      RI->getOperand(0));
           AccRecInstrNew->insertBefore(RI);
+          AccRecInstrNew->dropLocation();
           RI->setOperand(0, AccRecInstrNew);
         }
       }
@@ -807,6 +808,7 @@ void TailRecursionEliminator::cleanupAndFinalize() {
           AccRecInstrNew->setOperand(AccRecInstr->getOperand(0) == AccPN,
                                      SI->getFalseValue());
           AccRecInstrNew->insertBefore(SI);
+          AccRecInstrNew->dropLocation();
           SI->setFalseValue(AccRecInstrNew);
         }
       }
diff --git a/llvm/test/Transforms/TailCallElim/dropping_debugloc_acc_rec_inst_rnew.ll b/llvm/test/Transforms/TailCallElim/dropping_debugloc_acc_rec_inst_rnew.ll
new file mode 100644
index 0000000000000..4028a472daba9
--- /dev/null
+++ b/llvm/test/Transforms/TailCallElim/dropping_debugloc_acc_rec_inst_rnew.ll
@@ -0,0 +1,109 @@
+; RUN: opt -S -passes=tailcallelim < %s | FileCheck %s
+
+; Check that TailCallElim's cleanupAndFinalize drops the debug location of `AccRecInstrNew`,
+; because it is cloned from `AccRecInstr` (`%accumulate*` in the following IR) with the 
+; debug location and inserted into the block of a different branch. 
+; @test6_multiple_returns tests that when RetSelects.empty() is false (no select instruction is inserted)
+; @test7_multiple_accumulators tests it when RetSelects.empty() is true
+
+define i32 @test6_multiple_returns(i32 %x, i32 %y) local_unnamed_addr !dbg !5 {
+; CHECK-LABEL: define i32 @test6_multiple_returns(
+; CHECK:  case0:
+; CHECK:    [[ACCUMULATOR_RET_TR2:%.*]] = add i32 %accumulator.tr, %helper{{$}}
+; CHECK:  case99:
+; CHECK:     [[ACCUMULATOR_RET_TR:%.*]] = add i32 %accumulator.tr, 18{{$}}
+; CHECK:  default:
+;
+entry:
+  switch i32 %x, label %default [
+    i32 0, label %case0
+    i32 99, label %case99
+  ], !dbg !8
+
+case0:                                            ; preds = %entry
+  %helper = call i32 @test6_helper(), !dbg !9
+  ret i32 %helper, !dbg !10
+
+case99:                                           ; preds = %entry
+  %sub1 = add i32 %x, -1, !dbg !11
+  %recurse1 = call i32 @test6_multiple_returns(i32 %sub1, i32 %y), !dbg !12
+  ret i32 18, !dbg !13
+
+default:                                          ; preds = %entry
+  %sub2 = add i32 %x, -1, !dbg !14
+  %recurse2 = call i32 @test6_multiple_returns(i32 %sub2, i32 %y), !dbg !15
+  %accumulate = add i32 %recurse2, %y, !dbg !16
+  ret i32 %accumulate, !dbg !17
+}
+
+declare i32 @test6_helper()
+
+define i32 @test7_multiple_accumulators(i32 %a) local_unnamed_addr !dbg !18 {
+; CHECK-LABEL: define i32 @test7_multiple_accumulators(
+; CHECK:  if.end3:
+; CHECK:    [[ACCUMULATOR_RET_TR:%.*]] = add nsw i32 %accumulator.tr, [[ACCUMULATE2:.*]]{{$}}
+; CHECK:  return:
+; CHECK:    [[ACCUMULATOR_RET_TR1:%.*]] = add nsw i32 %accumulator.tr, 0{{$}}
+;
+entry:
+  %tobool = icmp eq i32 %a, 0, !dbg !19
+  br i1 %tobool, label %return, label %if.end, !dbg !20
+
+if.end:                                           ; preds = %entry
+  %and = and i32 %a, 1, !dbg !21
+  %tobool1 = icmp eq i32 %and, 0, !dbg !22
+  %sub = add nsw i32 %a, -1, !dbg !23
+  br i1 %tobool1, label %if.end3, label %if.then2, !dbg !24
+
+if.then2:                                         ; preds = %if.end
+  %recurse1 = tail call i32 @test7_multiple_accumulators(i32 %sub), !dbg !25
+  %accumulate1 = add nsw i32 %recurse1, 1, !dbg !26
+  br label %return, !dbg !27
+
+if.end3:                                          ; preds = %if.end
+  %recurse2 = tail call i32 @test7_multiple_accumulators(i32 %sub), !dbg !28
+  %accumulate2 = mul nsw i32 %recurse2, 2, !dbg !29
+  br label %return, !dbg !30
+
+return:                                           ; preds = %if.end3, %if.then2, %entry
+  %retval.0 = phi i32 [ %accumulate1, %if.then2 ], [ %accumulate2, %if.end3 ], [ 0, %entry ], !dbg !31
+  ret i32 %retval.0, !dbg !32
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "all.ll", directory: "/")
+!2 = !{i32 24}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "test6_multiple_returns", linkageName: "test6_multiple_returns", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!6 = !DISubroutineType(types: !7)
+!7 = !{}
+!8 = !DILocation(line: 1, column: 1, scope: !5)
+!9 = !DILocation(line: 2, column: 1, scope: !5)
+!10 = !DILocation(line: 3, column: 1, scope: !5)
+!11 = !DILocation(line: 4, column: 1, scope: !5)
+!12 = !DILocation(line: 5, column: 1, scope: !5)
+!13 = !DILocation(line: 6, column: 1, scope: !5)
+!14 = !DILocation(line: 7, column: 1, scope: !5)
+!15 = !DILocation(line: 8, column: 1, scope: !5)
+!16 = !DILocation(line: 9, column: 1, scope: !5)
+!17 = !DILocation(line: 10, column: 1, scope: !5)
+!18 = distinct !DISubprogram(name: "test7_multiple_accumulators", linkageName: "test7_multiple_accumulators", scope: null, file: !1, line: 11, type: !6, scopeLine: 11, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!19 = !DILocation(line: 11, column: 1, scope: !18)
+!20 = !DILocation(line: 12, column: 1, scope: !18)
+!21 = !DILocation(line: 13, column: 1, scope: !18)
+!22 = !DILocation(line: 14, column: 1, scope: !18)
+!23 = !DILocation(line: 15, column: 1, scope: !18)
+!24 = !DILocation(line: 16, column: 1, scope: !18)
+!25 = !DILocation(line: 17, column: 1, scope: !18)
+!26 = !DILocation(line: 18, column: 1, scope: !18)
+!27 = !DILocation(line: 19, column: 1, scope: !18)
+!28 = !DILocation(line: 20, column: 1, scope: !18)
+!29 = !DILocation(line: 21, column: 1, scope: !18)
+!30 = !DILocation(line: 22, column: 1, scope: !18)
+!31 = !DILocation(line: 23, column: 1, scope: !18)
+!32 = !DILocation(line: 24, column: 1, scope: !18)

Copy link
Contributor

@OCHyams OCHyams left a comment

Choose a reason for hiding this comment

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

LGTM - looks like this patch prevents misleading bonus coverage

Thanks!

@Apochens Apochens merged commit ace069d into llvm:main Jun 20, 2024
8 of 10 checks passed
@Apochens Apochens deleted the #95731_tailcallelim_accrecinstrnew_drop_location branch June 21, 2024 04:55
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
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][TailCallElim] Cloned AccRecInstrNew should drop its debugloc when being inserted into other blocks
3 participants