diff --git a/src/IR2Vec.cpp b/src/IR2Vec.cpp index bf5c42cd..0e518c56 100644 --- a/src/IR2Vec.cpp +++ b/src/IR2Vec.cpp @@ -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; @@ -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; } diff --git a/src/Symbolic.cpp b/src/Symbolic.cpp index 617bdcff..d864e964 100644 --- a/src/Symbolic.cpp +++ b/src/Symbolic.cpp @@ -40,8 +40,7 @@ void IR2Vec_Symbolic::generateSymbolicEncodings(std::ostream *o) { int noOfFunc = 0; for (auto &f : M) { if (!f.isDeclaration()) { - SmallVector funcStack; - auto tmp = func2Vec(f, funcStack); + auto tmp = func2Vec(f); funcVecMap[&f] = tmp; if (level == 'f') { res += updatedRes(tmp, &f, &M); @@ -84,8 +83,7 @@ void IR2Vec_Symbolic::generateSymbolicEncodingsForFunction(std::ostream *o, auto Result = getActualName(&f); if (!f.isDeclaration() && Result == name) { Vector tmp; - SmallVector funcStack; - tmp = func2Vec(f, funcStack); + tmp = func2Vec(f); funcVecMap[&f] = tmp; if (level == 'f') { res += updatedRes(tmp, &f, &M); @@ -99,20 +97,18 @@ void IR2Vec_Symbolic::generateSymbolicEncodingsForFunction(std::ostream *o, *o << res; } -Vector IR2Vec_Symbolic::func2Vec(Function &F, - SmallVector &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 RPOT(&F); MapVector 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(), @@ -121,12 +117,10 @@ Vector IR2Vec_Symbolic::func2Vec(Function &F, } } - funcStack.pop_back(); return funcVector; } -Vector IR2Vec_Symbolic::bb2Vec(BasicBlock &B, - SmallVector &funcStack) { +Vector IR2Vec_Symbolic::bb2Vec(BasicBlock &B) { Vector bbVector(DIM, 0); #pragma omp parallel for diff --git a/src/include/Symbolic.h b/src/include/Symbolic.h index 0abf19d5..d5965f1b 100644 --- a/src/include/Symbolic.h +++ b/src/include/Symbolic.h @@ -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 &funcStack); - IR2Vec::Vector func2Vec(llvm::Function &F, - llvm::SmallVector &funcStack); + IR2Vec::Vector bb2Vec(llvm::BasicBlock &B); + IR2Vec::Vector func2Vec(llvm::Function &F); std::string res; llvm::SmallMapVector funcVecMap; llvm::SmallMapVector