[cfe-commits] r109474 - in /cfe/trunk: include/clang/AST/DeclBase.h include/clang/Frontend/PCHBitCodes.h include/clang/Frontend/PCHReader.h include/clang/Frontend/PCHWriter.h lib/AST/DeclBase.cpp lib/Frontend/PCHReader.cpp lib/Frontend/PCHReaderDecl.cpp lib/Frontend/PCHWriter.cpp lib/Frontend/PCHWriterDecl.cpp

Sebastian Redl sebastian.redl at getdesigned.at
Mon Jul 26 17:17:23 PDT 2010


Author: cornedbee
Date: Mon Jul 26 19:17:23 2010
New Revision: 109474

URL: http://llvm.org/viewvc/llvm-project?rev=109474&view=rev
Log:
- Fix recording of offsets of types in dependent PCHs.
- Stop reading in (and thus deserializing) every declaration in the TU when creating a dependent PCH.
- Switch the storage of a decl context's lexical declarations to a blob containing the IDs instead of a record. This is the only sane way of supporting update records later on.

Modified:
    cfe/trunk/include/clang/AST/DeclBase.h
    cfe/trunk/include/clang/Frontend/PCHBitCodes.h
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/include/clang/Frontend/PCHWriter.h
    cfe/trunk/lib/AST/DeclBase.cpp
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHReaderDecl.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp
    cfe/trunk/lib/Frontend/PCHWriterDecl.cpp

Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=109474&r1=109473&r2=109474&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Mon Jul 26 19:17:23 2010
@@ -840,6 +840,12 @@
   decl_iterator decls_end() const;
   bool decls_empty() const;
 
+  /// noload_decls_begin/end - Iterate over the declarations stored in this
+  /// context that are currently loaded; don't attempt to retrieve anything
+  /// from an external source.
+  decl_iterator noload_decls_begin() const;
+  decl_iterator noload_decls_end() const;
+
   /// specific_decl_iterator - Iterates over a subrange of
   /// declarations stored in a DeclContext, providing only those that
   /// are of type SpecificDecl (or a class derived from it). This

Modified: cfe/trunk/include/clang/Frontend/PCHBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHBitCodes.h?rev=109474&r1=109473&r2=109474&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHBitCodes.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHBitCodes.h Mon Jul 26 19:17:23 2010
@@ -238,7 +238,7 @@
       /// PCH version and the name of the PCH this is chained to.
       CHAINED_METADATA = 26,
       
-      /// \brief Record  code for referenced selector pool.
+      /// \brief Record code for referenced selector pool.
       REFERENCED_SELECTOR_POOL = 27
     };
 
@@ -547,8 +547,8 @@
       /// \brief A record that stores the set of declarations that are
       /// lexically stored within a given DeclContext.
       ///
-      /// The record itself is an array of declaration IDs, in the
-      /// order in which those declarations were added to the
+      /// The record itself is a blob that is an array of declaration IDs,
+      /// in the order in which those declarations were added to the
       /// declaration context. This data is used when iterating over
       /// the contents of a DeclContext, e.g., via
       /// DeclContext::decls_begin()/DeclContext::decls_end().

Modified: cfe/trunk/include/clang/Frontend/PCHReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHReader.h?rev=109474&r1=109473&r2=109474&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Mon Jul 26 19:17:23 2010
@@ -307,8 +307,9 @@
   /// \brief Information about the contents of a DeclContext.
   struct DeclContextInfo {
     llvm::BitstreamCursor *Stream;
-    uint64_t OffsetToLexicalDecls;
     uint64_t OffsetToVisibleDecls;
+    const pch::DeclID *LexicalDecls;
+    unsigned NumLexicalDecls;
   };
   typedef llvm::SmallVector<DeclContextInfo, 1> DeclContextInfos;
   typedef llvm::DenseMap<const DeclContext *, DeclContextInfos>
@@ -318,6 +319,11 @@
   /// DeclContext.
   DeclContextOffsetsMap DeclContextOffsets;
 
+  /// \brief Read the records that describe the contents of declcontexts.
+  bool ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
+                              const std::pair<uint64_t, uint64_t> &Offsets,
+                              DeclContextInfo &Info);
+
   /// \brief A vector containing identifiers that have already been
   /// loaded.
   ///

Modified: cfe/trunk/include/clang/Frontend/PCHWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHWriter.h?rev=109474&r1=109473&r2=109474&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHWriter.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHWriter.h Mon Jul 26 19:17:23 2010
@@ -256,6 +256,7 @@
   void WriteAttributeRecord(const Attr *Attr);
 
   unsigned ParmVarDeclAbbrev;
