2018-08-28 23:06:51 +02:00
|
|
|
//===- PassTimingInfo.cpp - LLVM Pass Timing Implementation ---------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-08-28 23:06:51 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the LLVM Pass Timing infrastructure for both
|
|
|
|
// new and legacy pass managers.
|
|
|
|
//
|
2018-09-26 15:01:43 +02:00
|
|
|
// PassTimingInfo Class - This class is used to calculate information about the
|
2018-08-28 23:06:51 +02:00
|
|
|
// amount of time each pass takes to execute. This only happens when
|
|
|
|
// -time-passes is enabled on the command line.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/IR/PassTimingInfo.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-10-06 00:32:01 +02:00
|
|
|
#include "llvm/IR/PassInstrumentation.h"
|
2018-08-28 23:06:51 +02:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2018-09-04 08:12:28 +02:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2018-08-28 23:06:51 +02:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/Mutex.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-10-04 14:49:57 +02:00
|
|
|
#include <memory>
|
2018-08-28 23:06:51 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-10-06 00:32:01 +02:00
|
|
|
#define DEBUG_TYPE "time-passes"
|
|
|
|
|
2018-08-28 23:06:51 +02:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
bool TimePassesIsEnabled = false;
|
2018-09-26 15:01:43 +02:00
|
|
|
|
2018-08-28 23:06:51 +02:00
|
|
|
static cl::opt<bool, true> EnableTiming(
|
|
|
|
"time-passes", cl::location(TimePassesIsEnabled), cl::Hidden,
|
|
|
|
cl::desc("Time each pass, printing elapsed time for each on exit"));
|
|
|
|
|
|
|
|
namespace {
|
2018-09-26 15:01:43 +02:00
|
|
|
namespace legacy {
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-06 00:32:01 +02:00
|
|
|
// Legacy pass manager's PassTimingInfo implementation
|
2018-09-26 15:01:43 +02:00
|
|
|
|
|
|
|
/// Provides an interface for collecting pass timing information.
|
|
|
|
///
|
|
|
|
/// It was intended to be generic but now we decided to split
|
|
|
|
/// interfaces completely. This is now exclusively for legacy-pass-manager use.
|
|
|
|
class PassTimingInfo {
|
|
|
|
public:
|
|
|
|
using PassInstanceID = void *;
|
|
|
|
|
|
|
|
private:
|
|
|
|
StringMap<unsigned> PassIDCountMap; ///< Map that counts instances of passes
|
2018-10-04 14:49:57 +02:00
|
|
|
DenseMap<PassInstanceID, std::unique_ptr<Timer>> TimingData; ///< timers for pass instances
|
2018-09-26 15:01:43 +02:00
|
|
|
TimerGroup TG;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Default constructor for yet-inactive timeinfo.
|
|
|
|
/// Use \p init() to activate it.
|
|
|
|
PassTimingInfo();
|
|
|
|
|
|
|
|
/// Print out timing information and release timers.
|
|
|
|
~PassTimingInfo();
|
|
|
|
|
|
|
|
/// Initializes the static \p TheTimeInfo member to a non-null value when
|
|
|
|
/// -time-passes is enabled. Leaves it null otherwise.
|
|
|
|
///
|
|
|
|
/// This method may be called multiple times.
|
|
|
|
static void init();
|
|
|
|
|
|
|
|
/// Prints out timing information and then resets the timers.
|
2019-03-23 00:11:08 +01:00
|
|
|
/// By default it uses the stream created by CreateInfoOutputFile().
|
|
|
|
void print(raw_ostream *OutStream = nullptr);
|
2018-09-26 15:01:43 +02:00
|
|
|
|
|
|
|
/// Returns the timer for the specified pass if it exists.
|
|
|
|
Timer *getPassTimer(Pass *, PassInstanceID);
|
|
|
|
|
|
|
|
static PassTimingInfo *TheTimeInfo;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Timer *newPassTimer(StringRef PassID, StringRef PassDesc);
|
|
|
|
};
|
|
|
|
|
2018-08-28 23:06:51 +02:00
|
|
|
static ManagedStatic<sys::SmartMutex<true>> TimingInfoMutex;
|
|
|
|
|
2018-09-26 15:01:43 +02:00
|
|
|
PassTimingInfo::PassTimingInfo()
|
2018-08-28 23:06:51 +02:00
|
|
|
: TG("pass", "... Pass execution timing report ...") {}
|
|
|
|
|
2018-09-26 15:01:43 +02:00
|
|
|
PassTimingInfo::~PassTimingInfo() {
|
2018-08-28 23:06:51 +02:00
|
|
|
// Deleting the timers accumulates their info into the TG member.
|
|
|
|
// Then TG member is (implicitly) deleted, actually printing the report.
|
2018-10-04 14:49:57 +02:00
|
|
|
TimingData.clear();
|
2018-08-28 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 15:01:43 +02:00
|
|
|
void PassTimingInfo::init() {
|
2018-08-28 23:06:51 +02:00
|
|
|
if (!TimePassesIsEnabled || TheTimeInfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Constructed the first time this is called, iff -time-passes is enabled.
|
2018-09-04 08:12:28 +02:00
|
|
|
// This guarantees that the object will be constructed after static globals,
|
2018-08-28 23:06:51 +02:00
|
|
|
// thus it will be destroyed before them.
|
|
|
|
static ManagedStatic<PassTimingInfo> TTI;
|
|
|
|
TheTimeInfo = &*TTI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prints out timing information and then resets the timers.
|
2019-03-23 00:11:08 +01:00
|
|
|
void PassTimingInfo::print(raw_ostream *OutStream) {
|
|
|
|
TG.print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
|
|
|
|
}
|
2018-08-28 23:06:51 +02:00
|
|
|
|
2018-09-26 15:01:43 +02:00
|
|
|
Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
|
2018-09-04 08:12:28 +02:00
|
|
|
unsigned &num = PassIDCountMap[PassID];
|
|
|
|
num++;
|
|
|
|
// Appending description with a pass-instance number for all but the first one
|
|
|
|
std::string PassDescNumbered =
|
|
|
|
num <= 1 ? PassDesc.str() : formatv("{0} #{1}", PassDesc, num).str();
|
|
|
|
return new Timer(PassID, PassDescNumbered, TG);
|
|
|
|
}
|
|
|
|
|
2018-09-26 15:01:43 +02:00
|
|
|
Timer *PassTimingInfo::getPassTimer(Pass *P, PassInstanceID Pass) {
|
2018-08-28 23:06:51 +02:00
|
|
|
if (P->getAsPMDataManager())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
init();
|
|
|
|
sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
|
2018-10-04 14:49:57 +02:00
|
|
|
std::unique_ptr<Timer> &T = TimingData[Pass];
|
2018-08-28 23:06:51 +02:00
|
|
|
|
|
|
|
if (!T) {
|
2018-09-04 08:12:28 +02:00
|
|
|
StringRef PassName = P->getPassName();
|
2018-08-28 23:06:51 +02:00
|
|
|
StringRef PassArgument;
|
|
|
|
if (const PassInfo *PI = Pass::lookupPassInfo(P->getPassID()))
|
|
|
|
PassArgument = PI->getPassArgument();
|
2018-10-04 14:49:57 +02:00
|
|
|
T.reset(newPassTimer(PassArgument.empty() ? PassName : PassArgument, PassName));
|
2018-08-28 23:06:51 +02:00
|
|
|
}
|
2018-10-04 14:49:57 +02:00
|
|
|
return T.get();
|
2018-08-28 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 15:01:43 +02:00
|
|
|
PassTimingInfo *PassTimingInfo::TheTimeInfo;
|
|
|
|
} // namespace legacy
|
|
|
|
} // namespace
|
2018-08-28 23:06:51 +02:00
|
|
|
|
|
|
|
Timer *getPassTimer(Pass *P) {
|
2018-09-26 15:01:43 +02:00
|
|
|
legacy::PassTimingInfo::init();
|
|
|
|
if (legacy::PassTimingInfo::TheTimeInfo)
|
|
|
|
return legacy::PassTimingInfo::TheTimeInfo->getPassTimer(P, P);
|
2018-08-28 23:06:51 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If timing is enabled, report the times collected up to now and then reset
|
|
|
|
/// them.
|
2019-03-23 00:11:08 +01:00
|
|
|
void reportAndResetTimings(raw_ostream *OutStream) {
|
2018-09-26 15:01:43 +02:00
|
|
|
if (legacy::PassTimingInfo::TheTimeInfo)
|
2019-03-23 00:11:08 +01:00
|
|
|
legacy::PassTimingInfo::TheTimeInfo->print(OutStream);
|
2018-08-28 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2018-10-06 00:32:01 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass timing handling for the New Pass Manager
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Returns the timer for the specified pass invocation of \p PassID.
|
|
|
|
/// Each time it creates a new timer.
|
|
|
|
Timer &TimePassesHandler::getPassTimer(StringRef PassID) {
|
2020-03-27 19:38:32 +01:00
|
|
|
// Take a vector of Timers created for this \p PassID and append
|
|
|
|
// one more timer to it.
|
|
|
|
TimerVector &Timers = TimingData[PassID];
|
|
|
|
unsigned Count = Timers.size() + 1;
|
2018-10-06 00:32:01 +02:00
|
|
|
|
|
|
|
std::string FullDesc = formatv("{0} #{1}", PassID, Count).str();
|
|
|
|
|
|
|
|
Timer *T = new Timer(PassID, FullDesc, TG);
|
2020-03-27 19:38:32 +01:00
|
|
|
Timers.emplace_back(T);
|
|
|
|
assert(Count == Timers.size() && "sanity check");
|
|
|
|
|
|
|
|
return *T;
|
2018-10-06 00:32:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TimePassesHandler::TimePassesHandler(bool Enabled)
|
|
|
|
: TG("pass", "... Pass execution timing report ..."), Enabled(Enabled) {}
|
|
|
|
|
2019-03-15 23:15:23 +01:00
|
|
|
void TimePassesHandler::setOutStream(raw_ostream &Out) {
|
|
|
|
OutStream = &Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimePassesHandler::print() {
|
|
|
|
if (!Enabled)
|
|
|
|
return;
|
2019-03-23 00:11:08 +01:00
|
|
|
TG.print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
|
2019-03-15 23:15:23 +01:00
|
|
|
}
|
2018-10-06 00:32:01 +02:00
|
|
|
|
|
|
|
LLVM_DUMP_METHOD void TimePassesHandler::dump() const {
|
|
|
|
dbgs() << "Dumping timers for " << getTypeName<TimePassesHandler>()
|
|
|
|
<< ":\n\tRunning:\n";
|
|
|
|
for (auto &I : TimingData) {
|
2020-03-27 19:38:32 +01:00
|
|
|
StringRef PassID = I.getKey();
|
|
|
|
const TimerVector& MyTimers = I.getValue();
|
|
|
|
for (unsigned idx = 0; idx < MyTimers.size(); idx++) {
|
|
|
|
const Timer* MyTimer = MyTimers[idx].get();
|
|
|
|
if (MyTimer && MyTimer->isRunning())
|
|
|
|
dbgs() << "\tTimer " << MyTimer << " for pass " << PassID << "(" << idx << ")\n";
|
|
|
|
}
|
2018-10-06 00:32:01 +02:00
|
|
|
}
|
|
|
|
dbgs() << "\tTriggered:\n";
|
|
|
|
for (auto &I : TimingData) {
|
2020-03-27 19:38:32 +01:00
|
|
|
StringRef PassID = I.getKey();
|
|
|
|
const TimerVector& MyTimers = I.getValue();
|
|
|
|
for (unsigned idx = 0; idx < MyTimers.size(); idx++) {
|
|
|
|
const Timer* MyTimer = MyTimers[idx].get();
|
|
|
|
if (MyTimer && MyTimer->hasTriggered() && !MyTimer->isRunning())
|
|
|
|
dbgs() << "\tTimer " << MyTimer << " for pass " << PassID << "(" << idx << ")\n";
|
|
|
|
}
|
2018-10-06 00:32:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimePassesHandler::startTimer(StringRef PassID) {
|
|
|
|
Timer &MyTimer = getPassTimer(PassID);
|
|
|
|
TimerStack.push_back(&MyTimer);
|
|
|
|
if (!MyTimer.isRunning())
|
|
|
|
MyTimer.startTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimePassesHandler::stopTimer(StringRef PassID) {
|
|
|
|
assert(TimerStack.size() > 0 && "empty stack in popTimer");
|
|
|
|
Timer *MyTimer = TimerStack.pop_back_val();
|
|
|
|
assert(MyTimer && "timer should be present");
|
|
|
|
if (MyTimer->isRunning())
|
|
|
|
MyTimer->stopTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool matchPassManager(StringRef PassID) {
|
|
|
|
size_t prefix_pos = PassID.find('<');
|
|
|
|
if (prefix_pos == StringRef::npos)
|
|
|
|
return false;
|
|
|
|
StringRef Prefix = PassID.substr(0, prefix_pos);
|
|
|
|
return Prefix.endswith("PassManager") || Prefix.endswith("PassAdaptor") ||
|
|
|
|
Prefix.endswith("AnalysisManagerProxy");
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:05:35 +01:00
|
|
|
bool TimePassesHandler::runBeforePass(StringRef PassID) {
|
2018-10-06 00:32:01 +02:00
|
|
|
if (matchPassManager(PassID))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
startTimer(PassID);
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "after runBeforePass(" << PassID << ")\n");
|
|
|
|
LLVM_DEBUG(dump());
|
|
|
|
|
|
|
|
// we are not going to skip this pass, thus return true.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:05:35 +01:00
|
|
|
void TimePassesHandler::runAfterPass(StringRef PassID) {
|
2018-10-06 00:32:01 +02:00
|
|
|
if (matchPassManager(PassID))
|
|
|
|
return;
|
|
|
|
|
|
|
|
stopTimer(PassID);
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "after runAfterPass(" << PassID << ")\n");
|
|
|
|
LLVM_DEBUG(dump());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimePassesHandler::registerCallbacks(PassInstrumentationCallbacks &PIC) {
|
|
|
|
if (!Enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PIC.registerBeforePassCallback(
|
2018-12-11 20:05:35 +01:00
|
|
|
[this](StringRef P, Any) { return this->runBeforePass(P); });
|
2018-10-06 00:32:01 +02:00
|
|
|
PIC.registerAfterPassCallback(
|
2018-12-11 20:05:35 +01:00
|
|
|
[this](StringRef P, Any) { this->runAfterPass(P); });
|
|
|
|
PIC.registerAfterPassInvalidatedCallback(
|
|
|
|
[this](StringRef P) { this->runAfterPass(P); });
|
2018-10-06 00:32:01 +02:00
|
|
|
PIC.registerBeforeAnalysisCallback(
|
2018-12-11 20:05:35 +01:00
|
|
|
[this](StringRef P, Any) { this->runBeforePass(P); });
|
2018-10-06 00:32:01 +02:00
|
|
|
PIC.registerAfterAnalysisCallback(
|
2018-12-11 20:05:35 +01:00
|
|
|
[this](StringRef P, Any) { this->runAfterPass(P); });
|
2018-10-06 00:32:01 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 23:06:51 +02:00
|
|
|
} // namespace llvm
|