Skip to content

Commit 5c8985e

Browse files
authored
clangCodeGen: Introduce MCDC::State with MCDCState.h (#81497)
This packs; * `BitmapBytes` * `BitmapMap` * `CondIDMap` into `MCDC::State`.
1 parent bd2f7bb commit 5c8985e

File tree

5 files changed

+78
-55
lines changed

5 files changed

+78
-55
lines changed

clang/lib/CodeGen/CodeGenPGO.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
165165
llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
166166
/// The next bitmap byte index to assign.
167167
unsigned NextMCDCBitmapIdx;
168-
/// The map of statements to MC/DC bitmap coverage objects.
169-
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap;
168+
MCDC::State &MCDCState;
170169
/// Maximum number of supported MC/DC conditions in a boolean expression.
171170
unsigned MCDCMaxCond;
172171
/// The profile version.
@@ -176,11 +175,11 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
176175

177176
MapRegionCounters(PGOHashVersion HashVersion, uint64_t ProfileVersion,
178177
llvm::DenseMap<const Stmt *, unsigned> &CounterMap,
179-
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap,
180-
unsigned MCDCMaxCond, DiagnosticsEngine &Diag)
178+
MCDC::State &MCDCState, unsigned MCDCMaxCond,
179+
DiagnosticsEngine &Diag)
181180
: NextCounter(0), Hash(HashVersion), CounterMap(CounterMap),
182-
NextMCDCBitmapIdx(0), MCDCBitmapMap(MCDCBitmapMap),
183-
MCDCMaxCond(MCDCMaxCond), ProfileVersion(ProfileVersion), Diag(Diag) {}
181+
NextMCDCBitmapIdx(0), MCDCState(MCDCState), MCDCMaxCond(MCDCMaxCond),
182+
ProfileVersion(ProfileVersion), Diag(Diag) {}
184183

185184
// Blocks and lambdas are handled as separate functions, so we need not
186185
// traverse them in the parent context.
@@ -309,7 +308,7 @@ struct MapRegionCounters : public RecursiveASTVisitor<MapRegionCounters> {
309308

310309
// Otherwise, allocate the number of bytes required for the bitmap
311310
// based on the number of conditions. Must be at least 1-byte long.
312-
MCDCBitmapMap[BinOp] = NextMCDCBitmapIdx;
311+
MCDCState.BitmapMap[BinOp] = NextMCDCBitmapIdx;
313312
unsigned SizeInBits = std::max<unsigned>(1L << NumCond, CHAR_BIT);
314313
NextMCDCBitmapIdx += SizeInBits / CHAR_BIT;
315314
}
@@ -987,10 +986,9 @@ void CodeGenPGO::mapRegionCounters(const Decl *D) {
987986
unsigned MCDCMaxConditions = (CGM.getCodeGenOpts().MCDCCoverage) ? 6 : 0;
988987

989988
RegionCounterMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
990-
RegionMCDCBitmapMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
989+
RegionMCDCState.reset(new MCDC::State);
991990
MapRegionCounters Walker(HashVersion, ProfileVersion, *RegionCounterMap,
992-
*RegionMCDCBitmapMap, MCDCMaxConditions,
993-
CGM.getDiags());
991+
*RegionMCDCState, MCDCMaxConditions, CGM.getDiags());
994992
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
995993
Walker.TraverseDecl(const_cast<FunctionDecl *>(FD));
996994
else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
@@ -1001,7 +999,7 @@ void CodeGenPGO::mapRegionCounters(const Decl *D) {
1001999
Walker.TraverseDecl(const_cast<CapturedDecl *>(CD));
10021000
assert(Walker.NextCounter > 0 && "no entry counter mapped for decl");
10031001
NumRegionCounters = Walker.NextCounter;
1004-
MCDCBitmapBytes = Walker.NextMCDCBitmapIdx;
1002+
RegionMCDCState->BitmapBytes = Walker.NextMCDCBitmapIdx;
10051003
FunctionHash = Walker.Hash.finalize();
10061004
}
10071005

@@ -1033,11 +1031,10 @@ void CodeGenPGO::emitCounterRegionMapping(const Decl *D) {
10331031

10341032
std::string CoverageMapping;
10351033
llvm::raw_string_ostream OS(CoverageMapping);
1036-
RegionCondIDMap.reset(new llvm::DenseMap<const Stmt *, unsigned>);
1034+
RegionMCDCState->CondIDMap.clear();
10371035
CoverageMappingGen MappingGen(
10381036
*CGM.getCoverageMapping(), CGM.getContext().getSourceManager(),
1039-
CGM.getLangOpts(), RegionCounterMap.get(), RegionMCDCBitmapMap.get(),
1040-
RegionCondIDMap.get());
1037+
CGM.getLangOpts(), RegionCounterMap.get(), RegionMCDCState.get());
10411038
MappingGen.emitCounterMapping(D, OS);
10421039
OS.flush();
10431040

