2018-08-29 05:06:51 +08:00
|
|
|
//===- PassTimingInfo.cpp - LLVM Pass Timing Implementation ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the LLVM Pass Timing infrastructure for both
|
|
|
|
// new and legacy pass managers.
|
|
|
|
//
|
2018-09-26 21:01:43 +08:00
|
|
|
// PassTimingInfo Class - This class is used to calculate information about the
|
2018-08-29 05:06:51 +08: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"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2018-09-04 14:12:28 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2018-08-29 05:06:51 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/Mutex.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
bool TimePassesIsEnabled = false;
|
2018-09-26 21:01:43 +08:00
|
|
|
|
2018-08-29 05:06:51 +08: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 21:01:43 +08:00
|
|
|
namespace legacy {
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TimingInfo implementation
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
DenseMap<PassInstanceID, Timer *> TimingData; ///< timers for pass instances
|
|
|
|
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.
|
|
|
|
void print();
|
|
|
|
|
|
|
|
/// 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-29 05:06:51 +08:00
|
|
|
static ManagedStatic<sys::SmartMutex<true>> TimingInfoMutex;
|
|
|
|
|
2018-09-26 21:01:43 +08:00
|
|
|
PassTimingInfo::PassTimingInfo()
|
2018-08-29 05:06:51 +08:00
|
|
|
: TG("pass", "... Pass execution timing report ...") {}
|
|
|
|
|
2018-09-26 21:01:43 +08:00
|
|
|
PassTimingInfo::~PassTimingInfo() {
|
2018-08-29 05:06:51 +08:00
|
|
|
// Deleting the timers accumulates their info into the TG member.
|
|
|
|
// Then TG member is (implicitly) deleted, actually printing the report.
|
|
|
|
for (auto &I : TimingData)
|
2018-09-04 14:12:28 +08:00
|
|
|
delete I.getSecond();
|
2018-08-29 05:06:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 21:01:43 +08:00
|
|
|
void PassTimingInfo::init() {
|
2018-08-29 05:06:51 +08:00
|
|
|
if (!TimePassesIsEnabled || TheTimeInfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Constructed the first time this is called, iff -time-passes is enabled.
|
2018-09-04 14:12:28 +08:00
|
|
|
// This guarantees that the object will be constructed after static globals,
|
2018-08-29 05:06:51 +08:00
|
|
|
// thus it will be destroyed before them.
|
|
|
|
static ManagedStatic<PassTimingInfo> TTI;
|
|
|
|
TheTimeInfo = &*TTI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prints out timing information and then resets the timers.
|
2018-09-26 21:01:43 +08:00
|
|
|
void PassTimingInfo::print() { TG.print(*CreateInfoOutputFile()); }
|
2018-08-29 05:06:51 +08:00
|
|
|
|
2018-09-26 21:01:43 +08:00
|
|
|
Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
|
2018-09-04 14:12:28 +08: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 21:01:43 +08:00
|
|
|
Timer *PassTimingInfo::getPassTimer(Pass *P, PassInstanceID Pass) {
|
2018-08-29 05:06:51 +08:00
|
|
|
if (P->getAsPMDataManager())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
init();
|
|
|
|
sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
|
2018-09-04 14:12:28 +08:00
|
|
|
Timer *&T = TimingData[Pass];
|
2018-08-29 05:06:51 +08:00
|
|
|
|
|
|
|
if (!T) {
|
2018-09-04 14:12:28 +08:00
|
|
|
StringRef PassName = P->getPassName();
|
2018-08-29 05:06:51 +08:00
|
|
|
StringRef PassArgument;
|
|
|
|
if (const PassInfo *PI = Pass::lookupPassInfo(P->getPassID()))
|
|
|
|
PassArgument = PI->getPassArgument();
|
2018-09-04 14:12:28 +08:00
|
|
|
T = newPassTimer(PassArgument.empty() ? PassName : PassArgument, PassName);
|
2018-08-29 05:06:51 +08:00
|
|
|
}
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
2018-09-26 21:01:43 +08:00
|
|
|
PassTimingInfo *PassTimingInfo::TheTimeInfo;
|
|
|
|
} // namespace legacy
|
|
|
|
} // namespace
|
2018-08-29 05:06:51 +08:00
|
|
|
|
|
|
|
Timer *getPassTimer(Pass *P) {
|
2018-09-26 21:01:43 +08:00
|
|
|
legacy::PassTimingInfo::init();
|
|
|
|
if (legacy::PassTimingInfo::TheTimeInfo)
|
|
|
|
return legacy::PassTimingInfo::TheTimeInfo->getPassTimer(P, P);
|
2018-08-29 05:06:51 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If timing is enabled, report the times collected up to now and then reset
|
|
|
|
/// them.
|
|
|
|
void reportAndResetTimings() {
|
2018-09-26 21:01:43 +08:00
|
|
|
if (legacy::PassTimingInfo::TheTimeInfo)
|
|
|
|
legacy::PassTimingInfo::TheTimeInfo->print();
|
2018-08-29 05:06:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|