+  unsigned DeclContextLexicalAbbrev;
   void WriteDeclsBlockAbbrevs();
   void WriteDecl(ASTContext &Context, Decl *D);
 
@@ -345,6 +346,9 @@
   /// \brief Emit a reference to a declaration.
   void AddDeclRef(const Decl *D, RecordData &Record);
 
+  /// \brief Force a declaration to be emitted and get its ID.
+  pch::DeclID GetDeclRef(const Decl *D);
+
   /// \brief Determine the declaration ID of an already-emitted
   /// declaration.
   pch::DeclID getDeclID(const Decl *D);

Modified: cfe/trunk/lib/AST/DeclBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=109474&r1=109473&r2=109474&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp (original)
+++ cfe/trunk/lib/AST/DeclBase.cpp Mon Jul 26 19:17:23 2010
@@ -715,6 +715,14 @@
   }
 }
 
+DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
+  return decl_iterator(FirstDecl);
+}
+
+DeclContext::decl_iterator DeclContext::noload_decls_end() const {
+  return decl_iterator();
+}
+
 DeclContext::decl_iterator DeclContext::decls_begin() const {
   if (hasExternalLexicalStorage())
     LoadLexicalDeclsFromExternalStorage();

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=109474&r1=109473&r2=109474&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Mon Jul 26 19:17:23 2010
@@ -1634,8 +1634,8 @@
       unsigned int numEl = Record[0]*2;
       for (unsigned int i = 1; i <= numEl; i++)
         F.ReferencedSelectorsData.push_back(Record[i]);
-    }
       break;
+    }
 
     case pch::PP_COUNTER_VALUE:
       if (!Record.empty() && Listener)
@@ -2896,30 +2896,15 @@
   DeclContextInfos &Infos = DeclContextOffsets[DC];
   for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
        I != E; ++I) {
-    uint64_t Offset = I->OffsetToLexicalDecls;
-    // Offset can be 0 if this file only contains visible decls.
-    if (Offset == 0)
+    // IDs can be 0 if this context doesn't contain declarations.
+    if (!I->LexicalDecls)
       continue;
-    llvm::BitstreamCursor &DeclsCursor = *I->Stream;
-
-    // Keep track of where we are in the stream, then jump back there
-    // after reading this context.
-    SavedStreamPosition SavedPosition(DeclsCursor);
-
-    // Load the record containing all of the declarations lexically in
-    // this context.
-    DeclsCursor.JumpToBit(Offset);
-    RecordData Record;
-    unsigned Code = DeclsCursor.ReadCode();
-    unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
-    if (RecCode != pch::DECL_CONTEXT_LEXICAL) {
-      Error("Expected lexical block");
-      return true;
-    }
 
     // Load all of the declaration IDs
-    for (RecordData::iterator J = Record.begin(), F = Record.end(); J != F; ++J)
-      Decls.push_back(GetDecl(*J));
+    for (const pch::DeclID *ID = I->LexicalDecls,
+                           *IDE = ID + I->NumLexicalDecls;
+         ID != IDE; ++ID)
+      Decls.push_back(GetDecl(*ID));
   }
 
   ++NumLexicalDeclContextsRead;

Modified: cfe/trunk/lib/Frontend/PCHReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReaderDecl.cpp?rev=109474&r1=109473&r2=109474&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderDecl.cpp Mon Jul 26 19:17:23 2010
@@ -1517,11 +1517,9 @@
     if (Offsets.first || Offsets.second) {
       DC->setHasExternalLexicalStorage(Offsets.first != 0);
       DC->setHasExternalVisibleStorage(Offsets.second != 0);
-      PCHReader::DeclContextInfo Info = {
-        Loc.first,
-        Offsets.first,
-        Offsets.second
-      };
+      DeclContextInfo Info;
+      if (ReadDeclContextStorage(DeclsCursor, Offsets, Info))
+        return 0;
       DeclContextOffsets[DC].push_back(Info);
     }
   }
@@ -1536,3 +1534,35 @@
 
   return D;
 }
