Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Reassociate] Apply Debugloc to instrs produced when optimizing add #134676

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 2 commits into from
Apr 8, 2025

Conversation

SLTozer
Copy link
Contributor

@SLTozer SLTozer commented Apr 7, 2025

Currently in Reassociate we may create a set of new instructions when optimizing an add, but we do not set DebugLocs on the new instructions; this patch propagates the add's DebugLoc to the new instructions.

Found using #107279.

Currently in Reassociate we may create a set of new instructions when
optimizing an `add`, but we do not set DebugLocs on the new instructions;
this patch propagates the add's DebugLoc to the new instructions.
@llvmbot
Copy link
Member

llvmbot commented Apr 7, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-debuginfo

Author: Stephen Tozer (SLTozer)

Changes

Currently in Reassociate we may create a set of new instructions when optimizing an add, but we do not set DebugLocs on the new instructions; this patch propagates the add's DebugLoc to the new instructions.

Found using #107279.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/Reassociate.cpp (+7-4)
  • (added) llvm/test/Transforms/Reassociate/debugloc-reass-add.ll (+59)
diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index f9aef064641d8..0bfce13b07f1c 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -1086,13 +1086,15 @@ static unsigned FindInOperandList(const SmallVectorImpl<ValueEntry> &Ops,
 
 /// Emit a tree of add instructions, summing Ops together
 /// and returning the result.  Insert the tree before I.
-static Value *EmitAddTreeOfValues(BasicBlock::iterator It,
+static Value *EmitAddTreeOfValues(Instruction *I,
                                   SmallVectorImpl<WeakTrackingVH> &Ops) {
   if (Ops.size() == 1) return Ops.back();
 
   Value *V1 = Ops.pop_back_val();
-  Value *V2 = EmitAddTreeOfValues(It, Ops);
-  return CreateAdd(V2, V1, "reass.add", It, &*It);
+  Value *V2 = EmitAddTreeOfValues(I, Ops);
+  auto *NewAdd = CreateAdd(V2, V1, "reass.add", I->getIterator(), I);
+  NewAdd->setDebugLoc(I->getDebugLoc());
+  return NewAdd;
 }
 
 /// If V is an expression tree that is a multiplication sequence,
@@ -1682,7 +1684,7 @@ Value *ReassociatePass::OptimizeAdd(Instruction *I,
     DummyInst->deleteValue();
 
     unsigned NumAddedValues = NewMulOps.size();
-    Value *V = EmitAddTreeOfValues(I->getIterator(), NewMulOps);
+    Value *V = EmitAddTreeOfValues(I, NewMulOps);
 
     // Now that we have inserted the add tree, optimize it. This allows us to
     // handle cases that require multiple factoring steps, such as this:
@@ -1694,6 +1696,7 @@ Value *ReassociatePass::OptimizeAdd(Instruction *I,
 
     // Create the multiply.
     Instruction *V2 = CreateMul(V, MaxOccVal, "reass.mul", I->getIterator(), I);
+    V2->setDebugLoc(I->getDebugLoc());
 
     // Rerun associate on the multiply in case the inner expression turned into
     // a multiply.  We want to make sure that we keep things in canonical form.
diff --git a/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll b/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll
new file mode 100644
index 0000000000000..7fc94fdc13eca
--- /dev/null
+++ b/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll
@@ -0,0 +1,59 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -p=reassociate -S < %s | FileCheck %s
+
+;; Tests that when we reassociate %add93, we apply its debug location to the new
+;; instructions.
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @foo(i32 %0) {
+; CHECK-LABEL: define void @foo(
+; CHECK-SAME: i32 [[TMP0:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br label %[[FOR_COND23:.*]]
+; CHECK:       [[FOR_COND23]]:
+; CHECK-NEXT:    [[SUB59:%.*]] = sub i32 0, 0
+; CHECK-NEXT:    [[MUL68:%.*]] = mul i32 0, [[TMP0]]
+; CHECK-NEXT:    [[REASS_ADD:%.*]] = add i32 [[MUL68]], [[TMP0]], !dbg [[DBG3:![0-9]+]]
+; CHECK-NEXT:    [[REASS_MUL1:%.*]] = mul i32 [[REASS_ADD]], [[SUB59]], !dbg [[DBG3]]
+; CHECK-NEXT:    [[REASS_MUL:%.*]] = add i32 [[REASS_MUL1]], 1, !dbg [[DBG3]]
+; CHECK-NEXT:    [[CONV95:%.*]] = trunc i32 [[REASS_MUL]] to i16
+; CHECK-NEXT:    store i16 [[CONV95]], ptr null, align 2
+; CHECK-NEXT:    br label %[[FOR_COND23]]
+;
+entry:
+  br label %for.cond23
+
+for.cond23:                                       ; preds = %for.cond23, %entry
+  %sub59 = sub i32 0, 0
+  %mul62 = mul i32 %sub59, %0
+  %mul68 = mul i32 %mul62, 0
+  %mul77 = mul i32 %sub59, %0
+  %add84 = or i32 %mul68, %mul77
+  %add93 = add i32 %add84, 1, !dbg !4
+  %conv95 = trunc i32 %add93 to i16
+  store i16 %conv95, ptr null, align 2
+  br label %for.cond23
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 20.0.0git")
+!1 = !DIFile(filename: "test.c", directory: "/tmp")
+!2 = !{}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !DILocation(line: 15, column: 50, scope: !5)
+!5 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 14, type: !6, scopeLine: 14, unit: !0, retainedNodes: !2)
+!6 = distinct !DISubroutineType(types: !7)
+!7 = !{null}
+;.
+; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
+; CHECK: [[META1]] = !DIFile(filename: "test.c", directory: {{.*}})
+; CHECK: [[DBG3]] = !DILocation(line: 15, column: 50, scope: [[META4:![0-9]+]])
+; CHECK: [[META4]] = distinct !DISubprogram(name: "foo", scope: [[META1]], file: [[META1]], line: 14, type: [[META5:![0-9]+]], scopeLine: 14, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META7:![0-9]+]])
+; CHECK: [[META5]] = distinct !DISubroutineType(types: [[META6:![0-9]+]])
+; CHECK: [[META6]] = !{null}
+; CHECK: [[META7]] = !{}
+;.

Copy link
Member

@jryans jryans left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks! 😄

@SLTozer SLTozer merged commit 84fde79 into llvm:main Apr 8, 2025
5 of 9 checks passed
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.

3 participants