blob: 945f1c4ac38d8405e8bdac24b0975f7b05f36026 [file] [log] [blame]
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +00001/*===- 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 Smithf1c93612014-03-21 18:47:23 +000010#ifndef PROFILE_INSTRPROFILING_H_
11#define PROFILE_INSTRPROFILING_H_
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000012
Xinliang David Li26bcd192015-12-10 18:17:01 +000013#include "InstrProfilingPort.h"
Vedant Kumar17af8922016-06-08 16:39:43 +000014
15#define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY
Xinliang David Li0ca5bd42015-11-23 17:09:40 +000016#include "InstrProfData.inc"
17
Xinliang David Li78c4a442015-11-20 19:41:02 +000018enum ValueKind {
Xinliang David Li5c7fa272015-11-23 17:43:10 +000019#define VALUE_PROF_KIND(Enumerator, Value) Enumerator = Value,
20#include "InstrProfData.inc"
Xinliang David Li78c4a442015-11-20 19:41:02 +000021};
Duncan P. N. Exon Smith812dcae2014-03-21 18:29:24 +000022
Xinliang David Li78c4a442015-11-20 19:41:02 +000023typedef void *IntPtrT;
Xinliang David Liabfd5532015-12-16 03:29:15 +000024typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
25 __llvm_profile_data {
Xinliang David Lia1bebf22015-11-23 17:50:53 +000026#define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name;
27#include "InstrProfData.inc"
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000028} __llvm_profile_data;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000029
Xinliang David Li4da5de92015-10-16 22:21:56 +000030typedef struct __llvm_profile_header {
Xinliang David Li6376db52015-11-23 18:36:40 +000031#define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name;
32#include "InstrProfData.inc"
Xinliang David Li4da5de92015-10-16 22:21:56 +000033} __llvm_profile_header;
34
Xinliang David Lif90425e2016-05-16 20:33:30 +000035typedef struct ValueProfNode * PtrToNodeT;
36typedef struct ValueProfNode {
37#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer) Type Name;
38#include "InstrProfData.inc"
39} ValueProfNode;
40
Betul Buyukkurt808385f2015-11-18 18:12:35 +000041/*!
42 * \brief Get number of bytes necessary to pad the argument to eight
43 * byte boundary.
44 */
45uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes);
Xinliang David Li4da5de92015-10-16 22:21:56 +000046
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000047/*!
48 * \brief Get required size for profile buffer.
49 */
50uint64_t __llvm_profile_get_size_for_buffer(void);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000051
52/*!
53 * \brief Write instrumentation data to the given buffer.
54 *
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000055 * \pre \c Buffer is the start of a buffer at least as big as \a
56 * __llvm_profile_get_size_for_buffer().
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000057 */
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000058int __llvm_profile_write_buffer(char *Buffer);
Duncan P. N. Exon Smithda0de8a22014-03-20 03:23:10 +000059
Justin Bognercc0d7ee2014-09-04 15:45:31 +000060const __llvm_profile_data *__llvm_profile_begin_data(void);
61const __llvm_profile_data *__llvm_profile_end_data(void);
62const char *__llvm_profile_begin_names(void);
63const char *__llvm_profile_end_names(void);
64uint64_t *__llvm_profile_begin_counters(void);
65uint64_t *__llvm_profile_end_counters(void);
Xinliang David Li4e8754d2016-05-21 22:55:45 +000066ValueProfNode *__llvm_profile_begin_vnodes();
67ValueProfNode *__llvm_profile_end_vnodes();
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000068
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000069/*!
Xinliang David Lic5c32cb2015-11-18 21:54:40 +000070 * \brief Clear profile counters to zero.
71 *
72 */
73void __llvm_profile_reset_counters(void);
74
75/*!
Xinliang David Li31f251f2016-03-04 18:58:30 +000076 * \brief Merge profile data from buffer.
77 *
78 * Read profile data form buffer \p Profile and merge with
Xinliang David Lidd12e9a2016-03-03 18:54:46 +000079 * in-process profile counters. The client is expected to
80 * have checked or already knows the profile data in the
81 * buffer matches the in-process counter structure before
82 * calling it.
83 */
84void __llvm_profile_merge_from_buffer(const char *Profile, uint64_t Size);
85
Xinliang David Li31f251f2016-03-04 18:58:30 +000086/*! \brief Check if profile in buffer matches the current binary.
87 *
88 * Returns 0 (success) if the profile data in buffer \p Profile with size
89 * \p Size was generated by the same binary and therefore matches
90 * structurally the in-process counters. If the profile data in buffer is
91 * not compatible, the interface returns 1 (failure).
92 */
93int __llvm_profile_check_compatibility(const char *Profile,
94 uint64_t Size);
95
Xinliang David Lidd12e9a2016-03-03 18:54:46 +000096/*!
Betul Buyukkurt808385f2015-11-18 18:12:35 +000097 * \brief Counts the number of times a target value is seen.
98 *
99 * Records the target value for the CounterIndex if not seen before. Otherwise,
100 * increments the counter associated w/ the target value.
Xinliang David Li39f4c2c2015-11-23 20:07:09 +0000101 * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
102 * uint32_t CounterIndex);
Betul Buyukkurt808385f2015-11-18 18:12:35 +0000103 */
Xinliang David Li39f4c2c2015-11-23 20:07:09 +0000104void INSTR_PROF_VALUE_PROF_FUNC(
105#define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName
106#include "InstrProfData.inc"
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000107 );
Betul Buyukkurt808385f2015-11-18 18:12:35 +0000108
109/*!
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000110 * \brief Write instrumentation data to the current file.
111 *
Xinliang David Li54dd6832015-12-29 07:13:59 +0000112 * Writes to the file with the last name given to \a *
113 * __llvm_profile_set_filename(),
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000114 * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
Xinliang David Lie9539332016-07-21 23:19:18 +0000115 * or if that's not set, the last name set to INSTR_PROF_PROFILE_NAME_VAR,
116 * or if that's not set, \c "default.profraw".
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000117 */
118int __llvm_profile_write_file(void);
119
120/*!
Xinliang David Li3b2c0022016-08-09 04:21:14 +0000121 * \brief this is a wrapper interface to \c __llvm_profile_write_file.
122 * After this interface is invoked, a arleady dumped flag will be set
123 * so that profile won't be dumped again during program exit.
124 * Invocation of interface __llvm_profile_reset_counters will clear
125 * the flag. This interface is designed to be used to collect profile
126 * data from user selected hot regions. The use model is
127 * __llvm_profile_reset_counters();
128 * ... hot region 1
129 * __llvm_profile_dump();
130 * .. some other code
131 * __llvm_profile_reset_counters();
132 * ... hot region 2
133 * __llvm_profile_dump();
134 *
135 * It is expected that on-line profile merging is on with \c %m specifier
136 * used in profile filename . If merging is not turned on, user is expected
137 * to invoke __llvm_profile_set_filename to specify different profile names
138 * for different regions before dumping to avoid profile write clobbering.
139 */
140int __llvm_profile_dump(void);
141
142/*!
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000143 * \brief Set the filename for writing instrumentation data.
144 *
145 * Sets the filename to be used for subsequent calls to
146 * \a __llvm_profile_write_file().
147 *
148 * \c Name is not copied, so it must remain valid. Passing NULL resets the
149 * filename logic to the default behaviour.
150 */
151void __llvm_profile_set_filename(const char *Name);
152
153/*! \brief Register to write instrumentation data to file at exit. */
154int __llvm_profile_register_write_file_atexit(void);
155
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000156/*! \brief Initialize file handling. */
157void __llvm_profile_initialize_file(void);
Duncan P. N. Exon Smith08439882014-05-16 01:30:24 +0000158
Xinliang David Lieaf238d2016-07-20 04:26:09 +0000159/*!
160 * \brief Return path prefix (excluding the base filename) of the profile data.
161 * This is useful for users using \c -fprofile-generate=./path_prefix who do
162 * not care about the default raw profile name. It is also useful to collect
163 * more than more profile data files dumped in the same directory (Online
164 * merge mode is turned on for instrumented programs with shared libs).
165 * Side-effect: this API call will invoke malloc with dynamic memory allocation.
166 */
167const char *__llvm_profile_get_path_prefix();
168
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000169/*! \brief Get the magic token for the file format. */
170uint64_t __llvm_profile_get_magic(void);
171
172/*! \brief Get the version of the file format. */
173uint64_t __llvm_profile_get_version(void);
174
Vedant Kumarb8502512016-02-26 02:49:41 +0000175/*! \brief Get the number of entries in the profile data section. */
176uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
177 const __llvm_profile_data *End);
178
Xinliang David Li32173153a2016-05-18 17:44:57 +0000179/*!
Sean Silvaaa705ec2016-05-19 00:12:02 +0000180 * This variable is defined in InstrProfilingRuntime.cc as a hidden
Xinliang David Li32173153a2016-05-18 17:44:57 +0000181 * symbol. Its main purpose is to enable profile runtime user to
182 * bypass runtime initialization code -- if the client code explicitly
183 * define this variable, then InstProfileRuntime.o won't be linked in.
184 * Note that this variable's visibility needs to be hidden so that the
185 * definition of this variable in an instrumented shared library won't
186 * affect runtime initialization decision of the main program.
Xinliang David Lif0e0a742016-07-22 04:08:16 +0000187 * __llvm_profile_profile_runtime. */
188COMPILER_RT_VISIBILITY extern int INSTR_PROF_PROFILE_RUNTIME_VAR;
Xinliang David Li315e49d2016-05-24 21:29:18 +0000189
Xinliang David Li32173153a2016-05-18 17:44:57 +0000190/*!
191 * This variable is defined in InstrProfiling.c. Its main purpose is to
192 * encode the raw profile version value and other format related information
193 * such as whether the profile is from IR based instrumentation. The variable
194 * is defined as weak so that compiler can emit an overriding definition
195 * depending on user option. Since we don't support mixing FE and IR based
196 * data in the same raw profile data file (in other words, shared libs and
197 * main program are expected to be instrumented in the same way), there is
Sean Silva29e43472016-05-20 03:25:15 +0000198 * no need for this variable to be hidden.
Xinliang David Li32173153a2016-05-18 17:44:57 +0000199 */
Xinliang David Lif0e0a742016-07-22 04:08:16 +0000200extern uint64_t INSTR_PROF_RAW_VERSION_VAR; /* __llvm_profile_raw_version */
Xinliang David Li32173153a2016-05-18 17:44:57 +0000201
Xinliang David Lie9539332016-07-21 23:19:18 +0000202/*!
203 * This variable is a weak symbol defined in InstrProfiling.c. It allows
204 * compiler instrumentation to provide overriding definition with value
205 * from compiler command line. This variable has default visibility.
206 */
207extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */
208
Duncan P. N. Exon Smithf1c93612014-03-21 18:47:23 +0000209#endif /* PROFILE_INSTRPROFILING_H_ */