diff options
author | Adrian Prantl <aprantl@apple.com> | 2018-02-06 22:17:45 +0000 |
---|---|---|
committer | Adrian Prantl <aprantl@apple.com> | 2018-02-06 22:17:45 +0000 |
commit | c929f7ad423e9566dc9a06815d0f3f7f3d809db8 (patch) | |
tree | 705d15b04e8d0bf202bc035d8c981fa4eb0d6e56 /llvm/lib/CodeGen | |
parent | 0ccef9ee1693f774a9dc23a10e0d3d7bf8b9bfbf (diff) | |
download | bcm5719-llvm-c929f7ad423e9566dc9a06815d0f3f7f3d809db8.tar.gz bcm5719-llvm-c929f7ad423e9566dc9a06815d0f3f7f3d809db8.zip |
Fix a crash when emitting DIEs for variable-length arrays
VLAs may refer to a previous DIE to express the DW_AT_count of their
type. Clang generates an artificial "vla_expr" variable for this. If
this DIE hasn't been created yet LLVM asserts. This patch fixes this
by sorting the local variables so that dependencies come before they
are needed. It also replaces the linear scan in DWARFFile with a
std::map, which can be faster.
Differential Revision: https://reviews.llvm.org/D42940
llvm-svn: 324412
Diffstat (limited to 'llvm/lib/CodeGen')
-rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp | 30 | ||||
-rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/DwarfFile.cpp | 39 | ||||
-rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/DwarfFile.h | 14 |
3 files changed, 50 insertions, 33 deletions
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp index 3cde5ad38ce..85a63248a21 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -568,13 +568,41 @@ DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, return Var; } +/// Determine whether a variable appears in a count: expression. +static bool dependsOn(DbgVariable *A, DbgVariable *B) { + auto *Array = dyn_cast<DICompositeType>(A->getType()); + if (!Array || Array->getTag() != dwarf::DW_TAG_array_type) + return false; + return llvm::any_of(Array->getElements(), [&](DINode *El) { + if (auto *Subrange = dyn_cast<DISubrange>(El)) { + auto Count = Subrange->getCount(); + if (auto *Var = Count.dyn_cast<DIVariable *>()) + return Var == B->getVariable(); + } + return false; + }); +} + +/// Sort local variables so that variables appearing inside of helper +/// expressions come first. +static bool sortLocalVars(DbgVariable *A, DbgVariable *B) { + return dependsOn(B, A); +} + DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope, SmallVectorImpl<DIE *> &Children, bool *HasNonScopeChildren) { assert(Children.empty()); DIE *ObjectPointer = nullptr; - for (DbgVariable *DV : DU->getScopeVariables().lookup(Scope)) + // Emit function arguments (order is significant). + auto Vars = DU->getScopeVariables().lookup(Scope); + for (auto &DV : Vars.Args) + Children.push_back(constructVariableDIE(*DV.second, *Scope, ObjectPointer)); + + // Emit local variables. + std::stable_sort(Vars.Locals.begin(), Vars.Locals.end(), sortLocalVars); + for (DbgVariable *DV : Vars.Locals) Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); // Skip imported directives in gmlt-like data. diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfFile.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfFile.cpp index 87f39167c85..19a1dc3c66e 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfFile.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfFile.cpp @@ -103,37 +103,18 @@ void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection, } bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) { - SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS]; + auto &ScopeVars = ScopeVariables[LS]; const DILocalVariable *DV = Var->getVariable(); - // Variables with positive arg numbers are parameters. if (unsigned ArgNum = DV->getArg()) { - // Keep all parameters in order at the start of the variable list to ensure - // function types are correct (no out-of-order parameters) - // - // This could be improved by only doing it for optimized builds (unoptimized - // builds have the right order to begin with), searching from the back (this - // would catch the unoptimized case quickly), or doing a binary search - // rather than linear search. - auto I = Vars.begin(); - while (I != Vars.end()) { - unsigned CurNum = (*I)->getVariable()->getArg(); - // A local (non-parameter) variable has been found, insert immediately - // before it. - if (CurNum == 0) - break; - // A later indexed parameter has been found, insert immediately before it. - if (CurNum > ArgNum) - break; - if (CurNum == ArgNum) { - (*I)->addMMIEntry(*Var); - return false; - } - ++I; + auto Cached = ScopeVars.Args.find(ArgNum); + if (Cached == ScopeVars.Args.end()) + ScopeVars.Args[ArgNum] = Var; + else { + Cached->second->addMMIEntry(*Var); + return false; } - Vars.insert(I, Var); - return true; - } - - Vars.push_back(Var); + } else { + ScopeVars.Locals.push_back(Var); + } return true; } diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfFile.h b/llvm/lib/CodeGen/AsmPrinter/DwarfFile.h index 5de6af539c3..23ed043afb9 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfFile.h +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfFile.h @@ -17,6 +17,7 @@ #include "llvm/CodeGen/DIE.h" #include "llvm/IR/Metadata.h" #include "llvm/Support/Allocator.h" +#include <map> #include <memory> #include <utility> @@ -47,8 +48,15 @@ class DwarfFile { /// the string offsets table. The contribution is shared by all units. MCSymbol *StringOffsetsStartSym = nullptr; - // Collection of dbg variables of a scope. - DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8>> ScopeVariables; + /// The variables of a lexical scope. + struct ScopeVars { + /// We need to sort Args by ArgNo and check for duplicates. This could also + /// be implemented as a list or vector + std::lower_bound(). + std::map<unsigned, DbgVariable *> Args; + SmallVector<DbgVariable *, 8> Locals; + }; + /// Collection of DbgVariables of each lexical scope. + DenseMap<LexicalScope *, ScopeVars> ScopeVariables; // Collection of abstract subprogram DIEs. DenseMap<const MDNode *, DIE *> AbstractSPDies; @@ -109,7 +117,7 @@ public: /// \returns false if the variable was merged with a previous one. bool addScopeVariable(LexicalScope *LS, DbgVariable *Var); - DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8>> &getScopeVariables() { + DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() { return ScopeVariables; } |