llvm-project
1247 строк · 51.3 Кб
1//===- llvm/unittest/IR/DebugInfo.cpp - DebugInfo tests -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/IR/DebugInfo.h"
10#include "../lib/IR/LLVMContextImpl.h"
11#include "llvm/ADT/APSInt.h"
12#include "llvm/AsmParser/Parser.h"
13#include "llvm/IR/DIBuilder.h"
14#include "llvm/IR/DebugInfoMetadata.h"
15#include "llvm/IR/DebugProgramInstruction.h"
16#include "llvm/IR/IRBuilder.h"
17#include "llvm/IR/IntrinsicInst.h"
18#include "llvm/IR/LLVMContext.h"
19#include "llvm/IR/Metadata.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/Verifier.h"
22#include "llvm/Support/SourceMgr.h"
23#include "llvm/Transforms/Utils/Local.h"
24
25#include "gtest/gtest.h"
26
27using namespace llvm;
28
29extern cl::opt<bool> UseNewDbgInfoFormat;
30
31static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
32SMDiagnostic Err;
33std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
34if (!Mod)
35Err.print("DebugInfoTest", errs());
36return Mod;
37}
38
39namespace {
40
41TEST(DINodeTest, getFlag) {
42// Some valid flags.
43EXPECT_EQ(DINode::FlagPublic, DINode::getFlag("DIFlagPublic"));
44EXPECT_EQ(DINode::FlagProtected, DINode::getFlag("DIFlagProtected"));
45EXPECT_EQ(DINode::FlagPrivate, DINode::getFlag("DIFlagPrivate"));
46EXPECT_EQ(DINode::FlagVector, DINode::getFlag("DIFlagVector"));
47EXPECT_EQ(DINode::FlagRValueReference,
48DINode::getFlag("DIFlagRValueReference"));
49
50// FlagAccessibility shouldn't work.
51EXPECT_EQ(0u, DINode::getFlag("DIFlagAccessibility"));
52
53// Some other invalid strings.
54EXPECT_EQ(0u, DINode::getFlag("FlagVector"));
55EXPECT_EQ(0u, DINode::getFlag("Vector"));
56EXPECT_EQ(0u, DINode::getFlag("other things"));
57EXPECT_EQ(0u, DINode::getFlag("DIFlagOther"));
58}
59
60TEST(DINodeTest, getFlagString) {
61// Some valid flags.
62EXPECT_EQ(StringRef("DIFlagPublic"),
63DINode::getFlagString(DINode::FlagPublic));
64EXPECT_EQ(StringRef("DIFlagProtected"),
65DINode::getFlagString(DINode::FlagProtected));
66EXPECT_EQ(StringRef("DIFlagPrivate"),
67DINode::getFlagString(DINode::FlagPrivate));
68EXPECT_EQ(StringRef("DIFlagVector"),
69DINode::getFlagString(DINode::FlagVector));
70EXPECT_EQ(StringRef("DIFlagRValueReference"),
71DINode::getFlagString(DINode::FlagRValueReference));
72
73// FlagAccessibility actually equals FlagPublic.
74EXPECT_EQ(StringRef("DIFlagPublic"),
75DINode::getFlagString(DINode::FlagAccessibility));
76
77// Some other invalid flags.
78EXPECT_EQ(StringRef(),
79DINode::getFlagString(DINode::FlagPublic | DINode::FlagVector));
80EXPECT_EQ(StringRef(), DINode::getFlagString(DINode::FlagFwdDecl |
81DINode::FlagArtificial));
82EXPECT_EQ(StringRef(),
83DINode::getFlagString(static_cast<DINode::DIFlags>(0xffff)));
84}
85
86TEST(DINodeTest, splitFlags) {
87// Some valid flags.
88#define CHECK_SPLIT(FLAGS, VECTOR, REMAINDER) \
89{ \
90SmallVector<DINode::DIFlags, 8> V; \
91EXPECT_EQ(REMAINDER, DINode::splitFlags(FLAGS, V)); \
92EXPECT_TRUE(ArrayRef(V).equals(VECTOR)); \
93}
94CHECK_SPLIT(DINode::FlagPublic, {DINode::FlagPublic}, DINode::FlagZero);
95CHECK_SPLIT(DINode::FlagProtected, {DINode::FlagProtected}, DINode::FlagZero);
96CHECK_SPLIT(DINode::FlagPrivate, {DINode::FlagPrivate}, DINode::FlagZero);
97CHECK_SPLIT(DINode::FlagVector, {DINode::FlagVector}, DINode::FlagZero);
98CHECK_SPLIT(DINode::FlagRValueReference, {DINode::FlagRValueReference},
99DINode::FlagZero);
100DINode::DIFlags Flags[] = {DINode::FlagFwdDecl, DINode::FlagVector};
101CHECK_SPLIT(DINode::FlagFwdDecl | DINode::FlagVector, Flags,
102DINode::FlagZero);
103CHECK_SPLIT(DINode::FlagZero, {}, DINode::FlagZero);
104#undef CHECK_SPLIT
105}
106
107TEST(StripTest, LoopMetadata) {
108LLVMContext C;
109std::unique_ptr<Module> M = parseIR(C, R"(
110define void @f() !dbg !5 {
111ret void, !dbg !10, !llvm.loop !11
112}
113
114!llvm.dbg.cu = !{!0}
115!llvm.debugify = !{!3, !3}
116!llvm.module.flags = !{!4}
117
118!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
119!1 = !DIFile(filename: "loop.ll", directory: "/")
120!2 = !{}
121!3 = !{i32 1}
122!4 = !{i32 2, !"Debug Info Version", i32 3}
123!5 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !7)
124!6 = !DISubroutineType(types: !2)
125!7 = !{!8}
126!8 = !DILocalVariable(name: "1", scope: !5, file: !1, line: 1, type: !9)
127!9 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
128!10 = !DILocation(line: 1, column: 1, scope: !5)
129!11 = distinct !{!11, !10, !10}
130)");
131
132// Look up the debug info emission kind for the CU via the loop metadata
133// attached to the terminator. If, when stripping non-line table debug info,
134// we update the terminator's metadata correctly, we should be able to
135// observe the change in emission kind for the CU.
136auto getEmissionKind = [&]() {
137Instruction &I = *M->getFunction("f")->getEntryBlock().getFirstNonPHI();
138MDNode *LoopMD = I.getMetadata(LLVMContext::MD_loop);
139return cast<DILocation>(LoopMD->getOperand(1))
140->getScope()
141->getSubprogram()
142->getUnit()
143->getEmissionKind();
144};
145
146EXPECT_EQ(getEmissionKind(), DICompileUnit::FullDebug);
147
148bool Changed = stripNonLineTableDebugInfo(*M);
149EXPECT_TRUE(Changed);
150
151EXPECT_EQ(getEmissionKind(), DICompileUnit::LineTablesOnly);
152
153bool BrokenDebugInfo = false;
154bool HardError = verifyModule(*M, &errs(), &BrokenDebugInfo);
155EXPECT_FALSE(HardError);
156EXPECT_FALSE(BrokenDebugInfo);
157}
158
159TEST(MetadataTest, DeleteInstUsedByDbgRecord) {
160LLVMContext C;
161std::unique_ptr<Module> M = parseIR(C, R"(
162define i16 @f(i16 %a) !dbg !6 {
163%b = add i16 %a, 1, !dbg !11
164call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
165ret i16 0, !dbg !11
166}
167declare void @llvm.dbg.value(metadata, metadata, metadata) #0
168attributes #0 = { nounwind readnone speculatable willreturn }
169
170!llvm.dbg.cu = !{!0}
171!llvm.module.flags = !{!5}
172
173!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
174!1 = !DIFile(filename: "t.ll", directory: "/")
175!2 = !{}
176!5 = !{i32 2, !"Debug Info Version", i32 3}
177!6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
178!7 = !DISubroutineType(types: !2)
179!8 = !{!9}
180!9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
181!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
182!11 = !DILocation(line: 1, column: 1, scope: !6)
183)");
184
185// Find %b = add ...
186Instruction &I = *M->getFunction("f")->getEntryBlock().getFirstNonPHI();
187
188// Find the dbg.value using %b.
189SmallVector<DbgValueInst *, 1> DVIs;
190SmallVector<DbgVariableRecord *, 1> DVRs;
191findDbgValues(DVIs, &I, &DVRs);
192
193// Delete %b. The dbg.value should now point to undef.
194I.eraseFromParent();
195EXPECT_EQ(DVRs[0]->getNumVariableLocationOps(), 1u);
196EXPECT_TRUE(isa<UndefValue>(DVRs[0]->getValue(0)));
197}
198
199TEST(DbgVariableIntrinsic, EmptyMDIsKillLocation) {
200LLVMContext Ctx;
201std::unique_ptr<Module> M = parseIR(Ctx, R"(
202define dso_local void @fun() local_unnamed_addr #0 !dbg !9 {
203entry:
204call void @llvm.dbg.declare(metadata !{}, metadata !13, metadata !DIExpression()), !dbg !16
205ret void, !dbg !16
206}
207
208declare void @llvm.dbg.declare(metadata, metadata, metadata)
209
210!llvm.dbg.cu = !{!0}
211!llvm.module.flags = !{!2, !3}
212!llvm.ident = !{!8}
213
214!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 16.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
215!1 = !DIFile(filename: "test.c", directory: "/")
216!2 = !{i32 7, !"Dwarf Version", i32 5}
217!3 = !{i32 2, !"Debug Info Version", i32 3}
218!8 = !{!"clang version 16.0.0"}
219!9 = distinct !DISubprogram(name: "fun", scope: !1, file: !1, line: 1, type: !10, scopeLine: 1, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
220!10 = !DISubroutineType(types: !11)
221!11 = !{null}
222!12 = !{!13}
223!13 = !DILocalVariable(name: "a", scope: !9, file: !1, line: 1, type: !14)
224!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
225!16 = !DILocation(line: 1, column: 21, scope: !9)
226)");
227
228bool BrokenDebugInfo = true;
229verifyModule(*M, &errs(), &BrokenDebugInfo);
230ASSERT_FALSE(BrokenDebugInfo);
231
232// Get the dbg.declare.
233Function &F = *cast<Function>(M->getNamedValue("fun"));
234DbgVariableRecord *DbgDeclare =
235cast<DbgVariableRecord>(&*F.front().front().getDbgRecordRange().begin());
236// Check that this form counts as a "no location" marker.
237EXPECT_TRUE(DbgDeclare->isKillLocation());
238}
239
240// Duplicate of above test, but in DbgVariableRecord representation.
241TEST(MetadataTest, DeleteInstUsedByDbgVariableRecord) {
242LLVMContext C;
243bool OldDbgValueMode = UseNewDbgInfoFormat;
244UseNewDbgInfoFormat = true;
245
246std::unique_ptr<Module> M = parseIR(C, R"(
247define i16 @f(i16 %a) !dbg !6 {
248%b = add i16 %a, 1, !dbg !11
249call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
250call void @llvm.dbg.value(metadata !DIArgList(i16 %a, i16 %b), metadata !9, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus)), !dbg !11
251ret i16 0, !dbg !11
252}
253declare void @llvm.dbg.value(metadata, metadata, metadata) #0
254attributes #0 = { nounwind readnone speculatable willreturn }
255
256!llvm.dbg.cu = !{!0}
257!llvm.module.flags = !{!5}
258
259!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
260!1 = !DIFile(filename: "t.ll", directory: "/")
261!2 = !{}
262!5 = !{i32 2, !"Debug Info Version", i32 3}
263!6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
264!7 = !DISubroutineType(types: !2)
265!8 = !{!9}
266!9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
267!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
268!11 = !DILocation(line: 1, column: 1, scope: !6)
269)");
270
271Instruction &I = *M->getFunction("f")->getEntryBlock().getFirstNonPHI();
272
273// Find the DbgVariableRecords using %b.
274SmallVector<DbgValueInst *, 2> DVIs;
275SmallVector<DbgVariableRecord *, 2> DVRs;
276findDbgValues(DVIs, &I, &DVRs);
277ASSERT_EQ(DVRs.size(), 2u);
278
279// Delete %b. The DbgVariableRecord should now point to undef.
280I.eraseFromParent();
281EXPECT_EQ(DVRs[0]->getNumVariableLocationOps(), 1u);
282EXPECT_TRUE(isa<UndefValue>(DVRs[0]->getVariableLocationOp(0)));
283EXPECT_TRUE(DVRs[0]->isKillLocation());
284EXPECT_EQ(DVRs[1]->getNumVariableLocationOps(), 2u);
285EXPECT_TRUE(isa<UndefValue>(DVRs[1]->getVariableLocationOp(1)));
286EXPECT_TRUE(DVRs[1]->isKillLocation());
287UseNewDbgInfoFormat = OldDbgValueMode;
288}
289
290// Ensure that the order of dbg.value intrinsics returned by findDbgValues, and
291// their corresponding DbgVariableRecord representation, are consistent.
292TEST(MetadataTest, OrderingOfDbgVariableRecords) {
293bool OldDbgValueMode = UseNewDbgInfoFormat;
294UseNewDbgInfoFormat = false;
295LLVMContext C;
296std::unique_ptr<Module> M = parseIR(C, R"(
297define i16 @f(i16 %a) !dbg !6 {
298%b = add i16 %a, 1, !dbg !11
299call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
300call void @llvm.dbg.value(metadata i16 %b, metadata !12, metadata !DIExpression()), !dbg !11
301ret i16 0, !dbg !11
302}
303declare void @llvm.dbg.value(metadata, metadata, metadata) #0
304attributes #0 = { nounwind readnone speculatable willreturn }
305
306!llvm.dbg.cu = !{!0}
307!llvm.module.flags = !{!5}
308
309!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
310!1 = !DIFile(filename: "t.ll", directory: "/")
311!2 = !{}
312!5 = !{i32 2, !"Debug Info Version", i32 3}
313!6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
314!7 = !DISubroutineType(types: !2)
315!8 = !{!9}
316!9 = !DILocalVariable(name: "foo", scope: !6, file: !1, line: 1, type: !10)
317!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
318!11 = !DILocation(line: 1, column: 1, scope: !6)
319!12 = !DILocalVariable(name: "bar", scope: !6, file: !1, line: 1, type: !10)
320)");
321
322Instruction &I = *M->getFunction("f")->getEntryBlock().getFirstNonPHI();
323
324SmallVector<DbgValueInst *, 2> DVIs;
325SmallVector<DbgVariableRecord *, 2> DVRs;
326findDbgValues(DVIs, &I, &DVRs);
327ASSERT_EQ(DVIs.size(), 2u);
328ASSERT_EQ(DVRs.size(), 0u);
329
330// The correct order of dbg.values is given by their use-list, which becomes
331// the reverse order of creation. Thus the dbg.values should come out as
332// "bar" and then "foo".
333DILocalVariable *Var0 = DVIs[0]->getVariable();
334EXPECT_TRUE(Var0->getName() == "bar");
335DILocalVariable *Var1 = DVIs[1]->getVariable();
336EXPECT_TRUE(Var1->getName() == "foo");
337
338// Now try again, but in DbgVariableRecord form.
339DVIs.clear();
340
341M->convertToNewDbgValues();
342findDbgValues(DVIs, &I, &DVRs);
343ASSERT_EQ(DVIs.size(), 0u);
344ASSERT_EQ(DVRs.size(), 2u);
345
346Var0 = DVRs[0]->getVariable();
347EXPECT_TRUE(Var0->getName() == "bar");
348Var1 = DVRs[1]->getVariable();
349EXPECT_TRUE(Var1->getName() == "foo");
350
351M->convertFromNewDbgValues();
352UseNewDbgInfoFormat = OldDbgValueMode;
353}
354
355TEST(DIBuilder, CreateFile) {
356LLVMContext Ctx;
357std::unique_ptr<Module> M(new Module("MyModule", Ctx));
358DIBuilder DIB(*M);
359
360DIFile *F = DIB.createFile("main.c", "/");
361EXPECT_EQ(std::nullopt, F->getSource());
362
363std::optional<DIFile::ChecksumInfo<StringRef>> Checksum;
364std::optional<StringRef> Source;
365F = DIB.createFile("main.c", "/", Checksum, Source);
366EXPECT_EQ(Source, F->getSource());
367
368Source = "";
369F = DIB.createFile("main.c", "/", Checksum, Source);
370EXPECT_EQ(Source, F->getSource());
371}
372
373TEST(DIBuilder, CreateFortranArrayTypeWithAttributes) {
374LLVMContext Ctx;
375std::unique_ptr<Module> M(new Module("MyModule", Ctx));
376DIBuilder DIB(*M);
377
378DISubrange *Subrange = DIB.getOrCreateSubrange(1,1);
379SmallVector<Metadata*, 4> Subranges;
380Subranges.push_back(Subrange);
381DINodeArray Subscripts = DIB.getOrCreateArray(Subranges);
382
383auto getDIExpression = [&DIB](int offset) {
384SmallVector<uint64_t, 4> ops;
385ops.push_back(llvm::dwarf::DW_OP_push_object_address);
386DIExpression::appendOffset(ops, offset);
387ops.push_back(llvm::dwarf::DW_OP_deref);
388
389return DIB.createExpression(ops);
390};
391
392DIFile *F = DIB.createFile("main.c", "/");
393DICompileUnit *CU = DIB.createCompileUnit(
394dwarf::DW_LANG_C, DIB.createFile("main.c", "/"), "llvm-c", true, "", 0);
395
396DIVariable *DataLocation =
397DIB.createTempGlobalVariableFwdDecl(CU, "dl", "_dl", F, 1, nullptr, true);
398DIExpression *Associated = getDIExpression(1);
399DIExpression *Allocated = getDIExpression(2);
400DIExpression *Rank = DIB.createConstantValueExpression(3);
401
402DICompositeType *ArrayType = DIB.createArrayType(0, 0, nullptr, Subscripts,
403DataLocation, Associated,
404Allocated, Rank);
405
406EXPECT_TRUE(isa_and_nonnull<DICompositeType>(ArrayType));
407EXPECT_EQ(ArrayType->getRawDataLocation(), DataLocation);
408EXPECT_EQ(ArrayType->getRawAssociated(), Associated);
409EXPECT_EQ(ArrayType->getRawAllocated(), Allocated);
410EXPECT_EQ(ArrayType->getRawRank(), Rank);
411
412// Avoid memory leak.
413DIVariable::deleteTemporary(DataLocation);
414}
415
416TEST(DIBuilder, CreateSetType) {
417LLVMContext Ctx;
418std::unique_ptr<Module> M(new Module("MyModule", Ctx));
419DIBuilder DIB(*M);
420DIScope *Scope = DISubprogram::getDistinct(
421Ctx, nullptr, "", "", nullptr, 0, nullptr, 0, nullptr, 0, 0,
422DINode::FlagZero, DISubprogram::SPFlagZero, nullptr);
423DIType *Type = DIB.createBasicType("Int", 64, dwarf::DW_ATE_signed);
424DIFile *F = DIB.createFile("main.c", "/");
425
426DIDerivedType *SetType = DIB.createSetType(Scope, "set1", F, 1, 64, 64, Type);
427EXPECT_TRUE(isa_and_nonnull<DIDerivedType>(SetType));
428}
429
430TEST(DIBuilder, CreateStringType) {
431LLVMContext Ctx;
432std::unique_ptr<Module> M(new Module("MyModule", Ctx));
433DIBuilder DIB(*M);
434DIScope *Scope = DISubprogram::getDistinct(
435Ctx, nullptr, "", "", nullptr, 0, nullptr, 0, nullptr, 0, 0,
436DINode::FlagZero, DISubprogram::SPFlagZero, nullptr);
437DIFile *F = DIB.createFile("main.c", "/");
438StringRef StrName = "string";
439DIVariable *StringLen = DIB.createAutoVariable(Scope, StrName, F, 0, nullptr,
440false, DINode::FlagZero, 0);
441auto getDIExpression = [&DIB](int offset) {
442SmallVector<uint64_t, 4> ops;
443ops.push_back(llvm::dwarf::DW_OP_push_object_address);
444DIExpression::appendOffset(ops, offset);
445ops.push_back(llvm::dwarf::DW_OP_deref);
446
447return DIB.createExpression(ops);
448};
449DIExpression *StringLocationExp = getDIExpression(1);
450DIStringType *StringType =
451DIB.createStringType(StrName, StringLen, StringLocationExp);
452
453EXPECT_TRUE(isa_and_nonnull<DIStringType>(StringType));
454EXPECT_EQ(StringType->getName(), StrName);
455EXPECT_EQ(StringType->getStringLength(), StringLen);
456EXPECT_EQ(StringType->getStringLocationExp(), StringLocationExp);
457
458StringRef StrNameExp = "stringexp";
459DIExpression *StringLengthExp = getDIExpression(2);
460DIStringType *StringTypeExp =
461DIB.createStringType(StrNameExp, StringLengthExp, StringLocationExp);
462
463EXPECT_TRUE(isa_and_nonnull<DIStringType>(StringTypeExp));
464EXPECT_EQ(StringTypeExp->getName(), StrNameExp);
465EXPECT_EQ(StringTypeExp->getStringLocationExp(), StringLocationExp);
466EXPECT_EQ(StringTypeExp->getStringLengthExp(), StringLengthExp);
467}
468
469TEST(DIBuilder, DIEnumerator) {
470LLVMContext Ctx;
471std::unique_ptr<Module> M(new Module("MyModule", Ctx));
472DIBuilder DIB(*M);
473APSInt I1(APInt(32, 1));
474APSInt I2(APInt(33, 1));
475
476auto *E = DIEnumerator::get(Ctx, I1, I1.isSigned(), "name");
477EXPECT_TRUE(E);
478
479auto *E1 = DIEnumerator::getIfExists(Ctx, I1, I1.isSigned(), "name");
480EXPECT_TRUE(E1);
481
482auto *E2 = DIEnumerator::getIfExists(Ctx, I2, I1.isSigned(), "name");
483EXPECT_FALSE(E2);
484}
485
486TEST(DbgAssignIntrinsicTest, replaceVariableLocationOp) {
487LLVMContext C;
488std::unique_ptr<Module> M = parseIR(C, R"(
489define dso_local void @fun(i32 %v1, ptr %p1, ptr %p2) !dbg !7 {
490entry:
491call void @llvm.dbg.assign(metadata i32 %v1, metadata !14, metadata !DIExpression(), metadata !17, metadata ptr %p1, metadata !DIExpression()), !dbg !16
492ret void
493}
494
495declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
496
497!llvm.dbg.cu = !{!0}
498!llvm.module.flags = !{!3}
499
500!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 14.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
501!1 = !DIFile(filename: "test.cpp", directory: "/")
502!3 = !{i32 2, !"Debug Info Version", i32 3}
503!7 = distinct !DISubprogram(name: "fun", linkageName: "fun", scope: !1, file: !1, line: 2, type: !8, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
504!8 = !DISubroutineType(types: !9)
505!9 = !{null}
506!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
507!11 = !{}
508!14 = !DILocalVariable(name: "Local", scope: !7, file: !1, line: 3, type: !10)
509!16 = !DILocation(line: 0, scope: !7)
510!17 = distinct !DIAssignID()
511)");
512// Check the test IR isn't malformed.
513ASSERT_TRUE(M);
514
515Function &Fun = *M->getFunction("fun");
516Value *V1 = Fun.getArg(0);
517Value *P1 = Fun.getArg(1);
518Value *P2 = Fun.getArg(2);
519DbgVariableRecord *DbgAssign = cast<DbgVariableRecord>(
520&*Fun.front().front().getDbgRecordRange().begin());
521ASSERT_TRUE(V1 == DbgAssign->getVariableLocationOp(0));
522ASSERT_TRUE(P1 == DbgAssign->getAddress());
523
524#define TEST_REPLACE(Old, New, ExpectedValue, ExpectedAddr) \
525DbgAssign->replaceVariableLocationOp(Old, New); \
526EXPECT_EQ(DbgAssign->getVariableLocationOp(0), ExpectedValue); \
527EXPECT_EQ(DbgAssign->getAddress(), ExpectedAddr);
528
529// Replace address only.
530TEST_REPLACE(/*Old*/ P1, /*New*/ P2, /*Value*/ V1, /*Address*/ P2);
531// Replace value only.
532TEST_REPLACE(/*Old*/ V1, /*New*/ P2, /*Value*/ P2, /*Address*/ P2);
533// Replace both.
534TEST_REPLACE(/*Old*/ P2, /*New*/ P1, /*Value*/ P1, /*Address*/ P1);
535
536// Replace address only, value uses a DIArgList.
537// Value = {DIArgList(V1)}, Addr = P1.
538DbgAssign->setRawLocation(DIArgList::get(C, ValueAsMetadata::get(V1)));
539DbgAssign->setExpression(DIExpression::get(
540C, {dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_stack_value}));
541TEST_REPLACE(/*Old*/ P1, /*New*/ P2, /*Value*/ V1, /*Address*/ P2);
542#undef TEST_REPLACE
543}
544
545TEST(AssignmentTrackingTest, Utils) {
546// Test the assignment tracking utils defined in DebugInfo.h namespace at {}.
547// This includes:
548// getAssignmentInsts
549// getAssignmentMarkers
550// RAUW
551// deleteAll
552//
553// The input IR includes two functions, fun1 and fun2. Both contain an alloca
554// with a DIAssignID tag. fun1's alloca is linked to two llvm.dbg.assign
555// intrinsics, one of which is for an inlined variable and appears before the
556// alloca.
557
558LLVMContext C;
559std::unique_ptr<Module> M = parseIR(C, R"(
560define dso_local void @fun1() !dbg !7 {
561entry:
562call void @llvm.dbg.assign(metadata i32 undef, metadata !10, metadata !DIExpression(), metadata !12, metadata i32 undef, metadata !DIExpression()), !dbg !13
563%local = alloca i32, align 4, !DIAssignID !12
564call void @llvm.dbg.assign(metadata i32 undef, metadata !16, metadata !DIExpression(), metadata !12, metadata i32 undef, metadata !DIExpression()), !dbg !15
565ret void, !dbg !15
566}
567
568define dso_local void @fun2() !dbg !17 {
569entry:
570%local = alloca i32, align 4, !DIAssignID !20
571call void @llvm.dbg.assign(metadata i32 undef, metadata !18, metadata !DIExpression(), metadata !20, metadata i32 undef, metadata !DIExpression()), !dbg !19
572ret void, !dbg !19
573}
574
575define dso_local void @fun3() !dbg !21 {
576entry:
577%local = alloca i32, align 4, !DIAssignID !24
578call void @llvm.dbg.assign(metadata i32 undef, metadata !22, metadata !DIExpression(), metadata !24, metadata i32* undef, metadata !DIExpression()), !dbg !23
579ret void
580}
581
582declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
583
584!llvm.dbg.cu = !{!0}
585!llvm.module.flags = !{!3, !4, !5}
586!llvm.ident = !{!6}
587
588!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 14.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
589!1 = !DIFile(filename: "test.c", directory: "/")
590!2 = !{}
591!3 = !{i32 7, !"Dwarf Version", i32 4}
592!4 = !{i32 2, !"Debug Info Version", i32 3}
593!5 = !{i32 1, !"wchar_size", i32 4}
594!6 = !{!"clang version 14.0.0"}
595!7 = distinct !DISubprogram(name: "fun1", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
596!8 = !DISubroutineType(types: !9)
597!9 = !{null}
598!10 = !DILocalVariable(name: "local3", scope: !14, file: !1, line: 2, type: !11)
599!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
600!12 = distinct !DIAssignID()
601!13 = !DILocation(line: 5, column: 1, scope: !14, inlinedAt: !15)
602!14 = distinct !DISubprogram(name: "inline", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
603!15 = !DILocation(line: 3, column: 1, scope: !7)
604!16 = !DILocalVariable(name: "local1", scope: !7, file: !1, line: 2, type: !11)
605!17 = distinct !DISubprogram(name: "fun2", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
606!18 = !DILocalVariable(name: "local2", scope: !17, file: !1, line: 2, type: !11)
607!19 = !DILocation(line: 4, column: 1, scope: !17)
608!20 = distinct !DIAssignID()
609!21 = distinct !DISubprogram(name: "fun3", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
610!22 = !DILocalVariable(name: "local4", scope: !21, file: !1, line: 2, type: !11)
611!23 = !DILocation(line: 4, column: 1, scope: !21)
612!24 = distinct !DIAssignID()
613)");
614
615// Check the test IR isn't malformed.
616ASSERT_TRUE(M);
617
618Function &Fun1 = *M->getFunction("fun1");
619Instruction &Alloca = *Fun1.getEntryBlock().getFirstNonPHIOrDbg();
620
621// 1. Check the Instruction <-> Intrinsic mappings work in fun1.
622//
623// Check there are two llvm.dbg.assign intrinsics linked to Alloca.
624auto CheckFun1Mapping = [&Alloca]() {
625auto Markers = at::getDVRAssignmentMarkers(&Alloca);
626EXPECT_TRUE(std::distance(Markers.begin(), Markers.end()) == 2);
627// Check those two entries are distinct.
628DbgVariableRecord *First = *Markers.begin();
629DbgVariableRecord *Second = *std::next(Markers.begin());
630EXPECT_NE(First, Second);
631
632// Check that we can get back to Alloca from each llvm.dbg.assign.
633for (auto *DAI : Markers) {
634auto Insts = at::getAssignmentInsts(DAI);
635// Check there is exactly one instruction linked to each intrinsic. Use
636// ASSERT_TRUE because we're going to dereference the begin iterator.
637ASSERT_TRUE(std::distance(Insts.begin(), Insts.end()) == 1);
638EXPECT_FALSE(Insts.empty());
639// Check the linked instruction is Alloca.
640Instruction *LinkedInst = *Insts.begin();
641EXPECT_EQ(LinkedInst, &Alloca);
642}
643};
644CheckFun1Mapping();
645
646// 2. Check DIAssignID RAUW replaces attachments and uses.
647//
648DIAssignID *Old =
649cast_or_null<DIAssignID>(Alloca.getMetadata(LLVMContext::MD_DIAssignID));
650DIAssignID *New = DIAssignID::getDistinct(C);
651ASSERT_TRUE(Old && New && New != Old);
652at::RAUW(Old, New);
653// Check fun1's alloca and intrinsics have been updated and the mapping still
654// works.
655EXPECT_EQ(New, cast_or_null<DIAssignID>(
656Alloca.getMetadata(LLVMContext::MD_DIAssignID)));
657CheckFun1Mapping();
658
659// Check that fun2's alloca and intrinsic have not not been updated.
660Instruction &Fun2Alloca =
661*M->getFunction("fun2")->getEntryBlock().getFirstNonPHIOrDbg();
662DIAssignID *Fun2ID = cast_or_null<DIAssignID>(
663Fun2Alloca.getMetadata(LLVMContext::MD_DIAssignID));
664EXPECT_NE(New, Fun2ID);
665auto Fun2Markers = at::getDVRAssignmentMarkers(&Fun2Alloca);
666ASSERT_TRUE(std::distance(Fun2Markers.begin(), Fun2Markers.end()) == 1);
667auto Fun2Insts = at::getAssignmentInsts(*Fun2Markers.begin());
668ASSERT_TRUE(std::distance(Fun2Insts.begin(), Fun2Insts.end()) == 1);
669EXPECT_EQ(*Fun2Insts.begin(), &Fun2Alloca);
670
671// 3. Check that deleting dbg.assigns from a specific instruction works.
672Instruction &Fun3Alloca =
673*M->getFunction("fun3")->getEntryBlock().getFirstNonPHIOrDbg();
674auto Fun3Markers = at::getDVRAssignmentMarkers(&Fun3Alloca);
675ASSERT_TRUE(std::distance(Fun3Markers.begin(), Fun3Markers.end()) == 1);
676at::deleteAssignmentMarkers(&Fun3Alloca);
677Fun3Markers = at::getDVRAssignmentMarkers(&Fun3Alloca);
678EXPECT_EQ(Fun3Markers.empty(), true);
679
680// 4. Check that deleting works and applies only to the target function.
681at::deleteAll(&Fun1);
682// There should now only be the alloca and ret in fun1.
683EXPECT_EQ(Fun1.begin()->size(), 2u);
684// fun2's alloca should have the same DIAssignID and remain linked to its
685// llvm.dbg.assign.
686EXPECT_EQ(Fun2ID, cast_or_null<DIAssignID>(
687Fun2Alloca.getMetadata(LLVMContext::MD_DIAssignID)));
688EXPECT_FALSE(at::getDVRAssignmentMarkers(&Fun2Alloca).empty());
689}
690
691TEST(IRBuilder, GetSetInsertionPointWithEmptyBasicBlock) {
692LLVMContext C;
693std::unique_ptr<BasicBlock> BB(BasicBlock::Create(C, "start"));
694Module *M = new Module("module", C);
695IRBuilder<> Builder(BB.get());
696Function *DbgDeclare = Intrinsic::getDeclaration(M, Intrinsic::dbg_declare);
697Value *DIV = MetadataAsValue::get(C, (Metadata *)nullptr);
698SmallVector<Value *, 3> Args = {DIV, DIV, DIV};
699Builder.CreateCall(DbgDeclare, Args);
700auto IP = BB->getFirstInsertionPt();
701Builder.SetInsertPoint(BB.get(), IP);
702}
703
704TEST(AssignmentTrackingTest, InstrMethods) {
705// Test the assignment tracking Instruction methods.
706// This includes:
707// Instruction::mergeDIAssignID
708
709LLVMContext C;
710std::unique_ptr<Module> M = parseIR(C, R"(
711define dso_local void @fun() #0 !dbg !8 {
712entry:
713%Local = alloca [2 x i32], align 4, !DIAssignID !12
714call void @llvm.dbg.assign(metadata i1 undef, metadata !13, metadata !DIExpression(), metadata !12, metadata [2 x i32]* %Local, metadata !DIExpression()), !dbg !18
715%arrayidx = getelementptr inbounds [2 x i32], [2 x i32]* %Local, i64 0, i64 0, !dbg !19
716store i32 5, i32* %arrayidx, align 4, !dbg !20, !DIAssignID !21
717call void @llvm.dbg.assign(metadata i32 5, metadata !13, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 32), metadata !21, metadata i32* %arrayidx, metadata !DIExpression()), !dbg !18
718%arrayidx1 = getelementptr inbounds [2 x i32], [2 x i32]* %Local, i64 0, i64 1, !dbg !22
719store i32 6, i32* %arrayidx1, align 4, !dbg !23, !DIAssignID !24
720call void @llvm.dbg.assign(metadata i32 6, metadata !13, metadata !DIExpression(DW_OP_LLVM_fragment, 32, 32), metadata !24, metadata i32* %arrayidx1, metadata !DIExpression()), !dbg !18
721ret void, !dbg !25
722}
723
724declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata) #1
725
726!llvm.dbg.cu = !{!0}
727!llvm.module.flags = !{!2, !3, !4, !5, !6}
728!llvm.ident = !{!7}
729
730!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 14.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
731!1 = !DIFile(filename: "test.cpp", directory: "/")
732!2 = !{i32 7, !"Dwarf Version", i32 5}
733!3 = !{i32 2, !"Debug Info Version", i32 3}
734!4 = !{i32 1, !"wchar_size", i32 4}
735!5 = !{i32 7, !"uwtable", i32 1}
736!6 = !{i32 7, !"frame-pointer", i32 2}
737!7 = !{!"clang version 14.0.0"}
738!8 = distinct !DISubprogram(name: "fun", linkageName: "fun", scope: !1, file: !1, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !11)
739!9 = !DISubroutineType(types: !10)
740!10 = !{null}
741!11 = !{}
742!12 = distinct !DIAssignID()
743!13 = !DILocalVariable(name: "Local", scope: !8, file: !1, line: 2, type: !14)
744!14 = !DICompositeType(tag: DW_TAG_array_type, baseType: !15, size: 64, elements: !16)
745!15 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
746!16 = !{!17}
747!17 = !DISubrange(count: 2)
748!18 = !DILocation(line: 0, scope: !8)
749!19 = !DILocation(line: 3, column: 3, scope: !8)
750!20 = !DILocation(line: 3, column: 12, scope: !8)
751!21 = distinct !DIAssignID()
752!22 = !DILocation(line: 4, column: 3, scope: !8)
753!23 = !DILocation(line: 4, column: 12, scope: !8)
754!24 = distinct !DIAssignID()
755!25 = !DILocation(line: 5, column: 1, scope: !8)
756)");
757
758// Check the test IR isn't malformed.
759ASSERT_TRUE(M);
760Function &Fun = *M->getFunction("fun");
761SmallVector<Instruction *> Stores;
762for (auto &BB : Fun) {
763for (auto &I : BB) {
764if (isa<StoreInst>(&I))
765Stores.push_back(&I);
766}
767}
768
769// The test requires (at least) 2 stores.
770ASSERT_TRUE(Stores.size() == 2);
771// Use SetVectors to check that the attachments and markers are unique
772// (another test requirement).
773SetVector<Metadata *> OrigIDs;
774SetVector<DbgVariableRecord *> Markers;
775for (const Instruction *SI : Stores) {
776Metadata *ID = SI->getMetadata(LLVMContext::MD_DIAssignID);
777ASSERT_TRUE(OrigIDs.insert(ID));
778ASSERT_TRUE(ID != nullptr);
779auto Range = at::getDVRAssignmentMarkers(SI);
780ASSERT_TRUE(std::distance(Range.begin(), Range.end()) == 1);
781ASSERT_TRUE(Markers.insert(*Range.begin()));
782}
783
784// Test 1 - mergeDIAssignID.
785//
786// Input store0->mergeDIAssignID(store1)
787// ----- -------------------------
788// store0 !x store0 !x
789// dbg.assign0 !x dbg.assign !x
790// store1 !y store1 !x
791// dbg.assign1 !y dbg.assign1 !x
792{
793Stores[0]->mergeDIAssignID(Stores[1]);
794// Check that the stores share the same ID.
795Metadata *NewID0 = Stores[0]->getMetadata(LLVMContext::MD_DIAssignID);
796Metadata *NewID1 = Stores[1]->getMetadata(LLVMContext::MD_DIAssignID);
797EXPECT_NE(NewID0, nullptr);
798EXPECT_EQ(NewID0, NewID1);
799EXPECT_EQ(Markers[0]->getAssignID(), NewID0);
800EXPECT_EQ(Markers[1]->getAssignID(), NewID0);
801}
802
803// Test 2 - mergeDIAssignID.
804//
805// Input store0->mergeDIAssignID(store1)
806// ----- -------------------------
807// store0 !x store0 !x
808// dbg.assign0 !x dbg.assign !x
809// store1 store1
810{
811Stores[1]->setMetadata(LLVMContext::MD_DIAssignID, nullptr);
812Stores[0]->mergeDIAssignID(Stores[1]);
813// Check that store1 doesn't get a new ID.
814Metadata *NewID0 = Stores[0]->getMetadata(LLVMContext::MD_DIAssignID);
815Metadata *NewID1 = Stores[1]->getMetadata(LLVMContext::MD_DIAssignID);
816EXPECT_NE(NewID0, nullptr);
817EXPECT_EQ(NewID1, nullptr);
818EXPECT_EQ(Markers[0]->getAssignID(), NewID0);
819}
820
821// Test 3 - mergeDIAssignID.
822//
823// Input store1->mergeDIAssignID(store0)
824// ----- -------------------------
825// store0 !x store0 !x
826// dbg.assign0 !x dbg.assign !x
827// store1 store1 !x
828{
829Stores[1]->setMetadata(LLVMContext::MD_DIAssignID, nullptr);
830Stores[1]->mergeDIAssignID(Stores[0]);
831// Check that the stores share the same ID (note store1 starts with none).
832Metadata *NewID0 = Stores[0]->getMetadata(LLVMContext::MD_DIAssignID);
833Metadata *NewID1 = Stores[1]->getMetadata(LLVMContext::MD_DIAssignID);
834EXPECT_NE(NewID0, nullptr);
835EXPECT_EQ(NewID0, NewID1);
836EXPECT_EQ(Markers[0]->getAssignID(), NewID0);
837}
838
839// Test 4 - mergeDIAssignID.
840//
841// Input store1->mergeDIAssignID(store0)
842// ----- -------------------------
843// store0 !x store0 !x
844// dbg.assign0 !x dbg.assign !x
845// store1 !x store1 !x
846{
847Stores[0]->mergeDIAssignID(Stores[1]);
848// Check that the stores share the same ID.
849Metadata *NewID0 = Stores[0]->getMetadata(LLVMContext::MD_DIAssignID);
850Metadata *NewID1 = Stores[1]->getMetadata(LLVMContext::MD_DIAssignID);
851EXPECT_NE(NewID0, nullptr);
852EXPECT_EQ(NewID0, NewID1);
853EXPECT_EQ(Markers[0]->getAssignID(), NewID0);
854}
855
856// Test 5 - dropUnknownNonDebugMetadata.
857//
858// Input store0->dropUnknownNonDebugMetadata()
859// ----- -------------------------
860// store0 !x store0 !x
861{
862Stores[0]->dropUnknownNonDebugMetadata();
863Metadata *NewID0 = Stores[0]->getMetadata(LLVMContext::MD_DIAssignID);
864EXPECT_NE(NewID0, nullptr);
865}
866}
867
868// Test some very straight-forward operations on DbgVariableRecords -- these are
869// dbg.values that have been converted to a non-instruction format.
870TEST(MetadataTest, ConvertDbgToDbgVariableRecord) {
871LLVMContext C;
872bool OldDbgValueMode = UseNewDbgInfoFormat;
873UseNewDbgInfoFormat = false;
874std::unique_ptr<Module> M = parseIR(C, R"(
875define i16 @f(i16 %a) !dbg !6 {
876call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
877%b = add i16 %a, 1, !dbg !11
878call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
879ret i16 0, !dbg !11
880
881exit:
882%c = add i16 %b, 1, !dbg !11
883ret i16 0, !dbg !11
884}
885declare void @llvm.dbg.value(metadata, metadata, metadata) #0
886attributes #0 = { nounwind readnone speculatable willreturn }
887
888!llvm.dbg.cu = !{!0}
889!llvm.module.flags = !{!5}
890
891!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
892!1 = !DIFile(filename: "t.ll", directory: "/")
893!2 = !{}
894!5 = !{i32 2, !"Debug Info Version", i32 3}
895!6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
896!7 = !DISubroutineType(types: !2)
897!8 = !{!9}
898!9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
899!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
900!11 = !DILocation(line: 1, column: 1, scope: !6)
901)");
902
903// Find the first dbg.value,
904Instruction &I = *M->getFunction("f")->getEntryBlock().getFirstNonPHI();
905const DILocalVariable *Var = nullptr;
906const DIExpression *Expr = nullptr;
907const DILocation *Loc = nullptr;
908const Metadata *MLoc = nullptr;
909DbgVariableRecord *DVR1 = nullptr;
910{
911DbgValueInst *DPI = dyn_cast<DbgValueInst>(&I);
912ASSERT_TRUE(DPI);
913Var = DPI->getVariable();
914Expr = DPI->getExpression();
915Loc = DPI->getDebugLoc().get();
916MLoc = DPI->getRawLocation();
917
918// Test the creation of a DbgVariableRecord and it's conversion back to a
919// dbg.value.
920DVR1 = new DbgVariableRecord(DPI);
921EXPECT_EQ(DVR1->getVariable(), Var);
922EXPECT_EQ(DVR1->getExpression(), Expr);
923EXPECT_EQ(DVR1->getDebugLoc().get(), Loc);
924EXPECT_EQ(DVR1->getRawLocation(), MLoc);
925
926// Erase dbg.value,
927DPI->eraseFromParent();
928// Re-create from DVR1, inserting at front.
929DVR1->createDebugIntrinsic(&*M,
930&M->getFunction("f")->getEntryBlock().front());
931
932Instruction *NewDPI = &M->getFunction("f")->getEntryBlock().front();
933DbgValueInst *DPI2 = dyn_cast<DbgValueInst>(NewDPI);
934ASSERT_TRUE(DPI2);
935EXPECT_EQ(DPI2->getVariable(), Var);
936EXPECT_EQ(DPI2->getExpression(), Expr);
937EXPECT_EQ(DPI2->getDebugLoc().get(), Loc);
938EXPECT_EQ(DPI2->getRawLocation(), MLoc);
939}
940
941// Fetch the second dbg.value, convert it to a DbgVariableRecord,
942BasicBlock::iterator It = M->getFunction("f")->getEntryBlock().begin();
943It = std::next(std::next(It));
944DbgValueInst *DPI3 = dyn_cast<DbgValueInst>(It);
945ASSERT_TRUE(DPI3);
946DbgVariableRecord *DVR2 = new DbgVariableRecord(DPI3);
947
948// These dbg.values are supposed to refer to different values.
949EXPECT_NE(DVR1->getRawLocation(), DVR2->getRawLocation());
950
951// Try manipulating DbgVariableRecords and markers in the exit block.
952BasicBlock *ExitBlock = &*std::next(M->getFunction("f")->getEntryBlock().getIterator());
953Instruction *FirstInst = &ExitBlock->front();
954Instruction *RetInst = &*std::next(FirstInst->getIterator());
955
956// Set-up DbgMarkers in this block.
957ExitBlock->IsNewDbgInfoFormat = true;
958ExitBlock->createMarker(FirstInst);
959ExitBlock->createMarker(RetInst);
960
961// Insert DbgRecords into markers, order should come out DVR2, DVR1.
962FirstInst->DebugMarker->insertDbgRecord(DVR1, false);
963FirstInst->DebugMarker->insertDbgRecord(DVR2, true);
964unsigned int ItCount = 0;
965for (DbgRecord &Item : FirstInst->DebugMarker->getDbgRecordRange()) {
966EXPECT_TRUE((&Item == DVR2 && ItCount == 0) ||
967(&Item == DVR1 && ItCount == 1));
968EXPECT_EQ(Item.getMarker(), FirstInst->DebugMarker);
969++ItCount;
970}
971
972// Clone them onto the second marker -- should allocate new DVRs.
973RetInst->DebugMarker->cloneDebugInfoFrom(FirstInst->DebugMarker, std::nullopt,
974false);
975EXPECT_EQ(RetInst->DebugMarker->StoredDbgRecords.size(), 2u);
976ItCount = 0;
977// Check these things store the same information; but that they're not the same
978// objects.
979for (DbgVariableRecord &Item :
980filterDbgVars(RetInst->DebugMarker->getDbgRecordRange())) {
981EXPECT_TRUE(
982(Item.getRawLocation() == DVR2->getRawLocation() && ItCount == 0) ||
983(Item.getRawLocation() == DVR1->getRawLocation() && ItCount == 1));
984
985EXPECT_EQ(Item.getMarker(), RetInst->DebugMarker);
986EXPECT_NE(&Item, DVR1);
987EXPECT_NE(&Item, DVR2);
988++ItCount;
989}
990
991RetInst->DebugMarker->dropDbgRecords();
992EXPECT_EQ(RetInst->DebugMarker->StoredDbgRecords.size(), 0u);
993
994// Try cloning one single DbgVariableRecord.
995auto DIIt = std::next(FirstInst->DebugMarker->getDbgRecordRange().begin());
996RetInst->DebugMarker->cloneDebugInfoFrom(FirstInst->DebugMarker, DIIt, false);
997EXPECT_EQ(RetInst->DebugMarker->StoredDbgRecords.size(), 1u);
998// The second DbgVariableRecord should have been cloned; it should have the
999// same values as DVR1.
1000EXPECT_EQ(
1001cast<DbgVariableRecord>(RetInst->DebugMarker->StoredDbgRecords.begin())
1002->getRawLocation(),
1003DVR1->getRawLocation());
1004// We should be able to drop individual DbgRecords.
1005RetInst->DebugMarker->dropOneDbgRecord(
1006&*RetInst->DebugMarker->StoredDbgRecords.begin());
1007
1008// "Aborb" a DbgMarker: this means pretend that the instruction it's attached
1009// to is disappearing so it needs to be transferred into "this" marker.
1010RetInst->DebugMarker->absorbDebugValues(*FirstInst->DebugMarker, true);
1011EXPECT_EQ(RetInst->DebugMarker->StoredDbgRecords.size(), 2u);
1012// Should be the DVR1 and DVR2 objects.
1013ItCount = 0;
1014for (DbgRecord &Item : RetInst->DebugMarker->getDbgRecordRange()) {
1015EXPECT_TRUE((&Item == DVR2 && ItCount == 0) ||
1016(&Item == DVR1 && ItCount == 1));
1017EXPECT_EQ(Item.getMarker(), RetInst->DebugMarker);
1018++ItCount;
1019}
1020
1021// Finally -- there are two DbgVariableRecords left over. If we remove
1022// evrything in the basic block, then they should sink down into the
1023// "TrailingDbgRecords" container for dangling debug-info. Future facilities
1024// will restore them back when a terminator is inserted.
1025FirstInst->DebugMarker->removeMarker();
1026FirstInst->eraseFromParent();
1027RetInst->DebugMarker->removeMarker();
1028RetInst->eraseFromParent();
1029
1030DbgMarker *EndMarker = ExitBlock->getTrailingDbgRecords();
1031ASSERT_NE(EndMarker, nullptr);
1032EXPECT_EQ(EndMarker->StoredDbgRecords.size(), 2u);
1033// Test again that it's those two DbgVariableRecords, DVR1 and DVR2.
1034ItCount = 0;
1035for (DbgRecord &Item : EndMarker->getDbgRecordRange()) {
1036EXPECT_TRUE((&Item == DVR2 && ItCount == 0) ||
1037(&Item == DVR1 && ItCount == 1));
1038EXPECT_EQ(Item.getMarker(), EndMarker);
1039++ItCount;
1040}
1041
1042// Cleanup the trailing DbgVariableRecord records and marker.
1043EndMarker->eraseFromParent();
1044
1045// The record of those trailing DbgVariableRecords would dangle and cause an
1046// assertion failure if it lived until the end of the LLVMContext.
1047ExitBlock->deleteTrailingDbgRecords();
1048UseNewDbgInfoFormat = OldDbgValueMode;
1049}
1050
1051TEST(MetadataTest, DbgVariableRecordConversionRoutines) {
1052LLVMContext C;
1053
1054bool OldDbgValueMode = UseNewDbgInfoFormat;
1055UseNewDbgInfoFormat = false;
1056
1057std::unique_ptr<Module> M = parseIR(C, R"(
1058define i16 @f(i16 %a) !dbg !6 {
1059call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
1060%b = add i16 %a, 1, !dbg !11
1061call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
1062ret i16 0, !dbg !11
1063
1064exit:
1065%c = add i16 %b, 1, !dbg !11
1066ret i16 0, !dbg !11
1067}
1068declare void @llvm.dbg.value(metadata, metadata, metadata) #0
1069attributes #0 = { nounwind readnone speculatable willreturn }
1070
1071!llvm.dbg.cu = !{!0}
1072!llvm.module.flags = !{!5}
1073
1074!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1075!1 = !DIFile(filename: "t.ll", directory: "/")
1076!2 = !{}
1077!5 = !{i32 2, !"Debug Info Version", i32 3}
1078!6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
1079!7 = !DISubroutineType(types: !2)
1080!8 = !{!9}
1081!9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
1082!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
1083!11 = !DILocation(line: 1, column: 1, scope: !6)
1084)");
1085
1086// For the purpose of this test, set and un-set the command line option
1087// corresponding to UseNewDbgInfoFormat, but only after parsing, to ensure
1088// that the IR starts off in the old format.
1089UseNewDbgInfoFormat = true;
1090
1091// Check that the conversion routines and utilities between dbg.value
1092// debug-info format and DbgVariableRecords works.
1093Function *F = M->getFunction("f");
1094BasicBlock *BB1 = &F->getEntryBlock();
1095// First instruction should be a dbg.value.
1096EXPECT_TRUE(isa<DbgValueInst>(BB1->front()));
1097EXPECT_FALSE(BB1->IsNewDbgInfoFormat);
1098// Validating the block for DbgVariableRecords / DbgMarkers shouldn't fail --
1099// there's no data stored right now.
1100bool BrokenDebugInfo = false;
1101bool Error = verifyModule(*M, &errs(), &BrokenDebugInfo);
1102EXPECT_FALSE(Error);
1103EXPECT_FALSE(BrokenDebugInfo);
1104
1105// Function and module should be marked as not having the new format too.
1106EXPECT_FALSE(F->IsNewDbgInfoFormat);
1107EXPECT_FALSE(M->IsNewDbgInfoFormat);
1108
1109// Now convert.
1110M->convertToNewDbgValues();
1111EXPECT_TRUE(M->IsNewDbgInfoFormat);
1112EXPECT_TRUE(F->IsNewDbgInfoFormat);
1113EXPECT_TRUE(BB1->IsNewDbgInfoFormat);
1114
1115// There should now be no dbg.value instructions!
1116// Ensure the first instruction exists, the test all of them.
1117EXPECT_FALSE(isa<DbgValueInst>(BB1->front()));
1118for (auto &BB : *F)
1119for (auto &I : BB)
1120EXPECT_FALSE(isa<DbgValueInst>(I));
1121
1122// There should be a DbgMarker on each of the two instructions in the entry
1123// block, each containing one DbgVariableRecord.
1124EXPECT_EQ(BB1->size(), 2u);
1125Instruction *FirstInst = &BB1->front();
1126Instruction *SecondInst = FirstInst->getNextNode();
1127ASSERT_TRUE(FirstInst->DebugMarker);
1128ASSERT_TRUE(SecondInst->DebugMarker);
1129EXPECT_NE(FirstInst->DebugMarker, SecondInst->DebugMarker);
1130EXPECT_EQ(FirstInst, FirstInst->DebugMarker->MarkedInstr);
1131EXPECT_EQ(SecondInst, SecondInst->DebugMarker->MarkedInstr);
1132
1133EXPECT_EQ(FirstInst->DebugMarker->StoredDbgRecords.size(), 1u);
1134DbgVariableRecord *DVR1 = cast<DbgVariableRecord>(
1135&*FirstInst->DebugMarker->getDbgRecordRange().begin());
1136EXPECT_EQ(DVR1->getMarker(), FirstInst->DebugMarker);
1137// Should point at %a, an argument.
1138EXPECT_TRUE(isa<Argument>(DVR1->getVariableLocationOp(0)));
1139
1140EXPECT_EQ(SecondInst->DebugMarker->StoredDbgRecords.size(), 1u);
1141DbgVariableRecord *DVR2 = cast<DbgVariableRecord>(
1142&*SecondInst->DebugMarker->getDbgRecordRange().begin());
1143EXPECT_EQ(DVR2->getMarker(), SecondInst->DebugMarker);
1144// Should point at FirstInst.
1145EXPECT_EQ(DVR2->getVariableLocationOp(0), FirstInst);
1146
1147// There should be no DbgVariableRecords / DbgMarkers in the second block, but
1148// it should be marked as being in the new format.
1149BasicBlock *BB2 = BB1->getNextNode();
1150EXPECT_TRUE(BB2->IsNewDbgInfoFormat);
1151for (auto &Inst : *BB2)
1152// Either there should be no marker, or it should be empty.
1153EXPECT_TRUE(!Inst.DebugMarker ||
1154Inst.DebugMarker->StoredDbgRecords.empty());
1155
1156// Validating the first block should continue to not be a problem,
1157Error = verifyModule(*M, &errs(), &BrokenDebugInfo);
1158EXPECT_FALSE(Error);
1159EXPECT_FALSE(BrokenDebugInfo);
1160// But if we were to break something, it should be able to fire. Don't attempt
1161// to comprehensively test the validator, it's a smoke-test rather than a
1162// "proper" verification pass.
1163DVR1->setMarker(nullptr);
1164// A marker pointing the wrong way should be an error.
1165Error = verifyModule(*M, &errs(), &BrokenDebugInfo);
1166EXPECT_FALSE(Error);
1167EXPECT_TRUE(BrokenDebugInfo);
1168DVR1->setMarker(FirstInst->DebugMarker);
1169
1170DILocalVariable *DLV1 = DVR1->getVariable();
1171DIExpression *Expr1 = DVR1->getExpression();
1172DILocalVariable *DLV2 = DVR2->getVariable();
1173DIExpression *Expr2 = DVR2->getExpression();
1174
1175// Convert everything back to the "old" format and ensure it's right.
1176M->convertFromNewDbgValues();
1177EXPECT_FALSE(M->IsNewDbgInfoFormat);
1178EXPECT_FALSE(F->IsNewDbgInfoFormat);
1179EXPECT_FALSE(BB1->IsNewDbgInfoFormat);
1180
1181EXPECT_EQ(BB1->size(), 4u);
1182ASSERT_TRUE(isa<DbgValueInst>(BB1->front()));
1183DbgValueInst *DVI1 = cast<DbgValueInst>(&BB1->front());
1184// These dbg.values should still point at the same places.
1185EXPECT_TRUE(isa<Argument>(DVI1->getVariableLocationOp(0)));
1186DbgValueInst *DVI2 = cast<DbgValueInst>(DVI1->getNextNode()->getNextNode());
1187EXPECT_EQ(DVI2->getVariableLocationOp(0), FirstInst);
1188
1189// Check a few fields too,
1190EXPECT_EQ(DVI1->getVariable(), DLV1);
1191EXPECT_EQ(DVI1->getExpression(), Expr1);
1192EXPECT_EQ(DVI2->getVariable(), DLV2);
1193EXPECT_EQ(DVI2->getExpression(), Expr2);
1194
1195UseNewDbgInfoFormat = OldDbgValueMode;
1196}
1197
1198// Test that the hashing function for DISubprograms representing methods produce
1199// the same result after replacing their scope (the type containing the
1200// subprogram) from a temporary DIType with the permanent one.
1201TEST(DIBuilder, HashingDISubprogram) {
1202LLVMContext Ctx;
1203std::unique_ptr<Module> M = std::make_unique<Module>("MyModule", Ctx);
1204DIBuilder DIB(*M);
1205
1206DIFile *F = DIB.createFile("main.c", "/");
1207DICompileUnit *CU =
1208DIB.createCompileUnit(dwarf::DW_LANG_C, F, "Test", false, "", 0);
1209
1210llvm::TempDIType ForwardDeclaredType =
1211llvm::TempDIType(DIB.createReplaceableCompositeType(
1212llvm::dwarf::DW_TAG_structure_type, "MyType", CU, F, 0, 0, 8, 8, {},
1213"UniqueIdentifier"));
1214
1215// The hashing function is different for declarations and definitions, so
1216// create one of each.
1217DISubprogram *Declaration =
1218DIB.createMethod(ForwardDeclaredType.get(), "MethodName", "LinkageName",
1219F, 0, DIB.createSubroutineType({}));
1220
1221DISubprogram *Definition = DIB.createFunction(
1222ForwardDeclaredType.get(), "MethodName", "LinkageName", F, 0,
1223DIB.createSubroutineType({}), 0, DINode::FlagZero,
1224llvm::DISubprogram::SPFlagDefinition, nullptr, Declaration);
1225
1226// Produce the hash with the temporary scope.
1227unsigned HashDeclaration =
1228MDNodeKeyImpl<DISubprogram>(Declaration).getHashValue();
1229unsigned HashDefinition =
1230MDNodeKeyImpl<DISubprogram>(Definition).getHashValue();
1231
1232// Instantiate the real scope and replace the temporary one with it.
1233DICompositeType *Type = DIB.createStructType(CU, "MyType", F, 0, 8, 8, {}, {},
1234{}, 0, {}, "UniqueIdentifier");
1235DIB.replaceTemporary(std::move(ForwardDeclaredType), Type);
1236
1237// Now make sure the hashing is consistent.
1238unsigned HashDeclarationAfter =
1239MDNodeKeyImpl<DISubprogram>(Declaration).getHashValue();
1240unsigned HashDefinitionAfter =
1241MDNodeKeyImpl<DISubprogram>(Definition).getHashValue();
1242
1243EXPECT_EQ(HashDeclaration, HashDeclarationAfter);
1244EXPECT_EQ(HashDefinition, HashDefinitionAfter);
1245}
1246
1247} // end namespace
1248