Skip to content

Commit e2b5364

Browse files
authored
[CodeGen] Move CodeGenPGO behind unique_ptr (NFC) (#142155)
The InstrProf headers are very expensive. Avoid including them in all of CodeGen/ by moving the CodeGenPGO member behind a unqiue_ptr. This reduces clang build time by 0.8%.
1 parent 9801fdf commit e2b5364

14 files changed

+99
-63
lines changed

clang/lib/CodeGen/CGBlocks.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "CGOpenCLRuntime.h"
1818
#include "CodeGenFunction.h"
1919
#include "CodeGenModule.h"
20+
#include "CodeGenPGO.h"
2021
#include "ConstantEmitter.h"
2122
#include "TargetInfo.h"
2223
#include "clang/AST/Attr.h"
@@ -1522,7 +1523,7 @@ llvm::Function *CodeGenFunction::GenerateBlockFunction(
15221523
if (IsLambdaConversionToBlock)
15231524
EmitLambdaBlockInvokeBody();
15241525
else {
1525-
PGO.assignRegionCounters(GlobalDecl(blockDecl), fn);
1526+
PGO->assignRegionCounters(GlobalDecl(blockDecl), fn);
15261527
incrementProfileCounter(blockDecl->getBody());
15271528
EmitStmt(blockDecl->getBody());
15281529
}

clang/lib/CodeGen/CGCall.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "CGRecordLayout.h"
2222
#include "CodeGenFunction.h"
2323
#include "CodeGenModule.h"
24+
#include "CodeGenPGO.h"
2425
#include "TargetInfo.h"
2526
#include "clang/AST/Attr.h"
2627
#include "clang/AST/Decl.h"
@@ -5900,7 +5901,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
59005901
// For more details, see the comment before the definition of
59015902
// IPVK_IndirectCallTarget in InstrProfData.inc.
59025903
if (!CI->getCalledFunction())
5903-
PGO.valueProfile(Builder, llvm::IPVK_IndirectCallTarget, CI, CalleePtr);
5904+
PGO->valueProfile(Builder, llvm::IPVK_IndirectCallTarget, CI, CalleePtr);
59045905

59055906
// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
59065907
// optimizer it can aggressively ignore unwind edges.

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "CGOpenMPRuntime.h"
1919
#include "CodeGenFunction.h"
2020
#include "CodeGenModule.h"
21+
#include "CodeGenPGO.h"
2122
#include "ConstantEmitter.h"
2223
#include "EHScopeStack.h"
2324
#include "PatternInit.h"
@@ -368,7 +369,7 @@ CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
368369
return GV;
369370
}
370371

371-
PGO.markStmtMaybeUsed(D.getInit()); // FIXME: Too lazy
372+
PGO->markStmtMaybeUsed(D.getInit()); // FIXME: Too lazy
372373

373374
#ifndef NDEBUG
374375
CharUnits VarSize = CGM.getContext().getTypeSizeInChars(D.getType()) +
@@ -1944,7 +1945,7 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
19441945
// unless it contains a label.
19451946
if (!HaveInsertPoint()) {
19461947
if (!Init || !ContainsLabel(Init)) {
1947-
PGO.markStmtMaybeUsed(Init);
1948+
PGO->markStmtMaybeUsed(Init);
19481949
return;
19491950
}
19501951
EnsureInsertPoint();
@@ -2057,7 +2058,7 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
20572058
return EmitExprAsInit(Init, &D, lv, capturedByInit);
20582059
}
20592060

2060-
PGO.markStmtMaybeUsed(Init);
2061+
PGO->markStmtMaybeUsed(Init);
20612062

20622063
if (!emission.IsConstantAggregate) {
20632064
// For simple scalar/complex initialization, store the value directly.

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "CGRecordLayout.h"
2222
#include "CodeGenFunction.h"
2323
#include "CodeGenModule.h"
24+
#include "CodeGenPGO.h"
2425
#include "ConstantEmitter.h"
2526
#include "TargetInfo.h"
2627
#include "clang/AST/ASTContext.h"
@@ -207,7 +208,7 @@ RawAddress CodeGenFunction::CreateMemTempWithoutCast(QualType Ty,
207208
/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
208209
/// expression and compare the result against zero, returning an Int1Ty value.
209210
llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
210-
PGO.setCurrentStmt(E);
211+
PGO->setCurrentStmt(E);
211212
if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
212213
llvm::Value *MemPtr = EmitScalarExpr(E);
213214
return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);

clang/lib/CodeGen/CGObjC.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CGObjCRuntime.h"
1515
#include "CodeGenFunction.h"
1616
#include "CodeGenModule.h"
17+
#include "CodeGenPGO.h"
1718
#include "ConstantEmitter.h"
1819
#include "TargetInfo.h"
1920
#include "clang/AST/ASTContext.h"
@@ -805,7 +806,7 @@ static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
805806
/// its pointer, name, and types registered in the class structure.
806807
void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
807808
StartObjCMethod(OMD, OMD->getClassInterface());
808-
PGO.assignRegionCounters(GlobalDecl(OMD), CurFn);
809+
PGO->assignRegionCounters(GlobalDecl(OMD), CurFn);
809810
assert(isa<CompoundStmt>(OMD->getBody()));
810811
incrementProfileCounter(OMD->getBody());
811812
EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody()));

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CGOpenMPRuntime.h"
1515
#include "CodeGenFunction.h"
1616
#include "CodeGenModule.h"
17+
#include "CodeGenPGO.h"
1718
#include "TargetInfo.h"
1819
#include "clang/AST/Attr.h"
1920
#include "clang/AST/Expr.h"
@@ -59,7 +60,7 @@ void CodeGenFunction::EmitStopPoint(const Stmt *S) {
5960

6061
void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) {
6162
assert(S && "Null statement?");
62-
PGO.setCurrentStmt(S);
63+
PGO->setCurrentStmt(S);
6364

6465
// These statements have their own debug info handling.
6566
if (EmitSimpleStmt(S, Attrs))
@@ -76,7 +77,7 @@ void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) {
7677
// Verify that any decl statements were handled as simple, they may be in
7778
// scope of subsequent reachable statements.
7879
assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
79-
PGO.markStmtMaybeUsed(S);
80+
PGO->markStmtMaybeUsed(S);
8081
return;
8182
}
8283

