<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Oct 15, 2015 at 9:36 AM, Diego Novillo via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: dnovillo<br>
Date: Thu Oct 15 11:36:21 2015<br>
New Revision: 250427<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=250427&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=250427&view=rev</a><br>
Log:<br>
Sample Profiles - Adjust integer types. Mostly NFC.<br>
<br>
This adjusts all integers in the reader/writer to reflect the types<br>
stored on profile files. They should all be unsigned 32-bit or 64-bit<br>
values. Changed all associated internal types to be uint32_t or<br>
uint64_t.<br>
<br>
The only place that needed some adjustments is in the sample profile<br>
transformation. Altough the weight read from the profile are 64-bit<br>
values, the internal API for branch weights only accepts 32-bit values.<br>
The pass now saturates weights that overflow uint32_t.<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/ProfileData/SampleProf.h<br>
    llvm/trunk/include/llvm/ProfileData/SampleProfReader.h<br>
    llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h<br>
    llvm/trunk/lib/ProfileData/SampleProfReader.cpp<br>
    llvm/trunk/lib/ProfileData/SampleProfWriter.cpp<br>
    llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/ProfileData/SampleProf.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProf.h?rev=250427&r1=250426&r2=250427&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProf.h?rev=250427&r1=250426&r2=250427&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ProfileData/SampleProf.h (original)<br>
+++ llvm/trunk/include/llvm/ProfileData/SampleProf.h Thu Oct 15 11:36:21 2015<br>
@@ -73,9 +73,9 @@ static inline uint64_t SPVersion() { ret<br>
 /// that are on the same line but belong to different basic blocks<br>
 /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").<br>
 struct LineLocation {<br>
-  LineLocation(int L, unsigned D) : LineOffset(L), Discriminator(D) {}<br>
-  int LineOffset;<br>
-  unsigned Discriminator;<br>
+  LineLocation(uint32_t L, uint32_t D) : LineOffset(L), Discriminator(D) {}<br>
+  uint32_t LineOffset;<br></blockquote><div><br></div><div>There are a bunch of asserts in this file checking that LineOffset is >= 0.  This is now vacuously true.  Should the asserts be removed?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+  uint32_t Discriminator;<br>
 };<br>
<br>
 /// Represents the relative location of a callsite.<br>
@@ -85,7 +85,7 @@ struct LineLocation {<br>
 /// head is), the discriminator value within that line, and the callee<br>
 /// function name.<br>
 struct CallsiteLocation : public LineLocation {<br>
-  CallsiteLocation(int L, unsigned D, StringRef N)<br>
+  CallsiteLocation(uint32_t L, uint32_t D, StringRef N)<br>
       : LineLocation(L, D), CalleeName(N) {}<br>
   StringRef CalleeName;<br>
 };<br>
@@ -93,8 +93,8 @@ struct CallsiteLocation : public LineLoc<br>
 } // End namespace sampleprof<br>
<br>
 template <> struct DenseMapInfo<sampleprof::LineLocation> {<br>
-  typedef DenseMapInfo<int> OffsetInfo;<br>
-  typedef DenseMapInfo<unsigned> DiscriminatorInfo;<br>
+  typedef DenseMapInfo<uint32_t> OffsetInfo;<br>
+  typedef DenseMapInfo<uint32_t> DiscriminatorInfo;<br>
   static inline sampleprof::LineLocation getEmptyKey() {<br>
     return sampleprof::LineLocation(OffsetInfo::getEmptyKey(),<br>
                                     DiscriminatorInfo::getEmptyKey());<br>
@@ -104,8 +104,8 @@ template <> struct DenseMapInfo<samplepr<br>
                                     DiscriminatorInfo::getTombstoneKey());<br>
   }<br>
   static inline unsigned getHashValue(sampleprof::LineLocation Val) {<br>
-    return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(<br>
-        std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));<br>
+    return DenseMapInfo<std::pair<uint32_t, uint32_t>>::getHashValue(<br>
+        std::pair<uint32_t, uint32_t>(Val.LineOffset, Val.Discriminator));<br>
   }<br>
   static inline bool isEqual(sampleprof::LineLocation LHS,<br>
                              sampleprof::LineLocation RHS) {<br>
@@ -115,8 +115,8 @@ template <> struct DenseMapInfo<samplepr<br>
 };<br>
