Skip to content

Commit

Permalink
Code refactoring in IR2Vec.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant-sachdeva committed Jul 31, 2024
1 parent 8550d28 commit ac7fce7
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 100 deletions.
208 changes: 123 additions & 85 deletions src/IR2Vec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,102 @@ void printVersion(raw_ostream &ostream) {
cl::PrintVersionMessage();
}

int main(int argc, char **argv) {
cl::SetVersionPrinter(printVersion);
cl::HideUnrelatedOptions(category);
void generateSymEncodingsFunction(std::string funcName) {
auto M = getLLVMIR();
IR2Vec_Symbolic SYM(*M);
std::ofstream o;
o.open(oname, std::ios_base::app);
if (printTime) {
clock_t start = clock();
SYM.generateSymbolicEncodingsForFunction(&o, funcName);
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC;
printf("Time taken by on-demand generation of symbolic encodings "
"is: %.6f "
"seconds.\n",
elapsed);
} else {
SYM.generateSymbolicEncodingsForFunction(&o, funcName);
}
o.close();
}

void generateFAEncodingsFunction(std::string funcName) {
auto M = getLLVMIR();
IR2Vec_FA FA(*M);
std::ofstream o, missCount, cyclicCount;
o.open(oname, std::ios_base::app);
missCount.open("missCount_" + oname, std::ios_base::app);
cyclicCount.open("cyclicCount_" + oname, std::ios_base::app);
if (printTime) {
clock_t start = clock();
FA.generateFlowAwareEncodingsForFunction(&o, funcName, &missCount,
&cyclicCount);
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC;
printf("Time taken by on-demand generation of flow-aware encodings "
"is: %.6f "
"seconds.\n",
elapsed);
} else {
FA.generateFlowAwareEncodingsForFunction(&o, funcName, &missCount,
&cyclicCount);
}
o.close();
}

void generateFAEncodings() {
auto M = getLLVMIR();
IR2Vec_FA FA(*M);
std::ofstream o, missCount, cyclicCount;
o.open(oname, std::ios_base::app);
missCount.open("missCount_" + oname, std::ios_base::app);
cyclicCount.open("cyclicCount_" + oname, std::ios_base::app);
if (printTime) {
clock_t start = clock();
FA.generateFlowAwareEncodings(&o, &missCount, &cyclicCount);
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC;
printf("Time taken by normal generation of flow-aware encodings "
"is: %.6f "
"seconds.\n",
elapsed);
} else {
FA.generateFlowAwareEncodings(&o, &missCount, &cyclicCount);
}
o.close();
}

void generateSYMEncodings() {
auto M = getLLVMIR();
IR2Vec_Symbolic SYM(*M);
std::ofstream o;
o.open(oname, std::ios_base::app);
if (printTime) {
clock_t start = clock();
SYM.generateSymbolicEncodings(&o);
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC;
printf("Time taken by normal generation of symbolic encodings is: "
"%.6f "
"seconds.\n",
elapsed);
} else {
SYM.generateSymbolicEncodings(&o);
}
o.close();
}

void collectIR() {
auto M = getLLVMIR();
CollectIR cir(M);
std::ofstream o;
o.open(oname, std::ios_base::app);
cir.generateTriplets(o);
o.close();
}

void setGlobalVars(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv);

fa = cl_fa;
Expand All @@ -90,111 +183,56 @@ int main(int argc, char **argv) {
WT = cl_WT;
debug = cl_debug;
printTime = cl_printTime;
}

void checkFailureConditions() {
bool failed = false;
if (!((sym ^ fa) ^ collectIR)) {
errs() << "Either of sym, fa or collectIR should be specified\n";

if (!(sym || fa || collectIR)) {
errs() << "Either of sym, fa, or collectIR should be specified\n";
failed = true;
}

if (failed)
exit(1);

if (sym || fa) {
if (level != 'p' && level != 'f') {
errs() << "Invalid level specified: Use either p or f\n";
failed = true;
}
} else {
if (!collectIR) {
errs() << "Either of sym, fa or collectIR should be specified\n";
failed = true;
} else if (level)
// assert collectIR is True. Else
assert(collectIR == true);

if (collectIR && level) {
errs() << "[WARNING] level would not be used in collectIR mode\n";
}
}

if (failed)
exit(1);
}

int main(int argc, char **argv) {
cl::SetVersionPrinter(printVersion);
cl::HideUnrelatedOptions(category);

setGlobalVars(argc, argv);

checkFailureConditions();

auto M = getLLVMIR();
// newly added
if (sym && !(funcName.empty())) {
IR2Vec_Symbolic SYM(*M);
std::ofstream o;
o.open(oname, std::ios_base::app);
if (printTime) {
clock_t start = clock();
SYM.generateSymbolicEncodingsForFunction(&o, funcName);
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC;
printf("Time taken by on-demand generation of symbolic encodings "
"is: %.6f "
"seconds.\n",
elapsed);
} else {
SYM.generateSymbolicEncodingsForFunction(&o, funcName);
}
o.close();
generateSymEncodingsFunction(funcName);
} else if (fa && !(funcName.empty())) {
IR2Vec_FA FA(*M);
std::ofstream o, missCount, cyclicCount;
o.open(oname, std::ios_base::app);
missCount.open("missCount_" + oname, std::ios_base::app);
cyclicCount.open("cyclicCount_" + oname, std::ios_base::app);
if (printTime) {
clock_t start = clock();
FA.generateFlowAwareEncodingsForFunction(&o, funcName, &missCount,
&cyclicCount);
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC;
printf("Time taken by on-demand generation of flow-aware encodings "
"is: %.6f "
"seconds.\n",
elapsed);
} else {
FA.generateFlowAwareEncodingsForFunction(&o, funcName, &missCount,
&cyclicCount);
}
o.close();
generateFAEncodingsFunction(funcName);
} else if (fa) {
IR2Vec_FA FA(*M);
std::ofstream o, missCount, cyclicCount;
o.open(oname, std::ios_base::app);
missCount.open("missCount_" + oname, std::ios_base::app);
cyclicCount.open("cyclicCount_" + oname, std::ios_base::app);
if (printTime) {
clock_t start = clock();
FA.generateFlowAwareEncodings(&o, &missCount, &cyclicCount);
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC;
printf("Time taken by normal generation of flow-aware encodings "
"is: %.6f "
"seconds.\n",
elapsed);
} else {
FA.generateFlowAwareEncodings(&o, &missCount, &cyclicCount);
}
o.close();
generateFAEncodings();
} else if (sym) {
IR2Vec_Symbolic SYM(*M);
std::ofstream o;
o.open(oname, std::ios_base::app);
if (printTime) {
clock_t start = clock();
SYM.generateSymbolicEncodings(&o);
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC;
printf("Time taken by normal generation of symbolic encodings is: "
"%.6f "
"seconds.\n",
elapsed);
} else {
SYM.generateSymbolicEncodings(&o);
}
o.close();
generateSYMEncodings();
} else if (collectIR) {
CollectIR cir(M);
std::ofstream o;
o.open(oname, std::ios_base::app);
cir.generateTriplets(o);
o.close();
collectIR();
}
return 0;
}
16 changes: 5 additions & 11 deletions src/Symbolic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ void IR2Vec_Symbolic::generateSymbolicEncodings(std::ostream *o) {
int noOfFunc = 0;
for (auto &f : M) {
if (!f.isDeclaration()) {
SmallVector<Function *, 15> funcStack;
auto tmp = func2Vec(f, funcStack);
auto tmp = func2Vec(f);
funcVecMap[&f] = tmp;
if (level == 'f') {
res += updatedRes(tmp, &f, &M);
Expand Down Expand Up @@ -84,8 +83,7 @@ void IR2Vec_Symbolic::generateSymbolicEncodingsForFunction(std::ostream *o,
auto Result = getActualName(&f);
if (!f.isDeclaration() && Result == name) {
Vector tmp;
SmallVector<Function *, 15> funcStack;
tmp = func2Vec(f, funcStack);
tmp = func2Vec(f);
funcVecMap[&f] = tmp;
if (level == 'f') {
res += updatedRes(tmp, &f, &M);
Expand All @@ -99,20 +97,18 @@ void IR2Vec_Symbolic::generateSymbolicEncodingsForFunction(std::ostream *o,
*o << res;
}

Vector IR2Vec_Symbolic::func2Vec(Function &F,
SmallVector<Function *, 15> &funcStack) {
Vector IR2Vec_Symbolic::func2Vec(Function &F) {
auto It = funcVecMap.find(&F);
if (It != funcVecMap.end()) {
return It->second;
}
funcStack.push_back(&F);
Vector funcVector(DIM, 0);
ReversePostOrderTraversal<Function *> RPOT(&F);
MapVector<const BasicBlock *, double> cumulativeScore;

#pragma omp parallel for
for (auto *b : RPOT) {
Vector weightedBBVector = bb2Vec(*b, funcStack);
Vector weightedBBVector = bb2Vec(*b);
#pragma omp critical
{
std::transform(funcVector.begin(), funcVector.end(),
Expand All @@ -121,12 +117,10 @@ Vector IR2Vec_Symbolic::func2Vec(Function &F,
}
}

funcStack.pop_back();
return funcVector;
}

Vector IR2Vec_Symbolic::bb2Vec(BasicBlock &B,
SmallVector<Function *, 15> &funcStack) {
Vector IR2Vec_Symbolic::bb2Vec(BasicBlock &B) {
Vector bbVector(DIM, 0);

#pragma omp parallel for
Expand Down
6 changes: 2 additions & 4 deletions src/include/Symbolic.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ class IR2Vec_Symbolic {
IR2Vec::Vector pgmVector;

inline void getValue(IR2Vec::Vector &vec, std::string key);
IR2Vec::Vector bb2Vec(llvm::BasicBlock &B,
llvm::SmallVector<llvm::Function *, 15> &funcStack);
IR2Vec::Vector func2Vec(llvm::Function &F,
llvm::SmallVector<llvm::Function *, 15> &funcStack);
IR2Vec::Vector bb2Vec(llvm::BasicBlock &B);
IR2Vec::Vector func2Vec(llvm::Function &F);
std::string res;
llvm::SmallMapVector<const llvm::Function *, IR2Vec::Vector, 16> funcVecMap;
llvm::SmallMapVector<const llvm::Instruction *, IR2Vec::Vector, 128>
Expand Down

0 comments on commit ac7fce7

Please sign in to comment.