[llvm] [DebugInfo] Do not change #dbg_declare type during salvage (PR #134601)

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 7 02:28:10 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Nikita Popov (nikic)

<details>
<summary>Changes</summary>

When salvaging debug info for #dbg_declare, do not perform salvage that changes the type of the value, i.e. bail out on cast instructions. This ensures that we don't run afoul of the verifier check in #<!-- -->134355.

Fixes https://github.com/llvm/llvm-project/issues/134523#issuecomment-2781393701.

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


3 Files Affected:

- (modified) llvm/include/llvm/Transforms/Utils/Local.h (+5-1) 
- (modified) llvm/lib/Transforms/Utils/Local.cpp (+7-2) 
- (added) llvm/test/Transforms/CodeGenPrepare/X86/cast-debuginfo-salvage.ll (+33) 


``````````diff
diff --git a/llvm/include/llvm/Transforms/Utils/Local.h b/llvm/include/llvm/Transforms/Utils/Local.h
index db064e1f41f02..e522a268c83b1 100644
--- a/llvm/include/llvm/Transforms/Utils/Local.h
+++ b/llvm/include/llvm/Transforms/Utils/Local.h
@@ -347,9 +347,13 @@ void salvageDebugInfoForDbgValues(Instruction &I,
 ///   Return = %a
 ///   Ops = llvm::dwarf::DW_OP_LLVM_arg0 llvm::dwarf::DW_OP_add
 ///   AdditionalValues = %b
+///
+/// \param KeepType If enabled, only perform salvage that does not modify
+///                 the type of the value.
 Value *salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps,
                             SmallVectorImpl<uint64_t> &Ops,
-                            SmallVectorImpl<Value *> &AdditionalValues);
+                            SmallVectorImpl<Value *> &AdditionalValues,
+                            bool KeepType = false);
 
 /// Point debug users of \p From to \p To or salvage them. Use this function
 /// only when replacing all uses of \p From with \p To, with a guarantee that
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 2f3ea2266e07f..4e96adec5908b 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -2398,7 +2398,8 @@ void llvm::salvageDebugInfoForDbgValues(
       SmallVector<uint64_t, 16> Ops;
       unsigned LocNo = std::distance(DVRLocation.begin(), LocItr);
       uint64_t CurrentLocOps = SalvagedExpr->getNumLocationOperands();
-      Op0 = salvageDebugInfoImpl(I, CurrentLocOps, Ops, AdditionalValues);
+      Op0 = salvageDebugInfoImpl(I, CurrentLocOps, Ops, AdditionalValues,
+                                 DVR->isDbgDeclare());
       if (!Op0)
         break;
       SalvagedExpr =
@@ -2600,11 +2601,15 @@ Value *getSalvageOpsForIcmpOp(ICmpInst *Icmp, uint64_t CurrentLocOps,
 
 Value *llvm::salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps,
                                   SmallVectorImpl<uint64_t> &Ops,
-                                  SmallVectorImpl<Value *> &AdditionalValues) {
+                                  SmallVectorImpl<Value *> &AdditionalValues,
+                                  bool KeepType) {
   auto &M = *I.getModule();
   auto &DL = M.getDataLayout();
 
   if (auto *CI = dyn_cast<CastInst>(&I)) {
+    if (KeepType)
+      return nullptr;
+
     Value *FromValue = CI->getOperand(0);
     // No-op casts are irrelevant for debug info.
     if (CI->isNoopCast(DL)) {
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/cast-debuginfo-salvage.ll b/llvm/test/Transforms/CodeGenPrepare/X86/cast-debuginfo-salvage.ll
new file mode 100644
index 0000000000000..f22942d55306f
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/cast-debuginfo-salvage.ll
@@ -0,0 +1,33 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -codegenprepare -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+
+; Make sure we don't produce something like #dbg_declare(i64 %arg) here.
+define void @test(i64 %arg) {
+; CHECK-LABEL: define void @test(
+; CHECK-SAME: i64 [[ARG:%.*]]) {
+; CHECK-NEXT:      #dbg_declare(ptr poison, [[META4:![0-9]+]], !DIExpression(), [[META6:![0-9]+]])
+; CHECK-NEXT:    ret void
+;
+  %inttoptr = inttoptr i64 %arg to ptr
+  #dbg_declare(ptr %inttoptr, !4, !DIExpression(), !6)
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 21.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !2, globals: !2, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "test.cpp", directory: "/home/npopov/repos/llvm-project", checksumkind: CSK_MD5, checksum: "eabd3d442348430f50ca2f8c28d743e9")
+!2 = !{}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !DILocalVariable(name: "c", scope: !5, file: !1, line: 26)
+!5 = distinct !DISubprogram(name: "n<(lambda at test.cpp:39:14)>", linkageName: "_ZN1m1nIZN1o1pEvEUl1kE_EE5arrayT_i", scope: null, file: !1, line: 25, spFlags: DISPFlagDefinition, unit: !0)
+!6 = !DILocation(line: 26, column: 11, scope: !5)
+;.
+; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: [[META2:![0-9]+]], globals: [[META2]], splitDebugInlining: false, nameTableKind: None)
+; CHECK: [[META1]] = !DIFile(filename: "test.cpp", directory: {{.*}})
+; CHECK: [[META2]] = !{}
+; CHECK: [[META4]] = !DILocalVariable(name: "c", scope: [[META5:![0-9]+]], file: [[META1]], line: 26)
+; CHECK: [[META5]] = distinct !DISubprogram(name: "n<(lambda at test.cpp:39:14)>", linkageName: "_ZN1m1nIZN1o1pEvEUl1kE_EE5arrayT_i", scope: null, file: [[META1]], line: 25, spFlags: DISPFlagDefinition, unit: [[META0]])
+; CHECK: [[META6]] = !DILocation(line: 26, column: 11, scope: [[META5]])
+;.

``````````

</details>


https://github.com/llvm/llvm-project/pull/134601


More information about the llvm-commits mailing list