Skip to content

Commit 87251e0

Browse files
authored
Merge pull request #61108 from hamishknight/even-more-lazy
2 parents 25f6467 + d01fdc9 commit 87251e0

File tree

6 files changed

+48
-34
lines changed

6 files changed

+48
-34
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,7 @@ class SILFunction
562562
Profiler = InheritedProfiler;
563563
}
564564

565-
void createProfiler(ASTNode Root, SILDeclRef forDecl,
566-
ForDefinition_t forDefinition);
565+
void createProfiler(ASTNode Root, SILDeclRef Ref);
567566

568567
void discardProfiler() { Profiler = nullptr; }
569568

include/swift/SIL/SILProfiler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ class SILProfiler : public SILAllocated<SILProfiler> {
6969
EmitCoverageMapping(EmitCoverageMapping) {}
7070

7171
public:
72-
static SILProfiler *create(SILModule &M, ForDefinition_t forDefinition,
73-
ASTNode N, SILDeclRef forDecl);
72+
static SILProfiler *create(SILModule &M, ASTNode N, SILDeclRef Ref);
7473

7574
/// Check if the function is set up for profiling.
7675
bool hasRegionCounters() const { return NumRegionCounters != 0; }

lib/SIL/IR/SILFunction.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,9 @@ void SILFunction::deleteSnapshot(int ID) {
322322
} while ((f = f->snapshots) != nullptr);
323323
}
324324

