From 734d1b4e7aab846d1ac40855799264c2b0bfefd7 Mon Sep 17 00:00:00 2001 From: Mark Lacey Date: Wed, 28 Oct 2015 16:08:56 -0700 Subject: [PATCH] Make the linking pass add new functions to the call graph. If we already have a call graph built, we should add any newly deserialized functions to it. This is happening in other places we're deserializing, but wasn't happening in the linking pass, and we've been getting away with it because we invalidate the entire call graph and rebuild it when we next need it. An upcoming change will invalidate only the call graph node for the current function, so we really need to add these new functions to the call graph as we deserialize them. --- lib/SIL/Linker.cpp | 21 +++++++++++++-------- lib/SILPasses/UtilityPasses/Link.cpp | 10 ++++++++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/SIL/Linker.cpp b/lib/SIL/Linker.cpp index 0c1ccbf3f06..d7280311e06 100644 --- a/lib/SIL/Linker.cpp +++ b/lib/SIL/Linker.cpp @@ -58,18 +58,23 @@ bool SILLinkerVisitor::processFunction(SILFunction *F) { return false; // If F is a declaration, first deserialize it. - auto *NewFn = F->isExternalDeclaration() ? Loader->lookupSILFunction(F) : F; - if (!NewFn || NewFn->isExternalDeclaration()) - return false; + if (F->isExternalDeclaration()) { + auto *NewFn = Loader->lookupSILFunction(F); - // Notify client of new deserialized function. - if (Callback) - Callback(NewFn); + if (!NewFn || NewFn->isExternalDeclaration()) + return false; + + if (Callback) + Callback(NewFn); + + F = NewFn; + } ++NumFuncLinked; - // Try to transitively deserialize everything referenced by NewFn. - Worklist.push_back(NewFn); + // Try to transitively deserialize everything referenced by this + // function. + Worklist.push_back(F); process(); // Since we successfully processed at least one function, return true. diff --git a/lib/SILPasses/UtilityPasses/Link.cpp b/lib/SILPasses/UtilityPasses/Link.cpp index 159f4109487..bee973835f6 100644 --- a/lib/SILPasses/UtilityPasses/Link.cpp +++ b/lib/SILPasses/UtilityPasses/Link.cpp @@ -10,6 +10,8 @@ // //===----------------------------------------------------------------------===// +#include "swift/SILAnalysis/CallGraph.h" +#include "swift/SILAnalysis/CallGraphAnalysis.h" #include "swift/SILPasses/Passes.h" #include "swift/SILPasses/Transforms.h" #include "swift/SIL/SILModule.h" @@ -42,10 +44,14 @@ class SILLinker : public SILModuleTransform { void run() override { // Copies code from the standard library into the user program to enable // optimizations. + auto *CGA = PM->getAnalysis(); + auto *CG = CGA->getCallGraphOrNull(); + CallGraphLinkerEditor Editor(CG); SILModule &M = *getModule(); for (auto &Fn : M) - if (M.linkFunction(&Fn, SILModule::LinkingMode::LinkAll)) - invalidateAnalysis(&Fn, SILAnalysis::PreserveKind::Branches); + if (M.linkFunction(&Fn, SILModule::LinkingMode::LinkAll, + Editor.getCallback())) + invalidateAnalysis(&Fn, SILAnalysis::PreserveKind::ProgramFlow); } StringRef getName() override { return "SIL Linker"; }