Skip to content

Commit da70b97

Browse files
committed
[PassManager] Process the function in a bottom-up order.
This commit changes the order in which we process the functions in the module from module order to bottom-up order. Right now this change should not make any noticeable difference, but soon we'll be able to make the inliner a function pass and improve the caching of our inter-procedural analysis like side-effects analysis.
1 parent 422d466 commit da70b97

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

lib/SILPasses/PassManager/PassManager.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/SILPasses/Transforms.h"
2020
#include "llvm/ADT/Statistic.h"
2121
#include "llvm/ADT/StringSwitch.h"
22+
#include "swift/SILAnalysis/FunctionOrder.h"
2223
#include "llvm/Support/CommandLine.h"
2324
#include "llvm/Support/Debug.h"
2425
#include "llvm/Support/TimeValue.h"
@@ -159,20 +160,30 @@ SILPassManager::SILPassManager(SILModule *M, llvm::StringRef Stage) :
159160
bool SILPassManager::runFunctionPasses(PassList FuncTransforms) {
160161
const SILOptions &Options = getOptions();
161162

162-
for (auto &F : *Mod) {
163-
if (F.empty())
163+
BasicCalleeAnalysis *BCA = getAnalysis<BasicCalleeAnalysis>();
164+
BottomUpFunctionOrder BottomUpOrder(*Mod, BCA);
165+
auto BottomUpFunctions = BottomUpOrder.getFunctions();
166+
167+
for (auto I = BottomUpFunctions.rbegin(),
168+
E = BottomUpFunctions.rend();
169+
I != E; ++I) {
170+
SILFunction *F = *I;
171+
172+
if (F->empty())
164173
continue;
165174

166175
// Don't optimize functions that are marked with the opt.never attribute.
167-
if (!F.shouldOptimize())
176+
if (!F->shouldOptimize())
168177
continue;
169178

170-
CompletedPasses &completedPasses = CompletedPassesMap[&F];
179+
CompletedPasses &completedPasses = CompletedPassesMap[F];
180+
181+
171182

172183
for (auto SFT : FuncTransforms) {
173184
PrettyStackTraceSILFunctionTransform X(SFT);
174185
SFT->injectPassManager(this);
175-
SFT->injectFunction(&F);
186+
SFT->injectFunction(F);
176187

177188
// If nothing changed since the last run of this pass, we can skip this
178189
// pass.
@@ -187,13 +198,13 @@ bool SILPassManager::runFunctionPasses(PassList FuncTransforms) {
187198
if (SILPrintPassName)
188199
llvm::dbgs() << "#" << NumPassesRun << " Stage: " << StageName
189200
<< " Pass: " << SFT->getName()
190-
<< ", Function: " << F.getName() << "\n";
201+
<< ", Function: " << F->getName() << "\n";
191202

192-
if (doPrintBefore(SFT, &F)) {
203+
if (doPrintBefore(SFT, F)) {
193204
llvm::dbgs() << "*** SIL function before " << StageName << " "
194205
<< SFT->getName() << " (" << NumOptimizationIterations
195206
<< ") ***\n";
196-
F.dump(Options.EmitVerboseSIL);
207+
F->dump(Options.EmitVerboseSIL);
197208
}
198209

199210
llvm::sys::TimeValue StartTime = llvm::sys::TimeValue::now();
@@ -202,17 +213,17 @@ bool SILPassManager::runFunctionPasses(PassList FuncTransforms) {
202213
if (SILPrintPassTime) {
203214
auto Delta = llvm::sys::TimeValue::now().nanoseconds() -
204215
StartTime.nanoseconds();
205-
llvm::dbgs() << Delta << " (" << SFT->getName() << "," << F.getName()
216+
llvm::dbgs() << Delta << " (" << SFT->getName() << "," << F->getName()
206217
<< ")\n";
207218
}
208219

209220
// If this pass invalidated anything, print and verify.
210-
if (doPrintAfter(SFT, &F,
221+
if (doPrintAfter(SFT, F,
211222
currentPassHasInvalidated && SILPrintAll)) {
212223
llvm::dbgs() << "*** SIL function after " << StageName << " "
213224
<< SFT->getName() << " (" << NumOptimizationIterations
214225
<< ") ***\n";
215-
F.dump(Options.EmitVerboseSIL);
226+
F->dump(Options.EmitVerboseSIL);
216227
}
217228

218229
// Remember if this pass didn't change anything.
@@ -221,8 +232,8 @@ bool SILPassManager::runFunctionPasses(PassList FuncTransforms) {
221232

222233
if (Options.VerifyAll &&
223234
(currentPassHasInvalidated || SILVerifyWithoutInvalidation)) {
224-
F.verify();
225-
verifyAnalyses(&F);
235+
F->verify();
236+
verifyAnalyses(F);
226237
}
227238

228239
++NumPassesRun;

0 commit comments

Comments
 (0)