Skip to content

[MC/DC][Coverage] Enable profile correlation for MC/DC #136437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions compiler-rt/lib/profile/InstrProfilingWriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ COMPILER_RT_VISIBILITY int lprofWriteDataImpl(
/* The data and names sections are omitted in lightweight mode. */
if (NumData == 0 && NamesSize == 0) {
Header.CountersDelta = 0;
Header.BitmapDelta = 0;
Header.NamesDelta = 0;
}

Expand Down
14 changes: 14 additions & 0 deletions compiler-rt/test/profile/Inputs/instrprof-mcdc-correlation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
void test(bool a, bool b, bool c, bool d) {
if ((a && b) || (c && d))
;
if (b && c)
;
}

int main() {
test(true, true, true, true);
test(true, true, false, true);
test(true, false, true, true);
(void)0;
return 0;
}
21 changes: 21 additions & 0 deletions compiler-rt/test/profile/instrprof-mcdc-correlation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// REQUIRES: linux || windows
// Default
// RUN: %clang -o %t.normal -fprofile-instr-generate -fcoverage-mapping -fcoverage-mcdc %S/Inputs/instrprof-mcdc-correlation.cpp
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t.normal
// RUN: llvm-profdata merge -o %t.normal.profdata %t.profraw
// RUN: llvm-profdata show --all-functions --counts --text %t.normal.profdata > %t.normal.profdata.show

// With -profile-correlate=binary flag
// RUN: %clang -o %t-1.exe -fprofile-instr-generate -fcoverage-mapping -fcoverage-mcdc -mllvm -profile-correlate=binary %S/Inputs/instrprof-mcdc-correlation.cpp
// RUN: env LLVM_PROFILE_FILE=%t-1.profraw %run %t-1.exe
// RUN: llvm-profdata merge -o %t-1.profdata --binary-file=%t-1.exe %t-1.profraw
// RUN: llvm-profdata show --all-functions --counts --text %t-1.profdata > %t-1.profdata.show

// With -profile-correlate=debug-info flag
// RUN: %clang -o %t-2.exe -fprofile-instr-generate -fcoverage-mapping -fcoverage-mcdc -mllvm -profile-correlate=debug-info -g %S/Inputs/instrprof-mcdc-correlation.cpp
// RUN: env LLVM_PROFILE_FILE=%t-2.profraw %run %t-2.exe
// RUN: llvm-profdata merge -o %t-2.profdata --debug-info=%t-2.exe %t-2.profraw
// RUN: llvm-profdata show --all-functions --counts --text %t-2.profdata > %t-2.profdata.show

// RUN: diff %t.normal.profdata.show %t-1.profdata.show
// RUN: diff %t.normal.profdata.show %t-2.profdata.show
16 changes: 12 additions & 4 deletions llvm/include/llvm/ProfileData/InstrProfCorrelator.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class InstrProfCorrelator {
LLVM_ABI static const char *FunctionNameAttributeName;
LLVM_ABI static const char *CFGHashAttributeName;
LLVM_ABI static const char *NumCountersAttributeName;
LLVM_ABI static const char *NumBitmapBitsAttributeName;

enum InstrProfCorrelatorKind { CK_32Bit, CK_64Bit };
InstrProfCorrelatorKind getKind() const { return Kind; }
Expand All @@ -83,6 +84,9 @@ class InstrProfCorrelator {
/// The address range of the __llvm_prf_cnts section.
uint64_t CountersSectionStart;
uint64_t CountersSectionEnd;
/// The address range of the __llvm_prf_bits section.
uint64_t BitmapSectionStart;
uint64_t BitmapSectionEnd;
/// The pointer points to start/end of profile data/name sections if
/// FileKind is Binary.
const char *DataStart;
Expand All @@ -105,7 +109,9 @@ class InstrProfCorrelator {
std::optional<std::string> LinkageName;
yaml::Hex64 CFGHash;
yaml::Hex64 CounterOffset;
yaml::Hex64 BitmapOffset;
uint32_t NumCounters;
uint32_t NumBitmapBytes;
std::optional<std::string> FilePath;
std::optional<int> LineNumber;
};
Expand Down Expand Up @@ -159,8 +165,9 @@ class InstrProfCorrelatorImpl : public InstrProfCorrelator {
Error dumpYaml(int MaxWarnings, raw_ostream &OS) override;

void addDataProbe(uint64_t FunctionName, uint64_t CFGHash,
IntPtrT CounterOffset, IntPtrT FunctionPtr,
uint32_t NumCounters);
IntPtrT CounterOffset, IntPtrT BitmapOffset,
IntPtrT FunctionPtr, uint32_t NumCounters,
uint32_t NumBitmapBytes);

// Byte-swap the value if necessary.
template <class T> T maybeSwap(T Value) const {
Expand All @@ -172,6 +179,7 @@ class InstrProfCorrelatorImpl : public InstrProfCorrelator {
std::unique_ptr<InstrProfCorrelator::Context> Ctx)
: InstrProfCorrelator(Kind, std::move(Ctx)){};
llvm::DenseSet<IntPtrT> CounterOffsets;
llvm::DenseSet<IntPtrT> BitmapOffsets;
};

/// DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes
Expand All @@ -191,8 +199,8 @@ class DwarfInstrProfCorrelator : public InstrProfCorrelatorImpl<IntPtrT> {
std::optional<uint64_t> getLocation(const DWARFDie &Die) const;

/// Returns true if the provided DIE symbolizes an instrumentation probe
/// symbol.
static bool isDIEOfProbe(const DWARFDie &Die);
/// symbol of the necessary type.
static bool isDIEOfProbe(const DWARFDie &Die, const StringRef &Prefix);

/// Iterate over DWARF DIEs to find those that symbolize instrumentation
/// probes and construct the ProfileData vector and Names string.
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/ProfileData/InstrProfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ class RawInstrProfReader : public InstrProfReader {
bool atEnd() const { return Data == DataEnd; }

void advanceData() {
// `CountersDelta` is a constant zero when using debug info correlation.
// `CountersDelta` and `BitmapDelta` are constant zero when using debug info
// correlation.
if (!Correlator && !BIDFetcherCorrelator) {
// The initial CountersDelta is the in-memory address difference between
// the data and counts sections:
Expand Down
Loading