Skip to content

Commit 762e9c7

Browse files
eecksteincompnerd
authored andcommitted
Serialization: serialize if the module is a static library
If the `-static` option is specified, store that in the generated swiftmodule file. When de-serializing, recover this information in the representative SILModule. This will be used for code generation on Windows. It is the missing piece to allow static linking to function properly. It additionally opens the path to additional optimization on ELF-ish targets - GOT, PLT references can be avoided when the linked module is known to be static. Co-authored by: Saleem Abdulrasool <[email protected]>
1 parent fba9d83 commit 762e9c7

14 files changed

+50
-2
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,10 @@ class alignas(1 << DeclAlignInBits) Decl {
588588
HasAnyUnavailableValues : 1
589589
);
590590

591-
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1,
591+
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1,
592+
/// If the module is compiled as static library.
593+
StaticLibrary : 1,
594+
592595
/// If the module was or is being compiled with `-enable-testing`.
593596
TestingEnabled : 1,
594597

include/swift/AST/Module.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ class ModuleDecl : public DeclContext, public TypeDecl {
424424
DebugClient = R;
425425
}
426426

427+
/// Returns true if this module is compiled as static library.
428+
bool isStaticLibrary() const {
429+
return Bits.ModuleDecl.StaticLibrary;
430+
}
431+
void setStaticLibrary(bool isStatic = true) {
432+
Bits.ModuleDecl.StaticLibrary = isStatic;
433+
}
434+
427435
/// Returns true if this module was or is being compiled for testing.
428436
bool isTestingEnabled() const {
429437
return Bits.ModuleDecl.TestingEnabled;

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ class FrontendOptions {
314314
/// skip nodes entirely, depending on the errors involved.
315315
bool AllowModuleWithCompilerErrors = false;
316316

317+
/// True if the "-static" option is set.
318+
bool Static = false;
319+
317320
/// The different modes for validating TBD against the LLVM IR.
318321
enum class TBDValidationMode {
319322
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
@@ -136,6 +136,7 @@ namespace swift {
136136
bool SerializeOptionsForDebugging = false;
137137
bool IsSIB = false;
138138
bool DisableCrossModuleIncrementalInfo = false;
139+
bool StaticLibrary = false;
139140
};
140141

141142
} // end namespace swift

include/swift/Serialization/Validation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class ExtendedValidationInfo {
9898
struct {
9999
unsigned ArePrivateImportsEnabled : 1;
100100
unsigned IsSIB : 1;
101+
unsigned IsStaticLibrary: 1;
101102
unsigned IsTestable : 1;
102103
unsigned ResilienceStrategy : 2;
103104
unsigned IsImplicitDynamicEnabled : 1;
@@ -131,6 +132,10 @@ class ExtendedValidationInfo {
131132
void setImplicitDynamicEnabled(bool val) {
132133
Bits.IsImplicitDynamicEnabled = val;
133134
}
135+
bool isStaticLibrary() const { return Bits.IsStaticLibrary; }
136+
void setIsStaticLibrary(bool val) {
137+
Bits.IsStaticLibrary = val;
138+
}
134139
bool isTestable() const { return Bits.IsTestable; }
135140
void setIsTestable(bool val) {
136141
Bits.IsTestable = val;

lib/AST/Module.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
475475

476476
setAccess(AccessLevel::Public);
477477

478+
Bits.ModuleDecl.StaticLibrary = 0;
478479
Bits.ModuleDecl.TestingEnabled = 0;
479480
Bits.ModuleDecl.FailedToLoad = 0;
480481
Bits.ModuleDecl.RawResilienceStrategy = 0;

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

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

259259
Opts.SkipInheritedDocs = Args.hasArg(OPT_skip_inherited_docs);
260260

261+
Opts.Static = Args.hasArg(OPT_static);
262+
261263
return false;
262264
}
263265

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
182182
serializationOpts.DisableCrossModuleIncrementalInfo =
183183
opts.DisableCrossModuleIncrementalBuild;
184184

185+
serializationOpts.StaticLibrary = opts.Static;
186+
185187
return serializationOpts;
186188
}
187189

lib/Serialization/ModuleFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,11 @@ class ModuleFile
461461
return Core->Bits.IsTestable;
462462
}
463463

464+
/// Whether this module is compiled as static library.
465+
bool isStaticLibrary() const {
466+
return Core->Bits.IsStaticLibrary;
467+
}
468+
464469
/// Whether the module is resilient. ('-enable-library-evolution')
465470
ResilienceStrategy getResilienceStrategy() const {
466471
return ResilienceStrategy(Core->Bits.ResilienceStrategy);

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ static bool readOptionsBlock(llvm::BitstreamCursor &cursor,
131131
options_block::IsSIBLayout::readRecord(scratch, IsSIB);
132132
extendedInfo.setIsSIB(IsSIB);
133133
break;
134+
case options_block::IS_STATIC_LIBRARY:
135+
extendedInfo.setIsStaticLibrary(true);
136+
break;
134137
case options_block::IS_TESTABLE:
135138
extendedInfo.setIsTestable(true);
136139
break;
@@ -1180,6 +1183,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
11801183
UserModuleVersion = info.userModuleVersion;
11811184
Bits.ArePrivateImportsEnabled = extInfo.arePrivateImportsEnabled();
11821185
Bits.IsSIB = extInfo.isSIB();
1186+
Bits.IsStaticLibrary = extInfo.isStaticLibrary();
11831187
Bits.IsTestable = extInfo.isTestable();
11841188
Bits.ResilienceStrategy = unsigned(extInfo.getResilienceStrategy());
11851189
Bits.IsImplicitDynamicEnabled = extInfo.isImplicitDynamicEnabled();

0 commit comments

Comments
 (0)