Skip to content

Commit 38b7324

Browse files
Code refactoring in IR2Vec.cpp
1 parent fa86e87 commit 38b7324

File tree

3 files changed

+130
-100
lines changed

3 files changed

+130
-100
lines changed

src/IR2Vec.cpp

Lines changed: 123 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,102 @@ void printVersion(raw_ostream &ostream) {
6969
cl::PrintVersionMessage();
7070
}
7171

72-
int main(int argc, char **argv) {
73-
cl::SetVersionPrinter(printVersion);
74-
cl::HideUnrelatedOptions(category);
72+
void generateSymEncodingsFunction(std::string funcName) {
73+
auto M = getLLVMIR();
74+
IR2Vec_Symbolic SYM(*M);
75+
std::ofstream o;
76+
o.open(oname, std::ios_base::app);
77+
if (printTime) {
78+
clock_t start = clock();
79+
SYM.generateSymbolicEncodingsForFunction(&o, funcName);
80+
clock_t end = clock();
81+
double elapsed = double(end - start) / CLOCKS_PER_SEC;
82+
printf("Time taken by on-demand generation of symbolic encodings "
83+
"is: %.6f "
84+
"seconds.\n",
85+
elapsed);
86+
} else {
87+
SYM.generateSymbolicEncodingsForFunction(&o, funcName);
88+
}
89+
o.close();
90+
}
91+
92+
void generateFAEncodingsFunction(std::string funcName) {
93+
auto M = getLLVMIR();
94+
IR2Vec_FA FA(*M);
95+
std::ofstream o, missCount, cyclicCount;
96+
o.open(oname, std::ios_base::app);
97+
missCount.open("missCount_" + oname, std::ios_base::app);
98+
cyclicCount.open("cyclicCount_" + oname, std::ios_base::app);
99+
if (printTime) {
100+
clock_t start = clock();
101+
FA.generateFlowAwareEncodingsForFunction(&o, funcName, &missCount,
102+
&cyclicCount);
103+
clock_t end = clock();
104+
double elapsed = double(end - start) / CLOCKS_PER_SEC;
105+
printf("Time taken by on-demand generation of flow-aware encodings "
106+
"is: %.6f "
107+
"seconds.\n",
108+
elapsed);
109+
} else {
110+
FA.generateFlowAwareEncodingsForFunction(&o, funcName, &missCount,
111+
&cyclicCount);
112+
}
113+
o.close();
114+
}
115+
116+
void generateFAEncodings() {
117+
auto M = getLLVMIR();
118+
IR2Vec_FA FA(*M);
119+
std::ofstream o, missCount, cyclicCount;
120+
o.open(oname, std::ios_base::app);
121+
missCount.open("missCount_" + oname, std::ios_base::app);
122+
cyclicCount.open("cyclicCount_" + oname, std::ios_base::app);
123+
if (printTime) {
124+
clock_t start = clock();
125+
FA.generateFlowAwareEncodings(&o, &missCount, &cyclicCount);
126+
clock_t end = clock();
127+
double elapsed = double(end - start) / CLOCKS_PER_SEC;
128+
printf("Time taken by normal generation of flow-aware encodings "
129+
"is: %.6f "
130+
"seconds.\n",
131+
elapsed);
132+
} else {
133+
FA.generateFlowAwareEncodings(&o, &missCount, &cyclicCount);
134+
}
135+
o.close();
136+
}
137+
138+
void generateSYMEncodings() {
139+
auto M = getLLVMIR();
140+
IR2Vec_Symbolic SYM(*M);
141+
std::ofstream o;
142+
o.open(oname, std::ios_base::app);
143+
if (printTime) {
144+
clock_t start = clock();
145+
SYM.generateSymbolicEncodings(&o);
146+
clock_t end = clock();
147+
double elapsed = double(end - start) / CLOCKS_PER_SEC;
148+
printf("Time taken by normal generation of symbolic encodings is: "
149+
"%.6f "
150+
"seconds.\n",
151+
elapsed);
152+
} else {
153+
SYM.generateSymbolicEncodings(&o);
154+
}
155+
o.close();
156+
}
157+
158+
void collectIR() {
159+
auto M = getLLVMIR();
160+
CollectIR cir(M);
161+
std::ofstream o;
162+
o.open(oname, std::ios_base::app);
163+
cir.generateTriplets(o);
164+
o.close();
165+
}
166+
167+
void setGlobalVars(int argc, char **argv) {
75168
cl::ParseCommandLineOptions(argc, argv);
76169

77170
fa = cl_fa;
@@ -88,111 +181,56 @@ int main(int argc, char **argv) {
88181
WT = cl_WT;
89182
debug = cl_debug;
90183
printTime = cl_printTime;
184+
}
91185

186+
void checkFailureConditions() {
92187
bool failed = false;
93-
if (!((sym ^ fa) ^ collectIR)) {
94-
errs() << "Either of sym, fa or collectIR should be specified\n";
188+
189+
if (!(sym || fa || collectIR)) {
190+
errs() << "Either of sym, fa, or collectIR should be specified\n";
95191
failed = true;
96192
}
97193

194+
if (failed)
195+
exit(1);
196+
98197
if (sym || fa) {
99198
if (level != 'p' && level != 'f') {
100199
errs() << "Invalid level specified: Use either p or f\n";
101200
failed = true;
102201
}
103202
} else {
104-
if (!collectIR) {
105-
errs() << "Either of sym, fa or collectIR should be specified\n";
106-
failed = true;
107-
} else if (level)
203+
// assert collectIR is True. Else
204+
assert(collectIR == true);
205+
206+
if (collectIR && level) {
108207
errs() << "[WARNING] level would not be used in collectIR mode\n";
208+
}
109209
}
110210

111211
if (failed)
112212
exit(1);
213+
}
214+
215+
int main(int argc, char **argv) {
216+
cl::SetVersionPrinter(printVersion);
217+
cl::HideUnrelatedOptions(category);
218+
219+
setGlobalVars(argc, argv);
220+
221+
checkFailureConditions();
113222

114-
auto M = getLLVMIR();
115223
// newly added
116224
if (sym && !(funcName.empty())) {
117-
IR2Vec_Symbolic SYM(*M);
118-
std::ofstream o;
119-
o.open(oname, std::ios_base::app);
120-
if (printTime) {
121-
clock_t start = clock();
122-
SYM.generateSymbolicEncodingsForFunction(&o, funcName);
123-
clock_t end = clock();
124-
double elapsed = double(end - start) / CLOCKS_PER_SEC;
125-
printf("Time taken by on-demand generation of symbolic encodings "
126-
"is: %.6f "
127-
"seconds.\n",
128-
elapsed);
129-
} else {
130-
SYM.generateSymbolicEncodingsForFunction(&o, funcName);
131-
}
132-
o.close();
225+
generateSymEncodingsFunction(funcName);
133226
} else if (fa && !(funcName.empty())) {
134-
IR2Vec_FA FA(*M);
135-
std::ofstream o, missCount, cyclicCount;
136-
o.open(oname, std::ios_base::app);
137-
missCount.open("missCount_" + oname, std::ios_base::app);
138-
cyclicCount.open("cyclicCount_" + oname, std::ios_base::app);
139-
if (printTime) {
140-
clock_t start = clock();
141-
FA.generateFlowAwareEncodingsForFunction(&o, funcName, &missCount,
142-
&cyclicCount);
143-
clock_t end = clock();
144-
double elapsed = double(end - start) / CLOCKS_PER_SEC;
145-
printf("Time taken by on-demand generation of flow-aware encodings "
146-
"is: %.6f "
147-
"seconds.\n",
148-
elapsed);
149-
} else {
150-
FA.generateFlowAwareEncodingsForFunction(&o, funcName, &missCount,
151-
&cyclicCount);
152-
}
153-
o.close();
227+
generateFAEncodingsFunction(funcName);
154228
} else if (fa) {
155-
IR2Vec_FA FA(*M);
156-
std::ofstream o, missCount, cyclicCount;
157-
o.open(oname, std::ios_base::app);
158-
missCount.open("missCount_" + oname, std::ios_base::app);
159-
cyclicCount.open("cyclicCount_" + oname, std::ios_base::app);
160-
if (printTime) {
161-
clock_t start = clock();
162-
FA.generateFlowAwareEncodings(&o, &missCount, &cyclicCount);
163-
clock_t end = clock();
164-
double elapsed = double(end - start) / CLOCKS_PER_SEC;
165-
printf("Time taken by normal generation of flow-aware encodings "
166-
"is: %.6f "
167-
"seconds.\n",
168-
elapsed);
169-
} else {
170-
FA.generateFlowAwareEncodings(&o, &missCount, &cyclicCount);
171-
}
172-
o.close();
229+
generateFAEncodings();
173230
} else if (sym) {
174-
IR2Vec_Symbolic SYM(*M);
175-
std::ofstream o;
176-
o.open(oname, std::ios_base::app);
177-
if (printTime) {
178-
clock_t start = clock();
179-
SYM.generateSymbolicEncodings(&o);
180-
clock_t end = clock();
181-
double elapsed = double(end - start) / CLOCKS_PER_SEC;
182-
printf("Time taken by normal generation of symbolic encodings is: "
183-
"%.6f "
184-
"seconds.\n",
185-
elapsed);
186-
} else {
187-
SYM.generateSymbolicEncodings(&o);
188-
}
189-
o.close();
231+
generateSYMEncodings();
190232
} else if (collectIR) {
191-
CollectIR cir(M);
192-
std::ofstream o;
193-
o.open(oname, std::ios_base::app);
194-
cir.generateTriplets(o);
195-
o.close();
233+
collectIR();
196234
}
197235
return 0;
198236
}

src/Symbolic.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ void IR2Vec_Symbolic::generateSymbolicEncodings(std::ostream *o) {
3838
int noOfFunc = 0;
3939
for (auto &f : M) {
4040
if (!f.isDeclaration()) {
41-
SmallVector<Function *, 15> funcStack;
42-
auto tmp = func2Vec(f, funcStack);
41+
auto tmp = func2Vec(f);
4342
funcVecMap[&f] = tmp;
4443
if (level == 'f') {
4544
res += updatedRes(tmp, &f, &M);
@@ -82,8 +81,7 @@ void IR2Vec_Symbolic::generateSymbolicEncodingsForFunction(std::ostream *o,
8281
auto Result = getActualName(&f);
8382
if (!f.isDeclaration() && Result == name) {
8483
Vector tmp;
85-
SmallVector<Function *, 15> funcStack;
86-
tmp = func2Vec(f, funcStack);
84+
tmp = func2Vec(f);
8785
funcVecMap[&f] = tmp;
8886
if (level == 'f') {
8987
res += updatedRes(tmp, &f, &M);
@@ -97,20 +95,18 @@ void IR2Vec_Symbolic::generateSymbolicEncodingsForFunction(std::ostream *o,
9795
*o << res;
9896
}
9997

100-
Vector IR2Vec_Symbolic::func2Vec(Function &F,
101-
SmallVector<Function *, 15> &funcStack) {
98+
Vector IR2Vec_Symbolic::func2Vec(Function &F) {
10299
auto It = funcVecMap.find(&F);
103100
if (It != funcVecMap.end()) {
104101
return It->second;
105102
}
106-
funcStack.push_back(&F);
107103
Vector funcVector(DIM, 0);
108104
ReversePostOrderTraversal<Function *> RPOT(&F);
109105
MapVector<const BasicBlock *, double> cumulativeScore;
110106

111107
#pragma omp parallel for
112108
for (auto *b : RPOT) {
113-
Vector weightedBBVector = bb2Vec(*b, funcStack);
109+
Vector weightedBBVector = bb2Vec(*b);
114110
#pragma omp critical
115111
{
116112
std::transform(funcVector.begin(), funcVector.end(),
@@ -119,12 +115,10 @@ Vector IR2Vec_Symbolic::func2Vec(Function &F,
119115
}
120116
}
121117

122-
funcStack.pop_back();
123118
return funcVector;
124119
}
125120

126-
Vector IR2Vec_Symbolic::bb2Vec(BasicBlock &B,
127-
SmallVector<Function *, 15> &funcStack) {
121+
Vector IR2Vec_Symbolic::bb2Vec(BasicBlock &B) {
128122
Vector bbVector(DIM, 0);
129123

130124
#pragma omp parallel for

src/include/Symbolic.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ class IR2Vec_Symbolic {
2525
IR2Vec::Vector pgmVector;
2626

2727
inline void getValue(IR2Vec::Vector &vec, std::string key);
28-
IR2Vec::Vector bb2Vec(llvm::BasicBlock &B,
29-
llvm::SmallVector<llvm::Function *, 15> &funcStack);
30-
IR2Vec::Vector func2Vec(llvm::Function &F,
31-
llvm::SmallVector<llvm::Function *, 15> &funcStack);
28+
IR2Vec::Vector bb2Vec(llvm::BasicBlock &B);
29+
IR2Vec::Vector func2Vec(llvm::Function &F);
3230
std::string res;
3331
llvm::SmallMapVector<const llvm::Function *, IR2Vec::Vector, 16> funcVecMap;
3432
llvm::SmallMapVector<const llvm::Instruction *, IR2Vec::Vector, 128>

0 commit comments

Comments
 (0)