Skip to content

Commit ab27578

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 c47042c commit ab27578

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
@@ -68,6 +68,7 @@ class InstrProfCorrelator {
6868
static const char *FunctionNameAttributeName;
6969
static const char *CFGHashAttributeName;
7070
static const char *NumCountersAttributeName;
71+
static const char *NumBitmapBitsAttributeName;
7172

7273
enum InstrProfCorrelatorKind { CK_32Bit, CK_64Bit };
7374
InstrProfCorrelatorKind getKind() const { return Kind; }
@@ -82,6 +83,9 @@ class InstrProfCorrelator {
8283
/// The address range of the __llvm_prf_cnts section.
8384
uint64_t CountersSectionStart;
8485
uint64_t CountersSectionEnd;
86+
/// The address range of the __llvm_prf_bits section.
87+
uint64_t BitmapSectionStart;
88+
uint64_t BitmapSectionEnd;
8589
/// The pointer points to start/end of profile data/name sections if
8690
/// FileKind is Binary.
8791
const char *DataStart;
@@ -104,7 +108,9 @@ class InstrProfCorrelator {
104108
std::optional<std::string> LinkageName;
105109
yaml::Hex64 CFGHash;
106110
yaml::Hex64 CounterOffset;
111+
yaml::Hex64 BitmapOffset;
107112
uint32_t NumCounters;
113+
uint32_t NumBitmapBytes;
108114
std::optional<std::string> FilePath;
109115
std::optional<int> LineNumber;
110116
};
@@ -158,8 +164,9 @@ class InstrProfCorrelatorImpl : public InstrProfCorrelator {
158164
Error dumpYaml(int MaxWarnings, raw_ostream &OS) override;
159165

160166
void addDataProbe(uint64_t FunctionName, uint64_t CFGHash,
161-
IntPtrT CounterOffset, IntPtrT FunctionPtr,
162-
uint32_t NumCounters);
167+
IntPtrT CounterOffset, IntPtrT BitmapOffset,
168+
IntPtrT FunctionPtr, uint32_t NumCounters,
169+
uint32_t NumBitmapBytes);
163170

164171
// Byte-swap the value if necessary.
165172
template <class T> T maybeSwap(T Value) const {
@@ -171,6 +178,7 @@ class InstrProfCorrelatorImpl : public InstrProfCorrelator {
171178
std::unique_ptr<InstrProfCorrelator::Context> Ctx)
172179
: InstrProfCorrelator(Kind, std::move(Ctx)){};
173180
llvm::DenseSet<IntPtrT> CounterOffsets;
181+
llvm::DenseSet<IntPtrT> BitmapOffsets;
174182
};
175183

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

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

196204
/// Iterate over DWARF DIEs to find those that symbolize instrumentation
197205
/// 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
@@ -470,7 +470,8 @@ class RawInstrProfReader : public InstrProfReader {
470470
bool atEnd() const { return Data == DataEnd; }
471471

472472
void advanceData() {
473-
// `CountersDelta` is a constant zero when using debug info correlation.
473+
// `CountersDelta` and `BitmapDelta` are constant zero when using debug info
474+
// correlation.
474475
if (!Correlator && !BIDFetcherCorrelator) {
475476
// The initial CountersDelta is the in-memory address difference between
476477
// the data and counts sections:

0 commit comments

Comments
 (0)