[cfe-commits] r109890 - in /cfe/trunk: include/clang/Frontend/ASTUnit.h lib/Frontend/ASTUnit.cpp

Douglas Gregor dgregor at apple.com
Fri Jul 30 13:58:08 PDT 2010


Author: dgregor
Date: Fri Jul 30 15:58:08 2010
New Revision: 109890

URL: http://llvm.org/viewvc/llvm-project?rev=109890&view=rev
Log:
Add some timers to ASTUnit that are only enabled when the LIBCLANG_TIMING environment variable is set.

Modified:
    cfe/trunk/include/clang/Frontend/ASTUnit.h
    cfe/trunk/lib/Frontend/ASTUnit.cpp

Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=109890&r1=109889&r2=109890&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Fri Jul 30 15:58:08 2010
@@ -22,6 +22,7 @@
 #include "clang/Index/ASTLocation.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/System/Path.h"
+#include "llvm/Support/Timer.h"
 #include <map>
 #include <string>
 #include <vector>
@@ -119,7 +120,7 @@
   static const unsigned int CheckUnlocked = 9803453;
 
   /// \brief The file in which the precompiled preamble is stored.
-  llvm::sys::Path PreambleFile;
+  std::string PreambleFile;
   
   /// \brief The contents of the preamble that has been precompiled to
   /// \c PreambleFile.
@@ -140,6 +141,13 @@
   /// preamble.
   llvm::MemoryBuffer *SavedMainFileBuffer;
   
+  /// \brief The group of timers associated with this translation unit.
+  llvm::OwningPtr<llvm::TimerGroup> TimerGroup;
+  
+  /// \brief The timers we've created from the various parses, reparses, etc.
+  /// involved in this translation unit.
+  std::vector<llvm::Timer *> Timers;
+  
   ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
   

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=109890&r1=109889&r2=109890&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Fri Jul 30 15:58:08 2010
@@ -33,19 +33,21 @@
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/System/Host.h"
 #include "llvm/System/Path.h"
+#include "llvm/Support/Timer.h"
 #include <cstdlib>
 #include <cstdio>
 using namespace clang;
 
 ASTUnit::ASTUnit(bool _MainFileIsAST)
   : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST), 
-    ConcurrencyCheckValue(CheckUnlocked), SavedMainFileBuffer(0) { }
+    ConcurrencyCheckValue(CheckUnlocked), SavedMainFileBuffer(0) { 
+}
 
 ASTUnit::~ASTUnit() {
   ConcurrencyCheckValue = CheckLocked;
   CleanTemporaryFiles();
   if (!PreambleFile.empty())
-    PreambleFile.eraseFromDisk();
+    llvm::sys::Path(PreambleFile).eraseFromDisk();
   
   // Free the buffers associated with remapped files. We are required to
   // perform this operation here because we explicitly request that the
@@ -62,6 +64,9 @@
   }
   
   delete SavedMainFileBuffer;
+  
+  for (unsigned I = 0, N = Timers.size(); I != N; ++I)
+    delete Timers[I];
 }
 
 void ASTUnit::CleanTemporaryFiles() {
@@ -398,7 +403,7 @@
     PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
     PreprocessorOpts.PrecompiledPreambleBytes.second
                                                     = PreambleEndsAtStartOfLine;
-    PreprocessorOpts.ImplicitPCHInclude = PreambleFile.str();
+    PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
     PreprocessorOpts.DisablePCHValidation = true;
     
     // Keep track of the override buffer;
@@ -600,7 +605,7 @@
     // preamble, if we have one. It's obviously no good any more.
     Preamble.clear();
     if (!PreambleFile.empty()) {
-      PreambleFile.eraseFromDisk();
+      llvm::sys::Path(PreambleFile).eraseFromDisk();
       PreambleFile.clear();
     }
     if (CreatedPreambleBuffer)
@@ -633,10 +638,16 @@
     
     // We can't reuse the previously-computed preamble. Build a new one.
     Preamble.clear();
-    PreambleFile.eraseFromDisk();
+    llvm::sys::Path(PreambleFile).eraseFromDisk();
   } 
     
   // We did not previously compute a preamble, or it can't be reused anyway.
