]> granicus.if.org Git - clang/commitdiff
Lazy deserialization of function bodies for PCH files. For the Carbon
authorDouglas Gregor <dgregor@apple.com>
Sat, 18 Apr 2009 00:07:54 +0000 (00:07 +0000)
committerDouglas Gregor <dgregor@apple.com>
Sat, 18 Apr 2009 00:07:54 +0000 (00:07 +0000)
"Hello, World!", this takes us from deserializing 6469
statements/expressions down to deserializing 1
statement/expression. It only translated into a 1% improvement on the
Carbon-prefixed 403.gcc, but (a) it's the right thing to do, and (b)
we expect this to matter more once we lazily deserialize identifiers.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69407 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Decl.h
include/clang/AST/ExternalASTSource.h
include/clang/Frontend/PCHReader.h
lib/AST/Decl.cpp
lib/Frontend/PCHReader.cpp

index 083f6c6363813af6c0dd797b77a9f217f17c7528..a5c2801b487043120425c73ff07d1eaf2542ce06 100644 (file)
@@ -565,7 +565,7 @@ private:
   /// FunctionDecl object to save an allocation like FunctionType does.
   ParmVarDecl **ParamInfo;
   
-  Stmt *Body;
+  LazyStmtPtr Body;
   
   /// PreviousDeclaration - A link to the previous declaration of this
   /// same function, NULL if this is the first declaration. For
@@ -641,6 +641,7 @@ public:
   bool isThisDeclarationADefinition() const { return Body; }
 
   void setBody(CompoundStmt *B) { Body = (Stmt*) B; }
+  void setLazyBody(uint64_t Offset) { Body = Offset; }
 
   /// Whether this function is virtual, either by explicit marking, or by
   /// overriding a virtual function. Only valid on C++ member functions.
index d5499d7b05f41e7e82d03aefab1caf19f05036bd..267b4838a467e4f8e2a0edb385b13883af6a7f38 100644 (file)
@@ -55,6 +55,13 @@ public:
   /// building a new declaration.
   virtual Decl *GetDecl(unsigned ID) = 0;
 
+  /// \brief Resolve the offset of a statement into a statement.
+  ///
+  /// This operation will read a new statement from the external
+  /// source each time it is called, and is meant to be used via a
+  /// LazyOffsetPtr.
+  virtual Stmt *GetStmt(uint64_t Offset) = 0;
+
   /// \brief Read all of the declarations lexically stored in a
   /// declaration context.
   ///
@@ -95,6 +102,72 @@ public:
   virtual void PrintStats();
 };
 
+/// \brief A lazy pointer to an AST node (of base type T) that resides
+/// within an external AST source.
+///
+/// The AST node is identified within the external AST source by a
+/// 63-bit offset, and can be retrieved via an operation on the
+/// external AST source itself.
+template<typename T, T* (ExternalASTSource::*Get)(uint64_t Offset)>
+struct LazyOffsetPtr {
+  /// \brief Either a pointer to an AST node or the offset within the
+  /// external AST source where the AST node can be found.
+  ///
+  /// If the low bit is clear, a pointer to the AST node. If the low
+  /// bit is set, the upper 63 bits are the offset.
+  mutable uint64_t Ptr;
+
+public:
+  LazyOffsetPtr() : Ptr(0) { }
+
+  explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
+  explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
+    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
+    if (Offset == 0)
+      Ptr = 0;
+  }
+
+  LazyOffsetPtr &operator=(T *Ptr) {
+    this->Ptr = reinterpret_cast<uint64_t>(Ptr);
+    return *this;
+  }
+  
+  LazyOffsetPtr &operator=(uint64_t Offset) {
+    assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
+    if (Offset == 0)
+      Ptr = 0;
+    else
+      Ptr = (Offset << 1) | 0x01;
+
+    return *this;
+  }
+
+  /// \brief Whether this pointer is non-NULL.
+  ///
+  /// This operation does not require the AST node to be deserialized.
+  operator bool() const { return Ptr != 0; }
+
+  /// \brief Whether this pointer is currently stored as an offset.
+  bool isOffset() const { return Ptr & 0x01; }
+
+  /// \brief Retrieve the pointer to the AST node that this lazy pointer
+  ///
+  /// \param Source the external AST source.
+  ///
+  /// \returns a pointer to the AST node.
+  T* get(ExternalASTSource *Source) const {
+    if (isOffset()) {
+      assert(Source && 
+             "Cannot deserialize a lazy pointer without an AST source");
+      Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
+    }
+    return reinterpret_cast<T*>(Ptr);
+  }
+};
+
+/// \brief A lazy pointer to a statement.
+typedef LazyOffsetPtr<Stmt, &ExternalASTSource::GetStmt> LazyStmtPtr;
+
 } // end namespace clang
 
 #endif // LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H
