<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 18, 2015 at 1:54 PM, Xinliang David Li 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: davidxl<br>
Date: Wed Nov 18 15:54:40 2015<br>
New Revision: 253508<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=253508&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=253508&view=rev</a><br>
Log:<br>
[PGO] Minor cleanups (formating, comments etc) (NFC)<br>
1. Added missing public API decl in InstrProfiling.h<br>
2. Clang formatting fix<br>
3. Added more comments for new VP code<br>
4. refactor the VP allocation code to make it more readable.<br></blockquote><div><br></div><div>Please break these out into separate commits in the future.</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Modified:<br>
    compiler-rt/trunk/lib/profile/InstrProfiling.c<br>
    compiler-rt/trunk/lib/profile/InstrProfiling.h<br>
<br>
Modified: compiler-rt/trunk/lib/profile/InstrProfiling.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.c?rev=253508&r1=253507&r2=253508&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.c?rev=253508&r1=253507&r2=253508&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/profile/InstrProfiling.c (original)<br>
+++ compiler-rt/trunk/lib/profile/InstrProfiling.c Wed Nov 18 15:54:40 2015<br>
@@ -12,8 +12,7 @@<br>
 #include <stdlib.h><br>
 #include <string.h><br>
<br>
-__attribute__((visibility("hidden")))<br>
-uint64_t __llvm_profile_get_magic(void) {<br>
+__attribute__((visibility("hidden"))) uint64_t __llvm_profile_get_magic(void) {<br>
   /* Magic number to detect file format and endianness.<br>
    *<br>
    * Use 255 at one end, since no UTF-8 file can use that character.  Avoid 0,<br>
@@ -24,34 +23,29 @@ uint64_t __llvm_profile_get_magic(void)<br>
    * for 32-bit platforms.<br>
    */<br>
   unsigned char R = sizeof(void *) == sizeof(uint64_t) ? 'r' : 'R';<br>
-  return<br>
-    (uint64_t)255 << 56 |<br>
-    (uint64_t)'l' << 48 |<br>
-    (uint64_t)'p' << 40 |<br>
-    (uint64_t)'r' << 32 |<br>
-    (uint64_t)'o' << 24 |<br>
-    (uint64_t)'f' << 16 |<br>
-    (uint64_t) R  <<  8 |<br>
-    (uint64_t)129;<br>
+  return (uint64_t)255 << 56 | (uint64_t)'l' << 48 | (uint64_t)'p' << 40 |<br>
+         (uint64_t)'r' << 32 | (uint64_t)'o' << 24 | (uint64_t)'f' << 16 |<br>
+         (uint64_t)R << 8 | (uint64_t)129;<br>
 }<br>
<br>
-__attribute__((visibility("hidden")))<br>
-uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {<br>
+/* Return the number of bytes needed to add to SizeInBytes to make it<br>
+   the result a multiple of 8.  */<br>
+__attribute__((visibility("hidden"))) uint8_t<br>
+__llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {<br>
   return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));<br>
 }<br>
