Skip to content

Commit 36824b5

Browse files
authored
Merge pull request #69392 from tshortli/skip-non-exportable-decls-fixes
SILGen fixes for -experimental-skip-non-exportable-decls
2 parents 7781af5 + 114a46a commit 36824b5

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

include/swift/AST/DeclExportabilityVisitor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class DeclExportabilityVisitor
127127
#define DEFAULT_TO_ACCESS_LEVEL(KIND) \
128128
bool visit##KIND##Decl(const KIND##Decl *D) { \
129129
static_assert(std::is_convertible<KIND##Decl *, ValueDecl *>::value, \
130-
"##KIND##Decl must be a ValueDecl"); \
130+
#KIND "Decl must be a ValueDecl"); \
131131
return false; \
132132
}
133133
DEFAULT_TO_ACCESS_LEVEL(NominalType);
@@ -145,12 +145,11 @@ class DeclExportabilityVisitor
145145
// exportability queries.
146146
#define UNREACHABLE(KIND) \
147147
bool visit##KIND##Decl(const KIND##Decl *D) { \
148-
llvm_unreachable("unexpected decl kind"); \
148+
llvm_unreachable("unexpected " #KIND "Decl"); \
149149
return true; \
150150
}
151151
UNREACHABLE(Module);
152152
UNREACHABLE(TopLevelCode);
153-
UNREACHABLE(Import);
154153
UNREACHABLE(PoundDiagnostic);
155154
UNREACHABLE(Missing);
156155
UNREACHABLE(MissingMember);
@@ -169,6 +168,7 @@ class DeclExportabilityVisitor
169168
#define UNINTERESTING(KIND) \
170169
bool visit##KIND##Decl(const KIND##Decl *D) { return true; }
171170
UNINTERESTING(IfConfig);
171+
UNINTERESTING(Import);
172172
UNINTERESTING(PrecedenceGroup);
173173
UNINTERESTING(EnumCase);
174174
UNINTERESTING(Operator);

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,6 @@ bool ArgsToFrontendOptionsConverter::convert(
322322

323323
Opts.SkipNonExportableDecls |=
324324
Args.hasArg(OPT_experimental_skip_non_exportable_decls);
325-
// FIXME: Remove this with rdar://117020997
326-
Opts.SkipNonExportableDecls |= Args.hasArg(OPT_experimental_lazy_typecheck);
327325
Opts.DebugPrefixSerializedDebuggingOptions |=
328326
Args.hasArg(OPT_prefix_serialized_debugging_options);
329327
Opts.EnableSourceImport |= Args.hasArg(OPT_enable_source_import);

lib/SILGen/SILGen.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,28 @@ bool SILGenModule::shouldSkipDecl(Decl *D) {
801801
if (!D->isAvailableDuringLowering())
802802
return true;
803803

804-
if (getASTContext().SILOpts.SkipNonExportableDecls &&
805-
!D->isExposedToClients())
804+
if (!getASTContext().SILOpts.SkipNonExportableDecls)
805+
return false;
806+
807+
if (auto *afd = dyn_cast<AbstractFunctionDecl>(D)) {
808+
do {
809+
if (afd->isExposedToClients())
810+
return false;
811+
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);
817+
818+
// We didn't find a parent function that is exposed.
806819
return true;
820+
}
807821

808-
return false;
822+
if (D->isExposedToClients())
823+
return false;
824+
825+
return true;
809826
}
810827

811828
void SILGenModule::visit(Decl *D) {

test/SILGen/skip-non-exportable-decls.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// RUN: %target-swift-frontend -emit-silgen %s -parse-as-library -module-name Test | %FileCheck %s --check-prefixes=CHECK,CHECK-NO-SKIP
33
// RUN: %target-swift-frontend -emit-silgen %s -parse-as-library -module-name Test -experimental-skip-non-exportable-decls | %FileCheck %s --check-prefixes=CHECK,CHECK-SKIP
44

5+
import Swift
6+
57
// CHECK-NO-SKIP: sil private{{.*}} @$s4Test11privateFunc33_E3F0E1C7B46D05C8067CB98677DE566CLLyyF : $@convention(thin) () -> () {
68
// CHECK-SKIP-NOT: s4Test11privateFunc33_E3F0E1C7B46D05C8067CB98677DE566CLLyyF
79
private func privateFunc() {}
@@ -10,9 +12,39 @@ private func privateFunc() {}
1012
// CHECK-SKIP-NOT: s4Test12internalFuncyyF
1113
internal func internalFunc() {}
1214

15+
// CHECK-NO-SKIP: sil hidden{{.*}} @$s4Test022internalFuncWithNestedC0yyF : $@convention(thin) () -> () {
16+
// CHECK-SKIP-NOT: s4Test022internalFuncWithNestedC0yyF
17+
internal func internalFuncWithNestedFunc() {
18+
func nested() {}
19+
nested()
20+
}
21+
// CHECK-NO-SKIP: sil private{{.*}} @$s4Test022internalFuncWithNestedC0yyF6nestedL_yyF : $@convention(thin) () -> () {
22+
// CHECK-SKIP-NOT: s4Test022internalFuncWithNestedC0yyF6nestedL_yyF
23+
1324
// CHECK: sil{{.*}} @$s4Test10publicFuncyyF : $@convention(thin) () -> () {
1425
public func publicFunc() {}
1526

27+
// CHECK: sil{{.*}} @$s4Test25publicFuncWithNestedFuncsyyF : $@convention(thin) () -> () {
28+
public func publicFuncWithNestedFuncs() {
29+
defer { publicFunc() }
30+
func nested() {}
31+
nested()
32+
}
33+
// CHECK: sil private{{.*}} @$s4Test25publicFuncWithNestedFuncsyyF6$deferL_yyF : $@convention(thin) () -> () {
34+
// CHECK: sil private{{.*}} @$s4Test25publicFuncWithNestedFuncsyyF6nestedL_yyF : $@convention(thin) () -> () {
35+
36+
// CHECK: sil [serialized]{{.*}} @$s4Test13inlinableFuncyyF : $@convention(thin) () -> () {
37+
@inlinable internal func inlinableFunc() {}
38+
39+
// CHECK: sil [serialized]{{.*}} @$s4Test023inlinableFuncWithNestedC0yyF : $@convention(thin) () -> () {
40+
@inlinable internal func inlinableFuncWithNestedFunc() {
41+
defer { publicFunc() }
42+
func nested() {}
43+
nested()
44+
}
45+
// CHECK: sil shared [serialized]{{.*}} @$s4Test023inlinableFuncWithNestedC0yyF6$deferL_yyF : $@convention(thin) () -> () {
46+
// CHECK: sil shared [serialized]{{.*}} @$s4Test023inlinableFuncWithNestedC0yyF6nestedL_yyF : $@convention(thin) () -> () {
47+
1648
private class PrivateClass {
1749
// CHECK-NO-SKIP: sil private{{.*}} @$s4Test12PrivateClass33_E3F0E1C7B46D05C8067CB98677DE566CLLCfd : $@convention(method) (@guaranteed PrivateClass) -> @owned Builtin.NativeObject {
1850
// CHECK-SKIP-NOT: s4Test12PrivateClass33_E3F0E1C7B46D05C8067CB98677DE566CLLCfd

test/Serialization/lazy-typecheck.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
// RUN: %target-swift-frontend -swift-version 5 %S/../Inputs/lazy_typecheck.swift -enable-library-evolution -parse-as-library -package-name Package -DFLAG -typecheck -verify
33
// RUN: %target-swift-frontend -swift-version 5 %S/../Inputs/lazy_typecheck.swift -module-name lazy_typecheck -emit-module -emit-module-path %t/lazy_typecheck.swiftmodule -enable-library-evolution -parse-as-library -package-name Package -DFLAG -experimental-lazy-typecheck -experimental-skip-all-function-bodies -experimental-skip-non-exportable-decls
44

5-
// Verify the module also builds without -experimental-skip-non-exportable-decls
6-
// FIXME: Remove with rdar://117020997
7-
// RUN: %target-swift-frontend -swift-version 5 %S/../Inputs/lazy_typecheck.swift -module-name lazy_typecheck -emit-module -emit-module-path /dev/null -enable-library-evolution -parse-as-library -package-name Package -DFLAG -experimental-lazy-typecheck -experimental-skip-all-function-bodies
8-
95
// RUN: %target-swift-frontend -package-name Package -typecheck -verify %S/../Inputs/lazy_typecheck_client.swift -DFLAG -I %t
106

117
// FIXME: Re-run the test with -experimental-skip-non-inlinable-function-bodies

0 commit comments

Comments
 (0)