Skip to content

Commit 30568a6

Browse files
committed
[Profiler] Don't run PGO on unmapped decls
This avoids an unnecessary walk, as we don't have any interesting profile data for such decls.
1 parent 87251e0 commit 30568a6

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/SIL/IR/SILProfiler.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ static bool isUnmapped(ASTNode N) {
9797

9898
namespace swift {
9999
bool doesASTRequireProfiling(SILModule &M, ASTNode N) {
100-
return M.getOptions().GenerateProfile && !isUnmapped(N);
100+
// If profiling isn't enabled, don't profile anything.
101+
auto &Opts = M.getOptions();
102+
if (Opts.UseProfile.empty() && !Opts.GenerateProfile)
103+
return false;
104+
105+
return !isUnmapped(N);
101106
}
102107
} // namespace swift
103108

@@ -152,7 +157,7 @@ static bool canCreateProfilerForAST(ASTNode N, SILDeclRef forDecl) {
152157

153158
SILProfiler *SILProfiler::create(SILModule &M, ASTNode N, SILDeclRef Ref) {
154159
const auto &Opts = M.getOptions();
155-
if (!doesASTRequireProfiling(M, N) && Opts.UseProfile.empty())
160+
if (!doesASTRequireProfiling(M, N))
156161
return nullptr;
157162

158163
if (!canCreateProfilerForAST(N, Ref)) {

test/Profiler/pgo_with_coverage.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -profile-generate -Xfrontend -disable-incremental-llvm-codegen -module-name pgo_with_coverage -o %t/main
3+
4+
// This unusual use of 'sh' allows the path of the profraw file to be
5+
// substituted by %target-run.
6+
// RUN: %target-codesign %t/main
7+
// RUN: %target-run sh -c 'env LLVM_PROFILE_FILE=$1 $2' -- %t/default.profraw %t/main
8+
9+
// RUN: %llvm-profdata merge %t/default.profraw -o %t/default.profdata
10+
11+
// RUN: %target-swift-frontend %s -Xllvm -sil-full-demangle -profile-generate -profile-coverage-mapping -profile-use=%t/default.profdata -emit-ir -module-name pgo_with_coverage | %FileCheck %s --check-prefix=IR
12+
// RUN: %target-swift-frontend %s -Xllvm -sil-full-demangle -profile-generate -profile-coverage-mapping -profile-use=%t/default.profdata -O -emit-ir -module-name pgo_with_coverage | %FileCheck %s --check-prefix=IR
13+
14+
// REQUIRES: profile_runtime
15+
// REQUIRES: executable_test
16+
// REQUIRES: OS=macosx
17+
18+
// Make sure we don't try and profile implicit decls during PGO.
19+
// IR-NOT: no profile data available
20+
21+
struct S {
22+
var x = .random() ? 2 : 3
23+
}
24+
print(S().x)

0 commit comments

Comments
 (0)