Skip to content

[DebugInfo][DivRemPairs] Fix missing debug location updates #96045

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
merged 1 commit into from
Jun 23, 2024

Conversation

Apochens
Copy link
Contributor

Fix #96014 .

preserving-debugloc-realrem.ll tests the debug location propagation under two scenarios – whether the operands are signed.

@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2024

@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-llvm-transforms

Author: Shan Huang (Apochens)

Changes

Fix #96014 .

preserving-debugloc-realrem.ll tests the debug location propagation under two scenarios – whether the operands are signed.


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

3 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/DivRemPairs.cpp (+5)
  • (added) llvm/test/Transforms/DivRemPairs/X86/preserving-debugloc-frx-fry-mul-sub.ll (+50)
  • (added) llvm/test/Transforms/DivRemPairs/X86/preserving-debugloc-realrem.ll (+56)
diff --git a/llvm/lib/Transforms/Scalar/DivRemPairs.cpp b/llvm/lib/Transforms/Scalar/DivRemPairs.cpp
index f7ada9fb8eb8a..d8aea1e810e9b 100644
--- a/llvm/lib/Transforms/Scalar/DivRemPairs.cpp
+++ b/llvm/lib/Transforms/Scalar/DivRemPairs.cpp
@@ -215,6 +215,7 @@ static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
       RemInst = RealRem;
       // And replace the original instruction with the new one.
       OrigRemInst->replaceAllUsesWith(RealRem);
+      RealRem->setDebugLoc(OrigRemInst->getDebugLoc());
       OrigRemInst->eraseFromParent();
       NumRecomposed++;
       // Note that we have left ((X / Y) * Y) around.
@@ -366,7 +367,9 @@ static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
       if (!DivDominates)
         DivInst->moveBefore(RemInst);
       Mul->insertAfter(RemInst);
+      Mul->setDebugLoc(RemInst->getDebugLoc());
       Sub->insertAfter(Mul);
+      Sub->setDebugLoc(RemInst->getDebugLoc());
 
       // If DivInst has the exact flag, remove it. Otherwise this optimization
       // may replace a well-defined value 'X % Y' with poison.
@@ -384,6 +387,7 @@ static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
       if (!isGuaranteedNotToBeUndef(X, nullptr, DivInst, &DT)) {
         auto *FrX =
             new FreezeInst(X, X->getName() + ".frozen", DivInst->getIterator());
+        FrX->setDebugLoc(DivInst->getDebugLoc());
         DivInst->setOperand(0, FrX);
         Sub->setOperand(0, FrX);
       }
@@ -392,6 +396,7 @@ static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
       if (!isGuaranteedNotToBeUndef(Y, nullptr, DivInst, &DT)) {
         auto *FrY =
             new FreezeInst(Y, Y->getName() + ".frozen", DivInst->getIterator());
+        FrY->setDebugLoc(DivInst->getDebugLoc());
         DivInst->setOperand(1, FrY);
         Mul->setOperand(1, FrY);
       }
