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