<br>
 template <> struct DenseMapInfo<sampleprof::CallsiteLocation> {<br>
-  typedef DenseMapInfo<int> OffsetInfo;<br>
-  typedef DenseMapInfo<unsigned> DiscriminatorInfo;<br>
+  typedef DenseMapInfo<uint32_t> OffsetInfo;<br>
+  typedef DenseMapInfo<uint32_t> DiscriminatorInfo;<br>
   typedef DenseMapInfo<StringRef> CalleeNameInfo;<br>
   static inline sampleprof::CallsiteLocation getEmptyKey() {<br>
     return sampleprof::CallsiteLocation(OffsetInfo::getEmptyKey(),<br>
@@ -128,8 +128,8 @@ template <> struct DenseMapInfo<samplepr<br>
                                         "");<br>
   }<br>
   static inline unsigned getHashValue(sampleprof::CallsiteLocation Val) {<br>
-    return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(<br>
-        std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));<br>
+    return DenseMapInfo<std::pair<uint32_t, uint32_t>>::getHashValue(<br>
+        std::pair<uint32_t, uint32_t>(Val.LineOffset, Val.Discriminator));<br>
   }<br>
   static inline bool isEqual(sampleprof::CallsiteLocation LHS,<br>
                              sampleprof::CallsiteLocation RHS) {<br>
@@ -153,7 +153,7 @@ namespace sampleprof {<br>
 /// will be a list of one or more functions.<br>
 class SampleRecord {<br>
 public:<br>
-  typedef StringMap<unsigned> CallTargetMap;<br>
+  typedef StringMap<uint64_t> CallTargetMap;<br>
<br>
   SampleRecord() : NumSamples(0), CallTargets() {}<br>
<br>
@@ -161,29 +161,29 @@ public:<br>
   ///<br>
   /// Sample counts accumulate using saturating arithmetic, to avoid wrapping<br>
   /// around unsigned integers.<br>
-  void addSamples(unsigned S) {<br>
-    if (NumSamples <= std::numeric_limits<unsigned>::max() - S)<br>
+  void addSamples(uint64_t S) {<br>
+    if (NumSamples <= std::numeric_limits<uint64_t>::max() - S)<br>
       NumSamples += S;<br>
     else<br>
-      NumSamples = std::numeric_limits<unsigned>::max();<br>
+      NumSamples = std::numeric_limits<uint64_t>::max();<br>
   }<br>
<br>
   /// Add called function \p F with samples \p S.<br>
   ///<br>
   /// Sample counts accumulate using saturating arithmetic, to avoid wrapping<br>
   /// around unsigned integers.<br>
-  void addCalledTarget(StringRef F, unsigned S) {<br>
-    unsigned &TargetSamples = CallTargets[F];<br>
-    if (TargetSamples <= std::numeric_limits<unsigned>::max() - S)<br>
+  void addCalledTarget(StringRef F, uint64_t S) {<br>
+    uint64_t &TargetSamples = CallTargets[F];<br>
+    if (TargetSamples <= std::numeric_limits<uint64_t>::max() - S)<br>
       TargetSamples += S;<br>
     else<br>
-      TargetSamples = std::numeric_limits<unsigned>::max();<br>
+      TargetSamples = std::numeric_limits<uint64_t>::max();<br>
   }<br>
<br>
   /// Return true if this sample record contains function calls.<br>
   bool hasCalls() const { return CallTargets.size() > 0; }<br>
<br>
-  unsigned getSamples() const { return NumSamples; }<br>
+  uint64_t getSamples() const { return NumSamples; }<br>
   const CallTargetMap &getCallTargets() const { return CallTargets; }<br>
<br>
   /// Merge the samples in \p Other into this record.<br>
@@ -194,7 +194,7 @@ public:<br>
   }<br>
<br>
 private:<br>
-  unsigned NumSamples;<br>
+  uint64_t NumSamples;<br>
   CallTargetMap CallTargets;<br>
 };<br>
<br>
@@ -211,14 +211,15 @@ class FunctionSamples {<br>
 public:<br>
   FunctionSamples() : TotalSamples(0), TotalHeadSamples(0) {}<br>
   void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const;<br>
-  void addTotalSamples(unsigned Num) { TotalSamples += Num; }<br>
-  void addHeadSamples(unsigned Num) { TotalHeadSamples += Num; }<br>
-  void addBodySamples(int LineOffset, unsigned Discriminator, unsigned Num) {<br>
+  void addTotalSamples(uint64_t Num) { TotalSamples += Num; }<br>
+  void addHeadSamples(uint64_t Num) { TotalHeadSamples += Num; }<br>
+  void addBodySamples(uint32_t LineOffset, uint32_t Discriminator,<br>
+                      uint64_t Num) {<br>
     assert(LineOffset >= 0);<br>
     BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);<br>
   }<br>
-  void addCalledTargetSamples(int LineOffset, unsigned Discriminator,<br>
-                              std::string FName, unsigned Num) {<br>
+  void addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator,<br>
+                              std::string FName, uint64_t Num) {<br>
     assert(LineOffset >= 0);<br>
     BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,<br>
                                                                          Num);<br>
@@ -227,8 +228,8 @@ public:<br>
   /// Return the number of samples collected at the given location.<br>
   /// Each location is specified by \p LineOffset and \p Discriminator.<br>
   /// If the location is not found in profile, return error.<br>
-  ErrorOr<unsigned> findSamplesAt(int LineOffset,<br>
-                                  unsigned Discriminator) const {<br>
+  ErrorOr<uint64_t> findSamplesAt(uint32_t LineOffset,<br>
+                                  uint32_t Discriminator) const {<br>
     const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));<br>
     if (ret == BodySamples.end())<br>
       return std::error_code();<br>
@@ -255,11 +256,11 @@ public:<br>
   bool empty() const { return TotalSamples == 0; }<br>
<br>
   /// Return the total number of samples collected inside the function.<br>
-  unsigned getTotalSamples() const { return TotalSamples; }<br>
+  uint64_t getTotalSamples() const { return TotalSamples; }<br>
<br>
   /// Return the total number of samples collected at the head of the<br>
   /// function.<br>
-  unsigned getHeadSamples() const { return TotalHeadSamples; }<br>
+  uint64_t getHeadSamples() const { return TotalHeadSamples; }<br>
<br>
   /// Return all the samples collected in the body of the function.<br>
   const BodySampleMap &getBodySamples() const { return BodySamples; }<br>
@@ -290,12 +291,12 @@ private:<br>
   ///<br>
   /// Samples are cumulative, they include all the samples collected<br>
   /// inside this function and all its inlined callees.<br>
-  unsigned TotalSamples;<br>
+  uint64_t TotalSamples;<br>
<br>
   /// Total number of samples collected at the head of the function.<br>
   /// This is an approximation of the number of calls made to this function<br>
   /// at runtime.<br>
-  unsigned TotalHeadSamples;<br>
+  uint64_t TotalHeadSamples;<br>
<br>
   /// Map instruction locations to collected samples.<br>
   ///<br>
<br>
Modified: llvm/trunk/include/llvm/ProfileData/SampleProfReader.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProfReader.h?rev=250427&r1=250426&r2=250427&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProfReader.h?rev=250427&r1=250426&r2=250427&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ProfileData/SampleProfReader.h (original)<br>
+++ llvm/trunk/include/llvm/ProfileData/SampleProfReader.h Thu Oct 15 11:36:21 2015<br>
@@ -138,10 +138,9 @@<br>
 // FUNCTION BODY (one for each uninlined function body present in the profile)<br>
 //    NAME_IDX (uint32_t)<br>
 //        Index into the name table indicating the function name.<br>
-//    SAMPLES (uint32_t)<br>
+//    SAMPLES (uint64_t)<br>
 //        Total number of samples collected in this function.<br>
-//        FIXME(dnovillo) this should be a uint64_t value.<br>
-//    HEAD_SAMPLES (uint32_t)<br>
+//    HEAD_SAMPLES (uint64_t)<br>
 //        Total number of samples collected at the head of the function.<br>
 //    NRECS (uint32_t)<br>
 //        Total number of sampling records this function's profile.<br>
<br>
Modified: llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h?rev=250427&r1=250426&r2=250427&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h?rev=250427&r1=250426&r2=250427&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h (original)<br>
+++ llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h Thu Oct 15 11:36:21 2015<br>
@@ -110,7 +110,7 @@ private:<br>
   void addName(StringRef FName);<br>
   void addNames(const FunctionSamples &S);<br>
<br>
-  MapVector<StringRef, unsigned> NameTable;<br>
+  MapVector<StringRef, uint32_t> NameTable;<br>
 };<br>
<br>
 } // End namespace sampleprof<br>
<br>
Modified: llvm/trunk/lib/ProfileData/SampleProfReader.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProfReader.cpp?rev=250427&r1=250426&r2=250427&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProfReader.cpp?rev=250427&r1=250426&r2=250427&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/ProfileData/SampleProfReader.cpp (original)<br>
+++ llvm/trunk/lib/ProfileData/SampleProfReader.cpp Thu Oct 15 11:36:21 2015<br>
@@ -87,7 +87,7 @@ void SampleProfileReader::dump(raw_ostre<br>
 ///<br>
 /// \returns true if parsing is successful.<br>
 static bool ParseHead(const StringRef &Input, StringRef &FName,<br>
-                      unsigned &NumSamples, unsigned &NumHeadSamples) {<br>
+                      uint64_t &NumSamples, uint64_t &NumHeadSamples) {<br>
   if (Input[0] == ' ')<br>
     return false;<br>
   size_t n2 = Input.rfind(':');<br>
@@ -111,10 +111,10 @@ static bool ParseHead(const StringRef &I<br>
 /// \param TargetCountMap map from indirect call target to count.<br>
 ///<br>
 /// returns true if parsing is successful.<br>
-static bool ParseLine(const StringRef &Input, bool &IsCallsite, unsigned &Depth,<br>
-                      unsigned &NumSamples, unsigned &LineOffset,<br>
-                      unsigned &Discriminator, StringRef &CalleeName,<br>
-                      DenseMap<StringRef, unsigned> &TargetCountMap) {<br>
+static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth,<br>
+                      uint64_t &NumSamples, uint32_t &LineOffset,<br>
+                      uint32_t &Discriminator, StringRef &CalleeName,<br>
+                      DenseMap<StringRef, uint64_t> &TargetCountMap) {<br>
   for (Depth = 0; Input[Depth] == ' '; Depth++)<br>
     ;<br>
   if (Depth == 0)<br>
@@ -153,15 +153,15 @@ static bool ParseLine(const StringRef &I<br>
       if (n3 != StringRef::npos) {<br>
         pair = Rest.substr(0, n3);<br>
       }<br>
-      int n4 = pair.find(':');<br>
-      unsigned count;<br>
+      size_t n4 = pair.find(':');<br>
+      uint64_t count;<br>
       if (pair.substr(n4 + 1).getAsInteger(10, count))<br>
         return false;<br>
       TargetCountMap[pair.substr(0, n4)] = count;<br>
     }<br>
   } else {<br>
     IsCallsite = true;<br>
-    int n3 = Rest.find_last_of(':');<br>
+    size_t n3 = Rest.find_last_of(':');<br>
     CalleeName = Rest.substr(0, n3);<br>
     if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples))<br>
       return false;<br>
@@ -196,7 +196,7 @@ std::error_code SampleProfileReaderText:<br>
     // The only requirement we place on the identifier, then, is that it<br>
     // should not begin with a number.<br>
     if ((*LineIt)[0] != ' ') {<br>
-      unsigned NumSamples, NumHeadSamples;<br>
+      uint64_t NumSamples, NumHeadSamples;<br>
       StringRef FName;<br>
       if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {<br>
         reportError(LineIt.line_number(),<br>
@@ -210,11 +210,11 @@ std::error_code SampleProfileReaderText:<br>
       InlineStack.clear();<br>
       InlineStack.push_back(&FProfile);<br>
     } else {<br>
-      unsigned NumSamples;<br>
+      uint64_t NumSamples;<br>
       StringRef FName;<br>
-      DenseMap<StringRef, unsigned> TargetCountMap;<br>
+      DenseMap<StringRef, uint64_t> TargetCountMap;<br>
       bool IsCallsite;<br>
-      unsigned Depth, LineOffset, Discriminator;<br>
+      uint32_t Depth, LineOffset, Discriminator;<br>
       if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset,<br>
                      Discriminator, FName, TargetCountMap)) {<br>
         reportError(LineIt.line_number(),<br>
@@ -283,7 +283,7 @@ ErrorOr<StringRef> SampleProfileReaderBi<br>
<br>
 ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() {<br>
   std::error_code EC;<br>
-  auto Idx = readNumber<unsigned>();<br>
+  auto Idx = readNumber<uint32_t>();<br>
   if (std::error_code EC = Idx.getError())<br>
     return EC;<br>
   if (*Idx >= NameTable.size())<br>
@@ -293,22 +293,22 @@ ErrorOr<StringRef> SampleProfileReaderBi<br>
<br>
 std::error_code<br>
 SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {<br>
-  auto Val = readNumber<unsigned>();<br>
+  auto Val = readNumber<uint64_t>();<br>
   if (std::error_code EC = Val.getError())<br>
     return EC;<br>
   FProfile.addTotalSamples(*Val);<br>
<br>
-  Val = readNumber<unsigned>();<br>
+  Val = readNumber<uint64_t>();<br>
   if (std::error_code EC = Val.getError())<br>
     return EC;<br>
   FProfile.addHeadSamples(*Val);<br>
<br>
   // Read the samples in the body.<br>
-  auto NumRecords = readNumber<unsigned>();<br>
+  auto NumRecords = readNumber<uint32_t>();<br>
   if (std::error_code EC = NumRecords.getError())<br>
     return EC;<br>
<br>
-  for (unsigned I = 0; I < *NumRecords; ++I) {<br>
+  for (uint32_t I = 0; I < *NumRecords; ++I) {<br>
     auto LineOffset = readNumber<uint64_t>();<br>
     if (std::error_code EC = LineOffset.getError())<br>
       return EC;<br>
@@ -321,11 +321,11 @@ SampleProfileReaderBinary::readProfile(F<br>
     if (std::error_code EC = NumSamples.getError())<br>
       return EC;<br>
<br>
-    auto NumCalls = readNumber<unsigned>();<br>
+    auto NumCalls = readNumber<uint32_t>();<br>
     if (std::error_code EC = NumCalls.getError())<br>
       return EC;<br>
<br>
-    for (unsigned J = 0; J < *NumCalls; ++J) {<br>
+    for (uint32_t J = 0; J < *NumCalls; ++J) {<br>
       auto CalledFunction(readStringFromTable());<br>
       if (std::error_code EC = CalledFunction.getError())<br>
         return EC;<br>
@@ -342,11 +342,11 @@ SampleProfileReaderBinary::readProfile(F<br>
   }<br>
<br>
   // Read all the samples for inlined function calls.<br>
-  auto NumCallsites = readNumber<unsigned>();<br>
+  auto NumCallsites = readNumber<uint32_t>();<br>
   if (std::error_code EC = NumCallsites.getError())<br>
     return EC;<br>
<br>
-  for (unsigned J = 0; J < *NumCallsites; ++J) {<br>
+  for (uint32_t J = 0; J < *NumCallsites; ++J) {<br>
     auto LineOffset = readNumber<uint64_t>();<br>
     if (std::error_code EC = LineOffset.getError())<br>
       return EC;<br>
@@ -403,11 +403,11 @@ std::error_code SampleProfileReaderBinar<br>
     return sampleprof_error::unsupported_version;<br>
<br>
   // Read the name table.<br>
-  auto Size = readNumber<size_t>();<br>
+  auto Size = readNumber<uint32_t>();<br>
   if (std::error_code EC = Size.getError())<br>
     return EC;<br>
   NameTable.reserve(*Size);<br>
-  for (size_t I = 0; I < *Size; ++I) {<br>
+  for (uint32_t I = 0; I < *Size; ++I) {<br>
     auto Name(readString());<br>
     if (std::error_code EC = Name.getError())<br>
       return EC;<br>
@@ -678,7 +678,7 @@ setupMemoryBuffer(std::string Filename)<br>
   auto Buffer = std::move(BufferOrErr.get());<br>
<br>
   // Sanity check the file.<br>
-  if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())<br>
+  if (Buffer->getBufferSize() > std::numeric_limits<uint32_t>::max())<br>
     return sampleprof_error::too_large;<br>
<br>
   return std::move(Buffer);<br>
<br>
Modified: llvm/trunk/lib/ProfileData/SampleProfWriter.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProfWriter.cpp?rev=250427&r1=250426&r2=250427&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProfWriter.cpp?rev=250427&r1=250426&r2=250427&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/ProfileData/SampleProfWriter.cpp (original)<br>
+++ llvm/trunk/lib/ProfileData/SampleProfWriter.cpp Thu Oct 15 11:36:21 2015<br>
@@ -144,7 +144,7 @@ std::error_code SampleProfileWriterBinar<br>
     encodeULEB128(Sample.getCallTargets().size(), OS);<br>
     for (const auto &J : Sample.getCallTargets()) {<br>
       StringRef Callee = J.first();<br>
-      unsigned CalleeSamples = J.second;<br>
+      uint64_t CalleeSamples = J.second;<br>
       if (std::error_code EC = writeNameIdx(Callee))<br>
         return EC;<br>
       encodeULEB128(CalleeSamples, OS);<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp?rev=250427&r1=250426&r2=250427&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp?rev=250427&r1=250426&r2=250427&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp Thu Oct 15 11:36:21 2015<br>
@@ -65,10 +65,10 @@ static cl::opt<unsigned> SampleProfileMa<br>
              "sample block/edge weights through the CFG."));<br>
<br>
 namespace {<br>
-typedef DenseMap<const BasicBlock *, unsigned> BlockWeightMap;<br>
+typedef DenseMap<const BasicBlock *, uint64_t> BlockWeightMap;<br>
 typedef DenseMap<const BasicBlock *, const BasicBlock *> EquivalenceClassMap;<br>
 typedef std::pair<const BasicBlock *, const BasicBlock *> Edge;<br>
-typedef DenseMap<Edge, unsigned> EdgeWeightMap;<br>
+typedef DenseMap<Edge, uint64_t> EdgeWeightMap;<br>
 typedef DenseMap<const BasicBlock *, SmallVector<const BasicBlock *, 8>><br>
     BlockEdgeMap;<br>
<br>
@@ -104,8 +104,8 @@ protected:<br>
   bool runOnFunction(Function &F);<br>
   unsigned getFunctionLoc(Function &F);<br>
   bool emitAnnotations(Function &F);<br>
-  ErrorOr<unsigned> getInstWeight(const Instruction &I) const;<br>
-  ErrorOr<unsigned> getBlockWeight(const BasicBlock *BB) const;<br>
+  ErrorOr<uint64_t> getInstWeight(const Instruction &I) const;<br>
+  ErrorOr<uint64_t> getBlockWeight(const BasicBlock *BB) const;<br>
   const FunctionSamples *findCalleeFunctionSamples(const CallInst &I) const;<br>
   const FunctionSamples *findFunctionSamples(const Instruction &I) const;<br>
   bool inlineHotFunctions(Function &F);<br>
@@ -118,7 +118,7 @@ protected:<br>
                            SmallVector<BasicBlock *, 8> Descendants,<br>
                            DominatorTreeBase<BasicBlock> *DomTree);<br>
   void propagateWeights(Function &F);<br>
-  unsigned visitEdge(Edge E, unsigned *NumUnknownEdges, Edge *UnknownEdge);<br>
+  uint64_t visitEdge(Edge E, unsigned *NumUnknownEdges, Edge *UnknownEdge);<br>
   void buildEdges(Function &F);<br>
   bool propagateThroughEdges(Function &F);<br>
   void computeDominanceAndLoopInfo(Function &F);<br>
@@ -201,7 +201,7 @@ void SampleProfileLoader::printBlockEqui<br>
 void SampleProfileLoader::printBlockWeight(raw_ostream &OS,<br>
                                            const BasicBlock *BB) const {<br>
   const auto &I = BlockWeights.find(BB);<br>
-  unsigned W = (I == BlockWeights.end() ? 0 : I->second);<br>
+  uint64_t W = (I == BlockWeights.end() ? 0 : I->second);<br>
   OS << "weight[" << BB->getName() << "]: " << W << "\n";<br>
 }<br>
<br>
@@ -216,7 +216,7 @@ void SampleProfileLoader::printBlockWeig<br>
 /// \param Inst Instruction to query.<br>
 ///<br>
 /// \returns the weight of \p Inst.<br>
-ErrorOr<unsigned><br>
+ErrorOr<uint64_t><br>
 SampleProfileLoader::getInstWeight(const Instruction &Inst) const {<br>
   DebugLoc DLoc = Inst.getDebugLoc();<br>
   if (!DLoc)<br>
@@ -232,7 +232,7 @@ SampleProfileLoader::getInstWeight(const<br>
   if (Lineno < HeaderLineno)<br>
     return std::error_code();<br>
<br>
-  ErrorOr<unsigned> R =<br>
+  ErrorOr<uint64_t> R =<br>
       FS->findSamplesAt(Lineno - HeaderLineno, DIL->getDiscriminator());<br>
   if (R)<br>
     DEBUG(dbgs() << "    " << Lineno << "." << DIL->getDiscriminator() << ":"<br>
@@ -250,12 +250,12 @@ SampleProfileLoader::getInstWeight(const<br>
 /// \param BB The basic block to query.<br>
 ///<br>
 /// \returns the weight for \p BB.<br>
-ErrorOr<unsigned><br>
+ErrorOr<uint64_t><br>
 SampleProfileLoader::getBlockWeight(const BasicBlock *BB) const {<br>
   bool Found = false;<br>
-  unsigned Weight = 0;<br>
+  uint64_t Weight = 0;<br>
   for (auto &I : BB->getInstList()) {<br>
-    const ErrorOr<unsigned> &R = getInstWeight(I);<br>
+    const ErrorOr<uint64_t> &R = getInstWeight(I);<br>
     if (R && R.get() >= Weight) {<br>
       Weight = R.get();<br>
       Found = true;<br>
@@ -277,7 +277,7 @@ bool SampleProfileLoader::computeBlockWe<br>
   bool Changed = false;<br>
   DEBUG(dbgs() << "Block weights\n");<br>
   for (const auto &BB : F) {<br>
-    ErrorOr<unsigned> Weight = getBlockWeight(&BB);<br>
+    ErrorOr<uint64_t> Weight = getBlockWeight(&BB);<br>
     if (Weight) {<br>
       BlockWeights[&BB] = Weight.get();<br>
       VisitedBlocks.insert(&BB);<br>
@@ -431,7 +431,7 @@ void SampleProfileLoader::findEquivalenc<br>
     BasicBlock *BB1, SmallVector<BasicBlock *, 8> Descendants,<br>
     DominatorTreeBase<BasicBlock> *DomTree) {<br>
   const BasicBlock *EC = EquivalenceClass[BB1];<br>
-  unsigned Weight = BlockWeights[EC];<br>
+  uint64_t Weight = BlockWeights[EC];<br>
   for (const auto *BB2 : Descendants) {<br>
     bool IsDomParent = DomTree->dominates(BB2, BB1);<br>
     bool IsInSameLoop = LI->getLoopFor(BB1) == LI->getLoopFor(BB2);<br>
@@ -520,7 +520,7 @@ void SampleProfileLoader::findEquivalenc<br>
 /// \param UnknownEdge  Set if E has not been visited before.<br>
 ///<br>
 /// \returns E's weight, if known. Otherwise, return 0.<br>
-unsigned SampleProfileLoader::visitEdge(Edge E, unsigned *NumUnknownEdges,<br>
+uint64_t SampleProfileLoader::visitEdge(Edge E, unsigned *NumUnknownEdges,<br>
                                         Edge *UnknownEdge) {<br>
   if (!VisitedEdges.count(E)) {<br>
     (*NumUnknownEdges)++;<br>
@@ -555,7 +555,7 @@ bool SampleProfileLoader::propagateThrou<br>
     // only case we are interested in handling is when only a single<br>
     // edge is unknown (see setEdgeOrBlockWeight).<br>
     for (unsigned i = 0; i < 2; i++) {<br>
-      unsigned TotalWeight = 0;<br>
+      uint64_t TotalWeight = 0;<br>
       unsigned NumUnknownEdges = 0;<br>
       Edge UnknownEdge, SelfReferentialEdge;<br>
<br>
@@ -599,7 +599,7 @@ bool SampleProfileLoader::propagateThrou<br>
       // all edges will get a weight, or iteration will stop when<br>
       // it reaches SampleProfileMaxPropagateIterations.<br>
       if (NumUnknownEdges <= 1) {<br>
-        unsigned &BBWeight = BlockWeights[EC];<br>
+        uint64_t &BBWeight = BlockWeights[EC];<br>
         if (NumUnknownEdges == 0) {<br>
           // If we already know the weight of all edges, the weight of the<br>
           // basic block can be computed. It should be no larger than the sum<br>
@@ -626,7 +626,7 @@ bool SampleProfileLoader::propagateThrou<br>
                 printEdgeWeight(dbgs(), UnknownEdge));<br>
         }<br>
       } else if (SelfReferentialEdge.first && VisitedBlocks.count(EC)) {<br>
-        unsigned &BBWeight = BlockWeights[BB];<br>
+        uint64_t &BBWeight = BlockWeights[BB];<br>
         // We have a self-referential edge and the weight of BB is known.<br>
         if (BBWeight >= TotalWeight)<br>
           EdgeWeights[SelfReferentialEdge] = BBWeight - TotalWeight;<br>
@@ -692,7 +692,7 @@ void SampleProfileLoader::buildEdges(Fun<br>
 ///   known).<br>
 void SampleProfileLoader::propagateWeights(Function &F) {<br>
   bool Changed = true;<br>
-  unsigned i = 0;<br>
+  unsigned I = 0;<br>
<br>
   // Add an entry count to the function using the samples gathered<br>
   // at the function entry.<br>
@@ -706,7 +706,7 @@ void SampleProfileLoader::propagateWeigh<br>
   buildEdges(F);<br>
<br>
   // Propagate until we converge or we go past the iteration limit.<br>
-  while (Changed && i++ < SampleProfileMaxPropagateIterations) {<br>
+  while (Changed && I++ < SampleProfileMaxPropagateIterations) {<br>
     Changed = propagateThroughEdges(F);<br>
   }<br>
<br>
@@ -724,14 +724,21 @@ void SampleProfileLoader::propagateWeigh<br>
<br>
     DEBUG(dbgs() << "\nGetting weights for branch at line "<br>
                  << TI->getDebugLoc().getLine() << ".\n");<br>
-    SmallVector<unsigned, 4> Weights;<br>
+    SmallVector<uint32_t, 4> Weights;<br>
     bool AllWeightsZero = true;<br>
     for (unsigned I = 0; I < TI->getNumSuccessors(); ++I) {<br>
       BasicBlock *Succ = TI->getSuccessor(I);<br>
       Edge E = std::make_pair(BB, Succ);<br>
-      unsigned Weight = EdgeWeights[E];<br>
+      uint64_t Weight = EdgeWeights[E];<br>
       DEBUG(dbgs() << "\t"; printEdgeWeight(dbgs(), E));<br>
-      Weights.push_back(Weight);<br>
+      // Use uint32_t saturated arithmetic to adjust the incoming weights,<br>
+      // if needed. Sample counts in profiles are 64-bit unsigned values,<br>
+      // but internally branch weights are expressed as 32-bit values.<br>
+      if (Weight > std::numeric_limits<uint32_t>::max()) {<br>
+        DEBUG(dbgs() << " (saturated due to uint32_t overflow)");<br>
+        Weight = std::numeric_limits<uint32_t>::max();<br>
+      }<br>
+      Weights.push_back(static_cast<uint32_t>(Weight));<br>
       if (Weight != 0)<br>
         AllWeightsZero = false;<br>
     }<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>