Skip to content

Commit 3362cc7

Browse files
opt checkpoint : changed UpdateFuncVecMap from recursive to iterative
1 parent 513be7c commit 3362cc7

2 files changed

Lines changed: 50 additions & 54 deletions

File tree

src/FlowAware.cpp

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ void IR2Vec_FA::generateFlowAwareEncodings(std::ostream *o,
134134
#pragma omp parallel for
135135
for (auto &f : M) {
136136
if (!f.isDeclaration()) {
137-
SmallVector<Function *, 15> funcStack;
138-
auto tmp = func2Vec(f, funcStack);
137+
auto tmp = func2Vec(f);
139138

140139
#pragma omp critical
141140
{ funcVecMap[&f] = tmp; }
@@ -190,24 +189,50 @@ void IR2Vec_FA::generateFlowAwareEncodings(std::ostream *o,
190189

191190
// This function will update funcVecMap by doing DFS starting from parent
192191
// function
192+
// void IR2Vec_FA::updateFuncVecMap(
193+
// llvm::Function *function,
194+
// llvm::SmallSet<const llvm::Function *, 16> &visitedFunctions) {
195+
// visitedFunctions.insert(function);
196+
// auto tmpParent = func2Vec(*function);
197+
// // funcVecMap is updated with vectors returned by func2Vec
198+
// funcVecMap[function] = tmpParent;
199+
// auto calledFunctions = funcCallMap[function];
200+
// for (auto &calledFunction : calledFunctions) {
201+
// if (calledFunction && !calledFunction->isDeclaration() &&
202+
// visitedFunctions.count(calledFunction) == 0) {
203+
// // doing casting since calledFunctions is of type of const
204+
// // llvm::Function* and we need llvm::Function* as argument
205+
// auto *callee = const_cast<Function *>(calledFunction);
206+
// // This function is called recursively to update funcVecMap
207+
// updateFuncVecMap(callee, visitedFunctions);
208+
// }
209+
// }
210+
// }
211+
193212
void IR2Vec_FA::updateFuncVecMap(
194213
llvm::Function *function,
195214
llvm::SmallSet<const llvm::Function *, 16> &visitedFunctions) {
196-
visitedFunctions.insert(function);
197-
SmallVector<Function *, 15> funcStack;
198-
funcStack.clear();
199-
auto tmpParent = func2Vec(*function, funcStack);
200-
// funcVecMap is updated with vectors returned by func2Vec
201-
funcVecMap[function] = tmpParent;
202-
auto calledFunctions = funcCallMap[function];
203-
for (auto &calledFunction : calledFunctions) {
204-
if (calledFunction && !calledFunction->isDeclaration() &&
205-
visitedFunctions.count(calledFunction) == 0) {
206-
// doing casting since calledFunctions is of type of const
207-
// llvm::Function* and we need llvm::Function* as argument
208-
auto *callee = const_cast<Function *>(calledFunction);
209-
// This function is called recursively to update funcVecMap
210-
updateFuncVecMap(callee, visitedFunctions);
215+
216+
// Stack to simulate the recursive calls
217+
std::stack<llvm::Function *> stack;
218+
stack.push(function);
219+
220+
while (!stack.empty()) {
221+
auto *currentFunction = stack.top();
222+
stack.pop();
223+
224+
visitedFunctions.insert(currentFunction);
225+
auto tmpParent = func2Vec(*currentFunction);
226+
funcVecMap[currentFunction] = tmpParent;
227+
228+
auto calledFunctions = funcCallMap[currentFunction];
229+
for (auto &calledFunction : calledFunctions) {
230+
if (calledFunction && !calledFunction->isDeclaration() &&
231+
visitedFunctions.count(calledFunction) == 0) {
232+
// Cast and push the callee to the stack
233+
auto *callee = const_cast<llvm::Function *>(calledFunction);
234+
stack.push(callee);
235+
}
211236
}
212237
}
213238
}
@@ -336,15 +361,12 @@ void IR2Vec_FA::createKilllist(SmallVector<Instruction *, 16> &KillList,
336361
return;
337362
}
338363

339-
Vector IR2Vec_FA::func2Vec(Function &F,
340-
SmallVector<Function *, 15> &funcStack) {
364+
Vector IR2Vec_FA::func2Vec(Function &F) {
341365
auto It = funcVecMap.find(&F);
342366
if (It != funcVecMap.end()) {
343367
return It->second;
344368
}
345369

346-
funcStack.push_back(&F);
347-
348370
instReachingDefsMap.clear();
349371
allSCCs.clear();
350372
reverseReachingDefsMap.clear();
@@ -520,7 +542,7 @@ Vector IR2Vec_FA::func2Vec(Function &F,
520542
}
521543

522544
for (auto *b : RPOT) {
523-
bb2Vec(*b, funcStack);
545+
bb2Vec(*b);
524546
Vector bbVector(DIM, 0);
525547
IR2VEC_DEBUG(outs() << "-------------------------------------------\n");
526548
for (auto &I : *b) {
@@ -545,7 +567,6 @@ Vector IR2Vec_FA::func2Vec(Function &F,
545567
funcVector.begin(), std::plus<double>());
546568
}
547569

548-
funcStack.pop_back();
549570
funcVecMap[&F] = funcVector;
550571
return funcVector;
551572
}
@@ -1220,9 +1241,7 @@ void IR2Vec_FA::solveSingleComponent(
12201241
----------------------------------------------------------------------------------
12211242
*/
12221243

1223-
void IR2Vec_FA::inst2Vec(
1224-
const Instruction &I, SmallVector<Function *, 15> &funcStack,
1225-
SmallMapVector<const Instruction *, Vector, 16> &partialInstValMap) {
1244+
void IR2Vec_FA::inst2Vec(const Instruction &I) {
12261245

12271246
if (instVecMap.find(&I) != instVecMap.end()) {
12281247
IR2VEC_DEBUG(outs() << "Returning from inst2Vec() I found in Map\n");
@@ -1236,14 +1255,6 @@ void IR2Vec_FA::inst2Vec(
12361255
IR2VEC_DEBUG(I.print(outs()); outs() << "\n");
12371256
std::transform(instVector.begin(), instVector.end(), vec.begin(),
12381257
instVector.begin(), std::plus<double>());
1239-
partialInstValMap[&I] = instVector;
1240-
1241-
IR2VEC_DEBUG(outs() << "contents of partialInstValMap:\n";
1242-
for (auto i
1243-
: partialInstValMap) {
1244-
i.first->print(outs());
1245-
outs() << "\n";
1246-
});
12471258

12481259
auto type = I.getType();
12491260

@@ -1277,7 +1288,6 @@ void IR2Vec_FA::inst2Vec(
12771288
scaleVector(vec, WT);
12781289
std::transform(instVector.begin(), instVector.end(), vec.begin(),
12791290
instVector.begin(), std::plus<double>());
1280-
partialInstValMap[&I] = instVector;
12811291

12821292
unsigned operandNum;
12831293
bool isMemWrite = isMemOp(opcodeName, operandNum, memWriteOp);
@@ -1443,17 +1453,8 @@ void IR2Vec_FA::getAllSCC() {
14431453
}
14441454
}
14451455

1446-
void IR2Vec_FA::bb2Vec(BasicBlock &B, SmallVector<Function *, 15> &funcStack) {
1447-
SmallMapVector<const Instruction *, Vector, 16> partialInstValMap;
1448-
1456+
void IR2Vec_FA::bb2Vec(BasicBlock &B) {
14491457
for (auto &I : B) {
1450-
1451-
partialInstValMap[&I] = {};
1452-
IR2VEC_DEBUG(outs() << "XX------------ Call from bb2vec function "
1453-
"Started---------------------XX\n");
1454-
inst2Vec(I, funcStack, partialInstValMap);
1455-
IR2VEC_DEBUG(outs() << "YY------------Call from bb2vec function "
1456-
"Ended---------------------YY\n");
1457-
partialInstValMap.erase(&I);
1458+
inst2Vec(I);
14581459
}
14591460
}

src/include/FlowAware.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ class IR2Vec_FA {
9898
void topoDFS(int vertex, std::vector<bool> &Visited,
9999
std::vector<int> &visitStack);
100100

101-
void inst2Vec(const llvm::Instruction &I,
102-
llvm::SmallVector<llvm::Function *, 15> &funcStack,
103-
llvm::SmallMapVector<const llvm::Instruction *, IR2Vec::Vector,
104-
16> &instValMap);
101+
void inst2Vec(const llvm::Instruction &I);
105102
void traverseRD(const llvm::Instruction *inst,
106103
std::unordered_map<const llvm::Instruction *, bool> &Visited,
107104
llvm::SmallVector<const llvm::Instruction *, 10> &timeStack);
@@ -110,10 +107,8 @@ class IR2Vec_FA {
110107
std::unordered_map<const llvm::Instruction *, bool> &Visited,
111108
llvm::SmallVector<const llvm::Instruction *, 10> &set);
112109

113-
void bb2Vec(llvm::BasicBlock &B,
114-
llvm::SmallVector<llvm::Function *, 15> &funcStack);
115-
IR2Vec::Vector func2Vec(llvm::Function &F,
116-
llvm::SmallVector<llvm::Function *, 15> &funcStack);
110+
void bb2Vec(llvm::BasicBlock &B);
111+
IR2Vec::Vector func2Vec(llvm::Function &F);
117112

118113
bool isMemOp(llvm::StringRef opcode, unsigned &operand, memOpType op);
119114

0 commit comments

Comments
 (0)