Modular Codegen: Support homing debug info for types in modular objects
authorDavid Blaikie <dblaikie@gmail.com>
Tue, 11 Apr 2017 21:13:37 +0000 (21:13 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Tue, 11 Apr 2017 21:13:37 +0000 (21:13 +0000)
Matching the function-homing support for modular codegen. Any type
implicitly (implicit template specializations) or explicitly defined in
a module is attached to that module's object file and omitted elsewhere
(only a declaration used if necessary for references).

llvm-svn: 299987

14 files changed:
clang/include/clang/AST/ExternalASTSource.h
clang/include/clang/Sema/MultiplexExternalSemaSource.h
clang/include/clang/Serialization/ASTReader.h
clang/lib/AST/ExternalASTSource.cpp
clang/lib/CodeGen/CGDebugInfo.cpp
clang/lib/CodeGen/CGDebugInfo.h
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/Sema/MultiplexExternalSemaSource.cpp
clang/lib/Serialization/ASTReader.cpp
clang/lib/Serialization/ASTReaderDecl.cpp
clang/lib/Serialization/ASTWriter.cpp
clang/test/Modules/Inputs/codegen-nodep/foo.h
clang/test/Modules/codegen-nodep.test
clang/test/Modules/codegen.test

index f2b29cd9a73616d971d02eb5cb5a7c649f4964a8..d8dd18ecb8d3bb25703b21cd64b32a399baa9244 100644 (file)
@@ -172,7 +172,7 @@ public:
 
   enum ExtKind { EK_Always, EK_Never, EK_ReplyHazy };
 
-  virtual ExtKind hasExternalDefinitions(const FunctionDecl *FD);
+  virtual ExtKind hasExternalDefinitions(const Decl *D);
 
   /// \brief Finds all declarations lexically contained within the given
   /// DeclContext, after applying an optional filter predicate.
index 0386552dbe7e3a1326d9adfd617a2c3049466739..1d681a00552fc6fa1b8a9e4b4399cbc3056e5f50 100644 (file)
@@ -90,7 +90,7 @@ public:
   /// initializers themselves.
   CXXCtorInitializer **GetExternalCXXCtorInitializers(uint64_t Offset) override;
 
-  ExtKind hasExternalDefinitions(const FunctionDecl *FD) override;
+  ExtKind hasExternalDefinitions(const Decl *D) override;
 
   /// \brief Find all declarations with the given name in the
   /// given context.
index 4e1c1cf57da244334e6866027269a39a75dc6403..de122e63ba8c4cd9b8f5a4d532bb87484b50f9fd 100644 (file)
@@ -1115,7 +1115,7 @@ private:
   /// predefines buffer may contain additional definitions.
   std::string SuggestedPredefines;
 
-  llvm::DenseMap<const FunctionDecl *, bool> BodySource;
+  llvm::DenseMap<const Decl *, bool> BodySource;
 
   /// \brief Reads a statement from the specified cursor.
   Stmt *ReadStmtFromStream(ModuleFile &F);
@@ -1999,7 +1999,7 @@ public:
   /// \brief Return a descriptor for the corresponding module.
   llvm::Optional<ASTSourceDescriptor> getSourceDescriptor(unsigned ID) override;
 
-  ExtKind hasExternalDefinitions(const FunctionDecl *FD) override;
+  ExtKind hasExternalDefinitions(const Decl *D) override;
 
   /// \brief Retrieve a selector from the given module with its local ID
   /// number.
index 958a67843b9bc138127e91d9d439414d381f0928..182d38242f596dace3ebe3c7591f4076985d1d46 100644 (file)
@@ -29,7 +29,7 @@ ExternalASTSource::getSourceDescriptor(unsigned ID) {
 }
 
 ExternalASTSource::ExtKind
-ExternalASTSource::hasExternalDefinitions(const FunctionDecl *FD) {
+ExternalASTSource::hasExternalDefinitions(const Decl *D) {
   return EK_ReplyHazy;
 }
 
index 05987be8455f667baff9b4749066e04a04fe1122..818b51543d30af278411a799f8367e2dbeadf144 100644 (file)
@@ -1820,6 +1820,10 @@ static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind,
   if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
     return true;
 
+  if (auto *ES = RD->getASTContext().getExternalSource())
+    if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always)
+      return true;
+
   if (DebugKind > codegenoptions::LimitedDebugInfo)
     return false;
 
@@ -2552,11 +2556,17 @@ void CGDebugInfo::completeTemplateDefinition(
     const ClassTemplateSpecializationDecl &SD) {
   if (DebugKind <= codegenoptions::DebugLineTablesOnly)
     return;
+  completeUnusedClass(SD);
+}
+
+void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
+  if (DebugKind <= codegenoptions::DebugLineTablesOnly)
+    return;
 
-  completeClassData(&SD);
+  completeClassData(&D);
   // In case this type has no member function definitions being emitted, ensure
   // it is retained
-  RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr());
+  RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
 }
 
 llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) {
index c6d1c66e1311a94e83057066ea42d4fe33d2f1f0..5050ca0ad3fa0dcd16d21fd00f6321f91db06537 100644 (file)
@@ -438,6 +438,7 @@ public:
   void completeClass(const RecordDecl *RD);
 
   void completeTemplateDefinition(const ClassTemplateSpecializationDecl &SD);
+  void completeUnusedClass(const CXXRecordDecl &D);
 
   /// Create debug info for a macro defined by a #define directive or a macro
   /// undefined by a #undef directive.
index 96358bf6119e1c2c98a4d97a4461e2df6d4a1582..7fe8bf04129d55c910a47c1616b152585dc8105b 100644 (file)
@@ -3820,6 +3820,11 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
     EmitDeclContext(cast<NamespaceDecl>(D));
     break;
   case Decl::CXXRecord:
+    if (DebugInfo) {
+      if (auto *ES = D->getASTContext().getExternalSource())
+        if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
+          DebugInfo->completeUnusedClass(cast<CXXRecordDecl>(*D));
+    }
     // Emit any static data members, they may be definitions.
     for (auto *I : cast<CXXRecordDecl>(D)->decls())
       if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I))
