Skip to content

Commit c8b0273

Browse files
committed
[embedded] Avoid changes in isPossiblyUsedExternally, move logic to DFE instead
1 parent 8b1aca9 commit c8b0273

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

lib/SIL/IR/SILFunction.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,6 @@ bool SILFunction::hasValidLinkageForFragileRef() const {
842842
return hasPublicVisibility(getLinkage());
843843
}
844844

845-
static bool loweredFunctionHasGenericArguments(const SILFunction *f) {
846-
auto s = f->getLoweredFunctionType()->getInvocationGenericSignature();
847-
return s && !s->areAllParamsConcrete();
848-
}
849-
850845
bool
851846
SILFunction::isPossiblyUsedExternally() const {
852847
auto linkage = getLinkage();
@@ -874,13 +869,6 @@ SILFunction::isPossiblyUsedExternally() const {
874869
hasOpaqueResultTypeWithAvailabilityConditions())
875870
return true;
876871

877-
// In embedded Swift, generic functions, even public ones cannot be used
878-
// externally.
879-
bool embedded = getASTContext().LangOpts.hasFeature(Feature::Embedded);
880-
bool generic = loweredFunctionHasGenericArguments(this);
881-
if (embedded && generic)
882-
return false;
883-
884872
return swift::isPossiblyUsedExternally(linkage, getModule().isWholeModule());
885873
}
886874

lib/SILOptimizer/IPO/DeadFunctionElimination.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ STATISTIC(NumDeadGlobals, "Number of dead global variables eliminated");
3131

3232
namespace {
3333

34+
static bool loweredFunctionHasGenericArguments(const SILFunction *f) {
35+
auto s = f->getLoweredFunctionType()->getInvocationGenericSignature();
36+
return s && !s->areAllParamsConcrete();
37+
}
38+
3439
class DeadFunctionAndGlobalElimination {
3540
/// Represents a function which is implementing a vtable or witness table
3641
/// method.
@@ -90,6 +95,8 @@ class DeadFunctionAndGlobalElimination {
9095

9196
bool keepExternalWitnessTablesAlive;
9297

98+
bool removeUnspecializedFunctionsInEmbeddedSwift;
99+
93100
/// Checks is a function is alive, e.g. because it is visible externally.
94101
bool isAnchorFunction(SILFunction *F) {
95102

@@ -422,7 +429,15 @@ class DeadFunctionAndGlobalElimination {
422429
findAnchorsInTables();
423430

424431
for (SILFunction &F : *Module) {
425-
if (isAnchorFunction(&F)) {
432+
// In embedded Swift, generic functions, even public ones cannot be used
433+
// externally and are not anchors.
434+
bool embedded =
435+
Module->getASTContext().LangOpts.hasFeature(Feature::Embedded);
436+
bool generic = loweredFunctionHasGenericArguments(&F);
437+
bool ignoreAnchor =
438+
embedded && generic && removeUnspecializedFunctionsInEmbeddedSwift;
439+
440+
if (isAnchorFunction(&F) && !ignoreAnchor) {
426441
LLVM_DEBUG(llvm::dbgs() << " anchor function: " << F.getName() <<"\n");
427442
ensureAlive(&F);
428443
}
@@ -433,7 +448,7 @@ class DeadFunctionAndGlobalElimination {
433448
[this](SILFunction *targetFun) { ensureAlive(targetFun); });
434449

435450
bool retainBecauseFunctionIsNoOpt = !F.shouldOptimize();
436-
if (Module->getASTContext().LangOpts.hasFeature(Feature::Embedded))
451+
if (embedded)
437452
retainBecauseFunctionIsNoOpt = false;
438453

439454
if (retainBecauseFunctionIsNoOpt) {
@@ -692,13 +707,16 @@ class DeadFunctionAndGlobalElimination {
692707
}
693708

694709
public:
695-
DeadFunctionAndGlobalElimination(SILModule *module,
696-
bool keepExternalWitnessTablesAlive) :
697-
Module(module),
698-
keepExternalWitnessTablesAlive(keepExternalWitnessTablesAlive) {}
710+
DeadFunctionAndGlobalElimination(
711+
SILModule *module, bool keepExternalWitnessTablesAlive,
712+
bool removeUnspecializedFunctionsInEmbeddedSwift)
713+
: Module(module),
714+
keepExternalWitnessTablesAlive(keepExternalWitnessTablesAlive),
715+
removeUnspecializedFunctionsInEmbeddedSwift(
716+
removeUnspecializedFunctionsInEmbeddedSwift) {}
699717

700-
/// The main entry point of the optimization.
701-
void eliminateFunctionsAndGlobals(SILModuleTransform *DFEPass) {
718+
/// The main entry point of the optimization.
719+
void eliminateFunctionsAndGlobals(SILModuleTransform *DFEPass) {
702720

703721
LLVM_DEBUG(llvm::dbgs() << "running dead function elimination\n");
704722
findAliveFunctions();
@@ -779,8 +797,10 @@ class DeadFunctionAndGlobalEliminationPass : public SILModuleTransform {
779797
// can eliminate such functions.
780798
getModule()->invalidateSILLoaderCaches();
781799

782-
DeadFunctionAndGlobalElimination deadFunctionElimination(getModule(),
783-
/*keepExternalWitnessTablesAlive*/ !isLateDFE);
800+
DeadFunctionAndGlobalElimination deadFunctionElimination(
801+
getModule(),
802+
/*keepExternalWitnessTablesAlive*/ !isLateDFE,
803+
/*removeUnspecializedFunctionsInEmbeddedSwift*/ isLateDFE);
784804
deadFunctionElimination.eliminateFunctionsAndGlobals(this);
785805
}
786806
};

0 commit comments

Comments
 (0)