From 601c6b6e882ff1ef0311f6deb05522e5547b6477 Mon Sep 17 00:00:00 2001 From: Egor Mayorov Date: Sat, 24 May 2025 19:56:15 +0300 Subject: [PATCH] biulding new order --- src/SwapOperators/swapOperators.cpp | 149 +++++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 16 deletions(-) diff --git a/src/SwapOperators/swapOperators.cpp b/src/SwapOperators/swapOperators.cpp index cb76dcb..078c872 100644 --- a/src/SwapOperators/swapOperators.cpp +++ b/src/SwapOperators/swapOperators.cpp @@ -80,14 +80,16 @@ map> findAndAnalyzeLoops(SgStatement *st return result; } -map> AnalyzeLoopAndFindDeps(SgForStmt* forStatement, vector loopBlocks, map>& FullIR) +map> AnalyzeLoopAndFindDeps(SgForStmt* forStatement, vector loopBlocks, map>& FullIR) { - map> result; + map> result; for (SAPFOR::BasicBlock* bb: loopBlocks) { std::map> blockReachingDefinitions = bb -> getRD_In(); vector instructions = bb -> getInstructions(); 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) { SAPFOR::Argument* arg = instr -> getArg1(); @@ -122,18 +124,119 @@ map> AnalyzeLoopAndFindDeps(SgForStmt* return result; } -int RebuildLoop(int firstLine, int lastLine, map> moveRules) { - // only cout done yet (( - cout << "LOOP ANALYZE FROM " << firstLine << " TO " << lastLine << " RES\n" << endl; - for (auto r: moveRules) { - cout << "OPERATOR: " << endl; - cout << r.first -> lineNumber() << r.first -> sunparse(); - cout << "DEPENDS FROM NEXT: " << endl; - for (SgStatement* st: r.second) - cout << st -> lineNumber() << endl; +// int PrintSmthFromLoop(int firstLine, int lastLine, map> moveRules) { +// // only cout done yet (( +// cout << "LOOP ANALYZE FROM " << firstLine << " TO " << lastLine << " RES\n" << endl; +// for (auto r: moveRules) { +// cout << "OPERATOR: " << endl; +// cout << r.first -> lineNumber() << r.first -> sunparse(); +// cout << "DEPENDS FROM NEXT: " << endl; +// for (SgStatement* st: r.second) +// cout << st -> lineNumber() << endl; +// } +// cout << "\n\n\n"; +// return 0; +// } + +void GenNodesOfGraph( + const map>& dependencies, + set& allNodes, + map>& outEdges, + map>& inEdges) +{ + for (const auto& node: dependencies) { + SgStatement* u = node.first; + allNodes.insert(u); + for (SgStatement* v: node.second) { + allNodes.insert(v); + outEdges[v].insert(u); + inEdges[u].insert(v); + outEdges[u]; + inEdges[v]; + } + outEdges[u]; + inEdges[u]; } - cout << "\n\n\n"; - return 0; +} + +vector> FindLinksInGraph( + const set& allNodes, + const map>& outEdges, + const map>& inEdges) +{ + set visited; + vector> components; + for (SgStatement* v: allNodes) { + if (visited.count(v)) { + continue; + } + set component; + queue q; + q.push(v); + visited.insert(v); + + while (!q.empty()) { + SgStatement* curr = q.front(); + q.pop(); + component.insert(curr); + for (SgStatement* neighbour: outEdges.at(curr)) { + if (!visited.count(neighbour)) { + q.push(neighbour); visited.insert(neighbour); + } + } + for (SgStatement* neighbour: inEdges.at(curr)) { + if (!visited.count(neighbour)) { + q.push(neighbour); visited.insert(neighbour); + } + } + } + components.push_back(component); + } + return components; +} + +vector SortComponent( + const set& component, + const map>& outEdges, + const map>& inEdges) +{ + map inDegree; + for (auto v: component) { + inDegree[v] = inEdges.at(v).size(); + } + queue q; + for (auto v : component) { + if (inDegree[v] == 0) q.push(v); + } + vector result; + while (!q.empty()) { + auto curr = q.front(); + q.pop(); + result.push_back(curr); + for (SgStatement* neighbour: outEdges.at(curr)) { + if (component.count(neighbour)) { + inDegree[neighbour]--; + if (inDegree[neighbour] == 0) { + q.push(neighbour); + } + } + } + } + return result; +} + +vector SortNoInterleaving(const map>& dependencies) +{ + set allNodes; + map> outEdges, inEdges; + GenNodesOfGraph(dependencies, allNodes, outEdges, inEdges); + auto components = FindLinksInGraph(allNodes, outEdges, inEdges); + vector totalOrder; + for (auto& comp : components) { + auto part = SortComponent(comp, outEdges, inEdges); + totalOrder.insert(totalOrder.end(), part.begin(), part.end()); + } + return totalOrder; } void runSwapOperators(SgFile *file, std::map>& loopGraph, std::map>& FullIR, int& countOfTransform) @@ -149,11 +252,25 @@ void runSwapOperators(SgFile *file, std::map> loopsMapping = findAndAnalyzeLoops(st, blocks); for (pair> loopForAnalyze: loopsMapping) { - map> moveRules = AnalyzeLoopAndFindDeps(loopForAnalyze.first, loopForAnalyze.second, FullIR); - if (moveRules.size() != 0) { + map> dependencyGraph = AnalyzeLoopAndFindDeps(loopForAnalyze.first, loopForAnalyze.second, FullIR); + // TODO: Write a function that will go through the operators and update all dependencies so that there are no mix-ups and splits inside the semantic blocks (for if, do and may be some other cases) + cout << "\n\n"; + for (auto v: dependencyGraph) { + cout << "OPERATOR: " << v.first -> lineNumber() << "\nDEPENDS ON:" << endl; + for (auto vv: v.second) { + cout << vv -> lineNumber() << " "; + } + cout << endl; + } + if (dependencyGraph.size() != 0) { int firstLine = loopForAnalyze.first -> lineNumber(); int lastLine = loopForAnalyze.first -> lastNodeOfStmt() -> lineNumber(); - countOfTransform += RebuildLoop(firstLine, lastLine, moveRules); + // countOfTransform += PrintSmthFromLoop(firstLine, lastLine, dependencyGraph); + vector new_order = SortNoInterleaving(dependencyGraph); + cout << "\n\nLOOP ANALYZE FROM " << firstLine << " TO " << lastLine << " RES\n" << endl; + for (auto v: new_order) + if (v -> lineNumber() > firstLine) + cout << v -> lineNumber() << " "; } } }