Skip to content

[DebugInfo] Do not change #dbg_declare type during salvage #134601

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

Closed
wants to merge 1 commit into from

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Apr 7, 2025

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 #134523 (comment).

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 llvm#134355.
@llvmbot
Copy link
Member

llvmbot commented Apr 7, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Nikita Popov (nikic)

Changes

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 #134523 (comment).


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 --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]])
+;.

@nikic
Copy link
Contributor Author

nikic commented Apr 7, 2025

Closing this based on the comment here: #134523 (comment)

@nikic nikic closed this Apr 7, 2025
nikic added a commit that referenced this pull request Apr 10, 2025
Relaxes the newly added verifier rule to also allow an integer argument
to dbg_declare, which is interpreted as a pointer. Adjust CGP to deal with
it gracefully.

Fixes #134523.
Alternative to #134601.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Apr 10, 2025
…4803)

Relaxes the newly added verifier rule to also allow an integer argument
to dbg_declare, which is interpreted as a pointer. Adjust CGP to deal with
it gracefully.

Fixes llvm/llvm-project#134523.
Alternative to llvm/llvm-project#134601.
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
Relaxes the newly added verifier rule to also allow an integer argument
to dbg_declare, which is interpreted as a pointer. Adjust CGP to deal with
it gracefully.

Fixes llvm#134523.
Alternative to llvm#134601.
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.

Assertion `Ty->isPtrOrPtrVectorTy() && "This should only be called with a pointer or pointer vector type"' failed.
2 participants