xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingValue.c (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
10b57cec5SDimitry Andric /*===- InstrProfilingValue.c - Support library for PGO instrumentation ----===*\
20b57cec5SDimitry Andric |*
30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric |*
70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
80b57cec5SDimitry Andric 
9e8d8bef9SDimitry Andric #include <assert.h>
100b57cec5SDimitry Andric #include <limits.h>
110b57cec5SDimitry Andric #include <stdio.h>
120b57cec5SDimitry Andric #include <stdlib.h>
130b57cec5SDimitry Andric #include <string.h>
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "InstrProfiling.h"
160b57cec5SDimitry Andric #include "InstrProfilingInternal.h"
170b57cec5SDimitry Andric #include "InstrProfilingUtil.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #define INSTR_PROF_VALUE_PROF_DATA
200b57cec5SDimitry Andric #define INSTR_PROF_COMMON_API_IMPL
21e8d8bef9SDimitry Andric #define INSTR_PROF_VALUE_PROF_MEMOP_API
22480093f4SDimitry Andric #include "profile/InstrProfData.inc"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric static int hasStaticCounters = 1;
250b57cec5SDimitry Andric static int OutOfNodesWarnings = 0;
260b57cec5SDimitry Andric static int hasNonDefaultValsPerSite = 0;
270b57cec5SDimitry Andric #define INSTR_PROF_MAX_VP_WARNS 10
28e8d8bef9SDimitry Andric #define INSTR_PROF_DEFAULT_NUM_VAL_PER_SITE 24
290b57cec5SDimitry Andric #define INSTR_PROF_VNODE_POOL_SIZE 1024
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric #ifndef _MSC_VER
320b57cec5SDimitry Andric /* A shared static pool in addition to the vnodes statically
330b57cec5SDimitry Andric  * allocated by the compiler.  */
340b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode
350b57cec5SDimitry Andric     lprofValueProfNodes[INSTR_PROF_VNODE_POOL_SIZE] COMPILER_RT_SECTION(
360b57cec5SDimitry Andric        COMPILER_RT_SEG INSTR_PROF_VNODES_SECT_NAME);
370b57cec5SDimitry Andric #endif
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint32_t VPMaxNumValsPerSite =
400b57cec5SDimitry Andric     INSTR_PROF_DEFAULT_NUM_VAL_PER_SITE;
410b57cec5SDimitry Andric 
lprofSetupValueProfiler(void)4281ad6265SDimitry Andric COMPILER_RT_VISIBILITY void lprofSetupValueProfiler(void) {
430b57cec5SDimitry Andric   const char *Str = 0;
440b57cec5SDimitry Andric   Str = getenv("LLVM_VP_MAX_NUM_VALS_PER_SITE");
450b57cec5SDimitry Andric   if (Str && Str[0]) {
460b57cec5SDimitry Andric     VPMaxNumValsPerSite = atoi(Str);
470b57cec5SDimitry Andric     hasNonDefaultValsPerSite = 1;
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric   if (VPMaxNumValsPerSite > INSTR_PROF_MAX_NUM_VAL_PER_SITE)
500b57cec5SDimitry Andric     VPMaxNumValsPerSite = INSTR_PROF_MAX_NUM_VAL_PER_SITE;
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric 
lprofSetMaxValsPerSite(uint32_t MaxVals)530b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void lprofSetMaxValsPerSite(uint32_t MaxVals) {
540b57cec5SDimitry Andric   VPMaxNumValsPerSite = MaxVals;
550b57cec5SDimitry Andric   hasNonDefaultValsPerSite = 1;
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric /* This method is only used in value profiler mock testing.  */
590b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void
__llvm_profile_set_num_value_sites(__llvm_profile_data * Data,uint32_t ValueKind,uint16_t NumValueSites)600b57cec5SDimitry Andric __llvm_profile_set_num_value_sites(__llvm_profile_data *Data,
610b57cec5SDimitry Andric                                    uint32_t ValueKind, uint16_t NumValueSites) {
6206c3fb27SDimitry Andric #ifdef __GNUC__
6306c3fb27SDimitry Andric #pragma GCC diagnostic push
6406c3fb27SDimitry Andric #pragma GCC diagnostic ignored "-Wcast-qual"
65*7a6dacacSDimitry Andric #elif defined(__clang__)
66*7a6dacacSDimitry Andric #pragma clang diagnostic push
67*7a6dacacSDimitry Andric #pragma clang diagnostic ignored "-Wcast-qual"
6806c3fb27SDimitry Andric #endif
690b57cec5SDimitry Andric   *((uint16_t *)&Data->NumValueSites[ValueKind]) = NumValueSites;
7006c3fb27SDimitry Andric #ifdef __GNUC__
7106c3fb27SDimitry Andric #pragma GCC diagnostic pop
72*7a6dacacSDimitry Andric #elif defined(__clang__)
73*7a6dacacSDimitry Andric #pragma clang diagnostic pop
7406c3fb27SDimitry Andric #endif
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric /* This method is only used in value profiler mock testing.  */
780b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const __llvm_profile_data *
__llvm_profile_iterate_data(const __llvm_profile_data * Data)790b57cec5SDimitry Andric __llvm_profile_iterate_data(const __llvm_profile_data *Data) {
800b57cec5SDimitry Andric   return Data + 1;
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric /* This method is only used in value profiler mock testing.  */
840b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void *
__llvm_get_function_addr(const __llvm_profile_data * Data)850b57cec5SDimitry Andric __llvm_get_function_addr(const __llvm_profile_data *Data) {
860b57cec5SDimitry Andric   return Data->FunctionPointer;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric /* Allocate an array that holds the pointers to the linked lists of
900b57cec5SDimitry Andric  * value profile counter nodes. The number of element of the array
910b57cec5SDimitry Andric  * is the total number of value profile sites instrumented. Returns
920b57cec5SDimitry Andric  * 0 if allocation fails.
930b57cec5SDimitry Andric  */
940b57cec5SDimitry Andric 
allocateValueProfileCounters(__llvm_profile_data * Data)950b57cec5SDimitry Andric static int allocateValueProfileCounters(__llvm_profile_data *Data) {
960b57cec5SDimitry Andric   uint64_t NumVSites = 0;
970b57cec5SDimitry Andric   uint32_t VKI;
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   /* This function will never be called when value site array is allocated
1000b57cec5SDimitry Andric      statically at compile time.  */
1010b57cec5SDimitry Andric   hasStaticCounters = 0;
1020b57cec5SDimitry Andric   /* When dynamic allocation is enabled, allow tracking the max number of
1030b57cec5SDimitry Andric    * values allowd.  */
1040b57cec5SDimitry Andric   if (!hasNonDefaultValsPerSite)
1050b57cec5SDimitry Andric     VPMaxNumValsPerSite = INSTR_PROF_MAX_NUM_VAL_PER_SITE;
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
1080b57cec5SDimitry Andric     NumVSites += Data->NumValueSites[VKI];
1090b57cec5SDimitry Andric 
110e8d8bef9SDimitry Andric   // If NumVSites = 0, calloc is allowed to return a non-null pointer.
111e8d8bef9SDimitry Andric   assert(NumVSites > 0 && "NumVSites can't be zero");
1120b57cec5SDimitry Andric   ValueProfNode **Mem =
1130b57cec5SDimitry Andric       (ValueProfNode **)calloc(NumVSites, sizeof(ValueProfNode *));
1140b57cec5SDimitry Andric   if (!Mem)
1150b57cec5SDimitry Andric     return 0;
1160b57cec5SDimitry Andric   if (!COMPILER_RT_BOOL_CMPXCHG(&Data->Values, 0, Mem)) {
1170b57cec5SDimitry Andric     free(Mem);
1180b57cec5SDimitry Andric     return 0;
1190b57cec5SDimitry Andric   }
1200b57cec5SDimitry Andric   return 1;
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
allocateOneNode(void)1230b57cec5SDimitry Andric static ValueProfNode *allocateOneNode(void) {
1240b57cec5SDimitry Andric   ValueProfNode *Node;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   if (!hasStaticCounters)
1270b57cec5SDimitry Andric     return (ValueProfNode *)calloc(1, sizeof(ValueProfNode));
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   /* Early check to avoid value wrapping around.  */
1300b57cec5SDimitry Andric   if (CurrentVNode + 1 > EndVNode) {
1310b57cec5SDimitry Andric     if (OutOfNodesWarnings++ < INSTR_PROF_MAX_VP_WARNS) {
1320b57cec5SDimitry Andric       PROF_WARN("Unable to track new values: %s. "
1330b57cec5SDimitry Andric                 " Consider using option -mllvm -vp-counters-per-site=<n> to "
1340b57cec5SDimitry Andric                 "allocate more"
1350b57cec5SDimitry Andric                 " value profile counters at compile time. \n",
1360b57cec5SDimitry Andric                 "Running out of static counters");
1370b57cec5SDimitry Andric     }
1380b57cec5SDimitry Andric     return 0;
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric   Node = COMPILER_RT_PTR_FETCH_ADD(ValueProfNode, CurrentVNode, 1);
1410b57cec5SDimitry Andric   /* Due to section padding, EndVNode point to a byte which is one pass
1420b57cec5SDimitry Andric    * an incomplete VNode, so we need to skip the last incomplete node. */
1430b57cec5SDimitry Andric   if (Node + 1 > EndVNode)
1440b57cec5SDimitry Andric     return 0;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   return Node;
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric static COMPILER_RT_ALWAYS_INLINE void
instrumentTargetValueImpl(uint64_t TargetValue,void * Data,uint32_t CounterIndex,uint64_t CountValue)1500b57cec5SDimitry Andric instrumentTargetValueImpl(uint64_t TargetValue, void *Data,
1510b57cec5SDimitry Andric                           uint32_t CounterIndex, uint64_t CountValue) {
1520b57cec5SDimitry Andric   __llvm_profile_data *PData = (__llvm_profile_data *)Data;
1530b57cec5SDimitry Andric   if (!PData)
1540b57cec5SDimitry Andric     return;
1550b57cec5SDimitry Andric   if (!CountValue)
1560b57cec5SDimitry Andric     return;
1570b57cec5SDimitry Andric   if (!PData->Values) {
1580b57cec5SDimitry Andric     if (!allocateValueProfileCounters(PData))
1590b57cec5SDimitry Andric       return;
1600b57cec5SDimitry Andric   }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   ValueProfNode **ValueCounters = (ValueProfNode **)PData->Values;
1630b57cec5SDimitry Andric   ValueProfNode *PrevVNode = NULL;
1640b57cec5SDimitry Andric   ValueProfNode *MinCountVNode = NULL;
1650b57cec5SDimitry Andric   ValueProfNode *CurVNode = ValueCounters[CounterIndex];
1660b57cec5SDimitry Andric   uint64_t MinCount = UINT64_MAX;
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   uint8_t VDataCount = 0;
1690b57cec5SDimitry Andric   while (CurVNode) {
1700b57cec5SDimitry Andric     if (TargetValue == CurVNode->Value) {
1710b57cec5SDimitry Andric       CurVNode->Count += CountValue;
1720b57cec5SDimitry Andric       return;
1730b57cec5SDimitry Andric     }
1740b57cec5SDimitry Andric     if (CurVNode->Count < MinCount) {
1750b57cec5SDimitry Andric       MinCount = CurVNode->Count;
1760b57cec5SDimitry Andric       MinCountVNode = CurVNode;
1770b57cec5SDimitry Andric     }
1780b57cec5SDimitry Andric     PrevVNode = CurVNode;
1790b57cec5SDimitry Andric     CurVNode = CurVNode->Next;
1800b57cec5SDimitry Andric     ++VDataCount;
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   if (VDataCount >= VPMaxNumValsPerSite) {
1840b57cec5SDimitry Andric     /* Bump down the min count node's count. If it reaches 0,
1850b57cec5SDimitry Andric      * evict it. This eviction/replacement policy makes hot
1860b57cec5SDimitry Andric      * targets more sticky while cold targets less so. In other
1870b57cec5SDimitry Andric      * words, it makes it less likely for the hot targets to be
1880b57cec5SDimitry Andric      * prematurally evicted during warmup/establishment period,
1890b57cec5SDimitry Andric      * when their counts are still low. In a special case when
1900b57cec5SDimitry Andric      * the number of values tracked is reduced to only one, this
1910b57cec5SDimitry Andric      * policy will guarantee that the dominating target with >50%
1920b57cec5SDimitry Andric      * total count will survive in the end. Note that this scheme
1930b57cec5SDimitry Andric      * allows the runtime to track the min count node in an adaptive
1940b57cec5SDimitry Andric      * manner. It can correct previous mistakes and eventually
1950b57cec5SDimitry Andric      * lock on a cold target that is alread in stable state.
1960b57cec5SDimitry Andric      *
1970b57cec5SDimitry Andric      * In very rare cases,  this replacement scheme may still lead
1980b57cec5SDimitry Andric      * to target loss. For instance, out of \c N value slots, \c N-1
1990b57cec5SDimitry Andric      * slots are occupied by luke warm targets during the warmup
2000b57cec5SDimitry Andric      * period and the remaining one slot is competed by two or more
2010b57cec5SDimitry Andric      * very hot targets. If those hot targets occur in an interleaved
2020b57cec5SDimitry Andric      * way, none of them will survive (gain enough weight to throw out
2030b57cec5SDimitry Andric      * other established entries) due to the ping-pong effect.
2040b57cec5SDimitry Andric      * To handle this situation, user can choose to increase the max
2050b57cec5SDimitry Andric      * number of tracked values per value site. Alternatively, a more
2060b57cec5SDimitry Andric      * expensive eviction mechanism can be implemented. It requires
2070b57cec5SDimitry Andric      * the runtime to track the total number of evictions per-site.
2080b57cec5SDimitry Andric      * When the total number of evictions reaches certain threshold,
2090b57cec5SDimitry Andric      * the runtime can wipe out more than one lowest count entries
2100b57cec5SDimitry Andric      * to give space for hot targets.
2110b57cec5SDimitry Andric      */
2120b57cec5SDimitry Andric     if (MinCountVNode->Count <= CountValue) {
2130b57cec5SDimitry Andric       CurVNode = MinCountVNode;
2140b57cec5SDimitry Andric       CurVNode->Value = TargetValue;
2150b57cec5SDimitry Andric       CurVNode->Count = CountValue;
2160b57cec5SDimitry Andric     } else
2170b57cec5SDimitry Andric       MinCountVNode->Count -= CountValue;
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric     return;
2200b57cec5SDimitry Andric   }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   CurVNode = allocateOneNode();
2230b57cec5SDimitry Andric   if (!CurVNode)
2240b57cec5SDimitry Andric     return;
2250b57cec5SDimitry Andric   CurVNode->Value = TargetValue;
2260b57cec5SDimitry Andric   CurVNode->Count += CountValue;
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   uint32_t Success = 0;
2290b57cec5SDimitry Andric   if (!ValueCounters[CounterIndex])
2300b57cec5SDimitry Andric     Success =
2310b57cec5SDimitry Andric         COMPILER_RT_BOOL_CMPXCHG(&ValueCounters[CounterIndex], 0, CurVNode);
2320b57cec5SDimitry Andric   else if (PrevVNode && !PrevVNode->Next)
2330b57cec5SDimitry Andric     Success = COMPILER_RT_BOOL_CMPXCHG(&(PrevVNode->Next), 0, CurVNode);
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   if (!Success && !hasStaticCounters) {
2360b57cec5SDimitry Andric     free(CurVNode);
2370b57cec5SDimitry Andric     return;
2380b57cec5SDimitry Andric   }
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void
__llvm_profile_instrument_target(uint64_t TargetValue,void * Data,uint32_t CounterIndex)2420b57cec5SDimitry Andric __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
2430b57cec5SDimitry Andric                                  uint32_t CounterIndex) {
2440b57cec5SDimitry Andric   instrumentTargetValueImpl(TargetValue, Data, CounterIndex, 1);
2450b57cec5SDimitry Andric }
2460b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void
__llvm_profile_instrument_target_value(uint64_t TargetValue,void * Data,uint32_t CounterIndex,uint64_t CountValue)2470b57cec5SDimitry Andric __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data,
2480b57cec5SDimitry Andric                                        uint32_t CounterIndex,
2490b57cec5SDimitry Andric                                        uint64_t CountValue) {
2500b57cec5SDimitry Andric   instrumentTargetValueImpl(TargetValue, Data, CounterIndex, CountValue);
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric /*
254e8d8bef9SDimitry Andric  * The target values are partitioned into multiple ranges. The range spec is
255e8d8bef9SDimitry Andric  * defined in InstrProfData.inc.
2560b57cec5SDimitry Andric  */
257e8d8bef9SDimitry Andric COMPILER_RT_VISIBILITY void
__llvm_profile_instrument_memop(uint64_t TargetValue,void * Data,uint32_t CounterIndex)258e8d8bef9SDimitry Andric __llvm_profile_instrument_memop(uint64_t TargetValue, void *Data,
259e8d8bef9SDimitry Andric                                 uint32_t CounterIndex) {
260e8d8bef9SDimitry Andric   // Map the target value to the representative value of its range.
261e8d8bef9SDimitry Andric   uint64_t RepValue = InstrProfGetRangeRepValue(TargetValue);
262e8d8bef9SDimitry Andric   __llvm_profile_instrument_target(RepValue, Data, CounterIndex);
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric /*
2660b57cec5SDimitry Andric  * A wrapper struct that represents value profile runtime data.
2670b57cec5SDimitry Andric  * Like InstrProfRecord class which is used by profiling host tools,
268349cc55cSDimitry Andric  * ValueProfRuntimeRecord also implements the abstract interfaces defined in
2690b57cec5SDimitry Andric  * ValueProfRecordClosure so that the runtime data can be serialized using
2700b57cec5SDimitry Andric  * shared C implementation.
2710b57cec5SDimitry Andric  */
2720b57cec5SDimitry Andric typedef struct ValueProfRuntimeRecord {
2730b57cec5SDimitry Andric   const __llvm_profile_data *Data;
2740b57cec5SDimitry Andric   ValueProfNode **NodesKind[IPVK_Last + 1];
2750b57cec5SDimitry Andric   uint8_t **SiteCountArray;
2760b57cec5SDimitry Andric } ValueProfRuntimeRecord;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric /* ValueProfRecordClosure Interface implementation. */
2790b57cec5SDimitry Andric 
getNumValueSitesRT(const void * R,uint32_t VK)2800b57cec5SDimitry Andric static uint32_t getNumValueSitesRT(const void *R, uint32_t VK) {
2810b57cec5SDimitry Andric   return ((const ValueProfRuntimeRecord *)R)->Data->NumValueSites[VK];
2820b57cec5SDimitry Andric }
2830b57cec5SDimitry Andric 
getNumValueDataRT(const void * R,uint32_t VK)2840b57cec5SDimitry Andric static uint32_t getNumValueDataRT(const void *R, uint32_t VK) {
2850b57cec5SDimitry Andric   uint32_t S = 0, I;
2860b57cec5SDimitry Andric   const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R;
2870b57cec5SDimitry Andric   if (Record->SiteCountArray[VK] == INSTR_PROF_NULLPTR)
2880b57cec5SDimitry Andric     return 0;
2890b57cec5SDimitry Andric   for (I = 0; I < Record->Data->NumValueSites[VK]; I++)
2900b57cec5SDimitry Andric     S += Record->SiteCountArray[VK][I];
2910b57cec5SDimitry Andric   return S;
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric 
getNumValueDataForSiteRT(const void * R,uint32_t VK,uint32_t S)2940b57cec5SDimitry Andric static uint32_t getNumValueDataForSiteRT(const void *R, uint32_t VK,
2950b57cec5SDimitry Andric                                          uint32_t S) {
2960b57cec5SDimitry Andric   const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R;
2970b57cec5SDimitry Andric   return Record->SiteCountArray[VK][S];
2980b57cec5SDimitry Andric }
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric static ValueProfRuntimeRecord RTRecord;
3010b57cec5SDimitry Andric static ValueProfRecordClosure RTRecordClosure = {
3020b57cec5SDimitry Andric     &RTRecord,          INSTR_PROF_NULLPTR, /* GetNumValueKinds */
3030b57cec5SDimitry Andric     getNumValueSitesRT, getNumValueDataRT,  getNumValueDataForSiteRT,
3040b57cec5SDimitry Andric     INSTR_PROF_NULLPTR, /* RemapValueData */
3050b57cec5SDimitry Andric     INSTR_PROF_NULLPTR, /* GetValueForSite, */
3060b57cec5SDimitry Andric     INSTR_PROF_NULLPTR  /* AllocValueProfData */
3070b57cec5SDimitry Andric };
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric static uint32_t
initializeValueProfRuntimeRecord(const __llvm_profile_data * Data,uint8_t * SiteCountArray[])3100b57cec5SDimitry Andric initializeValueProfRuntimeRecord(const __llvm_profile_data *Data,
3110b57cec5SDimitry Andric                                  uint8_t *SiteCountArray[]) {
3120b57cec5SDimitry Andric   unsigned I, J, S = 0, NumValueKinds = 0;
3130b57cec5SDimitry Andric   ValueProfNode **Nodes = (ValueProfNode **)Data->Values;
3140b57cec5SDimitry Andric   RTRecord.Data = Data;
3150b57cec5SDimitry Andric   RTRecord.SiteCountArray = SiteCountArray;
3160b57cec5SDimitry Andric   for (I = 0; I <= IPVK_Last; I++) {
3170b57cec5SDimitry Andric     uint16_t N = Data->NumValueSites[I];
3180b57cec5SDimitry Andric     if (!N)
3190b57cec5SDimitry Andric       continue;
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric     NumValueKinds++;
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric     RTRecord.NodesKind[I] = Nodes ? &Nodes[S] : INSTR_PROF_NULLPTR;
3240b57cec5SDimitry Andric     for (J = 0; J < N; J++) {
3250b57cec5SDimitry Andric       /* Compute value count for each site. */
3260b57cec5SDimitry Andric       uint32_t C = 0;
3270b57cec5SDimitry Andric       ValueProfNode *Site =
3280b57cec5SDimitry Andric           Nodes ? RTRecord.NodesKind[I][J] : INSTR_PROF_NULLPTR;
3290b57cec5SDimitry Andric       while (Site) {
3300b57cec5SDimitry Andric         C++;
3310b57cec5SDimitry Andric         Site = Site->Next;
3320b57cec5SDimitry Andric       }
3330b57cec5SDimitry Andric       if (C > UCHAR_MAX)
3340b57cec5SDimitry Andric         C = UCHAR_MAX;
3350b57cec5SDimitry Andric       RTRecord.SiteCountArray[I][J] = C;
3360b57cec5SDimitry Andric     }
3370b57cec5SDimitry Andric     S += N;
3380b57cec5SDimitry Andric   }
3390b57cec5SDimitry Andric   return NumValueKinds;
3400b57cec5SDimitry Andric }
3410b57cec5SDimitry Andric 
getNextNValueData(uint32_t VK,uint32_t Site,InstrProfValueData * Dst,ValueProfNode * StartNode,uint32_t N)3420b57cec5SDimitry Andric static ValueProfNode *getNextNValueData(uint32_t VK, uint32_t Site,
3430b57cec5SDimitry Andric                                         InstrProfValueData *Dst,
3440b57cec5SDimitry Andric                                         ValueProfNode *StartNode, uint32_t N) {
3450b57cec5SDimitry Andric   unsigned I;
3460b57cec5SDimitry Andric   ValueProfNode *VNode = StartNode ? StartNode : RTRecord.NodesKind[VK][Site];
3470b57cec5SDimitry Andric   for (I = 0; I < N; I++) {
3480b57cec5SDimitry Andric     Dst[I].Value = VNode->Value;
3490b57cec5SDimitry Andric     Dst[I].Count = VNode->Count;
3500b57cec5SDimitry Andric     VNode = VNode->Next;
3510b57cec5SDimitry Andric   }
3520b57cec5SDimitry Andric   return VNode;
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric 
getValueProfDataSizeWrapper(void)3550b57cec5SDimitry Andric static uint32_t getValueProfDataSizeWrapper(void) {
3560b57cec5SDimitry Andric   return getValueProfDataSize(&RTRecordClosure);
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric 
getNumValueDataForSiteWrapper(uint32_t VK,uint32_t S)3590b57cec5SDimitry Andric static uint32_t getNumValueDataForSiteWrapper(uint32_t VK, uint32_t S) {
3600b57cec5SDimitry Andric   return getNumValueDataForSiteRT(&RTRecord, VK, S);
3610b57cec5SDimitry Andric }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric static VPDataReaderType TheVPDataReader = {
3640b57cec5SDimitry Andric     initializeValueProfRuntimeRecord, getValueProfRecordHeaderSize,
3650b57cec5SDimitry Andric     getFirstValueProfRecord,          getNumValueDataForSiteWrapper,
3660b57cec5SDimitry Andric     getValueProfDataSizeWrapper,      getNextNValueData};
3670b57cec5SDimitry Andric 
lprofGetVPDataReader(void)36881ad6265SDimitry Andric COMPILER_RT_VISIBILITY VPDataReaderType *lprofGetVPDataReader(void) {
3690b57cec5SDimitry Andric   return &TheVPDataReader;
3700b57cec5SDimitry Andric }
371