diff options
author | Reid Kleckner <rnk@google.com> | 2016-06-22 17:15:28 +0000 |
---|---|---|
committer | Reid Kleckner <rnk@google.com> | 2016-06-22 17:15:28 +0000 |
commit | 643dd83661d46f69d5f7a6dd0ec0e4df764ba6e3 (patch) | |
tree | 6ab7de8f881e89dd255c72473285ce425671eba4 /llvm/lib | |
parent | b12b353a4103f88ce635a609b3513b5a3b7f6e70 (diff) | |
download | bcm5719-llvm-643dd83661d46f69d5f7a6dd0ec0e4df764ba6e3.tar.gz bcm5719-llvm-643dd83661d46f69d5f7a6dd0ec0e4df764ba6e3.zip |
[codeview] Defer emission of all referenced complete records
This is the motivating example:
struct B { int b; };
struct A { B *b; };
int f(A *p) { return p->b->b; }
Clang emits complete types for both A and B because they are required to
be complete, but our CodeView emission would only emit forward
declarations of A and B. This was a consequence of the fact that the A*
type must reference the forward declaration of A, which doesn't
reference B at all.
We can't eagerly emit complete definitions of A and B when we request
the forward declaration's type index because of recursive types like
linked lists. If we did that, our stack usage could get out of hand, and
it would be possible to lower a type while attempting to lower a type,
and we would need to double check if our type is already present in the
TypeIndexMap after all recursive getTypeIndex calls.
Instead, defer complete type emission until after all type lowering has
completed. This ensures that all referenced complete types are emitted,
and that type lowering is not re-entrant.
llvm-svn: 273443
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp | 40 | ||||
-rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h | 11 |
2 files changed, 49 insertions, 2 deletions
diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp index 31f0872a301..da69f8b6ab6 100644 --- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -1400,6 +1400,8 @@ TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) { TypeIndex FwdDeclTI = TypeTable.writeClass(ClassRecord( Kind, 0, CO, HfaKind::None, WindowsRTClassKind::None, TypeIndex(), TypeIndex(), TypeIndex(), 0, FullName, Ty->getIdentifier())); + if (!Ty->isForwardDecl()) + DeferredCompleteTypes.push_back(Ty); return FwdDeclTI; } @@ -1431,6 +1433,8 @@ TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) { TypeIndex FwdDeclTI = TypeTable.writeUnion(UnionRecord(0, CO, HfaKind::None, TypeIndex(), 0, FullName, Ty->getIdentifier())); + if (!Ty->isForwardDecl()) + DeferredCompleteTypes.push_back(Ty); return FwdDeclTI; } @@ -1518,6 +1522,18 @@ CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) { return std::make_tuple(FieldTI, TypeIndex(), MemberCount); } +struct CodeViewDebug::TypeLoweringScope { + TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; } + ~TypeLoweringScope() { + // Don't decrement TypeEmissionLevel until after emitting deferred types, so + // inner TypeLoweringScopes don't attempt to emit deferred types. + if (CVD.TypeEmissionLevel == 1) + CVD.emitDeferredCompleteTypes(); + --CVD.TypeEmissionLevel; + } + CodeViewDebug &CVD; +}; + TypeIndex CodeViewDebug::getTypeIndex(DITypeRef TypeRef, DITypeRef ClassTyRef) { const DIType *Ty = TypeRef.resolve(); const DIType *ClassTy = ClassTyRef.resolve(); @@ -1533,9 +1549,14 @@ TypeIndex CodeViewDebug::getTypeIndex(DITypeRef TypeRef, DITypeRef ClassTyRef) { if (I != TypeIndices.end()) return I->second; - TypeIndex TI = lowerType(Ty, ClassTy); + TypeIndex TI; + { + TypeLoweringScope S(*this); + TI = lowerType(Ty, ClassTy); + recordTypeIndexForDINode(Ty, TI, ClassTy); + } - return recordTypeIndexForDINode(Ty, TI, ClassTy); + return TI; } TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) { @@ -1564,6 +1585,8 @@ TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) { if (!InsertResult.second) return InsertResult.first->second; + TypeLoweringScope S(*this); + // Make sure the forward declaration is emitted first. It's unclear if this // is necessary, but MSVC does it, and we should follow suit until we can show // otherwise. @@ -1592,6 +1615,19 @@ TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) { return TI; } +/// Emit all the deferred complete record types. Try to do this in FIFO order, +/// and do this until fixpoint, as each complete record type typically references +/// many other record types. +void CodeViewDebug::emitDeferredCompleteTypes() { + SmallVector<const DICompositeType *, 4> TypesToEmit; + while (!DeferredCompleteTypes.empty()) { + std::swap(DeferredCompleteTypes, TypesToEmit); + for (const DICompositeType *RecordTy : TypesToEmit) + getCompleteTypeIndex(RecordTy); + TypesToEmit.clear(); + } +} + void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) { // LocalSym record, see SymbolRecord.h for more info. MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(), diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h index be8d6cea024..a7976d8d2ac 100644 --- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h +++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h @@ -150,6 +150,13 @@ class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase { /// always looked up in the normal TypeIndices map. DenseMap<const DICompositeType *, codeview::TypeIndex> CompleteTypeIndices; + /// Complete record types to emit after all active type lowerings are + /// finished. + SmallVector<const DICompositeType *, 4> DeferredCompleteTypes; + + /// Number of type lowering frames active on the stack. + unsigned TypeEmissionLevel = 0; + const DISubprogram *CurrentSubprogram = nullptr; // The UDTs we have seen while processing types; each entry is a pair of type @@ -246,6 +253,10 @@ class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase { codeview::TypeIndex lowerCompleteTypeClass(const DICompositeType *Ty); codeview::TypeIndex lowerCompleteTypeUnion(const DICompositeType *Ty); + struct TypeLoweringScope; + + void emitDeferredCompleteTypes(); + void collectMemberInfo(ClassInfo &Info, const DIDerivedType *DDTy); ClassInfo collectClassInfo(const DICompositeType *Ty); |