@@ -1119,7 +1116,7 @@ bool CodeGenPGO::canEmitMCDCCoverage(const CGBuilderTy &Builder) {
11191116
}
11201117

11211118
void CodeGenPGO::emitMCDCParameters(CGBuilderTy &Builder) {
1122-
if (!canEmitMCDCCoverage(Builder) || !RegionMCDCBitmapMap)
1119+
if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState)
11231120
return;
11241121

11251122
auto *I8PtrTy = llvm::PointerType::getUnqual(CGM.getLLVMContext());
@@ -1129,21 +1126,21 @@ void CodeGenPGO::emitMCDCParameters(CGBuilderTy &Builder) {
11291126
// anything.
11301127
llvm::Value *Args[3] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
11311128
Builder.getInt64(FunctionHash),
1132-
Builder.getInt32(MCDCBitmapBytes)};
1129+
Builder.getInt32(RegionMCDCState->BitmapBytes)};
11331130
Builder.CreateCall(
11341131
CGM.getIntrinsic(llvm::Intrinsic::instrprof_mcdc_parameters), Args);
11351132
}
11361133

11371134
void CodeGenPGO::emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder,
11381135
const Expr *S,
11391136
Address MCDCCondBitmapAddr) {
1140-
if (!canEmitMCDCCoverage(Builder) || !RegionMCDCBitmapMap)
1137+
if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState)
11411138
return;
11421139

11431140
S = S->IgnoreParens();
11441141

1145-
auto ExprMCDCBitmapMapIterator = RegionMCDCBitmapMap->find(S);
1146-
if (ExprMCDCBitmapMapIterator == RegionMCDCBitmapMap->end())
1142+
auto ExprMCDCBitmapMapIterator = RegionMCDCState->BitmapMap.find(S);
1143+
if (ExprMCDCBitmapMapIterator == RegionMCDCState->BitmapMap.end())
11471144
return;
11481145

