Sample profiles - Re-arrange binary format to emit head samples only on top functions.
[oota-llvm.git] / include / llvm / ProfileData / SampleProfWriter.h
index 302a82d3286122d044cb3eb10ef908e828bde8bc..4bd5c71e00228ad6ff31f1a11e1d01594409bab7 100644 (file)
@@ -13,6 +13,7 @@
 #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
 #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
 
+#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Module.h"
@@ -35,47 +36,38 @@ public:
       : OS(Filename, EC, Flags) {}
   virtual ~SampleProfileWriter() {}
 
-  /// \brief Write sample profiles in \p S for function \p FName.
+  /// Write sample profiles in \p S for function \p FName.
   ///
-  /// \returns true if the file was updated successfully. False, otherwise.
-  virtual bool write(StringRef FName, const FunctionSamples &S) = 0;
+  /// \returns status code of the file update operation.
+  virtual std::error_code write(StringRef FName, const FunctionSamples &S) = 0;
 
-  /// \brief Write sample profiles in \p S for function \p F.
-  bool write(const Function &F, const FunctionSamples &S) {
-    return write(F.getName(), S);
-  }
-
-  /// \brief Write all the sample profiles for all the functions in \p M.
+  /// Write all the sample profiles in the given map of samples.
   ///
-  /// \returns true if the file was updated successfully. False, otherwise.
-  bool write(const Module &M, StringMap<FunctionSamples> &P) {
-    for (const auto &F : M) {
-      StringRef Name = F.getName();
-      if (!write(Name, P[Name]))
-        return false;
-    }
-    return true;
-  }
+  /// \returns status code of the file update operation.
+  std::error_code write(const StringMap<FunctionSamples> &ProfileMap) {
+    if (std::error_code EC = writeHeader(ProfileMap))
+      return EC;
 
-  /// \brief Write all the sample profiles in the given map of samples.
-  ///
-  /// \returns true if the file was updated successfully. False, otherwise.
-  bool write(StringMap<FunctionSamples> &ProfileMap) {
-    for (auto &I : ProfileMap) {
+    for (const auto &I : ProfileMap) {
       StringRef FName = I.first();
-      FunctionSamples &Profile = I.second;
-      if (!write(FName, Profile))
-        return false;
+      const FunctionSamples &Profile = I.second;
+      if (std::error_code EC = write(FName, Profile))
+        return EC;
     }
-    return true;
+    return sampleprof_error::success;
   }
 
-  /// \brief Profile writer factory. Create a new writer based on the value of
-  /// \p Format.
+  /// Profile writer factory.
+  ///
+  /// Create a new writer based on the value of \p Format.
   static ErrorOr<std::unique_ptr<SampleProfileWriter>>
   create(StringRef Filename, SampleProfileFormat Format);
 
 protected:
+  /// \brief Write a file header for the profile file.
+  virtual std::error_code
+  writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
+
   /// \brief Output stream where to emit the profile to.
   raw_fd_ostream OS;
 };
@@ -84,23 +76,42 @@ protected:
 class SampleProfileWriterText : public SampleProfileWriter {
 public:
   SampleProfileWriterText(StringRef F, std::error_code &EC)
-      : SampleProfileWriter(F, EC, sys::fs::F_Text) {}
+      : SampleProfileWriter(F, EC, sys::fs::F_Text), Indent(0) {}
+
+  std::error_code write(StringRef FName, const FunctionSamples &S) override;
 
-  bool write(StringRef FName, const FunctionSamples &S) override;
-  bool write(const Module &M, StringMap<FunctionSamples> &P) {
-    return SampleProfileWriter::write(M, P);
+protected:
+  std::error_code
+  writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
+    return sampleprof_error::success;
   }
+
+private:
+  /// Indent level to use when writing.
+  ///
+  /// This is used when printing inlined callees.
+  unsigned Indent;
 };
 
 /// \brief Sample-based profile writer (binary format).
 class SampleProfileWriterBinary : public SampleProfileWriter {
 public:
-  SampleProfileWriterBinary(StringRef F, std::error_code &EC);
+  SampleProfileWriterBinary(StringRef F, std::error_code &EC)
+      : SampleProfileWriter(F, EC, sys::fs::F_None), NameTable() {}
 
-  bool write(StringRef F, const FunctionSamples &S) override;
-  bool write(const Module &M, StringMap<FunctionSamples> &P) {
-    return SampleProfileWriter::write(M, P);
-  }
+  std::error_code write(StringRef F, const FunctionSamples &S) override;
+
+protected:
+  std::error_code
+  writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
+  std::error_code writeNameIdx(StringRef FName);
+  std::error_code writeBody(StringRef FName, const FunctionSamples &S);
+
+private:
+  void addName(StringRef FName);
+  void addNames(const FunctionSamples &S);
+
+  MapVector<StringRef, uint32_t> NameTable;
 };
 
 } // End namespace sampleprof