Use the llvm flag `-pgo-function-entry-coverage` to create single byte "counters" to track functions coverage. This mode has significantly less size overhead in both code and data because * We mark a function as "covered" with a store instead of an increment which generally requires fewer assembly instructions * We use a single byte per function rather than 8 bytes per block The trade off of course is that this mode only tells you if a function has been covered. This is useful, for example, to detect dead code. When combined with debug info correlation [0] we are able to create an instrumented Clang binary that is only 150M (the vanilla Clang binary is 143M). That is an overhead of 7M (4.9%) compared to the default instrumentation (without value profiling) which has an overhead of 31M (21.7%). [0] https://groups.google.com/g/llvm-dev/c/r03Z6JoN7d4 Reviewed By: kyulee Differential Revision: https://reviews.llvm.org/D116180
77 lines
2.4 KiB
C
77 lines
2.4 KiB
C
/*===- InstrProfiling.c - Support library for PGO instrumentation ---------===*\
|
|
|*
|
|
|* 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
|
|
|*
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
// Note: This is linked into the Darwin kernel, and must remain compatible
|
|
// with freestanding compilation. See `darwin_add_builtin_libraries`.
|
|
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "InstrProfiling.h"
|
|
#include "InstrProfilingInternal.h"
|
|
|
|
#define INSTR_PROF_VALUE_PROF_DATA
|
|
#include "profile/InstrProfData.inc"
|
|
|
|
COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_magic(void) {
|
|
return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64)
|
|
: (INSTR_PROF_RAW_MAGIC_32);
|
|
}
|
|
|
|
COMPILER_RT_VISIBILITY void __llvm_profile_set_dumped() {
|
|
lprofSetProfileDumped(1);
|
|
}
|
|
|
|
/* Return the number of bytes needed to add to SizeInBytes to make it
|
|
* the result a multiple of 8.
|
|
*/
|
|
COMPILER_RT_VISIBILITY uint8_t
|
|
__llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {
|
|
return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
|
|
}
|
|
|
|
COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_version(void) {
|
|
return INSTR_PROF_RAW_VERSION_VAR;
|
|
}
|
|
|
|
COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {
|
|
char *I = __llvm_profile_begin_counters();
|
|
char *E = __llvm_profile_end_counters();
|
|
|
|
char ResetValue =
|
|
(__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) ? 0xFF : 0;
|
|
memset(I, ResetValue, E - I);
|
|
|
|
const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
|
|
const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
|
|
const __llvm_profile_data *DI;
|
|
for (DI = DataBegin; DI < DataEnd; ++DI) {
|
|
uint64_t CurrentVSiteCount = 0;
|
|
uint32_t VKI, i;
|
|
if (!DI->Values)
|
|
continue;
|
|
|
|
ValueProfNode **ValueCounters = (ValueProfNode **)DI->Values;
|
|
|
|
for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
|
|
CurrentVSiteCount += DI->NumValueSites[VKI];
|
|
|
|
for (i = 0; i < CurrentVSiteCount; ++i) {
|
|
ValueProfNode *CurrentVNode = ValueCounters[i];
|
|
|
|
while (CurrentVNode) {
|
|
CurrentVNode->Count = 0;
|
|
CurrentVNode = CurrentVNode->Next;
|
|
}
|
|
}
|
|
}
|
|
lprofSetProfileDumped(0);
|
|
}
|