@@ -917,7 +918,7 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
917918
RunCleanupsScope ExecutedScope(*this);
918919
EmitStmt(Executed);
919920
}
920-
PGO.markStmtMaybeUsed(Skipped);
921+
PGO->markStmtMaybeUsed(Skipped);
921922
return;
922923
}
923924
}
@@ -2265,7 +2266,7 @@ void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
22652266
for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i)
22662267
EmitStmt(CaseStmts[i]);
22672268
incrementProfileCounter(&S);
2268-
PGO.markStmtMaybeUsed(S.getBody());
2269+
PGO->markStmtMaybeUsed(S.getBody());
22692270

22702271
// Now we want to restore the saved switch instance so that nested
22712272
// switches continue to function properly
@@ -2308,7 +2309,7 @@ void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
23082309
llvm::MDNode::get(CGM.getLLVMContext(), Vals));
23092310
}
23102311

2311-
if (PGO.haveRegionCounts()) {
2312+
if (PGO->haveRegionCounts()) {
23122313
// Walk the SwitchCase list to find how many there are.
23132314
uint64_t DefaultCount = 0;
23142315
unsigned NumCases = 0;
@@ -3329,7 +3330,7 @@ CodeGenFunction::GenerateCapturedStmtFunction(const CapturedStmt &S) {
33293330
CXXThisValue = EmitLoadOfLValue(ThisLValue, Loc).getScalarVal();
33303331
}
33313332

3332-
PGO.assignRegionCounters(GlobalDecl(CD), F);
3333+
PGO->assignRegionCounters(GlobalDecl(CD), F);
33333334
CapturedStmtInfo->EmitBody(*this, CD->getBody());
33343335
FinishFunction(CD->getBodyRBrace());
33353336

clang/lib/CodeGen/CGStmtOpenMP.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "CGOpenMPRuntime.h"
1616
#include "CodeGenFunction.h"
1717
#include "CodeGenModule.h"
18+
#include "CodeGenPGO.h"
1819
#include "TargetInfo.h"
1920
#include "clang/AST/ASTContext.h"
2021
#include "clang/AST/Attr.h"
@@ -710,7 +711,7 @@ CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S,
710711
(void)LocalScope.Privatize();
711712
for (const auto &VLASizePair : WrapperVLASizes)
712713
VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second;
713-
PGO.assignRegionCounters(GlobalDecl(CD), F);
714+
PGO->assignRegionCounters(GlobalDecl(CD), F);
714715
CapturedStmtInfo->EmitBody(*this, CD->getBody());
715716
(void)LocalScope.ForceCleanup();
716717
FinishFunction(CD->getBodyRBrace());

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
8080
Builder(cgm, cgm.getModule().getContext(), llvm::ConstantFolder(),
8181
CGBuilderInserterTy(this)),
8282
SanOpts(CGM.getLangOpts().Sanitize), CurFPFeatures(CGM.getLangOpts()),
83-
DebugInfo(CGM.getModuleDebugInfo()), PGO(cgm),
83+
DebugInfo(CGM.getModuleDebugInfo()),
84+
PGO(std::make_unique<CodeGenPGO>(cgm)),
8485
ShouldEmitLifetimeMarkers(
8586
shouldEmitLifetimeMarkers(CGM.getCodeGenOpts(), CGM.getLangOpts())) {
8687
if (!suppressNewContext)
@@ -1564,7 +1565,7 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
15641565
CurFn->addFnAttr(llvm::Attribute::MustProgress);
15651566

15661567
// Generate the body of the function.
1567-
PGO.assignRegionCounters(GD, CurFn);
1568+
PGO->assignRegionCounters(GD, CurFn);
15681569
if (isa<CXXDestructorDecl>(FD))
15691570
EmitDestructorBody(Args);
15701571
else if (isa<CXXConstructorDecl>(FD))
@@ -1650,7 +1651,7 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
16501651
// Emit the standard function epilogue.
16511652
FinishFunction(BodyRange.getEnd());
16521653

1653-
PGO.verifyCounterMap();
1654+
PGO->verifyCounterMap();
16541655

16551656
// If we haven't marked the function nothrow through other means, do
16561657
// a quick pass now to see if we can.
@@ -1774,7 +1775,7 @@ bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
17741775
if (!AllowLabels && CodeGenFunction::ContainsLabel(Cond))
17751776
return false; // Contains a label.
17761777

1777-
PGO.markStmtMaybeUsed(Cond);
1778+
PGO->markStmtMaybeUsed(Cond);
17781779
ResultInt = Int;
17791780
return true;
17801781
}
@@ -3355,4 +3356,4 @@ void CodeGenFunction::addInstToNewSourceAtom(llvm::Instruction *KeyInstruction,
33553356
ApplyAtomGroup Grp(getDebugInfo());
33563357
DI->addInstToCurrentSourceAtom(KeyInstruction, Backup);
33573358
}
3358-
}
3359+
}

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "CGLoopInfo.h"
1818
#include "CGValue.h"
1919
#include "CodeGenModule.h"
20-
#include "CodeGenPGO.h"
2120
#include "EHScopeStack.h"
2221
#include "VarBypassDetector.h"
2322
#include "clang/AST/CharUnits.h"
@@ -90,6 +89,7 @@ class OSLogBufferLayout;
9089

