llvm.org GIT mirror llvm / 48bb8b4
Support the Nodebug emission kind for DICompileUnits. Sample-based profiling and optimization remarks currently remove DICompileUnits from llvm.dbg.cu to suppress the emission of debug info from them. This is somewhat of a hack and only borderline legal IR. This patch uses the recently introduced NoDebug emission kind in DICompileUnit to achieve the same result without breaking the Verifier. A nice side-effect of this change is that it is now possible to combine NoDebug and regular compile units under LTO. http://reviews.llvm.org/D18808 <rdar://problem/25427165> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265861 91177308-0d34-0410-b5e6-96231b3b80d8 Adrian Prantl 3 years ago
9 changed file(s) with 173 addition(s) and 73 deletion(s). Raw diff Collapse all Expand all
9191 /// out into.
9292 /// \param Kind The kind of debug information to generate.
9393 /// \param DWOId The DWOId if this is a split skeleton compile unit.
94 /// \param EmitDebugInfo A boolean flag which indicates whether
95 /// debug information should be written to
96 /// the final output or not. When this is
97 /// false, debug information annotations will
98 /// be present in the IL but they are not
99 /// written to the final assembly or object
100 /// file. This supports tracking source
101 /// location information in the back end
102 /// without actually changing the output
103 /// (e.g., when using optimization remarks).
10494 DICompileUnit *
10595 createCompileUnit(unsigned Lang, StringRef File, StringRef Dir,
10696 StringRef Producer, bool isOptimized, StringRef Flags,
10797 unsigned RV, StringRef SplitName = StringRef(),
10898 DICompileUnit::DebugEmissionKind Kind =
10999 DICompileUnit::DebugEmissionKind::FullDebug,
110 uint64_t DWOId = 0, bool EmitDebugInfo = true);
100 uint64_t DWOId = 0);
111101
112102 /// Create a file descriptor to hold debugging information
113103 /// for a file.
3939 DISubprogram *getDISubprogram(const MDNode *Scope);
4040
4141 /// \brief Generate map by visiting all retained types.
42 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
42 DITypeIdentifierMap generateDITypeIdentifierMap(const Module &M);
4343
4444 /// \brief Strip debug info in the module if it exists.
4545 ///
625625 return make_range(named_metadata_begin(), named_metadata_end());
626626 }
627627
628 /// An iterator for DICompileUnits that skips those marked NoDebug.
629 class debug_compile_units_iterator
630 : public std::iterator {
631 NamedMDNode *CUs;
632 unsigned Idx;
633 void SkipNoDebugCUs();
634 public:
635 explicit debug_compile_units_iterator(NamedMDNode *CUs, unsigned Idx)
636 : CUs(CUs), Idx(Idx) {
637 SkipNoDebugCUs();
638 }
639 debug_compile_units_iterator &operator++() {
640 ++Idx;
641 SkipNoDebugCUs();
642 return *this;
643 }
644 debug_compile_units_iterator operator++(int) {
645 debug_compile_units_iterator T(*this);
646 ++Idx;
647 return T;
648 }
649 bool operator==(const debug_compile_units_iterator &I) const {
650 return Idx == I.Idx;
651 }
652 bool operator!=(const debug_compile_units_iterator &I) const {
653 return Idx != I.Idx;
654 }
655 DICompileUnit *operator*() const;
656 DICompileUnit *operator->() const;
657 };
658
659 debug_compile_units_iterator debug_compile_units_begin() const {
660 auto *CUs = getNamedMetadata("llvm.dbg.cu");
661 return debug_compile_units_iterator(CUs, 0);
662 }
663
664 debug_compile_units_iterator debug_compile_units_end() const {
665 auto *CUs = getNamedMetadata("llvm.dbg.cu");
666 return debug_compile_units_iterator(CUs, CUs ? CUs->getNumOperands() : 0);
667 }
668
669 /// Return an iterator for all DICompileUnits listed in this Module's
670 /// llvm.dbg.cu named metadata node and aren't explicitly marked as
671 /// NoDebug.
672 iterator_range debug_compile_units() const {
673 auto *CUs = getNamedMetadata("llvm.dbg.cu");
674 return make_range(
675 debug_compile_units_iterator(CUs, 0),
676 debug_compile_units_iterator(CUs, CUs ? CUs->getNumOperands() : 0));
677 }
678 /// @}
679
628680 /// Destroy ConstantArrays in LLVMContext if they are not used.
629681 /// ConstantArrays constructed during linking can cause quadratic memory
630682 /// explosion. Releasing all unused constants can cause a 20% LTO compile-time
634686 /// be called where all uses of the LLVMContext are understood.
635687 void dropTriviallyDeadConstantArrays();
636688
637 /// @}
638689 /// @name Utility functions for printing and dumping Module objects
639690 /// @{
640691
465465
466466 const Module *M = MMI->getModule();
467467
468 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
469 if (!CU_Nodes)
470 return;
471 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
472
473 SingleCU = CU_Nodes->getNumOperands() == 1;
474
475 for (MDNode *N : CU_Nodes->operands()) {
476 auto *CUNode = cast(N);
468 TypeIdentifierMap = generateDITypeIdentifierMap(*M);
469 unsigned NumDebugCUs = 0;
470 for (DICompileUnit *CUNode : M->debug_compile_units()) {
471 (void)CUNode;
472 ++NumDebugCUs;
473 }
474
475 // Tell MMI whether we have debug info.
476 MMI->setDebugInfoAvailability(NumDebugCUs > 0);
477 SingleCU = NumDebugCUs == 1;
478
479 for (DICompileUnit *CUNode : M->debug_compile_units()) {
477480 DwarfCompileUnit &CU = constructDwarfCompileUnit(CUNode);
478481 for (auto *IE : CUNode->getImportedEntities())
479482 CU.addImportedEntity(IE);
499502 for (auto *IE : CUNode->getImportedEntities())
500503 constructAndAddImportedEntityDIE(CU, IE);
501504 }
502
503 // Tell MMI that we have debug info.
504 MMI->setDebugInfoAvailability(true);
505505 }
506506
507507 void DwarfDebug::finishVariableDefinitions() {
537537 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
538538 for (MDNode *N : CU_Nodes->operands()) {
539539 auto *TheCU = cast(N);
540 if (TheCU->getEmissionKind() == DICompileUnit::NoDebug)
541 continue;
542
540543 // Construct subprogram DIE and add variables DIEs.
541544 DwarfCompileUnit *SPCU =
542545 static_cast(CUMap.lookup(TheCU));
10891092 // includes the directory of the cpp file being built, even when the file name
10901093 // is absolute (such as an <> lookup header)))
10911094 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1092 assert(TheCU && "Unable to find compile unit!");
1095 if (!TheCU)
1096 // Once DISubprogram points to the owning CU, we can assert that the CU has
1097 // a NoDebug EmissionKind here.
1098 return;
10931099 if (Asm->OutStreamer->hasRawTextSupport())
10941100 // Use a single line table if we are generating assembly.
10951101 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
11121118 "endFunction should be called with the same function as beginFunction");
11131119
11141120 if (!MMI->hasDebugInfo() || LScopes.empty() ||
1115 !MF->getFunction()->getSubprogram()) {
1121 !MF->getFunction()->getSubprogram() ||
1122 // Once DISubprogram points to the owning CU, we can check for a
1123 // CU with a NoDebug EmissionKind here.
1124 !SPMap.lookup(MF->getFunction()->getSubprogram())) {
11161125 // If we don't have a lexical scope for this function then there will
11171126 // be a hole in the range information. Keep note of this by setting the
11181127 // previously used section to nullptr.
136136 DICompileUnit *DIBuilder::createCompileUnit(
137137 unsigned Lang, StringRef Filename, StringRef Directory, StringRef Producer,
138138 bool isOptimized, StringRef Flags, unsigned RunTimeVer, StringRef SplitName,
139 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId, bool EmitDebugInfo) {
139 DICompileUnit::DebugEmissionKind Kind, uint64_t DWOId) {
140140
141141 assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) ||
142142 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
151151 nullptr, nullptr, nullptr, nullptr, nullptr, DWOId);
152152
153153 // Create a named metadata so that it is easier to find cu in a module.
154 // Note that we only generate this when the caller wants to actually
155 // emit debug information. When we are only interested in tracking
156 // source line locations throughout the backend, we prevent codegen from
157 // emitting debug info in the final output by not generating llvm.dbg.cu.
158 if (EmitDebugInfo) {
159 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
160 NMD->addOperand(CUNode);
161 }
162
154 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
155 NMD->addOperand(CUNode);
163156 trackIfUnresolved(CUNode);
164157 return CUNode;
165158 }
3838 }
3939
4040 DITypeIdentifierMap
41 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
41 llvm::generateDITypeIdentifierMap(const Module &M) {
4242 DITypeIdentifierMap Map;
43 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
44 auto *CU = cast(CU_Nodes->getOperand(CUi));
43 for (DICompileUnit *CU : M.debug_compile_units()) {
4544 DINodeArray Retain = CU->getRetainedTypes();
4645 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
4746 if (!isa(Retain[Ti]))
7877 }
7978
8079 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
81 if (!TypeMapInitialized)
82 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
83 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
84 TypeMapInitialized = true;
85 }
80 if (TypeMapInitialized)
81 return;
82 TypeIdentifierMap = generateDITypeIdentifierMap(M);
83 TypeMapInitialized = true;
8684 }
8785
8886 void DebugInfoFinder::processModule(const Module &M) {
8987 InitializeTypeMap(M);
90 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
91 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
92 auto *CU = cast(CU_Nodes->getOperand(i));
93 addCompileUnit(CU);
94 for (auto *DIG : CU->getGlobalVariables()) {
95 if (addGlobalVariable(DIG)) {
96 processScope(DIG->getScope());
97 processType(DIG->getType().resolve(TypeIdentifierMap));
98 }
88 for (auto *CU : M.debug_compile_units()) {
89 addCompileUnit(CU);
90 for (auto *DIG : CU->getGlobalVariables()) {
91 if (addGlobalVariable(DIG)) {
92 processScope(DIG->getScope());
93 processType(DIG->getType().resolve(TypeIdentifierMap));
9994 }
100 for (auto *SP : CU->getSubprograms())
95 }
96 for (auto *SP : CU->getSubprograms())
97 processSubprogram(SP);
98 for (auto *ET : CU->getEnumTypes())
99 processType(ET);
100 for (auto *RT : CU->getRetainedTypes())
101 processType(RT);
102 for (auto *Import : CU->getImportedEntities()) {
103 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
104 if (auto *T = dyn_cast(Entity))
105 processType(T);
106 else if (auto *SP = dyn_cast(Entity))
101107 processSubprogram(SP);
102 for (auto *ET : CU->getEnumTypes())
103 processType(ET);
104 for (auto *RT : CU->getRetainedTypes())
105 processType(RT);
106 for (auto *Import : CU->getImportedEntities()) {
107 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
108 if (auto *T = dyn_cast(Entity))
109 processType(T);
110 else if (auto *SP = dyn_cast(Entity))
111 processSubprogram(SP);
112 else if (auto *NS = dyn_cast(Entity))
113 processScope(NS->getScope());
114 else if (auto *M = dyn_cast(Entity))
115 processScope(M->getScope());
116 }
108 else if (auto *NS = dyn_cast(Entity))
109 processScope(NS->getScope());
110 else if (auto *M = dyn_cast(Entity))
111 processScope(M->getScope());
117112 }
118113 }
119114 }
1818 #include "llvm/ADT/StringExtras.h"
1919 #include "llvm/IR/Constants.h"
2020 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/DebugInfoMetadata.h"
2122 #include "llvm/IR/GVMaterializer.h"
2223 #include "llvm/IR/InstrTypes.h"
2324 #include "llvm/IR/LLVMContext.h"
379380
380381 const DataLayout &Module::getDataLayout() const { return DL; }
381382
383 DICompileUnit *Module::debug_compile_units_iterator::operator*() const {
384 return cast(CUs->getOperand(Idx));
385 }
386 DICompileUnit *Module::debug_compile_units_iterator::operator->() const {
387 return cast(CUs->getOperand(Idx));
388 }
389
390 void Module::debug_compile_units_iterator::SkipNoDebugCUs() {
391 while (CUs && (Idx < CUs->getNumOperands()) &&
392 ((*this)->getEmissionKind() == DICompileUnit::NoDebug))
393 ++Idx;
394 }
395
382396 //===----------------------------------------------------------------------===//
383397 // Methods to control the materialization of GlobalValues in the Module.
384398 //
0 ; RUN: llc %s -o %t -filetype=obj
1 ; RUN: llvm-dwarfdump -debug-dump=info %t | FileCheck %s
2 ; CHECK: DW_TAG_compile_unit
3 ; CHECK: DW_TAG_subprogram
4 ; CHECK: DW_AT_name{{.*}}"f"
5 ; CHECK-NOT: DW_TAG_compile_unit
6 ;
7 ; created from
8 ; void f() {} // compile with -g
9 ; void g() {} // compile with -Rpass=inline
10 ; and llvm-linking the result.
11
12 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
13 target triple = "x86_64-apple-macosx10.11.0"
14
15 ; Function Attrs: nounwind ssp uwtable
16 define void @f() #0 !dbg !4 {
17 entry:
18 ret void, !dbg !15
19 }
20
21 ; Function Attrs: nounwind ssp uwtable
22 define void @g() #0 !dbg !9 {
23 entry:
24 ret void, !dbg !16
25 }
26
27 attributes #0 = { nounwind ssp uwtable }
28
29 !llvm.dbg.cu = !{!0, !7}
30 !llvm.ident = !{!11, !11}
31 !llvm.module.flags = !{!12, !13, !14}
32
33 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.9.0 (trunk 265328) (llvm/trunk 265330)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, subprograms: !3)
34 !1 = !DIFile(filename: "test.c", directory: "/Volumes/Data/llvm")
35 !2 = !{}
36 !3 = !{!4}
37 !4 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, variables: !2)
38 !5 = !DISubroutineType(types: !6)
39 !6 = !{null}
40 !7 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.9.0 (trunk 265328) (llvm/trunk 265330)", isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug, enums: !2, subprograms: !8)
41 !8 = !{!9}
42 !9 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 1, type: !10, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, variables: !2)
43 !10 = !DISubroutineType(types: !2)
44 !11 = !{!"clang version 3.9.0 (trunk 265328) (llvm/trunk 265330)"}
45 !12 = !{i32 2, !"Dwarf Version", i32 2}
46 !13 = !{i32 2, !"Debug Info Version", i32 3}
47 !14 = !{i32 1, !"PIC Level", i32 2}
48 !15 = !DILocation(line: 1, column: 12, scope: !4)
49 !16 = !DILocation(line: 1, column: 12, scope: !9)
4444
4545 bool runOnModule(Module &M) override {
4646 TypeIdentifierMap.clear();
47 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
48 if (CU_Nodes)
49 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
47 TypeIdentifierMap = generateDITypeIdentifierMap(M);
5048
5149 StringSet<> Processed;
5250 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))