Skip to content

Commit aa86808

Browse files
committed
Allow TBDGenVisitor to visit non-public symbols
Add a `PublicSymbolsOnly` option to TBDGenOptions that controls whether to include public-only symbols. This is `true` by default, but will be disabled for symbol mapping.
1 parent 96f3b87 commit aa86808

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

include/swift/TBDGen/TBDGen.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ 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;
43+
4144
/// The install_name to use in the TBD file.
4245
std::string InstallName;
4346

@@ -66,6 +69,7 @@ struct TBDGenOptions {
6669
return lhs.HasMultipleIGMs == rhs.HasMultipleIGMs &&
6770
lhs.IsInstallAPI == rhs.IsInstallAPI &&
6871
lhs.LinkerDirectivesOnly == rhs.LinkerDirectivesOnly &&
72+
lhs.PublicSymbolsOnly == rhs.PublicSymbolsOnly &&
6973
lhs.InstallName == rhs.InstallName &&
7074
lhs.ModuleLinkName == rhs.ModuleLinkName &&
7175
lhs.CurrentVersion == rhs.CurrentVersion &&
@@ -82,8 +86,9 @@ struct TBDGenOptions {
8286
using namespace llvm;
8387
return hash_combine(
8488
opts.HasMultipleIGMs, opts.IsInstallAPI, opts.LinkerDirectivesOnly,
85-
opts.InstallName, opts.ModuleLinkName, opts.CurrentVersion,
86-
opts.CompatibilityVersion, opts.ModuleInstallNameMapPath,
89+
opts.PublicSymbolsOnly, opts.InstallName, opts.ModuleLinkName,
90+
opts.CurrentVersion, opts.CompatibilityVersion,
91+
opts.ModuleInstallNameMapPath,
8792
hash_combine_range(opts.embedSymbolsFromModules.begin(),
8893
opts.embedSymbolsFromModules.end()));
8994
}

lib/TBDGen/TBDGen.cpp

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,10 @@ void TBDGenVisitor::addSymbol(SILDeclRef declRef) {
406406
auto linkage = effectiveLinkageForClassMember(
407407
declRef.getLinkage(ForDefinition),
408408
declRef.getSubclassScope());
409-
if (linkage == SILLinkage::Public)
410-
addSymbol(declRef.mangle(), SymbolSource::forSILDeclRef(declRef));
409+
if (Opts.PublicSymbolsOnly && linkage != SILLinkage::Public)
410+
return;
411+
412+
addSymbol(declRef.mangle(), SymbolSource::forSILDeclRef(declRef));
411413
}
412414

413415
void TBDGenVisitor::addSymbol(LinkEntity entity) {
@@ -418,8 +420,10 @@ void TBDGenVisitor::addSymbol(LinkEntity entity) {
418420
llvm::GlobalValue::isExternalLinkage(linkage.getLinkage()) &&
419421
linkage.getVisibility() != llvm::GlobalValue::HiddenVisibility;
420422

421-
if (externallyVisible)
422-
addSymbol(linkage.getName(), SymbolSource::forIRLinkEntity(entity));
423+
if (Opts.PublicSymbolsOnly && !externallyVisible)
424+
return;
425+
426+
addSymbol(linkage.getName(), SymbolSource::forIRLinkEntity(entity));
423427
}
424428

425429
void TBDGenVisitor::addDispatchThunk(SILDeclRef declRef) {
@@ -486,15 +490,21 @@ void TBDGenVisitor::addConformances(const IterableDeclContext *IDC) {
486490
auto addSymbolIfNecessary = [&](ValueDecl *requirementDecl,
487491
ValueDecl *witnessDecl) {
488492
auto witnessRef = SILDeclRef(witnessDecl);
489-
if (conformanceIsFixed &&
490-
(isa<SelfProtocolConformance>(rootConformance) ||
491-
fixmeWitnessHasLinkageThatNeedsToBePublic(witnessRef))) {
492-
Mangle::ASTMangler Mangler;
493-
494-
// FIXME: We should have a SILDeclRef SymbolSource for this.
495-
addSymbol(Mangler.mangleWitnessThunk(rootConformance, requirementDecl),
496-
SymbolSource::forUnknown());
493+
if (Opts.PublicSymbolsOnly) {
494+
if (!conformanceIsFixed)
495+
return;
496+
497+
if (!isa<SelfProtocolConformance>(rootConformance) &&
498+
!fixmeWitnessHasLinkageThatNeedsToBePublic(witnessRef)) {
499+
return;
500+
}
497501
}
502+
503+
Mangle::ASTMangler Mangler;
504+
505+
// FIXME: We should have a SILDeclRef SymbolSource for this.
506+
addSymbol(Mangler.mangleWitnessThunk(rootConformance, requirementDecl),
507+
SymbolSource::forUnknown());
498508
};
499509

500510
rootConformance->forEachValueWitness([&](ValueDecl *valueReq,
@@ -525,11 +535,11 @@ void TBDGenVisitor::addAutoDiffLinearMapFunction(AbstractFunctionDecl *original,
525535
auto declRef =
526536
SILDeclRef(original).asForeign(requiresForeignEntryPoint(original));
527537

528-
if (!declRef.isSerialized())
529-
return;
530-
// Linear maps are public only when the original function is serialized.
531-
if (!declRef.isSerialized())
538+
// Linear maps are public only when the original function is serialized. So
539+
// if we're only including public symbols and it's not serialized, bail.
540+
if (Opts.PublicSymbolsOnly && !declRef.isSerialized())
532541
return;
542+
533543
// Differential functions are emitted only when forward-mode is enabled.
534544
if (kind == AutoDiffLinearMapKind::Differential &&
535545
!ctx.LangOpts.EnableExperimentalForwardModeDifferentiation)
@@ -573,7 +583,7 @@ void TBDGenVisitor::addDifferentiabilityWitness(
573583
auto originalLinkage = declRef.getLinkage(ForDefinition);
574584
if (foreign)
575585
originalLinkage = stripExternalFromLinkage(originalLinkage);
576-
if (originalLinkage != SILLinkage::Public)
586+
if (Opts.PublicSymbolsOnly && originalLinkage != SILLinkage::Public)
577587
return;
578588

579589
auto *silParamIndices = autodiff::getLoweredParameterIndices(
@@ -631,7 +641,7 @@ static bool shouldUseAllocatorMangling(const AbstractFunctionDecl *afd) {
631641
void TBDGenVisitor::visitDefaultArguments(ValueDecl *VD, ParameterList *PL) {
632642
auto publicDefaultArgGenerators = SwiftModule->isTestingEnabled() ||
633643
SwiftModule->arePrivateImportsEnabled();
634-
if (!publicDefaultArgGenerators)
644+
if (Opts.PublicSymbolsOnly && !publicDefaultArgGenerators)
635645
return;
636646

637647
// In Swift 3 (or under -enable-testing), default arguments (of public
@@ -771,7 +781,8 @@ void TBDGenVisitor::visitVarDecl(VarDecl *VD) {
771781
// statically/globally stored variables have some special handling.
772782
if (VD->hasStorage() &&
773783
isGlobalOrStaticVar(VD)) {
774-
if (getDeclLinkage(VD) == FormalLinkage::PublicUnique) {
784+
if (!Opts.PublicSymbolsOnly ||
785+
getDeclLinkage(VD) == FormalLinkage::PublicUnique) {
775786
// The actual variable has a symbol.
776787
// FIXME: We ought to have a symbol source for this.
777788
Mangle::ASTMangler mangler;
@@ -814,7 +825,8 @@ void TBDGenVisitor::visitNominalTypeDecl(NominalTypeDecl *NTD) {
814825
}
815826

816827
void TBDGenVisitor::visitClassDecl(ClassDecl *CD) {
817-
if (getDeclLinkage(CD) != FormalLinkage::PublicUnique)
828+
if (Opts.PublicSymbolsOnly &&
829+
getDeclLinkage(CD) != FormalLinkage::PublicUnique)
818830
return;
819831

820832
auto &ctxt = CD->getASTContext();

0 commit comments

Comments
 (0)