[llvm] [Verifier][CGP] Allow integer argument to dbg_declare (PR #134803)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 8 01:05:58 PDT 2025
https://github.com/nikic created https://github.com/llvm/llvm-project/pull/134803
Relaxes the newly added verifier rule to also allow an integer argument to dbg_declare, which is interpreted as a pointer. Adjust CGP code to deal with it gracefully.
Something I'm not sure on is whether this ever actually does anything useful -- obviously we're not going to get a FrameIndex for this dbg_declare, but I guess it could in theory do something for entry values?
Fixes https://github.com/llvm/llvm-project/issues/134523. Alternative to https://github.com/llvm/llvm-project/pull/134601.
>From 2881b1e6c1e1b5206794e9eace5c4025ac6c762e Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 8 Apr 2025 10:03:31 +0200
Subject: [PATCH] [Verifier][CGP] Allow integer argument to dbg_declare
Relaxes the newly added verifier rule to also allow an integer
argument to dbg_declare, which is interpreted as a pointer.
Adjust CGP code to deal with it gracefully.
Something I'm not sure on is whether this ever actually does
anything useful -- obviously we're not going to get a FrameIndex
for this dbg_declare, but I guess it could in theory do something
for entry values?
Fixes https://github.com/llvm/llvm-project/issues/134523.
---
.../CodeGen/SelectionDAG/SelectionDAGISel.cpp | 3 ++
llvm/lib/IR/Verifier.cpp | 9 +++--
.../X86/cast-debuginfo-salvage.ll | 33 +++++++++++++++++++
3 files changed, 42 insertions(+), 3 deletions(-)
create mode 100644 llvm/test/Transforms/CodeGenPrepare/X86/cast-debuginfo-salvage.ll
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index bfbfdbc7e3ca2..083b984444bcb 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -1556,6 +1556,9 @@ static bool processDbgDeclare(FunctionLoweringInfo &FuncInfo,
if (processIfEntryValueDbgDeclare(FuncInfo, Address, Expr, Var, DbgLoc))
return true;
+ if (!Address->getType()->isPointerTy())
+ return false;
+
MachineFunction *MF = FuncInfo.MF;
const DataLayout &DL = MF->getDataLayout();
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 7423e746dfa9a..ad6f3e649d7b5 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -6668,9 +6668,12 @@ void Verifier::visit(DbgVariableRecord &DVR) {
"invalid #dbg record address/value", &DVR, MD);
if (auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {
visitValueAsMetadata(*VAM, F);
- if (DVR.isDbgDeclare())
- CheckDI(VAM->getValue()->getType()->isPointerTy(),
- "location of #dbg_declare must be a pointer", &DVR, MD);
+ if (DVR.isDbgDeclare()) {
+ // Allow integers here to support inttoptr salvage.
+ Type *Ty = VAM->getValue()->getType();
+ CheckDI(Ty->isPointerTy() || Ty->isIntegerTy(),
+ "location of #dbg_declare must be a pointer or int", &DVR, MD);
+ }
} else if (auto *AL = dyn_cast<DIArgList>(MD)) {
visitDIArgList(*AL, F);
}
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..51e75c1fc8486
--- /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 salvaging an dbg_declare on inttoptr does not crash.
+define void @test(i64 %arg) {
+; CHECK-LABEL: define void @test(
+; CHECK-SAME: i64 [[ARG:%.*]]) {
+; CHECK-NEXT: #dbg_declare(i64 [[ARG]], [[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]])
+;.
More information about the llvm-commits
mailing list