Skip to content

Commit c89eca6

Browse files
authored
Enforce consistent usage of -experimental-hermetic-seat-at-link flag (#39986)
We've recently added the -experimental-hermetic-seal-at-link compiler flag, which turns on aggressive dead-stripping optimizations and assumes that library code can be optimized against client code because all users of the library code/types are present at link/LTO time. This means that any module that's built with -experimental-hermetic-seal-at-link requires all clients of this module to also use -experimental-hermetic-seal-at-link. This PR enforces that by storing a bit in the serialized module, and checking the bit when importing modules.
1 parent 775c632 commit c89eca6

18 files changed

+91
-5
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
594594
HasAnyUnavailableValues : 1
595595
);
596596

597-
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1,
597+
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1,
598598
/// If the module is compiled as static library.
599599
StaticLibrary : 1,
600600

@@ -631,6 +631,9 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
631631
/// Whether this module has incremental dependency information available.
632632
HasIncrementalInfo : 1,
633633

634+
/// Whether this module was built with -experimental-hermetic-seal-at-link.
635+
HasHermeticSealAtLink : 1,
636+
634637
/// Whether this module has been compiled with comprehensive checking for
635638
/// concurrency, e.g., Sendable checking.
636639
IsConcurrencyChecked : 1

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,10 @@ WARNING(serialization_malformed_sourceinfo,none,
832832
"'%0' is either malformed or generated by a different Swift version. "
833833
"Note that it uses an unstable format and may leak internal project "
834834
"details, it should not be distributed alongside modules", (StringRef))
835+
ERROR(need_hermetic_seal_to_import_module,none,
836+
"module %0 was built with -experimental-hermetic-seal-at-link, but "
837+
"current compilation does not have -experimental-hermetic-seal-at-link",
838+
(Identifier))
835839

836840
ERROR(reserved_member_name,none,
837841
"type member must not be named %0, since it would conflict with the"

include/swift/AST/Module.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,15 @@ class ModuleDecl
517517
Bits.ModuleDecl.HasIncrementalInfo = enabled;
518518
}
519519

520+
/// Returns true if this module was built with
521+
/// -experimental-hermetic-seal-at-link.
522+
bool hasHermeticSealAtLink() const {
523+
return Bits.ModuleDecl.HasHermeticSealAtLink;
524+
}
525+
void setHasHermeticSealAtLink(bool enabled = true) {
526+
Bits.ModuleDecl.HasHermeticSealAtLink = enabled;
527+
}
528+
520529
/// \returns true if this module is a system module; note that the StdLib is
521530
/// considered a system module.
522531
bool isSystemModule() const {

include/swift/Basic/LangOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,11 @@ namespace swift {
439439
/// Load swiftmodule files in memory as volatile and avoid mmap.
440440
bool EnableVolatileModules = false;
441441

442+
/// Enable experimental 'hermetic seal at link' feature. Turns on
443+
/// dead-stripping optimizations assuming that all users of library code
444+
/// are present at LTO time.
445+
bool HermeticSealAtLink = false;
446+
442447
/// Allow deserializing implementation only dependencies. This should only
443448
/// be set true by lldb and other tooling, so that deserilization
444449
/// recovery issues won't bring down the debugger.

include/swift/Frontend/FrontendOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ class FrontendOptions {
341341
/// True if the "-static" option is set.
342342
bool Static = false;
343343

344+
/// True if building with -experimental-hermetic-seal-at-link. Turns on
345+
/// dead-stripping optimizations assuming that all users of library code
346+
/// are present at LTO time.
347+
bool HermeticSealAtLink = false;
348+
344349
/// The different modes for validating TBD against the LLVM IR.
345350
enum class TBDValidationMode {
346351
Default, ///< Do the default validation for the current platform.

include/swift/Serialization/SerializationOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ namespace swift {
142142
bool IsSIB = false;
143143
bool DisableCrossModuleIncrementalInfo = false;
144144
bool StaticLibrary = false;
145+
bool HermeticSealAtLink = false;
145146
bool IsOSSA = false;
146147
};
147148

include/swift/Serialization/Validation.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ class ExtendedValidationInfo {
109109
struct {
110110
unsigned ArePrivateImportsEnabled : 1;
111111
unsigned IsSIB : 1;
112-
unsigned IsStaticLibrary: 1;
112+
unsigned IsStaticLibrary : 1;
113+
unsigned HasHermeticSealAtLink : 1;
113114
unsigned IsTestable : 1;
114115
unsigned ResilienceStrategy : 2;
115116
unsigned IsImplicitDynamicEnabled : 1;
@@ -148,6 +149,10 @@ class ExtendedValidationInfo {
148149
void setIsStaticLibrary(bool val) {
149150
Bits.IsStaticLibrary = val;
150151
}
152+
bool hasHermeticSealAtLink() const { return Bits.HasHermeticSealAtLink; }
153+
void setHasHermeticSealAtLink(bool val) {
154+
Bits.HasHermeticSealAtLink = val;
155+
}
151156
bool isTestable() const { return Bits.IsTestable; }
152157
void setIsTestable(bool val) {
153158
Bits.IsTestable = val;

lib/AST/Module.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
488488
Bits.ModuleDecl.IsNonSwiftModule = 0;
489489
Bits.ModuleDecl.IsMainModule = 0;
490490
Bits.ModuleDecl.HasIncrementalInfo = 0;
491+
Bits.ModuleDecl.HasHermeticSealAtLink = 0;
491492
Bits.ModuleDecl.IsConcurrencyChecked = 0;
492493
}
493494

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

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

291291
Opts.Static = Args.hasArg(OPT_static);
292292

293+
Opts.HermeticSealAtLink = Args.hasArg(OPT_experimental_hermetic_seal_at_link);
294+
293295
return false;
294296
}
295297

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
736736

737737
Opts.EnableVolatileModules |= Args.hasArg(OPT_enable_volatile_modules);
738738

739+
Opts.HermeticSealAtLink |= Args.hasArg(OPT_experimental_hermetic_seal_at_link);
740+
739741
Opts.UseDarwinPreStableABIBit =
740742
(Target.isMacOSX() && Target.isMacOSXVersionLT(10, 14, 4)) ||
741743
(Target.isiOS() && Target.isOSVersionLT(12, 2)) ||

0 commit comments

Comments
 (0)