Skip to content

Commit 09e7d86

Browse files
authored
[RemoveDIs] Fix findDbgValues to return dbg_assign records too (#90471)
In the debug intrinsic class heirachy, a dbg.assign is a (inherits from) dbg.value, so `findDbgValues` returns dbg.values and dbg.assigns (by design). That hierarchy doesn't exist for DbgRecords - fix findDbgValues to return dbg_assign records as well as dbg_values and add unittest.
1 parent 91a8cb7 commit 09e7d86

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

llvm/lib/IR/DebugInfo.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ TinyPtrVector<DbgVariableRecord *> llvm::findDVRDeclares(Value *V) {
8080
return Declares;
8181
}
8282

83-
template <typename IntrinsicT, DbgVariableRecord::LocationType Type =
84-
DbgVariableRecord::LocationType::Any>
83+
template <typename IntrinsicT, bool DbgAssignAndValuesOnly>
8584
static void
8685
findDbgIntrinsics(SmallVectorImpl<IntrinsicT *> &Result, Value *V,
8786
SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords) {
@@ -114,8 +113,7 @@ findDbgIntrinsics(SmallVectorImpl<IntrinsicT *> &Result, Value *V,
114113
// Get DbgVariableRecords that use this as a single value.
115114
if (LocalAsMetadata *L = dyn_cast<LocalAsMetadata>(MD)) {
116115
for (DbgVariableRecord *DVR : L->getAllDbgVariableRecordUsers()) {
117-
if (Type == DbgVariableRecord::LocationType::Any ||
118-
DVR->getType() == Type)
116+
if (!DbgAssignAndValuesOnly || DVR->isDbgValue() || DVR->isDbgAssign())
119117
if (EncounteredDbgVariableRecords.insert(DVR).second)
120118
DbgVariableRecords->push_back(DVR);
121119
}
@@ -130,8 +128,7 @@ findDbgIntrinsics(SmallVectorImpl<IntrinsicT *> &Result, Value *V,
130128
continue;
131129
DIArgList *DI = cast<DIArgList>(AL);
132130
for (DbgVariableRecord *DVR : DI->getAllDbgVariableRecordUsers())
133-
if (Type == DbgVariableRecord::LocationType::Any ||
134-
DVR->getType() == Type)
131+
if (!DbgAssignAndValuesOnly || DVR->isDbgValue() || DVR->isDbgAssign())
135132
if (EncounteredDbgVariableRecords.insert(DVR).second)
136133
DbgVariableRecords->push_back(DVR);
137134
}
@@ -141,14 +138,14 @@ findDbgIntrinsics(SmallVectorImpl<IntrinsicT *> &Result, Value *V,
141138
void llvm::findDbgValues(
142139
SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V,
143140
SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords) {
144-
findDbgIntrinsics<DbgValueInst, DbgVariableRecord::LocationType::Value>(
141+
findDbgIntrinsics<DbgValueInst, /*DbgAssignAndValuesOnly=*/true>(
145142
DbgValues, V, DbgVariableRecords);
146143
}
147144

148145
void llvm::findDbgUsers(
149146
SmallVectorImpl<DbgVariableIntrinsic *> &DbgUsers, Value *V,
150147
SmallVectorImpl<DbgVariableRecord *> *DbgVariableRecords) {
151-
findDbgIntrinsics<DbgVariableIntrinsic, DbgVariableRecord::LocationType::Any>(
148+
findDbgIntrinsics<DbgVariableIntrinsic, /*DbgAssignAndValuesOnly=*/false>(
152149
DbgUsers, V, DbgVariableRecords);
153150
}
154151

llvm/unittests/Transforms/Utils/LocalTest.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,67 @@ TEST(Local, FindDbgUsers) {
732732
EXPECT_EQ(Vals.size(), 1u);
733733
}
734734

735+
TEST(Local, FindDbgRecords) {
736+
// DbgRecord copy of the FindDbgUsers test above.
737+
LLVMContext Ctx;
738+
std::unique_ptr<Module> M = parseIR(Ctx,
739+
R"(
740+
define dso_local void @fun(ptr %a) #0 !dbg !11 {
741+
entry:
742+
call void @llvm.dbg.assign(metadata ptr %a, metadata !16, metadata !DIExpression(), metadata !15, metadata ptr %a, metadata !DIExpression()), !dbg !19
743+
ret void
744+
}
745+
746+
!llvm.dbg.cu = !{!0}
747+
!llvm.module.flags = !{!2, !3, !9}
748+
!llvm.ident = !{!10}
749+
750+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 17.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
751+
!1 = !DIFile(filename: "test.cpp", directory: "/")
752+
!2 = !{i32 7, !"Dwarf Version", i32 5}
753+
!3 = !{i32 2, !"Debug Info Version", i32 3}
754+
!4 = !{i32 1, !"wchar_size", i32 4}
755+
!9 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
756+
!10 = !{!"clang version 17.0.0"}
757+
!11 = distinct !DISubprogram(name: "fun", linkageName: "fun", scope: !1, file: !1, line: 1, type: !12, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14)
758+
!12 = !DISubroutineType(types: !13)
759+
!13 = !{null}
760+
!14 = !{}
761+
!15 = distinct !DIAssignID()
762+
!16 = !DILocalVariable(name: "x", scope: !11, file: !1, line: 2, type: !17)
763+
!17 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !18, size: 64)
764+
!18 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
765+
!19 = !DILocation(line: 0, scope: !11)
766+
)");
767+
768+
bool BrokenDebugInfo = true;
769+
verifyModule(*M, &errs(), &BrokenDebugInfo);
770+
ASSERT_FALSE(BrokenDebugInfo);
771+
bool NewDbgInfoFormat = UseNewDbgInfoFormat;
772+
UseNewDbgInfoFormat = true;
773+
M->convertToNewDbgValues();
774+
775+
Function &Fun = *cast<Function>(M->getNamedValue("fun"));
776+
Value *Arg = Fun.getArg(0);
777+
778+
SmallVector<DbgVariableIntrinsic *> Users;
779+
SmallVector<DbgVariableRecord *> Records;
780+
// Arg (%a) is used twice by a single dbg_assign. Check findDbgUsers returns
781+
// only 1 pointer to it rather than 2.
782+
findDbgUsers(Users, Arg, &Records);
783+
EXPECT_EQ(Users.size(), 0u);
784+
EXPECT_EQ(Records.size(), 1u);
785+
786+
SmallVector<DbgValueInst *> Vals;
787+
Records.clear();
788+
// Arg (%a) is used twice by a single dbg_assign. Check findDbgValues returns
789+
// only 1 pointer to it rather than 2.
790+
findDbgValues(Vals, Arg, &Records);
791+
EXPECT_EQ(Vals.size(), 0u);
792+
EXPECT_EQ(Records.size(), 1u);
793+
UseNewDbgInfoFormat = NewDbgInfoFormat;
794+
}
795+
735796
TEST(Local, ReplaceAllDbgUsesWith) {
736797
using namespace llvm::dwarf;
737798

0 commit comments

Comments
 (0)