Skip to content

Commit 0771f1e

Browse files
authored
Merge pull request #37211 from compnerd/windows-static-linking
IRGen: support static linking on Windows
2 parents d974373 + 8da2c37 commit 0771f1e

17 files changed

+128
-7
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/IRGen/GenDecl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,7 @@ void irgen::updateLinkageForDefinition(IRGenModule &IGM,
20302030
bool isKnownLocal = entity.isAlwaysSharedLinkage();
20312031
if (const auto *DC = entity.getDeclContextForEmission())
20322032
if (const auto *MD = DC->getParentModule())
2033-
isKnownLocal = IGM.getSwiftModule() == MD;
2033+
isKnownLocal = IGM.getSwiftModule() == MD || MD->isStaticLibrary();
20342034

20352035
auto IRL =
20362036
getIRLinkage(linkInfo, entity.getLinkage(ForDefinition),
@@ -2059,7 +2059,7 @@ LinkInfo LinkInfo::get(const UniversalLinkageInfo &linkInfo,
20592059
bool isKnownLocal = entity.isAlwaysSharedLinkage();
20602060
if (const auto *DC = entity.getDeclContextForEmission())
20612061
if (const auto *MD = DC->getParentModule())
2062-
isKnownLocal = MD == swiftModule;
2062+
isKnownLocal = MD == swiftModule || MD->isStaticLibrary();
20632063

20642064
entity.mangle(result.Name);
20652065
bool weakImported = entity.isWeakImported(swiftModule);
@@ -2074,6 +2074,8 @@ LinkInfo LinkInfo::get(const UniversalLinkageInfo &linkInfo, StringRef name,
20742074
bool isWeakImported) {
20752075
LinkInfo result;
20762076

2077+
// TODO(compnerd) handle this properly
2078+
20772079
result.Name += name;
20782080
result.IRL = getIRLinkage(linkInfo, linkage, isDefinition, isWeakImported);
20792081
result.ForDefinition = isDefinition;

lib/IRGen/GenProto.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,9 +1117,15 @@ class DirectConformanceInfo : public ConformanceInfo {
11171117

11181118
llvm::Constant *tryGetConstantTable(IRGenModule &IGM,
11191119
CanType conformingType) const override {
1120-
if (IGM.getOptions().LazyInitializeProtocolConformances &&
1121-
RootConformance->getDeclContext()->getParentModule() != IGM.getSwiftModule())
1122-
return nullptr;
1120+
if (IGM.getOptions().LazyInitializeProtocolConformances) {
1121+
const auto *MD = RootConformance->getDeclContext()->getParentModule();
1122+
// If the protocol conformance is defined in the current module or the
1123+
// module will be statically linked, then we can statically initialize the
1124+
// conformance as we know that the protocol conformance is guaranteed to
1125+
// be present.
1126+
if (!(MD == IGM.getSwiftModule() || MD->isStaticLibrary()))
1127+
return nullptr;
1128+
}
11231129
return IGM.getAddrOfWitnessTable(RootConformance);
11241130
}
11251131
};

0 commit comments

Comments
 (0)