Skip to content

Commit 7ead2d8

Browse files
authored
[MC/DC][Coverage] Loosen the limit of NumConds from 6 (#82448)
By storing possible test vectors instead of combinations of conditions, the restriction is dramatically relaxed. This introduces two options to `cc1`: * `-fmcdc-max-conditions=32767` * `-fmcdc-max-test-vectors=2147483646` This change makes coverage mapping, profraw, and profdata incompatible with Clang-18. - Bitmap semantics changed. It is incompatible with previous format. - `BitmapIdx` in `Decision` points to the end of the bitmap. - Bitmap is packed per function. - `llvm-cov` can understand `profdata` generated by `llvm-profdata-18`. RFC: https://discourse.llvm.org/t/rfc-coverage-new-algorithm-and-file-format-for-mc-dc/76798
1 parent 933d6be commit 7ead2d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+529
-297
lines changed

clang/docs/SourceBasedCodeCoverage.rst

+25-4
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,31 @@ MC/DC Instrumentation
484484
---------------------
485485

486486
When instrumenting for Modified Condition/Decision Coverage (MC/DC) using the
487-
clang option ``-fcoverage-mcdc``, users are limited to at most **six** leaf-level
488-
conditions in a boolean expression. A warning will be generated for boolean
489-
expressions that contain more than six, and they will not be instrumented for
490-
MC/DC.
487+
clang option ``-fcoverage-mcdc``, there are two hard limits.
488+
489+
The maximum number of terms is limited to 32767, which is practical for
490+
handwritten expressions. To be more restrictive in order to enforce coding rules,
491+
use ``-Xclang -fmcdc-max-conditions=n``. Expressions with exceeded condition
492+
counts ``n`` will generate warnings and will be excluded in the MC/DC coverage.
493+
494+
The number of test vectors (the maximum number of possible combinations of
495+
expressions) is limited to 2,147,483,646. In this case, approximately
496+
256MiB (==2GiB/8) is used to record test vectors.
497+
498+
To reduce memory usage, users can limit the maximum number of test vectors per
499+
expression with ``-Xclang -fmcdc-max-test-vectors=m``.
500+
If the number of test vectors resulting from the analysis of an expression
501+
exceeds ``m``, a warning will be issued and the expression will be excluded
502+
from the MC/DC coverage.
503+
504+
The number of test vectors ``m``, for ``n`` terms in an expression, can be
505+
``m <= 2^n`` in the theoretical worst case, but is usually much smaller.
506+
In simple cases, such as expressions consisting of a sequence of single
507+
operators, ``m == n+1``. For example, ``(a && b && c && d && e && f && g)``
508+
requires 8 test vectors.
509+
510+
Expressions such as ``((a0 && b0) || (a1 && b1) || ...)`` can cause the
511+
number of test vectors to increase exponentially.
491512

492513
Also, if a boolean expression is embedded in the nest of another boolean
493514
expression but separated by a non-logical operator, this is also not supported.

clang/include/clang/Basic/CodeGenOptions.def

+2
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ CODEGENOPT(CoverageMapping , 1, 0) ///< Generate coverage mapping regions to
223223
CODEGENOPT(DumpCoverageMapping , 1, 0) ///< Dump the generated coverage mapping
224224
///< regions.
225225
CODEGENOPT(MCDCCoverage , 1, 0) ///< Enable MC/DC code coverage criteria.
226+
VALUE_CODEGENOPT(MCDCMaxConds, 16, 32767) ///< MC/DC Maximum conditions.
227+
VALUE_CODEGENOPT(MCDCMaxTVs, 32, 0x7FFFFFFE) ///< MC/DC Maximum test vectors.
226228

227229
/// If -fpcc-struct-return or -freg-struct-return is specified.
228230
ENUM_CODEGENOPT(StructReturnConvention, StructReturnConventionKind, 2, SRCK_Default)

clang/include/clang/Driver/Options.td

+8
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,14 @@ defm mcdc_coverage : BoolFOption<"coverage-mcdc",
17901790
"Enable MC/DC criteria when generating code coverage">,
17911791
NegFlag<SetFalse, [], [ClangOption], "Disable MC/DC coverage criteria">,
17921792
BothFlags<[], [ClangOption, CLOption]>>;
1793+
def fmcdc_max_conditions_EQ : Joined<["-"], "fmcdc-max-conditions=">,
1794+
Group<f_Group>, Visibility<[CC1Option]>,
1795+
HelpText<"Maximum number of conditions in MC/DC coverage">,
1796+
MarshallingInfoInt<CodeGenOpts<"MCDCMaxConds">, "32767">;
1797+
def fmcdc_max_test_vectors_EQ : Joined<["-"], "fmcdc-max-test-vectors=">,
1798+
Group<f_Group>, Visibility<[CC1Option]>,
1799+
HelpText<"Maximum number of test vectors in MC/DC coverage">,
1800+
MarshallingInfoInt<CodeGenOpts<"MCDCMaxTVs">, "0x7FFFFFFE">;
17931801
def fprofile_generate : Flag<["-"], "fprofile-generate">,
17941802
Group<f_Group>, Visibility<[ClangOption, CLOption]>,
17951803
HelpText<"Generate instrumented code to collect execution counts into default.profraw (overridden by LLVM_PROFILE_FILE env var)">;

clang/lib/CodeGen/CodeGenPGO.cpp

+27-23
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
167167
PGOHash Hash;
168168
/// The map of statements to counters.
169169
llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
170-
/// The next bitmap byte index to assign.
171-
unsigned NextMCDCBitmapIdx;
172170
/// The state of MC/DC Coverage in this function.
173171
MCDC::State &MCDCState;
174172
/// Maximum number of supported MC/DC conditions in a boolean expression.
@@ -183,7 +181,7 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
183181
MCDC::State &MCDCState, unsigned MCDCMaxCond,
184182
DiagnosticsEngine &Diag)
185183
: NextCounter(0), Hash(HashVersion), CounterMap(CounterMap),
186-
NextMCDCBitmapIdx(0), MCDCState(MCDCState), MCDCMaxCond(MCDCMaxCond),
184+
MCDCState(MCDCState), MCDCMaxCond(MCDCMaxCond),
187185
ProfileVersion(ProfileVersion), Diag(Diag) {}
188186

