Skip to content

Commit 3135a8b

Browse files
committed
[MC/DC][Coverage] Enable profile correlation for MC/DC
When using the `-fcoverage-mcdc` option in profile correlation mode MC/DC coverage is not actually collected. In binary profile correlation mode this completes elements of a vector of per-function profile data structures called `Data` with BitmapPtr and NumBitmapBytes values that are taken from profile data section in the same way as it is done for Counters. In debug info correlation mode this adds new `Profile Bitmap Type` DIEs to DWARFContext. These entries contain FunctionName and NumBitmapBits for functions. They are used by `correlateProfileDataImpl()` function to obtain BitmapPtr and NumBitmapBytes values to complete the corresponding elements of the vector of per-function profile data structures called `Data`. Creating and reading these new DIEs occur in the same way as it is done for DIEs of the type `Profile Data Type`. Fixes #97385
1 parent 7138397 commit 3135a8b

File tree

8 files changed

+304
-96
lines changed

8 files changed

+304
-96
lines changed

compiler-rt/lib/profile/InstrProfilingWriter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ COMPILER_RT_VISIBILITY int lprofWriteDataImpl(
320320
/* The data and names sections are omitted in lightweight mode. */
321321
if (NumData == 0 && NamesSize == 0) {
322322
Header.CountersDelta = 0;
323+
Header.BitmapDelta = 0;
323324
Header.NamesDelta = 0;
324325
}
325326

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
void test(bool a, bool b, bool c, bool d) {
2+
if ((a && b) || (c && d))
3+
;
4+
if (b && c)
5+
;
6+
}
7+
8+
int main() {
9+
test(true, true, true, true);
10+
test(true, true, false, true);
11+
test(true, false, true, true);
12+
(void)0;
13+
return 0;
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// REQUIRES: linux || windows
2+
// Default
3+
// RUN: %clang -o %t.normal -fprofile-instr-generate -fcoverage-mapping -fcoverage-mcdc %S/Inputs/instrprof-mcdc-correlation.cpp
4+
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t.normal
5+
// RUN: llvm-profdata merge -o %t.normal.profdata %t.profraw
6+
// RUN: llvm-profdata show --all-functions --counts --text %t.normal.profdata > %t.normal.profdata.show
7+
8+
// With -profile-correlate=binary flag
9+
// RUN: %clang -o %t-1.exe -fprofile-instr-generate -fcoverage-mapping -fcoverage-mcdc -mllvm -profile-correlate=binary %S/Inputs/instrprof-mcdc-correlation.cpp
10+
// RUN: env LLVM_PROFILE_FILE=%t-1.profraw %run %t-1.exe
11+
// RUN: llvm-profdata merge -o %t-1.profdata --binary-file=%t-1.exe %t-1.profraw
12+
// RUN: llvm-profdata show --all-functions --counts --text %t-1.profdata > %t-1.profdata.show
13+
14+
// With -profile-correlate=debug-info flag
15+
// 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
16+
// RUN: env LLVM_PROFILE_FILE=%t-2.profraw %run %t-2.exe
17+
// RUN: llvm-profdata merge -o %t-2.profdata --debug-info=%t-2.exe %t-2.profraw
18+
// RUN: llvm-profdata show --all-functions --counts --text %t-2.profdata > %t-2.profdata.show
19+
20+
// RUN: diff %t.normal.profdata.show %t-1.profdata.show
21+
// RUN: diff %t.normal.profdata.show %t-2.profdata.show

llvm/include/llvm/ProfileData/InstrProfCorrelator.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class InstrProfCorrelator {
6969
LLVM_ABI static const char *FunctionNameAttributeName;
7070
LLVM_ABI static const char *CFGHashAttributeName;
7171
LLVM_ABI static const char *NumCountersAttributeName;
72+
LLVM_ABI static const char *NumBitmapBitsAttributeName;
7273

7374
enum InstrProfCorrelatorKind { CK_32Bit, CK_64Bit };
7475
InstrProfCorrelatorKind getKind() const { return Kind; }
@@ -83,6 +84,9 @@ class InstrProfCorrelator {
8384
/// The address range of the __llvm_prf_cnts section.
8485
uint64_t CountersSectionStart;
8586
uint64_t CountersSectionEnd;
87+
/// The address range of the __llvm_prf_bits section.
88+
uint64_t BitmapSectionStart;
89+
uint64_t BitmapSectionEnd;
8690
/// The pointer points to start/end of profile data/name sections if
8791
/// FileKind is Binary.
8892
const char *DataStart;
@@ -105,7 +109,9 @@ class InstrProfCorrelator {
105109
std::optional<std::string> LinkageName;
106110
yaml::Hex64 CFGHash;
107111
yaml::Hex64 CounterOffset;
112+
yaml::Hex64 BitmapOffset;
108113
uint32_t NumCounters;
114+
uint32_t NumBitmapBytes;
109115
std::optional<std::string> FilePath;
110116
std::optional<int> LineNumber;
111117
};
@@ -159,8 +165,9 @@ class InstrProfCorrelatorImpl : public InstrProfCorrelator {
159165
Error dumpYaml(int MaxWarnings, raw_ostream &OS) override;
160166

161167
void addDataProbe(uint64_t FunctionName, uint64_t CFGHash,
162-
IntPtrT CounterOffset, IntPtrT FunctionPtr,
163-
uint32_t NumCounters);
168+
IntPtrT CounterOffset, IntPtrT BitmapOffset,
169+
IntPtrT FunctionPtr, uint32_t NumCounters,
170+
uint32_t NumBitmapBytes);
164171

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

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

193201
/// Returns true if the provided DIE symbolizes an instrumentation probe
194-
/// symbol.
195-
static bool isDIEOfProbe(const DWARFDie &Die);
202+
/// symbol of the necessary type.
203+
static bool isDIEOfProbe(const DWARFDie &Die, const StringRef &Prefix);
196204

197205
/// Iterate over DWARF DIEs to find those that symbolize instrumentation
198206
/// probes and construct the ProfileData vector and Names string.

llvm/include/llvm/ProfileData/InstrProfReader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ class RawInstrProfReader : public InstrProfReader {
473473
bool atEnd() const { return Data == DataEnd; }
474474

475475
void advanceData() {
476-
// `CountersDelta` is a constant zero when using debug info correlation.
476+
// `CountersDelta` and `BitmapDelta` are constant zero when using debug info
477+
// correlation.
477478
if (!Correlator && !BIDFetcherCorrelator) {
478479
// The initial CountersDelta is the in-memory address difference between
479480
// the data and counts sections:

0 commit comments

Comments
 (0)