1/*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
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
9#ifndef PROFILE_INSTRPROFILING_H_
10#define PROFILE_INSTRPROFILING_H_
11
12#include "InstrProfilingPort.h"
13#include <stddef.h>
14#if defined(__FreeBSD__) && defined(_KERNEL)
15#include <sys/types.h>
16#endif
17#ifndef COMPILER_RT_PROFILE_BAREMETAL
18#include <stdio.h>
19#endif
20
21// Make sure __LLVM_INSTR_PROFILE_GENERATE is always defined before
22// including instr_prof_interface.h so the interface functions are
23// declared correctly for the runtime.
24// __LLVM_INSTR_PROFILE_GENERATE is always `#undef`ed after the header,
25// because compiler-rt does not support profiling the profiling runtime itself.
26#ifndef __LLVM_INSTR_PROFILE_GENERATE
27#define __LLVM_INSTR_PROFILE_GENERATE
28#endif
29#include "profile/instr_prof_interface.h"
30#undef __LLVM_INSTR_PROFILE_GENERATE
31
32#define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY
33#include "profile/InstrProfData.inc"
34
35enum ValueKind {
36#define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,
37#include "profile/InstrProfData.inc"
38};
39
40typedef void *IntPtrT;
41typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
42 __llvm_profile_data {
43#define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name;
44#include "profile/InstrProfData.inc"
45} __llvm_profile_data;
46
47typedef struct __llvm_profile_header {
48#define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name;
49#include "profile/InstrProfData.inc"
50} __llvm_profile_header;
51
52typedef struct ValueProfNode * PtrToNodeT;
53typedef struct ValueProfNode {
54#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer) Type Name;
55#include "profile/InstrProfData.inc"
56} ValueProfNode;
57
58typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT) VTableProfData {
59#define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Initializer) Type Name;
60#include "profile/InstrProfData.inc"
61} VTableProfData;
62
63typedef struct __llvm_profile_gpu_sections {
64#define INSTR_PROF_GPU_SECT(Type, LLVMType, Name, Initializer) Type Name;
65#include "profile/InstrProfData.inc"
66} __llvm_profile_gpu_sections;
67
68typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
69 __llvm_gcov_init_func_struct {
70#define COVINIT_FUNC(Type, LLVMType, Name, Initializer) Type Name;
71#include "profile/InstrProfData.inc"
72} __llvm_gcov_init_func_struct;
73
74/*!
75 * \brief Return 1 if profile counters are continuously synced to the raw
76 * profile via an mmap(). This is in contrast to the default mode, in which
77 * the raw profile is written out at program exit time.
78 */
79int __llvm_profile_is_continuous_mode_enabled(void);
80
81/*!
82 * \brief Enable continuous mode.
83 *
84 * See \ref __llvm_profile_is_continuous_mode_enabled. The behavior is undefined
85 * if continuous mode is already enabled, or if it cannot be enable due to
86 * conflicting options.
87 */
88void __llvm_profile_enable_continuous_mode(void);
89
90/*!
91 * \brief Disable continuous mode.
92 *
93 */
94void __llvm_profile_disable_continuous_mode(void);
95
96/*!
97 * \brief Set the page size.
98 *
99 * This is a pre-requisite for enabling continuous mode. The buffer size
100 * calculation code inside of libprofile cannot simply call getpagesize(), as
101 * it is not allowed to depend on libc.
102 */
103void __llvm_profile_set_page_size(unsigned PageSize);
104
105/*!
106 * \brief Get number of bytes necessary to pad the argument to eight
107 * byte boundary.
108 */
109uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes);
110
111const __llvm_profile_data *__llvm_profile_begin_data(void);
112const __llvm_profile_data *__llvm_profile_end_data(void);
113const char *__llvm_profile_begin_names(void);
114const char *__llvm_profile_end_names(void);
115const char *__llvm_profile_begin_vtabnames(void);
116const char *__llvm_profile_end_vtabnames(void);
117char *__llvm_profile_begin_counters(void);
118char *__llvm_profile_end_counters(void);
119char *__llvm_profile_begin_bitmap(void);
120char *__llvm_profile_end_bitmap(void);
121ValueProfNode *__llvm_profile_begin_vnodes(void);
122ValueProfNode *__llvm_profile_end_vnodes(void);
123const VTableProfData *__llvm_profile_begin_vtables(void);
124const VTableProfData *__llvm_profile_end_vtables(void);
125
126/*!
127 * \brief Counts the number of times a target value is seen.
128 *
129 * Records the target value for the CounterIndex if not seen before. Otherwise,
130 * increments the counter associated w/ the target value.
131 * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
132 * uint32_t CounterIndex);
133 */
134void INSTR_PROF_VALUE_PROF_FUNC(
135#define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName
136#include "profile/InstrProfData.inc"
137 );
138
139void __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data,
140 uint32_t CounterIndex,
141 uint64_t CounterValue);
142
143/*!
144 * \brief Wave-cooperative counter increment for GPU targets.
145 *
146 * Reduces per-lane atomic contention by electing a single lane per wave to
147 * perform the counter update. \c Uniform is an optional counter tracking the
148 * number of uniform.
149 */
150void INSTR_PROF_INSTRUMENT_GPU_FUNC(uint64_t *Counter, uint64_t *Uniform,
151 uint64_t Step);
152
153/*!
154 * \brief Set the FILE object for writing instrumentation data. Return 0 if set
155 * successfully or return 1 if failed.
156 *
157 * Sets the FILE object to be used for subsequent calls to
158 * \a __llvm_profile_write_file(). The profile file name set by environment
159 * variable, command-line option, or calls to \a __llvm_profile_set_filename
160 * will be ignored.
161 *
162 * \c File will not be closed after a call to \a __llvm_profile_write_file() but
163 * it may be flushed. Passing NULL restores default behavior.
164 *
165 * If \c EnableMerge is nonzero, the runtime will always merge profiling data
166 * with the contents of the profiling file. If EnableMerge is zero, the runtime
167 * may still merge the data if it would have merged for another reason (for
168 * example, because of a %m specifier in the file name).
169 *
170 * Note: There may be multiple copies of the profile runtime (one for each
171 * instrumented image/DSO). This API only modifies the file object within the
172 * copy of the runtime available to the calling image.
173 *
174 * Warning: This is a no-op if EnableMerge is 0 in continuous mode (\ref
175 * __llvm_profile_is_continuous_mode_enabled), because disable merging requires
176 * copying the old profile file to new profile file and this function is usually
177 * used when the proess doesn't have permission to open file.
178 */
179#ifndef COMPILER_RT_PROFILE_BAREMETAL
180int __llvm_profile_set_file_object(FILE *File, int EnableMerge);
181#endif
182
183/*! \brief Register to write instrumentation data to file at exit. */
184int __llvm_profile_register_write_file_atexit(void);
185
186/*! \brief Initialize the profile runtime. */
187void __llvm_profile_initialize(void);
188
189/*! \brief Initialize the gcov profile runtime. */
190void __llvm_profile_gcov_initialize(void);
191
192/*!
193 * \brief Return path prefix (excluding the base filename) of the profile data.
194 * This is useful for users using \c -fprofile-generate=./path_prefix who do
195 * not care about the default raw profile name. It is also useful to collect
196 * more than more profile data files dumped in the same directory (Online
197 * merge mode is turned on for instrumented programs with shared libs).
198 * Side-effect: this API call will invoke malloc with dynamic memory allocation.
199 */
200const char *__llvm_profile_get_path_prefix(void);
201
202/*! \brief Get the magic token for the file format. */
203uint64_t __llvm_profile_get_magic(void);
204
205/*! \brief Get the version of the file format. */
206uint64_t __llvm_profile_get_version(void);
207
208/*! \brief Get the number of entries in the profile data section. */
209uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,
210 const __llvm_profile_data *End);
211
212/*! \brief Get the size of the profile data section in bytes. */
213uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
214 const __llvm_profile_data *End);
215
216/*! \brief Get the size in bytes of a single counter entry. */
217size_t __llvm_profile_counter_entry_size(void);
218
219/*! \brief Get the number of entries in the profile counters section. */
220uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End);
221
222/*! \brief Get the size of the profile counters section in bytes. */
223uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End);
224
225/*! \brief Get the number of bytes in the profile bitmap section. */
226uint64_t __llvm_profile_get_num_bitmap_bytes(const char *Begin,
227 const char *End);
228
229/*! \brief Get the size of the profile name section in bytes. */
230uint64_t __llvm_profile_get_name_size(const char *Begin, const char *End);
231
232/*! \brief Get the number of virtual table profile data entries */
233uint64_t __llvm_profile_get_num_vtable(const VTableProfData *Begin,
234 const VTableProfData *End);
235
236/*! \brief Get the size of virtual table profile data in bytes. */
237uint64_t __llvm_profile_get_vtable_section_size(const VTableProfData *Begin,
238 const VTableProfData *End);
239
240/* ! \brief Given the sizes of the data and counter information, computes the
241 * number of padding bytes before and after the counter section, as well as the
242 * number of padding bytes after other sections in the raw profile.
243 * Returns -1 upon errors and 0 upon success. Output parameters should be used
244 * iff return value is 0.
245 *
246 * Note: When mmap() mode is disabled, no padding bytes before/after counters
247 * are needed. However, in mmap() mode, the counter section in the raw profile
248 * must be page-aligned: this API computes the number of padding bytes
249 * needed to achieve that.
250 */
251int __llvm_profile_get_padding_sizes_for_counters(
252 uint64_t DataSize, uint64_t CountersSize, uint64_t NumBitmapBytes,
253 uint64_t NumUniformCounters, uint64_t NamesSize, uint64_t VTableSize,
254 uint64_t VNameSize, uint64_t *PaddingBytesBeforeCounters,
255 uint64_t *PaddingBytesAfterCounters, uint64_t *PaddingBytesAfterBitmap,
256 uint64_t *PaddingBytesAfterUniformCounters,
257 uint64_t *PaddingBytesAfterNames, uint64_t *PaddingBytesAfterVTable,
258 uint64_t *PaddingBytesAfterVNames);
259
260/*!
261 * \brief Set the flag that profile data has been dumped to the file.
262 * This is useful for users to disable dumping profile data to the file for
263 * certain processes in case the processes don't have permission to write to
264 * the disks, and trying to do so would result in side effects such as crashes.
265 */
266void __llvm_profile_set_dumped(void);
267
268/*!
269 * \brief Write custom target-specific profiling data to a separate file.
270 * Used by offload PGO (HIP and OpenMP).
271 *
272 * \param Target Target triple (e.g., "amdgcn-amd-amdhsa")
273 * \param DataBegin Start of profile data records
274 * \param DataEnd End of profile data records
275 * \param CountersBegin Start of counter data
276 * \param CountersEnd End of counter data
277 * \param UniformCountersBegin Start of uniform counters (NULL if not used)
278 * \param UniformCountersEnd End of uniform counters (NULL if not used)
279 * \param NamesBegin Start of names data
280 * \param NamesEnd End of names data
281 * \param VersionOverride Profile version override (NULL to use default)
282 */
283int __llvm_write_custom_profile(
284 const char *Target, const __llvm_profile_data *DataBegin,
285 const __llvm_profile_data *DataEnd, const char *CountersBegin,
286 const char *CountersEnd, const char *UniformCountersBegin,
287 const char *UniformCountersEnd, const char *NamesBegin,
288 const char *NamesEnd, const uint64_t *VersionOverride);
289
290/*!
291 * This variable is defined in InstrProfilingRuntime.cpp as a hidden
292 * symbol. Its main purpose is to enable profile runtime user to
293 * bypass runtime initialization code -- if the client code explicitly
294 * define this variable, then InstProfileRuntime.o won't be linked in.
295 * Note that this variable's visibility needs to be hidden so that the
296 * definition of this variable in an instrumented shared library won't
297 * affect runtime initialization decision of the main program.
298 * __llvm_profile_profile_runtime. */
299COMPILER_RT_VISIBILITY extern int INSTR_PROF_PROFILE_RUNTIME_VAR;
300
301/*!
302 * This variable is defined in InstrProfilingVersionVar.c as a hidden symbol
303 * (except on Apple platforms where this symbol is checked by TAPI). Its main
304 * purpose is to encode the raw profile version value and other format related
305 * information such as whether the profile is from IR based instrumentation. The
306 * variable is defined as weak so that compiler can emit an overriding
307 * definition depending on user option.
308 */
309COMPILER_RT_VISIBILITY extern uint64_t
310 INSTR_PROF_RAW_VERSION_VAR; /* __llvm_profile_raw_version */
311
312/*!
313 * This variable is a weak symbol defined in InstrProfiling.c. It allows
314 * compiler instrumentation to provide overriding definition with value
315 * from compiler command line. This variable has default visibility.
316 */
317extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */
318
319const __llvm_gcov_init_func_struct *__llvm_profile_begin_covinit();
320const __llvm_gcov_init_func_struct *__llvm_profile_end_covinit();
321
322#endif /* PROFILE_INSTRPROFILING_H_ */
323