189187
// Blocks and lambdas are handled as separate functions, so we need not
@@ -314,11 +312,8 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
314312
return true;
315313
}
316314

317-
// Otherwise, allocate the number of bytes required for the bitmap
318-
// based on the number of conditions. Must be at least 1-byte long.
319-
MCDCState.DecisionByStmt[BinOp].BitmapIdx = NextMCDCBitmapIdx;
320-
unsigned SizeInBits = std::max<unsigned>(1L << NumCond, CHAR_BIT);
321-
NextMCDCBitmapIdx += SizeInBits / CHAR_BIT;
315+
// Otherwise, allocate the Decision.
316+
MCDCState.DecisionByStmt[BinOp].BitmapIdx = 0;
322317
}
323318
return true;
324319
}
@@ -1083,7 +1078,9 @@ void CodeGenPGO::mapRegionCounters(const Decl *D) {
10831078
// for most embedded applications. Setting a maximum value prevents the
10841079
// bitmap footprint from growing too large without the user's knowledge. In
10851080
// the future, this value could be adjusted with a command-line option.
1086-
unsigned MCDCMaxConditions = (CGM.getCodeGenOpts().MCDCCoverage) ? 6 : 0;
1081+
unsigned MCDCMaxConditions =
1082+
(CGM.getCodeGenOpts().MCDCCoverage ? CGM.getCodeGenOpts().MCDCMaxConds
1083+
: 0);
10871084

10881085
RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
10891086
RegionMCDCState.reset(new MCDC::State);
@@ -1099,7 +1096,6 @@ void CodeGenPGO::mapRegionCounters(const Decl *D) {
10991096
Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
11001097
assert(Walker.NextCounter > 0 && "no entry counter mapped for decl");
11011098
NumRegionCounters = Walker.NextCounter;
1102-
RegionMCDCState->BitmapBytes = Walker.NextMCDCBitmapIdx;
11031099
FunctionHash = Walker.Hash.finalize();
11041100
}
11051101

@@ -1232,7 +1228,7 @@ void CodeGenPGO::emitMCDCParameters(CGBuilderTy &Builder) {
12321228
// anything.
12331229
llvm::Value *Args[3] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
12341230
Builder.getInt64(FunctionHash),
1235-
Builder.getInt32(RegionMCDCState->BitmapBytes)};
1231+
Builder.getInt32(RegionMCDCState->BitmapBits)};
12361232
Builder.CreateCall(
12371233
CGM.getIntrinsic(llvm::Intrinsic::instrprof_mcdc_parameters), Args);
12381234
}
@@ -1250,6 +1246,11 @@ void CodeGenPGO::emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder,
12501246
if (DecisionStateIter == RegionMCDCState->DecisionByStmt.end())
12511247
return;
12521248