<br>
-__attribute__((visibility("hidden")))<br>
-uint64_t __llvm_profile_get_version(void) {<br>
+__attribute__((visibility("hidden"))) uint64_t<br>
+__llvm_profile_get_version(void) {<br>
   /* This should be bumped any time the output format changes. */<br>
   return 2;<br>
 }<br>
<br>
-__attribute__((visibility("hidden")))<br>
-void __llvm_profile_reset_counters(void) {<br>
+__attribute__((visibility("hidden"))) void __llvm_profile_reset_counters(void) {<br>
   uint64_t *I = __llvm_profile_begin_counters();<br>
   uint64_t *E = __llvm_profile_end_counters();<br>
<br>
-  memset(I, 0, sizeof(uint64_t)*(E - I));<br>
+  memset(I, 0, sizeof(uint64_t) * (E - I));<br>
<br>
   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();<br>
   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();<br>
@@ -76,33 +70,48 @@ void __llvm_profile_reset_counters(void)<br>
   }<br>
 }<br>
<br>
+// Total number of value profile data in bytes.<br>
 static uint64_t TotalValueDataSize = 0;<br>
<br>
-__attribute__((visibility("hidden")))<br>
-void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data_,<br>
-  uint32_t CounterIndex) {<br>
+/* Allocate an array that holds the pointers to the linked lists of<br>
+   value profile counter nodes. The number of element of the array<br>
+   is the total number of value profile sites instrumented. Returns<br>
+   0 if allocation fails.  */<br>
+<br>
+static int allocateValueProfileCounters(__llvm_profile_data *Data) {<br>
+  uint64_t NumVSites = 0;<br>
+  uint32_t VKI;<br>
+  for (VKI = VK_FIRST; VKI <= VK_LAST; ++VKI)<br>
+    NumVSites += Data->NumValueSites[VKI];<br>
+<br>
+  __llvm_profile_value_node **Mem = (__llvm_profile_value_node **)calloc(<br>
+      NumVSites, sizeof(__llvm_profile_value_node *));<br>
+  if (!Mem)<br>
+    return 0;<br>
+  if (!__sync_bool_compare_and_swap(&Data->ValueCounters, 0, Mem)) {<br>
+    free(Mem);<br>
+    return 0;<br>
+  }<br>
+  /* In the raw format, there will be an value count array preceding<br>
+     the value profile data. The element type of the array is uint8_t,<br>
+   and there is one element in array per value site. The element<br>
+   stores the number of values profiled for the corresponding site. */<br>
+  uint8_t Padding = __llvm_profile_get_num_padding_bytes(NumVSites);<br>
+  __sync_fetch_and_add(&TotalValueDataSize, NumVSites + Padding);<br>
+  return 1;<br>
+}<br>
+<br>
+__attribute__((visibility("hidden"))) void<br>
+__llvm_profile_instrument_target(uint64_t TargetValue, void *Data_,<br>
+                                 uint32_t CounterIndex) {<br>
<br>
-  __llvm_profile_data *Data = (__llvm_profile_data*)Data_;<br>
+  __llvm_profile_data *Data = (__llvm_profile_data *)Data_;<br>
   if (!Data)<br>
     return;<br>
<br>
   if (!Data->ValueCounters) {<br>
-    uint64_t NumVSites = 0;<br>
-    uint32_t VKI;<br>
-    for (VKI = VK_FIRST; VKI <= VK_LAST; ++VKI)<br>
-      NumVSites += Data->NumValueSites[VKI];<br>
-<br>
-    __llvm_profile_value_node** Mem = (__llvm_profile_value_node**)<br>
-        calloc(NumVSites, sizeof(__llvm_profile_value_node*));<br>
-    if (!Mem)<br>
-      return;<br>
-    if (!__sync_bool_compare_and_swap(&Data->ValueCounters, 0, Mem)) {<br>
-      free(Mem);<br>
+    if (!allocateValueProfileCounters(Data))<br>
       return;<br>
-    }<br>
-    // Acccount for padding during write out.<br>
-    uint8_t Padding = __llvm_profile_get_num_padding_bytes(NumVSites);<br>
-    __sync_fetch_and_add(&TotalValueDataSize, NumVSites + Padding);<br>
   }<br>
<br>
   __llvm_profile_value_node *PrevVNode = NULL;<br>
@@ -122,8 +131,8 @@ void __llvm_profile_instrument_target(ui<br>
   if (VDataCount >= UCHAR_MAX)<br>
     return;<br>
<br>
-  CurrentVNode = (__llvm_profile_value_node*)<br>
-    calloc(1, sizeof(__llvm_profile_value_node));<br>
+  CurrentVNode =<br>
+      (__llvm_profile_value_node *)calloc(1, sizeof(__llvm_profile_value_node));<br>
   if (!CurrentVNode)<br>
     return;<br>
<br>
@@ -132,8 +141,8 @@ void __llvm_profile_instrument_target(ui<br>
<br>
   uint32_t Success = 0;<br>
   if (!Data->ValueCounters[CounterIndex])<br>
-   Success = __sync_bool_compare_and_swap(<br>
-      &(Data->ValueCounters[CounterIndex]), 0, CurrentVNode);<br>
+    Success = __sync_bool_compare_and_swap(&(Data->ValueCounters[CounterIndex]),<br>
+                                           0, CurrentVNode);<br>
   else if (PrevVNode && !PrevVNode->Next)<br>
     Success = __sync_bool_compare_and_swap(&(PrevVNode->Next), 0, CurrentVNode);<br>
<br>
@@ -145,14 +154,14 @@ void __llvm_profile_instrument_target(ui<br>
                        Success * sizeof(__llvm_profile_value_data));<br>
 }<br>
<br>
-__attribute__((visibility("hidden")))<br>
-uint64_t __llvm_profile_gather_value_data(uint8_t **VDataArray) {<br>
+__attribute__((visibility("hidden"))) uint64_t<br>
+__llvm_profile_gather_value_data(uint8_t **VDataArray) {<br>
<br>
   if (!VDataArray || 0 == TotalValueDataSize)<br>
     return 0;<br>
<br>
   uint64_t NumData = TotalValueDataSize;<br>
-  *VDataArray = (uint8_t*) calloc(NumData, sizeof(uint8_t));<br>
+  *VDataArray = (uint8_t *)calloc(NumData, sizeof(uint8_t));<br>
   if (!*VDataArray)<br>
     return 0;<br>
<br>
@@ -161,8 +170,7 @@ uint64_t __llvm_profile_gather_value_dat<br>
   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();<br>
   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();<br>
   __llvm_profile_data *I;<br>
-  for (I = (__llvm_profile_data *)DataBegin;<br>
-       I != DataEnd; ++I) {<br>
+  for (I = (__llvm_profile_data *)DataBegin; I != DataEnd; ++I) {<br>
<br>
     uint64_t NumVSites = 0;<br>
     uint32_t VKI, i;<br>
@@ -183,7 +191,7 @@ uint64_t __llvm_profile_gather_value_dat<br>
       __llvm_profile_value_node *VNode = I->ValueCounters[i];<br>
<br>
       uint8_t VDataCount = 0;<br>
-      while (VNode && ((uint8_t*)(VDataPtr + 1) <= VDataEnd)) {<br>
+      while (VNode && ((uint8_t *)(VDataPtr + 1) <= VDataEnd)) {<br>
         *VDataPtr = VNode->VData;<br>
         VNode = VNode->Next;<br>
         ++VDataPtr;<br>
<br>
Modified: compiler-rt/trunk/lib/profile/InstrProfiling.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.h?rev=253508&r1=253507&r2=253508&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.h?rev=253508&r1=253507&r2=253508&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/profile/InstrProfiling.h (original)<br>
+++ compiler-rt/trunk/lib/profile/InstrProfiling.h Wed Nov 18 15:54:40 2015<br>
@@ -100,6 +100,12 @@ uint64_t *__llvm_profile_begin_counters(<br>
 uint64_t *__llvm_profile_end_counters(void);<br>
<br>
 /*!<br>
+ * \brief Clear profile counters to zero.<br>
+ *<br>
+ */<br>
+void __llvm_profile_reset_counters(void);<br>
+<br>
+/*!<br>
  * \brief Counts the number of times a target value is seen.<br>
  *<br>
  * Records the target value for the CounterIndex if not seen before. Otherwise,<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>