Skip to content

Commit 4b9c54b

Browse files
committed
Introduce new SILLinkage and FormalLinkage for package decls.
A package access level is currently set to SILLinkage::Public. This limits ability to optimize around resilience and more fine-grained control. This PR sets up a separate SIL linkage for package decls and pipes them down to IRGen. Resolves rdar://121409846
1 parent 4bf5a34 commit 4b9c54b

38 files changed

+503
-321
lines changed

include/swift/IRGen/TBDGen.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ struct TBDGenOptions {
3838
/// Only collect linker directive symbols.
3939
bool LinkerDirectivesOnly = false;
4040

41-
/// Whether to include only symbols with public linkage.
42-
bool PublicSymbolsOnly = true;
41+
/// Whether to include only symbols with public or package linkage.
42+
bool PublicOrPackageSymbolsOnly = true;
4343

4444
/// Whether LLVM IR Virtual Function Elimination is enabled.
4545
bool VirtualFunctionElimination = false;
@@ -75,7 +75,7 @@ struct TBDGenOptions {
7575
return lhs.HasMultipleIGMs == rhs.HasMultipleIGMs &&
7676
lhs.IsInstallAPI == rhs.IsInstallAPI &&
7777
lhs.LinkerDirectivesOnly == rhs.LinkerDirectivesOnly &&
78-
lhs.PublicSymbolsOnly == rhs.PublicSymbolsOnly &&
78+
lhs.PublicOrPackageSymbolsOnly == rhs.PublicOrPackageSymbolsOnly &&
7979
lhs.VirtualFunctionElimination == rhs.VirtualFunctionElimination &&
8080
lhs.WitnessMethodElimination == rhs.WitnessMethodElimination &&
8181
lhs.InstallName == rhs.InstallName &&
@@ -94,7 +94,7 @@ struct TBDGenOptions {
9494
using namespace llvm;
9595
return hash_combine(
9696
opts.HasMultipleIGMs, opts.IsInstallAPI, opts.LinkerDirectivesOnly,
97-
opts.PublicSymbolsOnly, opts.VirtualFunctionElimination,
97+
opts.PublicOrPackageSymbolsOnly, opts.VirtualFunctionElimination,
9898
opts.WitnessMethodElimination,
9999
opts.InstallName, opts.ModuleLinkName,
100100
opts.CurrentVersion, opts.CompatibilityVersion,

include/swift/SIL/FormalLinkage.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ enum class FormalLinkage {
3434
/// have a unique file that is known to define it.
3535
PublicNonUnique,
3636

37+
/// This entity is visible in multiple Swift modules within a package
38+
/// and has a unique file that is known to define it.
39+
PackageUnique,
40+
3741
/// This entity is visible in only a single Swift module and has a
3842
/// unique file that is known to define it.
3943
HiddenUnique,

include/swift/SIL/SILLinkage.h

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ enum class SILLinkage : uint8_t {
5858
/// PublicNonABI functions must be definitions.
5959
PublicNonABI,
6060

61+
/// Visible to multiple Swift modules within a package.
62+
Package,
63+
64+
/// Used for default argument expressions and `@_alwaysEmitIntoClient`.
65+
PackageNonABI,
66+
6167
/// This object definition is visible only to the current Swift
6268
/// module (and thus should not be visible across linkage-unit
6369
/// boundaries). There are no other object definitions with this
@@ -91,6 +97,9 @@ enum class SILLinkage : uint8_t {
9197
/// definition.
9298
PublicExternal,
9399

100+
/// Used to reference a package symbol in a different package.
101+
PackageExternal,
102+
94103
/// A Public or Hidden definition with the same name as this object
95104
/// will be defined by the current Swift module at runtime.
96105
///
@@ -166,20 +175,35 @@ enum class SubclassScope : uint8_t {
166175
/// Strip external from public_external, hidden_external. Otherwise just return
167176
/// the linkage.
168177
inline SILLinkage stripExternalFromLinkage(SILLinkage linkage) {
169-
if (linkage == SILLinkage::PublicExternal)
178+
switch (linkage) {
179+
case SILLinkage::PublicExternal:
170180
return SILLinkage::Public;
171-
if (linkage == SILLinkage::HiddenExternal)
181+
case SILLinkage::PackageExternal:
182+
return SILLinkage::Package;
183+
case SILLinkage::HiddenExternal:
172184
return SILLinkage::Hidden;
173-
return linkage;
185+
case SILLinkage::Public:
186+
case SILLinkage::PublicNonABI:
187+
case SILLinkage::Package:
188+
case SILLinkage::PackageNonABI:
189+
case SILLinkage::Hidden:
190+
case SILLinkage::Shared:
191+
case SILLinkage::Private:
192+
return linkage;
193+
}
194+
llvm_unreachable("Unhandled SILLinkage in switch.");
174195
}
175196

176197
/// Add the 'external' attribute to \p linkage.
177198
inline SILLinkage addExternalToLinkage(SILLinkage linkage) {
178199
switch (linkage) {
179200
case SILLinkage::Public:
180201
return SILLinkage::PublicExternal;
202+
case SILLinkage::Package:
203+
return SILLinkage::PackageExternal;
181204
case SILLinkage::PublicNonABI:
182-
// An external reference to a public non-ABI function is only valid
205+
case SILLinkage::PackageNonABI:
206+
// An external reference to a public or package non-ABI function is only valid
183207
// if the function was emitted in another translation unit of the
184208
// same Swift module, so we treat it as hidden here.
185209
return SILLinkage::HiddenExternal;
@@ -188,6 +212,7 @@ inline SILLinkage addExternalToLinkage(SILLinkage linkage) {
188212
case SILLinkage::Shared:
189213
case SILLinkage::Private:
190214
case SILLinkage::PublicExternal:
215+
case SILLinkage::PackageExternal:
191216
case SILLinkage::HiddenExternal:
192217
return linkage;
193218
}
@@ -198,15 +223,15 @@ inline SILLinkage addExternalToLinkage(SILLinkage linkage) {
198223
/// Return whether the linkage indicates that an object has a
199224
/// definition outside the current SILModule.
200225
inline bool isAvailableExternally(SILLinkage linkage) {
201-
return linkage >= SILLinkage::PublicExternal;
226+
return linkage >= SILLinkage::PublicExternal; // ESQ: package external
202227
}
203228

204229
/// Return whether the given linkage indicates that an object's
205230
/// definition might be required outside the current SILModule.
206231
/// If \p is true then we are in whole-module compilation.
207232
inline bool isPossiblyUsedExternally(SILLinkage linkage, bool wholeModule) {
208233
if (wholeModule) {
209-
return linkage <= SILLinkage::PublicNonABI;
234+
return linkage <= SILLinkage::PackageNonABI; // ESQ: change this to linkage method
210235
}
211236
return linkage <= SILLinkage::Hidden;
212237
}
@@ -219,6 +244,9 @@ inline bool hasPublicVisibility(SILLinkage linkage) {
219244
case SILLinkage::PublicExternal:
220245
case SILLinkage::PublicNonABI:
221246
return true;
247+
case SILLinkage::Package:
248+
case SILLinkage::PackageExternal:
249+
case SILLinkage::PackageNonABI:
222250
case SILLinkage::Hidden:
223251
case SILLinkage::Shared:
224252
case SILLinkage::Private:
@@ -236,6 +264,9 @@ inline bool hasSharedVisibility(SILLinkage linkage) {
236264
case SILLinkage::Public:
237265
case SILLinkage::PublicExternal:
238266
case SILLinkage::PublicNonABI:
267+
case SILLinkage::Package:
268+
case SILLinkage::PackageExternal:
269+
case SILLinkage::PackageNonABI:
239270
case SILLinkage::Hidden:
240271
case SILLinkage::HiddenExternal:
241272
case SILLinkage::Private:
@@ -252,6 +283,9 @@ inline bool hasPrivateVisibility(SILLinkage linkage) {
252283
case SILLinkage::Public:
253284
case SILLinkage::PublicExternal:
254285
case SILLinkage::PublicNonABI:
286+
case SILLinkage::Package:
287+
case SILLinkage::PackageExternal:
288+
case SILLinkage::PackageNonABI:
255289
case SILLinkage::Hidden:
256290
case SILLinkage::HiddenExternal:
257291
case SILLinkage::Shared:
@@ -266,9 +300,9 @@ inline SILLinkage effectiveLinkageForClassMember(SILLinkage linkage,
266300
switch (scope) {
267301
case SubclassScope::External:
268302
if (linkage == SILLinkage::Private || linkage == SILLinkage::Hidden)
269-
return SILLinkage::Public;
303+
return SILLinkage::Public; // ESQ: package?
270304
if (linkage == SILLinkage::HiddenExternal)
271-
return SILLinkage::PublicExternal;
305+
return SILLinkage::PublicExternal; // ESQ: package?
272306
break;
273307

274308
case SubclassScope::Internal:

include/swift/SIL/SILSymbolVisitor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ struct SILSymbolVisitorOptions {
3131
/// are needed (e.g. decls with `@_originallyDefinedIn`.
3232
bool LinkerDirectivesOnly = false;
3333

34-
/// Whether to only visit symbols with public linkage.
35-
bool PublicSymbolsOnly = true;
34+
/// Whether to only visit symbols with public or package linkage.
35+
bool PublicOrPackageSymbolsOnly = true;
3636

3737
/// Whether LLVM IR Virtual Function Elimination is enabled.
3838
bool VirtualFunctionElimination = false;

lib/IRGen/GenDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,11 +2330,13 @@ getIRLinkage(StringRef name, const UniversalLinkageInfo &info,
23302330

23312331
switch (linkage) {
23322332
case SILLinkage::Public:
2333+
case SILLinkage::Package:
23332334
return {llvm::GlobalValue::ExternalLinkage, PublicDefinitionVisibility,
23342335
info.Internalize ? llvm::GlobalValue::DefaultStorageClass
23352336
: ExportedStorage};
23362337

23372338
case SILLinkage::PublicNonABI:
2339+
case SILLinkage::PackageNonABI:
23382340
return isDefinition ? RESULT(WeakODR, Hidden, Default)
23392341
: RESULT(External, Hidden, Default);
23402342

@@ -2359,6 +2361,7 @@ getIRLinkage(StringRef name, const UniversalLinkageInfo &info,
23592361
return {linkage, visibility, llvm::GlobalValue::DefaultStorageClass};
23602362
}
23612363

2364+
case SILLinkage::PackageExternal:
23622365
case SILLinkage::PublicExternal: {
23632366
if (isDefinition)
23642367
return RESULT(AvailableExternally, Default, Default);

lib/IRGen/GenDiffWitness.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void IRGenModule::emitSILDifferentiabilityWitness(
3131
if (dw->isDeclaration())
3232
return;
3333
// Don't emit `public_external` witnesses.
34-
if (dw->getLinkage() == SILLinkage::PublicExternal)
34+
if (dw->getLinkage() == SILLinkage::PublicExternal) // ESQ: package external
3535
return;
3636
ConstantInitBuilder builder(*this);
3737
auto diffWitnessContents = builder.beginStruct();

lib/IRGen/GenInit.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ void IRGenModule::emitSILGlobalVariable(SILGlobalVariable *var) {
4646
auto DbgTy = DebugTypeInfo::getGlobal(var, Int8Ty, *this);
4747
DebugInfo->emitGlobalVariableDeclaration(
4848
nullptr, var->getDecl()->getName().str(), "", DbgTy,
49-
var->getLinkage() != SILLinkage::Public, SILLocation(var->getDecl()));
49+
var->getLinkage() != SILLinkage::Public, // ESQ: package
50+
SILLocation(var->getDecl()));
5051
}
5152
return;
5253
}

lib/IRGen/GenMeta.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2131,13 +2131,16 @@ namespace {
21312131
.getLinkage(NotForDefinition)) {
21322132
case SILLinkage::Public:
21332133
case SILLinkage::PublicExternal:
2134+
case SILLinkage::Package:
2135+
case SILLinkage::PackageExternal:
21342136
case SILLinkage::Hidden:
21352137
case SILLinkage::HiddenExternal:
21362138
case SILLinkage::Private:
21372139
return true;
21382140

21392141
case SILLinkage::Shared:
21402142
case SILLinkage::PublicNonABI:
2143+
case SILLinkage::PackageNonABI:
21412144
return false;
21422145
}
21432146
llvm_unreachable("covered switch");
@@ -7017,7 +7020,7 @@ ExtendedExistentialTypeShapeInfo::get(
70177020
.getCanonicalSignature();
70187021

70197022
auto linkage = getExistentialShapeLinkage(genSig, shapeType);
7020-
assert(linkage != FormalLinkage::PublicUnique);
7023+
assert(linkage != FormalLinkage::PublicUnique && linkage != FormalLinkage::PackageUnique);
70217024

70227025
return { genSig, shapeType, SubstitutionMap(), linkage };
70237026
}

lib/IRGen/GenProto.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2412,7 +2412,12 @@ static void addWTableTypeMetadata(IRGenModule &IGM,
24122412
vis = VCallVisibility::VCallVisibilityLinkageUnit;
24132413
break;
24142414
case SILLinkage::Public:
2415-
default:
2415+
case SILLinkage::PublicExternal:
2416+
case SILLinkage::PublicNonABI:
2417+
case SILLinkage::Package:
2418+
case SILLinkage::PackageExternal:
2419+
case SILLinkage::PackageNonABI:
2420+
case SILLinkage::HiddenExternal:
24162421
if (IGM.getOptions().InternalizeAtLink) {
24172422
vis = VCallVisibility::VCallVisibilityLinkageUnit;
24182423
}

lib/IRGen/IRGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ getSymbolSourcesToEmit(const IRGenDescriptor &desc) {
10851085
// making sure to include non-public symbols.
10861086
auto &ctx = desc.getParentModule()->getASTContext();
10871087
auto tbdDesc = desc.getTBDGenDescriptor();
1088-
tbdDesc.getOptions().PublicSymbolsOnly = false;
1088+
tbdDesc.getOptions().PublicOrPackageSymbolsOnly = false;
10891089
const auto *symbolMap =
10901090
llvm::cantFail(ctx.evaluator(SymbolSourceMapRequest{std::move(tbdDesc)}));
10911091

0 commit comments

Comments
 (0)