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