Skip to content

Commit d36173a

Browse files
authored
Merge pull request #70317 from tshortli/silgen-dont-skip-func-nested-in-closure-in-inlinable-func
SILGen: Don't skip functions nested in closures in inlinable functions
2 parents 338d426 + 13a1861 commit d36173a

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -804,24 +804,23 @@ bool SILGenModule::shouldSkipDecl(Decl *D) {
804804
if (!getASTContext().SILOpts.SkipNonExportableDecls)
805805
return false;
806806

807-
if (auto *afd = dyn_cast<AbstractFunctionDecl>(D)) {
808-
do {
809-
if (afd->isExposedToClients())
810-
return false;
807+
if (D->isExposedToClients())
808+
return false;
811809

812-
// If this function is nested within another function that is exposed to
813-
// clients then it should be emitted.
814-
auto dc = afd->getDeclContext()->getAsDecl();
815-
afd = dc ? dyn_cast<AbstractFunctionDecl>(dc) : nullptr;
816-
} while (afd);
810+
if (isa<AbstractFunctionDecl>(D)) {
811+
// If this function is nested within another function that is exposed to
812+
// clients then it should be emitted.
813+
auto dc = D->getDeclContext();
814+
do {
815+
if (auto afd = dyn_cast<AbstractFunctionDecl>(dc))
816+
if (afd->isExposedToClients())
817+
return false;
818+
} while ((dc = dc->getParent()));
817819

818820
// We didn't find a parent function that is exposed.
819821
return true;
820822
}
821823

822-
if (D->isExposedToClients())
823-
return false;
824-
825824
return true;
826825
}
827826

test/SILGen/skip_non_exportable_decls.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,26 @@ internal func internalFunc() {}
2626
// CHECK-NO-SKIP: sil hidden{{.*}} @$s4Test022internalFuncWithNestedC0yyF : $@convention(thin) () -> () {
2727
// CHECK-SKIP-NOT: s4Test022internalFuncWithNestedC0yyF
2828
internal func internalFuncWithNestedFunc() {
29+
defer { internalFunc() }
2930
func nested() {}
3031
nested()
32+
let _: () -> () = {
33+
defer { internalFunc() }
34+
internalFunc()
35+
}()
3136
}
37+
// CHECK-NO-SKIP: sil private{{.*}} @$s4Test022internalFuncWithNestedC0yyF6$deferL_yyF : $@convention(thin) () -> () {
38+
// CHECK-SKIP-NOT: s4Test022internalFuncWithNestedC0yyF6$deferL_yyF
39+
3240
// CHECK-NO-SKIP: sil private{{.*}} @$s4Test022internalFuncWithNestedC0yyF6nestedL_yyF : $@convention(thin) () -> () {
3341
// CHECK-SKIP-NOT: s4Test022internalFuncWithNestedC0yyF6nestedL_yyF
3442

43+
// CHECK-NO-SKIP: sil private{{.*}} @$s4Test022internalFuncWithNestedC0yyFyycyXEfU_ : $@convention(thin) () -> @owned @callee_guaranteed () -> () {
44+
// CHECK-SKIP-NOT: s4Test022internalFuncWithNestedC0yyFyycyXEfU_
45+
46+
// CHECK-NO-SKIP: sil private{{.*}} @$s4Test022internalFuncWithNestedC0yyFyycyXEfU_6$deferL_yyF : $@convention(thin) () -> () {
47+
// CHECK-SKIP-NOT: @$s4Test022internalFuncWithNestedC0yyFyycyXEfU_6$deferL_yyF
48+
3549
// CHECK: sil{{.*}} @$s4Test10publicFuncyyF : $@convention(thin) () -> () {
3650
public func publicFunc() {}
3751

@@ -66,9 +80,15 @@ public var publicGlobalVar = 1
6680
defer { publicFunc() }
6781
func nested() {}
6882
nested()
83+
let _: () -> () = {
84+
defer { publicFunc() }
85+
publicFunc()
86+
}()
6987
}
7088
// CHECK: sil shared [serialized]{{.*}} @$s4Test023inlinableFuncWithNestedC0yyF6$deferL_yyF : $@convention(thin) () -> () {
7189
// CHECK: sil shared [serialized]{{.*}} @$s4Test023inlinableFuncWithNestedC0yyF6nestedL_yyF : $@convention(thin) () -> () {
90+
// CHECK: sil shared [serialized]{{.*}} @$s4Test023inlinableFuncWithNestedC0yyFyycyXEfU_ : $@convention(thin) () -> @owned @callee_guaranteed () -> () {
91+
// CHECK: sil shared [serialized]{{.*}} @$s4Test023inlinableFuncWithNestedC0yyFyycyXEfU_6$deferL_yyF : $@convention(thin) () -> () {
7292

7393
private class PrivateClass {
7494
// CHECK-NO-SKIP: sil private{{.*}} @$s4Test12PrivateClass33_CFB3F9DC47F5EF9E1D08B58758351A08LLCfd : $@convention(method) (@guaranteed PrivateClass) -> @owned Builtin.NativeObject {

0 commit comments

Comments
 (0)