Skip to content

Commit 76b893d

Browse files
authored
Merge pull request #61519 from hamishknight/minor-cleanup
2 parents d665ae0 + 04ef945 commit 76b893d

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

lib/SIL/IR/SILProfiler.cpp

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,6 @@ static bool shouldProfile(ASTNode N, SILDeclRef Constant) {
5959
return true;
6060
}
6161

62-
/// Get the DeclContext for the decl referenced by \p forDecl.
63-
DeclContext *getProfilerContextForDecl(ASTNode N, SILDeclRef forDecl) {
64-
if (auto *D = N.dyn_cast<Decl *>())
65-
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D))
66-
return TLCD;
67-
assert(!forDecl.isNull() && "Expected a nonnull SILDeclRef");
68-
if (auto *ACE = forDecl.getAbstractClosureExpr())
69-
return ACE;
70-
return forDecl.getDecl()->getDeclContext();
71-
}
72-
7362
static Stmt *getProfilerStmtForCase(CaseStmt *caseStmt) {
7463
switch (caseStmt->getParentKind()) {
7564
case CaseParentKind::Switch:
@@ -82,15 +71,14 @@ static Stmt *getProfilerStmtForCase(CaseStmt *caseStmt) {
8271

8372
/// Check that the input AST has at least been type-checked.
8473
LLVM_ATTRIBUTE_UNUSED
85-
static bool hasASTBeenTypeChecked(ASTNode N, SILDeclRef forDecl) {
86-
DeclContext *DC = getProfilerContextForDecl(N, forDecl);
87-
SourceFile *SF = DC->getParentSourceFile();
88-
return !SF || SF->ASTStage >= SourceFile::TypeChecked;
74+
static bool hasFileBeenTypeChecked(SILDeclRef forDecl) {
75+
auto *SF = forDecl.getInnermostDeclContext()->getParentSourceFile();
76+
return SF && SF->ASTStage >= SourceFile::TypeChecked;
8977
}
9078

9179
/// Check whether a mapped AST node is valid for profiling.
9280
static bool canCreateProfilerForAST(ASTNode N, SILDeclRef forDecl) {
93-
assert(hasASTBeenTypeChecked(N, forDecl) &&
81+
assert(hasFileBeenTypeChecked(forDecl) &&
9482
"Cannot use this AST for profiling");
9583

9684
if (auto *D = N.dyn_cast<Decl *>()) {
@@ -757,8 +745,9 @@ struct CoverageMapping : public ASTWalker {
757745
Res.first->second = std::move(Expr);
758746
}
759747

760-
/// Create a counter expression referencing \c Node's own counter.
761-
CounterExpr assignCounter(ASTNode Node) {
748+
/// Create a counter expression referencing \c Node's own counter. This must
749+
/// have been previously mapped by MapRegionCounters.
750+
CounterExpr assignKnownCounter(ASTNode Node) {
762751
auto Counter = CounterExpr::Leaf(Node);
763752
assignCounter(Node, Counter);
764753
return Counter;
@@ -966,10 +955,10 @@ struct CoverageMapping : public ASTWalker {
966955
PreWalkAction walkToDeclPre(Decl *D) override {
967956
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
968957
return visitFunctionDecl(*this, AFD, [&] {
969-
assignCounter(AFD->getBody());
958+
assignKnownCounter(AFD->getBody());
970959
});
971960
} else if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
972-
assignCounter(TLCD->getBody());
961+
assignKnownCounter(TLCD->getBody());
973962
ImplicitTopLevelBody = TLCD->getBody();
974963
return Action::Continue();
975964
}
@@ -1005,15 +994,15 @@ struct CoverageMapping : public ASTWalker {
1005994

1006995
// We emit a counter for the then block, and define the else block in
1007996
// terms of it.
1008-
auto ThenCounter = assignCounter(IS->getThenStmt());
997+
auto ThenCounter = assignKnownCounter(IS->getThenStmt());
1009998
if (IS->getElseStmt()) {
1010999
auto ElseCounter =
10111000
CounterExpr::Sub(getCurrentCounter(), ThenCounter, CounterAlloc);
10121001
assignCounter(IS->getElseStmt(), ElseCounter);
10131002
}
10141003
} else if (auto *GS = dyn_cast<GuardStmt>(S)) {
10151004
assignCounter(GS, CounterExpr::Zero());
1016-
assignCounter(GS->getBody());
1005+
assignKnownCounter(GS->getBody());
10171006

10181007
} else if (auto *WS = dyn_cast<WhileStmt>(S)) {
10191008
// The counter for the while statement itself tracks the number of jumps
@@ -1022,22 +1011,22 @@ struct CoverageMapping : public ASTWalker {
10221011

10231012
if (auto *E = getConditionNode(WS->getCond()))
10241013
assignCounter(E, getCurrentCounter());
1025-
assignCounter(WS->getBody());
1014+
assignKnownCounter(WS->getBody());
10261015

10271016
} else if (auto *RWS = dyn_cast<RepeatWhileStmt>(S)) {
10281017
// The counter for the while statement itself tracks the number of jumps
10291018
// to it by break and continue statements.
10301019
assignCounter(RWS, CounterExpr::Zero());
10311020

1032-
auto BodyCounter = assignCounter(RWS->getBody());
1021+
auto BodyCounter = assignKnownCounter(RWS->getBody());
10331022
assignCounter(RWS->getCond(), BodyCounter);
10341023
RepeatWhileStack.push_back(RWS);
10351024

10361025
} else if (auto *FES = dyn_cast<ForEachStmt>(S)) {
10371026
// The counter for the for statement itself tracks the number of jumps
10381027
// to it by break and continue statements.
10391028
assignCounter(FES, CounterExpr::Zero());
1040-
assignCounter(FES->getBody());
1029+
assignKnownCounter(FES->getBody());
10411030

10421031
} else if (auto *SS = dyn_cast<SwitchStmt>(S)) {
10431032
// The counter for the switch statement itself tracks the number of jumps
@@ -1049,7 +1038,7 @@ struct CoverageMapping : public ASTWalker {
10491038

10501039
// Assign counters for cases so they're available for fallthrough.
10511040
for (CaseStmt *Case : SS->getCases())
1052-
assignCounter(Case);
1041+
assignKnownCounter(Case);
10531042

10541043
} else if (auto caseStmt = dyn_cast<CaseStmt>(S)) {
10551044
if (caseStmt->getParentKind() == CaseParentKind::Switch)
@@ -1066,7 +1055,7 @@ struct CoverageMapping : public ASTWalker {
10661055
assignCounter(DCS->getBody(), getCurrentCounter());
10671056

10681057
for (CaseStmt *Catch : DCS->getCatches())
1069-
assignCounter(Catch->getBody());
1058+
assignKnownCounter(Catch->getBody());
10701059

10711060
// Initialize the exit count of the do-catch to the entry count, then
10721061
// subtract off non-local exits as they are visited.
@@ -1183,19 +1172,19 @@ struct CoverageMapping : public ASTWalker {
11831172
if (Parent.isNull()) {
11841173
assert(RegionStack.empty() &&
11851174
"Mapped a region before visiting the root?");
1186-
assignCounter(E);
1175+
assignKnownCounter(E);
11871176
}
11881177

11891178
if (isa<LazyInitializerExpr>(E))
1190-
assignCounter(E);
1179+
assignKnownCounter(E);
11911180

11921181
if (hasCounter(E))
11931182
pushRegion(E);
11941183

11951184
assert(!RegionStack.empty() && "Must be within a region");
11961185

11971186
if (auto *IE = dyn_cast<TernaryExpr>(E)) {
1198-
auto ThenCounter = assignCounter(IE->getThenExpr());
1187+
auto ThenCounter = assignKnownCounter(IE->getThenExpr());
11991188
auto ElseCounter =
12001189
CounterExpr::Sub(getCurrentCounter(), ThenCounter, CounterAlloc);
12011190
assignCounter(IE->getElseExpr(), ElseCounter);
@@ -1239,17 +1228,17 @@ getEquivalentPGOLinkage(FormalLinkage Linkage) {
12391228
llvm_unreachable("Unhandled FormalLinkage in switch.");
12401229
}
12411230

1242-
static StringRef getCurrentFileName(ASTNode N, SILDeclRef forDecl) {
1243-
DeclContext *Ctx = getProfilerContextForDecl(N, forDecl);
1244-
if (auto *ParentFile = Ctx->getParentSourceFile())
1231+
static StringRef getCurrentFileName(SILDeclRef forDecl) {
1232+
auto *DC = forDecl.getInnermostDeclContext();
1233+
if (auto *ParentFile = DC->getParentSourceFile())
12451234
return ParentFile->getFilename();
12461235
return {};
12471236
}
12481237

12491238
void SILProfiler::assignRegionCounters() {
12501239
const auto &SM = M.getASTContext().SourceMgr;
12511240

1252-
CurrentFileName = getCurrentFileName(Root, forDecl);
1241+
CurrentFileName = getCurrentFileName(forDecl);
12531242

12541243
MapRegionCounters Mapper(forDecl, RegionCounterMap);
12551244

0 commit comments

Comments
 (0)