1249+
// Don't create tvbitmap_update if the record is allocated but excluded.
1250+
// Or `bitmap |= (1 << 0)` would be wrongly executed to the next bitmap.
1251+
if (DecisionStateIter->second.Indices.size() == 0)
1252+
return;
1253+
12531254
// Extract the offset of the global bitmap associated with this expression.
12541255
unsigned MCDCTestVectorBitmapOffset = DecisionStateIter->second.BitmapIdx;
12551256
auto *I8PtrTy = llvm::PointerType::getUnqual(CGM.getLLVMContext());
@@ -1261,7 +1262,7 @@ void CodeGenPGO::emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder,
12611262
// index represents an executed test vector.
12621263
llvm::Value *Args[5] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
12631264
Builder.getInt64(FunctionHash),
1264-
Builder.getInt32(RegionMCDCState->BitmapBytes),
1265+
Builder.getInt32(0), // Unused
12651266
Builder.getInt32(MCDCTestVectorBitmapOffset),
12661267
MCDCCondBitmapAddr.emitRawPointer(CGF)};
12671268
Builder.CreateCall(
@@ -1305,19 +1306,22 @@ void CodeGenPGO::emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
13051306
// Extract the ID of the condition we are setting in the bitmap.
13061307
const auto &Branch = BranchStateIter->second;
13071308
assert(Branch.ID >= 0 && "Condition has no ID!");
1309+
assert(Branch.DecisionStmt);
13081310

1309-
auto *I8PtrTy = llvm::PointerType::getUnqual(CGM.getLLVMContext());
1311+
// Cancel the emission if the Decision is erased after the allocation.
1312+
const auto DecisionIter =
1313+
RegionMCDCState->DecisionByStmt.find(Branch.DecisionStmt);
1314+
if (DecisionIter == RegionMCDCState->DecisionByStmt.end())
1315+
return;
13101316

1311-
// Emit intrinsic that updates a dedicated temporary value on the stack after
1312-
// a condition is evaluated. After the set of conditions has been updated,
1313-
// the resulting value is used to update the boolean expression's bitmap.
1314-
llvm::Value *Args[5] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
1315-
Builder.getInt64(FunctionHash),
1316-
Builder.getInt32(Branch.ID),
1317-
MCDCCondBitmapAddr.emitRawPointer(CGF), Val};
1318-
Builder.CreateCall(
1319-
CGM.getIntrinsic(llvm::Intrinsic::instrprof_mcdc_condbitmap_update),
1320-
Args);
1317+
const auto &TVIdxs = DecisionIter->second.Indices[Branch.ID];
1318+
1319+
auto *CurTV = Builder.CreateLoad(MCDCCondBitmapAddr,
1320+
"mcdc." + Twine(Branch.ID + 1) + ".cur");
1321+
auto *NewTV = Builder.CreateAdd(CurTV, Builder.getInt32(TVIdxs[true]));
1322+
NewTV = Builder.CreateSelect(
1323+
Val, NewTV, Builder.CreateAdd(CurTV, Builder.getInt32(TVIdxs[false])));
1324+
Builder.CreateStore(NewTV, MCDCCondBitmapAddr);
13211325
}
13221326

