[llvm-profdata] Add support for weighted merge of profile data (2nd try)
[oota-llvm.git] / include / llvm / ProfileData / SampleProf.h
index aa715df162fabbb37c55af4770fecffd82e21191..7607e24ec1c84714cc38c94b60af1c384ea52bf0 100644 (file)
 // sample profile data.
 //
 //===----------------------------------------------------------------------===//
+
 #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H_
 #define LLVM_PROFILEDATA_SAMPLEPROF_H_
 
-#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/raw_ostream.h"
+
+#include <map>
 #include <system_error>
 
 namespace llvm {
@@ -34,6 +36,8 @@ enum class sampleprof_error {
   truncated,
   malformed,
   unrecognized_format,
+  unsupported_writing_format,
+  truncated_name_table,
   not_implemented
 };
 
@@ -59,7 +63,7 @@ static inline uint64_t SPMagic() {
          uint64_t('2') << (64 - 56) | uint64_t(0xff);
 }
 
-static inline uint64_t SPVersion() { return 100; }
+static inline uint64_t SPVersion() { return 102; }
 
 /// Represents the relative location of an instruction.
 ///
@@ -71,11 +75,20 @@ static inline uint64_t SPVersion() { return 100; }
 /// that are on the same line but belong to different basic blocks
 /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
 struct LineLocation {
-  LineLocation(int L, unsigned D) : LineOffset(L), Discriminator(D) {}
-  int LineOffset;
-  unsigned Discriminator;
+  LineLocation(uint32_t L, uint32_t D) : LineOffset(L), Discriminator(D) {}
+  void print(raw_ostream &OS) const;
+  void dump() const;
+  bool operator<(const LineLocation &O) const {
+    return LineOffset < O.LineOffset ||
+           (LineOffset == O.LineOffset && Discriminator < O.Discriminator);
+  }
+
+  uint32_t LineOffset;
+  uint32_t Discriminator;
 };
 
+raw_ostream &operator<<(raw_ostream &OS, const LineLocation &Loc);
+
 /// Represents the relative location of a callsite.
 ///
 /// Callsite locations are specified by the line offset from the
@@ -83,61 +96,15 @@ struct LineLocation {
 /// head is), the discriminator value within that line, and the callee
 /// function name.
 struct CallsiteLocation : public LineLocation {
-  CallsiteLocation(int L, unsigned D, StringRef N)
+  CallsiteLocation(uint32_t L, uint32_t D, StringRef N)
       : LineLocation(L, D), CalleeName(N) {}
-  StringRef CalleeName;
-};
-
-} // End namespace sampleprof
-
-template <> struct DenseMapInfo<sampleprof::LineLocation> {
-  typedef DenseMapInfo<int> OffsetInfo;
-  typedef DenseMapInfo<unsigned> DiscriminatorInfo;
-  static inline sampleprof::LineLocation getEmptyKey() {
-    return sampleprof::LineLocation(OffsetInfo::getEmptyKey(),
-                                    DiscriminatorInfo::getEmptyKey());
-  }
-  static inline sampleprof::LineLocation getTombstoneKey() {
-    return sampleprof::LineLocation(OffsetInfo::getTombstoneKey(),
-                                    DiscriminatorInfo::getTombstoneKey());
-  }
-  static inline unsigned getHashValue(sampleprof::LineLocation Val) {
-    return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(
-        std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));
-  }
-  static inline bool isEqual(sampleprof::LineLocation LHS,
-                             sampleprof::LineLocation RHS) {
-    return LHS.LineOffset == RHS.LineOffset &&
-           LHS.Discriminator == RHS.Discriminator;
-  }
-};
+  void print(raw_ostream &OS) const;
+  void dump() const;
 
-template <> struct DenseMapInfo<sampleprof::CallsiteLocation> {
-  typedef DenseMapInfo<int> OffsetInfo;
-  typedef DenseMapInfo<unsigned> DiscriminatorInfo;
-  typedef DenseMapInfo<StringRef> CalleeNameInfo;
-  static inline sampleprof::CallsiteLocation getEmptyKey() {
-    return sampleprof::CallsiteLocation(OffsetInfo::getEmptyKey(),
-                                        DiscriminatorInfo::getEmptyKey(), "");
-  }
-  static inline sampleprof::CallsiteLocation getTombstoneKey() {
-    return sampleprof::CallsiteLocation(OffsetInfo::getTombstoneKey(),
-                                        DiscriminatorInfo::getTombstoneKey(),
-                                        "");
-  }
-  static inline unsigned getHashValue(sampleprof::CallsiteLocation Val) {
-    return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(
-        std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));
-  }
-  static inline bool isEqual(sampleprof::CallsiteLocation LHS,
-                             sampleprof::CallsiteLocation RHS) {
-    return LHS.LineOffset == RHS.LineOffset &&
-           LHS.Discriminator == RHS.Discriminator &&
-           LHS.CalleeName.equals(RHS.CalleeName);
-  }
+  StringRef CalleeName;
 };
 
