13cab2bb3Spatrick /*===- InstrProfilingMerge.c - Profile in-process Merging  ---------------===*\
23cab2bb3Spatrick |*
33cab2bb3Spatrick |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick |* See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick |*
73cab2bb3Spatrick |*===----------------------------------------------------------------------===*
83cab2bb3Spatrick |* This file defines the API needed for in-process merging of profile data
93cab2bb3Spatrick |* stored in memory buffer.
103cab2bb3Spatrick \*===---------------------------------------------------------------------===*/
113cab2bb3Spatrick 
123cab2bb3Spatrick #include "InstrProfiling.h"
133cab2bb3Spatrick #include "InstrProfilingInternal.h"
143cab2bb3Spatrick #include "InstrProfilingUtil.h"
153cab2bb3Spatrick 
163cab2bb3Spatrick #define INSTR_PROF_VALUE_PROF_DATA
173cab2bb3Spatrick #include "profile/InstrProfData.inc"
183cab2bb3Spatrick 
193cab2bb3Spatrick COMPILER_RT_VISIBILITY
203cab2bb3Spatrick void (*VPMergeHook)(ValueProfData *, __llvm_profile_data *);
213cab2bb3Spatrick 
223cab2bb3Spatrick COMPILER_RT_VISIBILITY
lprofGetLoadModuleSignature(void)23*810390e3Srobert uint64_t lprofGetLoadModuleSignature(void) {
243cab2bb3Spatrick   /* A very fast way to compute a module signature.  */
25d89ec533Spatrick   uint64_t Version = __llvm_profile_get_version();
26*810390e3Srobert   uint64_t NumCounters = __llvm_profile_get_num_counters(
27*810390e3Srobert       __llvm_profile_begin_counters(), __llvm_profile_end_counters());
28*810390e3Srobert   uint64_t NumData = __llvm_profile_get_num_data(__llvm_profile_begin_data(),
293cab2bb3Spatrick                                                  __llvm_profile_end_data());
303cab2bb3Spatrick   uint64_t NamesSize =
313cab2bb3Spatrick       (uint64_t)(__llvm_profile_end_names() - __llvm_profile_begin_names());
323cab2bb3Spatrick   uint64_t NumVnodes =
333cab2bb3Spatrick       (uint64_t)(__llvm_profile_end_vnodes() - __llvm_profile_begin_vnodes());
343cab2bb3Spatrick   const __llvm_profile_data *FirstD = __llvm_profile_begin_data();
353cab2bb3Spatrick 
36*810390e3Srobert   return (NamesSize << 40) + (NumCounters << 30) + (NumData << 20) +
37*810390e3Srobert          (NumVnodes << 10) + (NumData > 0 ? FirstD->NameRef : 0) + Version +
38*810390e3Srobert          __llvm_profile_get_magic();
393cab2bb3Spatrick }
403cab2bb3Spatrick 
413cab2bb3Spatrick /* Returns 1 if profile is not structurally compatible.  */
423cab2bb3Spatrick COMPILER_RT_VISIBILITY
__llvm_profile_check_compatibility(const char * ProfileData,uint64_t ProfileSize)433cab2bb3Spatrick int __llvm_profile_check_compatibility(const char *ProfileData,
443cab2bb3Spatrick                                        uint64_t ProfileSize) {
453cab2bb3Spatrick   /* Check profile header only for now  */
463cab2bb3Spatrick   __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
473cab2bb3Spatrick   __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
483cab2bb3Spatrick   SrcDataStart =
49d89ec533Spatrick       (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
50d89ec533Spatrick                               Header->BinaryIdsSize);
513cab2bb3Spatrick   SrcDataEnd = SrcDataStart + Header->DataSize;
523cab2bb3Spatrick 
533cab2bb3Spatrick   if (ProfileSize < sizeof(__llvm_profile_header))
543cab2bb3Spatrick     return 1;
553cab2bb3Spatrick 
563cab2bb3Spatrick   /* Check the header first.  */
573cab2bb3Spatrick   if (Header->Magic != __llvm_profile_get_magic() ||
583cab2bb3Spatrick       Header->Version != __llvm_profile_get_version() ||
593cab2bb3Spatrick       Header->DataSize !=
60*810390e3Srobert           __llvm_profile_get_num_data(__llvm_profile_begin_data(),
613cab2bb3Spatrick                                       __llvm_profile_end_data()) ||
62*810390e3Srobert       Header->CountersSize !=
63*810390e3Srobert           __llvm_profile_get_num_counters(__llvm_profile_begin_counters(),
64*810390e3Srobert                                           __llvm_profile_end_counters()) ||
653cab2bb3Spatrick       Header->NamesSize != (uint64_t)(__llvm_profile_end_names() -
663cab2bb3Spatrick                                       __llvm_profile_begin_names()) ||
673cab2bb3Spatrick       Header->ValueKindLast != IPVK_Last)
683cab2bb3Spatrick     return 1;
693cab2bb3Spatrick 
70*810390e3Srobert   if (ProfileSize <
71*810390e3Srobert       sizeof(__llvm_profile_header) + Header->BinaryIdsSize +
72*810390e3Srobert           Header->DataSize * sizeof(__llvm_profile_data) + Header->NamesSize +
73*810390e3Srobert           Header->CountersSize * __llvm_profile_counter_entry_size())
743cab2bb3Spatrick     return 1;
753cab2bb3Spatrick 
763cab2bb3Spatrick   for (SrcData = SrcDataStart,
773cab2bb3Spatrick        DstData = (__llvm_profile_data *)__llvm_profile_begin_data();
783cab2bb3Spatrick        SrcData < SrcDataEnd; ++SrcData, ++DstData) {
793cab2bb3Spatrick     if (SrcData->NameRef != DstData->NameRef ||
803cab2bb3Spatrick         SrcData->FuncHash != DstData->FuncHash ||
813cab2bb3Spatrick         SrcData->NumCounters != DstData->NumCounters)
823cab2bb3Spatrick       return 1;
833cab2bb3Spatrick   }
843cab2bb3Spatrick 
853cab2bb3Spatrick   /* Matched! */
863cab2bb3Spatrick   return 0;
873cab2bb3Spatrick }
883cab2bb3Spatrick 
signextIfWin64(void * V)89*810390e3Srobert static uintptr_t signextIfWin64(void *V) {
90*810390e3Srobert #ifdef _WIN64
91*810390e3Srobert   return (uintptr_t)(int32_t)(uintptr_t)V;
92*810390e3Srobert #else
93*810390e3Srobert   return (uintptr_t)V;
94*810390e3Srobert #endif
95*810390e3Srobert }
96*810390e3Srobert 
973cab2bb3Spatrick COMPILER_RT_VISIBILITY
__llvm_profile_merge_from_buffer(const char * ProfileData,uint64_t ProfileSize)98d89ec533Spatrick int __llvm_profile_merge_from_buffer(const char *ProfileData,
993cab2bb3Spatrick                                      uint64_t ProfileSize) {
100*810390e3Srobert   if (__llvm_profile_get_version() & VARIANT_MASK_DBG_CORRELATE) {
101*810390e3Srobert     PROF_ERR(
102*810390e3Srobert         "%s\n",
103*810390e3Srobert         "Debug info correlation does not support profile merging at runtime. "
104*810390e3Srobert         "Instead, merge raw profiles using the llvm-profdata tool.");
105*810390e3Srobert     return 1;
106*810390e3Srobert   }
107*810390e3Srobert 
1083cab2bb3Spatrick   __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
1093cab2bb3Spatrick   __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
110*810390e3Srobert   char *SrcCountersStart;
1113cab2bb3Spatrick   const char *SrcNameStart;
112d89ec533Spatrick   const char *SrcValueProfDataStart, *SrcValueProfData;
113*810390e3Srobert   uintptr_t CountersDelta = Header->CountersDelta;
1143cab2bb3Spatrick 
1153cab2bb3Spatrick   SrcDataStart =
116d89ec533Spatrick       (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
117d89ec533Spatrick                               Header->BinaryIdsSize);
1183cab2bb3Spatrick   SrcDataEnd = SrcDataStart + Header->DataSize;
119*810390e3Srobert   SrcCountersStart = (char *)SrcDataEnd;
120*810390e3Srobert   SrcNameStart = SrcCountersStart +
121*810390e3Srobert                  Header->CountersSize * __llvm_profile_counter_entry_size();
1223cab2bb3Spatrick   SrcValueProfDataStart =
123d89ec533Spatrick       SrcNameStart + Header->NamesSize +
124d89ec533Spatrick       __llvm_profile_get_num_padding_bytes(Header->NamesSize);
125*810390e3Srobert   if (SrcNameStart < SrcCountersStart)
126d89ec533Spatrick     return 1;
1273cab2bb3Spatrick 
1283cab2bb3Spatrick   for (SrcData = SrcDataStart,
1293cab2bb3Spatrick       DstData = (__llvm_profile_data *)__llvm_profile_begin_data(),
1303cab2bb3Spatrick       SrcValueProfData = SrcValueProfDataStart;
1313cab2bb3Spatrick        SrcData < SrcDataEnd; ++SrcData, ++DstData) {
132*810390e3Srobert     // For the in-memory destination, CounterPtr is the distance from the start
133*810390e3Srobert     // address of the data to the start address of the counter. On WIN64,
134*810390e3Srobert     // CounterPtr is a truncated 32-bit value due to COFF limitation. Sign
135*810390e3Srobert     // extend CounterPtr to get the original value.
136*810390e3Srobert     char *DstCounters =
137*810390e3Srobert         (char *)((uintptr_t)DstData + signextIfWin64(DstData->CounterPtr));
138d89ec533Spatrick     unsigned NVK = 0;
1393cab2bb3Spatrick 
140*810390e3Srobert     // SrcData is a serialized representation of the memory image. We need to
141*810390e3Srobert     // compute the in-buffer counter offset from the in-memory address distance.
142*810390e3Srobert     // The initial CountersDelta is the in-memory address difference
143*810390e3Srobert     // start(__llvm_prf_cnts)-start(__llvm_prf_data), so SrcData->CounterPtr -
144*810390e3Srobert     // CountersDelta computes the offset into the in-buffer counter section.
145*810390e3Srobert     //
146*810390e3Srobert     // On WIN64, CountersDelta is truncated as well, so no need for signext.
147*810390e3Srobert     char *SrcCounters =
148*810390e3Srobert         SrcCountersStart + ((uintptr_t)SrcData->CounterPtr - CountersDelta);
149*810390e3Srobert     // CountersDelta needs to be decreased as we advance to the next data
150*810390e3Srobert     // record.
151*810390e3Srobert     CountersDelta -= sizeof(*SrcData);
152d89ec533Spatrick     unsigned NC = SrcData->NumCounters;
153d89ec533Spatrick     if (NC == 0)
154d89ec533Spatrick       return 1;
155*810390e3Srobert     if (SrcCounters < SrcCountersStart || SrcCounters >= SrcNameStart ||
156*810390e3Srobert         (SrcCounters + __llvm_profile_counter_entry_size() * NC) > SrcNameStart)
157d89ec533Spatrick       return 1;
158*810390e3Srobert     for (unsigned I = 0; I < NC; I++) {
159*810390e3Srobert       if (__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) {
160*810390e3Srobert         // A value of zero signifies the function is covered.
161*810390e3Srobert         DstCounters[I] &= SrcCounters[I];
162*810390e3Srobert       } else {
163*810390e3Srobert         ((uint64_t *)DstCounters)[I] += ((uint64_t *)SrcCounters)[I];
164*810390e3Srobert       }
165*810390e3Srobert     }
1663cab2bb3Spatrick 
1673cab2bb3Spatrick     /* Now merge value profile data. */
1683cab2bb3Spatrick     if (!VPMergeHook)
1693cab2bb3Spatrick       continue;
1703cab2bb3Spatrick 
171d89ec533Spatrick     for (unsigned I = 0; I <= IPVK_Last; I++)
1723cab2bb3Spatrick       NVK += (SrcData->NumValueSites[I] != 0);
1733cab2bb3Spatrick 
1743cab2bb3Spatrick     if (!NVK)
1753cab2bb3Spatrick       continue;
1763cab2bb3Spatrick 
177d89ec533Spatrick     if (SrcValueProfData >= ProfileData + ProfileSize)
178d89ec533Spatrick       return 1;
179d89ec533Spatrick     VPMergeHook((ValueProfData *)SrcValueProfData, DstData);
180d89ec533Spatrick     SrcValueProfData =
181d89ec533Spatrick         SrcValueProfData + ((ValueProfData *)SrcValueProfData)->TotalSize;
1823cab2bb3Spatrick   }
183d89ec533Spatrick 
184d89ec533Spatrick   return 0;
1853cab2bb3Spatrick }
186