8000 Update to profraw format 11 by xd009642 · Pull Request #36 · Amanieu/minicov · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

- Upgraded the bundled LLVM runtime to raw profile format version 11.

## v0.3.8 - 2025-12-05

- Fixed broken profiling on WASM. (#32)
Expand Down
20 changes: 19 additions & 1 deletion minicov-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ fn bar() {
println!("b");
}

#[inline(never)]
fn call_indirect(f: fn()) {
f();
}

fn do_stuff(x: bool) {
if x {
foo()
Expand All @@ -17,9 +22,22 @@ fn main() {
println!("built with coverage: {}", minicov::coverage_enabled());

do_stuff(false);
let f: fn() = if std::hint::black_box(false) {
foo
} else {
bar
};
call_indirect(std::hint::black_box(f));

let mut coverage = vec![];
unsafe {
minicov::capture_coverage(&mut coverage).unwrap();
minicov::reset_coverage();
minicov::merge_coverage(&coverage).unwrap();
}
let mut merged_coverage = vec![];
unsafe {
minicov::capture_coverage(&mut merged_coverage).unwrap();
}
std::fs::write("output.profraw", coverage).unwrap();
std::fs::write("output.profraw", merged_coverage).unwrap();
}
11 changes: 5 additions & 6 deletions minicov/build.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
use cc::Build;
use std::env;
use walkdir::WalkDir;

fn main() {
let mut cfg = Build::new();
cfg.compiler("clang");
cfg.flag("-nostdlibinc");
cfg.flag("-fno-stack-protector");
cfg.flag("-fno-profile-generate");
cfg.flag("-fno-profile-use");
cfg.flag("-fno-profile-instr-generate");
cfg.flag("-fno-profile-instr-use");
cfg.flag("-fno-coverage-mapping");
cfg.define("COMPILER_RT_HAS_ATOMICS", "1");
cfg.define("COMPILER_RT_PROFILE_BAREMETAL", "1");

let sources = vec![
"c/InstrProfiling.c",
"c/InstrProfilingBuffer.c",
"c/InstrProfilingInternal.c",
"c/InstrProfilingMerge.c",
"c/InstrProfilingMergeFile.c",
"c/InstrProfilingPlatformLinux.c",
"c/InstrProfilingPlatformWindows.c",
"c/InstrProfilingWriter.c",
"c/InstrProfilingValue.c",
"c/InstrProfilingVersionVar.c",
];

let target = env::var("TARGET").unwrap_or_default();
if target.ends_with("-uefi") {
cfg.define("MINICOV_UEFI", "1");
}

for source in &sources {
cfg.file(source);
}
Expand Down
86 changes: 70 additions & 16 deletions minicov/c/InstrProfiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#define PROFILE_INSTRPROFILING_H_

#include "InstrProfilingPort.h"
#include <stddef.h>
#ifndef COMPILER_RT_PROFILE_BAREMETAL
#include <stdio.h>
#endif

// Make sure __LLVM_INSTR_PROFILE_GENERATE is always defined before
// including instr_prof_interface.h so the interface functions are
Expand Down Expand Up @@ -53,6 +57,17 @@ typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT) VTableProfData {
#include "profile/InstrProfData.inc"
} VTableProfData;

typedef struct __llvm_profile_gpu_sections {
#define INSTR_PROF_GPU_SECT(Type, LLVMType, Name, Initializer) Type Name;
#include "profile/InstrProfData.inc"
} __llvm_profile_gpu_sections;

typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
__llvm_gcov_init_func_struct {
#define COVINIT_FUNC(Type, LLVMType, Name, Initializer) Type Name;
#include "profile/InstrProfData.inc"
} __llvm_gcov_init_func_struct;

/*!
* \brief Return 1 if profile counters are continuously synced to the raw
* profile via an mmap(). This is in contrast to the default mode, in which
Expand Down Expand Up @@ -113,11 +128,10 @@ char *__llvm_profile_begin_counters(void);
char *__llvm_profile_end_counters(void);
char *__llvm_profile_begin_bitmap(void);
char *__llvm_profile_end_bitmap(void);
ValueProfNode *__llvm_profile_begin_vnodes();
ValueProfNode *__llvm_profile_end_vnodes();
const VTableProfData *__llvm_profile_begin_vtables();
const VTableProfData *__llvm_profile_end_vtables();
uint32_t *__llvm_profile_begin_orderfile();
ValueProfNode *__llvm_profile_begin_vnodes(void);
ValueProfNode *__llvm_profile_end_vnodes(void);
const VTableProfData *__llvm_profile_begin_vtables(void);
const VTableProfData *__llvm_profile_end_vtables(void);

/*!
* \brief Merge profile data from buffer.
Expand Down Expand Up @@ -157,6 +171,16 @@ void __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data,
uint32_t CounterIndex,
uint64_t CounterValue);

/*!
* \brief Wave-cooperative counter increment for GPU targets.
*
* Reduces per-lane atomic contention by electing a single lane per wave to
* perform the counter update. \c Uniform is an optional counter tracking the
* number of uniform.
*/
void INSTR_PROF_INSTRUMENT_GPU_FUNC(uint64_t *Counter, uint64_t *Uniform,
uint64_t Step);

/*!
* \brief Write instrumentation data to the current file.
*
Expand All @@ -168,8 +192,6 @@ void __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data,
*/
int __llvm_profile_write_file(void);

int __llvm_orderfile_write_file(void);

/*!
* \brief Set the FILE object for writing instrumentation data. Return 0 if set
* successfully or return 1 if failed.
Expand All @@ -196,7 +218,9 @@ int __llvm_orderfile_write_file(void);
* copying the old profile file to new profile file and this function is usually
* used when the proess doesn't have permission to open file.
*/
//int __llvm_profile_set_file_object(FILE *File, int EnableMerge);
#ifndef COMPILER_RT_PROFILE_BAREMETAL
int __llvm_profile_set_file_object(FILE *File, int EnableMerge);
#endif

/*! \brief Register to write instrumentation data to file at exit. */
int __llvm_profile_register_write_file_atexit(void);
Expand All @@ -207,6 +231,9 @@ void __llvm_profile_initialize_file(void);
/*! \brief Initialize the profile runtime. */
void __llvm_profile_initialize(void);

/*! \brief Initialize the gcov profile runtime. */
void __llvm_profile_gcov_initialize(void);

/*!
* \brief Return path prefix (excluding the base filename) of the profile data.
* This is useful for users using \c -fprofile-generate=./path_prefix who do
Expand All @@ -215,7 +242,7 @@ void __llvm_profile_initialize(void);
* merge mode is turned on for instrumented programs with shared libs).
* Side-effect: this API call will invoke malloc with dynamic memory allocation.
*/
const char *__llvm_profile_get_path_prefix();
const char *__llvm_profile_get_path_prefix(void);

/*!
* \brief Return filename (including path) of the profile data. Note that if the
Expand All @@ -228,7 +255,7 @@ const char *__llvm_profile_get_path_prefix();
* instrumented image/DSO). This API only retrieves the filename from the copy
* of the runtime available to the calling image.
*/
const char *__llvm_profile_get_filename();
const char *__llvm_profile_get_filename(void);

/*! \brief Get the magic token for the file format. */
uint64_t __llvm_profile_get_magic(void);
Expand Down Expand Up @@ -270,7 +297,7 @@ uint64_t __llvm_profile_get_vtable_section_size(const VTableProfData *Begin,

/* ! \brief Given the sizes of the data and counter information, computes the
* number of padding bytes before and after the counter section, as well as the
* number of padding bytes after other setions in the raw profile.
* number of padding bytes after other sections in the raw profile.
* Returns -1 upon errors and 0 upon success. Output parameters should be used
* iff return value is 0.
*
Expand All @@ -281,18 +308,42 @@ uint64_t __llvm_profile_get_vtable_section_size(const VTableProfData *Begin,
*/
int __llvm_profile_get_padding_sizes_for_counters(
uint64_t DataSize, uint64_t CountersSize, uint64_t NumBitmapBytes,
uint64_t NamesSize, uint64_t VTableSize, uint64_t VNameSize,
uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,
uint64_t *PaddingBytesAfterBitmap, uint64_t *PaddingBytesAfterNames,
uint64_t *PaddingBytesAfterVTable, uint64_t *PaddingBytesAfterVNames);
uint64_t NumUniformCounters, uint64_t NamesSize, uint64_t VTableSize,
uint64_t VNameSize, uint64_t *PaddingBytesBeforeCounters,
uint64_t *PaddingBytesAfterCounters, uint64_t *PaddingBytesAfterBitmap,
uint64_t *PaddingBytesAfterUniformCounters,
uint64_t *PaddingBytesAfterNames, uint64_t *PaddingBytesAfterVTable,
uint64_t *PaddingBytesAfterVNames);

/*!
* \brief Set the flag that profile data has been dumped to the file.
* This is useful for users to disable dumping profile data to the file for
* certain processes in case the processes don't have permission to write to
* the disks, and trying to do so would result in side effects such as crashes.
*/
void __llvm_profile_set_dumped();
void __llvm_profile_set_dumped(void);

/*!
* \brief Write custom target-specific profiling data to a separate file.
* Used by offload PGO (HIP and OpenMP).
*
* \param Target Target triple (e.g., "amdgcn-amd-amdhsa")
* \param DataBegin Start of profile data records
* \param DataEnd End of profile data records
* \param CountersBegin Start of counter data
* \param CountersEnd End of counter data
* \param UniformCountersBegin Start of uniform counters (NULL if not used)
* \param UniformCountersEnd End of uniform counters (NULL if not used)
* \param NamesBegin Start of names data
* \param NamesEnd End of names data
* \param VersionOverride Profile version override (NULL to use default)
*/
int __llvm_write_custom_profile(
const char *Target, const __llvm_profile_data *DataBegin,
const __llvm_profile_data *DataEnd, const char *CountersBegin,
const char *CountersEnd, const char *UniformCountersBegin,
const char *UniformCountersEnd, const char *NamesBegin,
const char *NamesEnd, const uint64_t *VersionOverride);

/*!
* This variable is defined in InstrProfilingRuntime.cpp as a hidden
Expand Down Expand Up @@ -323,4 +374,7 @@ COMPILER_RT_VISIBILITY extern uint64_t
*/
extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */

const __llvm_gcov_init_func_struct *__llvm_profile_begin_covinit();
const __llvm_gcov_init_func_struct *__llvm_profile_end_covinit();

#endif /* PROFILE_INSTRPROFILING_H_ */
37 changes: 24 additions & 13 deletions minicov/c/InstrProfilingBuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,22 @@ static int needsCounterPadding(void) {
COMPILER_RT_VISIBILITY
int __llvm_profile_get_padding_sizes_for_counters(
uint64_t DataSize, uint64_t CountersSize, uint64_t NumBitmapBytes,
uint64_t NamesSize, uint64_t VTableSize, uint64_t VNameSize,
uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,
uint64_t *PaddingBytesAfterBitmapBytes, uint64_t *PaddingBytesAfterNames,
uint64_t *PaddingBytesAfterVTable, uint64_t *PaddingBytesAfterVName) {
uint64_t NumUniformCounters, uint64_t NamesSize, uint64_t VTableSize,
uint64_t VNameSize, uint64_t *PaddingBytesBeforeCounters,
uint64_t *PaddingBytesAfterCounters, uint64_t *PaddingBytesAfterBitmapBytes,
uint64_t *PaddingBytesAfterUniformCounters,
uint64_t *PaddingBytesAfterNames, uint64_t *PaddingBytesAfterVTable,
uint64_t *PaddingBytesAfterVName) {
// Counter padding is needed only if continuous mode is enabled.
if (!needsCounterPadding()) {
*PaddingBytesBeforeCounters = 0;
*PaddingBytesAfterCounters =
__llvm_profile_get_num_padding_bytes(CountersSize);
*PaddingBytesAfterBitmapBytes =
__llvm_profile_get_num_padding_bytes(NumBitmapBytes);
if (PaddingBytesAfterUniformCounters != NULL)
*PaddingBytesAfterUniformCounters = __llvm_profile_get_num_padding_bytes(
NumUniformCounters * sizeof(uint64_t));
*PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize);
if (PaddingBytesAfterVTable != NULL)
*PaddingBytesAfterVTable =
Expand All @@ -179,6 +184,8 @@ int __llvm_profile_get_padding_sizes_for_counters(
*PaddingBytesAfterCounters = calculateBytesNeededToPageAlign(CountersSize);
*PaddingBytesAfterBitmapBytes =
calculateBytesNeededToPageAlign(NumBitmapBytes);
if (PaddingBytesAfterUniformCounters != NULL)
*PaddingBytesAfterUniformCounters = 0;
*PaddingBytesAfterNames = calculateBytesNeededToPageAlign(NamesSize);
// Set these two variables to zero to avoid uninitialized variables
// even if VTableSize and VNameSize are known to be zero.
Expand Down Expand Up @@ -212,20 +219,22 @@ uint64_t __llvm_profile_get_size_for_buffer_internal(
* the names. */
uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
PaddingBytesAfterNames, PaddingBytesAfterBitmapBytes,
PaddingBytesAfterVTable, PaddingBytesAfterVNames;
PaddingBytesAfterUniformCounters, PaddingBytesAfterVTable,
PaddingBytesAfterVNames;
__llvm_profile_get_padding_sizes_for_counters(
DataSize, CountersSize, NumBitmapBytes, NamesSize, 0 /* VTableSize */,
0 /* VNameSize */, &PaddingBytesBeforeCounters,
&PaddingBytesAfterCounters, &PaddingBytesAfterBitmapBytes,
DataSize, CountersSize, NumBitmapBytes, 0 /* NumUniformCounters */,
NamesSize, 0 /* VTableSize */, 0 /* VNameSize */,
&PaddingBytesBeforeCounters, &PaddingBytesAfterCounters,
&PaddingBytesAfterBitmapBytes, &PaddingBytesAfterUniformCounters,
&PaddingBytesAfterNames, &PaddingBytesAfterVTable,
&PaddingBytesAfterVNames);

return sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) +
DataSize + PaddingBytesBeforeCounters + CountersSize +
PaddingBytesAfterCounters + NumBitmapBytes +
PaddingBytesAfterBitmapBytes + NamesSize + PaddingBytesAfterNames +
VTableSize + PaddingBytesAfterVTable + VNameSize +
PaddingBytesAfterVNames;
PaddingBytesAfterBitmapBytes + PaddingBytesAfterUniformCounters +
NamesSize + PaddingBytesAfterNames + VTableSize +
PaddingBytesAfterVTable + VNameSize + PaddingBytesAfterVNames;
}

COMPILER_RT_VISIBILITY
Expand All @@ -250,7 +259,9 @@ COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
// Set virtual table arguments to NULL since they are not supported yet.
return lprofWriteDataImpl(
&BufferWriter, DataBegin, DataEnd, CountersBegin, CountersEnd,
BitmapBegin, BitmapEnd, /*VPDataReader=*/0, NamesBegin, NamesEnd,
BitmapBegin, BitmapEnd, /*UniformCountersBegin=*/NULL,
/*UniformCountersEnd=*/NULL, /*VPDataReader=*/0, NamesBegin, NamesEnd,
/*VTableBegin=*/NULL, /*VTableEnd=*/NULL, /*VNamesBegin=*/NULL,
/*VNamesEnd=*/NULL, /*SkipNameDataWrite=*/0);
/*VNamesEnd=*/NULL, /*SkipNameDataWrite=*/0,
__llvm_profile_get_version());
}
26 changes: 13 additions & 13 deletions minicov/c/InstrProfilingInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ typedef struct VPDataReaderType {
uint8_t *SiteCountArray[]);
/* Function pointer to getValueProfRecordHeader method. */
uint32_t (*GetValueProfRecordHeaderSize)(uint32_t NumSites);
/* Function pointer to getFristValueProfRecord method. */
/* Function pointer to getFirstValueProfRecord method. */
struct ValueProfRecord *(*GetFirstValueProfRecord)(struct ValueProfData *);
/* Return the number of value data for site \p Site. */
uint32_t (*GetNumValueDataForSite)(uint32_t VK, uint32_t Site);
Expand All @@ -152,35 +152,35 @@ typedef struct VPDataReaderType {
the name data is already in destination, we just skip over it. */
int lprofWriteData(ProfDataWriter *Writer, VPDataReaderType *VPDataReader,
int SkipNameDataWrite);
int lprofWriteDataImpl(ProfDataWriter *Writer,
const __llvm_profile_data *DataBegin,
const __llvm_profile_data *DataEnd,
const char *CountersBegin, const char *CountersEnd,
const char *BitmapBegin, const char *BitmapEnd,
VPDataReaderType *VPDataReader, const char *NamesBegin,
const char *NamesEnd, const VTableProfData *VTableBegin,
const VTableProfData *VTableEnd, const char *VNamesBegin,
const char *VNamesEnd, int SkipNameDataWrite);
int lprofWriteDataImpl(
ProfDataWriter *Writer, const __llvm_profile_data *DataBegin,
const __llvm_profile_data *DataEnd, const char *CountersBegin,
const char *CountersEnd, const char *BitmapBegin, const char *BitmapEnd,
const char *UniformCountersBegin, const char *UniformCountersEnd,
VPDataReaderType *VPDataReader, const char *NamesBegin,
const char *NamesEnd, const VTableProfData *VTableBegin,
const VTableProfData *VTableEnd, const char *VNamesBegin,
const char *VNamesEnd, int SkipNameDataWrite, uint64_t Version);

/* Merge value profile data pointed to by SrcValueProfData into
* in-memory profile counters pointed by to DstData. */
void lprofMergeValueProfData(struct ValueProfData *SrcValueProfData,
__llvm_profile_data *DstData);

VPDataReaderType *lprofGetVPDataReader();
VPDataReaderType *lprofGetVPDataReader(void);

/* 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);
void lprofSetupValueProfiler();
void lprofSetupValueProfiler(void);

/* 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. */
uint64_t lprofGetLoadModuleSignature();
uint64_t lprofGetLoadModuleSignature(void);

/*
* Return non zero value if the profile data has already been
Expand Down
Loading
Loading
0