diff --git a/llvm/test/Transforms/DivRemPairs/X86/preserving-debugloc-frx-fry-mul-sub.ll b/llvm/test/Transforms/DivRemPairs/X86/preserving-debugloc-frx-fry-mul-sub.ll
new file mode 100644
index 0000000000000..f0166acac128e
--- /dev/null
+++ b/llvm/test/Transforms/DivRemPairs/X86/preserving-debugloc-frx-fry-mul-sub.ll
@@ -0,0 +1,50 @@
+; RUN: opt -S -passes=div-rem-pairs -mtriple=x86_64-unknown-unknown < %s | FileCheck %s
+
+; Check that DivRemPairs's optimizeDivRem() correctly propagates debug locations
+; of `%div` and `%rem` to new freeze, mul and sub instructions.
+
+define i128 @dont_hoist_urem(i128 %a, i128 %b) !dbg !5 {
+; CHECK-LABEL: define i128 @dont_hoist_urem(
+; CHECK-SAME: i128 [[A:%.*]], i128 [[B:%.*]])
+; CHECK:       entry:
+; CHECK-NEXT:    [[A_FROZEN:%.*]] = freeze i128 [[A]], !dbg [[DBG8:![0-9]+]]
+; CHECK-NEXT:    [[B_FROZEN:%.*]] = freeze i128 [[B]], !dbg [[DBG8]]
+; CHECK:       if:
+; CHECK-NEXT:    [[TMP0:%.*]] = mul i128 [[DIV:%.*]], [[B_FROZEN]], !dbg [[DBG11:![0-9]+]]
+; CHECK-NEXT:    [[REM_DECOMPOSED:%.*]] = sub i128 [[A_FROZEN]], [[TMP0:%.*]], !dbg [[DBG11]]
+; CHECK:       end:
+entry:
+  %div = udiv i128 %a, %b, !dbg !8
+  %cmp = icmp eq i128 %div, 42, !dbg !9
+  br i1 %cmp, label %if, label %end, !dbg !10
+
+if:                                               ; preds = %entry
+  %rem = urem i128 %a, %b, !dbg !11
+  br label %end, !dbg !12
+
+end:                                              ; preds = %if, %entry
+  %ret = phi i128 [ %rem, %if ], [ 3, %entry ], !dbg !13
+  ret i128 %ret, !dbg !14
+}
+
+!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: "frx_fry_.preserve.ll", directory: "/")
+!2 = !{i32 7}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "dont_hoist_urem", linkageName: "dont_hoist_urem", 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)
+; CHECK: [[DBG8]] = !DILocation(line: 1,
+; CHECK: [[DBG11]] = !DILocation(line: 4,
diff --git a/llvm/test/Transforms/DivRemPairs/X86/preserving-debugloc-realrem.ll b/llvm/test/Transforms/DivRemPairs/X86/preserving-debugloc-realrem.ll
new file mode 100644
index 0000000000000..d74c8b9108ffa
--- /dev/null
+++ b/llvm/test/Transforms/DivRemPairs/X86/preserving-debugloc-realrem.ll
@@ -0,0 +1,56 @@
+; RUN: opt -S -passes=div-rem-pairs -mtriple=x86_64-unknown-unknown < %s | FileCheck %s
+
+; Check that DivRemPairs's optimizeDivRem() propagates the debug location of
+; replaced `%rem` to the new srem or urem instruction.
+; @decompose_illegal_srem_same_block tests it when the operands of `%rem` is signed.
+; @decompose_illegal_urem_same_block tests it when the operands of `%rem` is unsigned.
+
+define void @decompose_illegal_srem_same_block(i32 %a, i32 %b) !dbg !5 {
+; CHECK-LABEL: define void @decompose_illegal_srem_same_block(
+; CHECK:         %rem.recomposed = srem i32 [[A:%.*]], [[B:%.*]], !dbg [[DBG10:![0-9]+]]
+  %div = sdiv i32 %a, %b, !dbg !8
+  %t0 = mul i32 %div, %b, !dbg !9
+  %rem = sub i32 %a, %t0, !dbg !10
+  call void @foo(i32 %rem, i32 %div), !dbg !11
+  ret void, !dbg !12
+}
+
+define void @decompose_illegal_urem_same_block(i32 %a, i32 %b) !dbg !13 {
+; CHECK-LABEL: define void @decompose_illegal_urem_same_block(
+; CHECK:         %rem.recomposed = urem i32 [[A:%.*]], [[B:%.*]], !dbg [[DBG16:![0-9]+]]
+  %div = udiv i32 %a, %b, !dbg !14
+  %t0 = mul i32 %div, %b, !dbg !15
+  %rem = sub i32 %a, %t0, !dbg !16
+  call void @foo(i32 %rem, i32 %div), !dbg !17
+  ret void, !dbg !18
+}
+
+declare void @foo(i32, i32)
+
+!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: "realrem_preverse.ll", directory: "/")
+!2 = !{i32 10}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "decompose_illegal_srem_same_block", linkageName: "decompose_illegal_srem_same_block", 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 = distinct !DISubprogram(name: "decompose_illegal_urem_same_block", linkageName: "decompose_illegal_urem_same_block", scope: null, file: !1, line: 6, type: !6, scopeLine: 6, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!14 = !DILocation(line: 6, column: 1, scope: !13)
+!15 = !DILocation(line: 7, column: 1, scope: !13)
+!16 = !DILocation(line: 8, column: 1, scope: !13)
+!17 = !DILocation(line: 9, column: 1, scope: !13)
+!18 = !DILocation(line: 10, column: 1, scope: !13)
+
+; CHECK: [[DBG10]] = !DILocation(line: 3,
+; CHECK: [[DBG16]] = !DILocation(line: 8,
+

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!

@Apochens Apochens merged commit 3ae6755 into llvm:main Jun 23, 2024
10 checks passed
@Apochens Apochens deleted the #96014_divrempairs_debugloc_preserving branch June 25, 2024 08:17
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][DivRemPairs] Missing debug location updates
3 participants