From 65237e4d635e5b456183b541bf5a125a9ac40804 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Sun, 22 Jun 2025 09:19:37 +0300 Subject: [PATCH] added inductive variables and loop type to LoopGraph --- src/CFGraph/private_variables_analysis.cpp | 4 +- .../directive_creator_base.cpp | 12 ++--- src/Distribution/DvmhDirective.cpp | 2 +- src/GraphLoop/graph_loops.cpp | 9 +++- src/GraphLoop/graph_loops.h | 52 ++++++++++++++++--- src/GraphLoop/graph_loops_base.cpp | 4 +- src/LoopAnalyzer/loop_analyzer.cpp | 2 +- src/PrivateAnalyzer/region.cpp | 2 +- .../LoopCombining/loops_combiner.cpp | 26 +++++----- .../LoopSplitting/loops_splitter.cpp | 2 +- .../PrivateArrayRemoving/private_removing.cpp | 2 +- .../private_arrays_resizing.cpp | 4 +- src/Utils/version.h | 2 +- 13 files changed, 83 insertions(+), 40 deletions(-) diff --git a/src/CFGraph/private_variables_analysis.cpp b/src/CFGraph/private_variables_analysis.cpp index 1a5b24a..653ca2a 100644 --- a/src/CFGraph/private_variables_analysis.cpp +++ b/src/CFGraph/private_variables_analysis.cpp @@ -250,7 +250,7 @@ static set analyzeLoop(LoopGraph* loop, const set& commonArgs, FuncInfo* func, map>& messages) { - if (!loop->isFor) + if (!loop->isFor()) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); //should be called only with FOR loops SgStatement* loop_operator = loop->loop->GetOriginal(); @@ -450,7 +450,7 @@ static void recAnalyzeLoop(LoopGraph* loop, const set& bloc const map& commonArgs, FuncInfo* func, map>& messages) { - const auto& loop_body = loop->isFor ? analyzeLoop(loop, blocks, commonVars, commonArgs, func, messages) : blocks; + const auto& loop_body = loop->isFor() ? analyzeLoop(loop, blocks, commonVars, commonArgs, func, messages) : blocks; for (const auto& inner_loop : loop->children) recAnalyzeLoop(inner_loop, loop_body, commonVars, commonArgs, func, messages); diff --git a/src/DirectiveProcessing/directive_creator_base.cpp b/src/DirectiveProcessing/directive_creator_base.cpp index 7cdb252..c05e281 100644 --- a/src/DirectiveProcessing/directive_creator_base.cpp +++ b/src/DirectiveProcessing/directive_creator_base.cpp @@ -75,7 +75,7 @@ static LoopGraph* createDirectiveForLoop(LoopGraph *currentLoop, MapToArray &mai } } - directive->parallel.push_back(currentLoop->loopSymbol); + directive->parallel.push_back(currentLoop->loopSymbol()); directive->arrayRef = mainArray.arrayRef; DIST::Array *tmp = mainArray.arrayRef; @@ -84,7 +84,7 @@ static LoopGraph* createDirectiveForLoop(LoopGraph *currentLoop, MapToArray &mai for (int i = 0; i < tmp->GetDimSize(); ++i) { if (i == pos) - directive->on.push_back(make_pair(currentLoop->loopSymbol, mainAccess)); + directive->on.push_back(make_pair(currentLoop->loopSymbol(), mainAccess)); else directive->on.push_back(make_pair("*", make_pair(0, 0))); } @@ -808,7 +808,7 @@ void createParallelDirectives(const mapGetDimSize(); ++i) { if (i == dimPos) - parDir->on2.push_back(make_pair(currLoop->loopSymbol, mainAccess)); + parDir->on2.push_back(make_pair(currLoop->loopSymbol(), mainAccess)); else parDir->on2.push_back(make_pair("*", make_pair(0, 0))); } @@ -1100,7 +1100,7 @@ static bool tryToResolveUnmatchedDims(const map> &dim LoopGraph* tmpL = loop; for (int z = 0; z < nested; ++z) { - deprecateToMatch.insert(tmpL->loopSymbol); + deprecateToMatch.insert(tmpL->loopSymbol()); if (tmpL->children.size()) tmpL = tmpL->children[0]; else if (z != nested - 1) @@ -1113,7 +1113,7 @@ static bool tryToResolveUnmatchedDims(const map> &dim tmpL = loop->parent; while (tmpL) { - if (!tmpL->isFor) // TODO: need to add all inductive variables! + if (tmpL->isWhile()) // TODO: need to add all inductive variables! { SgWhileStmt* dow = isSgWhileStmt(tmpL->loop->GetOriginal()); if (dow->conditional()) @@ -1124,7 +1124,7 @@ static bool tryToResolveUnmatchedDims(const map> &dim } } else - deprecateToMatch.insert(tmpL->loopSymbol); + deprecateToMatch.insert(tmpL->loopSymbol()); tmpL = tmpL->parent; } diff --git a/src/Distribution/DvmhDirective.cpp b/src/Distribution/DvmhDirective.cpp index 558f4c0..421eea8 100644 --- a/src/Distribution/DvmhDirective.cpp +++ b/src/Distribution/DvmhDirective.cpp @@ -346,7 +346,7 @@ static vector { needToAdd = true; dim_found = true; - subs[i] = new SgVarRefExp(findSymbolOrCreate(file, currLoop->loopSymbol)); + subs[i] = new SgVarRefExp(findSymbolOrCreate(file, currLoop->loopSymbol())); break; } } diff --git a/src/GraphLoop/graph_loops.cpp b/src/GraphLoop/graph_loops.cpp index e35ae5c..a749f9f 100644 --- a/src/GraphLoop/graph_loops.cpp +++ b/src/GraphLoop/graph_loops.cpp @@ -699,7 +699,12 @@ void loopGraphAnalyzer(SgFile *file, vector &loopGraph, const vector newLoop->hasPrints = hasThisIds(st, newLoop->linesOfIO, { WRITE_STAT, READ_STAT, OPEN_STAT, CLOSE_STAT, PRINT_STAT } ); // FORMAT_STAT newLoop->hasStops = hasThisIds(st, newLoop->linesOfStop, { STOP_STAT, PAUSE_NODE }); newLoop->hasDvmIntervals = hasThisIds(st, tmpLines, { DVM_INTERVAL_DIR, DVM_ENDINTERVAL_DIR, DVM_EXIT_INTERVAL_DIR }); - newLoop->isFor = isSgForStmt(st) ? true : false; + if (isSgForStmt(st)) + newLoop->loopType = LoopType::FOR; + else if (isSgWhileStmt(st)) + newLoop->loopType = LoopType::WHILE; + else + newLoop->loopType = LoopType::NONE; newLoop->inCanonicalFrom = isSgForStmt(st) ? true : false; newLoop->hasSubstringRefs = hasSubstringRef(st); @@ -777,7 +782,7 @@ void loopGraphAnalyzer(SgFile *file, vector &loopGraph, const vector newLoop->startEndExpr = std::make_pair((Expression*)NULL, (Expression*)NULL); newLoop->loop = new Statement(st); - newLoop->loopSymbol = st->symbol() ? st->symbol()->identifier() : "unknown"; + newLoop->loopSymbols.addMainVar(st->symbol() ? st->symbol()->identifier() : "unknown"); findArrayRefs(newLoop); SgStatement *lexPrev = st->lexPrev(); diff --git a/src/GraphLoop/graph_loops.h b/src/GraphLoop/graph_loops.h index 70d12e5..778221d 100644 --- a/src/GraphLoop/graph_loops.h +++ b/src/GraphLoop/graph_loops.h @@ -25,6 +25,33 @@ namespace DIST = Distribution; void getRealArrayRefs(DIST::Array* addTo, DIST::Array* curr, std::set& realArrayRefs, const std::map>& arrayLinksByFuncCalls); void getAllArrayRefs(DIST::Array* addTo, DIST::Array* curr, std::set& realArrayRefs, const std::map>& arrayLinksByFuncCalls); +enum class LoopType { NONE, FOR, WHILE, IMPLICIT }; + +struct InductiveVariables +{ +private: + std::string mainVar; + std::set allVars; + +public: + InductiveVariables() { } + + explicit InductiveVariables(const std::string& mainVar, const std::set& allVars) : mainVar(mainVar), allVars(allVars) { }; + + std::string getMainVar() const { return mainVar; } + std::set getAllVars() const { return allVars; } + + void addVar(const std::string& var) { allVars.insert(var); } + void addMainVar(const std::string& var) { mainVar = var; allVars.insert(var); } + + void replaceMainVar(const std::string& var) + { + allVars.erase(mainVar); + + addMainVar(var); + } +}; + struct LoopGraph { private: @@ -70,7 +97,7 @@ public: calculatedCountOfIters = 0; executionTimeInSec = -1.0; inDvmhRegion = 0; - isFor = false; + loopType = LoopType::NONE; inCanonicalFrom = false; hasAccessToSubArray = false; hasSubstringRefs = false; @@ -113,21 +140,24 @@ public: { return hasUnknownArrayDep || hasUnknownScalarDep || hasGoto || hasPrints || (hasConflicts.size() != 0) || hasStops || hasNonPureProcedures || hasUnknownArrayAssigns || hasNonRectangularBounds || hasIndirectAccess || hasWritesToNonDistribute || hasDifferentAlignRules || hasDvmIntervals || - !isFor || lastprivateScalars.size() || hasAccessToSubArray || hasSubstringRefs; + !isFor() || lastprivateScalars.size() || hasAccessToSubArray || hasSubstringRefs; } bool hasLimitsToSplit() const { - return hasGoto || hasStops || !isFor || hasPrints; + return hasGoto || hasStops || !isFor() || hasPrints; } bool hasLimitsToCombine() const { - return hasGoto || hasStops || !isFor || hasPrints || linesOfCycle.size(); + return hasGoto || hasStops || !isFor() || hasPrints || linesOfCycle.size(); } void addConflictMessages(std::vector *messages) { + if (messages == NULL) + return; + const int line = altLineNum > 0 ? altLineNum : lineNum; if (hasUnknownArrayDep) messages->push_back(Messages(NOTE, line, R113, L"unknown array dependency prevents parallelization of this loop", 3006)); @@ -168,7 +198,7 @@ public: if (hasDvmIntervals) messages->push_back(Messages(NOTE, line, R145, L"DVM intervals prevent parallelization of this loop", 3006)); - if (!isFor || !inCanonicalFrom) + if (!isFor() || !inCanonicalFrom) messages->push_back(Messages(NOTE, line, R178, L"This type of loop is not supported by the system", 3006)); if (lastprivateScalars.size()) @@ -393,6 +423,14 @@ public: void* getRealStat(const char* file) const; + bool isFor() const { return loopType == LoopType::FOR; } + + bool isWhile() const { return loopType == LoopType::WHILE; } + + bool isImplicit() const { return loopType == LoopType::IMPLICIT; } + + std::string loopSymbol() const { return loopSymbols.getMainVar(); } + public: int lineNum; int altLineNum; @@ -407,7 +445,7 @@ public: int startVal, endVal, stepVal; std::tuple startEndStepVals; - std::string loopSymbol; + InductiveVariables loopSymbols; std::pair startEndExpr; bool hasGoto; @@ -448,7 +486,7 @@ public: bool hasSubstringRefs; - bool isFor; + LoopType loopType; bool inCanonicalFrom; diff --git a/src/GraphLoop/graph_loops_base.cpp b/src/GraphLoop/graph_loops_base.cpp index 031a4d6..38fb3dd 100644 --- a/src/GraphLoop/graph_loops_base.cpp +++ b/src/GraphLoop/graph_loops_base.cpp @@ -551,7 +551,7 @@ void addToDistributionGraph(const map> continue; } - if (!loopAccess.first->isFor) + if (!loopAccess.first->isFor()) continue; DIST::GraphCSR& G = currReg->GetGraphToModify(); @@ -775,7 +775,7 @@ static void isAllOk(const vector &loops, vector &currMessa { if (loops[i]->region) { - if (loops[i]->countOfIters == 0 && loops[i]->region && loops[i]->isFor) + if (loops[i]->countOfIters == 0 && loops[i]->region && loops[i]->isFor()) { wstring bufE, bufR; __spf_printToLongBuf(bufE, L" Can not calculate count of iterations for this loop, information about iterations in all loops in parallel regions '%s' will be ignored", diff --git a/src/LoopAnalyzer/loop_analyzer.cpp b/src/LoopAnalyzer/loop_analyzer.cpp index dd312bf..0fdcb67 100644 --- a/src/LoopAnalyzer/loop_analyzer.cpp +++ b/src/LoopAnalyzer/loop_analyzer.cpp @@ -2199,7 +2199,7 @@ void loopAnalyzer(SgFile *file, vector ®ions, mapregion = reg; - tmpLoop->isFor = true; + tmpLoop->loopType = LoopType::FOR; tmpLoops.push_back(tmpLoop); diff --git a/src/PrivateAnalyzer/region.cpp b/src/PrivateAnalyzer/region.cpp index 71fbd22..252c9d2 100644 --- a/src/PrivateAnalyzer/region.cpp +++ b/src/PrivateAnalyzer/region.cpp @@ -51,7 +51,7 @@ pair> GetBasicBlocksForL } static void BuildLoopIndex(map& loopForIndex, LoopGraph* loop) { - string index = loop->loopSymbol; + string index = loop->loopSymbol(); loopForIndex[index] = loop; for (const auto& childLoop : loop->children) diff --git a/src/Transformations/LoopCombining/loops_combiner.cpp b/src/Transformations/LoopCombining/loops_combiner.cpp index c0ac86e..ed08b6d 100644 --- a/src/Transformations/LoopCombining/loops_combiner.cpp +++ b/src/Transformations/LoopCombining/loops_combiner.cpp @@ -20,7 +20,7 @@ using std::wstring; static SgSymbol* getLoopSymbol(const LoopGraph* loop) { - if (!loop || !loop->isFor) + if (!loop || !loop->isFor()) return NULL; SgForStmt* stmt = (SgForStmt*)loop->loop->GetOriginal(); @@ -667,12 +667,12 @@ static void renameIterationVariables(LoopGraph* loop, const maploopSymbol; - for (auto& pair : symbols) + string& loopName = loop->loopSymbol(); + for (auto& [from, to] : symbols) { - if (pair.first->identifier() == loopName) + if (from->identifier() == loopName) { - loop->loopSymbol = (string)pair.second->identifier(); + loop->loopSymbols.replaceMainVar(to->identifier()); break; } } @@ -691,9 +691,9 @@ static void renameVariablesInLoop(LoopGraph* loop, const mapvariant() == FOR_NODE) { SgForStmt* for_st = (SgForStmt*)st; - for (auto& pair : symbols) - if (isEqSymbols(pair.first, for_st->symbol())) - for_st->setDoName(*pair.second); + for (auto& [from, to] : symbols) + if (isEqSymbols(from, for_st->symbol())) + for_st->setDoName(*to); } for (int i = 0; i < 3; ++i) @@ -710,12 +710,12 @@ static void renamePrivatesInMap(LoopGraph* loop, const map for (auto& priv : privates->second) { bool found = false; - for (auto& pair : symbols) + for (auto& [from, to] : symbols) { - if (isEqSymbols(priv, pair.first)) + if (isEqSymbols(priv, from)) { found = true; - newList.insert(pair.second); + newList.insert(to); break; } } @@ -1590,7 +1590,7 @@ static bool combine(LoopGraph* firstLoop, const vector& nextLoops, s bool wasCombine = false; for (LoopGraph* loop : nextLoops) { - if (!loop->isFor) + if (!loop->isFor()) return wasCombine; int perfectLoop = std::min(firstLoop->perfectLoop, loop->perfectLoop); @@ -1723,7 +1723,7 @@ static bool tryToCombine(vector& loopGraphs, mapisFor) + if (!loop->isFor()) continue; vector nextLoops = getNextLoops(loop, loopGraphs, -1); diff --git a/src/Transformations/LoopSplitting/loops_splitter.cpp b/src/Transformations/LoopSplitting/loops_splitter.cpp index 07d5776..e06ddf0 100644 --- a/src/Transformations/LoopSplitting/loops_splitter.cpp +++ b/src/Transformations/LoopSplitting/loops_splitter.cpp @@ -1035,7 +1035,7 @@ int splitLoops(SgFile *file, vector &loopGraphs, vector &m for (auto &loopPair : mapLoopGraph) { - if (!loopPair.second->isFor) + if (!loopPair.second->isFor()) continue; LoopGraph *loop = loopPair.second; diff --git a/src/Transformations/PrivateArrayRemoving/private_removing.cpp b/src/Transformations/PrivateArrayRemoving/private_removing.cpp index 0ddd7a6..3869c4f 100644 --- a/src/Transformations/PrivateArrayRemoving/private_removing.cpp +++ b/src/Transformations/PrivateArrayRemoving/private_removing.cpp @@ -2257,7 +2257,7 @@ void removePrivatesAnalysis(string filename, { for (LoopGraph* loop : loopGraphs) { - if (!loop->isFor) + if (!loop->isFor()) continue; SgForStmt* loopStmt = (SgForStmt*)loop->loop->GetOriginal(); diff --git a/src/Transformations/PrivateArrayResizing/private_arrays_resizing.cpp b/src/Transformations/PrivateArrayResizing/private_arrays_resizing.cpp index d2d4a58..3c491b8 100644 --- a/src/Transformations/PrivateArrayResizing/private_arrays_resizing.cpp +++ b/src/Transformations/PrivateArrayResizing/private_arrays_resizing.cpp @@ -74,7 +74,7 @@ static void fillIterationVariables(const LoopGraph* loop, set& vars, int { if (dimensions == -1) { - vars.insert(loop->loopSymbol); + vars.insert(loop->loopSymbol()); for (LoopGraph* child : loop->children) fillIterationVariables(child, vars); } @@ -82,7 +82,7 @@ static void fillIterationVariables(const LoopGraph* loop, set& vars, int { for (int i = 0; i < dimensions; ++i) { - vars.insert(loop->loopSymbol); + vars.insert(loop->loopSymbol()); if (i != dimensions - 1) loop = loop->children[0]; } diff --git a/src/Utils/version.h b/src/Utils/version.h index 092925c..eb41217 100644 --- a/src/Utils/version.h +++ b/src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2430" +#define VERSION_SPF "2431"