2014-12-09 22:07:25 +00:00
|
|
|
/*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
|
|
|
|
|*
|
2019-01-19 08:50:56 +00:00
|
|
|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|* See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-12-09 22:07:25 +00:00
|
|
|
|*
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#ifndef PROFILE_INSTRPROFILING_INTERNALH_
|
|
|
|
#define PROFILE_INSTRPROFILING_INTERNALH_
|
|
|
|
|
2017-12-14 19:01:04 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2014-12-09 22:07:25 +00:00
|
|
|
#include "InstrProfiling.h"
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Write instrumentation data to the given buffer, given explicit
|
|
|
|
* pointers to the live data in memory. This function is probably not what you
|
|
|
|
* want. Use __llvm_profile_get_size_for_buffer instead. Use this function if
|
|
|
|
* your program has a custom memory layout.
|
|
|
|
*/
|
|
|
|
uint64_t __llvm_profile_get_size_for_buffer_internal(
|
|
|
|
const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
|
2023-09-21 13:07:31 -05:00
|
|
|
const char *CountersBegin, const char *CountersEnd, const char *BitmapBegin,
|
Reland "[TypeProf][InstrPGO] Introduce raw and instr profile format change for type profiling." (#82711)
New change on top of [reviewed
patch](https://github.com/llvm/llvm-project/pull/81691) are [in commits
after this
one](https://github.com/llvm/llvm-project/pull/82711/commits/d0757f46b3e3865b5f7c552bc0744309a363e0ac).
Previous commits are restored from the remote branch with timestamps.
1. Fix build breakage for non-ELF platforms, by defining the missing
functions {`__llvm_profile_begin_vtables`, `__llvm_profile_end_vtables`,
`__llvm_profile_begin_vtabnames `, `__llvm_profile_end_vtabnames`}
everywhere.
* Tested on mac laptop (for darwins) and Windows. Specifically,
functions in `InstrProfilingPlatformWindows.c` returns `NULL` to make it
more explicit that type prof isn't supported; see comments for the
reason.
* For the rest (AIX, other), mostly follow existing examples (like this
[one](https://github.com/llvm/llvm-project/commit/f95b2f1acf1171abb0d00089fd4c9238753847e3))
2. Rename `__llvm_prf_vtabnames` -> `__llvm_prf_vns` for shorter section
name, and make returned pointers
[const](https://github.com/llvm/llvm-project/pull/82711/commits/a825d2a4ec00f07772a373091a702f149c3b0c34#diff-4de780ce726d76b7abc9d3353aef95013e7b21e7bda01be8940cc6574fb0b5ffR120-R121)
**Original Description**
* Raw profile format
- Header: records the byte size of compressed vtable names, and the
number of profiled vtable entries (call it `VTableProfData`). Header
also records padded bytes of each section.
- Payload: adds a section for compressed vtable names, and a section to
store `VTableProfData`. Both sections are padded so the size is a
multiple of 8.
* Indexed profile format
- Header: records the byte offset of compressed vtable names.
- Payload: adds a section to store compressed vtable names. This section
is used by `llvm-profdata` to show the list of vtables profiled for an
instrumented site.
[The originally reviewed
patch](https://github.com/llvm/llvm-project/pull/66825) will have
profile reader/write change and llvm-profdata change.
- To ensure this PR has all the necessary profile format change along
with profile version bump, created a copy of the originally reviewed
patch in https://github.com/llvm/llvm-project/pull/80761. The copy
doesn't have profile format change, but it has the set of tests which
covers type profile generation, profile read and profile merge. Tests
pass there.
rfc in
https://discourse.llvm.org/t/rfc-dynamic-type-profiling-and-optimizations-in-llvm/74600
---------
Co-authored-by: modiking <modiking213@gmail.com>
2024-02-27 11:07:40 -08:00
|
|
|
const char *BitmapEnd, const char *NamesBegin, const char *NamesEnd,
|
|
|
|
const VTableProfData *VTableBegin, const VTableProfData *VTableEnd,
|
|
|
|
const char *VNamesBegin, const char *VNamesEnd);
|
2014-12-09 22:07:25 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Write instrumentation data to the given buffer, given explicit
|
|
|
|
* pointers to the live data in memory. This function is probably not what you
|
|
|
|
* want. Use __llvm_profile_write_buffer instead. Use this function if your
|
|
|
|
* program has a custom memory layout.
|
|
|
|
*
|
|
|
|
* \pre \c Buffer is the start of a buffer at least as big as \a
|
|
|
|
* __llvm_profile_get_size_for_buffer_internal().
|
|
|
|
*/
|
|
|
|
int __llvm_profile_write_buffer_internal(
|
|
|
|
char *Buffer, const __llvm_profile_data *DataBegin,
|
2021-12-29 13:36:28 -08:00
|
|
|
const __llvm_profile_data *DataEnd, const char *CountersBegin,
|
2023-09-21 13:07:31 -05:00
|
|
|
const char *CountersEnd, const char *BitmapBegin, const char *BitmapEnd,
|
|
|
|
const char *NamesBegin, const char *NamesEnd);
|
2014-12-09 22:07:25 +00:00
|
|
|
|
2015-11-18 21:08:03 +00:00
|
|
|
/*!
|
2015-12-29 23:54:41 +00:00
|
|
|
* The data structure describing the data to be written by the
|
|
|
|
* low level writer callback function.
|
2019-12-10 18:17:28 -08:00
|
|
|
*
|
|
|
|
* If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is
|
|
|
|
* 0, the write is skipped (the writer simply advances ElmSize*NumElm bytes).
|
|
|
|
*
|
|
|
|
* If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is
|
|
|
|
* nonzero, ElmSize*NumElm zero bytes are written.
|
2015-11-18 21:08:03 +00:00
|
|
|
*/
|
2015-11-21 04:16:42 +00:00
|
|
|
typedef struct ProfDataIOVec {
|
2015-11-21 07:26:46 +00:00
|
|
|
const void *Data;
|
2015-11-21 04:16:42 +00:00
|
|
|
size_t ElmSize;
|
|
|
|
size_t NumElm;
|
2019-12-10 18:17:28 -08:00
|
|
|
int UseZeroPadding;
|
2015-11-21 04:16:42 +00:00
|
|
|
} ProfDataIOVec;
|
|
|
|
|
2017-06-27 17:28:01 +00:00
|
|
|
struct ProfDataWriter;
|
|
|
|
typedef uint32_t (*WriterCallback)(struct ProfDataWriter *This, ProfDataIOVec *,
|
|
|
|
uint32_t NumIOVecs);
|
|
|
|
|
|
|
|
typedef struct ProfDataWriter {
|
|
|
|
WriterCallback Write;
|
|
|
|
void *WriterCtx;
|
|
|
|
} ProfDataWriter;
|
2015-12-29 23:54:41 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* The data structure for buffered IO of profile data.
|
|
|
|
*/
|
|
|
|
typedef struct ProfBufferIO {
|
2017-06-27 17:28:01 +00:00
|
|
|
ProfDataWriter *FileWriter;
|
|
|
|
uint32_t OwnFileWriter;
|
2015-12-29 23:54:41 +00:00
|
|
|
/* The start of the buffer. */
|
|
|
|
uint8_t *BufferStart;
|
|
|
|
/* Total size of the buffer. */
|
|
|
|
uint32_t BufferSz;
|
|
|
|
/* Current byte offset from the start of the buffer. */
|
|
|
|
uint32_t CurOffset;
|
|
|
|
} ProfBufferIO;
|
|
|
|
|
|
|
|
/* The creator interface used by testing. */
|
2016-05-13 18:26:26 +00:00
|
|
|
ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);
|
|
|
|
|
2015-12-29 23:54:41 +00:00
|
|
|
/*!
|
|
|
|
* This is the interface to create a handle for buffered IO.
|
|
|
|
*/
|
2017-06-27 17:28:01 +00:00
|
|
|
ProfBufferIO *lprofCreateBufferIO(ProfDataWriter *FileWriter);
|
2016-05-13 18:26:26 +00:00
|
|
|
|
2015-12-29 23:54:41 +00:00
|
|
|
/*!
|
|
|
|
* The interface to destroy the bufferIO handle and reclaim
|
|
|
|
* the memory.
|
|
|
|
*/
|
2016-03-06 04:18:13 +00:00
|
|
|
void lprofDeleteBufferIO(ProfBufferIO *BufferIO);
|
2015-12-29 23:54:41 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* This is the interface to write \c Data of \c Size bytes through
|
|
|
|
* \c BufferIO. Returns 0 if successful, otherwise return -1.
|
|
|
|
*/
|
2016-03-06 04:18:13 +00:00
|
|
|
int lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data,
|
|
|
|
uint32_t Size);
|
2015-12-29 23:54:41 +00:00
|
|
|
/*!
|
|
|
|
* The interface to flush the remaining data in the buffer.
|
|
|
|
* through the low level writer callback.
|
|
|
|
*/
|
2016-03-06 04:18:13 +00:00
|
|
|
int lprofBufferIOFlush(ProfBufferIO *BufferIO);
|
2015-12-29 23:54:41 +00:00
|
|
|
|
|
|
|
/* The low level interface to write data into a buffer. It is used as the
|
|
|
|
* callback by other high level writer methods such as buffered IO writer
|
|
|
|
* and profile data writer. */
|
2017-06-27 17:28:01 +00:00
|
|
|
uint32_t lprofBufferWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
|
|
|
|
uint32_t NumIOVecs);
|
|
|
|
void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer);
|
2015-12-29 23:54:41 +00:00
|
|
|
|
2016-05-14 20:12:42 +00:00
|
|
|
struct ValueProfData;
|
|
|
|
struct ValueProfRecord;
|
|
|
|
struct InstrProfValueData;
|
|
|
|
struct ValueProfNode;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* The class that defines a set of methods to read value profile
|
|
|
|
* data for streaming/serialization from the instrumentation runtime.
|
|
|
|
*/
|
|
|
|
typedef struct VPDataReaderType {
|
|
|
|
uint32_t (*InitRTRecord)(const __llvm_profile_data *Data,
|
|
|
|
uint8_t *SiteCountArray[]);
|
|
|
|
/* Function pointer to getValueProfRecordHeader method. */
|
|
|
|
uint32_t (*GetValueProfRecordHeaderSize)(uint32_t NumSites);
|
2025-03-28 12:51:13 -07:00
|
|
|
/* Function pointer to getFirstValueProfRecord method. */
|
2016-05-14 20:12:42 +00:00
|
|
|
struct ValueProfRecord *(*GetFirstValueProfRecord)(struct ValueProfData *);
|
|
|
|
/* Return the number of value data for site \p Site. */
|
|
|
|
uint32_t (*GetNumValueDataForSite)(uint32_t VK, uint32_t Site);
|
|
|
|
/* Return the total size of the value profile data of the
|
|
|
|
* current function. */
|
|
|
|
uint32_t (*GetValueProfDataSize)(void);
|
|
|
|
/*!
|
|
|
|
* Read the next \p N value data for site \p Site and store the data
|
|
|
|
* in \p Dst. \p StartNode is the first value node to start with if
|
|
|
|
* it is not null. The function returns the pointer to the value
|
|
|
|
* node pointer to be used as the \p StartNode of the next batch reading.
|
|
|
|
* If there is nothing left, it returns NULL.
|
|
|
|
*/
|
|
|
|
struct ValueProfNode *(*GetValueData)(uint32_t ValueKind, uint32_t Site,
|
|
|
|
struct InstrProfValueData *Dst,
|
|
|
|
struct ValueProfNode *StartNode,
|
|
|
|
uint32_t N);
|
|
|
|
} VPDataReaderType;
|
|
|
|
|
2021-09-04 13:01:34 +05:30
|
|
|
/* Write profile data to destination. If SkipNameDataWrite is set to 1,
|
|
|
|
the name data is already in destination, we just skip over it. */
|
2017-06-28 16:46:06 +00:00
|
|
|
int lprofWriteData(ProfDataWriter *Writer, VPDataReaderType *VPDataReader,
|
|
|
|
int SkipNameDataWrite);
|
2017-06-27 17:28:01 +00:00
|
|
|
int lprofWriteDataImpl(ProfDataWriter *Writer,
|
2016-03-06 04:18:13 +00:00
|
|
|
const __llvm_profile_data *DataBegin,
|
|
|
|
const __llvm_profile_data *DataEnd,
|
2021-12-29 13:36:28 -08:00
|
|
|
const char *CountersBegin, const char *CountersEnd,
|
2023-09-21 13:07:31 -05:00
|
|
|
const char *BitmapBegin, const char *BitmapEnd,
|
2016-05-14 20:12:42 +00:00
|
|
|
VPDataReaderType *VPDataReader, const char *NamesBegin,
|
Reland "[TypeProf][InstrPGO] Introduce raw and instr profile format change for type profiling." (#82711)
New change on top of [reviewed
patch](https://github.com/llvm/llvm-project/pull/81691) are [in commits
after this
one](https://github.com/llvm/llvm-project/pull/82711/commits/d0757f46b3e3865b5f7c552bc0744309a363e0ac).
Previous commits are restored from the remote branch with timestamps.
1. Fix build breakage for non-ELF platforms, by defining the missing
functions {`__llvm_profile_begin_vtables`, `__llvm_profile_end_vtables`,
`__llvm_profile_begin_vtabnames `, `__llvm_profile_end_vtabnames`}
everywhere.
* Tested on mac laptop (for darwins) and Windows. Specifically,
functions in `InstrProfilingPlatformWindows.c` returns `NULL` to make it
more explicit that type prof isn't supported; see comments for the
reason.
* For the rest (AIX, other), mostly follow existing examples (like this
[one](https://github.com/llvm/llvm-project/commit/f95b2f1acf1171abb0d00089fd4c9238753847e3))
2. Rename `__llvm_prf_vtabnames` -> `__llvm_prf_vns` for shorter section
name, and make returned pointers
[const](https://github.com/llvm/llvm-project/pull/82711/commits/a825d2a4ec00f07772a373091a702f149c3b0c34#diff-4de780ce726d76b7abc9d3353aef95013e7b21e7bda01be8940cc6574fb0b5ffR120-R121)
**Original Description**
* Raw profile format
- Header: records the byte size of compressed vtable names, and the
number of profiled vtable entries (call it `VTableProfData`). Header
also records padded bytes of each section.
- Payload: adds a section for compressed vtable names, and a section to
store `VTableProfData`. Both sections are padded so the size is a
multiple of 8.
* Indexed profile format
- Header: records the byte offset of compressed vtable names.
- Payload: adds a section to store compressed vtable names. This section
is used by `llvm-profdata` to show the list of vtables profiled for an
instrumented site.
[The originally reviewed
patch](https://github.com/llvm/llvm-project/pull/66825) will have
profile reader/write change and llvm-profdata change.
- To ensure this PR has all the necessary profile format change along
with profile version bump, created a copy of the originally reviewed
patch in https://github.com/llvm/llvm-project/pull/80761. The copy
doesn't have profile format change, but it has the set of tests which
covers type profile generation, profile read and profile merge. Tests
pass there.
rfc in
https://discourse.llvm.org/t/rfc-dynamic-type-profiling-and-optimizations-in-llvm/74600
---------
Co-authored-by: modiking <modiking213@gmail.com>
2024-02-27 11:07:40 -08:00
|
|
|
const char *NamesEnd, const VTableProfData *VTableBegin,
|
|
|
|
const VTableProfData *VTableEnd, const char *VNamesBegin,
|
2025-03-19 19:01:38 -05:00
|
|
|
const char *VNamesEnd, int SkipNameDataWrite,
|
|
|
|
uint64_t Version);
|
2016-05-10 00:17:31 +00:00
|
|
|
|
2016-03-03 18:54:46 +00:00
|
|
|
/* Merge value profile data pointed to by SrcValueProfData into
|
|
|
|
* in-memory profile counters pointed by to DstData. */
|
2016-03-06 04:18:13 +00:00
|
|
|
void lprofMergeValueProfData(struct ValueProfData *SrcValueProfData,
|
|
|
|
__llvm_profile_data *DstData);
|
2015-11-18 21:08:03 +00:00
|
|
|
|
2024-10-09 09:30:32 +03:00
|
|
|
VPDataReaderType *lprofGetVPDataReader(void);
|
2016-05-14 20:12:42 +00:00
|
|
|
|
2016-05-19 21:35:34 +00:00
|
|
|
/* Internal interface used by test to reset the max number of
|
|
|
|
* tracked values per value site to be \p MaxVals.
|
|
|
|
*/
|
|
|
|
void lprofSetMaxValsPerSite(uint32_t MaxVals);
|
2024-10-09 09:30:32 +03:00
|
|
|
void lprofSetupValueProfiler(void);
|
2016-05-18 22:34:05 +00:00
|
|
|
|
2016-06-08 23:43:56 +00:00
|
|
|
/* Return the profile header 'signature' value associated with the current
|
|
|
|
* executable or shared library. The signature value can be used to for
|
|
|
|
* a profile name that is unique to this load module so that it does not
|
|
|
|
* collide with profiles from other binaries. It also allows shared libraries
|
|
|
|
* to dump merged profile data into its own profile file. */
|
2024-10-09 09:30:32 +03:00
|
|
|
uint64_t lprofGetLoadModuleSignature(void);
|
2016-06-08 23:43:56 +00:00
|
|
|
|
2016-08-09 04:21:14 +00:00
|
|
|
/*
|
|
|
|
* Return non zero value if the profile data has already been
|
|
|
|
* dumped to the file.
|
|
|
|
*/
|
2020-03-24 20:02:44 -07:00
|
|
|
unsigned lprofProfileDumped(void);
|
|
|
|
void lprofSetProfileDumped(unsigned);
|
2016-08-09 04:21:14 +00:00
|
|
|
|
2016-05-09 19:01:19 +00:00
|
|
|
COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
|
2016-05-13 18:26:26 +00:00
|
|
|
COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;
|
|
|
|
COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;
|
2016-05-18 22:34:05 +00:00
|
|
|
COMPILER_RT_VISIBILITY extern uint32_t VPMaxNumValsPerSite;
|
2016-05-21 22:55:45 +00:00
|
|
|
/* Pointer to the start of static value counters to be allocted. */
|
|
|
|
COMPILER_RT_VISIBILITY extern ValueProfNode *CurrentVNode;
|
|
|
|
COMPILER_RT_VISIBILITY extern ValueProfNode *EndVNode;
|
2016-03-03 18:54:46 +00:00
|
|
|
extern void (*VPMergeHook)(struct ValueProfData *, __llvm_profile_data *);
|
2015-12-10 19:50:04 +00:00
|
|
|
|
2021-05-06 16:09:12 +00:00
|
|
|
/*
|
|
|
|
* Write binary ids into profiles if writer is given.
|
|
|
|
* Return -1 if an error occurs, otherwise, return total size of binary ids.
|
|
|
|
*/
|
|
|
|
int __llvm_write_binary_ids(ProfDataWriter *Writer);
|
|
|
|
|
2023-03-20 15:51:17 -04:00
|
|
|
/*
|
|
|
|
* Write binary id length and then its data, because binary id does not
|
|
|
|
* have a fixed length.
|
|
|
|
*/
|
|
|
|
int lprofWriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen,
|
|
|
|
const uint8_t *BinaryIdData,
|
|
|
|
uint64_t BinaryIdPadding);
|
|
|
|
|
2014-12-09 22:07:25 +00:00
|
|
|
#endif
|