Skip to content

Commit 835c1b5

Browse files
authored
[RemoveDIs] Auto-upgrade debug intrinsics to DbgRecords (default false) (#85650)
If --load-bitcode-into-experimental-debuginfo-iterators is true then debug intrinsics are auto-upgraded to DbgRecords (the new debug info format). The upgrade is trivial because the two representations are semantically identical. llvm.dbg.value with 4 operands and llvm.dbg.addr intrinsics are upgraded in the same way as usual, but converted directly into DbgRecords instead of debug intrinsics.
1 parent 94c6ce1 commit 835c1b5

File tree

5 files changed

+93
-10
lines changed

5 files changed

+93
-10
lines changed

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -609,17 +609,25 @@ class MetadataLoader::MetadataLoaderImpl {
609609
if (!NeedDeclareExpressionUpgrade)
610610
return;
611611

612+
auto UpdateDeclareIfNeeded = [&](auto *Declare) {
613+
auto *DIExpr = Declare->getExpression();
614+
if (!DIExpr || !DIExpr->startsWithDeref() ||
615+
!isa_and_nonnull<Argument>(Declare->getAddress()))
616+
return;
617+
SmallVector<uint64_t, 8> Ops;
618+
Ops.append(std::next(DIExpr->elements_begin()), DIExpr->elements_end());
619+
Declare->setExpression(DIExpression::get(Context, Ops));
620+
};
621+
612622
for (auto &BB : F)
613-
for (auto &I : BB)
623+
for (auto &I : BB) {
624+
for (DPValue &DPV : filterDbgVars(I.getDbgRecordRange())) {
625+
if (DPV.isDbgDeclare())
626+
UpdateDeclareIfNeeded(&DPV);
627+
}
614628
if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
615-
if (auto *DIExpr = DDI->getExpression())
616-
if (DIExpr->startsWithDeref() &&
617-
isa_and_nonnull<Argument>(DDI->getAddress())) {
618-
SmallVector<uint64_t, 8> Ops;
619-
Ops.append(std::next(DIExpr->elements_begin()),
620-
DIExpr->elements_end());
621-
DDI->setExpression(DIExpression::get(Context, Ops));
622-
}
629+
UpdateDeclareIfNeeded(DDI);
630+
}
623631
}
624632

625633
/// Upgrade the expression from previous versions.

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,18 @@ static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
10561056
}
10571057
case 'd':
10581058
if (Name.consume_front("dbg.")) {
1059+
// Mark debug intrinsics for upgrade to new debug format.
1060+
if (F->getParent()->IsNewDbgInfoFormat) {
1061+
if (Name == "addr" || Name == "value" || Name == "assign" ||
1062+
Name == "declare" || Name == "label") {
1063+
// There's no function to replace these with.
1064+
NewFn = nullptr;
1065+
// But we do want these to get upgraded.
1066+
return true;
1067+
}
1068+
}
1069+
// Update llvm.dbg.addr intrinsics even in "new debug mode"; they'll get
1070+
// converted to DPValues later.
10591071
if (Name == "addr" || (Name == "value" && F->arg_size() == 4)) {
10601072
rename(F);
10611073
NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_value);
@@ -2332,6 +2344,59 @@ static Value *upgradeAMDGCNIntrinsicCall(StringRef Name, CallBase *CI,
23322344
llvm_unreachable("Unknown function for AMDGPU intrinsic upgrade.");
23332345
}
23342346

2347+
/// Helper to unwrap intrinsic call MetadataAsValue operands.
2348+
template <typename MDType>
2349+
static MDType *unwrapMAVOp(CallBase *CI, unsigned Op) {
2350+
if (MetadataAsValue *MAV = dyn_cast<MetadataAsValue>(CI->getArgOperand(Op)))
2351+
return dyn_cast<MDType>(MAV->getMetadata());
2352+
return nullptr;
2353+
}
2354+
2355+
/// Convert debug intrinsic calls to non-instruction debug records.
2356+
/// \p Name - Final part of the intrinsic name, e.g. 'value' in llvm.dbg.value.
2357+
/// \p CI - The debug intrinsic call.
2358+
static void upgradeDbgIntrinsicToDbgRecord(StringRef Name, CallBase *CI) {
2359+
DbgRecord *DR = nullptr;
2360+
if (Name == "label") {
2361+
DR = new DPLabel(unwrapMAVOp<DILabel>(CI, 0), CI->getDebugLoc());
2362+
} else if (Name == "assign") {
2363+
DR = new DPValue(
2364+
unwrapMAVOp<Metadata>(CI, 0), unwrapMAVOp<DILocalVariable>(CI, 1),
2365+
unwrapMAVOp<DIExpression>(CI, 2), unwrapMAVOp<DIAssignID>(CI, 3),
2366+
unwrapMAVOp<Metadata>(CI, 4), unwrapMAVOp<DIExpression>(CI, 5),
2367+
CI->getDebugLoc());
2368+
} else if (Name == "declare") {
2369+
DR = new DPValue(unwrapMAVOp<Metadata>(CI, 0),
2370+
unwrapMAVOp<DILocalVariable>(CI, 1),
2371+
unwrapMAVOp<DIExpression>(CI, 2), CI->getDebugLoc(),
2372+
DPValue::LocationType::Declare);
2373+
} else if (Name == "addr") {
2374+
// Upgrade dbg.addr to dbg.value with DW_OP_deref.
2375+
DIExpression *Expr = unwrapMAVOp<DIExpression>(CI, 2);
2376+
Expr = DIExpression::append(Expr, dwarf::DW_OP_deref);
2377+
DR = new DPValue(unwrapMAVOp<Metadata>(CI, 0),
2378+
unwrapMAVOp<DILocalVariable>(CI, 1), Expr,
2379+
CI->getDebugLoc());
2380+
} else if (Name == "value") {
2381+
// An old version of dbg.value had an extra offset argument.
2382+
unsigned VarOp = 1;
2383+
unsigned ExprOp = 2;
2384+
if (CI->arg_size() == 4) {
2385+
auto *Offset = dyn_cast_or_null<Constant>(CI->getArgOperand(1));
2386+
// Nonzero offset dbg.values get dropped without a replacement.
2387+
if (!Offset || !Offset->isZeroValue())
2388+
return;
2389+
VarOp = 2;
2390+
ExprOp = 3;
2391+
}
2392+
DR = new DPValue(unwrapMAVOp<Metadata>(CI, 0),
2393+
unwrapMAVOp<DILocalVariable>(CI, VarOp),
2394+
unwrapMAVOp<DIExpression>(CI, ExprOp), CI->getDebugLoc());
2395+
}
2396+
assert(DR && "Unhandled intrinsic kind in upgrade to DbgRecord");
2397+
CI->getParent()->insertDbgRecordBefore(DR, CI->getIterator());
2398+
}
2399+
23352400
/// Upgrade a call to an old intrinsic. All argument and return casting must be
23362401
/// provided to seamlessly integrate with existing context.
23372402
void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
@@ -2357,6 +2422,7 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
23572422
bool IsNVVM = Name.consume_front("nvvm.");
23582423
bool IsARM = Name.consume_front("arm.");
23592424
bool IsAMDGCN = Name.consume_front("amdgcn.");
2425+
bool IsDbg = Name.consume_front("dbg.");
23602426

23612427
if (IsX86 && Name.starts_with("sse4a.movnt.")) {
23622428
SmallVector<Metadata *, 1> Elts;
@@ -2461,7 +2527,7 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
24612527
return;
24622528
}
24632529

2464-
Value *Rep;
2530+
Value *Rep = nullptr;
24652531
// Upgrade packed integer vector compare intrinsics to compare instructions.
24662532
if (IsX86 && (Name.starts_with("sse2.pcmp") ||
24672533
Name.starts_with("avx2.pcmp"))) {
@@ -4196,6 +4262,8 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
41964262
Rep = upgradeARMIntrinsicCall(Name, CI, F, Builder);
41974263
} else if (IsAMDGCN) {
41984264
Rep = upgradeAMDGCNIntrinsicCall(Name, CI, F, Builder);
4265+
} else if (IsDbg && CI->getModule()->IsNewDbgInfoFormat) {
4266+
upgradeDbgIntrinsicToDbgRecord(Name, CI);
41994267
} else {
42004268
llvm_unreachable("Unknown function for CallBase upgrade.");
42014269
}

llvm/test/Bitcode/DIExpression-aggresult.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: llvm-dis -o - %s.bc | FileCheck %s
2+
; RUN: llvm-dis -o - %s.bc --load-bitcode-into-experimental-debuginfo-iterators=true | FileCheck %s
23
%class.A = type { i32, i32, i32, i32 }
34

45
define void @_Z3fooi(%class.A* sret(%class.A) %agg.result) #0 !dbg !3 {

llvm/test/Bitcode/dbg-record-roundtrip.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
; RUN: | llvm-dis --load-bitcode-into-experimental-debuginfo-iterators=true --write-experimental-debuginfo=true \
1111
; RUN: | FileCheck %s --check-prefixes=RECORDS
1212

13+
;; Load intrinsics directly into the new format (auto-upgrade).
14+
; RUN: llvm-as --write-experimental-debuginfo-iterators-to-bitcode=false %s -o - \
15+
; RUN: | llvm-dis --load-bitcode-into-experimental-debuginfo-iterators=true --write-experimental-debuginfo=true \
16+
; RUN: | FileCheck %s --check-prefixes=RECORDS
17+
1318
;; Check that verify-uselistorder passes regardless of input format.
1419
; RUN: llvm-as %s --write-experimental-debuginfo-iterators-to-bitcode=true -o - | verify-uselistorder
1520
; RUN: verify-uselistorder %s

llvm/test/Bitcode/upgrade-dbg-addr.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
; Test upgrade of dbg.addr intrinsics into dbg.value with DW_OP_deref appended
22
;
33
; RUN: llvm-dis < %s.bc | FileCheck %s
4+
; RUN: llvm-dis < %s.bc --load-bitcode-into-experimental-debuginfo-iterators --write-experimental-debuginfo=false | FileCheck %s
45
; RUN: verify-uselistorder < %s.bc
56

67
define i32 @example(i32 %num) {

0 commit comments

Comments
 (0)