Skip to content

Commit 3b212ce

Browse files
authored
Merge pull request swiftlang#68295 from kubamracek/embedded-modules
[embedded] Add basics of module serialization, importing and validation in embedded Swift.
2 parents 74e22dc + 25eb997 commit 3b212ce

25 files changed

+188
-21
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
663663
HasAnyUnavailableDuringLoweringValues : 1
664664
);
665665

666-
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
666+
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
667667
/// If the module is compiled as static library.
668668
StaticLibrary : 1,
669669

@@ -707,6 +707,9 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
707707
/// Whether this module was built with -experimental-hermetic-seal-at-link.
708708
HasHermeticSealAtLink : 1,
709709

710+
/// Whether this module was built with embedded Swift.
711+
IsEmbeddedSwiftModule : 1,
712+
710713
/// Whether this module has been compiled with comprehensive checking for
711714
/// concurrency, e.g., Sendable checking.
712715
IsConcurrencyChecked : 1,

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,12 @@ ERROR(need_hermetic_seal_to_import_module,none,
872872
"module %0 was built with -experimental-hermetic-seal-at-link, but "
873873
"current compilation does not have -experimental-hermetic-seal-at-link",
874874
(Identifier))
875+
ERROR(cannot_import_embedded_module,none,
876+
"module %0 cannot be imported because it was built with embedded Swift",
877+
(Identifier))
878+
ERROR(cannot_import_non_embedded_module,none,
879+
"module %0 cannot be imported in embedded Swift mode",
880+
(Identifier))
875881
ERROR(need_cxx_interop_to_import_module,none,
876882
"module %0 was built with C++ interoperability enabled, but "
877883
"current compilation does not enable C++ interoperability",

include/swift/AST/Module.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,14 @@ class ModuleDecl
656656
Bits.ModuleDecl.HasHermeticSealAtLink = enabled;
657657
}
658658

659+
/// Returns true if this module was built with embedded Swift
660+
bool isEmbeddedSwiftModule() const {
661+
return Bits.ModuleDecl.IsEmbeddedSwiftModule;
662+
}
663+
void setIsEmbeddedSwiftModule(bool enabled = true) {
664+
Bits.ModuleDecl.IsEmbeddedSwiftModule = enabled;
665+
}
666+
659667
/// Returns true if this module was built with C++ interoperability enabled.
660668
bool hasCxxInteroperability() const {
661669
return Bits.ModuleDecl.HasCxxInteroperability;

include/swift/Serialization/SerializationOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ namespace swift {
155155
bool DisableCrossModuleIncrementalInfo = false;
156156
bool StaticLibrary = false;
157157
bool HermeticSealAtLink = false;
158+
bool EmbeddedSwiftModule = false;
158159
bool IsOSSA = false;
159160
bool SerializeExternalDeclsOnly = false;
160161
};

include/swift/Serialization/Validation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class ExtendedValidationInfo {
128128
unsigned IsSIB : 1;
129129
unsigned IsStaticLibrary : 1;
130130
unsigned HasHermeticSealAtLink : 1;
131+
unsigned IsEmbeddedSwiftModule : 1;
131132
unsigned IsTestable : 1;
132133
unsigned ResilienceStrategy : 2;
133134
unsigned IsImplicitDynamicEnabled : 1;
@@ -181,6 +182,10 @@ class ExtendedValidationInfo {
181182
void setHasHermeticSealAtLink(bool val) {
182183
Bits.HasHermeticSealAtLink = val;
183184
}
185+
bool isEmbeddedSwiftModule() const { return Bits.IsEmbeddedSwiftModule; }
186+
void setIsEmbeddedSwiftModule(bool val) {
187+
Bits.IsEmbeddedSwiftModule = val;
188+
}
184189
bool isTestable() const { return Bits.IsTestable; }
185190
void setIsTestable(bool val) {
186191
Bits.IsTestable = val;

lib/AST/Module.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
718718
Bits.ModuleDecl.IsMainModule = 0;
719719
Bits.ModuleDecl.HasIncrementalInfo = 0;
720720
Bits.ModuleDecl.HasHermeticSealAtLink = 0;
721+
Bits.ModuleDecl.IsEmbeddedSwiftModule = 0;
721722
Bits.ModuleDecl.IsConcurrencyChecked = 0;
722723
Bits.ModuleDecl.ObjCNameLookupCachePopulated = 0;
723724
Bits.ModuleDecl.HasCxxInteroperability = 0;

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
223223

224224
serializationOpts.HermeticSealAtLink = opts.HermeticSealAtLink;
225225

226+
serializationOpts.EmbeddedSwiftModule =
227+
LangOpts.hasFeature(Feature::Embedded);
228+
226229
serializationOpts.IsOSSA = getSILOptions().EnableOSSAModules;
227230

228231
serializationOpts.SerializeExternalDeclsOnly =

lib/FrontendTool/FrontendTool.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,11 @@ static bool validateTBDIfNeeded(const CompilerInvocation &Invocation,
15571557
return false;
15581558
}
15591559

1560+
// Embedded Swift does not support TBD.
1561+
if (Invocation.getLangOptions().hasFeature(Feature::Embedded)) {
1562+
return false;
1563+
}
1564+
15601565
// Cross-module optimization does not support TBD.
15611566
if (Invocation.getSILOptions().CMOMode == CrossModuleOptimizationMode::Aggressive) {
15621567
return false;

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
536536
// libraries. This may however cause the library to get pulled in in
537537
// situations where it isn't useful, such as for dylibs, though this is
538538
// harmless aside from code size.
539-
if (!IRGen.Opts.UseJIT) {
539+
if (!IRGen.Opts.UseJIT && !Context.LangOpts.hasFeature(Feature::Embedded)) {
540540
auto addBackDeployLib = [&](llvm::VersionTuple version,
541541
StringRef libraryName, bool forceLoad) {
542542
llvm::Optional<llvm::VersionTuple> compatibilityVersion;
@@ -1416,7 +1416,6 @@ void IRGenerator::emitLazyDefinitions() {
14161416
LazySpecializedTypeMetadataRecords.clear();
14171417
LazyTypeContextDescriptors.clear();
14181418
LazyOpaqueTypeDescriptors.clear();
1419-
LazyFunctionDefinitions.clear();
14201419
LazyCanonicalSpecializedMetadataAccessors.clear();
14211420
LazyMetadataAccessors.clear();
14221421
LazyWitnessTables.clear();

lib/IRGen/MetadataRequest.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3303,7 +3303,11 @@ llvm::Value *IRGenFunction::emitTypeMetadataRef(CanType type) {
33033303
MetadataResponse
33043304
IRGenFunction::emitTypeMetadataRef(CanType type,
33053305
DynamicMetadataRequest request) {
3306-
assert(!type->getASTContext().LangOpts.hasFeature(Feature::Embedded));
3306+
if (type->getASTContext().LangOpts.hasFeature(Feature::Embedded)) {
3307+
llvm::errs() << "Metadata pointer requested in embedded Swift for type "
3308+
<< type << "\n";
3309+
assert(0 && "metadata used in embedded mode");
3310+
}
33073311

33083312
type = IGM.getRuntimeReifiedType(type);
33093313
// Look through any opaque types we're allowed to.

0 commit comments

Comments
 (0)