Skip to content

Support a package SILLinkage #71083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/swift/IRGen/TBDGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ struct TBDGenOptions {
/// Only collect linker directive symbols.
bool LinkerDirectivesOnly = false;

/// Whether to include only symbols with public linkage.
bool PublicSymbolsOnly = true;
/// Whether to include only symbols with public or package linkage.
bool PublicOrPackageSymbolsOnly = true;

/// Whether LLVM IR Virtual Function Elimination is enabled.
bool VirtualFunctionElimination = false;
Expand Down Expand Up @@ -75,7 +75,7 @@ struct TBDGenOptions {
return lhs.HasMultipleIGMs == rhs.HasMultipleIGMs &&
lhs.IsInstallAPI == rhs.IsInstallAPI &&
lhs.LinkerDirectivesOnly == rhs.LinkerDirectivesOnly &&
lhs.PublicSymbolsOnly == rhs.PublicSymbolsOnly &&
lhs.PublicOrPackageSymbolsOnly == rhs.PublicOrPackageSymbolsOnly &&
lhs.VirtualFunctionElimination == rhs.VirtualFunctionElimination &&
lhs.WitnessMethodElimination == rhs.WitnessMethodElimination &&
lhs.InstallName == rhs.InstallName &&
Expand All @@ -94,7 +94,7 @@ struct TBDGenOptions {
using namespace llvm;
return hash_combine(
opts.HasMultipleIGMs, opts.IsInstallAPI, opts.LinkerDirectivesOnly,
opts.PublicSymbolsOnly, opts.VirtualFunctionElimination,
opts.PublicOrPackageSymbolsOnly, opts.VirtualFunctionElimination,
opts.WitnessMethodElimination,
opts.InstallName, opts.ModuleLinkName,
opts.CurrentVersion, opts.CompatibilityVersion,
Expand Down
4 changes: 4 additions & 0 deletions include/swift/SIL/FormalLinkage.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ enum class FormalLinkage {
/// have a unique file that is known to define it.
PublicNonUnique,

/// This entity is visible in multiple Swift modules within a package
/// and has a unique file that is known to define it.
PackageUnique,

/// This entity is visible in only a single Swift module and has a
/// unique file that is known to define it.
HiddenUnique,
Expand Down
102 changes: 89 additions & 13 deletions include/swift/SIL/SILLinkage.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ enum class SILLinkage : uint8_t {
/// PublicNonABI functions must be definitions.
PublicNonABI,

/// Same as \c Public, except the definition is visible within a package
/// of modules.
Package,

/// Similar to \c PublicNonABI, this definition is used for symbols treated
/// as package but do not have package entry points in the generated binary.
/// It's used for default argument expressions and `@_alwaysEmitIntoClient`.
/// When deserialized, this will become \c Shared linkage.
PackageNonABI,

/// This object definition is visible only to the current Swift
/// module (and thus should not be visible across linkage-unit
/// boundaries). There are no other object definitions with this
Expand Down Expand Up @@ -91,6 +101,11 @@ enum class SILLinkage : uint8_t {
/// definition.
PublicExternal,

/// Similar to \c PublicExternal.
/// Used to reference a \c Package definition in a different module
/// within a package.
PackageExternal,

/// A Public or Hidden definition with the same name as this object
/// will be defined by the current Swift module at runtime.
///
Expand Down Expand Up @@ -166,20 +181,35 @@ enum class SubclassScope : uint8_t {
/// Strip external from public_external, hidden_external. Otherwise just return
/// the linkage.
inline SILLinkage stripExternalFromLinkage(SILLinkage linkage) {
if (linkage == SILLinkage::PublicExternal)
switch (linkage) {
case SILLinkage::PublicExternal:
return SILLinkage::Public;
if (linkage == SILLinkage::HiddenExternal)
case SILLinkage::PackageExternal:
return SILLinkage::Package;
case SILLinkage::HiddenExternal:
return SILLinkage::Hidden;
return linkage;
case SILLinkage::Public:
case SILLinkage::PublicNonABI:
case SILLinkage::Package:
case SILLinkage::PackageNonABI:
case SILLinkage::Hidden:
case SILLinkage::Shared:
case SILLinkage::Private:
return linkage;
}
llvm_unreachable("Unhandled SILLinkage in switch.");
}

/// Add the 'external' attribute to \p linkage.
inline SILLinkage addExternalToLinkage(SILLinkage linkage) {
switch (linkage) {
case SILLinkage::Public:
return SILLinkage::PublicExternal;
case SILLinkage::Package:
return SILLinkage::PackageExternal;
case SILLinkage::PublicNonABI:
// An external reference to a public non-ABI function is only valid
case SILLinkage::PackageNonABI:
// An external reference to a public or package non-ABI function is only valid
// if the function was emitted in another translation unit of the
// same Swift module, so we treat it as hidden here.
return SILLinkage::HiddenExternal;
Expand All @@ -188,6 +218,7 @@ inline SILLinkage addExternalToLinkage(SILLinkage linkage) {
case SILLinkage::Shared:
case SILLinkage::Private:
case SILLinkage::PublicExternal:
case SILLinkage::PackageExternal:
case SILLinkage::HiddenExternal:
return linkage;
}
Expand All @@ -198,17 +229,43 @@ inline SILLinkage addExternalToLinkage(SILLinkage linkage) {
/// Return whether the linkage indicates that an object has a
/// definition outside the current SILModule.
inline bool isAvailableExternally(SILLinkage linkage) {
return linkage >= SILLinkage::PublicExternal;
switch (linkage) {
case SILLinkage::Public:
case SILLinkage::PublicNonABI:
case SILLinkage::Package:
case SILLinkage::PackageNonABI:
case SILLinkage::Hidden:
case SILLinkage::Shared:
case SILLinkage::Private:
return false;
case SILLinkage::PublicExternal:
case SILLinkage::PackageExternal:
case SILLinkage::HiddenExternal:
return true;
}
llvm_unreachable("Unhandled SILLinkage in switch.");
}

/// Return whether the given linkage indicates that an object's
/// definition might be required outside the current SILModule.
/// If \p is true then we are in whole-module compilation.
inline bool isPossiblyUsedExternally(SILLinkage linkage, bool wholeModule) {
if (wholeModule) {
return linkage <= SILLinkage::PublicNonABI;
switch (linkage) {
case SILLinkage::Public:
case SILLinkage::PublicNonABI:
case SILLinkage::Package:
case SILLinkage::PackageNonABI:
return true;
case SILLinkage::Hidden:
return !wholeModule;
case SILLinkage::Shared:
case SILLinkage::Private:
case SILLinkage::PublicExternal:
case SILLinkage::PackageExternal:
case SILLinkage::HiddenExternal:
return false;
}
return linkage <= SILLinkage::Hidden;
llvm_unreachable("Unhandled SILLinkage in switch.");
}

SILLinkage getDeclSILLinkage(const ValueDecl *decl);
Expand All @@ -219,6 +276,9 @@ inline bool hasPublicVisibility(SILLinkage linkage) {
case SILLinkage::PublicExternal:
case SILLinkage::PublicNonABI:
return true;
case SILLinkage::Package:
case SILLinkage::PackageExternal:
case SILLinkage::PackageNonABI:
case SILLinkage::Hidden:
case SILLinkage::Shared:
case SILLinkage::Private:
Expand All @@ -236,6 +296,9 @@ inline bool hasSharedVisibility(SILLinkage linkage) {
case SILLinkage::Public:
case SILLinkage::PublicExternal:
case SILLinkage::PublicNonABI:
case SILLinkage::Package:
case SILLinkage::PackageExternal:
case SILLinkage::PackageNonABI:
case SILLinkage::Hidden:
case SILLinkage::HiddenExternal:
case SILLinkage::Private:
Expand All @@ -252,6 +315,9 @@ inline bool hasPrivateVisibility(SILLinkage linkage) {
case SILLinkage::Public:
case SILLinkage::PublicExternal:
case SILLinkage::PublicNonABI:
case SILLinkage::Package:
case SILLinkage::PackageExternal:
case SILLinkage::PackageNonABI:
case SILLinkage::Hidden:
case SILLinkage::HiddenExternal:
case SILLinkage::Shared:
Expand All @@ -265,12 +331,22 @@ inline SILLinkage effectiveLinkageForClassMember(SILLinkage linkage,
SubclassScope scope) {
switch (scope) {
case SubclassScope::External:
if (linkage == SILLinkage::Private || linkage == SILLinkage::Hidden)
return SILLinkage::Public;
if (linkage == SILLinkage::HiddenExternal)
return SILLinkage::PublicExternal;
switch (linkage) {
case SILLinkage::Hidden:
case SILLinkage::Private:
return SILLinkage::Public;
case SILLinkage::HiddenExternal:
return SILLinkage::PublicExternal;
case SILLinkage::Public:
case SILLinkage::PublicNonABI:
case SILLinkage::Package:
case SILLinkage::PackageNonABI:
case SILLinkage::PublicExternal:
case SILLinkage::PackageExternal:
case SILLinkage::Shared:
break;
}
break;

case SubclassScope::Internal:
if (linkage == SILLinkage::Private)
return SILLinkage::Hidden;
Expand Down
4 changes: 2 additions & 2 deletions include/swift/SIL/SILSymbolVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ struct SILSymbolVisitorOptions {
/// are needed (e.g. decls with `@_originallyDefinedIn`.
bool LinkerDirectivesOnly = false;

/// Whether to only visit symbols with public linkage.
bool PublicSymbolsOnly = true;
/// Whether to only visit symbols with public or package linkage.
bool PublicOrPackageSymbolsOnly = true;

/// Whether LLVM IR Virtual Function Elimination is enabled.
bool VirtualFunctionElimination = false;
Expand Down
3 changes: 3 additions & 0 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2330,11 +2330,13 @@ getIRLinkage(StringRef name, const UniversalLinkageInfo &info,

switch (linkage) {
case SILLinkage::Public:
case SILLinkage::Package:
return {llvm::GlobalValue::ExternalLinkage, PublicDefinitionVisibility,
info.Internalize ? llvm::GlobalValue::DefaultStorageClass
: ExportedStorage};

case SILLinkage::PublicNonABI:
case SILLinkage::PackageNonABI:
return isDefinition ? RESULT(WeakODR, Hidden, Default)
: RESULT(External, Hidden, Default);

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

case SILLinkage::PackageExternal:
case SILLinkage::PublicExternal: {
if (isDefinition)
return RESULT(AvailableExternally, Default, Default);
Expand Down
8 changes: 5 additions & 3 deletions lib/IRGen/GenInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ void IRGenModule::emitSILGlobalVariable(SILGlobalVariable *var) {
if (ti.isKnownEmpty(expansion)) {
if (DebugInfo && var->getDecl()) {
auto DbgTy = DebugTypeInfo::getGlobal(var, Int8Ty, *this);
DebugInfo->emitGlobalVariableDeclaration(
nullptr, var->getDecl()->getName().str(), "", DbgTy,
var->getLinkage() != SILLinkage::Public, SILLocation(var->getDecl()));
DebugInfo->emitGlobalVariableDeclaration(nullptr, var->getDecl()->getName().str(),
"", DbgTy,
var->getLinkage() != SILLinkage::Public &&
var->getLinkage() != SILLinkage::Package,
SILLocation(var->getDecl()));
}
return;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2131,13 +2131,16 @@ namespace {
.getLinkage(NotForDefinition)) {
case SILLinkage::Public:
case SILLinkage::PublicExternal:
case SILLinkage::Package:
case SILLinkage::PackageExternal:
case SILLinkage::Hidden:
case SILLinkage::HiddenExternal:
case SILLinkage::Private:
return true;

case SILLinkage::Shared:
case SILLinkage::PublicNonABI:
case SILLinkage::PackageNonABI:
return false;
}
llvm_unreachable("covered switch");
Expand Down Expand Up @@ -7017,7 +7020,7 @@ ExtendedExistentialTypeShapeInfo::get(
.getCanonicalSignature();

auto linkage = getExistentialShapeLinkage(genSig, shapeType);
assert(linkage != FormalLinkage::PublicUnique);
assert(linkage != FormalLinkage::PublicUnique && linkage != FormalLinkage::PackageUnique);

return { genSig, shapeType, SubstitutionMap(), linkage };
}
Expand Down
7 changes: 6 additions & 1 deletion lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2412,7 +2412,12 @@ static void addWTableTypeMetadata(IRGenModule &IGM,
vis = VCallVisibility::VCallVisibilityLinkageUnit;
break;
case SILLinkage::Public:
default:
case SILLinkage::PublicExternal:
case SILLinkage::PublicNonABI:
case SILLinkage::Package:
case SILLinkage::PackageExternal:
case SILLinkage::PackageNonABI:
case SILLinkage::HiddenExternal:
if (IGM.getOptions().InternalizeAtLink) {
vis = VCallVisibility::VCallVisibilityLinkageUnit;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ getSymbolSourcesToEmit(const IRGenDescriptor &desc) {
// making sure to include non-public symbols.
auto &ctx = desc.getParentModule()->getASTContext();
auto tbdDesc = desc.getTBDGenDescriptor();
tbdDesc.getOptions().PublicSymbolsOnly = false;
tbdDesc.getOptions().PublicOrPackageSymbolsOnly = false;
const auto *symbolMap =
llvm::cantFail(ctx.evaluator(SymbolSourceMapRequest{std::move(tbdDesc)}));

Expand Down
17 changes: 13 additions & 4 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2942,12 +2942,21 @@ FunctionPointer::Kind irgen::classifyFunctionPointerKind(SILFunction *fn) {
// function's implementation and the function's context size in the async
// function pointer data structure.
static bool mayDirectlyCallAsync(SILFunction *fn) {
if (fn->getLinkage() == SILLinkage::Shared ||
fn->getLinkage() == SILLinkage::PublicNonABI) {
switch (fn->getLinkage()) {
case SILLinkage::PublicNonABI:
case SILLinkage::PackageNonABI:
case SILLinkage::Shared:
return false;
case SILLinkage::Public:
case SILLinkage::Package:
case SILLinkage::Hidden:
case SILLinkage::Private:
case SILLinkage::PublicExternal:
case SILLinkage::PackageExternal:
case SILLinkage::HiddenExternal:
return true;
}

return true;
llvm_unreachable("Invalid SIL linkage");
}

void IRGenSILFunction::visitFunctionRefBaseInst(FunctionRefBaseInst *i) {
Expand Down
6 changes: 3 additions & 3 deletions lib/IRGen/IRSymbolVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static bool shouldUseAllocatorMangling(const AbstractFunctionDecl *AFD) {
class IRSymbolVisitorImpl : public SILSymbolVisitor {
IRSymbolVisitor &Visitor;
const IRSymbolVisitorContext &Ctx;
bool PublicSymbolsOnly;
bool PublicOrPackageSymbolsOnly;

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

if (PublicSymbolsOnly && !externallyVisible)
if (PublicOrPackageSymbolsOnly && !externallyVisible)
return;
}

Expand All @@ -70,7 +70,7 @@ class IRSymbolVisitorImpl : public SILSymbolVisitor {
IRSymbolVisitorImpl(IRSymbolVisitor &Visitor,
const IRSymbolVisitorContext &Ctx)
: Visitor{Visitor}, Ctx{Ctx},
PublicSymbolsOnly{Ctx.getSILCtx().getOpts().PublicSymbolsOnly} {}
PublicOrPackageSymbolsOnly{Ctx.getSILCtx().getOpts().PublicOrPackageSymbolsOnly} {}

bool willVisitDecl(Decl *D) override {
return Visitor.willVisitDecl(D);
Expand Down
5 changes: 4 additions & 1 deletion lib/IRGen/Linking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,8 @@ SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
switch (getTypeMetadataAccessStrategy(getType())) {
case MetadataAccessStrategy::PublicUniqueAccessor:
return getSILLinkage(FormalLinkage::PublicUnique, forDefinition);
case MetadataAccessStrategy::PackageUniqueAccessor:
return getSILLinkage(FormalLinkage::PackageUnique, forDefinition);
case MetadataAccessStrategy::HiddenUniqueAccessor:
return getSILLinkage(FormalLinkage::HiddenUnique, forDefinition);
case MetadataAccessStrategy::PrivateAccessor:
Expand Down Expand Up @@ -724,7 +726,8 @@ SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
assert(linkage != FormalLinkage::PublicNonUnique &&
"Cannot have a resilient class with non-unique linkage");

if (linkage == FormalLinkage::PublicUnique)
if (linkage == FormalLinkage::PublicUnique ||
linkage == FormalLinkage::PackageUnique)
linkage = FormalLinkage::HiddenUnique;
}

Expand Down
Loading