Skip to content

[AggrInstCombine][DebugInfo] Propagate DILocation for inlined memchr #134808

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 8, 2025

Conversation

SLTozer
Copy link
Contributor

@SLTozer SLTozer commented Apr 8, 2025

When AggressiveInstCombine replaces a memchr with a switch instruction, it currently drops the DILocation for that memchr. This patch changes this, propagating the memchr DILocation to all the generated instructions that replace it.

Found using #107279.

When AggressiveInstCombine replaces a memchr with a switch instruction, it
currently drops the DILocation for that memchr. This patch changes this,
propagating the memchr DILocation to all the generated instructions that
replace it.
@llvmbot
Copy link
Member

llvmbot commented Apr 8, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Stephen Tozer (SLTozer)

Changes

When AggressiveInstCombine replaces a memchr with a switch instruction, it currently drops the DILocation for that memchr. This patch changes this, propagating the memchr DILocation to all the generated instructions that replace it.

Found using #107279.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp (+1)
  • (added) llvm/test/Transforms/AggressiveInstCombine/dbgloc-memchr.ll (+44)
diff --git a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
index 6b0f568864fd5..af994022a8ec1 100644
--- a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
+++ b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
@@ -1133,6 +1133,7 @@ static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
   BasicBlock *BB = Call->getParent();
   BasicBlock *BBNext = SplitBlock(BB, Call, DTU);
   IRBuilder<> IRB(BB);
+  IRB.SetCurrentDebugLocation(Call->getDebugLoc());
   IntegerType *ByteTy = IRB.getInt8Ty();
   BB->getTerminator()->eraseFromParent();
   SwitchInst *SI = IRB.CreateSwitch(
diff --git a/llvm/test/Transforms/AggressiveInstCombine/dbgloc-memchr.ll b/llvm/test/Transforms/AggressiveInstCombine/dbgloc-memchr.ll
new file mode 100644
index 0000000000000..c580490a9ae68
--- /dev/null
+++ b/llvm/test/Transforms/AggressiveInstCombine/dbgloc-memchr.ll
@@ -0,0 +1,44 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -p=aggressive-instcombine < %s | FileCheck %s
+;; Tests that when we inline a simple memchr, we transfer its DILocation to the
+;; generated instructions.
+
+@.str.30 = constant [3 x i8] c"/\\\00"
+
+define i32 @foo(i32 %conv22) {
+; CHECK-LABEL: define i32 @foo(
+; CHECK-SAME: i32 [[CONV22:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = trunc i32 [[CONV22]] to i8, !dbg [[DBG3:![0-9]+]]
+; CHECK-NEXT:    switch i8 [[TMP0]], label %[[ENTRY_SPLIT:.*]] [
+; CHECK-NEXT:    ], !dbg [[DBG3]]
+; CHECK:       [[MEMCHR_SUCCESS:.*:]]
+; CHECK-NEXT:    br label %[[ENTRY_SPLIT]], !dbg [[DBG3]]
+; CHECK:       [[ENTRY_SPLIT]]:
+; CHECK-NEXT:    ret i32 0
+;
+entry:
+  %memchr = call ptr @memchr(ptr @.str.30, i32 %conv22, i64 0), !dbg !3
+  ret i32 0
+}
+
+declare ptr @memchr(ptr, i32, i64)
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 20.0.0git")
+!1 = !DIFile(filename: "test.c", directory: "/tmp")
+!2 = !{i32 2, !"Debug Info Version", i32 3}
+!3 = !DILocation(line: 19, column: 42, scope: !4)
+!4 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 174, type: !5, unit: !0, retainedNodes: !6)
+!5 = !DISubroutineType(types: !6)
+!6 = !{}
+;.
+; 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: 19, column: 42, scope: [[META4:![0-9]+]])
+; CHECK: [[META4]] = distinct !DISubprogram(name: "foo", scope: [[META1]], file: [[META1]], line: 174, type: [[META5:![0-9]+]], spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META6:![0-9]+]])
+; CHECK: [[META5]] = !DISubroutineType(types: [[META6]])
+; CHECK: [[META6]] = !{}
+;.

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, follows the same rules as inlining nodbg function IIRC (propagate call loc)

@SLTozer SLTozer merged commit 19ee7ff into llvm:main Apr 8, 2025
13 checks passed
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…lvm#134808)

When AggressiveInstCombine replaces a memchr with a switch instruction,
it currently drops the DILocation for that memchr. This patch changes
this, propagating the memchr DILocation to all the generated
instructions that replace it.

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