9190
namespace CodeGen {
9291
class CodeGenTypes;
92+
class CodeGenPGO;
9393
class CGCallee;
9494
class CGFunctionInfo;
9595
class CGBlockInfo;
@@ -1670,7 +1670,7 @@ class CodeGenFunction : public CodeGenTypeCache {
16701670
llvm::Value *emitCondLikelihoodViaExpectIntrinsic(llvm::Value *Cond,
16711671
Stmt::Likelihood LH);
16721672

1673-
CodeGenPGO PGO;
1673+
std::unique_ptr<CodeGenPGO> PGO;
16741674

16751675
/// Bitmap used by MC/DC to track condition outcomes of a boolean expression.
16761676
Address MCDCCondBitmapAddr = Address::invalid();
@@ -1683,12 +1683,9 @@ class CodeGenFunction : public CodeGenTypeCache {
16831683
uint64_t LoopCount) const;
16841684

16851685
public:
1686-
auto getIsCounterPair(const Stmt *S) const { return PGO.getIsCounterPair(S); }
1687-
1688-
void markStmtAsUsed(bool Skipped, const Stmt *S) {
1689-
PGO.markStmtAsUsed(Skipped, S);
1690-
}
1691-
void markStmtMaybeUsed(const Stmt *S) { PGO.markStmtMaybeUsed(S); }
1686+
std::pair<bool, bool> getIsCounterPair(const Stmt *S) const;
1687+
void markStmtAsUsed(bool Skipped, const Stmt *S);
1688+
void markStmtMaybeUsed(const Stmt *S);
16921689

16931690
/// Increment the profiler's counter for the given statement by \p StepV.
16941691
/// If \p StepV is null, the default increment is 1.
@@ -1702,57 +1699,32 @@ class CodeGenFunction : public CodeGenTypeCache {
17021699

17031700
/// Allocate a temp value on the stack that MCDC can use to track condition
17041701
/// results.
1705-
void maybeCreateMCDCCondBitmap() {
1706-
if (isMCDCCoverageEnabled()) {
1707-
PGO.emitMCDCParameters(Builder);
1708-
MCDCCondBitmapAddr =
1709-
CreateIRTemp(getContext().UnsignedIntTy, "mcdc.addr");
1710-
}
1711-
}
1702+
void maybeCreateMCDCCondBitmap();
17121703

17131704
bool isBinaryLogicalOp(const Expr *E) const {
17141705
const BinaryOperator *BOp = dyn_cast<BinaryOperator>(E->IgnoreParens());
17151706
return (BOp && BOp->isLogicalOp());
17161707
}
17171708

17181709
/// Zero-init the MCDC temp value.
1719-
void maybeResetMCDCCondBitmap(const Expr *E) {
1720-
if (isMCDCCoverageEnabled() && isBinaryLogicalOp(E)) {
1721-
PGO.emitMCDCCondBitmapReset(Builder, E, MCDCCondBitmapAddr);
1722-
PGO.setCurrentStmt(E);
1723-
}
1724-
}
1710+
void maybeResetMCDCCondBitmap(const Expr *E);
17251711

17261712
/// Increment the profiler's counter for the given expression by \p StepV.
17271713
/// If \p StepV is null, the default increment is 1.
1728-
void maybeUpdateMCDCTestVectorBitmap(const Expr *E) {
1729-
if (isMCDCCoverageEnabled() && isBinaryLogicalOp(E)) {
1730-
PGO.emitMCDCTestVectorBitmapUpdate(Builder, E, MCDCCondBitmapAddr, *this);
1731-
PGO.setCurrentStmt(E);
1732-
}
1733-
}
1714+
void maybeUpdateMCDCTestVectorBitmap(const Expr *E);
17341715

17351716
/// Update the MCDC temp value with the condition's evaluated result.
1736-
void maybeUpdateMCDCCondBitmap(const Expr *E, llvm::Value *Val) {
1737-
if (isMCDCCoverageEnabled()) {
1738-
PGO.emitMCDCCondBitmapUpdate(Builder, E, MCDCCondBitmapAddr, Val, *this);
1739-
PGO.setCurrentStmt(E);
1740-
}
1741-
}
1717+
void maybeUpdateMCDCCondBitmap(const Expr *E, llvm::Value *Val);
17421718

17431719
/// Get the profiler's count for the given statement.
1744-
uint64_t getProfileCount(const Stmt *S) {
1745-
return PGO.getStmtCount(S).value_or(0);
1746-
}
1720+
uint64_t getProfileCount(const Stmt *S);
17471721

17481722
/// Set the profiler's current count.
1749-
void setCurrentProfileCount(uint64_t Count) {
1750-
PGO.setCurrentRegionCount(Count);
1751-
}
1723+
void setCurrentProfileCount(uint64_t Count);
17521724

17531725
/// Get the profiler's current count. This is generally the count for the most
17541726
/// recently incremented counter.
1755-
uint64_t getCurrentProfileCount() { return PGO.getCurrentRegionCount(); }
1727+
uint64_t getCurrentProfileCount();
17561728

17571729
/// See CGDebugInfo::addInstToCurrentSourceAtom.
17581730
void addInstToCurrentSourceAtom(llvm::Instruction *KeyInstruction,

0 commit comments

Comments
 (0)