[cfe-commits] r69407 - in /cfe/trunk: include/clang/AST/Decl.h include/clang/AST/ExternalASTSource.h include/clang/Frontend/PCHReader.h lib/AST/Decl.cpp lib/Frontend/PCHReader.cpp

Douglas Gregor dgregor at apple.com
Fri Apr 17 17:07:55 PDT 2009


Author: dgregor
Date: Fri Apr 17 19:07:54 2009
New Revision: 69407

URL: http://llvm.org/viewvc/llvm-project?rev=69407&view=rev
Log:
Lazy deserialization of function bodies for PCH files. For the Carbon
"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.


Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/include/clang/AST/ExternalASTSource.h
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/Frontend/PCHReader.cpp

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=69407&r1=69406&r2=69407&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Fri Apr 17 19:07:54 2009
@@ -565,7 +565,7 @@
   /// 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 @@
   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.

Modified: cfe/trunk/include/clang/AST/ExternalASTSource.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExternalASTSource.h?rev=69407&r1=69406&r2=69407&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ExternalASTSource.h (original)
+++ cfe/trunk/include/clang/AST/ExternalASTSource.h Fri Apr 17 19:07:54 2009
@@ -55,6 +55,13 @@
   /// 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 @@
   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

Modified: cfe/trunk/include/clang/Frontend/PCHReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHReader.h?rev=69407&r1=69406&r2=69407&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Fri Apr 17 19:07:54 2009
@@ -190,6 +190,13 @@
   /// 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.
   ///

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=69407&r1=69406&r2=69407&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Fri Apr 17 19:07:54 2009
@@ -308,8 +308,8 @@
 //===----------------------------------------------------------------------===//
 
 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 @@
   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::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;

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=69407&r1=69406&r2=69407&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Fri Apr 17 19:07:54 2009
@@ -140,7 +140,7 @@
 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 @@
   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() && 





More information about the cfe-commits mailing list