Skip to content

Commit ac63abc

Browse files
committed
[SILGen] Consolidate emission of SILDeclRef definitions
Fill in the missing SILDeclRef cases in `emitDelayedFunction`, and rename it to `emitFunctionDefinition`. This will allow SIL to be emitted only for a specific set of symbols.
1 parent 789659e commit ac63abc

File tree

2 files changed

+103
-85
lines changed

2 files changed

+103
-85
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 99 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -676,17 +676,15 @@ bool SILGenModule::hasFunction(SILDeclRef constant) {
676676

677677
void SILGenModule::visitFuncDecl(FuncDecl *fd) { emitFunction(fd); }
678678

679-
static void emitDelayedFunction(SILGenModule &SGM,
680-
SILDeclRef constant,
681-
SILFunction *f) {
679+
void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
682680
switch (constant.kind) {
683681
case SILDeclRef::Kind::Func: {
684682
auto *fd = cast<FuncDecl>(constant.getDecl());
685683

686-
SGM.preEmitFunction(constant, fd, f, fd);
684+
preEmitFunction(constant, fd, f, fd);
687685
PrettyStackTraceSILFunction X("silgen emitFunction", f);
688-
SILGenFunction(SGM, *f, fd).emitFunction(fd);
689-
SGM.postEmitFunction(constant, f);
686+
SILGenFunction(*this, *f, fd).emitFunction(fd);
687+
postEmitFunction(constant, f);
690688
break;
691689
}
692690

@@ -696,16 +694,16 @@ static void emitDelayedFunction(SILGenModule &SGM,
696694
if (decl->getDeclContext()->getSelfClassDecl() &&
697695
(decl->isDesignatedInit() ||
698696
decl->isObjC())) {
699-
SGM.preEmitFunction(constant, decl, f, decl);
697+
preEmitFunction(constant, decl, f, decl);
700698
PrettyStackTraceSILFunction X("silgen emitConstructor", f);
701-
SILGenFunction(SGM, *f, decl).emitClassConstructorAllocator(decl);
702-
SGM.postEmitFunction(constant, f);
699+
SILGenFunction(*this, *f, decl).emitClassConstructorAllocator(decl);
700+
postEmitFunction(constant, f);
703701
} else {
704-
SGM.preEmitFunction(constant, decl, f, decl);
702+
preEmitFunction(constant, decl, f, decl);
705703
PrettyStackTraceSILFunction X("silgen emitConstructor", f);
706704
f->createProfiler(decl, constant, ForDefinition);
707-
SILGenFunction(SGM, *f, decl).emitValueConstructor(decl);
708-
SGM.postEmitFunction(constant, f);
705+
SILGenFunction(*this, *f, decl).emitValueConstructor(decl);
706+
postEmitFunction(constant, f);
709707
}
710708
break;
711709
}
@@ -714,11 +712,11 @@ static void emitDelayedFunction(SILGenModule &SGM,
714712
auto *decl = cast<ConstructorDecl>(constant.getDecl());
715713
assert(decl->getDeclContext()->getSelfClassDecl());
716714

717-
SGM.preEmitFunction(constant, decl, f, decl);
715+
preEmitFunction(constant, decl, f, decl);
718716
PrettyStackTraceSILFunction X("silgen constructor initializer", f);
719717
f->createProfiler(decl, constant, ForDefinition);
720-
SILGenFunction(SGM, *f, decl).emitClassConstructorInitializer(decl);
721-
SGM.postEmitFunction(constant, f);
718+
SILGenFunction(*this, *f, decl).emitClassConstructorInitializer(decl);
719+
postEmitFunction(constant, f);
722720
break;
723721
}
724722

@@ -731,22 +729,22 @@ static void emitDelayedFunction(SILGenModule &SGM,
731729
case DefaultArgumentKind::Normal: {
732730
auto arg = param->getTypeCheckedDefaultExpr();
733731
auto loc = RegularLocation::getAutoGeneratedLocation(arg);
734-
SGM.preEmitFunction(constant, arg, f, loc);
732+
preEmitFunction(constant, arg, f, loc);
735733
PrettyStackTraceSILFunction X("silgen emitDefaultArgGenerator ", f);
736-
SILGenFunction SGF(SGM, *f, initDC);
734+
SILGenFunction SGF(*this, *f, initDC);
737735
SGF.emitGeneratorFunction(constant, arg);
738-
SGM.postEmitFunction(constant, f);
736+
postEmitFunction(constant, f);
739737
break;
740738
}
741739

742740
case DefaultArgumentKind::StoredProperty: {
743741
auto arg = param->getStoredProperty();
744742
auto loc = RegularLocation::getAutoGeneratedLocation(arg);
745-
SGM.preEmitFunction(constant, arg, f, loc);
743+
preEmitFunction(constant, arg, f, loc);
746744
PrettyStackTraceSILFunction X("silgen emitDefaultArgGenerator ", f);
747-
SILGenFunction SGF(SGM, *f, initDC);
745+
SILGenFunction SGF(*this, *f, initDC);
748746
SGF.emitGeneratorFunction(constant, arg);
749-
SGM.postEmitFunction(constant, f);
747+
postEmitFunction(constant, f);
750748
break;
751749
}
752750

@@ -780,78 +778,122 @@ static void emitDelayedFunction(SILGenModule &SGM,
780778
}
781779

782780
auto loc = RegularLocation::getAutoGeneratedLocation(init);
783-
SGM.preEmitFunction(constant, init, f, loc);
781+
preEmitFunction(constant, init, f, loc);
784782
PrettyStackTraceSILFunction X("silgen emitStoredPropertyInitialization", f);
785783
f->createProfiler(init, constant, ForDefinition);
786-
SILGenFunction SGF(SGM, *f, initDC);
784+
SILGenFunction SGF(*this, *f, initDC);
787785

788786
// If this is a stored property initializer inside a type at global scope,
789787
// it may close over a global variable. If we're emitting top-level code,
790788
// then emit a "mark_function_escape" that lists the captured global
791789
// variables so that definite initialization can reason about this
792790
// escape point.
793-
if (!var->getDeclContext()->isLocalContext() &&
794-
SGM.TopLevelSGF && SGM.TopLevelSGF->B.hasValidInsertionPoint()) {
795-
SGM.emitMarkFunctionEscapeForTopLevelCodeGlobals(var, captureInfo);
791+
if (!var->getDeclContext()->isLocalContext() && TopLevelSGF &&
792+
TopLevelSGF->B.hasValidInsertionPoint()) {
793+
emitMarkFunctionEscapeForTopLevelCodeGlobals(var, captureInfo);
796794
}
797795

798796
SGF.emitGeneratorFunction(constant, init, /*EmitProfilerIncrement=*/true);
799-
SGM.postEmitFunction(constant, f);
797+
postEmitFunction(constant, f);
800798
break;
801799
}
802800

803801
case SILDeclRef::Kind::PropertyWrapperBackingInitializer: {
804802
auto *var = cast<VarDecl>(constant.getDecl());
805803

806804
auto loc = RegularLocation::getAutoGeneratedLocation(var);
807-
SGM.preEmitFunction(constant, var, f, loc);
805+
preEmitFunction(constant, var, f, loc);
808806
PrettyStackTraceSILFunction X(
809807
"silgen emitPropertyWrapperBackingInitializer", f);
810808
auto wrapperInfo = var->getPropertyWrapperBackingPropertyInfo();
811809
assert(wrapperInfo.initializeFromOriginal);
812810
f->createProfiler(wrapperInfo.initializeFromOriginal, constant,
813811
ForDefinition);
814812
auto varDC = var->getInnermostDeclContext();
815-
SILGenFunction SGF(SGM, *f, varDC);
813+
SILGenFunction SGF(*this, *f, varDC);
816814
SGF.emitGeneratorFunction(constant, wrapperInfo.initializeFromOriginal);
817-
SGM.postEmitFunction(constant, f);
815+
postEmitFunction(constant, f);
818816
break;
819817
}
820818

821819
case SILDeclRef::Kind::GlobalAccessor: {
822820
auto *global = cast<VarDecl>(constant.getDecl());
823-
auto found = SGM.delayedGlobals.find(global);
824-
assert(found != SGM.delayedGlobals.end());
821+
auto found = delayedGlobals.find(global);
822+
assert(found != delayedGlobals.end());
825823

826824
auto *onceToken = found->second.first;
827825
auto *onceFunc = found->second.second;
828826

829827
auto loc = RegularLocation::getAutoGeneratedLocation(global);
830-
SGM.preEmitFunction(constant, global, f, loc);
828+
preEmitFunction(constant, global, f, loc);
831829
PrettyStackTraceSILFunction X("silgen emitGlobalAccessor", f);
832-
SILGenFunction(SGM, *f, global->getDeclContext())
830+
SILGenFunction(*this, *f, global->getDeclContext())
833831
.emitGlobalAccessor(global, onceToken, onceFunc);
834-
SGM.postEmitFunction(constant, f);
832+
postEmitFunction(constant, f);
835833
break;
836834
}
837835

838836
case SILDeclRef::Kind::EnumElement: {
839837
auto *decl = cast<EnumElementDecl>(constant.getDecl());
840838

841839
auto loc = RegularLocation::getAutoGeneratedLocation(decl);
842-
SGM.preEmitFunction(constant, decl, f, loc);
840+
preEmitFunction(constant, decl, f, loc);
843841
PrettyStackTraceSILFunction X("silgen enum constructor", f);
844-
SILGenFunction(SGM, *f, decl->getDeclContext()).emitEnumConstructor(decl);
845-
SGM.postEmitFunction(constant, f);
842+
SILGenFunction(*this, *f, decl->getDeclContext()).emitEnumConstructor(decl);
843+
postEmitFunction(constant, f);
846844
break;
847845
}
848846

849-
case SILDeclRef::Kind::Destroyer:
850-
case SILDeclRef::Kind::Deallocator:
851-
case SILDeclRef::Kind::IVarInitializer:
852-
case SILDeclRef::Kind::IVarDestroyer:
853-
llvm_unreachable("Cannot emit as a delayed function");
854-
break;
847+
case SILDeclRef::Kind::Destroyer: {
848+
auto *dd = cast<DestructorDecl>(constant.getDecl());
849+
preEmitFunction(constant, dd, f, dd);
850+
PrettyStackTraceSILFunction X("silgen emitDestroyingDestructor", f);
851+
SILGenFunction(*this, *f, dd).emitDestroyingDestructor(dd);
852+
postEmitFunction(constant, f);
853+
return;
854+
}
855+
856+
case SILDeclRef::Kind::Deallocator: {
857+
auto *dd = cast<DestructorDecl>(constant.getDecl());
858+
auto *cd = cast<ClassDecl>(dd->getDeclContext());
859+
860+
if (usesObjCAllocator(cd)) {
861+
preEmitFunction(constant, dd, f, dd);
862+
PrettyStackTraceSILFunction X("silgen emitDestructor -dealloc", f);
863+
f->createProfiler(dd, constant, ForDefinition);
864+
SILGenFunction(*this, *f, dd).emitObjCDestructor(constant);
865+
postEmitFunction(constant, f);
866+
return;
867+
}
868+
869+
auto loc = RegularLocation::getAutoGeneratedLocation(dd);
870+
preEmitFunction(constant, dd, f, loc);
871+
PrettyStackTraceSILFunction X("silgen emitDeallocatingDestructor", f);
872+
f->createProfiler(dd, constant, ForDefinition);
873+
SILGenFunction(*this, *f, dd).emitDeallocatingDestructor(dd);
874+
postEmitFunction(constant, f);
875+
return;
876+
}
877+
878+
case SILDeclRef::Kind::IVarInitializer: {
879+
auto *cd = cast<ClassDecl>(constant.getDecl());
880+
auto loc = RegularLocation::getAutoGeneratedLocation(cd);
881+
preEmitFunction(constant, cd, f, loc);
882+
PrettyStackTraceSILFunction X("silgen emitDestructor ivar initializer", f);
883+
SILGenFunction(*this, *f, cd).emitIVarInitializer(constant);
884+
postEmitFunction(constant, f);
885+
return;
886+
}
887+
888+
case SILDeclRef::Kind::IVarDestroyer: {
889+
auto *cd = cast<ClassDecl>(constant.getDecl());
890+
auto loc = RegularLocation::getAutoGeneratedLocation(cd);
891+
preEmitFunction(constant, cd, f, loc);
892+
PrettyStackTraceSILFunction X("silgen emitDestructor ivar destroyer", f);
893+
SILGenFunction(*this, *f, cd).emitIVarDestroyer(constant);
894+
postEmitFunction(constant, f);
895+
return;
896+
}
855897
}
856898
}
857899

@@ -889,7 +931,7 @@ static void emitOrDelayFunction(SILGenModule &SGM,
889931
return;
890932
}
891933

892-
emitDelayedFunction(SGM, constant, f);
934+
SGM.emitFunctionDefinition(constant, f);
893935
}
894936

895937
void SILGenModule::preEmitFunction(SILDeclRef constant,
@@ -1232,12 +1274,7 @@ void SILGenModule::emitObjCAllocatorDestructor(ClassDecl *cd,
12321274
// Destructors are a necessary part of class metadata, so can't be delayed.
12331275
if (dd->hasBody()) {
12341276
SILDeclRef dealloc(dd, SILDeclRef::Kind::Deallocator);
1235-
SILFunction *f = getFunction(dealloc, ForDefinition);
1236-
preEmitFunction(dealloc, dd, f, dd);
1237-
PrettyStackTraceSILFunction X("silgen emitDestructor -dealloc", f);
1238-
f->createProfiler(dd, dealloc, ForDefinition);
1239-
SILGenFunction(*this, *f, dd).emitObjCDestructor(dealloc);
1240-
postEmitFunction(dealloc, f);
1277+
emitFunctionDefinition(dealloc, getFunction(dealloc, ForDefinition));
12411278
}
12421279

12431280
// Emit the Objective-C -dealloc entry point if it has
@@ -1249,24 +1286,16 @@ void SILGenModule::emitObjCAllocatorDestructor(ClassDecl *cd,
12491286
if (requiresIVarInitialization(*this, cd)) {
12501287
auto ivarInitializer = SILDeclRef(cd, SILDeclRef::Kind::IVarInitializer)
12511288
.asForeign();
1252-
SILFunction *f = getFunction(ivarInitializer, ForDefinition);
1253-
auto loc = RegularLocation::getAutoGeneratedLocation(dd);
1254-
preEmitFunction(ivarInitializer, dd, f, loc);
1255-
PrettyStackTraceSILFunction X("silgen emitDestructor ivar initializer", f);
1256-
SILGenFunction(*this, *f, cd).emitIVarInitializer(ivarInitializer);
1257-
postEmitFunction(ivarInitializer, f);
1289+
emitFunctionDefinition(ivarInitializer,
1290+
getFunction(ivarInitializer, ForDefinition));
12581291
}
12591292

12601293
// Emit the ivar destroyer, if needed.
12611294
if (hasNonTrivialIVars(cd)) {
12621295
auto ivarDestroyer = SILDeclRef(cd, SILDeclRef::Kind::IVarDestroyer)
12631296
.asForeign();
1264-
SILFunction *f = getFunction(ivarDestroyer, ForDefinition);
1265-
auto loc = RegularLocation::getAutoGeneratedLocation(dd);
1266-
preEmitFunction(ivarDestroyer, dd, f, loc);
1267-
PrettyStackTraceSILFunction X("silgen emitDestructor ivar destroyer", f);
1268-
SILGenFunction(*this, *f, cd).emitIVarDestroyer(ivarDestroyer);
1269-
postEmitFunction(ivarDestroyer, f);
1297+
emitFunctionDefinition(ivarDestroyer,
1298+
getFunction(ivarDestroyer, ForDefinition));
12701299
}
12711300
}
12721301

@@ -1276,12 +1305,8 @@ void SILGenModule::emitDestructor(ClassDecl *cd, DestructorDecl *dd) {
12761305
// Emit the ivar destroyer, if needed.
12771306
if (requiresIVarDestroyer(cd)) {
12781307
SILDeclRef ivarDestroyer(cd, SILDeclRef::Kind::IVarDestroyer);
1279-
SILFunction *f = getFunction(ivarDestroyer, ForDefinition);
1280-
auto loc = RegularLocation::getAutoGeneratedLocation(dd);
1281-
preEmitFunction(ivarDestroyer, dd, f, loc);
1282-
PrettyStackTraceSILFunction X("silgen emitDestructor ivar destroyer", f);
1283-
SILGenFunction(*this, *f, dd).emitIVarDestroyer(ivarDestroyer);
1284-
postEmitFunction(ivarDestroyer, f);
1308+
emitFunctionDefinition(ivarDestroyer,
1309+
getFunction(ivarDestroyer, ForDefinition));
12851310
}
12861311

12871312
// If the class would use the Objective-C allocator, only emit -dealloc.
@@ -1294,24 +1319,14 @@ void SILGenModule::emitDestructor(ClassDecl *cd, DestructorDecl *dd) {
12941319
// Destructors are a necessary part of class metadata, so can't be delayed.
12951320
if (dd->hasBody()) {
12961321
SILDeclRef destroyer(dd, SILDeclRef::Kind::Destroyer);
1297-
SILFunction *f = getFunction(destroyer, ForDefinition);
1298-
RegularLocation loc(dd);
1299-
preEmitFunction(destroyer, dd, f, loc);
1300-
PrettyStackTraceSILFunction X("silgen emitDestroyingDestructor", f);
1301-
SILGenFunction(*this, *f, dd).emitDestroyingDestructor(dd);
1302-
postEmitFunction(destroyer, f);
1322+
emitFunctionDefinition(destroyer, getFunction(destroyer, ForDefinition));
13031323
}
13041324

13051325
// Emit the deallocating destructor.
13061326
{
13071327
SILDeclRef deallocator(dd, SILDeclRef::Kind::Deallocator);
1308-
SILFunction *f = getFunction(deallocator, ForDefinition);
1309-
auto loc = RegularLocation::getAutoGeneratedLocation(dd);
1310-
preEmitFunction(deallocator, dd, f, loc);
1311-
PrettyStackTraceSILFunction X("silgen emitDeallocatingDestructor", f);
1312-
f->createProfiler(dd, deallocator, ForDefinition);
1313-
SILGenFunction(*this, *f, dd).emitDeallocatingDestructor(dd);
1314-
postEmitFunction(deallocator, f);
1328+
emitFunctionDefinition(deallocator,
1329+
getFunction(deallocator, ForDefinition));
13151330
}
13161331
}
13171332

@@ -1910,8 +1925,8 @@ class SILGenModuleRAII {
19101925
|| !SGM.pendingConformances.empty()) {
19111926
while (!SGM.forcedFunctions.empty()) {
19121927
auto &front = SGM.forcedFunctions.front();
1913-
emitDelayedFunction(SGM, front,
1914-
SGM.getEmittedFunction(front, ForDefinition));
1928+
SGM.emitFunctionDefinition(
1929+
front, SGM.getEmittedFunction(front, ForDefinition));
19151930
SGM.forcedFunctions.pop_front();
19161931
}
19171932
while (!SGM.pendingConformances.empty()) {

lib/SILGen/SILGen.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
270270
/// curried functions, curried entry point Functions are also generated and
271271
/// added to the current SILModule.
272272
void emitFunction(FuncDecl *fd);
273-
273+
274+
/// Emits the function definition for a given SILDeclRef.
275+
void emitFunctionDefinition(SILDeclRef constant, SILFunction *f);
276+
274277
/// Generates code for the given closure expression and adds the
275278
/// SILFunction to the current SILModule under the name SILDeclRef(ce).
276279
SILFunction *emitClosure(AbstractClosureExpr *ce);

0 commit comments

Comments
 (0)