-namespace sampleprof {
+raw_ostream &operator<<(raw_ostream &OS, const CallsiteLocation &Loc);
 
 /// Representation of a single sample record.
 ///
@@ -151,54 +118,70 @@ namespace sampleprof {
 /// will be a list of one or more functions.
 class SampleRecord {
 public:
-  typedef StringMap<unsigned> CallTargetMap;
+  typedef StringMap<uint64_t> CallTargetMap;
 
   SampleRecord() : NumSamples(0), CallTargets() {}
 
   /// Increment the number of samples for this record by \p S.
+  /// Optionally scale sample count \p S by \p Weight.
   ///
   /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
   /// around unsigned integers.
-  void addSamples(unsigned S) {
-    if (NumSamples <= std::numeric_limits<unsigned>::max() - S)
-      NumSamples += S;
-    else
-      NumSamples = std::numeric_limits<unsigned>::max();
+  void addSamples(uint64_t S, uint64_t Weight = 1) {
+    // FIXME: Improve handling of counter overflow.
+    bool Overflowed;
+    if (Weight > 1) {
+      S = SaturatingMultiply(S, Weight, &Overflowed);
+      assert(!Overflowed && "Sample counter overflowed!");
+    }
+    NumSamples = SaturatingAdd(NumSamples, S, &Overflowed);
+    assert(!Overflowed && "Sample counter overflowed!");
   }
 
   /// Add called function \p F with samples \p S.
+  /// Optionally scale sample count \p S by \p Weight.
   ///
   /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
   /// around unsigned integers.
-  void addCalledTarget(StringRef F, unsigned S) {
-    unsigned &TargetSamples = CallTargets[F];
-    if (TargetSamples <= std::numeric_limits<unsigned>::max() - S)
-      TargetSamples += S;
-    else
-      TargetSamples = std::numeric_limits<unsigned>::max();
+  void addCalledTarget(StringRef F, uint64_t S, uint64_t Weight = 1) {
+    // FIXME: Improve handling of counter overflow.
+    uint64_t &TargetSamples = CallTargets[F];
+    bool Overflowed;
+    if (Weight > 1) {
+      S = SaturatingMultiply(S, Weight, &Overflowed);
+      assert(!Overflowed && "Called target counter overflowed!");
+    }
+    TargetSamples = SaturatingAdd(TargetSamples, S, &Overflowed);
+    assert(!Overflowed && "Called target counter overflowed!");
   }
 
   /// Return true if this sample record contains function calls.
   bool hasCalls() const { return CallTargets.size() > 0; }
 
-  unsigned getSamples() const { return NumSamples; }
+  uint64_t getSamples() const { return NumSamples; }
   const CallTargetMap &getCallTargets() const { return CallTargets; }
 
   /// Merge the samples in \p Other into this record.
-  void merge(const SampleRecord &Other) {
-    addSamples(Other.getSamples());
+  /// Optionally scale sample counts by \p Weight.
+  void merge(const SampleRecord &Other, uint64_t Weight = 1) {
+    addSamples(Other.getSamples(), Weight);
     for (const auto &I : Other.getCallTargets())
-      addCalledTarget(I.first(), I.second);
+      addCalledTarget(I.first(), I.second, Weight);
   }
 
+  void print(raw_ostream &OS, unsigned Indent) const;
+  void dump() const;
+
 private:
-  unsigned NumSamples;
+  uint64_t NumSamples;
   CallTargetMap CallTargets;
 };
 
-typedef DenseMap<LineLocation, SampleRecord> BodySampleMap;
+raw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample);
+
+typedef std::map<LineLocation, SampleRecord> BodySampleMap;
 class FunctionSamples;
-typedef DenseMap<CallsiteLocation, FunctionSamples> CallsiteSampleMap;
+typedef std::map<CallsiteLocation, FunctionSamples> CallsiteSampleMap;
 
 /// Representation of the samples collected for a function.
 ///
@@ -209,24 +192,44 @@ class FunctionSamples {
 public:
   FunctionSamples() : TotalSamples(0), TotalHeadSamples(0) {}
   void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const;
-  void addTotalSamples(unsigned Num) { TotalSamples += Num; }
-  void addHeadSamples(unsigned Num) { TotalHeadSamples += Num; }
-  void addBodySamples(int LineOffset, unsigned Discriminator, unsigned Num) {
-    assert(LineOffset >= 0);
-    BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
+  void dump() const;
+  void addTotalSamples(uint64_t Num, uint64_t Weight = 1) {
+    // FIXME: Improve handling of counter overflow.
+    bool Overflowed;
+    if (Weight > 1) {
+      Num = SaturatingMultiply(Num, Weight, &Overflowed);
+      assert(!Overflowed && "Total samples counter overflowed!");
+    }
+    TotalSamples = SaturatingAdd(TotalSamples, Num, &Overflowed);
+    assert(!Overflowed && "Total samples counter overflowed!");
+  }
+  void addHeadSamples(uint64_t Num, uint64_t Weight = 1) {
+    // FIXME: Improve handling of counter overflow.
+    bool Overflowed;
+    if (Weight > 1) {
+      Num = SaturatingMultiply(Num, Weight, &Overflowed);
+      assert(!Overflowed && "Total head samples counter overflowed!");
+    }
+    TotalHeadSamples = SaturatingAdd(TotalHeadSamples, Num, &Overflowed);
+    assert(!Overflowed && "Total head samples counter overflowed!");
+  }
+  void addBodySamples(uint32_t LineOffset, uint32_t Discriminator, uint64_t Num,
+                      uint64_t Weight = 1) {
+    BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num,
+                                                                    Weight);
   }
-  void addCalledTargetSamples(int LineOffset, unsigned Discriminator,
-                              std::string FName, unsigned Num) {
-    assert(LineOffset >= 0);
-    BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
-                                                                         Num);
+  void addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator,
+                              std::string FName, uint64_t Num,
+                              uint64_t Weight = 1) {
+    BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(
+        FName, Num, Weight);
   }
 
   /// Return the number of samples collected at the given location.
   /// Each location is specified by \p LineOffset and \p Discriminator.
   /// If the location is not found in profile, return error.
-  ErrorOr<unsigned> findSamplesAt(int LineOffset,
-                                  unsigned Discriminator) const {
+  ErrorOr<uint64_t> findSamplesAt(uint32_t LineOffset,
+                                  uint32_t Discriminator) const {
     const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
     if (ret == BodySamples.end())
       return std::error_code();
@@ -244,7 +247,7 @@ public:
   findFunctionSamplesAt(const CallsiteLocation &Loc) const {
     auto iter = CallsiteSamples.find(Loc);
     if (iter == CallsiteSamples.end()) {
-      return NULL;
+      return nullptr;
     } else {
       return &iter->second;
     }
@@ -253,11 +256,11 @@ public:
   bool empty() const { return TotalSamples == 0; }
 
   /// Return the total number of samples collected inside the function.
-  unsigned getTotalSamples() const { return TotalSamples; }
+  uint64_t getTotalSamples() const { return TotalSamples; }
 
   /// Return the total number of samples collected at the head of the
   /// function.
-  unsigned getHeadSamples() const { return TotalHeadSamples; }
+  uint64_t getHeadSamples() const { return TotalHeadSamples; }
 
   /// Return all the samples collected in the body of the function.
   const BodySampleMap &getBodySamples() const { return BodySamples; }
@@ -268,18 +271,19 @@ public:
   }
 
   /// Merge the samples in \p Other into this one.
-  void merge(const FunctionSamples &Other) {
-    addTotalSamples(Other.getTotalSamples());
-    addHeadSamples(Other.getHeadSamples());
+  /// Optionally scale samples by \p Weight.
+  void merge(const FunctionSamples &Other, uint64_t Weight = 1) {
+    addTotalSamples(Other.getTotalSamples(), Weight);
+    addHeadSamples(Other.getHeadSamples(), Weight);
     for (const auto &I : Other.getBodySamples()) {
       const LineLocation &Loc = I.first;
       const SampleRecord &Rec = I.second;
-      BodySamples[Loc].merge(Rec);
+      BodySamples[Loc].merge(Rec, Weight);
     }
     for (const auto &I : Other.getCallsiteSamples()) {
       const CallsiteLocation &Loc = I.first;
       const FunctionSamples &Rec = I.second;
-      functionSamplesAt(Loc).merge(Rec);
+      functionSamplesAt(Loc).merge(Rec, Weight);
     }
   }
 
@@ -288,12 +292,12 @@ private:
   ///
   /// Samples are cumulative, they include all the samples collected
   /// inside this function and all its inlined callees.
-  unsigned TotalSamples;
+  uint64_t TotalSamples;
 
   /// Total number of samples collected at the head of the function.
   /// This is an approximation of the number of calls made to this function
   /// at runtime.
-  unsigned TotalHeadSamples;
+  uint64_t TotalHeadSamples;
 
   /// Map instruction locations to collected samples.
   ///
@@ -321,8 +325,33 @@ private:
   CallsiteSampleMap CallsiteSamples;
 };
 
-} // End namespace sampleprof
+raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS);
 
-} // End namespace llvm
+/// Sort a LocationT->SampleT map by LocationT.
+///
+/// It produces a sorted list of <LocationT, SampleT> records by ascending
+/// order of LocationT.
+template <class LocationT, class SampleT> class SampleSorter {
+public:
+  typedef std::pair<const LocationT, SampleT> SamplesWithLoc;
+  typedef SmallVector<const SamplesWithLoc *, 20> SamplesWithLocList;
+
+  SampleSorter(const std::map<LocationT, SampleT> &Samples) {
+    for (const auto &I : Samples)
+      V.push_back(&I);
+    std::stable_sort(V.begin(), V.end(),
+                     [](const SamplesWithLoc *A, const SamplesWithLoc *B) {
+                       return A->first < B->first;
+                     });
+  }
+  const SamplesWithLocList &get() const { return V; }
+
+private:
+  SamplesWithLocList V;
+};
+
+} // end namespace sampleprof
+
+} // end namespace llvm
 
 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_