Skip to content

[SILProfiler] Set up profiling for lazily-emitted functions #22403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,20 +606,12 @@ SILGenModule::getOrCreateProfilerForConstructors(DeclContext *ctx,
return profiler;
}

SILFunction *SILGenModule::getFunction(SILDeclRef constant,
ForDefinition_t forDefinition) {
// If we already emitted the function, return it (potentially preparing it
// for definition).
if (auto emitted = getEmittedFunction(constant, forDefinition))
return emitted;

// Note: Do not provide any SILLocation. You can set it afterwards.
SILGenFunctionBuilder builder(*this);
auto *F = builder.getOrCreateFunction(constant.hasDecl() ? constant.getDecl()
: (Decl *)nullptr,
constant, forDefinition);
/// Set up the function for profiling instrumentation.
static void setUpForProfiling(SILDeclRef constant, SILFunction *F,
ForDefinition_t forDefinition) {
if (!forDefinition)
return;

// Set up the function for profiling instrumentation.
ASTNode profiledNode;
if (!haveProfiledAssociatedFunction(constant)) {
if (constant.hasDecl()) {
Expand All @@ -637,6 +629,23 @@ SILFunction *SILGenModule::getFunction(SILDeclRef constant,
if (SILProfiler *SP = F->getProfiler())
F->setEntryCount(SP->getExecutionCount(profiledNode));
}
}

SILFunction *SILGenModule::getFunction(SILDeclRef constant,
ForDefinition_t forDefinition) {
// If we already emitted the function, return it (potentially preparing it
// for definition).
if (auto emitted = getEmittedFunction(constant, forDefinition)) {
setUpForProfiling(constant, emitted, forDefinition);
return emitted;
}

// Note: Do not provide any SILLocation. You can set it afterwards.
SILGenFunctionBuilder builder(*this);
auto *F = builder.getOrCreateFunction(constant.hasDecl() ? constant.getDecl()
: (Decl *)nullptr,
constant, forDefinition);
setUpForProfiling(constant, F, forDefinition);

assert(F && "SILFunction should have been defined");

Expand Down
14 changes: 14 additions & 0 deletions test/Profiler/coverage_private.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -profile-generate -profile-coverage-mapping -emit-sorted-sil -emit-sil -module-name coverage_private %s | %FileCheck %s

struct S {
func visible() {
hidden()
}

// CHECK-LABEL: sil_coverage_map {{.*}}// coverage_private.S.(hidden in {{.*}})() -> ()
// CHECK-NEXT: [[@LINE+1]]:25 -> [[@LINE+2]]:4 : 0
private func hidden() {
}
}

S().visible()
16 changes: 16 additions & 0 deletions test/Profiler/coverage_smoke.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,20 @@ let _ = Class3()
let _ = Struct1(field: 1)
let _ = Struct1()

struct Struct2 {
func visible() {
hidden()
}
private func hidden() {
var x: Int = 0 // CHECK-COV: {{ *}}[[@LINE]]|{{ *}}1
func helper() {
x += 1 // CHECK-COV: {{ *}}[[@LINE]]|{{ *}}1
}
helper() // CHECK-COV: {{ *}}[[@LINE]]|{{ *}}1
}
}

var s2 = Struct2()
s2.visible()

// CHECK-REPORT: TOTAL {{.*}} 100.00% {{.*}} 100.00%