From 1855f12a536cbc65346691af88347d09b4a4f7a0 Mon Sep 17 00:00:00 2001 From: Egor Mayorov Date: Tue, 27 May 2025 15:55:02 +0300 Subject: [PATCH] swap operators in AST --- src/SwapOperators/swapOperators.cpp | 120 +++++++++++++++++++++------- 1 file changed, 90 insertions(+), 30 deletions(-) diff --git a/src/SwapOperators/swapOperators.cpp b/src/SwapOperators/swapOperators.cpp index bfe33a2..4985c43 100644 --- a/src/SwapOperators/swapOperators.cpp +++ b/src/SwapOperators/swapOperators.cpp @@ -43,7 +43,7 @@ vector findFuncBlocksByFuncStatement(SgStatement *st, map result; Statement* forSt = (Statement*)st; - for (auto& func : FullIR) + for (auto& func: FullIR) { if (func.first -> funcPointer -> getCurrProcessFile() == forSt -> getCurrProcessFile() && func.first -> funcPointer -> lineNumber() == forSt -> lineNumber()) @@ -86,20 +86,25 @@ map> findAndAnalyzeLoops(SgStatement *st map> AnalyzeLoopAndFindDeps(SgForStmt* forStatement, vector loopBlocks, map>& FullIR) { map> result; - for (SAPFOR::BasicBlock* bb: loopBlocks) { + for (SAPFOR::BasicBlock* bb: loopBlocks) + { map> blockReachingDefinitions = bb -> getRD_In(); vector instructions = bb -> getInstructions(); - for (SAPFOR::IR_Block* irBlock: instructions) { + for (SAPFOR::IR_Block* irBlock: instructions) + { // TODO: Think about what to do with function calls and array references. Because there are also dependencies there that are not reflected in RD, but they must be taken into account SAPFOR::Instruction* instr = irBlock -> getInstruction(); result[instr -> getOperator()]; // take Argument 1 and it's RD and push operators to final set - if (instr -> getArg1() != NULL) { + if (instr -> getArg1() != NULL) + { SAPFOR::Argument* arg = instr -> getArg1(); set prevInstructionsNumbers = blockReachingDefinitions[arg]; - for (int i: prevInstructionsNumbers) { + for (int i: prevInstructionsNumbers) + { SAPFOR::Instruction* foundInstruction = getInstructionAndBlockByNumber(FullIR, i).first; - if (foundInstruction != NULL) { + if (foundInstruction != NULL) + { SgStatement* prevOp = foundInstruction -> getOperator(); if (prevOp != forStatement && instr -> getOperator() != forStatement && instr -> getOperator() -> lineNumber() > prevOp -> lineNumber() && prevOp -> lineNumber() > forStatement -> lineNumber()) @@ -108,12 +113,15 @@ map> AnalyzeLoopAndFindDeps(SgForStmt* forStatem } } // take Argument 2 (if exists) and it's RD and push operators to final set - if (instr -> getArg2() != NULL) { + if (instr -> getArg2() != NULL) + { SAPFOR::Argument* arg = instr -> getArg2(); set prevInstructionsNumbers = blockReachingDefinitions[arg]; - for (int i: prevInstructionsNumbers) { + for (int i: prevInstructionsNumbers) + { SAPFOR::Instruction* foundInstruction = getInstructionAndBlockByNumber(FullIR, i).first; - if (foundInstruction != NULL) { + if (foundInstruction != NULL) + { SgStatement* prevOp = foundInstruction -> getOperator(); if (prevOp != forStatement && instr -> getOperator() != forStatement&& instr -> getOperator() -> lineNumber() > prevOp -> lineNumber() && prevOp -> lineNumber() > forStatement -> lineNumber()) @@ -139,26 +147,36 @@ void buildAdditionalDeps(SgForStmt* forStatement, map variant() == LOGIF_NODE) { + if (st -> variant() == LOGIF_NODE) + { logIfOp = st; } - if (importantDepsTags.find(st -> variant()) != importantDepsTags.end()) { + if (importantDepsTags.find(st -> variant()) != importantDepsTags.end()) + { importantDeps.push_back(st); } - if (importantUpdDepsTags.find(st -> variant()) != importantUpdDepsTags.end()) { + if (importantUpdDepsTags.find(st -> variant()) != importantUpdDepsTags.end()) + { importantDeps.pop_back(); importantDeps.push_back(st); } - if (importantEndTags.find(st -> variant()) != importantEndTags.end()) { + if (importantEndTags.find(st -> variant()) != importantEndTags.end()) + { if(importantDeps.size() != 0) + { importantDeps.pop_back(); + } } st = st -> lexNext(); } @@ -180,15 +198,17 @@ struct ReadyOpCompare { } }; -vector scheduleOperations( - const map>& dependencies -) { +vector scheduleOperations(const map>& dependencies) +{ // get all statements unordered_set allStmtsSet; - for (const auto& pair : dependencies) { + for (const auto& pair : dependencies) + { allStmtsSet.insert(pair.first); for (SgStatement* dep : pair.second) + { allStmtsSet.insert(dep); + } } vector allStmts(allStmtsSet.begin(), allStmtsSet.end()); // count deps and build reversed graph @@ -199,7 +219,8 @@ vector scheduleOperations( inDegree[op] = 0; // find and remember initial dependencies unordered_set dependentStmts; - for (const auto& pair : dependencies) { + for (const auto& pair : dependencies) + { SgStatement* op = pair.first; const auto& deps = pair.second; degree[op] = deps.size(); @@ -210,40 +231,57 @@ vector scheduleOperations( graph[dep].push_back(op); } for (SgStatement* op : allStmts) + { if (!degree.count(op)) + { degree[op] = 0; + } + } // build queues using PQ = priority_queue, ReadyOpCompare>; PQ readyDependent; queue readyIndependent; size_t arrivalCounter = 0; - for (auto op : allStmts) { - if (inDegree[op] == 0) { - if (dependentStmts.count(op)) { + for (auto op : allStmts) + { + if (inDegree[op] == 0) + { + if (dependentStmts.count(op)) + { readyDependent.emplace(op, degree[op], arrivalCounter++); - } else { + } + else + { readyIndependent.push(op); } } } // main sort algorythm vector executionOrder; - while (!readyDependent.empty() || !readyIndependent.empty()) { + while (!readyDependent.empty() || !readyIndependent.empty()) + { SgStatement* current = nullptr; - if (!readyDependent.empty()) { + if (!readyDependent.empty()) + { current = readyDependent.top().stmt; readyDependent.pop(); - } else { + } + else + { current = readyIndependent.front(); readyIndependent.pop(); } executionOrder.push_back(current); - for (SgStatement* neighbor : graph[current]) { + for (SgStatement* neighbor : graph[current]) + { inDegree[neighbor]--; if (inDegree[neighbor] == 0) { - if (dependentStmts.count(neighbor)) { + if (dependentStmts.count(neighbor)) + { readyDependent.emplace(neighbor, degree[neighbor], arrivalCounter++); - } else { + } + else + { readyIndependent.push(neighbor); } } @@ -252,6 +290,21 @@ vector scheduleOperations( return executionOrder; } +void buildNewAST(SgStatement* loop, vector& newBody) +{ + SgStatement* endDo = loop->lastNodeOfStmt(); + SgStatement* st = loop; + int lineNum = loop -> lineNumber() + 1; + for (int i = 0; i < newBody.size(); i++) + { + st -> setLexNext(*newBody[i]); + st = st -> lexNext(); + st -> setlineNumber(lineNum); + lineNum++; + } + st -> setLexNext(*endDo); +} + void runSwapOperators(SgFile *file, std::map>& loopGraph, std::map>& FullIR, int& countOfTransform) @@ -286,6 +339,13 @@ void runSwapOperators(SgFile *file, std::map lineNumber() > firstLine) cout << v -> lineNumber() << endl; + buildNewAST(loopForAnalyze.first, new_order); + st = loopForAnalyze.first -> lexNext(); + while (st != loopForAnalyze.first -> lastNodeOfStmt()) + { + cout << st -> lineNumber() << " " << st -> sunparse() << endl; + st = st -> lexNext(); + } } }