Files
swift-mirror/lib/SILPasses/PassManager.cpp
Erik Eckstein 72f0cc2ab4 Add more options to dump the SIL during optimizations.
-sil-print-before=<string>     Print out the sil before passes which contain this string in their names
-sil-print-after=<string>      Print out the sil after passes which contain this string in their names
-sil-print-around=<string>     Print out the sil before and after passes which contain this string in their names

Output can further be restricted to a single function with the existing option
-sil-print-only-function=<function>




Swift SVN r23737
2014-12-05 16:52:23 +00:00

278 lines
8.8 KiB
C++

//===----- PassManager.cpp - Swift Pass Manager ---------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-passmanager"
#include "swift/SILPasses/PassManager.h"
#include "swift/SILPasses/Transforms.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILFunction.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/TimeValue.h"
using namespace swift;
STATISTIC(NumOptzIterations, "Number of optimization iterations");
llvm::cl::opt<std::string>
SILPrintOnlyFun("sil-print-only-function", llvm::cl::init(""),
llvm::cl::desc("Only print out the sil for this function"));
llvm::cl::opt<std::string>
SILPrintBefore("sil-print-before", llvm::cl::init(""), llvm::cl::desc(
"Print out the sil before passes which contain this string"));
llvm::cl::opt<std::string>
SILPrintAfter("sil-print-after", llvm::cl::init(""), llvm::cl::desc(
"Print out the sil after passes which contain this string"));
llvm::cl::opt<std::string>
SILPrintAround("sil-print-around", llvm::cl::init(""), llvm::cl::desc(
"Print out the sil before and after passes which contain this string"));
static bool doPrintBefore(SILTransform *T, SILFunction *F) {
if (!SILPrintOnlyFun.empty() && (!F || F->getName() != SILPrintOnlyFun))
return false;
if (!SILPrintBefore.empty() &&
T->getName().find(SILPrintBefore) != StringRef::npos)
return true;
if (!SILPrintAround.empty() &&
T->getName().find(SILPrintAround) != StringRef::npos)
return true;
return false;
}
static bool doPrintAfter(SILTransform *T, SILFunction *F, bool Default) {
if (!SILPrintOnlyFun.empty() && (!F || F->getName() != SILPrintOnlyFun))
return false;
if (!SILPrintAfter.empty() &&
T->getName().find(SILPrintAfter) != StringRef::npos)
return true;
if (!SILPrintAround.empty() &&
T->getName().find(SILPrintAround) != StringRef::npos)
return true;
return Default;
}
bool SILPassManager::
runFunctionPasses(llvm::ArrayRef<SILFunctionTransform*> FuncTransforms) {
CompleteFunctions *CompleteFuncs = getAnalysis<CompleteFunctions>();
const SILOptions &Options = getOptions();
for (auto &F : *Mod) {
if (F.empty() || CompleteFuncs->isComplete(&F))
continue;
for (auto SFT : FuncTransforms) {
CompleteFuncs->resetChanged();
SFT->injectPassManager(this);
SFT->injectFunction(&F);
if (Options.PrintPassName)
llvm::dbgs() << "#" << NumPassesRun << " Pass: " << SFT->getName()
<< ", Function: " << F.getName() << "\n";
if (doPrintBefore(SFT, &F)) {
llvm::dbgs() << "*** SIL function before " << SFT->getName() << " ("
<< NumOptimizationIterations << ") ***\n";
F.dump();
}
llvm::sys::TimeValue StartTime = llvm::sys::TimeValue::now();
SFT->run();
++NumPassesRun;
if (Mod->getStage() == SILStage::Canonical
&& NumPassesRun >= Options.NumOptPassesToRun)
return false;
if (Options.TimeTransforms) {
auto Delta = llvm::sys::TimeValue::now().nanoseconds() -
StartTime.nanoseconds();
llvm::dbgs() << Delta << " (" << SFT->getName() << "," <<
F.getName() << ")\n";
}
// If this pass invalidated anything, print and verify.
if (doPrintAfter(SFT, &F,
CompleteFuncs->hasChanged() && Options.PrintAll)) {
llvm::dbgs() << "*** SIL function after " << SFT->getName() << " ("
<< NumOptimizationIterations << ") ***\n";
F.dump();
}
if (CompleteFuncs->hasChanged() && Options.VerifyAll) {
F.verify();
}
}
}
return true;
}
void SILPassManager::runOneIteration() {
const SILOptions &Options = getOptions();
DEBUG(llvm::dbgs() << "*** Optimizing the module *** \n");
if (Options.PrintAll && NumOptimizationIterations == 0) {
if (SILPrintOnlyFun.empty()) {
llvm::dbgs() << "*** SIL module before transformation ("
<< NumOptimizationIterations << ") ***\n";
Mod->dump();
} else {
for (auto &F : *Mod) {
if (F.getName().str() == SILPrintOnlyFun) {
llvm::dbgs() << "*** SIL function before transformation ("
<< NumOptimizationIterations << ") ***\n";
F.dump();
}
}
}
}
NumOptzIterations++;
NumOptimizationIterations++;
CompleteFunctions *CompleteFuncs = getAnalysis<CompleteFunctions>();
SmallVector<SILFunctionTransform*, 16> PendingFuncTransforms;
// For each transformation:
for (SILTransform *ST : Transformations) {
// Bail out if we've hit the optimization pass limit.
if (Mod->getStage() == SILStage::Canonical
&& NumPassesRun >= Options.NumOptPassesToRun)
return;
// Run module transformations on the module.
if (SILModuleTransform *SMT = llvm::dyn_cast<SILModuleTransform>(ST)) {
// Run all function passes that we've seen since the last module pass. If
// one of the passes asked us to stop the pass pipeline, return false.
if (!runFunctionPasses(PendingFuncTransforms))
return;
PendingFuncTransforms.clear();
CompleteFuncs->resetChanged();
SMT->injectPassManager(this);
SMT->injectModule(Mod);
if (Options.PrintPassName)
llvm::dbgs() << "#" << NumPassesRun << " Pass: " << SMT->getName()
<< " (module pass)\n";
if (doPrintBefore(SMT, nullptr)) {
llvm::dbgs() << "*** SIL module before " << SMT->getName() << " ("
<< NumOptimizationIterations << ") ***\n";
Mod->dump();
}
llvm::sys::TimeValue StartTime = llvm::sys::TimeValue::now();
SMT->run();
++NumPassesRun;
if (Mod->getStage() == SILStage::Canonical
&& NumPassesRun >= Options.NumOptPassesToRun) {
return;
}
if (Options.TimeTransforms) {
auto Delta = llvm::sys::TimeValue::now().nanoseconds() -
StartTime.nanoseconds();
llvm::dbgs() << Delta << " (" << SMT->getName() << ",Module)\n";
}
// If this pass invalidated anything, print and verify.
if (doPrintAfter(SMT, nullptr,
CompleteFuncs->hasChanged() && Options.PrintAll)) {
llvm::dbgs() << "*** SIL module after " << SMT->getName() << " ("
<< NumOptimizationIterations << ") ***\n";
Mod->dump();
}
if (CompleteFuncs->hasChanged() && Options.VerifyAll) {
Mod->verify();
}
continue;
}
// Run function transformation on all functions.
if (SILFunctionTransform *SFT = llvm::dyn_cast<SILFunctionTransform>(ST)) {
PendingFuncTransforms.push_back(SFT);
continue;
}
llvm_unreachable("Unknown pass kind.");
}
runFunctionPasses(PendingFuncTransforms);
CompleteFuncs->setComplete();
}
void SILPassManager::run() {
const SILOptions &Options = getOptions();
if (Options.PrintAll) {
if (SILPrintOnlyFun.empty()) {
llvm::dbgs() << "*** SIL module before transformation ("
<< NumOptimizationIterations << ") ***\n";
Mod->dump();
} else {
for (auto &F : *Mod) {
if (F.getName().str() == SILPrintOnlyFun) {
llvm::dbgs() << "*** SIL function before transformation ("
<< NumOptimizationIterations << ") ***\n";
F.dump();
}
}
}
}
// Keep optimizing the module until no pass requested another iteration
// of the pass or we reach the maximum.
const unsigned IterationLimit = 20;
do {
anotherIteration = false;
runOneIteration();
} while (anotherIteration && NumOptimizationIterations < IterationLimit);
}
/// D'tor.
SILPassManager::~SILPassManager() {
// Free all transformations.
for (auto T : Transformations)
delete T;
// delete the analyis.
for (auto A : Analysis)
delete A;
}
/// \brief Reset the state of the pass manager and remove all transformation
/// owned by the pass manager. Anaysis passes will be kept.
void SILPassManager::resetAndRemoveTransformations() {
for (auto T : Transformations)
delete T;
Transformations.clear();
NumOptimizationIterations = 0;
anotherIteration = false;
CompleteFunctions *CompleteFuncs = getAnalysis<CompleteFunctions>();
CompleteFuncs->reset();
}
const SILOptions &SILPassManager::getOptions() const {
return Mod->getOptions();
}