11491146
// Extract the ID of the global bitmap associated with this expression.
@@ -1157,7 +1154,7 @@ void CodeGenPGO::emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder,
11571154
// index represents an executed test vector.
11581155
llvm::Value *Args[5] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
11591156
Builder.getInt64(FunctionHash),
1160-
Builder.getInt32(MCDCBitmapBytes),
1157+
Builder.getInt32(RegionMCDCState->BitmapBytes),
11611158
Builder.getInt32(MCDCTestVectorBitmapID),
11621159
MCDCCondBitmapAddr.getPointer()};
11631160
Builder.CreateCall(
@@ -1166,12 +1163,12 @@ void CodeGenPGO::emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder,
11661163

11671164
void CodeGenPGO::emitMCDCCondBitmapReset(CGBuilderTy &Builder, const Expr *S,
11681165
Address MCDCCondBitmapAddr) {
1169-
if (!canEmitMCDCCoverage(Builder) || !RegionMCDCBitmapMap)
1166+
if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState)
11701167
return;
11711168

11721169
S = S->IgnoreParens();
11731170

1174-
if (RegionMCDCBitmapMap->find(S) == RegionMCDCBitmapMap->end())
1171+
if (!RegionMCDCState->BitmapMap.contains(S))
11751172
return;
11761173

11771174
// Emit intrinsic that resets a dedicated temporary value on the stack to 0.
@@ -1181,7 +1178,7 @@ void CodeGenPGO::emitMCDCCondBitmapReset(CGBuilderTy &Builder, const Expr *S,
11811178
void CodeGenPGO::emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
11821179
Address MCDCCondBitmapAddr,
11831180
llvm::Value *Val) {
1184-
if (!canEmitMCDCCoverage(Builder) || !RegionCondIDMap)
1181+
if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState)
11851182
return;
11861183

11871184
// Even though, for simplicity, parentheses and unary logical-NOT operators
@@ -1193,8 +1190,8 @@ void CodeGenPGO::emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
11931190
// also make debugging a bit easier.
11941191
S = CodeGenFunction::stripCond(S);
11951192

1196-
auto ExprMCDCConditionIDMapIterator = RegionCondIDMap->find(S);
1197-
if (ExprMCDCConditionIDMapIterator == RegionCondIDMap->end())
1193+
auto ExprMCDCConditionIDMapIterator = RegionMCDCState->CondIDMap.find(S);
1194+
if (ExprMCDCConditionIDMapIterator == RegionMCDCState->CondIDMap.end())
11981195
return;
11991196

12001197
// Extract the ID of the condition we are setting in the bitmap.

clang/lib/CodeGen/CodeGenPGO.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "CGBuilder.h"
1717
#include "CodeGenModule.h"
1818
#include "CodeGenTypes.h"
19+
#include "MCDCState.h"
1920
#include "llvm/ProfileData/InstrProfReader.h"
2021
#include <array>
2122
#include <memory>
@@ -33,21 +34,18 @@ class CodeGenPGO {
3334

3435
std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
3536
unsigned NumRegionCounters;
36-
unsigned MCDCBitmapBytes;
3737
uint64_t FunctionHash;
3838
std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
39-
std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionMCDCBitmapMap;
40-
std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCondIDMap;
4139
std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
4240
std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
41+
std::unique_ptr<MCDC::State> RegionMCDCState;
4342
std::vector<uint64_t> RegionCounts;
4443
uint64_t CurrentRegionCount;
4544

4645
public:
4746
CodeGenPGO(CodeGenModule &CGModule)
4847
: CGM(CGModule), FuncNameVar(nullptr), NumValueSites({{0}}),
49-
NumRegionCounters(0), MCDCBitmapBytes(0), FunctionHash(0),
50-
CurrentRegionCount(0) {}
48+
NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0) {}
5149

5250
/// Whether or not we have PGO region data for the current function. This is
5351
/// false both when we have no data at all and when our data has been

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,8 @@ struct MCDCCoverageBuilder {
689689
CodeGenModule &CGM;
690690

691691
llvm::SmallVector<DecisionIDPair> DecisionStack;
692+
MCDC::State &MCDCState;
692693
llvm::DenseMap<const Stmt *, mcdc::ConditionID> &CondIDs;
693-
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap;
694694
mcdc::ConditionID NextID = 1;
695695
bool NotMapped = false;
696696

@@ -703,12 +703,9 @@ struct MCDCCoverageBuilder {
703703
}
704704

705705
public:
706-
MCDCCoverageBuilder(
707-
CodeGenModule &CGM,
708-
llvm::DenseMap<const Stmt *, mcdc::ConditionID> &CondIDMap,
709-
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap)
710-
: CGM(CGM), DecisionStack(1, DecisionStackSentinel), CondIDs(CondIDMap),
711-
MCDCBitmapMap(MCDCBitmapMap) {}
706+
MCDCCoverageBuilder(CodeGenModule &CGM, MCDC::State &MCDCState)
707+
: CGM(CGM), DecisionStack(1, DecisionStackSentinel), MCDCState(MCDCState),
708+
CondIDs(MCDCState.CondIDMap) {}
712709

713710
/// Return whether the build of the control flow map is at the top-level
714711
/// (root) of a logical operator nest in a boolean expression prior to the
@@ -745,7 +742,8 @@ struct MCDCCoverageBuilder {
745742
return;
746743

747744
// If binary expression is disqualified, don't do mapping.
748-
if (!isBuilding() && !MCDCBitmapMap.contains(CodeGenFunction::stripCond(E)))
745+
if (!isBuilding() &&
746+
!MCDCState.BitmapMap.contains(CodeGenFunction::stripCond(E)))
749747
NotMapped = true;
750748

751749
// Don't go any further if we don't need to map condition IDs.
@@ -818,8 +816,7 @@ struct CounterCoverageMappingBuilder
818816
/// The map of statements to count values.
819817
llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
820818

821-
/// The map of statements to bitmap coverage object values.
822-
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap;
819+
MCDC::State &MCDCState;
823820

824821
/// A stack of currently live regions.
825822
llvm::SmallVector<SourceMappingRegion> RegionStack;
@@ -863,7 +860,7 @@ struct CounterCoverageMappingBuilder
863860
return Counter::getCounter(CounterMap[S]);
864861
}
865862

866-
unsigned getRegionBitmap(const Stmt *S) { return MCDCBitmapMap[S]; }
863+
unsigned getRegionBitmap(const Stmt *S) { return MCDCState.BitmapMap[S]; }
867864

868865
/// Push a region onto the stack.
869866
///
@@ -1341,12 +1338,9 @@ struct CounterCoverageMappingBuilder
13411338
CounterCoverageMappingBuilder(
13421339
CoverageMappingModuleGen &CVM,
13431340
llvm::DenseMap<const Stmt *, unsigned> &CounterMap,
1344-
llvm::DenseMap<const Stmt *, unsigned> &MCDCBitmapMap,
1345-
llvm::DenseMap<const Stmt *, mcdc::ConditionID> &CondIDMap,
1346-
SourceManager &SM, const LangOptions &LangOpts)
1341+
MCDC::State &MCDCState, SourceManager &SM, const LangOptions &LangOpts)
13471342
: CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
1348-
MCDCBitmapMap(MCDCBitmapMap),
1349-
MCDCBuilder(CVM.getCodeGenModule(), CondIDMap, MCDCBitmapMap) {}
1343+
MCDCState(MCDCState), MCDCBuilder(CVM.getCodeGenModule(), MCDCState) {}
13501344

13511345
/// Write the mapping data to the output stream
13521346
void write(llvm::raw_ostream &OS) {
@@ -2350,9 +2344,9 @@ unsigned CoverageMappingModuleGen::getFileID(FileEntryRef File) {
23502344

23512345
void CoverageMappingGen::emitCounterMapping(const Decl *D,
23522346
llvm::raw_ostream &OS) {
2353-
assert(CounterMap && MCDCBitmapMap);
2354-
CounterCoverageMappingBuilder Walker(CVM, *CounterMap, *MCDCBitmapMap,
2355-
*CondIDMap, SM, LangOpts);
2347+
assert(CounterMap && MCDCState);
2348+
CounterCoverageMappingBuilder Walker(CVM, *CounterMap, *MCDCState, SM,
2349+
LangOpts);
23562350
Walker.VisitDecl(D);
23572351
Walker.write(OS);
23582352
}

clang/lib/CodeGen/CoverageMappingGen.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ namespace CodeGen {
9191

9292
class CodeGenModule;
9393

94+
namespace MCDC {
95+
struct State;
96+
}
97+
9498
/// Organizes the cross-function state that is used while generating
9599
/// code coverage mapping data.
96100
class CoverageMappingModuleGen {
@@ -150,22 +154,20 @@ class CoverageMappingGen {
150154
SourceManager &SM;
151155
const LangOptions &LangOpts;
152156
llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
153-
llvm::DenseMap<const Stmt *, unsigned> *MCDCBitmapMap;
154-
llvm::DenseMap<const Stmt *, unsigned> *CondIDMap;
157+
MCDC::State *MCDCState;
155158

156159
public:
157160
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
158161
const LangOptions &LangOpts)
159162
: CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr),
160-
MCDCBitmapMap(nullptr), CondIDMap(nullptr) {}
163+
MCDCState(nullptr) {}
161164

162165
CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
163166
const LangOptions &LangOpts,
164167
llvm::DenseMap<const Stmt *, unsigned> *CounterMap,
165-
llvm::DenseMap<const Stmt *, unsigned> *MCDCBitmapMap,
166-
llvm::DenseMap<const Stmt *, unsigned> *CondIDMap)
168+
MCDC::State *MCDCState)
167169
: CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap),
168-
MCDCBitmapMap(MCDCBitmapMap), CondIDMap(CondIDMap) {}
170+
MCDCState(MCDCState) {}
169171

170172
/// Emit the coverage mapping data which maps the regions of
171173
/// code to counters that will be used to find the execution

clang/lib/CodeGen/MCDCState.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===---- MCDCState.h - Per-Function MC/DC state ----------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Per-Function MC/DC state for PGO
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_CLANG_LIB_CODEGEN_MCDCSTATE_H
14+
#define LLVM_CLANG_LIB_CODEGEN_MCDCSTATE_H
15+
16+
#include "llvm/ADT/DenseMap.h"
17+
#include "llvm/ProfileData/Coverage/MCDCTypes.h"
18+
19+
namespace clang::CodeGen::MCDC {
20+
21+
using namespace llvm::coverage::mcdc;
22+
23+
/// Per-Function MC/DC state
24+
struct State {
25+
unsigned BitmapBytes = 0;
26+
llvm::DenseMap<const Stmt *, unsigned> BitmapMap;
27+
llvm::DenseMap<const Stmt *, ConditionID> CondIDMap;
28+
};
29+
30+
} // namespace clang::CodeGen::MCDC
31+
32+
#endif // LLVM_CLANG_LIB_CODEGEN_MCDCSTATE_H

0 commit comments

Comments
 (0)