xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingMerge.c (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
1 /*===- InstrProfilingMerge.c - Profile in-process Merging  ---------------===*\
2 |*
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 |*
7 |*===----------------------------------------------------------------------===*
8 |* This file defines the API needed for in-process merging of profile data
9 |* stored in memory buffer.
10 \*===---------------------------------------------------------------------===*/
11 
12 #include "InstrProfiling.h"
13 #include "InstrProfilingInternal.h"
14 #include "InstrProfilingUtil.h"
15 
16 #define INSTR_PROF_VALUE_PROF_DATA
17 #include "profile/InstrProfData.inc"
18 
19 COMPILER_RT_VISIBILITY
20 void (*VPMergeHook)(ValueProfData *, __llvm_profile_data *);
21 
22 COMPILER_RT_VISIBILITY
23 uint64_t lprofGetLoadModuleSignature() {
24   /* A very fast way to compute a module signature.  */
25   uint64_t Version = __llvm_profile_get_version();
26   uint64_t CounterSize = (uint64_t)(__llvm_profile_end_counters() -
27                                     __llvm_profile_begin_counters());
28   uint64_t DataSize = __llvm_profile_get_data_size(__llvm_profile_begin_data(),
29                                                    __llvm_profile_end_data());
30   uint64_t NamesSize =
31       (uint64_t)(__llvm_profile_end_names() - __llvm_profile_begin_names());
32   uint64_t NumVnodes =
33       (uint64_t)(__llvm_profile_end_vnodes() - __llvm_profile_begin_vnodes());
34   const __llvm_profile_data *FirstD = __llvm_profile_begin_data();
35 
36   return (NamesSize << 40) + (CounterSize << 30) + (DataSize << 20) +
37          (NumVnodes << 10) + (DataSize > 0 ? FirstD->NameRef : 0) + Version;
38 }
39 
40 /* Returns 1 if profile is not structurally compatible.  */
41 COMPILER_RT_VISIBILITY
42 int __llvm_profile_check_compatibility(const char *ProfileData,
43                                        uint64_t ProfileSize) {
44   /* Check profile header only for now  */
45   __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
46   __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
47   SrcDataStart =
48       (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
49                               Header->BinaryIdsSize);
50   SrcDataEnd = SrcDataStart + Header->DataSize;
51 
52   if (ProfileSize < sizeof(__llvm_profile_header))
53     return 1;
54 
55   /* Check the header first.  */
56   if (Header->Magic != __llvm_profile_get_magic() ||
57       Header->Version != __llvm_profile_get_version() ||
58       Header->DataSize !=
59           __llvm_profile_get_data_size(__llvm_profile_begin_data(),
60                                        __llvm_profile_end_data()) ||
61       Header->CountersSize != (uint64_t)(__llvm_profile_end_counters() -
62                                          __llvm_profile_begin_counters()) ||
63       Header->NamesSize != (uint64_t)(__llvm_profile_end_names() -
64                                       __llvm_profile_begin_names()) ||
65       Header->ValueKindLast != IPVK_Last)
66     return 1;
67 
68   if (ProfileSize < sizeof(__llvm_profile_header) + Header->BinaryIdsSize +
69                         Header->DataSize * sizeof(__llvm_profile_data) +
70                         Header->NamesSize + Header->CountersSize)
71     return 1;
72 
73   for (SrcData = SrcDataStart,
74        DstData = (__llvm_profile_data *)__llvm_profile_begin_data();
75        SrcData < SrcDataEnd; ++SrcData, ++DstData) {
76     if (SrcData->NameRef != DstData->NameRef ||
77         SrcData->FuncHash != DstData->FuncHash ||
78         SrcData->NumCounters != DstData->NumCounters)
79       return 1;
80   }
81 
82   /* Matched! */
83   return 0;
84 }
85 
86 static uintptr_t signextIfWin64(void *V) {
87 #ifdef _WIN64
88   return (uintptr_t)(int32_t)(uintptr_t)V;
89 #else
90   return (uintptr_t)V;
91 #endif
92 }
93 
94 COMPILER_RT_VISIBILITY
95 int __llvm_profile_merge_from_buffer(const char *ProfileData,
96                                      uint64_t ProfileSize) {
97   __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
98   __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
99   uint64_t *SrcCountersStart;
100   const char *SrcNameStart;
101   const char *SrcValueProfDataStart, *SrcValueProfData;
102   uintptr_t CountersDelta = Header->CountersDelta;
103 
104   SrcDataStart =
105       (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
106                               Header->BinaryIdsSize);
107   SrcDataEnd = SrcDataStart + Header->DataSize;
108   SrcCountersStart = (uint64_t *)SrcDataEnd;
109   SrcNameStart = (const char *)(SrcCountersStart + Header->CountersSize);
110   SrcValueProfDataStart =
111       SrcNameStart + Header->NamesSize +
112       __llvm_profile_get_num_padding_bytes(Header->NamesSize);
113   if (SrcNameStart < (const char *)SrcCountersStart)
114     return 1;
115 
116   for (SrcData = SrcDataStart,
117       DstData = (__llvm_profile_data *)__llvm_profile_begin_data(),
118       SrcValueProfData = SrcValueProfDataStart;
119        SrcData < SrcDataEnd; ++SrcData, ++DstData) {
120     // For the in-memory destination, CounterPtr is the distance from the start
121     // address of the data to the start address of the counter. On WIN64,
122     // CounterPtr is a truncated 32-bit value due to COFF limitation. Sign
123     // extend CounterPtr to get the original value.
124     uint64_t *DstCounters =
125         (uint64_t *)((uintptr_t)DstData + signextIfWin64(DstData->CounterPtr));
126     unsigned NVK = 0;
127 
128     // SrcData is a serialized representation of the memory image. We need to
129     // compute the in-buffer counter offset from the in-memory address distance.
130     // The initial CountersDelta is the in-memory address difference
131     // start(__llvm_prf_cnts)-start(__llvm_prf_data), so SrcData->CounterPtr -
132     // CountersDelta computes the offset into the in-buffer counter section.
133     //
134     // On WIN64, CountersDelta is truncated as well, so no need for signext.
135     uint64_t *SrcCounters =
136         SrcCountersStart +
137         ((uintptr_t)SrcData->CounterPtr - CountersDelta) / sizeof(uint64_t);
138     // CountersDelta needs to be decreased as we advance to the next data
139     // record.
140     CountersDelta -= sizeof(*SrcData);
141     unsigned NC = SrcData->NumCounters;
142     if (NC == 0)
143       return 1;
144     if (SrcCounters < SrcCountersStart ||
145         (const char *)SrcCounters >= SrcNameStart ||
146         (const char *)(SrcCounters + NC) > SrcNameStart)
147       return 1;
148     for (unsigned I = 0; I < NC; I++)
149       DstCounters[I] += SrcCounters[I];
150 
151     /* Now merge value profile data. */
152     if (!VPMergeHook)
153       continue;
154 
155     for (unsigned I = 0; I <= IPVK_Last; I++)
156       NVK += (SrcData->NumValueSites[I] != 0);
157 
158     if (!NVK)
159       continue;
160 
161     if (SrcValueProfData >= ProfileData + ProfileSize)
162       return 1;
163     VPMergeHook((ValueProfData *)SrcValueProfData, DstData);
164     SrcValueProfData =
165         SrcValueProfData + ((ValueProfData *)SrcValueProfData)->TotalSize;
166   }
167 
168   return 0;
169 }
170