325-
void SILFunction::createProfiler(ASTNode Root, SILDeclRef forDecl,
326-
ForDefinition_t forDefinition) {
325+
void SILFunction::createProfiler(ASTNode Root, SILDeclRef Ref) {
327326
assert(!Profiler && "Function already has a profiler");
328-
Profiler = SILProfiler::create(Module, forDefinition, Root, forDecl);
327+
Profiler = SILProfiler::create(Module, Root, Ref);
329328
}
330329

331330
bool SILFunction::hasForeignBody() const {

lib/SIL/IR/SILProfiler.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ static bool isUnmapped(ASTNode N) {
7777
return true;
7878
}
7979

80-
// Map implicit getters.
81-
if (auto *accessor = dyn_cast<AccessorDecl>(AFD))
82-
if (accessor->isImplicit() && accessor->isGetter())
80+
// Map implicit getters for lazy variables.
81+
if (auto *accessor = dyn_cast<AccessorDecl>(AFD)) {
82+
if (accessor->isImplicit() && accessor->isGetter() &&
83+
accessor->getStorage()->getAttrs().hasAttribute<LazyAttr>()) {
8384
return false;
85+
}
86+
}
8487
}
8588

8689
// Skip any remaining implicit, or otherwise unsupported decls.
@@ -147,24 +150,19 @@ static bool canCreateProfilerForAST(ASTNode N, SILDeclRef forDecl) {
147150
return false;
148151
}
149152

150-
SILProfiler *SILProfiler::create(SILModule &M, ForDefinition_t forDefinition,
151-
ASTNode N, SILDeclRef forDecl) {
152-
// Avoid generating profiling state for declarations.
153-
if (!forDefinition)
154-
return nullptr;
155-
153+
SILProfiler *SILProfiler::create(SILModule &M, ASTNode N, SILDeclRef Ref) {
156154
const auto &Opts = M.getOptions();
157155
if (!doesASTRequireProfiling(M, N) && Opts.UseProfile.empty())
158156
return nullptr;
159157

160-
if (!canCreateProfilerForAST(N, forDecl)) {
158+
if (!canCreateProfilerForAST(N, Ref)) {
161159
N.dump(llvm::errs());
162160
llvm_unreachable("Invalid AST node for profiling");
163161
}
164162

165163
auto *Buf = M.allocate<SILProfiler>(1);
166164
auto *SP =
167-
::new (Buf) SILProfiler(M, N, forDecl, Opts.EmitProfileCoverageMapping);
165+
::new (Buf) SILProfiler(M, N, Ref, Opts.EmitProfileCoverageMapping);
168166
SP->assignRegionCounters();
169167
return SP;
170168
}

lib/SILGen/SILGen.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -667,12 +667,12 @@ static void setUpForProfiling(SILDeclRef constant, SILFunction *F,
667667
if (constant.hasDecl()) {
668668
if (auto *fd = constant.getFuncDecl()) {
669669
if (fd->hasBody()) {
670-
F->createProfiler(fd, constant, forDefinition);
670+
F->createProfiler(fd, constant);
671671
profiledNode = fd->getBody(/*canSynthesize=*/false);
672672
}
673673
}
674674
} else if (auto *ace = constant.getAbstractClosureExpr()) {
675-
F->createProfiler(ace, constant, forDefinition);
675+
F->createProfiler(ace, constant);
676676
profiledNode = ace;
677677
}
678678
// Set the function entry count for PGO.
@@ -914,7 +914,7 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
914914
} else {
915915
preEmitFunction(constant, f, decl);
916916
PrettyStackTraceSILFunction X("silgen emitConstructor", f);
917-
f->createProfiler(decl, constant, ForDefinition);
917+
f->createProfiler(decl, constant);
918918
SILGenFunction(*this, *f, decl).emitValueConstructor(decl);
919919
postEmitFunction(constant, f);
920920
}
@@ -927,7 +927,7 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
927927

928928
preEmitFunction(constant, f, decl);
929929
PrettyStackTraceSILFunction X("silgen constructor initializer", f);
930-
f->createProfiler(decl, constant, ForDefinition);
930+
f->createProfiler(decl, constant);
931931
SILGenFunction(*this, *f, decl).emitClassConstructorInitializer(decl);
932932
postEmitFunction(constant, f);
933933
break;
@@ -993,7 +993,7 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
993993
auto loc = RegularLocation::getAutoGeneratedLocation(init);
994994
preEmitFunction(constant, f, loc);
995995
PrettyStackTraceSILFunction X("silgen emitStoredPropertyInitialization", f);
996-
f->createProfiler(init, constant, ForDefinition);
996+
f->createProfiler(init, constant);
997997
SILGenFunction SGF(*this, *f, initDC);
998998

999999
// If this is a stored property initializer inside a type at global scope,
@@ -1020,8 +1020,7 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
10201020
"silgen emitPropertyWrapperBackingInitializer", f);
10211021
auto wrapperInfo = var->getPropertyWrapperInitializerInfo();
10221022
assert(wrapperInfo.hasInitFromWrappedValue());
1023-
f->createProfiler(wrapperInfo.getInitFromWrappedValue(), constant,
1024-
ForDefinition);
1023+
f->createProfiler(wrapperInfo.getInitFromWrappedValue(), constant);
10251024
auto varDC = var->getInnermostDeclContext();
10261025
SILGenFunction SGF(*this, *f, varDC);
10271026
SGF.emitGeneratorFunction(constant, wrapperInfo.getInitFromWrappedValue());
@@ -1038,8 +1037,7 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
10381037
"silgen emitPropertyWrapperInitFromProjectedValue", f);
10391038
auto initInfo = var->getPropertyWrapperInitializerInfo();
10401039
assert(initInfo.hasInitFromProjectedValue());
1041-
f->createProfiler(initInfo.getInitFromProjectedValue(), constant,
1042-
ForDefinition);
1040+
f->createProfiler(initInfo.getInitFromProjectedValue(), constant);
10431041
auto varDC = var->getInnermostDeclContext();
10441042
SILGenFunction SGF(*this, *f, varDC);
10451043
SGF.emitGeneratorFunction(constant, initInfo.getInitFromProjectedValue());
@@ -1079,7 +1077,7 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
10791077
auto *dd = cast<DestructorDecl>(constant.getDecl());
10801078
preEmitFunction(constant, f, dd);
10811079
PrettyStackTraceSILFunction X("silgen emitDestroyingDestructor", f);
1082-
f->createProfiler(dd, constant, ForDefinition);
1080+
f->createProfiler(dd, constant);
10831081
SILGenFunction(*this, *f, dd).emitDestroyingDestructor(dd);
10841082
postEmitFunction(constant, f);
10851083
return;
@@ -1092,7 +1090,7 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
10921090
if (usesObjCAllocator(cd)) {
10931091
preEmitFunction(constant, f, dd);
10941092
PrettyStackTraceSILFunction X("silgen emitDestructor -dealloc", f);
1095-
f->createProfiler(dd, constant, ForDefinition);
1093+
f->createProfiler(dd, constant);
10961094
SILGenFunction(*this, *f, dd).emitObjCDestructor(constant);
10971095
postEmitFunction(constant, f);
10981096
return;
@@ -1461,8 +1459,6 @@ void SILGenModule::emitConstructor(ConstructorDecl *decl) {
14611459
SILDeclRef constant(decl);
14621460
DeclContext *declCtx = decl->getDeclContext();
14631461

1464-
bool ForCoverageMapping = doesASTRequireProfiling(M, decl);
1465-
14661462
if (declCtx->getSelfClassDecl()) {
14671463
// Designated initializers for classes, as well as @objc convenience
14681464
// initializers, have have separate entry points for allocation and
@@ -1472,8 +1468,7 @@ void SILGenModule::emitConstructor(ConstructorDecl *decl) {
14721468

14731469
if (decl->hasBody()) {
14741470
SILDeclRef initConstant(decl, SILDeclRef::Kind::Initializer);
1475-
emitOrDelayFunction(*this, initConstant,
1476-
/*forceEmission=*/ForCoverageMapping);
1471+
emitOrDelayFunction(*this, initConstant);
14771472
}
14781473

14791474
return;
@@ -1954,7 +1949,7 @@ void SILGenModule::visitTopLevelCodeDecl(TopLevelCodeDecl *td) {
19541949
// A single SILFunction may be used to lower multiple top-level decls. When
19551950
// this happens, fresh profile counters must be assigned to the new decl.
19561951
TopLevelSGF->F.discardProfiler();
1957-
TopLevelSGF->F.createProfiler(td, SILDeclRef(), ForDefinition);
1952+
TopLevelSGF->F.createProfiler(td, SILDeclRef());
19581953

19591954
TopLevelSGF->emitProfilerIncrement(td->getBody());
19601955

test/Profiler/unmapped.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -suppress-warnings -profile-generate -profile-coverage-mapping -emit-sorted-sil -emit-sil -module-name unmapped %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -profile-generate -profile-coverage-mapping -emit-ir %s
3+
4+
// This test is exclusively for AST that we should never profile, as there is
5+
// no interesting user-written code.
6+
7+
// CHECK-NOT: increment_profiler_counter
8+
// CHECK-NOT: sil_coverage_map
9+
10+
struct S {
11+
// Don't profile the implicit accessor, or the implicit constructor.
12+
var x: Int
13+
}
14+
15+
// Don't profile any synthesized codable methods.
16+
struct R : Codable {
17+
var x: String
18+
var y: Int
19+
}
20+
21+
// Don't profile the implicit rawValue.
22+
enum E : Int {
23+
case a
24+
}

0 commit comments

Comments
 (0)