Skip to content

Commit ac7fce7

Browse files
Code refactoring in IR2Vec.cpp
1 parent 8550d28 commit ac7fce7

3 files changed

Lines changed: 130 additions & 100 deletions

File tree

src/IR2Vec.cpp

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

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

79172
fa = cl_fa;
@@ -90,111 +183,56 @@ int main(int argc, char **argv) {
90183
WT = cl_WT;
91184
debug = cl_debug;
92185
printTime = cl_printTime;
186+
}
93187

188+
void checkFailureConditions() {
94189
bool failed = false;
95-
if (!((sym ^ fa) ^ collectIR)) {
96-
errs() << "Either of sym, fa or collectIR should be specified\n";
190+
191+
if (!(sym || fa || collectIR)) {
192+
errs() << "Either of sym, fa, or collectIR should be specified\n";
97193
failed = true;
98194
}
99195

196+
if (failed)
197+
exit(1);
198+
100199
if (sym || fa) {
101200
if (level != 'p' && level != 'f') {
102201
errs() << "Invalid level specified: Use either p or f\n";
103202
failed = true;
104203
}
105204
} else {
106-
if (!collectIR) {
107-
errs() << "Either of sym, fa or collectIR should be specified\n";
108-
failed = true;
109-
} else if (level)
205+
// assert collectIR is True. Else
206+
assert(collectIR == true);
207+
208+
if (collectIR && level) {
110209
errs() << "[WARNING] level would not be used in collectIR mode\n";
210+
}
111211
}
112212

113213
if (failed)
114214
exit(1);
215+
}
216+
217+
int main(int argc, char **argv) {
218+
cl::SetVersionPrinter(printVersion);
219+
cl::HideUnrelatedOptions(category);
220+
221+
setGlobalVars(argc, argv);
222+
223+
checkFailureConditions();
115224

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

src/Symbolic.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ void IR2Vec_Symbolic::generateSymbolicEncodings(std::ostream *o) {
4040
int noOfFunc = 0;
4141
for (auto &f : M) {
4242
if (!f.isDeclaration()) {
43-
SmallVector<Function *, 15> funcStack;
44-
auto tmp = func2Vec(f, funcStack);
43+
auto tmp = func2Vec(f);
4544
funcVecMap[&f] = tmp;
4645
if (level == 'f') {
4746
res += updatedRes(tmp, &f, &M);
@@ -84,8 +83,7 @@ void IR2Vec_Symbolic::generateSymbolicEncodingsForFunction(std::ostream *o,
8483
auto Result = getActualName(&f);
8584
if (!f.isDeclaration() && Result == name) {
8685
Vector tmp;
87-
SmallVector<Function *, 15> funcStack;
88-
tmp = func2Vec(f, funcStack);
86+
tmp = func2Vec(f);
8987
funcVecMap[&f] = tmp;
9088
if (level == 'f') {
9189
res += updatedRes(tmp, &f, &M);
@@ -99,20 +97,18 @@ void IR2Vec_Symbolic::generateSymbolicEncodingsForFunction(std::ostream *o,
9997
*o << res;
10098
}
10199

102-
Vector IR2Vec_Symbolic::func2Vec(Function &F,
103-
SmallVector<Function *, 15> &funcStack) {
100+
Vector IR2Vec_Symbolic::func2Vec(Function &F) {
104101
auto It = funcVecMap.find(&F);
105102
if (It != funcVecMap.end()) {
106103
return It->second;
107104
}
108-
funcStack.push_back(&F);
109105
Vector funcVector(DIM, 0);
110106
ReversePostOrderTraversal<Function *> RPOT(&F);
111107
MapVector<const BasicBlock *, double> cumulativeScore;
112108

113109
#pragma omp parallel for
114110
for (auto *b : RPOT) {
115-
Vector weightedBBVector = bb2Vec(*b, funcStack);
111+
Vector weightedBBVector = bb2Vec(*b);
116112
#pragma omp critical
117113
{
118114
std::transform(funcVector.begin(), funcVector.end(),
@@ -121,12 +117,10 @@ Vector IR2Vec_Symbolic::func2Vec(Function &F,
121117
}
122118
}
123119

124-
funcStack.pop_back();
125120
return funcVector;
126121
}
127122

128-
Vector IR2Vec_Symbolic::bb2Vec(BasicBlock &B,
129-
SmallVector<Function *, 15> &funcStack) {
123+
Vector IR2Vec_Symbolic::bb2Vec(BasicBlock &B) {
130124
Vector bbVector(DIM, 0);
131125

132126
#pragma omp parallel for

src/include/Symbolic.h

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

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

0 commit comments

Comments
 (0)