Skip to content

Commit 0eba486

Browse files
committed
[SILGen] Remove setUpForProfiling
Instead, setup the profilers when emitting function definitions. This is consistent with what we do for all the other kinds of SILDeclRef.
1 parent 5b1ca3f commit 0eba486

File tree

2 files changed

+18
-35
lines changed

2 files changed

+18
-35
lines changed

lib/SIL/IR/SILFunction.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,20 @@ void SILFunction::deleteSnapshot(int ID) {
335335
void SILFunction::createProfiler(ASTNode Root, SILDeclRef Ref) {
336336
assert(!Profiler && "Function already has a profiler");
337337
Profiler = SILProfiler::create(Module, Root, Ref);
338+
if (!Profiler || Ref.isNull())
339+
return;
340+
341+
// If we loaded a profile, set the entry counts for functions and closures
342+
// for PGO to use.
343+
if (Ref.isFunc()) {
344+
if (auto *Closure = Ref.getAbstractClosureExpr()) {
345+
setEntryCount(Profiler->getExecutionCount(Closure));
346+
} else {
347+
auto *FD = Ref.getFuncDecl();
348+
assert(FD);
349+
setEntryCount(Profiler->getExecutionCount(FD->getBody()));
350+
}
351+
}
338352
}
339353

340354
bool SILFunction::hasForeignBody() const {

lib/SILGen/SILGen.cpp

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -654,35 +654,6 @@ static SILFunction *getFunctionToInsertAfter(SILGenModule &SGM,
654654
return nullptr;
655655
}
656656

657-
static bool haveProfiledAssociatedFunction(SILDeclRef constant) {
658-
return constant.isDefaultArgGenerator() || constant.isForeign;
659-
}
660-
661-
/// Set up the function for profiling instrumentation.
662-
static void setUpForProfiling(SILDeclRef constant, SILFunction *F,
663-
ForDefinition_t forDefinition) {
664-
if (!forDefinition || F->getProfiler())
665-
return;
666-
667-
ASTNode profiledNode;
668-
if (!haveProfiledAssociatedFunction(constant)) {
669-
if (constant.hasDecl()) {
670-
if (auto *fd = constant.getFuncDecl()) {
671-
if (fd->hasBody()) {
672-
F->createProfiler(fd, constant);
673-
profiledNode = fd->getBody(/*canSynthesize=*/false);
674-
}
675-
}
676-
} else if (auto *ace = constant.getAbstractClosureExpr()) {
677-
F->createProfiler(ace, constant);
678-
profiledNode = ace;
679-
}
680-
// Set the function entry count for PGO.
681-
if (SILProfiler *SP = F->getProfiler())
682-
F->setEntryCount(SP->getExecutionCount(profiledNode));
683-
}
684-
}
685-
686657
static bool isEmittedOnDemand(SILModule &M, SILDeclRef constant) {
687658
if (!constant.hasDecl())
688659
return false;
@@ -730,12 +701,9 @@ static bool isEmittedOnDemand(SILModule &M, SILDeclRef constant) {
730701

731702
SILFunction *SILGenModule::getFunction(SILDeclRef constant,
732703
ForDefinition_t forDefinition) {
733-
// If we already emitted the function, return it (potentially preparing it
734-
// for definition).
735-
if (auto emitted = getEmittedFunction(constant, forDefinition)) {
736-
setUpForProfiling(constant, emitted, forDefinition);
704+
// If we already emitted the function, return it.
705+
if (auto emitted = getEmittedFunction(constant, forDefinition))
737706
return emitted;
738-
}
739707

740708
auto getBestLocation = [](SILDeclRef ref) -> SILLocation {
741709
if (ref.hasDecl())
@@ -755,7 +723,6 @@ SILFunction *SILGenModule::getFunction(SILDeclRef constant,
755723
[&IGM](SILLocation loc, SILDeclRef constant) -> SILFunction * {
756724
return IGM.getFunction(constant, NotForDefinition);
757725
});
758-
setUpForProfiling(constant, F, forDefinition);
759726

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

@@ -889,6 +856,7 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
889856
if (auto *ce = constant.getAbstractClosureExpr()) {
890857
preEmitFunction(constant, f, ce);
891858
PrettyStackTraceSILFunction X("silgen closureexpr", f);
859+
f->createProfiler(ce, constant);
892860
SILGenFunction(*this, *f, ce).emitClosure(ce);
893861
postEmitFunction(constant, f);
894862
break;
@@ -898,6 +866,7 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
898866

899867
preEmitFunction(constant, f, fd);
900868
PrettyStackTraceSILFunction X("silgen emitFunction", f);
869+
f->createProfiler(fd, constant);
901870
SILGenFunction(*this, *f, fd).emitFunction(fd);
902871
postEmitFunction(constant, f);
903872
break;

0 commit comments

Comments
 (0)