Skip to content

Commit 7009d20

Browse files
STA commit - further refactors
1 parent 27d0c01 commit 7009d20

File tree

3 files changed

+57
-49
lines changed

3 files changed

+57
-49
lines changed

src/IR2Vec.cpp

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "CollectIR.h"
1010
#include "FlowAware.h"
1111
#include "Symbolic.h"
12-
#include "Vocabulary.h"
1312
#include "version.h"
1413

1514
#include "llvm/Support/CommandLine.h"
@@ -73,54 +72,6 @@ void printVersion(raw_ostream &ostream) {
7372
cl::PrintVersionMessage();
7473
}
7574

76-
struct SymOutputs {
77-
std::ofstream out;
78-
};
79-
80-
struct FAOutputs : SymOutputs {
81-
std::ofstream miss;
82-
std::ofstream cyclic;
83-
};
84-
85-
inline SymOutputs openSymOutputs(const std::string &baseName) {
86-
SymOutputs f;
87-
f.out.open(baseName, std::ios_base::app);
88-
return f;
89-
}
90-
91-
inline FAOutputs openFAOutputs(const std::string &baseName) {
92-
FAOutputs f;
93-
f.out.open(baseName, std::ios_base::app);
94-
f.miss.open("missCount_" + baseName, std::ios_base::app);
95-
f.cyclic.open("cyclicCount_" + baseName, std::ios_base::app);
96-
return f;
97-
}
98-
99-
template <class F>
100-
inline void runMaybeTimed(bool shouldTime, const char *timingMsgFmt, F &&job) {
101-
if (shouldTime) {
102-
const clock_t start = clock();
103-
std::forward<F>(job)();
104-
const clock_t end = clock();
105-
const double elapsed = static_cast<double>(end - start) / CLOCKS_PER_SEC;
106-
std::printf(timingMsgFmt, elapsed);
107-
} else {
108-
std::forward<F>(job)();
109-
}
110-
}
111-
112-
template <class Encoder, class Outputs, class OutputsFactory, class Body>
113-
inline void executeEncoder(const char *timingMsgFmt, bool shouldTime,
114-
OutputsFactory &&makeOutputs, Body &&body) {
115-
auto M = getLLVMIR();
116-
auto vocabulary = VocabularyFactory::createVocabulary(DIM)->getVocabulary();
117-
Encoder encoder(*M, vocabulary);
118-
auto files = std::forward<OutputsFactory>(makeOutputs)(oname);
119-
120-
auto job = [&] { std::forward<Body>(body)(encoder, files); };
121-
runMaybeTimed(shouldTime, timingMsgFmt, job);
122-
}
123-
12475
void generateFAEncodingsFunction(std::string funcName) {
12576
executeEncoder<IR2Vec_FA, FAOutputs>(
12677
"Time taken by on-demand generation of flow-aware encodings is: %.6f "

src/include/utils.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef __IR2Vec_Utils__
1010
#define __IR2Vec_Utils__
1111

12+
#include "Vocabulary.h"
13+
1214
#include "llvm/ADT/SmallVector.h"
1315
#include "llvm/Demangle/Demangle.h" //for getting function base name
1416
#include "llvm/IR/Module.h"
@@ -17,6 +19,7 @@
1719
#include "llvm/Support/raw_ostream.h"
1820

1921
#include <cxxabi.h>
22+
#include <fstream>
2023

2124
#include <map>
2225

@@ -53,6 +56,46 @@ void scaleVector(Vector &vec, float factor);
5356
std::string getDemagledName(const llvm::Function *function);
5457
char *getActualName(llvm::Function *function);
5558
std::string updatedRes(IR2Vec::Vector tmp, llvm::Function *f, llvm::Module *M);
59+
60+
struct SymOutputs {
61+
std::ofstream out;
62+
};
63+
64+
SymOutputs openSymOutputs(const std::string &baseName);
65+
66+
struct FAOutputs {
67+
std::ofstream out;
68+
std::ofstream miss;
69+
std::ofstream cyclic;
70+
};
71+
72+
FAOutputs openFAOutputs(const std::string &baseName);
73+
74+
template <class F>
75+
inline void runMaybeTimed(bool shouldTime, const char *timingMsgFmt, F &&job) {
76+
if (shouldTime) {
77+
const clock_t start = clock();
78+
std::forward<F>(job)();
79+
const clock_t end = clock();
80+
const double elapsed = static_cast<double>(end - start) / CLOCKS_PER_SEC;
81+
std::printf(timingMsgFmt, elapsed);
82+
} else {
83+
std::forward<F>(job)();
84+
}
85+
}
86+
87+
template <class Encoder, class Outputs, class OutputsFactory, class Body>
88+
inline void executeEncoder(const char *timingMsgFmt, bool shouldTime,
89+
OutputsFactory &&makeOutputs, Body &&body) {
90+
auto M = getLLVMIR();
91+
auto vocabulary = VocabularyFactory::createVocabulary(DIM)->getVocabulary();
92+
Encoder encoder(*M, vocabulary);
93+
auto files = std::forward<OutputsFactory>(makeOutputs)(oname);
94+
95+
auto job = [&] { std::forward<Body>(body)(encoder, files); };
96+
runMaybeTimed(shouldTime, timingMsgFmt, job);
97+
}
98+
5699
} // namespace IR2Vec
57100

58101
#endif

src/utils.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,17 @@ std::string IR2Vec::updatedRes(IR2Vec::Vector tmp, llvm::Function *f,
9595

9696
return res;
9797
}
98+
99+
IR2Vec::SymOutputs IR2Vec::openSymOutputs(const std::string &baseName) {
100+
SymOutputs f;
101+
f.out.open(baseName, std::ios_base::app);
102+
return f;
103+
}
104+
105+
IR2Vec::FAOutputs IR2Vec::openFAOutputs(const std::string &baseName) {
106+
FAOutputs f;
107+
f.out.open(baseName, std::ios_base::app);
108+
f.miss.open("missCount_" + baseName, std::ios_base::app);
109+
f.cyclic.open("cyclicCount_" + baseName, std::ios_base::app);
110+
return f;
111+
}

0 commit comments

Comments
 (0)