Index: include/llvm/IR/DebugInfoMetadata.h =================================================================== --- include/llvm/IR/DebugInfoMetadata.h +++ include/llvm/IR/DebugInfoMetadata.h @@ -23,6 +23,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator_range.h" #include "llvm/BinaryFormat/Dwarf.h" +#include "llvm/CodeGen/DIE.h" #include "llvm/IR/Metadata.h" #include "llvm/Support/Casting.h" #include @@ -473,7 +474,7 @@ friend class MDNode; public: - // These values must be explictly set, as they end up in the final object + // These values must be explicitly set, as they end up in the final object // file. enum ChecksumKind { CSK_None = 0, Index: lib/CodeGen/AsmPrinter/DwarfCompileUnit.h =================================================================== --- lib/CodeGen/AsmPrinter/DwarfCompileUnit.h +++ lib/CodeGen/AsmPrinter/DwarfCompileUnit.h @@ -83,6 +83,10 @@ DenseMap AbstractSPDies; DenseMap> AbstractVariables; + // DIEs that are to be issued local to lexical scopes. + typedef llvm::SmallVector LocalLexicalDIEs; + llvm::DenseMap LocalLexicalDIEsMap; + /// \brief Construct a DIE for the given DbgVariable without initializing the /// DbgVariable's DIE reference. DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract); @@ -129,6 +133,21 @@ getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV, ArrayRef GlobalExprs); + /// addLocalLexicalDIE - Add DIE(s) to local lexical scope + void addLocalLexicalDIE(DIE &Die, const DILexicalBlock *Scope) { + LocalLexicalDIEsMap[Scope].push_back(&Die); + } + + /// getLocalLexicalDIEs - Return vector of local DIEs for lexical scope + const LocalLexicalDIEs& getLocalLexicalDIEs(const DILexicalBlock *Scope) { + static const LocalLexicalDIEs Empty; + auto Result = LocalLexicalDIEsMap.find(Scope); + if (Result != LocalLexicalDIEsMap.end()) + return Result->second; + else + return Empty; + } + /// addLabelAddress - Add a dwarf label attribute data and value using /// either DW_FORM_addr or DW_FORM_GNU_addr_index. void addLabelAddress(DIE &Die, dwarf::Attribute Attribute, Index: lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -120,10 +120,15 @@ // Construct the context before querying for the existence of the DIE in // case such construction creates the DIE. - DIE *ContextDIE = getOrCreateContextDIE(GVContext); + // For Lexical Scope, do not construct context DIE. + bool IsLexicalScope = GVContext && isa(GVContext); + DIE *ContextDIE = IsLexicalScope ? nullptr : getOrCreateContextDIE(GVContext); + assert(ContextDIE || IsLexicalScope); + + // Create new type and add to map. + DIE *VariableDIE = IsLexicalScope ? createDIE(GV->getTag(), GVContext) + : &createAndAddDIE(GV->getTag(), *ContextDIE, GV); - // Add to map. - DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); DIScope *DeclContext; if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) { DeclContext = resolve(SDMDecl->getScope()); @@ -148,6 +153,11 @@ // Add line number info. addSourceLine(*VariableDIE, GV); + + if (IsLexicalScope) { + DILexicalBlock* LS = static_cast(GVContext); + addLocalLexicalDIE(*VariableDIE, LS); + } } if (!GV->isDefinition()) @@ -589,6 +599,16 @@ for (LexicalScope *LS : Scope->getChildren()) constructScopeDIE(LS, Children); + auto *DS = Scope->getScopeNode(); + if (isa(DS)) { + auto LS = dyn_cast(DS); + auto DD = getLocalLexicalDIEs(LS); + for (DIE* D : DD ) + Children.push_back(D); + if (HasNonScopeChildren) + *HasNonScopeChildren = !Children.empty(); + } + return ObjectPointer; } Index: lib/CodeGen/AsmPrinter/DwarfUnit.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -332,6 +332,13 @@ Entry); } +DIE *DwarfUnit::createDIE(unsigned Tag, const DINode *N) { + DIE *Die = DIE::get(DIEValueAllocator, (dwarf::Tag)Tag); + if (N) + insertDIE(N, Die); + return Die; +} + DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N) { DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, (dwarf::Tag)Tag)); if (N)