index f749b7d139450cc8ceee68aee3d212adef96119b..b7e343c6471836d4554e253f313cef8e06002266 100644 (file)
@@ -95,9 +95,9 @@ MultiplexExternalSemaSource::GetExternalCXXCtorInitializers(uint64_t Offset) {
 }
 
 ExternalASTSource::ExtKind
-MultiplexExternalSemaSource::hasExternalDefinitions(const FunctionDecl *FD) {
+MultiplexExternalSemaSource::hasExternalDefinitions(const Decl *D) {
   for (const auto &S : Sources)
-    if (auto EK = S->hasExternalDefinitions(FD))
+    if (auto EK = S->hasExternalDefinitions(D))
       if (EK != EK_ReplyHazy)
         return EK;
   return EK_ReplyHazy;
index a51f144ae069ed1cbec6ae8f8c9eea176f47a129..322fc863be91fa3b1d663355eecbc3f1569800d2 100644 (file)
@@ -8147,8 +8147,7 @@ ASTReader::getSourceDescriptor(unsigned ID) {
   return None;
 }
 
-ExternalASTSource::ExtKind
-ASTReader::hasExternalDefinitions(const FunctionDecl *FD) {
+ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
   auto I = BodySource.find(FD);
   if (I == BodySource.end())
     return EK_ReplyHazy;
index 9b9b41a104f1258102723741c56c7d81761a58bd..db7d55ec0b877c395b6b84d2c82a284d68fe0fc0 100644 (file)
@@ -119,7 +119,8 @@ namespace clang {
     }
 
     void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
-    void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data);
+    void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
+                               const CXXRecordDecl *D);
     void MergeDefinitionData(CXXRecordDecl *D,
                              struct CXXRecordDecl::DefinitionData &&NewDD);
     void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
@@ -1490,7 +1491,7 @@ void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
 }
 
 void ASTDeclReader::ReadCXXDefinitionData(
-                                   struct CXXRecordDecl::DefinitionData &Data) {
+    struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
   // Note: the caller has deserialized the IsLambda bit already.
   Data.UserDeclaredConstructor = Record.readInt();
   Data.UserDeclaredSpecialMembers = Record.readInt();
@@ -1536,6 +1537,12 @@ void ASTDeclReader::ReadCXXDefinitionData(
   Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
   Data.ODRHash = Record.readInt();
 
+  if (Record.readInt()) {
+    Reader.BodySource[D] = Loc.F->Kind == ModuleKind::MK_MainFile
+                               ? ExternalASTSource::EK_Never
+                               : ExternalASTSource::EK_Always;
+  }
+
   Data.NumBases = Record.readInt();
   if (Data.NumBases)
     Data.Bases = ReadGlobalOffset();
@@ -1707,7 +1714,7 @@ void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
   else
     DD = new (C) struct CXXRecordDecl::DefinitionData(D);
 
-  ReadCXXDefinitionData(*DD);
+  ReadCXXDefinitionData(*DD, D);
 
   // We might already have a definition for this record. This can happen either
   // because we're reading an update record, or because we've already done some
@@ -2553,7 +2560,11 @@ static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
            Var->isThisDeclarationADefinition() == VarDecl::Definition;
   if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
     return Func->doesThisDeclarationHaveABody() || HasBody;
-  
+
+  if (auto *ES = D->getASTContext().getExternalSource())
+    if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
+      return true;
+
   return false;
 }
 
index 23859c241d5280b916660cc6f29643c31164e915..ec59250e1921808207c7f2b8a1e734cdb09fc5f6 100644 (file)
@@ -5770,6 +5770,12 @@ void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
   Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
   Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
   Record->push_back(Data.ODRHash);
+  bool ModularCodegen = Writer->Context->getLangOpts().ModularCodegen &&
+                        Writer->WritingModule && !D->isDependentType();
+  Record->push_back(ModularCodegen);
+  if (ModularCodegen)
+    Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
+
   // IsLambda bit is already saved.
 
   Record->push_back(Data.NumBases);
index af91e8d263b80432cf4e3c75f7f8ba986a150c0a..e7b20a512db16ed2f4e4954d48878fab8fdc0ea6 100644 (file)
@@ -1,5 +1,11 @@
 template <typename T>
-void ftempl() {
+void foot() {
 }
-inline void f() {
+inline void foo() {
 }
+
+template <typename T>
+struct bart {
+};
+struct bar {
+};
index cb2b4e37e9d1a4ca7de89e372139e9df89512052..0c3eb47a1eaf92d1f5e1027122212767f35cb355 100644 (file)
@@ -7,7 +7,7 @@ RUN:            %S/Inputs/codegen-nodep/foo.modulemap -o - \
 RUN:          | llvm-bcanalyzer - -dump \
 RUN:          | FileCheck %s
 
-Ensure there's only one modular codegen decl - the sentinel plain inline
-function, not any for the function template.
+Ensure there are only two modular codegen decls (one for the class, one for the
+function - none for the class and function templates).
 
-CHECK: <MODULAR_CODEGEN_DECLS op0={{[0-9]+}}/>
+CHECK: <MODULAR_CODEGEN_DECLS op0={{[0-9]+}} op1={{[0-9]+}}/>
index 6807640e603d653f792d0265cbbc3bff7ee0ac48..ce252e89ebdcd55b973f7539a723e6ab2febad94 100644 (file)
@@ -3,8 +3,8 @@ REQUIRES: x86-registered-target
 
 RUN: %clang_cc1 -triple=x86_64-linux-gnu -fmodules-codegen -x c++ -fmodules -emit-module -fmodule-name=foo %S/Inputs/codegen/foo.modulemap -o %t/foo.pcm
 
-RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %t/foo.pcm | FileCheck --check-prefix=FOO --check-prefix=BOTH %s
-RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - -fmodules -fmodule-file=%t/foo.pcm %S/Inputs/codegen/use.cpp | FileCheck --check-prefix=BOTH --check-prefix=USE %s
+RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -debug-info-kind=limited -o - %t/foo.pcm | FileCheck --check-prefix=FOO --check-prefix=BOTH %s
+RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -debug-info-kind=limited -o - -fmodules -fmodule-file=%t/foo.pcm %S/Inputs/codegen/use.cpp | FileCheck --check-prefix=BOTH --check-prefix=USE %s
 
 FOO: $_Z2f1PKcz = comdat any
 FOO: $_ZN13implicit_dtorD1Ev = comdat any
@@ -25,3 +25,11 @@ FOO: define weak_odr void @_ZN13implicit_dtorD2Ev
 USE: define linkonce_odr void @_ZN20uninst_implicit_dtorD1Ev
 USE: define linkonce_odr void @_Z4instIiEvv
 USE: define linkonce_odr void @_ZN20uninst_implicit_dtorD2Ev
+
+Modular debug info puts the definition of a class defined in a module in that
+module's object. Users of the module only get a declaration.
+
+'distinct' is used for definition records (the flags field is empty/unspecified)
+FOO: = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "implicit_dtor"
+Declarations are non-distinct and include the 'DIFlagFwdDecl' flag.
+USE: = !DICompositeType(tag: DW_TAG_structure_type, name: "implicit_dtor", {{.*}}, flags: DIFlagFwdDecl