Skip to content

[DebugInfo][Reassociate] Propagate source locs when factoring add->mul #134829

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
Apr 9, 2025

Conversation

SLTozer
Copy link
Contributor

@SLTozer SLTozer commented Apr 8, 2025

As part of reassociating add instructions, we may factorize some of the adds and produce a mul instruction; this patch propagates the source location of the reassociated tree of instructions to the new mul.

Found using #107279.

…to mul

As part of reassociating add instructions, we may factorize some of the
adds and produce a mul instruction; this patch propagates the source
location of the reassociated tree of instructions to the new mul.
@llvmbot
Copy link
Member

llvmbot commented Apr 8, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-debuginfo

Author: Stephen Tozer (SLTozer)

Changes

As part of reassociating add instructions, we may factorize some of the adds and produce a mul instruction; this patch propagates the source location of the reassociated tree of instructions to the new mul.

Found using #107279.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/Reassociate.cpp (+1)
  • (added) llvm/test/Transforms/Reassociate/debugloc-factoring-add.ll (+43)
diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index f9aef064641d8..0abb68c6a3964 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -1519,6 +1519,7 @@ Value *ReassociatePass::OptimizeAdd(Instruction *I,
       Constant *C = Ty->isIntOrIntVectorTy() ?
         ConstantInt::get(Ty, NumFound) : ConstantFP::get(Ty, NumFound);
       Instruction *Mul = CreateMul(TheOp, C, "factor", I->getIterator(), I);
+      Mul->setDebugLoc(I->getDebugLoc());
 
       // Now that we have inserted a multiply, optimize it. This allows us to
       // handle cases that require multiple factoring steps, such as this:
diff --git a/llvm/test/Transforms/Reassociate/debugloc-factoring-add.ll b/llvm/test/Transforms/Reassociate/debugloc-factoring-add.ll
new file mode 100644
index 0000000000000..9c507f998b5e7
--- /dev/null
+++ b/llvm/test/Transforms/Reassociate/debugloc-factoring-add.ll
@@ -0,0 +1,43 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -p=reassociate -S | FileCheck %s
+
+;; Ensure that when we factorize part of a tree of adds into a multiply, we
+;; propagate the debug loc of the tree to the mul.
+
+define fastcc void @ham(i32 %arg) !dbg !10 {
+; CHECK-LABEL: define fastcc void @ham(
+; CHECK-SAME: i32 [[ARG:%.*]]) !dbg [[DBG4:![0-9]+]] {
+; CHECK-NEXT:  [[BB:.*:]]
+; CHECK-NEXT:    [[FACTOR:%.*]] = mul i32 [[ARG]], 2, !dbg [[DBG7:![0-9]+]]
+; CHECK-NEXT:    [[ADD1:%.*]] = add i32 [[FACTOR]], 1, !dbg [[DBG7]]
+; CHECK-NEXT:    store i32 [[ADD1]], ptr null, align 4
+; CHECK-NEXT:    ret void
+;
+bb:
+  %add = add i32 %arg, 1
+  %add1 = add i32 %arg, %add, !dbg !4
+  store i32 %add1, ptr null, align 4
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 20.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !2, globals: !2, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "debugloc-factoring-add.c", directory: "/tmp")
+!2 = !{}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !DILocation(line: 10, column: 1, scope: !10)
+!6 = !DIFile(filename: "debugloc-factoring-add.c", directory: "/tmp")
+!10 = distinct !DISubprogram(name: "ham", scope: !6, file: !6, line: 10, type: !11, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
+!11 = distinct !DISubroutineType(types: !12)
+!12 = !{null}
+;.
+; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: [[META2:![0-9]+]], retainedTypes: [[META2]], globals: [[META2]], splitDebugInlining: false, nameTableKind: None)
+; CHECK: [[META1]] = !DIFile(filename: "debugloc-factoring-add.c", directory: {{.*}})
+; CHECK: [[META2]] = !{}
+; CHECK: [[DBG4]] = distinct !DISubprogram(name: "ham", scope: [[META1]], file: [[META1]], line: 10, type: [[META5:![0-9]+]], flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META2]])
+; CHECK: [[META5]] = distinct !DISubroutineType(types: [[META6:![0-9]+]])
+; CHECK: [[META6]] = !{null}
+; CHECK: [[DBG7]] = !DILocation(line: 10, column: 1, scope: [[DBG4]])
+;.

@SLTozer SLTozer merged commit c4667c5 into llvm:main Apr 9, 2025
14 checks passed
AllinLeeYL pushed a commit to AllinLeeYL/llvm-project that referenced this pull request Apr 10, 2025
llvm#134829)

As part of reassociating add instructions, we may factorize some of the
adds and produce a mul instruction; this patch propagates the source
location of the reassociated tree of instructions to the new mul.

Found using llvm#107279.
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
llvm#134829)

As part of reassociating add instructions, we may factorize some of the
adds and produce a mul instruction; this patch propagates the source
location of the reassociated tree of instructions to the new mul.

Found using llvm#107279.
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