@@ -59,17 +59,6 @@ static bool shouldProfile(ASTNode N, SILDeclRef Constant) {
59
59
return true ;
60
60
}
61
61
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
-
73
62
static Stmt *getProfilerStmtForCase (CaseStmt *caseStmt) {
74
63
switch (caseStmt->getParentKind ()) {
75
64
case CaseParentKind::Switch:
@@ -82,15 +71,14 @@ static Stmt *getProfilerStmtForCase(CaseStmt *caseStmt) {
82
71
83
72
// / Check that the input AST has at least been type-checked.
84
73
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;
89
77
}
90
78
91
79
// / Check whether a mapped AST node is valid for profiling.
92
80
static bool canCreateProfilerForAST (ASTNode N, SILDeclRef forDecl) {
93
- assert (hasASTBeenTypeChecked (N, forDecl) &&
81
+ assert (hasFileBeenTypeChecked ( forDecl) &&
94
82
" Cannot use this AST for profiling" );
95
83
96
84
if (auto *D = N.dyn_cast <Decl *>()) {
@@ -757,8 +745,9 @@ struct CoverageMapping : public ASTWalker {
757
745
Res.first ->second = std::move (Expr);
758
746
}
759
747
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) {
762
751
auto Counter = CounterExpr::Leaf (Node);
763
752
assignCounter (Node, Counter);
764
753
return Counter;
@@ -966,10 +955,10 @@ struct CoverageMapping : public ASTWalker {
966
955
PreWalkAction walkToDeclPre (Decl *D) override {
967
956
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
968
957
return visitFunctionDecl (*this , AFD, [&] {
969
- assignCounter (AFD->getBody ());
958
+ assignKnownCounter (AFD->getBody ());
970
959
});
971
960
} else if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
972
- assignCounter (TLCD->getBody ());
961
+ assignKnownCounter (TLCD->getBody ());
973
962
ImplicitTopLevelBody = TLCD->getBody ();
974
963
return Action::Continue ();
975
964
}
@@ -1005,15 +994,15 @@ struct CoverageMapping : public ASTWalker {
1005
994
1006
995
// We emit a counter for the then block, and define the else block in
1007
996
// terms of it.
1008
- auto ThenCounter = assignCounter (IS->getThenStmt ());
997
+ auto ThenCounter = assignKnownCounter (IS->getThenStmt ());
1009
998
if (IS->getElseStmt ()) {
1010
999
auto ElseCounter =
1011
1000
CounterExpr::Sub (getCurrentCounter (), ThenCounter, CounterAlloc);
1012
1001
assignCounter (IS->getElseStmt (), ElseCounter);
1013
1002
}
1014
1003
} else if (auto *GS = dyn_cast<GuardStmt>(S)) {
1015
1004
assignCounter (GS, CounterExpr::Zero ());
1016
- assignCounter (GS->getBody ());
1005
+ assignKnownCounter (GS->getBody ());
1017
1006
1018
1007
} else if (auto *WS = dyn_cast<WhileStmt>(S)) {
1019
1008
// The counter for the while statement itself tracks the number of jumps
@@ -1022,22 +1011,22 @@ struct CoverageMapping : public ASTWalker {
1022
1011
1023
1012
if (auto *E = getConditionNode (WS->getCond ()))
1024
1013
assignCounter (E, getCurrentCounter ());
1025
- assignCounter (WS->getBody ());
1014
+ assignKnownCounter (WS->getBody ());
1026
1015
1027
1016
} else if (auto *RWS = dyn_cast<RepeatWhileStmt>(S)) {
1028
1017
// The counter for the while statement itself tracks the number of jumps
1029
1018
// to it by break and continue statements.
1030
1019
assignCounter (RWS, CounterExpr::Zero ());
1031
1020
1032
- auto BodyCounter = assignCounter (RWS->getBody ());
1021
+ auto BodyCounter = assignKnownCounter (RWS->getBody ());
1033
1022
assignCounter (RWS->getCond (), BodyCounter);
1034
1023
RepeatWhileStack.push_back (RWS);
1035
1024
1036
1025
} else if (auto *FES = dyn_cast<ForEachStmt>(S)) {
1037
1026
// The counter for the for statement itself tracks the number of jumps
1038
1027
// to it by break and continue statements.
1039
1028
assignCounter (FES, CounterExpr::Zero ());
1040
- assignCounter (FES->getBody ());
1029
+ assignKnownCounter (FES->getBody ());
1041
1030
1042
1031
} else if (auto *SS = dyn_cast<SwitchStmt>(S)) {
1043
1032
// The counter for the switch statement itself tracks the number of jumps
@@ -1049,7 +1038,7 @@ struct CoverageMapping : public ASTWalker {
1049
1038
1050
1039
// Assign counters for cases so they're available for fallthrough.
1051
1040
for (CaseStmt *Case : SS->getCases ())
1052
- assignCounter (Case);
1041
+ assignKnownCounter (Case);
1053
1042
1054
1043
} else if (auto caseStmt = dyn_cast<CaseStmt>(S)) {
1055
1044
if (caseStmt->getParentKind () == CaseParentKind::Switch)
@@ -1066,7 +1055,7 @@ struct CoverageMapping : public ASTWalker {
1066
1055
assignCounter (DCS->getBody (), getCurrentCounter ());
1067
1056
1068
1057
for (CaseStmt *Catch : DCS->getCatches ())
1069
- assignCounter (Catch->getBody ());
1058
+ assignKnownCounter (Catch->getBody ());
1070
1059
1071
1060
// Initialize the exit count of the do-catch to the entry count, then
1072
1061
// subtract off non-local exits as they are visited.
@@ -1183,19 +1172,19 @@ struct CoverageMapping : public ASTWalker {
1183
1172
if (Parent.isNull ()) {
1184
1173
assert (RegionStack.empty () &&
1185
1174
" Mapped a region before visiting the root?" );
1186
- assignCounter (E);
1175
+ assignKnownCounter (E);
1187
1176
}
1188
1177
1189
1178
if (isa<LazyInitializerExpr>(E))
1190
- assignCounter (E);
1179
+ assignKnownCounter (E);
1191
1180
1192
1181
if (hasCounter (E))
1193
1182
pushRegion (E);
1194
1183
1195
1184
assert (!RegionStack.empty () && " Must be within a region" );
1196
1185
1197
1186
if (auto *IE = dyn_cast<TernaryExpr>(E)) {
1198
- auto ThenCounter = assignCounter (IE->getThenExpr ());
1187
+ auto ThenCounter = assignKnownCounter (IE->getThenExpr ());
1199
1188
auto ElseCounter =
1200
1189
CounterExpr::Sub (getCurrentCounter (), ThenCounter, CounterAlloc);
1201
1190
assignCounter (IE->getElseExpr (), ElseCounter);
@@ -1239,17 +1228,17 @@ getEquivalentPGOLinkage(FormalLinkage Linkage) {
1239
1228
llvm_unreachable (" Unhandled FormalLinkage in switch." );
1240
1229
}
1241
1230
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 ())
1245
1234
return ParentFile->getFilename ();
1246
1235
return {};
1247
1236
}
1248
1237
1249
1238
void SILProfiler::assignRegionCounters () {
1250
1239
const auto &SM = M.getASTContext ().SourceMgr ;
1251
1240
1252
- CurrentFileName = getCurrentFileName (Root, forDecl);
1241
+ CurrentFileName = getCurrentFileName (forDecl);
1253
1242
1254
1243
MapRegionCounters Mapper (forDecl, RegionCounterMap);
1255
1244
0 commit comments