+
+bool PCHReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
+                                   const std::pair<uint64_t, uint64_t> &Offsets,
+                                       DeclContextInfo &Info) {
+  SavedStreamPosition SavedPosition(Cursor);
+  // First the lexical decls.
+  if (Offsets.first != 0) {
+    Cursor.JumpToBit(Offsets.first);
+
+    RecordData Record;
+    const char *Blob;
+    unsigned BlobLen;
+    unsigned Code = Cursor.ReadCode();
+    unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
+    if (RecCode != pch::DECL_CONTEXT_LEXICAL) {
+      Error("Expected lexical block");
+      return true;
+    }
+
+    Info.LexicalDecls = reinterpret_cast<const pch::DeclID*>(Blob);
+    Info.NumLexicalDecls = BlobLen / sizeof(pch::DeclID);
+  } else {
+    Info.LexicalDecls = 0;
+    Info.NumLexicalDecls = 0;
+  }
+
+  // Now the visible decls.
+  Info.Stream = &Cursor;
+  Info.OffsetToVisibleDecls = Offsets.second;
+
+  return false;
+}

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=109474&r1=109473&r2=109474&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Mon Jul 26 19:17:23 2010
@@ -1391,11 +1391,12 @@
     ID = NextTypeID++;
 
   // Record the offset for this type.
-  if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
+  unsigned Index = ID - FirstTypeID;
+  if (TypeOffsets.size() == Index)
     TypeOffsets.push_back(Stream.GetCurrentBitNo());
-  else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
-    TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
-    TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
+  else if (TypeOffsets.size() < Index) {
+    TypeOffsets.resize(Index + 1);
+    TypeOffsets[Index] = Stream.GetCurrentBitNo();
   }
 
   RecordData Record;
@@ -1442,12 +1443,15 @@
 
   uint64_t Offset = Stream.GetCurrentBitNo();
   RecordData Record;
+  Record.push_back(pch::DECL_CONTEXT_LEXICAL);
+  llvm::SmallVector<pch::DeclID, 64> Decls;
   for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
          D != DEnd; ++D)
-    AddDeclRef(*D, Record);
+    Decls.push_back(GetDeclRef(*D));
 
   ++NumLexicalDeclContexts;
-  Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
+  Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
+     reinterpret_cast<char*>(Decls.data()), Decls.size() * sizeof(pch::DeclID));
   return Offset;
 }
 
@@ -2332,7 +2336,6 @@
 
   ASTContext &Context = SemaRef.Context;
   Preprocessor &PP = SemaRef.PP;
-  (void)PP;
 
   RecordData Record;
   Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
@@ -2351,16 +2354,15 @@
   // The TU was loaded before we managed to register ourselves as a listener.
   // Thus we need to add it manually.
   DeclIDs[TU] = 1;
-  // FIXME: We don't want to iterate over everything here, because it needlessly
-  // deserializes the entire original PCH. Instead we only want to iterate over
-  // the stuff that's already there.
-  // All in good time, though.
-  for (DeclContext::decl_iterator I = TU->decls_begin(), E = TU->decls_end();
+  Record.clear();
+  for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
+                                  E = TU->noload_decls_end();
        I != E; ++I) {
     if ((*I)->getPCHLevel() == 0) {
-      DeclTypesToEmit.push(*I);
+      AddDeclRef(*I, Record);
     }
   }
+  // We also need to write a lexical updates block for the TU.
 
   Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
   WriteDeclsBlockAbbrevs();
@@ -2584,9 +2586,12 @@
 }
 
 void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
+  Record.push_back(GetDeclRef(D));
+}
+
+pch::DeclID PCHWriter::GetDeclRef(const Decl *D) {
   if (D == 0) {
-    Record.push_back(0);
-    return;
+    return 0;
   }
 
   pch::DeclID &ID = DeclIDs[D];
@@ -2597,7 +2602,7 @@
     DeclTypesToEmit.push(const_cast<Decl *>(D));
   }
 
-  Record.push_back(ID);
+  return ID;
 }
 
 pch::DeclID PCHWriter::getDeclID(const Decl *D) {

Modified: cfe/trunk/lib/Frontend/PCHWriterDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriterDecl.cpp?rev=109474&r1=109473&r2=109474&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriterDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriterDecl.cpp Mon Jul 26 19:17:23 2010
@@ -1059,6 +1059,11 @@
   Abv->Add(BitCodeAbbrevOp(0));                   // HasUninstantiatedDefaultArg
 
   ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
+
+  Abv = new BitCodeAbbrev();
+  Abv->Add(BitCodeAbbrevOp(pch::DECL_CONTEXT_LEXICAL));
+  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
+  DeclContextLexicalAbbrev = Stream.EmitAbbrev(Abv);
 }
 
 /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by





More information about the cfe-commits mailing list