Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 1 | /*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\ |
| 2 | |* |
| 3 | |* The LLVM Compiler Infrastructure |
| 4 | |* |
| 5 | |* This file is distributed under the University of Illinois Open Source |
| 6 | |* License. See LICENSE.TXT for details. |
| 7 | |* |
| 8 | \*===----------------------------------------------------------------------===*/ |
| 9 | |
Duncan P. N. Exon Smith | f1c9361 | 2014-03-21 18:47:23 +0000 | [diff] [blame] | 10 | #ifndef PROFILE_INSTRPROFILING_H_ |
| 11 | #define PROFILE_INSTRPROFILING_H_ |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 12 | |
Xinliang David Li | 26bcd19 | 2015-12-10 18:17:01 +0000 | [diff] [blame] | 13 | #include "InstrProfilingPort.h" |
Xinliang David Li | 0ca5bd4 | 2015-11-23 17:09:40 +0000 | [diff] [blame] | 14 | #include "InstrProfData.inc" |
| 15 | |
Xinliang David Li | 78c4a44 | 2015-11-20 19:41:02 +0000 | [diff] [blame] | 16 | enum ValueKind { |
Xinliang David Li | 5c7fa27 | 2015-11-23 17:43:10 +0000 | [diff] [blame] | 17 | #define VALUE_PROF_KIND(Enumerator, Value) Enumerator = Value, |
| 18 | #include "InstrProfData.inc" |
Xinliang David Li | 78c4a44 | 2015-11-20 19:41:02 +0000 | [diff] [blame] | 19 | }; |
Duncan P. N. Exon Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 20 | |
Xinliang David Li | 78c4a44 | 2015-11-20 19:41:02 +0000 | [diff] [blame] | 21 | typedef void *IntPtrT; |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 22 | typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT) |
| 23 | __llvm_profile_data { |
Xinliang David Li | a1bebf2 | 2015-11-23 17:50:53 +0000 | [diff] [blame] | 24 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name; |
| 25 | #include "InstrProfData.inc" |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame] | 26 | } __llvm_profile_data; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 27 | |
Xinliang David Li | 4da5de9 | 2015-10-16 22:21:56 +0000 | [diff] [blame] | 28 | typedef struct __llvm_profile_header { |
Xinliang David Li | 6376db5 | 2015-11-23 18:36:40 +0000 | [diff] [blame] | 29 | #define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name; |
| 30 | #include "InstrProfData.inc" |
Xinliang David Li | 4da5de9 | 2015-10-16 22:21:56 +0000 | [diff] [blame] | 31 | } __llvm_profile_header; |
| 32 | |
Xinliang David Li | f90425e | 2016-05-16 20:33:30 +0000 | [diff] [blame] | 33 | typedef struct ValueProfNode * PtrToNodeT; |
| 34 | typedef struct ValueProfNode { |
| 35 | #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer) Type Name; |
| 36 | #include "InstrProfData.inc" |
| 37 | } ValueProfNode; |
| 38 | |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame] | 39 | /*! |
| 40 | * \brief Get number of bytes necessary to pad the argument to eight |
| 41 | * byte boundary. |
| 42 | */ |
| 43 | uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes); |
Xinliang David Li | 4da5de9 | 2015-10-16 22:21:56 +0000 | [diff] [blame] | 44 | |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 45 | /*! |
| 46 | * \brief Get required size for profile buffer. |
| 47 | */ |
| 48 | uint64_t __llvm_profile_get_size_for_buffer(void); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 49 | |
| 50 | /*! |
| 51 | * \brief Write instrumentation data to the given buffer. |
| 52 | * |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 53 | * \pre \c Buffer is the start of a buffer at least as big as \a |
| 54 | * __llvm_profile_get_size_for_buffer(). |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 55 | */ |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 56 | int __llvm_profile_write_buffer(char *Buffer); |
Duncan P. N. Exon Smith | da0de8a2 | 2014-03-20 03:23:10 +0000 | [diff] [blame] | 57 | |
Justin Bogner | cc0d7ee | 2014-09-04 15:45:31 +0000 | [diff] [blame] | 58 | const __llvm_profile_data *__llvm_profile_begin_data(void); |
| 59 | const __llvm_profile_data *__llvm_profile_end_data(void); |
| 60 | const char *__llvm_profile_begin_names(void); |
| 61 | const char *__llvm_profile_end_names(void); |
| 62 | uint64_t *__llvm_profile_begin_counters(void); |
| 63 | uint64_t *__llvm_profile_end_counters(void); |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 64 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 65 | /*! |
Xinliang David Li | c5c32cb | 2015-11-18 21:54:40 +0000 | [diff] [blame] | 66 | * \brief Clear profile counters to zero. |
| 67 | * |
| 68 | */ |
| 69 | void __llvm_profile_reset_counters(void); |
| 70 | |
| 71 | /*! |
Xinliang David Li | 31f251f | 2016-03-04 18:58:30 +0000 | [diff] [blame] | 72 | * \brief Merge profile data from buffer. |
| 73 | * |
| 74 | * Read profile data form buffer \p Profile and merge with |
Xinliang David Li | dd12e9a | 2016-03-03 18:54:46 +0000 | [diff] [blame] | 75 | * in-process profile counters. The client is expected to |
| 76 | * have checked or already knows the profile data in the |
| 77 | * buffer matches the in-process counter structure before |
| 78 | * calling it. |
| 79 | */ |
| 80 | void __llvm_profile_merge_from_buffer(const char *Profile, uint64_t Size); |
| 81 | |
Xinliang David Li | 31f251f | 2016-03-04 18:58:30 +0000 | [diff] [blame] | 82 | /*! \brief Check if profile in buffer matches the current binary. |
| 83 | * |
| 84 | * Returns 0 (success) if the profile data in buffer \p Profile with size |
| 85 | * \p Size was generated by the same binary and therefore matches |
| 86 | * structurally the in-process counters. If the profile data in buffer is |
| 87 | * not compatible, the interface returns 1 (failure). |
| 88 | */ |
| 89 | int __llvm_profile_check_compatibility(const char *Profile, |
| 90 | uint64_t Size); |
| 91 | |
Xinliang David Li | dd12e9a | 2016-03-03 18:54:46 +0000 | [diff] [blame] | 92 | /*! |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame] | 93 | * \brief Counts the number of times a target value is seen. |
| 94 | * |
| 95 | * Records the target value for the CounterIndex if not seen before. Otherwise, |
| 96 | * increments the counter associated w/ the target value. |
Xinliang David Li | 39f4c2c | 2015-11-23 20:07:09 +0000 | [diff] [blame] | 97 | * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data, |
| 98 | * uint32_t CounterIndex); |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame] | 99 | */ |
Xinliang David Li | 39f4c2c | 2015-11-23 20:07:09 +0000 | [diff] [blame] | 100 | void INSTR_PROF_VALUE_PROF_FUNC( |
| 101 | #define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName |
| 102 | #include "InstrProfData.inc" |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 103 | ); |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame] | 104 | |
| 105 | /*! |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 106 | * \brief Write instrumentation data to the current file. |
| 107 | * |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 108 | * Writes to the file with the last name given to \a * |
| 109 | * __llvm_profile_set_filename(), |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 110 | * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable, |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 111 | * or if that's not set, the last name given to |
| 112 | * \a __llvm_profile_override_default_filename(), or if that's not set, |
Diego Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 113 | * \c "default.profraw". |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 114 | */ |
| 115 | int __llvm_profile_write_file(void); |
| 116 | |
| 117 | /*! |
| 118 | * \brief Set the filename for writing instrumentation data. |
| 119 | * |
| 120 | * Sets the filename to be used for subsequent calls to |
| 121 | * \a __llvm_profile_write_file(). |
| 122 | * |
| 123 | * \c Name is not copied, so it must remain valid. Passing NULL resets the |
| 124 | * filename logic to the default behaviour. |
| 125 | */ |
| 126 | void __llvm_profile_set_filename(const char *Name); |
| 127 | |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 128 | /*! |
| 129 | * \brief Set the filename for writing instrumentation data, unless the |
| 130 | * \c LLVM_PROFILE_FILE environment variable was set. |
| 131 | * |
| 132 | * Unless overridden, sets the filename to be used for subsequent calls to |
| 133 | * \a __llvm_profile_write_file(). |
| 134 | * |
| 135 | * \c Name is not copied, so it must remain valid. Passing NULL resets the |
| 136 | * filename logic to the default behaviour (unless the \c LLVM_PROFILE_FILE |
| 137 | * was set in which case it has no effect). |
| 138 | */ |
| 139 | void __llvm_profile_override_default_filename(const char *Name); |
| 140 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 141 | /*! \brief Register to write instrumentation data to file at exit. */ |
| 142 | int __llvm_profile_register_write_file_atexit(void); |
| 143 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 144 | /*! \brief Initialize file handling. */ |
| 145 | void __llvm_profile_initialize_file(void); |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 146 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 147 | /*! \brief Get the magic token for the file format. */ |
| 148 | uint64_t __llvm_profile_get_magic(void); |
| 149 | |
| 150 | /*! \brief Get the version of the file format. */ |
| 151 | uint64_t __llvm_profile_get_version(void); |
| 152 | |
Vedant Kumar | b850251 | 2016-02-26 02:49:41 +0000 | [diff] [blame] | 153 | /*! \brief Get the number of entries in the profile data section. */ |
| 154 | uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin, |
| 155 | const __llvm_profile_data *End); |
| 156 | |
Xinliang David Li | 32173153a | 2016-05-18 17:44:57 +0000 | [diff] [blame] | 157 | /*! |
Sean Silva | aa705ec | 2016-05-19 00:12:02 +0000 | [diff] [blame] | 158 | * This variable is defined in InstrProfilingRuntime.cc as a hidden |
Xinliang David Li | 32173153a | 2016-05-18 17:44:57 +0000 | [diff] [blame] | 159 | * symbol. Its main purpose is to enable profile runtime user to |
| 160 | * bypass runtime initialization code -- if the client code explicitly |
| 161 | * define this variable, then InstProfileRuntime.o won't be linked in. |
| 162 | * Note that this variable's visibility needs to be hidden so that the |
| 163 | * definition of this variable in an instrumented shared library won't |
| 164 | * affect runtime initialization decision of the main program. |
| 165 | */ |
| 166 | COMPILER_RT_VISIBILITY extern int __llvm_profile_runtime; |
Sean Silva | 29e4347 | 2016-05-20 03:25:15 +0000 | [diff] [blame^] | 167 | /*! |
Xinliang David Li | 32173153a | 2016-05-18 17:44:57 +0000 | [diff] [blame] | 168 | * This variable is defined in InstrProfilingFile.c. Its visibility is |
| 169 | * not hidden so that instrumented shared libraries and the main program |
| 170 | * can share the raw data file with the same name. |
| 171 | */ |
| 172 | extern int __llvm_profile_OwnsFilename; |
| 173 | extern const char *__llvm_profile_CurrentFilename; |
| 174 | /*! |
| 175 | * This variable is defined in InstrProfiling.c. Its main purpose is to |
| 176 | * encode the raw profile version value and other format related information |
| 177 | * such as whether the profile is from IR based instrumentation. The variable |
| 178 | * is defined as weak so that compiler can emit an overriding definition |
| 179 | * depending on user option. Since we don't support mixing FE and IR based |
| 180 | * data in the same raw profile data file (in other words, shared libs and |
| 181 | * main program are expected to be instrumented in the same way), there is |
Sean Silva | 29e4347 | 2016-05-20 03:25:15 +0000 | [diff] [blame^] | 182 | * no need for this variable to be hidden. |
Xinliang David Li | 32173153a | 2016-05-18 17:44:57 +0000 | [diff] [blame] | 183 | */ |
| 184 | extern uint64_t __llvm_profile_raw_version; |
| 185 | |
Duncan P. N. Exon Smith | f1c9361 | 2014-03-21 18:47:23 +0000 | [diff] [blame] | 186 | #endif /* PROFILE_INSTRPROFILING_H_ */ |