Skip to content

Commit e38bc97

Browse files
authored
Merge pull request #71083 from apple/es-sil-pkg
Support a package SILLinkage
2 parents d93d742 + 72a7760 commit e38bc97

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1135
-648
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: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ enum class SILLinkage : uint8_t {
5858
/// PublicNonABI functions must be definitions.
5959
PublicNonABI,
6060

61+
/// Same as \c Public, except the definition is visible within a package
62+
/// of modules.
63+
Package,
64+
65+
/// Similar to \c PublicNonABI, this definition is used for symbols treated
66+
/// as package but do not have package entry points in the generated binary.
67+
/// It's used for default argument expressions and `@_alwaysEmitIntoClient`.
68+
/// When deserialized, this will become \c Shared linkage.
69+
PackageNonABI,
70+
6171
/// This object definition is visible only to the current Swift
6272
/// module (and thus should not be visible across linkage-unit
6373
/// boundaries). There are no other object definitions with this
@@ -91,6 +101,11 @@ enum class SILLinkage : uint8_t {
91101
/// definition.
92102
PublicExternal,
93103

104+
/// Similar to \c PublicExternal.
105+
/// Used to reference a \c Package definition in a different module
106+
/// within a package.
107+
PackageExternal,
108+
94109
/// A Public or Hidden definition with the same name as this object
95110
/// will be defined by the current Swift module at runtime.
96111
///
@@ -166,20 +181,35 @@ enum class SubclassScope : uint8_t {
166181
/// Strip external from public_external, hidden_external. Otherwise just return
167182
/// the linkage.
168183
inline SILLinkage stripExternalFromLinkage(SILLinkage linkage) {
169-
if (linkage == SILLinkage::PublicExternal)
184+
switch (linkage) {
185+
case SILLinkage::PublicExternal:
170186
return SILLinkage::Public;
171-
if (linkage == SILLinkage::HiddenExternal)
187+
case SILLinkage::PackageExternal:
188+
return SILLinkage::Package;
189+
case SILLinkage::HiddenExternal:
172190
return SILLinkage::Hidden;
173-
return linkage;
191+
case SILLinkage::Public:
192+
case SILLinkage::PublicNonABI:
193+
case SILLinkage::Package:
194+
case SILLinkage::PackageNonABI:
195+
case SILLinkage::Hidden:
196+
case SILLinkage::Shared:
197+
case SILLinkage::Private:
198+
return linkage;
199+
}
200+
llvm_unreachable("Unhandled SILLinkage in switch.");
174201
}
175202

176203
/// Add the 'external' attribute to \p linkage.
177204
inline SILLinkage addExternalToLinkage(SILLinkage linkage) {
178205
switch (linkage) {
179206
case SILLinkage::Public:
180207
return SILLinkage::PublicExternal;
208+
case SILLinkage::Package:
209+
return SILLinkage::PackageExternal;
181210
case SILLinkage::PublicNonABI:
182-
// An external reference to a public non-ABI function is only valid
211+
case SILLinkage::PackageNonABI:
212+
// An external reference to a public or package non-ABI function is only valid
183213
// if the function was emitted in another translation unit of the
184214
// same Swift module, so we treat it as hidden here.
185215
return SILLinkage::HiddenExternal;
@@ -188,6 +218,7 @@ inline SILLinkage addExternalToLinkage(SILLinkage linkage) {
188218
case SILLinkage::Shared:
189219
case SILLinkage::Private:
190220
case SILLinkage::PublicExternal:
221+
case SILLinkage::PackageExternal:
191222
case SILLinkage::HiddenExternal:
192223
return linkage;
193224
}
@@ -198,17 +229,43 @@ inline SILLinkage addExternalToLinkage(SILLinkage linkage) {
198229
/// Return whether the linkage indicates that an object has a
199230
/// definition outside the current SILModule.
200231
inline bool isAvailableExternally(SILLinkage linkage) {
201-
return linkage >= SILLinkage::PublicExternal;
232+
switch (linkage) {
233+
case SILLinkage::Public:
234+
case SILLinkage::PublicNonABI:
235+
case SILLinkage::Package:
236+
case SILLinkage::PackageNonABI:
237+
case SILLinkage::Hidden:
238+
case SILLinkage::Shared:
239+
case SILLinkage::Private:
240+
return false;
241+
case SILLinkage::PublicExternal:
242+
case SILLinkage::PackageExternal:
243+
case SILLinkage::HiddenExternal:
244+
return true;
245+
}
246+
llvm_unreachable("Unhandled SILLinkage in switch.");
202247
}
203248

204249
/// Return whether the given linkage indicates that an object's
205250
/// definition might be required outside the current SILModule.
206251
/// If \p is true then we are in whole-module compilation.
207252
inline bool isPossiblyUsedExternally(SILLinkage linkage, bool wholeModule) {
208-
if (wholeModule) {
209-
return linkage <= SILLinkage::PublicNonABI;
253+
switch (linkage) {
254+
case SILLinkage::Public:
255+
case SILLinkage::PublicNonABI:
256+
case SILLinkage::Package:
257+
case SILLinkage::PackageNonABI:
258+
return true;
259+
case SILLinkage::Hidden:
260+
return !wholeModule;
261+
case SILLinkage::Shared:
262+
case SILLinkage::Private:
263+
case SILLinkage::PublicExternal:
264+
case SILLinkage::PackageExternal:
265+
case SILLinkage::HiddenExternal:
266+
return false;
210267
}
211-
return linkage <= SILLinkage::Hidden;
268+
llvm_unreachable("Unhandled SILLinkage in switch.");
212269
}
213270

214271
SILLinkage getDeclSILLinkage(const ValueDecl *decl);
@@ -219,6 +276,9 @@ inline bool hasPublicVisibility(SILLinkage linkage) {
219276
case SILLinkage::PublicExternal:
220277
case SILLinkage::PublicNonABI:
221278
return true;
279+
case SILLinkage::Package:
280+
case SILLinkage::PackageExternal:
281+
case SILLinkage::PackageNonABI:
222282
case SILLinkage::Hidden:
223283
case SILLinkage::Shared:
224284
case SILLinkage::Private:
@@ -236,6 +296,9 @@ inline bool hasSharedVisibility(SILLinkage linkage) {
236296
case SILLinkage::Public:
237297
case SILLinkage::PublicExternal:
238298
case SILLinkage::PublicNonABI:
299+
case SILLinkage::Package:
300+
case SILLinkage::PackageExternal:
301+
case SILLinkage::PackageNonABI:
239302
case SILLinkage::Hidden:
240303
case SILLinkage::HiddenExternal:
241304
case SILLinkage::Private:
@@ -252,6 +315,9 @@ inline bool hasPrivateVisibility(SILLinkage linkage) {
252315
case SILLinkage::Public:
253316
case SILLinkage::PublicExternal:
254317
case SILLinkage::PublicNonABI:
318+
case SILLinkage::Package:
319+
case SILLinkage::PackageExternal:
320+
case SILLinkage::PackageNonABI:
255321
case SILLinkage::Hidden:
256322
case SILLinkage::HiddenExternal:
257323
case SILLinkage::Shared:
@@ -265,12 +331,22 @@ inline SILLinkage effectiveLinkageForClassMember(SILLinkage linkage,
265331
SubclassScope scope) {
266332
switch (scope) {
267333
case SubclassScope::External:
268-
if (linkage == SILLinkage::Private || linkage == SILLinkage::Hidden)
269-
return SILLinkage::Public;
270-
if (linkage == SILLinkage::HiddenExternal)
271-
return SILLinkage::PublicExternal;
334+
switch (linkage) {
335+
case SILLinkage::Hidden:
336+
case SILLinkage::Private:
337+
return SILLinkage::Public;
338+
case SILLinkage::HiddenExternal:
339+
return SILLinkage::PublicExternal;
340+
case SILLinkage::Public:
341+
case SILLinkage::PublicNonABI:
342+
case SILLinkage::Package:
343+
case SILLinkage::PackageNonABI:
344+
case SILLinkage::PublicExternal:
345+
case SILLinkage::PackageExternal:
346+
case SILLinkage::Shared:
347+
break;
348+
}
272349
break;
273-
274350
case SubclassScope::Internal:
275351
if (linkage == SILLinkage::Private)
276352
return SILLinkage::Hidden;

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/GenInit.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ void IRGenModule::emitSILGlobalVariable(SILGlobalVariable *var) {
4444
if (ti.isKnownEmpty(expansion)) {
4545
if (DebugInfo && var->getDecl()) {
4646
auto DbgTy = DebugTypeInfo::getGlobal(var, Int8Ty, *this);
47-
DebugInfo->emitGlobalVariableDeclaration(
48-
nullptr, var->getDecl()->getName().str(), "", DbgTy,
49-
var->getLinkage() != SILLinkage::Public, SILLocation(var->getDecl()));
47+
DebugInfo->emitGlobalVariableDeclaration(nullptr, var->getDecl()->getName().str(),
48+
"", DbgTy,
49+
var->getLinkage() != SILLinkage::Public &&
50+
var->getLinkage() != SILLinkage::Package,
51+
SILLocation(var->getDecl()));
5052
}
5153
return;
5254
}

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

lib/IRGen/IRGenSIL.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,12 +2942,21 @@ FunctionPointer::Kind irgen::classifyFunctionPointerKind(SILFunction *fn) {
29422942
// function's implementation and the function's context size in the async
29432943
// function pointer data structure.
29442944
static bool mayDirectlyCallAsync(SILFunction *fn) {
2945-
if (fn->getLinkage() == SILLinkage::Shared ||
2946-
fn->getLinkage() == SILLinkage::PublicNonABI) {
2945+
switch (fn->getLinkage()) {
2946+
case SILLinkage::PublicNonABI:
2947+
case SILLinkage::PackageNonABI:
2948+
case SILLinkage::Shared:
29472949
return false;
2950+
case SILLinkage::Public:
2951+
case SILLinkage::Package:
2952+
case SILLinkage::Hidden:
2953+
case SILLinkage::Private:
2954+
case SILLinkage::PublicExternal:
2955+
case SILLinkage::PackageExternal:
2956+
case SILLinkage::HiddenExternal:
2957+
return true;
29482958
}
2949-
2950-
return true;
2959+
llvm_unreachable("Invalid SIL linkage");
29512960
}
29522961

29532962
void IRGenSILFunction::visitFunctionRefBaseInst(FunctionRefBaseInst *i) {

lib/IRGen/IRSymbolVisitor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static bool shouldUseAllocatorMangling(const AbstractFunctionDecl *AFD) {
4242
class IRSymbolVisitorImpl : public SILSymbolVisitor {
4343
IRSymbolVisitor &Visitor;
4444
const IRSymbolVisitorContext &Ctx;
45-
bool PublicSymbolsOnly;
45+
bool PublicOrPackageSymbolsOnly;
4646

4747
/// Emits the given `LinkEntity` to the downstream visitor as long as the
4848
/// entity has the required linkage.
@@ -59,7 +59,7 @@ class IRSymbolVisitorImpl : public SILSymbolVisitor {
5959
llvm::GlobalValue::isExternalLinkage(linkage.getLinkage()) &&
6060
linkage.getVisibility() != llvm::GlobalValue::HiddenVisibility;
6161

62-
if (PublicSymbolsOnly && !externallyVisible)
62+
if (PublicOrPackageSymbolsOnly && !externallyVisible)
6363
return;
6464
}
6565

@@ -70,7 +70,7 @@ class IRSymbolVisitorImpl : public SILSymbolVisitor {
7070
IRSymbolVisitorImpl(IRSymbolVisitor &Visitor,
7171
const IRSymbolVisitorContext &Ctx)
7272
: Visitor{Visitor}, Ctx{Ctx},
73-
PublicSymbolsOnly{Ctx.getSILCtx().getOpts().PublicSymbolsOnly} {}
73+
PublicOrPackageSymbolsOnly{Ctx.getSILCtx().getOpts().PublicOrPackageSymbolsOnly} {}
7474

7575
bool willVisitDecl(Decl *D) override {
7676
return Visitor.willVisitDecl(D);

lib/IRGen/Linking.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,8 @@ SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
673673
switch (getTypeMetadataAccessStrategy(getType())) {
674674
case MetadataAccessStrategy::PublicUniqueAccessor:
675675
return getSILLinkage(FormalLinkage::PublicUnique, forDefinition);
676+
case MetadataAccessStrategy::PackageUniqueAccessor:
677+
return getSILLinkage(FormalLinkage::PackageUnique, forDefinition);
676678
case MetadataAccessStrategy::HiddenUniqueAccessor:
677679
return getSILLinkage(FormalLinkage::HiddenUnique, forDefinition);
678680
case MetadataAccessStrategy::PrivateAccessor:
@@ -724,7 +726,8 @@ SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
724726
assert(linkage != FormalLinkage::PublicNonUnique &&
725727
"Cannot have a resilient class with non-unique linkage");
726728

727-
if (linkage == FormalLinkage::PublicUnique)
729+
if (linkage == FormalLinkage::PublicUnique ||
730+
linkage == FormalLinkage::PackageUnique)
728731
linkage = FormalLinkage::HiddenUnique;
729732
}
730733

0 commit comments

Comments
 (0)