Skip to content

Commit ef5335a

Browse files
committed
[SILGen] Move thunk emission to emitFunctionDefinition
1 parent ae5b385 commit ef5335a

File tree

2 files changed

+43
-64
lines changed

2 files changed

+43
-64
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,39 @@ bool SILGenModule::hasFunction(SILDeclRef constant) {
677677
void SILGenModule::visitFuncDecl(FuncDecl *fd) { emitFunction(fd); }
678678

679679
void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
680+
681+
if (constant.isForeignToNativeThunk()) {
682+
f->setThunk(IsThunk);
683+
if (constant.asForeign().isClangGenerated())
684+
f->setSerialized(IsSerializable);
685+
686+
auto loc = constant.getAsRegularLocation();
687+
loc.markAutoGenerated();
688+
auto *dc = loc.getAsDeclContext();
689+
assert(dc);
690+
691+
preEmitFunction(constant, f, loc);
692+
PrettyStackTraceSILFunction X("silgen emitForeignToNativeThunk", f);
693+
SILGenFunction(*this, *f, dc).emitForeignToNativeThunk(constant);
694+
postEmitFunction(constant, f);
695+
return;
696+
}
697+
698+
if (constant.isNativeToForeignThunk()) {
699+
auto loc = constant.getAsRegularLocation();
700+
loc.markAutoGenerated();
701+
auto *dc = loc.getAsDeclContext();
702+
assert(dc);
703+
704+
preEmitFunction(constant, f, loc);
705+
PrettyStackTraceSILFunction X("silgen emitNativeToForeignThunk", f);
706+
f->setBare(IsBare);
707+
f->setThunk(IsThunk);
708+
SILGenFunction(*this, *f, dc).emitNativeToForeignThunk(constant);
709+
postEmitFunction(constant, f);
710+
return;
711+
}
712+
680713
switch (constant.kind) {
681714
case SILDeclRef::Kind::Func: {
682715
if (auto *ce = constant.getAbstractClosureExpr()) {
@@ -1412,14 +1445,7 @@ void SILGenModule::emitObjCMethodThunk(FuncDecl *method) {
14121445
return;
14131446

14141447
// ObjC entry points are always externally usable, so can't be delay-emitted.
1415-
1416-
SILFunction *f = getFunction(thunk, ForDefinition);
1417-
preEmitFunction(thunk, f, method);
1418-
PrettyStackTraceSILFunction X("silgen emitObjCMethodThunk", f);
1419-
f->setBare(IsBare);
1420-
f->setThunk(IsThunk);
1421-
SILGenFunction(*this, *f, method).emitNativeToForeignThunk(thunk);
1422-
postEmitFunction(thunk, f);
1448+
emitNativeToForeignThunk(thunk);
14231449
}
14241450

14251451
void SILGenModule::emitObjCPropertyMethodThunks(AbstractStorageDecl *prop) {
@@ -1435,33 +1461,17 @@ void SILGenModule::emitObjCPropertyMethodThunks(AbstractStorageDecl *prop) {
14351461
if (hasFunction(getterRef))
14361462
return;
14371463

1438-
auto thunkBodyLoc = RegularLocation::getAutoGeneratedLocation(prop);
14391464
// ObjC entry points are always externally usable, so emitting can't be
14401465
// delayed.
1441-
{
1442-
SILFunction *f = getFunction(getterRef, ForDefinition);
1443-
preEmitFunction(getterRef, f, thunkBodyLoc);
1444-
PrettyStackTraceSILFunction X("silgen objc property getter thunk", f);
1445-
f->setBare(IsBare);
1446-
f->setThunk(IsThunk);
1447-
SILGenFunction(*this, *f, getter).emitNativeToForeignThunk(getterRef);
1448-
postEmitFunction(getterRef, f);
1449-
}
1466+
emitNativeToForeignThunk(getterRef);
14501467

14511468
if (!prop->isSettable(prop->getDeclContext()))
14521469
return;
14531470

14541471
// FIXME: Add proper location.
14551472
auto *setter = prop->getOpaqueAccessor(AccessorKind::Set);
14561473
auto setterRef = SILDeclRef(setter, SILDeclRef::Kind::Func).asForeign();
1457-
1458-
SILFunction *f = getFunction(setterRef, ForDefinition);
1459-
preEmitFunction(setterRef, f, thunkBodyLoc);
1460-
PrettyStackTraceSILFunction X("silgen objc property setter thunk", f);
1461-
f->setBare(IsBare);
1462-
f->setThunk(IsThunk);
1463-
SILGenFunction(*this, *f, setter).emitNativeToForeignThunk(setterRef);
1464-
postEmitFunction(setterRef, f);
1474+
emitNativeToForeignThunk(setterRef);
14651475
}
14661476

14671477
void SILGenModule::emitObjCConstructorThunk(ConstructorDecl *constructor) {
@@ -1473,15 +1483,7 @@ void SILGenModule::emitObjCConstructorThunk(ConstructorDecl *constructor) {
14731483
return;
14741484
// ObjC entry points are always externally usable, so emitting can't be
14751485
// delayed.
1476-
1477-
SILFunction *f = getFunction(thunk, ForDefinition);
1478-
auto loc = RegularLocation::getAutoGeneratedLocation(constructor);
1479-
preEmitFunction(thunk, f, loc);
1480-
PrettyStackTraceSILFunction X("silgen objc constructor thunk", f);
1481-
f->setBare(IsBare);
1482-
f->setThunk(IsThunk);
1483-
SILGenFunction(*this, *f, constructor).emitNativeToForeignThunk(thunk);
1484-
postEmitFunction(thunk, f);
1486+
emitNativeToForeignThunk(thunk);
14851487
}
14861488

14871489
void SILGenModule::emitObjCDestructorThunk(DestructorDecl *destructor) {
@@ -1491,14 +1493,8 @@ void SILGenModule::emitObjCDestructorThunk(DestructorDecl *destructor) {
14911493
// Don't emit the thunk if it already exists.
14921494
if (hasFunction(thunk))
14931495
return;
1494-
SILFunction *f = getFunction(thunk, ForDefinition);
1495-
auto loc = RegularLocation::getAutoGeneratedLocation(destructor);
1496-
preEmitFunction(thunk, destructor, f, loc);
1497-
PrettyStackTraceSILFunction X("silgen objc destructor thunk", f);
1498-
f->setBare(IsBare);
1499-
f->setThunk(IsThunk);
1500-
SILGenFunction(*this, *f, destructor).emitNativeToForeignThunk(thunk);
1501-
postEmitFunction(thunk, f);
1496+
1497+
emitNativeToForeignThunk(thunk);
15021498
}
15031499

15041500
void SILGenModule::visitPatternBindingDecl(PatternBindingDecl *pd) {

lib/SILGen/SILGenThunk.cpp

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,14 @@ SILGenFunction::emitDynamicMethodRef(SILLocation loc, SILDeclRef constant,
9090

9191
void SILGenModule::emitForeignToNativeThunk(SILDeclRef thunk) {
9292
// Thunks are always emitted by need, so don't need delayed emission.
93-
assert(!thunk.isForeign && "foreign-to-native thunks only");
94-
SILFunction *f = getFunction(thunk, ForDefinition);
95-
f->setThunk(IsThunk);
96-
if (thunk.asForeign().isClangGenerated())
97-
f->setSerialized(IsSerializable);
98-
preEmitFunction(thunk, f, thunk.getDecl());
99-
PrettyStackTraceSILFunction X("silgen emitForeignToNativeThunk", f);
100-
SILGenFunction(*this, *f, SwiftModule).emitForeignToNativeThunk(thunk);
101-
postEmitFunction(thunk, f);
93+
assert(thunk.isForeignToNativeThunk() && "foreign-to-native thunks only");
94+
emitFunctionDefinition(thunk, getFunction(thunk, ForDefinition));
10295
}
10396

10497
void SILGenModule::emitNativeToForeignThunk(SILDeclRef thunk) {
10598
// Thunks are always emitted by need, so don't need delayed emission.
106-
assert(thunk.isForeign && "native-to-foreign thunks only");
107-
108-
SILFunction *f = getFunction(thunk, ForDefinition);
109-
if (thunk.hasDecl())
110-
preEmitFunction(thunk, f, thunk.getDecl());
111-
else
112-
preEmitFunction(thunk, f, thunk.getAbstractClosureExpr());
113-
PrettyStackTraceSILFunction X("silgen emitNativeToForeignThunk", f);
114-
f->setBare(IsBare);
115-
f->setThunk(IsThunk);
116-
SILGenFunction(*this, *f, SwiftModule).emitNativeToForeignThunk(thunk);
117-
postEmitFunction(thunk, f);
99+
assert(thunk.isNativeToForeignThunk() && "native-to-foreign thunks only");
100+
emitFunctionDefinition(thunk, getFunction(thunk, ForDefinition));
118101
}
119102

120103
SILValue

0 commit comments

Comments
 (0)