@@ -42,13 +42,30 @@ static bool doesClosureHaveBody(AbstractClosureExpr *ACE) {
4242}
4343
4444// / Check whether a root AST node should be profiled.
45- static bool shouldProfile (ASTNode N) {
45+ static bool shouldProfile (ASTNode N, SILDeclRef Constant ) {
4646 // Do not profile AST nodes with invalid source locations.
4747 if (N.getStartLoc ().isInvalid () || N.getEndLoc ().isInvalid ()) {
4848 LLVM_DEBUG (llvm::dbgs ()
4949 << " Skipping ASTNode: invalid start/end locations\n " );
5050 return false ;
5151 }
52+ if (!Constant) {
53+ // We should only ever have a null SILDeclRef for top-level code, which is
54+ // always user-written, and should always be profiled.
55+ // FIXME: Once top-level code is unified under a single SILProfiler, this
56+ // case can be eliminated.
57+ assert (isa<TopLevelCodeDecl>(N.get <Decl *>()));
58+ return true ;
59+ }
60+
61+ // Do not profile AST nodes in unavailable contexts.
62+ auto *DC = Constant.getInnermostDeclContext ();
63+ if (auto *D = DC->getInnermostDeclarationDeclContext ()) {
64+ if (D->isSemanticallyUnavailable ()) {
65+ LLVM_DEBUG (llvm::dbgs () << " Skipping ASTNode: unavailable context\n " );
66+ return false ;
67+ }
68+ }
5269
5370 if (auto *E = N.dyn_cast <Expr *>()) {
5471 if (auto *CE = dyn_cast<AbstractClosureExpr>(E)) {
@@ -96,13 +113,13 @@ static bool shouldProfile(ASTNode N) {
96113}
97114
98115namespace swift {
99- bool doesASTRequireProfiling (SILModule &M, ASTNode N) {
116+ bool doesASTRequireProfiling (SILModule &M, ASTNode N, SILDeclRef Constant ) {
100117 // If profiling isn't enabled, don't profile anything.
101118 auto &Opts = M.getOptions ();
102119 if (Opts.UseProfile .empty () && !Opts.GenerateProfile )
103120 return false ;
104121
105- return shouldProfile (N);
122+ return shouldProfile (N, Constant );
106123}
107124} // namespace swift
108125
@@ -157,7 +174,7 @@ static bool canCreateProfilerForAST(ASTNode N, SILDeclRef forDecl) {
157174
158175SILProfiler *SILProfiler::create (SILModule &M, ASTNode N, SILDeclRef Ref) {
159176 const auto &Opts = M.getOptions ();
160- if (!doesASTRequireProfiling (M, N))
177+ if (!doesASTRequireProfiling (M, N, Ref ))
161178 return nullptr ;
162179
163180 if (!canCreateProfilerForAST (N, Ref)) {
0 commit comments