Add -tv-elapsed-time
MODIFY
tv/tv.cpp
tv/tv.cpp
#include "smt/solver.h"
#include "tools/transform.h"
#include "util/config.h"
+#include "util/stopwatch.h"
#include "util/version.h"
#include "llvm/ADT/Any.h"
#include "llvm/ADT/Triple.h"
@@ -146,6 +147,11 @@ llvm::cl::opt<bool> opt_io_nobuiltin(
"(unused by clang plugin)"),
llvm::cl::cat(TVOptions), llvm::cl::init(false));
+llvm::cl::opt<bool> opt_elapsed_time(
+ "tv-elapsed-time",
+ llvm::cl::desc("Print the elapsed time"),
+ llvm::cl::cat(TVOptions), llvm::cl::init(false));
+
struct FnInfo {
Function fn;
unsigned order;
@@ -159,6 +165,7 @@ optional<smt::smt_initializer> smt_init;
optional<llvm_util::initializer> llvm_util_init;
TransformPrintOpts print_opts;
unordered_map<string, FnInfo> fns;
+unordered_map<string, float> fns_elapsed_time;
set<string> fnsToVerify;
unsigned initialized = 0;
bool showed_stats = false;
@@ -182,6 +189,10 @@ struct TVPass final : public llvm::FunctionPass {
if (!fnsToVerify.empty() && !fnsToVerify.count(F.getName().str()))
return false;
+ ScopedWatch timer([&] (const StopWatch &sw) {
+ fns_elapsed_time[F.getName().str()] += sw.seconds();
+ });
+
llvm::TargetLibraryInfo *TLI = nullptr;
unique_ptr<llvm::TargetLibraryInfo> TLI_holder;
if (is_clangtv) {
@@ -371,6 +382,16 @@ struct TVPass final : public llvm::FunctionPass {
smt_init.reset();
--initialized;
+ if (opt_elapsed_time) {
+ *out << "\n----------------- ELAPSED TIME ------------------\n";
+ float total = 0;
+ for (auto &[name, t]: fns_elapsed_time) {
+ *out << " " << name << ": " << t << " s\n";
+ total += t;
+ }
+ *out << " <TOTAL>: " << total << " s\n";
+ }
+
if (has_failure) {
if (opt_error_fatal)
cerr << "Alive2: Transform doesn't verify; aborting!" << endl;

MODIFY
util/stopwatch.cpp
util/stopwatch.cpp
@@ -37,4 +37,9 @@ ostream& operator<<(ostream &os, const StopWatch &w) {
return os << seconds << " s";
}
+ScopedWatch::~ScopedWatch() {
+ sw.stop();
+ callback(sw);
+}
+
}

MODIFY
util/stopwatch.h
util/stopwatch.h
#include <chrono>
#include <ostream>
+#include <functional>
namespace util {
@@ -22,4 +23,14 @@ public:
friend std::ostream& operator<<(std::ostream &os, const StopWatch &w);
};
+class ScopedWatch {
+ StopWatch sw;
+ std::function<void(const StopWatch &)> callback;
+
+public:
+ ScopedWatch(std::function<void(const StopWatch &)> &&callback)
+ : callback(move(callback)) {}
+ ~ScopedWatch();
+};
+
}

Add -tv-elapsed-time (#533)