index c4f90a3c794714d70713a75f616dc050b88f95bd..34cf7bc1aadc9a991bb2e5467c4cb21242bb568a 100644 (file)
@@ -190,6 +190,13 @@ public:
   /// building a new declaration.
   virtual Decl *GetDecl(pch::DeclID ID);
 
+  /// \brief Resolve the offset of a statement into a statement.
+  ///
+  /// This operation will read a new statement from the external
+  /// source each time it is called, and is meant to be used via a
+  /// LazyOffsetPtr.
+  virtual Stmt *GetStmt(uint64_t Offset);
+
   /// \brief Read all of the declarations lexically stored in a
   /// declaration context.
   ///
index 5d49d706d7ecd72e0b618af56ab24d45e66b3bea..8bda32398fc5971625aeea5216f654249281265b 100644 (file)
@@ -308,8 +308,8 @@ const Expr *VarDecl::getDefinition(const VarDecl *&Def) const {
 //===----------------------------------------------------------------------===//
 
 void FunctionDecl::Destroy(ASTContext& C) {
-  if (Body)
-    Body->Destroy(C);
+  if (Body && Body.isOffset())
+    Body.get(C.getExternalSource())->Destroy(C);
 
   for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
     (*I)->Destroy(C);
@@ -325,7 +325,7 @@ CompoundStmt *FunctionDecl::getBody(ASTContext &Context,
   for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
     if (FD->Body) {
       Definition = FD;
-      return cast<CompoundStmt>(FD->Body);
+      return cast<CompoundStmt>(FD->Body.get(Context.getExternalSource()));
     }
   }
 
@@ -334,8 +334,9 @@ CompoundStmt *FunctionDecl::getBody(ASTContext &Context,
 
 CompoundStmt *FunctionDecl::getBodyIfAvailable() const {
   for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
-    if (FD->Body)
-      return cast<CompoundStmt>(FD->Body);
+    if (FD->Body && !FD->Body.isOffset()) {
+      return cast<CompoundStmt>(FD->Body.get(0));
+    }
   }
 
   return 0;
index 5ea1f7c6c2254e3ed56257d4ed3413fbd2e6bf40..dc8d3fce34fb15d158a5b94ba5f29cf411e55fa6 100644 (file)
@@ -140,7 +140,7 @@ void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
 void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
   VisitValueDecl(FD);
   if (Record[Idx++])
-    FD->setBody(cast<CompoundStmt>(Reader.ReadStmt()));
+    FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
   FD->setPreviousDeclaration(
                    cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
   FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
@@ -1880,6 +1880,15 @@ Decl *PCHReader::GetDecl(pch::DeclID ID) {
   return ReadDeclRecord(DeclOffsets[Index], Index);
 }
 
+Stmt *PCHReader::GetStmt(uint64_t Offset) {
+  // Keep track of where we are in the stream, then jump back there
+  // after reading this declaration.
+  SavedStreamPosition SavedPosition(Stream);
+
+  Stream.JumpToBit(Offset);
+  return ReadStmt();
+}
+
 bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
                                   llvm::SmallVectorImpl<pch::DeclID> &Decls) {
   assert(DC->hasExternalLexicalStorage() &&