Skip to content

Commit f5a14e0

Browse files
authored
Merge pull request swiftlang#79526 from swiftlang/elsh/revert-import-filters
[6.1] Revert import access level / attribute filtering
2 parents 8502c4a + d682632 commit f5a14e0

File tree

4 files changed

+34
-186
lines changed

4 files changed

+34
-186
lines changed

include/swift/AST/Module.h

+5
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,11 @@ class ModuleDecl
10161016
/// This assumes that \p module was imported.
10171017
bool isImportedImplementationOnly(const ModuleDecl *module) const;
10181018

1019+
/// Returns true if decl context or its content can be serialized by
1020+
/// cross-module-optimization.
1021+
/// The \p ctxt can e.g. be a NominalType or the context of a function.
1022+
bool canBeUsedForCrossModuleOptimization(DeclContext *ctxt) const;
1023+
10191024
/// Finds all top-level decls of this module.
10201025
///
10211026
/// This does a simple local lookup, not recursively looking through imports.

lib/AST/Module.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -3010,6 +3010,33 @@ bool ModuleDecl::isImportedImplementationOnly(const ModuleDecl *module) const {
30103010
return true;
30113011
}
30123012

3013+
bool ModuleDecl::
3014+
canBeUsedForCrossModuleOptimization(DeclContext *ctxt) const {
3015+
ModuleDecl *moduleOfCtxt = ctxt->getParentModule();
3016+
3017+
// If the context defined in the same module - or is the same module, it's
3018+
// fine.
3019+
if (moduleOfCtxt == this)
3020+
return true;
3021+
3022+
// See if context is imported in a "regular" way, i.e. not with
3023+
// @_implementationOnly, `package import` or @_spiOnly.
3024+
ModuleDecl::ImportFilter filter = {
3025+
ModuleDecl::ImportFilterKind::ImplementationOnly,
3026+
ModuleDecl::ImportFilterKind::PackageOnly,
3027+
ModuleDecl::ImportFilterKind::SPIOnly
3028+
};
3029+
SmallVector<ImportedModule, 4> results;
3030+
getImportedModules(results, filter);
3031+
3032+
auto &imports = getASTContext().getImportCache();
3033+
for (auto &desc : results) {
3034+
if (imports.isImportedBy(moduleOfCtxt, desc.importedModule))
3035+
return false;
3036+
}
3037+
return true;
3038+
}
3039+
30133040
void SourceFile::lookupImportedSPIGroups(
30143041
const ModuleDecl *importedModule,
30153042
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {

lib/SILOptimizer/IPO/CrossModuleOptimization.cpp

+2-65
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#define DEBUG_TYPE "cross-module-serialization-setup"
1818
#include "swift/AST/Module.h"
19-
#include "swift/AST/ImportCache.h"
2019
#include "swift/Basic/Assertions.h"
2120
#include "swift/IRGen/TBDGen.h"
2221
#include "swift/SIL/ApplySite.h"
@@ -104,11 +103,6 @@ class CrossModuleOptimization {
104103
bool canSerializeType(CanType type);
105104
bool canSerializeDecl(NominalTypeDecl *decl);
106105

107-
/// Check whether decls imported with certain access levels or attributes
108-
/// can be serialized.
109-
/// The \p ctxt can e.g. be a NominalType or the context of a function.
110-
bool checkImports(DeclContext *ctxt) const;
111-
112106
bool canUseFromInline(DeclContext *declCtxt);
113107

114108
bool canUseFromInline(SILFunction *func);
@@ -742,12 +736,7 @@ static bool couldBeLinkedStatically(DeclContext *funcCtxt, SILModule &module) {
742736
// The stdlib module is always linked dynamically.
743737
if (funcModule == module.getASTContext().getStdlibModule())
744738
return false;
745-
746-
// An sdk or system module should be linked dynamically.
747-
if (isPackageCMOEnabled(module.getSwiftModule()) &&
748-
funcModule->isNonUserModule())
749-
return false;
750-
739+
751740
// Conservatively assume the function is in a statically linked module.
752741
return true;
753742
}
@@ -757,7 +746,7 @@ bool CrossModuleOptimization::canUseFromInline(DeclContext *declCtxt) {
757746
if (everything)
758747
return true;
759748

760-
if (!checkImports(declCtxt))
749+
if (!M.getSwiftModule()->canBeUsedForCrossModuleOptimization(declCtxt))
761750
return false;
762751

763752
/// If we are emitting a TBD file, the TBD file only contains public symbols
@@ -773,58 +762,6 @@ bool CrossModuleOptimization::canUseFromInline(DeclContext *declCtxt) {
773762
return true;
774763
}
775764

776-
bool CrossModuleOptimization::checkImports(DeclContext *ctxt) const {
777-
ModuleDecl *moduleOfCtxt = ctxt->getParentModule();
778-
779-
// If the context defined in the same module - or is the same module, it's
780-
// fine.
781-
if (moduleOfCtxt == M.getSwiftModule())
782-
return true;
783-
784-
ModuleDecl::ImportFilter filter;
785-
786-
if (isPackageCMOEnabled(M.getSwiftModule())) {
787-
// When Package CMO is enabled, types imported with `package import`
788-
// or `@_spiOnly import` into this module should be allowed to be
789-
// serialized. These types may be used in APIs with `package` or
790-
// higher access level, with or without `@_spi`, and such APIs should
791-
// be serializable to allow direct access by another module if it's
792-
// in the same package.
793-
//
794-
// However, types are from modules imported as `@_implementationOnly`
795-
// should not be serialized, even if their defining modules are SDK
796-
// or system modules. Since these types are intended to remain hidden
797-
// from external clients, their metadata (e.g. field offsets) may be
798-
// stripped, making it unavailable for look up at runtime. If serialized,
799-
// the client will attempt to use the serialized accessor and fail
800-
// because the metadata is missing, leading to a linker error.
801-
//
802-
// This issue applies to transitively imported types as well;
803-
// `@_implementationOnly import Foundation` imports `ObjectiveC`
804-
// indirectly, and metadata for types like `NSObject` from `ObjectiveC`
805-
// can also be stripped, thus such types should not be allowed for
806-
// serialization.
807-
filter = { ModuleDecl::ImportFilterKind::ImplementationOnly };
808-
} else {
809-
// See if context is imported in a "regular" way, i.e. not with
810-
// @_implementationOnly, `package import` or @_spiOnly.
811-
filter = {
812-
ModuleDecl::ImportFilterKind::ImplementationOnly,
813-
ModuleDecl::ImportFilterKind::PackageOnly,
814-
ModuleDecl::ImportFilterKind::SPIOnly
815-
};
816-
}
817-
SmallVector<ImportedModule, 4> results;
818-
M.getSwiftModule()->getImportedModules(results, filter);
819-
820-
auto &imports = M.getSwiftModule()->getASTContext().getImportCache();
821-
for (auto &desc : results) {
822-
if (imports.isImportedBy(moduleOfCtxt, desc.importedModule))
823-
return false;
824-
}
825-
return true;
826-
}
827-
828765
/// Returns true if the function \p func can be used from a serialized function.
829766
bool CrossModuleOptimization::canUseFromInline(SILFunction *function) {
830767
if (everything)

test/SILOptimizer/package-cmo-import-filter.swift

-121
This file was deleted.

0 commit comments

Comments
 (0)