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

source code of llvm/unittests/IR/DebugInfoTest.cpp