+  llvm::Timer *PreambleTimer = 0;
+  if (TimerGroup.get()) {
+    PreambleTimer = new llvm::Timer("Precompiling preamble", *TimerGroup);
+    PreambleTimer->startTimer();
+    Timers.push_back(PreambleTimer);
+  }
   
   // Create a new buffer that stores the preamble. The buffer also contains
   // extra space for the original contents of the file (which will be present
@@ -672,10 +683,7 @@
   FrontendOpts.ProgramAction = frontend::GeneratePCH;
   // FIXME: Set ChainedPCH, once it is ready.
   // FIXME: Generate the precompiled header into memory?
-  if (PreambleFile.isEmpty())
-    FrontendOpts.OutputFile = GetPreamblePCHPath();
-  else
-    FrontendOpts.OutputFile = PreambleFile.str();
+  FrontendOpts.OutputFile = GetPreamblePCHPath();
   
   // Create the compiler instance to use for building the precompiled preamble.
   CompilerInstance Clang;
@@ -695,6 +703,8 @@
     Preamble.clear();
     if (CreatedPreambleBuffer)
       delete NewPreamble.first;
+    if (PreambleTimer)
+      PreambleTimer->stopTimer();
 
     return 0;
   }
@@ -737,7 +747,9 @@
     Preamble.clear();
     if (CreatedPreambleBuffer)
       delete NewPreamble.first;
-    
+    if (PreambleTimer)
+      PreambleTimer->stopTimer();
+
     return 0;
   }
   
@@ -754,13 +766,19 @@
     Preamble.clear();
     if (CreatedPreambleBuffer)
       delete NewPreamble.first;
-    
+    if (PreambleTimer)
+      PreambleTimer->stopTimer();
+    if (PreambleTimer)
+      PreambleTimer->stopTimer();
+
     return 0;
   }
   
   // Keep track of the preamble we precompiled.
   PreambleFile = FrontendOpts.OutputFile;
-  fprintf(stderr, "Preamble PCH: %s\n", FrontendOpts.OutputFile.c_str());
+  if (PreambleTimer)
+    PreambleTimer->stopTimer();
+  
   return CreatePaddedMainFileBuffer(NewPreamble.first, 
                                     CreatedPreambleBuffer,
                                     PreambleReservedSize,
@@ -788,15 +806,28 @@
   AST->Invocation.reset(CI);
   CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
   
+  if (getenv("LIBCLANG_TIMING"))
+    AST->TimerGroup.reset(
+                  new llvm::TimerGroup(CI->getFrontendOpts().Inputs[0].second));
+  
+  
   llvm::MemoryBuffer *OverrideMainBuffer = 0;
   // FIXME: When C++ PCH is ready, allow use of it for a precompiled preamble.
   if (PrecompilePreamble && !CI->getLangOpts().CPlusPlus)
     OverrideMainBuffer = AST->BuildPrecompiledPreamble();
   
-  if (!AST->Parse(OverrideMainBuffer))
-    return AST.take();
+  llvm::Timer *ParsingTimer = 0;
+  if (AST->TimerGroup.get()) {
+    ParsingTimer = new llvm::Timer("Initial parse", *AST->TimerGroup);
+    ParsingTimer->startTimer();
+    AST->Timers.push_back(ParsingTimer);
+  }
   
-  return 0;
+  bool Failed = AST->Parse(OverrideMainBuffer);
+  if (ParsingTimer)
+    ParsingTimer->stopTimer();
+  
+  return Failed? 0 : AST.take();
 }
 
 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
@@ -875,6 +906,13 @@
   if (!Invocation.get())
     return true;
   
+  llvm::Timer *ReparsingTimer = 0;
+  if (TimerGroup.get()) {
+    ReparsingTimer = new llvm::Timer("Reparse", *TimerGroup);
+    ReparsingTimer->startTimer();
+    Timers.push_back(ReparsingTimer);
+  }
+  
   // If we have a preamble file lying around, build or reuse the precompiled
   // preamble.
   llvm::MemoryBuffer *OverrideMainBuffer = 0;
@@ -892,5 +930,7 @@
 
   // Parse the sources
   bool Result = Parse(OverrideMainBuffer);  
+  if (ReparsingTimer)
+    ReparsingTimer->stopTimer();
   return Result;
 }





More information about the cfe-commits mailing list