Index: lib/profile/InstrProfiling.c =================================================================== --- lib/profile/InstrProfiling.c +++ lib/profile/InstrProfiling.c @@ -11,6 +11,9 @@ #include #include #include +#define INSTR_PROF_VALUE_PROF_DATA +#define INSTR_PROF_COMMON_API_IMPL +#include "InstrProfData.inc" LLVM_LIBRARY_VISIBILITY uint64_t __llvm_profile_get_magic(void) { return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64) @@ -60,20 +63,26 @@ } } -/* Total number of value profile data in bytes. */ -static uint64_t TotalValueDataSize = 0; - -#ifdef _MIPS_ARCH +/* This method is only used in value profiler mock testing. */ LLVM_LIBRARY_VISIBILITY void -__llvm_profile_instrument_target(uint64_t TargetValue, void *Data_, - uint32_t CounterIndex) {} - -#else +__llvm_profile_set_num_value_sites(__llvm_profile_data *Data, + uint32_t ValueKind, uint16_t NumValueSites) { + *((uint16_t *)&Data->NumValueSites[ValueKind]) = NumValueSites; +} +/* This method is only used in value profiler mock testing. */ +LLVM_LIBRARY_VISIBILITY const __llvm_profile_data * +__llvm_profile_iterate_data(const __llvm_profile_data *Data) { + return Data + 1; +} +LLVM_LIBRARY_VISIBILITY void * +__llvm_get_function_addr(const __llvm_profile_data *Data) { + return Data->FunctionPointer; +} /* Allocate an array that holds the pointers to the linked lists of * value profile counter nodes. The number of element of the array * is the total number of value profile sites instrumented. Returns - * 0 if allocation fails. + * 0 if allocation fails. */ static int allocateValueProfileCounters(__llvm_profile_data *Data) { @@ -90,16 +99,27 @@ free(Mem); return 0; } - /* In the raw format, there will be an value count array preceding - * the value profile data. The element type of the array is uint8_t, - * and there is one element in array per value site. The element - * stores the number of values profiled for the corresponding site. - */ - uint8_t Padding = __llvm_profile_get_num_padding_bytes(NumVSites); - __sync_fetch_and_add(&TotalValueDataSize, NumVSites + Padding); return 1; } +static void deallocateValueProfileCounters(__llvm_profile_data *Data) { + uint64_t NumVSites = 0, I; + uint32_t VKI; + if (!Data->Values) + return; + for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI) + NumVSites += Data->NumValueSites[VKI]; + for (I = 0; I < NumVSites; I++) { + ValueProfNode *Node = ((ValueProfNode **)Data->Values)[I]; + while (Node) { + ValueProfNode *Next = Node->Next; + free(Node); + Node = Next; + } + } + free(Data->Values); +} + LLVM_LIBRARY_VISIBILITY void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data, uint32_t CounterIndex) { @@ -149,61 +169,69 @@ free(CurrentVNode); return; } - __sync_fetch_and_add(&TotalValueDataSize, Success * sizeof(ValueProfNode)); } -#endif LLVM_LIBRARY_VISIBILITY uint64_t __llvm_profile_gather_value_data(uint8_t **VDataArray) { - - if (!VDataArray || 0 == TotalValueDataSize) - return 0; - - uint64_t NumData = TotalValueDataSize; - *VDataArray = (uint8_t *)calloc(NumData, sizeof(uint8_t)); - if (!*VDataArray) + size_t S = 0, TotalSize = 0; + __llvm_profile_data *I; + ValueProfRuntimeRecord *Records = NULL; + if (!VDataArray) return 0; - uint8_t *VDataEnd = *VDataArray + NumData; - uint8_t *PerSiteCountsHead = *VDataArray; const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); - __llvm_profile_data *I; - for (I = (__llvm_profile_data *)DataBegin; I != DataEnd; ++I) { - - uint64_t NumVSites = 0; - uint32_t VKI, i; + size_t ND = DataEnd - DataBegin; - if (!I->Values) - continue; + Records = + (ValueProfRuntimeRecord *)calloc(sizeof(ValueProfRuntimeRecord), ND); - ValueProfNode **ValueCounters = (ValueProfNode **)I->Values; + if (!Records) + return 0; + /* + * Compute the total Size of the buffer to hold ValueProfData + * structures for functions with value profile data. + */ + for (I = (__llvm_profile_data *)DataBegin; I != DataEnd; ++I) { + ValueProfRuntimeRecord *R = &Records[I - DataBegin]; + /* Extract the value profile data info from the runtime. */ + if (initializeValueProfRuntimeRecord(R, I->NumValueSites, I->Values)) + goto Return; + /* Compute the size of ValueProfData from this runtime record. */ + if (getNumValueKindsRT(R) != 0) + S += getValueProfDataSizeRT(R); + } + /* No value sites or no value profile data is collected. */ + if (!S) + goto Return; - for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI) - NumVSites += I->NumValueSites[VKI]; - uint8_t Padding = __llvm_profile_get_num_padding_bytes(NumVSites); + *VDataArray = calloc(S, sizeof(uint8_t)); + if (!*VDataArray) + goto Return; - uint8_t *PerSiteCountPtr = PerSiteCountsHead; - InstrProfValueData *VDataPtr = - (InstrProfValueData *)(PerSiteCountPtr + NumVSites + Padding); - - for (i = 0; i < NumVSites; ++i) { - - ValueProfNode *VNode = ValueCounters[i]; - - uint8_t VDataCount = 0; - while (VNode && ((uint8_t *)(VDataPtr + 1) <= VDataEnd)) { - *VDataPtr = VNode->VData; - VNode = VNode->Next; - ++VDataPtr; - if (++VDataCount == UCHAR_MAX) - break; - } - *PerSiteCountPtr = VDataCount; - ++PerSiteCountPtr; - } - I->Values = (void *)PerSiteCountsHead; - PerSiteCountsHead = (uint8_t *)VDataPtr; - } - return PerSiteCountsHead - *VDataArray; + /* Now commit the final total size to be returned. */ + TotalSize = S; + ValueProfData *VD = (ValueProfData *)(*VDataArray); + /* + * Extract value profile data and write into ValueProfData structure + * one by one. Note that new value profile data added to any value + * site (from another thread) after the ValueProfRuntimeRecord is + * initialized (when the profile data snapshot is taken) won't be + * collected. This is not a problem as those dropped value will have + * very low taken count. + */ + for (I = (__llvm_profile_data *)DataBegin; I != DataEnd; ++I) { + ValueProfRuntimeRecord *R = &Records[I - DataBegin]; + if (getNumValueKindsRT(R) == 0) + continue; + serializeValueProfDataFromRT(R, VD); + deallocateValueProfileCounters(I); + I->Values = VD; + finalizeValueProfRuntimeRecord(R); + VD = (ValueProfData *)((char *)VD + VD->TotalSize); + } +Return: + if (Records) + free(Records); + return TotalSize; } Index: test/profile/instrprof-value-prof.c =================================================================== --- test/profile/instrprof-value-prof.c +++ test/profile/instrprof-value-prof.c @@ -0,0 +1,178 @@ +// RUN: %clang_profgen -O2 -o %t %s +// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t 1 +// RUN: env LLVM_PROFILE_FILE=%t-2.profraw %run %t +// RUN: llvm-profdata merge -o %t.profdata %t.profraw +// RUN: llvm-profdata merge -o %t-2.profdata %t-2.profraw +// RUN: llvm-profdata merge -o %t-merged.profdata %t.profraw %t-2.profdata +// RUN: llvm-profdata show --all-functions -ic-targets %t-2.profdata | FileCheck %s -check-prefix=NO-VALUE +// RUN: llvm-profdata show --all-functions -ic-targets %t.profdata | FileCheck %s +// RUN: llvm-profdata show --all-functions -ic-targets %t-merged.profdata | FileCheck %s + +#include +#include +#include +typedef struct __llvm_profile_data __llvm_profile_data; +const __llvm_profile_data *__llvm_profile_begin_data(void); +const __llvm_profile_data *__llvm_profile_end_data(void); +void __llvm_profile_set_num_value_sites(__llvm_profile_data *Data, + uint32_t ValueKind, + uint16_t NumValueSites); +__llvm_profile_data * +__llvm_profile_iterate_data(const __llvm_profile_data *Data); +void *__llvm_get_function_addr(const __llvm_profile_data *Data); +void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data, + uint32_t CounterIndex); + +#define DEF_FUNC(x) \ + void x() {} +#define DEF_2_FUNCS(x) DEF_FUNC(x##_1) DEF_FUNC(x##_2) +#define DEF_4_FUNCS(x) DEF_2_FUNCS(x##_1) DEF_2_FUNCS(x##_2) +#define DEF_8_FUNCS(x) DEF_4_FUNCS(x##_1) DEF_4_FUNCS(x##_2) +#define DEF_16_FUNCS(x) DEF_8_FUNCS(x##_1) DEF_8_FUNCS(x##_2) +#define DEF_32_FUNCS(x) DEF_16_FUNCS(x##_1) DEF_16_FUNCS(x##_2) +#define DEF_64_FUNCS(x) DEF_32_FUNCS(x##_1) DEF_32_FUNCS(x##_2) +#define DEF_128_FUNCS(x) DEF_64_FUNCS(x##_1) DEF_64_FUNCS(x##_2) + +#define FUNC_ADDR(x) &x, +#define FUNC_2_ADDRS(x) FUNC_ADDR(x##_1) FUNC_ADDR(x##_2) +#define FUNC_4_ADDRS(x) FUNC_2_ADDRS(x##_1) FUNC_2_ADDRS(x##_2) +#define FUNC_8_ADDRS(x) FUNC_4_ADDRS(x##_1) FUNC_4_ADDRS(x##_2) +#define FUNC_16_ADDRS(x) FUNC_8_ADDRS(x##_1) FUNC_8_ADDRS(x##_2) +#define FUNC_32_ADDRS(x) FUNC_16_ADDRS(x##_1) FUNC_16_ADDRS(x##_2) +#define FUNC_64_ADDRS(x) FUNC_32_ADDRS(x##_1) FUNC_32_ADDRS(x##_2) +#define FUNC_128_ADDRS(x) FUNC_64_ADDRS(x##_1) FUNC_64_ADDRS(x##_2) + +DEF_8_FUNCS(callee) +DEF_128_FUNCS(caller) + +void *CallerAddrs[] = {FUNC_128_ADDRS(caller)}; + +void *CalleeAddrs[] = {FUNC_8_ADDRS(callee)}; + +static int cmpaddr(const void *p1, const void *p2) { + void *addr1 = *(void **)p1; + void *addr2 = *(void **)p2; + return (intptr_t)addr2 - (intptr_t)addr1; +} + +int main(int argc, const char *argv[]) { + unsigned S, NS = 0, V, doInstrument = 1; + const __llvm_profile_data *Data, *DataEnd; + + if (argc < 2) + doInstrument = 0; + + qsort(CallerAddrs, sizeof(CallerAddrs) / sizeof(void *), sizeof(void *), + cmpaddr); + + /* We will synthesis value profile data for 128 callers functions. + * The number of * value sites. The number values for each value site + * ranges from 0 to 8. */ + + Data = __llvm_profile_begin_data(); + DataEnd = __llvm_profile_end_data(); + + for (; Data < DataEnd; Data = __llvm_profile_iterate_data(Data)) { + void *func = __llvm_get_function_addr(Data); + if (bsearch(&func, CallerAddrs, sizeof(CallerAddrs) / sizeof(void *), + sizeof(void *), cmpaddr)) { + __llvm_profile_set_num_value_sites((__llvm_profile_data *)Data, + 0 /*IPVK_IndirectCallTarget */, NS); + if (!doInstrument) { + NS++; + continue; + } + for (S = 0; S < NS; S++) { + for (V = 0; V < S % 8; V++) { + unsigned C; + for (C = 0; C < V + 1; C++) + __llvm_profile_instrument_target((uint64_t)CalleeAddrs[V], + (void *)Data, S); + } + } + NS++; + } + } +} + +// NO-VALUE: Indirect Call Site Count: 127 +// NO-VALUE-NEXT: Indirect Target Results: +// CHECK: Indirect Call Site Count: 127 +// CHECK-NEXT: Indirect Target Results: +// CHECK-NEXT: [ 1, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 2, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 2, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 3, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 3, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 3, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 4, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 4, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 4, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 4, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 5, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 5, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 5, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 5, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 5, callee_2_1_1, 5 ] +// CHECK-NEXT: [ 6, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 6, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 6, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 6, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 6, callee_2_1_1, 5 ] +// CHECK-NEXT: [ 6, callee_2_1_2, 6 ] +// CHECK-NEXT: [ 7, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 7, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 7, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 7, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 7, callee_2_1_1, 5 ] +// CHECK-NEXT: [ 7, callee_2_1_2, 6 ] +// CHECK-NEXT: [ 7, callee_2_2_1, 7 ] +// CHECK-NEXT: [ 9, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 10, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 10, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 11, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 11, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 11, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 12, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 12, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 12, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 12, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 13, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 13, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 13, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 13, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 13, callee_2_1_1, 5 ] +// CHECK-NEXT: [ 14, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 14, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 14, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 14, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 14, callee_2_1_1, 5 ] +// CHECK-NEXT: [ 14, callee_2_1_2, 6 ] +// CHECK-NEXT: [ 15, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 15, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 15, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 15, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 15, callee_2_1_1, 5 ] +// CHECK-NEXT: [ 15, callee_2_1_2, 6 ] +// CHECK-NEXT: [ 15, callee_2_2_1, 7 ] +// CHECK-NEXT: [ 17, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 18, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 18, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 19, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 19, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 19, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 20, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 20, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 20, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 20, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 21, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 21, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 21, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 21, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 21, callee_2_1_1, 5 ] +// CHECK-NEXT: [ 22, callee_1_1_1, 1 ] +// CHECK-NEXT: [ 22, callee_1_1_2, 2 ] +// CHECK-NEXT: [ 22, callee_1_2_1, 3 ] +// CHECK-NEXT: [ 22, callee_1_2_2, 4 ] +// CHECK-NEXT: [ 22, callee_2_1_1, 5 ] +