13231327
void CodeGenPGO::setValueProfilingFlag(llvm::Module &M) {

clang/lib/CodeGen/CoverageMappingGen.cpp

+72-5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ class SourceMappingRegion {
195195
return std::holds_alternative<mcdc::BranchParameters>(MCDCParams);
196196
}
197197

198+
const auto &getMCDCBranchParams() const {
199+
return mcdc::getParams<const mcdc::BranchParameters>(MCDCParams);
200+
}
201+
198202
bool isMCDCDecision() const {
199203
return std::holds_alternative<mcdc::DecisionParameters>(MCDCParams);
200204
}
@@ -204,6 +208,8 @@ class SourceMappingRegion {
204208
}
205209

206210
const mcdc::Parameters &getMCDCParams() const { return MCDCParams; }
211+
212+
void resetMCDCParams() { MCDCParams = mcdc::Parameters(); }
207213
};
208214

209215
/// Spelling locations for the start and end of a source region.
@@ -748,6 +754,7 @@ struct MCDCCoverageBuilder {
748754

749755
llvm::SmallVector<mcdc::ConditionIDs> DecisionStack;
750756
MCDC::State &MCDCState;
757+
const Stmt *DecisionStmt = nullptr;
751758
mcdc::ConditionID NextID = 0;
752759
bool NotMapped = false;
753760

@@ -777,7 +784,8 @@ struct MCDCCoverageBuilder {
777784

778785
/// Set the given condition's ID.
779786
void setCondID(const Expr *Cond, mcdc::ConditionID ID) {
780-
MCDCState.BranchByStmt[CodeGenFunction::stripCond(Cond)].ID = ID;
787+
MCDCState.BranchByStmt[CodeGenFunction::stripCond(Cond)] = {ID,
788+
DecisionStmt};
781789
}
782790

783791
/// Return the ID of a given condition.
@@ -808,6 +816,11 @@ struct MCDCCoverageBuilder {
808816
if (NotMapped)
809817
return;
810818

819+
if (NextID == 0) {
820+
DecisionStmt = E;
821+
assert(MCDCState.DecisionByStmt.contains(E));
822+
}
823+
811824
const mcdc::ConditionIDs &ParentDecision = DecisionStack.back();
812825

813826
// If the operator itself has an assigned ID, this means it represents a
@@ -2122,20 +2135,70 @@ struct CounterCoverageMappingBuilder
21222135
subtractCounters(ParentCount, TrueCount));
21232136
}
21242137

2125-
void createDecision(const BinaryOperator *E) {
2138+
void createOrCancelDecision(const BinaryOperator *E, unsigned Since) {
21262139
unsigned NumConds = MCDCBuilder.getTotalConditionsAndReset(E);
21272140
if (NumConds == 0)
21282141
return;
21292142

2143+
// Extract [ID, Conds] to construct the graph.
2144+
llvm::SmallVector<mcdc::ConditionIDs> CondIDs(NumConds);
2145+
for (const auto &SR : ArrayRef(SourceRegions).slice(Since)) {
2146+
if (SR.isMCDCBranch()) {
2147+
auto [ID, Conds] = SR.getMCDCBranchParams();
2148+
CondIDs[ID] = Conds;
2149+
}
2150+
}
2151+
2152+
// Construct the graph and calculate `Indices`.
2153+
mcdc::TVIdxBuilder Builder(CondIDs);
2154+
unsigned NumTVs = Builder.NumTestVectors;
2155+
unsigned MaxTVs = CVM.getCodeGenModule().getCodeGenOpts().MCDCMaxTVs;
2156+
assert(MaxTVs < mcdc::TVIdxBuilder::HardMaxTVs);
2157+
2158+
if (NumTVs > MaxTVs) {
2159+
// NumTVs exceeds MaxTVs -- warn and cancel the Decision.
2160+
cancelDecision(E, Since, NumTVs, MaxTVs);
2161+
return;
2162+
}
2163+
2164+
// Update the state for CodeGenPGO
2165+
assert(MCDCState.DecisionByStmt.contains(E));
2166+
MCDCState.DecisionByStmt[E] = {
2167+
MCDCState.BitmapBits, // Top
2168+
std::move(Builder.Indices),
2169+
};
2170+
21302171
auto DecisionParams = mcdc::DecisionParameters{
2131-
MCDCState.DecisionByStmt[E].BitmapIdx,
2172+
MCDCState.BitmapBits += NumTVs, // Tail
21322173
NumConds,
21332174
};
21342175

21352176
// Create MCDC Decision Region.
21362177
createDecisionRegion(E, DecisionParams);
21372178
}
21382179

2180+
// Warn and cancel the Decision.
2181+
void cancelDecision(const BinaryOperator *E, unsigned Since, int NumTVs,
2182+
int MaxTVs) {
2183+
auto &Diag = CVM.getCodeGenModule().getDiags();
2184+
unsigned DiagID =
2185+
Diag.getCustomDiagID(DiagnosticsEngine::Warning,
2186+
"unsupported MC/DC boolean expression; "
2187+
"number of test vectors (%0) exceeds max (%1). "
2188+
"Expression will not be covered");
2189+
Diag.Report(E->getBeginLoc(), DiagID) << NumTVs << MaxTVs;
2190+
2191+
// Restore MCDCBranch to Branch.
2192+
for (auto &SR : MutableArrayRef(SourceRegions).slice(Since)) {
2193+
assert(!SR.isMCDCDecision() && "Decision shouldn't be seen here");
2194+
if (SR.isMCDCBranch())
2195+
SR.resetMCDCParams();
2196+
}
2197+
2198+
// Tell CodeGenPGO not to instrument.
2199+
MCDCState.DecisionByStmt.erase(E);
2200+
}
2201+
21392202
/// Check if E belongs to system headers.
21402203
bool isExprInSystemHeader(const BinaryOperator *E) const {
21412204
return (!SystemHeadersCoverage &&
@@ -2152,6 +2215,8 @@ struct CounterCoverageMappingBuilder
21522215

21532216
bool IsRootNode = MCDCBuilder.isIdle();
21542217

2218+
unsigned SourceRegionsSince = SourceRegions.size();
2219+
21552220
// Keep track of Binary Operator and assign MCDC condition IDs.
21562221
MCDCBuilder.pushAndAssignIDs(E);
21572222

@@ -2190,7 +2255,7 @@ struct CounterCoverageMappingBuilder
21902255

21912256
// Create MCDC Decision Region if at top-level (root).
21922257
if (IsRootNode)
2193-
createDecision(E);
2258+
createOrCancelDecision(E, SourceRegionsSince);
21942259
}
21952260

21962261
// Determine whether the right side of OR operation need to be visited.
@@ -2211,6 +2276,8 @@ struct CounterCoverageMappingBuilder
22112276

22122277
bool IsRootNode = MCDCBuilder.isIdle();
22132278

2279+
unsigned SourceRegionsSince = SourceRegions.size();
2280+
22142281
// Keep track of Binary Operator and assign MCDC condition IDs.
22152282
MCDCBuilder.pushAndAssignIDs(E);
22162283

@@ -2253,7 +2320,7 @@ struct CounterCoverageMappingBuilder
22532320

22542321
// Create MCDC Decision Region if at top-level (root).
22552322
if (IsRootNode)
2256-
createDecision(E);
2323+
createOrCancelDecision(E, SourceRegionsSince);
22572324
}
22582325

22592326
void VisitLambdaExpr(const LambdaExpr *LE) {

clang/lib/CodeGen/MCDCState.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ using namespace llvm::coverage::mcdc;
2626

2727
/// Per-Function MC/DC state
2828
struct State {
29-
unsigned BitmapBytes = 0;
29+
unsigned BitmapBits = 0;
3030

3131
struct Decision {
3232
unsigned BitmapIdx;
33+
llvm::SmallVector<std::array<int, 2>> Indices;
3334
};
3435

3536
llvm::DenseMap<const Stmt *, Decision> DecisionByStmt;
3637

3738
struct Branch {
3839
ConditionID ID;
40+
const Stmt *DecisionStmt;
3941
};
4042

4143
llvm::DenseMap<const Stmt *, Branch> BranchByStmt;

0 commit comments

Comments
 (0)