Skip to content

SILGen fixes for -experimental-skip-non-exportable-decls #69392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/swift/AST/DeclExportabilityVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class DeclExportabilityVisitor
#define DEFAULT_TO_ACCESS_LEVEL(KIND) \
bool visit##KIND##Decl(const KIND##Decl *D) { \
static_assert(std::is_convertible<KIND##Decl *, ValueDecl *>::value, \
"##KIND##Decl must be a ValueDecl"); \
#KIND "Decl must be a ValueDecl"); \
return false; \
}
DEFAULT_TO_ACCESS_LEVEL(NominalType);
Expand All @@ -145,12 +145,11 @@ class DeclExportabilityVisitor
// exportability queries.
#define UNREACHABLE(KIND) \
bool visit##KIND##Decl(const KIND##Decl *D) { \
llvm_unreachable("unexpected decl kind"); \
llvm_unreachable("unexpected " #KIND "Decl"); \
return true; \
}
UNREACHABLE(Module);
UNREACHABLE(TopLevelCode);
UNREACHABLE(Import);
UNREACHABLE(PoundDiagnostic);
UNREACHABLE(Missing);
UNREACHABLE(MissingMember);
Expand All @@ -169,6 +168,7 @@ class DeclExportabilityVisitor
#define UNINTERESTING(KIND) \
bool visit##KIND##Decl(const KIND##Decl *D) { return true; }
UNINTERESTING(IfConfig);
UNINTERESTING(Import);
UNINTERESTING(PrecedenceGroup);
UNINTERESTING(EnumCase);
UNINTERESTING(Operator);
Expand Down
2 changes: 0 additions & 2 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,6 @@ bool ArgsToFrontendOptionsConverter::convert(

Opts.SkipNonExportableDecls |=
Args.hasArg(OPT_experimental_skip_non_exportable_decls);
// FIXME: Remove this with rdar://117020997
Opts.SkipNonExportableDecls |= Args.hasArg(OPT_experimental_lazy_typecheck);
Opts.DebugPrefixSerializedDebuggingOptions |=
Args.hasArg(OPT_prefix_serialized_debugging_options);
Opts.EnableSourceImport |= Args.hasArg(OPT_enable_source_import);
Expand Down
23 changes: 20 additions & 3 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,28 @@ bool SILGenModule::shouldSkipDecl(Decl *D) {
if (!D->isAvailableDuringLowering())
return true;

if (getASTContext().SILOpts.SkipNonExportableDecls &&
!D->isExposedToClients())
if (!getASTContext().SILOpts.SkipNonExportableDecls)
return false;

if (auto *afd = dyn_cast<AbstractFunctionDecl>(D)) {
do {
if (afd->isExposedToClients())
return false;

// If this function is nested within another function that is exposed to
// clients then it should be emitted.
auto dc = afd->getDeclContext()->getAsDecl();
afd = dc ? dyn_cast<AbstractFunctionDecl>(dc) : nullptr;
} while (afd);

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

return false;
if (D->isExposedToClients())
return false;

return true;
}

void SILGenModule::visit(Decl *D) {
Expand Down
32 changes: 32 additions & 0 deletions test/SILGen/skip-non-exportable-decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// RUN: %target-swift-frontend -emit-silgen %s -parse-as-library -module-name Test | %FileCheck %s --check-prefixes=CHECK,CHECK-NO-SKIP
// 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

import Swift

// CHECK-NO-SKIP: sil private{{.*}} @$s4Test11privateFunc33_E3F0E1C7B46D05C8067CB98677DE566CLLyyF : $@convention(thin) () -> () {
// CHECK-SKIP-NOT: s4Test11privateFunc33_E3F0E1C7B46D05C8067CB98677DE566CLLyyF
private func privateFunc() {}
Expand All @@ -10,9 +12,39 @@ private func privateFunc() {}
// CHECK-SKIP-NOT: s4Test12internalFuncyyF
internal func internalFunc() {}

// CHECK-NO-SKIP: sil hidden{{.*}} @$s4Test022internalFuncWithNestedC0yyF : $@convention(thin) () -> () {
// CHECK-SKIP-NOT: s4Test022internalFuncWithNestedC0yyF
internal func internalFuncWithNestedFunc() {
func nested() {}
nested()
}
// CHECK-NO-SKIP: sil private{{.*}} @$s4Test022internalFuncWithNestedC0yyF6nestedL_yyF : $@convention(thin) () -> () {
// CHECK-SKIP-NOT: s4Test022internalFuncWithNestedC0yyF6nestedL_yyF

// CHECK: sil{{.*}} @$s4Test10publicFuncyyF : $@convention(thin) () -> () {
public func publicFunc() {}

// CHECK: sil{{.*}} @$s4Test25publicFuncWithNestedFuncsyyF : $@convention(thin) () -> () {
public func publicFuncWithNestedFuncs() {
defer { publicFunc() }
func nested() {}
nested()
}
// CHECK: sil private{{.*}} @$s4Test25publicFuncWithNestedFuncsyyF6$deferL_yyF : $@convention(thin) () -> () {
// CHECK: sil private{{.*}} @$s4Test25publicFuncWithNestedFuncsyyF6nestedL_yyF : $@convention(thin) () -> () {

// CHECK: sil [serialized]{{.*}} @$s4Test13inlinableFuncyyF : $@convention(thin) () -> () {
@inlinable internal func inlinableFunc() {}

// CHECK: sil [serialized]{{.*}} @$s4Test023inlinableFuncWithNestedC0yyF : $@convention(thin) () -> () {
@inlinable internal func inlinableFuncWithNestedFunc() {
defer { publicFunc() }
func nested() {}
nested()
}
// CHECK: sil shared [serialized]{{.*}} @$s4Test023inlinableFuncWithNestedC0yyF6$deferL_yyF : $@convention(thin) () -> () {
// CHECK: sil shared [serialized]{{.*}} @$s4Test023inlinableFuncWithNestedC0yyF6nestedL_yyF : $@convention(thin) () -> () {

private class PrivateClass {
// CHECK-NO-SKIP: sil private{{.*}} @$s4Test12PrivateClass33_E3F0E1C7B46D05C8067CB98677DE566CLLCfd : $@convention(method) (@guaranteed PrivateClass) -> @owned Builtin.NativeObject {
// CHECK-SKIP-NOT: s4Test12PrivateClass33_E3F0E1C7B46D05C8067CB98677DE566CLLCfd
Expand Down
4 changes: 0 additions & 4 deletions test/Serialization/lazy-typecheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// RUN: %target-swift-frontend -swift-version 5 %S/../Inputs/lazy_typecheck.swift -enable-library-evolution -parse-as-library -package-name Package -DFLAG -typecheck -verify
// 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

// Verify the module also builds without -experimental-skip-non-exportable-decls
// FIXME: Remove with rdar://117020997
// 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

// RUN: %target-swift-frontend -package-name Package -typecheck -verify %S/../Inputs/lazy_typecheck_client.swift -DFLAG -I %t

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