Skip to content

[DebugInfo][InstCombine] Propagate DILocation when noop-ing invoke #134678

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

In InstCombine we may decide that an alloc is removable, and the alloc fn is called by an InvokeInst, we replace that InvokeInst with a invoke of a noop intrinsic; this patch has us also copy the original invoke's DILocation to the new noop invoke.

Found using #107279.

…Location

In InstCombine we may decide that an alloc is removable, and the alloc fn is
called by an InvokeInst, we replace that InvokeInst with a invoke of
a noop intrinsic; this patch has us also copy the original invoke's
DILocation to the new noop invoke.
@SLTozer SLTozer self-assigned this Apr 7, 2025
@SLTozer SLTozer requested a review from nikic as a code owner April 7, 2025 16:03
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Apr 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 7, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-debuginfo

Author: Stephen Tozer (SLTozer)

Changes

In InstCombine we may decide that an alloc is removable, and the alloc fn is called by an InvokeInst, we replace that InvokeInst with a invoke of a noop intrinsic; this patch has us also copy the original invoke's DILocation to the new noop invoke.

Found using #107279.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+3-2)
  • (added) llvm/test/Transforms/InstCombine/debuginfo-invoke.ll (+51)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 3a2fa154b0fdd..856e02c9f1ddb 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3406,8 +3406,9 @@ Instruction *InstCombinerImpl::visitAllocSite(Instruction &MI) {
       // Replace invoke with a NOP intrinsic to maintain the original CFG
       Module *M = II->getModule();
       Function *F = Intrinsic::getOrInsertDeclaration(M, Intrinsic::donothing);
-      InvokeInst::Create(F, II->getNormalDest(), II->getUnwindDest(), {}, "",
-                         II->getParent());
+      auto *NewII = InvokeInst::Create(
+          F, II->getNormalDest(), II->getUnwindDest(), {}, "", II->getParent());
+      NewII->setDebugLoc(II->getDebugLoc());
     }
 
     // Remove debug intrinsics which describe the value contained within the
diff --git a/llvm/test/Transforms/InstCombine/debuginfo-invoke.ll b/llvm/test/Transforms/InstCombine/debuginfo-invoke.ll
new file mode 100644
index 0000000000000..287582b9baf3b
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/debuginfo-invoke.ll
@@ -0,0 +1,51 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=instcombine -S %s -o - | FileCheck %s
+
+define void @foo() personality ptr null !dbg !4 {
+; CHECK-LABEL: define void @foo(
+; CHECK-SAME: ) personality ptr null !dbg [[DBG3:![0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    invoke void @llvm.donothing()
+; CHECK-NEXT:            to label %[[COMMON_RET:.*]] unwind label %[[LPAD159:.*]], !dbg [[DBG7:![0-9]+]]
+; CHECK:       [[COMMON_RET]]:
+; CHECK-NEXT:    ret void
+; CHECK:       [[LPAD159]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = landingpad { ptr, i32 }
+; CHECK-NEXT:            cleanup
+; CHECK-NEXT:    br label %[[COMMON_RET]]
+;
+entry:
+  %call.i.i895904 = invoke ptr @_Znam(i64 0)
+          to label %common.ret unwind label %lpad159, !dbg !9
+
+common.ret:                                       ; preds = %lpad159, %entry
+  ret void
+
+lpad159:                                          ; preds = %entry
+  %0 = landingpad { ptr, i32 }
+  cleanup
+  br label %common.ret
+}
+
+declare ptr @_Znam(i64)
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 20.0.0git")
+!1 = !DIFile(filename: "ArchiveCommandLine.cpp", directory: "/tmp")
+!2 = !{}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 13, type: !6, scopeLine: 13, unit: !0, retainedNodes: !2)
+!6 = distinct !DISubroutineType(types: !7)
+!7 = !{null}
+!9 = !DILocation(line: 14, column: 20, scope: !4)
+;.
+; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
+; CHECK: [[META1]] = !DIFile(filename: "ArchiveCommandLine.cpp", directory: {{.*}})
+; CHECK: [[DBG3]] = distinct !DISubprogram(name: "foo", scope: [[META1]], file: [[META1]], line: 13, type: [[META4:![0-9]+]], scopeLine: 13, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META6:![0-9]+]])
+; CHECK: [[META4]] = distinct !DISubroutineType(types: [[META5:![0-9]+]])
+; CHECK: [[META5]] = !{null}
+; CHECK: [[META6]] = !{}
+; CHECK: [[DBG7]] = !DILocation(line: 14, column: 20, scope: [[DBG3]])
+;.

@SLTozer SLTozer merged commit 93505f8 into llvm:main Apr 8, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
debuginfo llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants