From 99f5febd58bf7ee50b84f58d88d11fa4609fb937 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Wed, 7 May 2025 19:59:54 +0300 Subject: [PATCH 01/15] added json for SPF_GetAllDeclaratedArrays pass --- src/Distribution/Array.h | 60 +++++++++++++++++++++++++ src/Utils/version.h | 2 +- src/VisualizerCalls/get_information.cpp | 13 +++--- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/Distribution/Array.h b/src/Distribution/Array.h index 4d56db1..17365a0 100644 --- a/src/Distribution/Array.h +++ b/src/Distribution/Array.h @@ -9,6 +9,7 @@ #include "DvmhDirectiveBase.h" #include "../Utils/utils.h" #include "../Utils/errors.h" +#include "../Utils/json.hpp" class Symbol; class Expression; @@ -20,6 +21,7 @@ struct FuncInfo; #define MAP std::map #define SET std::set #define TO_STR std::to_string +#define JSON nlohmann::json #if __SPF extern int sharedMemoryParallelization; @@ -424,6 +426,7 @@ namespace Distribution void ClearShadowSpecs() { allShadowSpecs.clear(); } + //TODO: to remove STRING toString() { STRING retVal = ""; @@ -466,6 +469,63 @@ namespace Distribution return retVal; } + JSON toJson() + { + JSON retVal; + + retVal["id"] = (int64_t)id; + retVal["name"] = name; + retVal["shortName"] = shortName; + + retVal["dimSize"] = dimSize; + retVal["typeSize"] = typeSize; + retVal["state"] = (int)isNonDistribute; + retVal["location"] = (int)locationPos.first; + retVal["locName"] = locationPos.second; + + retVal["isTemplFlag"] = (int)isTemplFlag; + retVal["isLoopArrayFlag"] = (int)isLoopArrayFlag; + + JSON deprToDist = nlohmann::json::array(); + for (int i = 0; i < depracateToDistribute.size(); ++i) + deprToDist.push_back((int)depracateToDistribute[i]); + retVal["depracateToDist"] = deprToDist; + + JSON mappedDimsJ = nlohmann::json::array(); + for (int i = 0; i < mappedDims.size(); ++i) + mappedDimsJ.push_back((int)mappedDims[i]); + retVal["mappedDims"] = mappedDimsJ; + + JSON sizesJ = nlohmann::json::array(); + for (int i = 0; i < sizes.size(); ++i) + { + JSON pair; + pair["key"] = sizes[i].first; + pair["value"] = sizes[i].second; + sizesJ.push_back(pair); + } + retVal["sizes"] = sizesJ; + + JSON regions = nlohmann::json::array(); + for (auto& reg : containsInRegions) + regions.push_back(reg); + retVal["regions"] = regions; + + + JSON declPlacesJ = nlohmann::json::array(); + for (auto& place : declPlaces) + { + JSON elem; + elem["array_name"] = place.first; + elem["array_loc"] = place.second; + + declPlacesJ.push_back(elem); + } + retVal["declPlaces"] = declPlacesJ; + + return retVal; + } + Array* GetTemplateArray(const uint64_t regionId, bool withCheck = true) { TemplateLink *currLink = getTemlateInfo(regionId, withCheck); diff --git a/src/Utils/version.h b/src/Utils/version.h index 2633120..ed408b9 100644 --- a/src/Utils/version.h +++ b/src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2416" +#define VERSION_SPF "2417" diff --git a/src/VisualizerCalls/get_information.cpp b/src/VisualizerCalls/get_information.cpp index 698d970..058108f 100644 --- a/src/VisualizerCalls/get_information.cpp +++ b/src/VisualizerCalls/get_information.cpp @@ -1416,14 +1416,17 @@ int SPF_GetAllDeclaratedArrays(void*& context, int winHandler, short *options, s { runPassesForVisualizer(projName, { GET_ALL_ARRAY_DECL }); - string resVal = ""; - for (auto f = declaredArrays.begin(); f != declaredArrays.end(); ++f) + json arrays = json::array(); + for (const auto& [_, array] : declaredArrays) { - if (f != declaredArrays.begin()) - resVal += "@"; - resVal += f->second.first->toString(); + json jArray = array.first->toJson(); + arrays.push_back(jArray); } + json allArrays; + allArrays["allArrays"] = arrays; + string resVal = allArrays.dump(); + copyStringToShort(result, resVal); retSize = (int)resVal.size() + 1; } From 29a8c30370c6c02b5197efea6d72803c8e4e6d6f Mon Sep 17 00:00:00 2001 From: ALEXks Date: Wed, 7 May 2025 20:16:43 +0300 Subject: [PATCH 02/15] fixed Array::toJson --- src/Distribution/Array.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Distribution/Array.h b/src/Distribution/Array.h index 17365a0..dd9f615 100644 --- a/src/Distribution/Array.h +++ b/src/Distribution/Array.h @@ -516,8 +516,8 @@ namespace Distribution for (auto& place : declPlaces) { JSON elem; - elem["array_name"] = place.first; - elem["array_loc"] = place.second; + elem["file"] = place.first; + elem["line"] = place.second; declPlacesJ.push_back(elem); } From 738f2c5d127133971ca3113dff787972589f199c Mon Sep 17 00:00:00 2001 From: xnpster Date: Sat, 10 May 2025 15:27:03 +0300 Subject: [PATCH 03/15] Support APPLY_FRAGMENT(WEIGHT(double)) clause: add weights to fragments of parallel regions (and use it in at loopAnalyzer) --- .gitignore | 1 + src/LoopAnalyzer/loop_analyzer.cpp | 29 +++- src/ParallelizationRegions/ParRegions.cpp | 182 ++++++++++++++++++---- src/ParallelizationRegions/ParRegions.h | 47 ++++-- src/Utils/utils.cpp | 31 ++++ 5 files changed, 246 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index 0000101..d9bf98a 100644 --- a/.gitignore +++ b/.gitignore @@ -74,6 +74,7 @@ Sapfor/Sapc++/Sapc++/x64/ Sapfor/Sapc++/x64/ /build +/cmake-build-debug Sapfor/out/ Sapfor/_bin/* diff --git a/src/LoopAnalyzer/loop_analyzer.cpp b/src/LoopAnalyzer/loop_analyzer.cpp index 785f134..4568c85 100644 --- a/src/LoopAnalyzer/loop_analyzer.cpp +++ b/src/LoopAnalyzer/loop_analyzer.cpp @@ -1635,9 +1635,9 @@ void loopAnalyzer(SgFile *file, vector ®ions, mapfunctions(i)->symbol()->identifier(); #if _WIN32 if (file->functions(i)->variant() != MODULE_STMT) - sendMessage_2lvl(wstring(L" '") + wstring(fName.begin(), fName.end()) + L"'"); + sendMessage_2lvl(wstring(L"��������� ������� '") + wstring(fName.begin(), fName.end()) + L"'"); else - sendMessage_2lvl(wstring(L" '") + wstring(fName.begin(), fName.end()) + L"'"); + sendMessage_2lvl(wstring(L"��������� ������ '") + wstring(fName.begin(), fName.end()) + L"'"); #else if (file->functions(i)->variant() != MODULE_STMT) sendMessage_2lvl(wstring(L"processing function '") + wstring(fName.begin(), fName.end()) + L"'"); @@ -1710,6 +1710,9 @@ void loopAnalyzer(SgFile *file, vector ®ions, map> notMappedDistributedArrays; set mappedDistrbutedArrays; + const ParallelRegionLines* prevParLines = NULL; + double prevLinesWeight = 1.0; + double currentWeight = 1.0; while (st != lastNode) { @@ -1733,13 +1736,31 @@ void loopAnalyzer(SgFile *file, vector ®ions, maplineNumber() < -1 ? st->localLineNumber() : st->lineNumber(); - ParallelRegion *currReg = getRegionByLine(regions, st->fileName(), currentLine); + auto regAndLines = getRegionAndLinesByLine(regions, st->fileName(), currentLine); + + auto *currReg = regAndLines.first; + auto *parLines = regAndLines.second; if (currReg == NULL) { st = st->lexNext(); continue; } + if (parLines != prevParLines) + { + prevParLines = parLines; + auto newParLinesWeight = parLines ? parLines->weight : 1.0; + + if (prevParLines) + currentWeight /= prevLinesWeight; + + if (parLines) + currentWeight *= newParLinesWeight; + + prevLinesWeight = newParLinesWeight; + prevParLines = parLines; + } + if (isSgExecutableStatement(st) == NULL) delcsStatViewed.insert(st); else if (!sharedMemoryParallelization && @@ -2168,7 +2189,7 @@ void loopAnalyzer(SgFile *file, vector ®ions, mapfunctions(i)->symbol()->identifier(); #ifdef _WIN32 - sendMessage_2lvl(wstring(L" ") + std::to_wstring(idx) + L"/" + std::to_wstring(convertedLoopInfo.size())); + sendMessage_2lvl(wstring(L"��������� ����� ") + std::to_wstring(idx) + L"/" + std::to_wstring(convertedLoopInfo.size())); #else sendMessage_2lvl(wstring(L"processing loop ") + std::to_wstring(idx) + L"/" + std::to_wstring(convertedLoopInfo.size())); #endif diff --git a/src/ParallelizationRegions/ParRegions.cpp b/src/ParallelizationRegions/ParRegions.cpp index 85d1953..d185ea9 100644 --- a/src/ParallelizationRegions/ParRegions.cpp +++ b/src/ParallelizationRegions/ParRegions.cpp @@ -93,8 +93,10 @@ static inline SgStatement* getParentStat(SgStatement *st) return iterator; } -static void updateRegionInfo(SgStatement *st, map> &startEnd, map> &lines_, - set &funcCallFromReg, const map &mapFuncs) +static void updateRegionInfo(SgStatement *st, map> &startEnd, + map> &lines_, + map>> &funcCallFromReg, + const map &mapFuncs) { string containsPrefix = ""; SgStatement *st_ps = getParentStat(st); @@ -103,17 +105,26 @@ static void updateRegionInfo(SgStatement *st, mapsymbol()->identifier() + string("."); extendRegionInfo(st, startEnd, lines_); + + set calls_from_statement; + if (st->variant() == PROC_STAT) { string fullName = st->symbol()->identifier(); //check contains if (mapFuncs.find(containsPrefix + fullName) != mapFuncs.end()) fullName = containsPrefix + fullName; - funcCallFromReg.insert(fullName); + calls_from_statement.insert(fullName); } for (int z = 0; z < 3; ++z) - findFuncCalls(st->expr(z), funcCallFromReg, containsPrefix, mapFuncs); + findFuncCalls(st->expr(z), calls_from_statement, containsPrefix, mapFuncs); + + string filename = st->fileName(); + int line = st->lineNumber(); + + for (const auto &func_name : calls_from_statement) + funcCallFromReg[func_name][filename].insert(line); } static void fillArrayNamesInReg(set &usedArrayInRegion, SgExpression *exp) @@ -283,6 +294,71 @@ static void checkForEmpty(SgStatement *start, SgStatement *end, vector } } +static bool parseFortranDouble(const char* str, double &val) +{ + int base_sign = 1, exp_sign = 1; + + int integer_part = 0, power = 0; + double decimal_part = 0; + while (*str && *str != '.' && *str != 'd' && *str != 'D') + { + if (*str >= '0' && *str <= '9') + integer_part = integer_part * 10 + (*str - '0'); + else if (*str == '-') + base_sign = -1; + else if (*str == '+') + base_sign = 1; + + str++; + } + + if (*str == '.') + { + str++; + + int base = 10; + while (*str >= '0' && *str <= '9') + { + decimal_part += double(*str - '0') / base; + str++; + base *= 10; + } + } + + if (*str == 'd' || *str == 'D') + { + str++; + + while (*str == '+' || *str == '-' || *str >= '0' && *str <= '9') + { + if (*str >= '0' && *str <= '9') + power = power * 10 + (*str - '0'); + else if (*str == '-') + exp_sign = -1; + else if (*str == '+') + exp_sign = 1; + + str++; + } + } + + double result = integer_part + decimal_part; + + for(int i = 0; i < power; i++) + { + if (exp_sign > 0) + result *= 10; + else + result /= 10; + } + + if (base_sign < 0) + result = -result; + + val = result; + return true; +} + void fillRegionLines(SgFile *file, vector ®ions, vector& messagesForFile, vector *loops, vector *funcs) { map mapFuncs; @@ -318,8 +394,9 @@ void fillRegionLines(SgFile *file, vector ®ions, vector> startEnd; map> lines_; - set funcCallFromReg; + map>> funcCallFromReg; bool regionStarted = false; + double fragmentWeight = 1.0; vector toDel; for (int i = 0; i < funcNum; ++i) @@ -368,6 +445,37 @@ void fillRegionLines(SgFile *file, vector ®ions, vectorsecond->callRegions.insert(0); } } + + // parse SPF_APPLY_FRAGMENT clause + auto *apply_fragment = data->expr(1); + fragmentWeight = 1.0; + + while (apply_fragment) + { + auto *curr = apply_fragment->lhs(); + + if (curr) + { + __spf_print(1, "%s %d\n", curr->unparse(), curr->variant()); + + if (curr->variant() == SPF_WEIGHT_OP) + { + if (curr->lhs() && + isSgValueExp(curr->lhs()) && + isSgValueExp(curr->lhs())->doubleValue() && + parseFortranDouble(isSgValueExp(curr->lhs())->doubleValue(), fragmentWeight)) + { + __spf_print(1, "->> %lf\n", fragmentWeight); + } + else + { + __spf_print(1, "WEIGHT clause without double argument\n"); + } + } + } + + apply_fragment = apply_fragment->rhs(); + } } if (next && next->variant() == SPF_END_PARALLEL_REG_DIR) @@ -400,10 +508,12 @@ void fillRegionLines(SgFile *file, vector ®ions, vectorAddLines(lines_[itRegInfo->first], itRegInfo->first, &itRegInfo->second); + currReg->AddLines(lines_[itRegInfo->first], itRegInfo->first, &itRegInfo->second, fragmentWeight); - for (auto &func : funcCallFromReg) - currReg->AddFuncCalls(func); + for (auto &by_func : funcCallFromReg) + for (auto &by_file : by_func.second) + for(auto &by_line : by_file.second) + currReg->AddFuncCalls(by_func.first, by_file.first, by_line); filterUserDirectives(currReg, usedArrayInRegion, userDvmRedistrDirs, userDvmRealignDirs, userDvmShadowDirs); currReg->AddUserDirectives(userDvmRealignDirs, DVM_REALIGN_DIR); @@ -496,34 +606,48 @@ void fillRegionLinesStep2(vector ®ions, const mapGetName() != "DEFAULT") for (auto &func : regions[i]->GetFuncCalls()) - setExplicitFlag(func, funcMap); + setExplicitFlag(func.first, funcMap); } for (int i = 0; i < regions.size(); ++i) { if (regions[i]->GetName() != "DEFAULT") { - set uniqFuncCalls; - for (auto &elem : regions[i]->GetFuncCalls()) - uniqFuncCalls.insert(elem); + map uniqFuncCalls; + map>> callPlaces; + + for (auto &elem : regions[i]->GetFuncCalls()) + { + double max_weight = 0; + for (auto* fragment : elem.second) + if (fragment->weight > max_weight) + max_weight = fragment->weight; + + uniqFuncCalls[elem.first] = max_weight; + } + + bool wasChanged = true; + auto funcsBefore = uniqFuncCalls; - bool wasChanged = 1; while (wasChanged) { - wasChanged = 0; + wasChanged = false; + auto updated = uniqFuncCalls; + for (auto &uniqF : uniqFuncCalls) { - auto func = funcMap.find(uniqF); + auto func = funcMap.find(uniqF.first); if (func != funcMap.end()) { - for (auto &calls : func->second->callsFrom) + for (auto &call : func->second->callsFromDetailed) { - auto it = uniqFuncCalls.find(calls); - if (it == uniqFuncCalls.end()) - { - uniqFuncCalls.insert(it, calls); - wasChanged = 1; - } + auto it = updated.find(call.detailCallsFrom.first); + if (it == updated.end()) + updated.insert({call.detailCallsFrom.first, uniqF.second}); + else + it->second = std::max(it->second, uniqF.second); + + callPlaces[call.detailCallsFrom.first][func->second->fileName].insert(call.detailCallsFrom.second); } } } @@ -532,21 +656,27 @@ void fillRegionLinesStep2(vector ®ions, const mapAddLines(it->second->linesNum, it->second->fileName); - regions[i]->AddFuncCallsToAllCalls(it->second); + regions[i]->AddLines(it->second->linesNum, it->second->fileName, NULL, elem.second); if (it->second->inRegion == 0) it->second->inRegion = 2; it->second->callRegions.insert(i); - toPrint += elem + " "; + toPrint += elem.first + " "; } } + for (auto &elem : callPlaces) + { + for (const auto &byFile : elem.second) + for (auto byLine : byFile.second) + regions[i]->AddFuncCalls(elem.first, byFile.first, byLine); + } + if (toPrint != "") __spf_print(1, "[%s]: funcs: %s\n", regions[i]->GetName().c_str(), toPrint.c_str()); } diff --git a/src/ParallelizationRegions/ParRegions.h b/src/ParallelizationRegions/ParRegions.h index 97a4a50..30087b0 100644 --- a/src/ParallelizationRegions/ParRegions.h +++ b/src/ParallelizationRegions/ParRegions.h @@ -17,7 +17,7 @@ struct ParallelRegionLines { - ParallelRegionLines() + ParallelRegionLines(double weight = 1.0) : weight(weight) { lines = std::make_pair(-1, -1); stats = std::make_pair(NULL, NULL); @@ -25,14 +25,15 @@ struct ParallelRegionLines intervalAfter = std::make_pair(NULL, NULL); } - ParallelRegionLines(const std::pair &lines) : lines(lines) + ParallelRegionLines(const std::pair &lines, double weight = 1.0) : lines(lines), weight(weight) { stats = std::make_pair(NULL, NULL); intervalBefore = std::make_pair(NULL, NULL); intervalAfter = std::make_pair(NULL, NULL); } - ParallelRegionLines(const std::pair &lines, const std::pair stats) : lines(lines), stats(stats) + ParallelRegionLines(const std::pair &lines, const std::pair stats, double weight = 1.0) + : lines(lines), stats(stats), weight(weight) { intervalBefore = std::make_pair(NULL, NULL); intervalAfter = std::make_pair(NULL, NULL); @@ -60,6 +61,8 @@ struct ParallelRegionLines // interval std::pair intervalBefore; std::pair intervalAfter; + + double weight; // weight of the fragment among all fragments of this region }; #if __SPF @@ -114,7 +117,7 @@ public: currentVariant = copy.currentVariant; } - int AddLines(const std::pair &linesToAdd, const std::string &file, const std::pair *startEnd = NULL) + int AddLines(const std::pair &linesToAdd, const std::string &file, const std::pair *startEnd = NULL, double weight = 1.0) { if (linesToAdd.first > linesToAdd.second) return -1; @@ -124,17 +127,29 @@ public: it = lines.insert(it, make_pair(file, std::vector())); if (startEnd) - it->second.push_back(ParallelRegionLines(linesToAdd, *startEnd)); + it->second.push_back(ParallelRegionLines(linesToAdd, *startEnd, weight)); else - it->second.push_back(ParallelRegionLines(linesToAdd)); + it->second.push_back(ParallelRegionLines(linesToAdd, weight)); return 0; } - void AddFuncCalls(const std::string &func) { functionsCall.insert(func); } + void AddFuncCalls(const std::string &func, const std::string &file, const int line) + { + auto *found_lines = GetLinesByLine(file, line); + + if (found_lines) + functionsCall[func].insert(found_lines); + } #if __SPF - void AddFuncCallsToAllCalls(FuncInfo *func) { allFunctionsCall.insert(func); } + void AddFuncCallsToAllCalls(FuncInfo *func, const std::string &file, const int line) + { + auto *found_lines = GetLinesByLine(file, line); + + if (found_lines) + allFunctionsCall[func].insert(found_lines); + } #endif uint64_t GetId() const { return regionId; } @@ -174,10 +189,10 @@ public: const DataDirective& GetDataDir() const { return dataDirectives; } DataDirective& GetDataDirToModify() { return dataDirectives; } - const std::set& GetFuncCalls() const { return functionsCall; } + const std::map>& GetFuncCalls() const { return functionsCall; } #if __SPF - const std::set& GetAllFuncCalls() const { return allFunctionsCall; } + const std::map>& GetAllFuncCalls() const { return allFunctionsCall; } const std::map>>& GetUsedLocalArrays() const { return usedLocalArrays; } const std::map>>& GetUsedCommonArrays() const { return usedCommonArrays; } @@ -216,7 +231,7 @@ public: } #endif - bool HasThisLine(const int line, const std::string &file) const + bool HasThisLine(const int line, const std::string &file, const ParallelRegionLines** found = nullptr) const { bool retVal = false; auto it = lines.find(file); @@ -227,6 +242,9 @@ public: if (it->second[i].lines.first <= line && it->second[i].lines.second >= line) { retVal = true; + if (found) + *found = &(it->second[i]); + break; } } @@ -283,7 +301,7 @@ public: fprintf(fileOut, " originalName '%s'\n", originalName.c_str()); fprintf(fileOut, " functions call from %d:\n", (int)functionsCall.size()); for (auto &func : functionsCall) - fprintf(fileOut, " '%s'\n", func.c_str()); + fprintf(fileOut, " '%s'\n", func.first.c_str()); fprintf(fileOut, " total lines %d:\n", (int)lines.size()); for (auto &line : lines) { @@ -353,11 +371,11 @@ private: std::string originalName; // file -> lines info std::map> lines; - std::set functionsCall; + std::map> functionsCall; // func name -> fragments with calls #if __SPF // for RESOLVE_PAR_REGIONS - std::set allFunctionsCall; + std::map> allFunctionsCall; // function -> fragments with calls std::map>> usedLocalArrays; // func -> array -> lines std::map>> usedCommonArrays; // func -> array -> lines // @@ -399,4 +417,5 @@ private: ParallelRegion* getRegionById(const std::vector& regions, const uint64_t regionId); ParallelRegion* getRegionByName(const std::vector& regions, const std::string& regionName); ParallelRegion* getRegionByLine(const std::vector& regions, const std::string& file, const int line); +std::pair getRegionAndLinesByLine(const std::vector& regions, const std::string& file, const int line); std::set getAllRegionsByLine(const std::vector& regions, const std::string& file, const int line); \ No newline at end of file diff --git a/src/Utils/utils.cpp b/src/Utils/utils.cpp index a7d2faf..d301100 100644 --- a/src/Utils/utils.cpp +++ b/src/Utils/utils.cpp @@ -1405,6 +1405,37 @@ ParallelRegion* getRegionByLine(const vector& regions, const st return NULL; } +std::pair getRegionAndLinesByLine(const vector& regions, const string& file, const int line) +{ + if (regions.size() == 1 && regions[0]->GetName() == "DEFAULT") // only default + return {regions[0], NULL}; + + else if (regions.size() > 0) + { + map regFound; + + const ParallelRegionLines* foundLines = nullptr; + + for (int i = 0; i < regions.size(); ++i) + if (regions[i]->HasThisLine(line, file, &foundLines)) + regFound[regions[i]] = foundLines; + + if (regFound.size() == 0) + return {NULL, NULL}; + else if (regFound.size() == 1) + return *regFound.begin(); + else + { + __spf_print(1, "WARN: this lines included in more than one region!!\n"); + return {NULL, NULL}; + } + } + else + return {NULL, NULL}; + + return {NULL, NULL}; +} + set getAllRegionsByLine(const vector& regions, const string& file, const int line) { set regFound; From b7ebccf045d910dafc97cb1a3f34208f6fcd5b2e Mon Sep 17 00:00:00 2001 From: Oleg Nikitin Date: Mon, 31 Mar 2025 02:50:30 +0300 Subject: [PATCH 04/15] add Collapse --- CMakeLists.txt | 4 +- src/PrivateAnalyzer/private_arrays_search.cpp | 590 ++++++++++++++++++ src/PrivateAnalyzer/private_arrays_search.h | 84 +++ 3 files changed, 677 insertions(+), 1 deletion(-) create mode 100644 src/PrivateAnalyzer/private_arrays_search.cpp create mode 100644 src/PrivateAnalyzer/private_arrays_search.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 87eb86c..1b0f054 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,7 +113,9 @@ set(OMEGA src/SageAnalysisTool/OmegaForSage/add-assert.cpp src/SageAnalysisTool/set.cpp) set(PRIV src/PrivateAnalyzer/private_analyzer.cpp - src/PrivateAnalyzer/private_analyzer.h) + src/PrivateAnalyzer/private_analyzer.h + src/PrivateAnalyzer/private_arrays_search.cpp + src/PrivateAnalyzer/private_arrays_search.h) set(FDVM ${fdvm_sources}/acc.cpp ${fdvm_sources}/acc_across.cpp diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp new file mode 100644 index 0000000..084cbcc --- /dev/null +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -0,0 +1,590 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "private_arrays_search.h" +#include "../Utils/SgUtils.h" +#include "../GraphLoop/graph_loops.h" +#include "../CFGraph/CFGraph.h" + +using namespace std; + +void print_info(LoopGraph* loop) +{ + cout << "loopSymbol: " << loop->loopSymbol << endl; + for (const auto& ops : loop->writeOpsForLoop) + { + cout << "Array name: " << ops.first->GetShortName() << endl; + for (const auto i : ops.second) + { + i.printInfo(); + } + } + if (!loop->children.empty()) + { + for (const auto child : loop->children) + { + print_info(child); + } + } +} + +static bool isParentStmt(SgStatement* stmt, SgStatement* parent) +{ + for (; stmt; stmt = stmt->controlParent()) + if (stmt == parent) + { + return true; + } + return false; +} + +/*returns head block and loop*/ +static pair> GetBasicBlocksForLoop(LoopGraph* loop, vector blocks) +{ + unordered_set block_loop; + SAPFOR::BasicBlock* head_block = nullptr; + auto loop_operator = loop->loop->GetOriginal(); + for (const auto& block : blocks) + { + if (!block || (block->getInstructions().size() == 0)) + { + continue; + } + SgStatement* first = block->getInstructions().front()->getInstruction()->getOperator(); + SgStatement* last = block->getInstructions().back()->getInstruction()->getOperator(); + if (isParentStmt(first, loop_operator) && isParentStmt(last, loop_operator)) + { + block_loop.insert(block); + + if ((!head_block) && (first == loop_operator) && (last == loop_operator) && + (block->getInstructions().size() == 2) && + (block->getInstructions().back()->getInstruction()->getOperation() == SAPFOR::CFG_OP::JUMP_IF)) + { + head_block = block; + } + + } + } + return { head_block, block_loop }; +} + + +static void BuildLoopIndex(map& loopForIndex, LoopGraph* loop) { + string index = loop->loopSymbol; + loopForIndex[index] = loop; + for (const auto& childLoop : loop->children) { + BuildLoopIndex(loopForIndex, childLoop); + } +} + +static string FindIndexName(int pos, SAPFOR::BasicBlock* block, map& loopForIndex) { + unordered_set args = {block->getInstructions()[pos]->getInstruction()->getArg1()}; + + for (int i = pos-1; i >= 0; i--) { + SAPFOR::Argument* res = block->getInstructions()[i]->getInstruction()->getResult(); + if (res && args.find(res) != args.end()) { + SAPFOR::Argument* arg1 = block->getInstructions()[i]->getInstruction()->getArg1(); + SAPFOR::Argument* arg2 = block->getInstructions()[i]->getInstruction()->getArg2(); + if (arg1) { + string name = arg1->getValue(); + int idx = name.find('%'); + if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end()) + return name.substr(idx + 1); + else { + args.insert(arg1); + } + } + if (arg2) { + string name = arg2->getValue(); + int idx = name.find('%'); + if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end()) + return name.substr(idx + 1); + else { + args.insert(arg2); + } + } + } + } + return ""; +} + +static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAccessingIndexes& def, ArrayAccessingIndexes& use) { + auto instructions = block->getInstructions(); + map loopForIndex; + BuildLoopIndex(loopForIndex, loop); + for(int i = 0; i < instructions.size(); i++) + { + auto instruction = instructions[i]; + if(!instruction->getInstruction()->getArg1()) { + continue; + } + auto operation = instruction->getInstruction()->getOperation(); + auto type = instruction->getInstruction()->getArg1()->getType(); + if ((operation == SAPFOR::CFG_OP::STORE || operation == SAPFOR::CFG_OP::LOAD) && type == SAPFOR::CFG_ARG_TYPE::ARRAY) + { + vector index_vars; + vector refPos; + string array_name; + if (operation == SAPFOR::CFG_OP::STORE) + { + array_name = instruction->getInstruction()->getArg1()->getValue(); + } + else + { + array_name = instruction->getInstruction()->getArg2()->getValue(); + } + int j = i - 1; + while (j >= 0 && instructions[j]->getInstruction()->getOperation() == SAPFOR::CFG_OP::REF) + { + index_vars.push_back(instructions[j]->getInstruction()->getArg1()); + refPos.push_back(j); + j--; + } + /*to choose correct dimension*/ + int n = index_vars.size(); + vector accessPoint(n); + /*if (operation == SAPFOR::CFG_OP::STORE) + { + if (def[array_name].empty()) + { + def[array_name].resize(n); + } + } + else + { + if (use[array_name].empty()) + { + use[array_name].resize(n); + } + }*/ + + SgArrayRefExp* ref = (SgArrayRefExp*)instruction->getInstruction()->getExpression(); + vector> coefsForDims; + for (int i = 0; i < ref->numberOfSubscripts(); ++i) + { + const vector& coefs = getAttributes(ref->subscript(i), set{ INT_VAL }); + if (coefs.size() == 1) + { + const pair coef(coefs[0][0], coefs[0][1]); + coefsForDims.push_back(coef); + } + + } + + while (!index_vars.empty()) + { + auto var = index_vars.back(); + int currentVarPos = refPos.back(); + pair currentCoefs = coefsForDims.back(); + ArrayDimension current_dim; + if (var->getType() == SAPFOR::CFG_ARG_TYPE::CONST) { + current_dim = { stoul(var->getValue()), 1, 1 }; + } + else + { + string name, full_name = var->getValue(); + int pos = full_name.find('%'); + LoopGraph* currentLoop; + if (pos != -1) { + name = full_name.substr(pos+1); + if (loopForIndex.find(name) != loopForIndex.end()) { + currentLoop = loopForIndex[name]; + } + else { + return -1; + } + } + else { + name = FindIndexName(currentVarPos, block, loopForIndex); + if (name == "") { + return -1; + } + if (loopForIndex.find(name) != loopForIndex.end()) { + currentLoop = loopForIndex[name]; + } + else { + return -1; + } + } + uint64_t start = currentLoop->startVal * currentCoefs.first + currentCoefs.second; + uint64_t step = currentCoefs.first; + current_dim = { start, step, (uint64_t)currentLoop->calculatedCountOfIters }; + } + /*if (operation == SAPFOR::CFG_OP::STORE) + { + def[array_name][n - index_vars.size()].push_back(current_dim); + } + else + { + use[array_name][n - index_vars.size()].push_back(current_dim); + }*/ + accessPoint[n - index_vars.size()] = current_dim; + index_vars.pop_back(); + refPos.pop_back(); + coefsForDims.pop_back(); + } + if (operation == SAPFOR::CFG_OP::STORE) + { + def[array_name].Insert(accessPoint); + } + else + { + use[array_name].Insert(accessPoint); + } + } + } + return 0; + +} + +static vector FindParticularSolution(const ArrayDimension& dim1, const ArrayDimension& dim2) +{ + for (uint64_t i = 0; i < dim1.tripCount; i++) + { + uint64_t leftPart = dim1.start + i * dim1.step; + for (uint64_t j = 0; j < dim2.tripCount; j++) + { + uint64_t rightPart = dim2.start + j * dim2.step; + if (leftPart == rightPart) + { + return {i, j}; + } + } + } + return {}; +} + +/* dim1 /\ dim2 */ +static ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const ArrayDimension& dim2) +{ + vector partSolution = FindParticularSolution(dim1, dim2); + if (partSolution.empty()) + { + return NULL; + } + int64_t x0 = partSolution[0], y0 = partSolution[1]; + /* x = x_0 + c * t */ + /* y = y_0 + d * t */ + int64_t c = dim2.step / gcd(dim1.step, dim2.step); + int64_t d = dim1.step / gcd(dim1.step, dim2.step); + int64_t tXMin, tXMax, tYMin, tYMax; + tXMin = -x0 / c; + tXMax = (dim1.tripCount - 1 - x0) / c; + tYMin = -y0 / d; + tYMax = (dim2.tripCount - 1 - y0) / d; + int64_t tMin = max(tXMin, tYMin); + uint64_t tMax = min(tXMax, tYMax); + if (tMin > tMax) + { + return NULL; + } + uint64_t start3 = dim1.start + x0 * dim1.step; + uint64_t step3 = c * dim1.step; + ArrayDimension* result = new(ArrayDimension){ start3, step3, tMax + 1 }; + return result; +} + +/* dim1 / dim2 */ +static vector DimensionDifference(const ArrayDimension& dim1, const ArrayDimension& dim2) +{ + ArrayDimension* intersection = DimensionIntersection(dim1, dim2); + if (!intersection) + { + return {dim1}; + } + vector result; + /* add the part before intersection */ + if (dim1.start < intersection->start) + { + result.push_back({ dim1.start, dim1.step, (intersection->start - dim1.start) / dim1.step }); + } + /* add the parts between intersection steps */ + uint64_t start = (intersection->start - dim1.start) / dim1.step; + uint64_t interValue = intersection->start; + for (int64_t i = start; dim1.start + i * dim1.step <= intersection->start + intersection->step * (intersection->tripCount - 1); i++) + { + uint64_t centerValue = dim1.start + i * dim1.step; + if (centerValue == interValue) + { + if (i - start > 1) + { + result.push_back({ dim1.start + (start + 1) * dim1.step, dim1.step, i - start - 1 }); + start = i; + } + interValue += intersection->step; + } + } + /* add the part after intersection */ + if (intersection->start + intersection->step * (intersection->tripCount - 1) < dim1.start + dim1.step * (dim1.tripCount - 1)) + { + /* first value after intersection */ + uint64_t right_start = intersection->start + intersection->step * (intersection->tripCount - 1) + dim1.step; + uint64_t tripCount = (dim1.start + dim1.step * dim1.tripCount - right_start) / dim1.step; + result.push_back({right_start, dim1.step, tripCount}); + } + delete(intersection); + return result; +} + + +static vector DimensionUnion(const ArrayDimension& dim1, const ArrayDimension& dim2) +{ + vector res; + ArrayDimension* inter = DimensionIntersection(dim1, dim2); + if(!inter) + { + return { dim1, dim2 }; + } + res.push_back(*inter); + delete(inter); + vector diff1, diff2; + diff1 = DimensionDifference(dim1, dim2); + diff2 = DimensionDifference(dim2, dim1); + res.insert(res.end(), diff1.begin(), diff1.end()); + res.insert(res.end(), diff2.begin(), diff2.end()); + return res; +} + +static vector ElementsIntersection(const vector& firstElement, const vector& secondElement) +{ + if(firstElement.empty() || secondElement.empty()) { + return {}; + } + size_t dimAmount = firstElement.size(); + /* check if there is no intersecction */ + for(size_t i = 0; i < dimAmount; i++) + { + if(FindParticularSolution(firstElement[i], secondElement[i]).empty()){ + return {}; + } + } + vector result(dimAmount); + for(size_t i = 0; i < dimAmount; i++) + { + ArrayDimension* resPtr = DimensionIntersection(firstElement[i], secondElement[i]); + if(resPtr) + { + result[i] = *resPtr; + } + else + { + return {}; + } + } + return result; +} + +static vector> ElementsDifference(const vector& firstElement, + const vector& secondElement) +{ + if(firstElement.empty() || secondElement.empty()) { + return {}; + } + vector intersection = ElementsIntersection(firstElement, secondElement); + vector> result; + if(intersection.empty()) + { + return {firstElement}; + } + for(int i = 0; i < firstElement.size(); i++) + { + auto dimDiff = DimensionDifference(firstElement[i], secondElement[i]); + if(!dimDiff.empty()) + { + vector firstCopy = firstElement; + for(const auto range: dimDiff) + { + firstCopy[i] = range; + result.push_back(firstCopy); + } + } + } + return result; +} + +static void ElementsUnion(const vector& firstElement, const vector& secondElement, + vector>& lc, vector>& rc, + vector& intersection) +{ + /* lc(rc) is a set of ranges, which only exist in first(second) element*/ + intersection = ElementsIntersection(firstElement, secondElement); + lc = ElementsDifference(firstElement, intersection); + rc = ElementsDifference(secondElement, intersection); +} + +void AccessingSet::FindUncovered(const vector& element, vector>& result) const{ + vector> newTails; + result.push_back(element); + for(const auto& currentElement: allElements) + { + for(const auto& tailLoc: result) + { + auto intersection = ElementsIntersection(tailLoc, currentElement); + auto diff = ElementsDifference(tailLoc, intersection); + if(!diff.empty()) { + newTails.insert(newTails.end(), diff.begin(), diff.end()); + } + } + result = move(newTails); + } +} + +bool AccessingSet::ContainsElement(const vector& element) const +{ + vector> tails; + FindUncovered(element, tails); + return !tails.empty(); +} + +void AccessingSet::FindCoveredBy(const vector& element, vector>& result) const +{ + for(const auto& currentElement: allElements) + { + auto intersection = ElementsIntersection(element, currentElement); + if(!intersection.empty()) { + result.push_back(intersection); + } + } +} + +vector> AccessingSet::GetElements() const +{ + return allElements; +} + +void AccessingSet::Insert(const vector& element) +{ + vector> tails; + FindUncovered(element, tails); + allElements.insert(allElements.end(), tails.begin(), tails.end()); +} + +void AccessingSet::Union(const AccessingSet& source) { + for(auto& element: source.GetElements()) { + Insert(element); + } +} + +AccessingSet AccessingSet::Intersect(const AccessingSet& secondSet) const +{ + vector> result; + for(const auto& element: allElements) + { + if(secondSet.ContainsElement(element)) + { + result.push_back(element); + } + else + { + vector> coveredBy; + secondSet.FindCoveredBy(element, coveredBy); + if(!coveredBy.empty()) + { + result.insert(result.end(), coveredBy.begin(), coveredBy.end()); + } + } + } + return AccessingSet(result); +} + +AccessingSet AccessingSet::Diff(const AccessingSet& secondSet) const +{ + AccessingSet intersection = this->Intersect(secondSet); + AccessingSet uncovered = *this; + vector> result; + for (const auto& element : intersection.GetElements()) + { + vector> current_uncovered; + uncovered.FindUncovered(element, current_uncovered); + uncovered = AccessingSet(current_uncovered); + } + return uncovered; +} + +void Collapse(Region* region) +{ + Region* newBlock = new Region(); + for (auto& [arrayName, arrayRanges] : region->GetHeader()->array_out) + { + for (Region* byBlock : region->GetBasickBlocks()) + { + AccessingSet intersection = byBlock->array_def[arrayName].Intersect(arrayRanges); + newBlock->array_def[arrayName].Union(intersection); + } + } + + for (auto& byBlock : region->GetBasickBlocks()) { + for (auto& [arrayName, arrayRanges] : byBlock->array_use) + { + AccessingSet diff = byBlock->array_use[arrayName].Diff(byBlock->array_in[arrayName]); + newBlock->array_use[arrayName].Union(diff); + } + } + for (Region* prevRegion : region->getPrevRegions()) { + prevRegion->setNextRegion(newBlock); + } + region->getNextRegion()->setPrevRegion(newBlock); +} + +void FindPrivateArrays(map> &loopGraph, map>& FullIR) +{ + for (const auto& curr_graph_pair: loopGraph) + { + for (const auto& curr_loop : curr_graph_pair.second) + { + auto block_loop = GetBasicBlocksForLoop(curr_loop, (*FullIR.begin()).second); + for (const auto& bb : block_loop.second) { + ArrayAccessingIndexes def, use; + //GetDefUseArray(bb, curr_loop, def, use); + } + ArrayAccessingIndexes loopDimensionsInfo; + //GetDimensionInfo(curr_loop, loopDimensionsInfo, 0); + //print_info(curr_loop); + } + } + +} + +void GetDimensionInfo(LoopGraph* loop, map>>& loopDimensionsInfo, int level) +{ + cout << "line_num: " << loop->lineNum << endl; + for (const auto& writeOpPairs : loop->writeOpsForLoop) + { + vector> arrayDimensions(writeOpPairs.first->GetDimSize()); + loopDimensionsInfo[writeOpPairs.first] = arrayDimensions; + for (const auto& writeOp : writeOpPairs.second) + { + for (const auto& coeficient_pair : writeOp.coefficients) + { + uint64_t start, step, tripCount; + start = loop->startVal * coeficient_pair.first.first + coeficient_pair.first.second; + step = loop->stepVal * coeficient_pair.first.first; + tripCount = (loop->endVal - coeficient_pair.first.second) / step; + if (start <= loop->endVal) + { + loopDimensionsInfo[writeOpPairs.first][level].push_back({start, step, tripCount}); + cout << "level: " << level << endl; + cout << "start: " << start << endl; + cout << "step: " << step << endl; + cout << "trip_count: " << tripCount << endl; + cout << endl; + } + + + } + } + } + cout << "line_num_after: " << loop->lineNumAfterLoop << endl; + if (!loop->children.empty()) + { + for (const auto& childLoop : loop->children) + { + GetDimensionInfo(childLoop, loopDimensionsInfo, level+1); + } + } +} diff --git a/src/PrivateAnalyzer/private_arrays_search.h b/src/PrivateAnalyzer/private_arrays_search.h new file mode 100644 index 0000000..d146d52 --- /dev/null +++ b/src/PrivateAnalyzer/private_arrays_search.h @@ -0,0 +1,84 @@ +#pragma once + +#include "../GraphLoop/graph_loops.h" +#include "../CFGraph/CFGraph.h" + +using std::vector; +using std::map; +using std::string; +using std::set; + +struct ArrayDimension +{ + uint64_t start, step, tripCount; +}; + +class AccessingSet { + private: + vector> allElements; + + public: + AccessingSet(vector> input) : allElements(input) {}; + AccessingSet() {}; + vector> GetElements() const; + void Insert(const vector& element); + void Union(const AccessingSet& source); + AccessingSet Intersect(const AccessingSet& secondSet) const; + AccessingSet Diff(const AccessingSet& secondSet) const; + bool ContainsElement(const vector& element) const; + void FindCoveredBy(const vector& element, vector>& result) const; + void FindUncovered(const vector& element, vector>& result) const; +}; + +using ArrayAccessingIndexes = map; + +class Region: public SAPFOR::BasicBlock { + public: + Region() + { + header = nullptr; + nextRegion = nullptr; + } + Region(SAPFOR::BasicBlock block) : SAPFOR::BasicBlock::BasicBlock(block) + { + header = nullptr; + nextRegion = nullptr; + }; + //Region(LoopGraph* loop); + Region* GetHeader() + { + return header; + } + set GetBasickBlocks() + { + return basickBlocks; + } + vector getPrevRegions() + { + return prevRegions; + } + Region* getNextRegion() + { + return nextRegion; + } + void setPrevRegion(Region* region) + { + prevRegions.push_back(region); + } + void setNextRegion(Region* region) + { + nextRegion = region; + } + ArrayAccessingIndexes array_def, array_use, array_out, array_in; + + private: + set subRegions, basickBlocks; + Region* header; + Region* nextRegion; + vector prevRegions; +}; + +void Collapse(Region* region); +void FindPrivateArrays(map>& loopGraph, map>& FullIR); +void GetDimensionInfo(LoopGraph* loop, map>>& loopDimensionsInfo, int level); +set GetBasicBlocksForLoop(LoopGraph* loop, vector); From 54eb1ecc9535faa0bb68436bb7078d18a4adc56e Mon Sep 17 00:00:00 2001 From: Oleg Nikitin Date: Tue, 8 Apr 2025 15:25:39 +0300 Subject: [PATCH 05/15] add Region constructor and SolveDataflow function --- src/PrivateAnalyzer/private_arrays_search.cpp | 89 ++++++++++++++++--- src/PrivateAnalyzer/private_arrays_search.h | 78 +++++++++++----- 2 files changed, 136 insertions(+), 31 deletions(-) diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index 084cbcc..17bd679 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -44,7 +44,7 @@ static bool isParentStmt(SgStatement* stmt, SgStatement* parent) } /*returns head block and loop*/ -static pair> GetBasicBlocksForLoop(LoopGraph* loop, vector blocks) +pair> GetBasicBlocksForLoop(LoopGraph* loop, vector blocks) { unordered_set block_loop; SAPFOR::BasicBlock* head_block = nullptr; @@ -397,7 +397,7 @@ static vector> ElementsDifference(const vector firstCopy = firstElement; - for(const auto range: dimDiff) + for(const auto& range: dimDiff) { firstCopy[i] = range; result.push_back(firstCopy); @@ -508,27 +508,94 @@ AccessingSet AccessingSet::Diff(const AccessingSet& secondSet) const void Collapse(Region* region) { - Region* newBlock = new Region(); - for (auto& [arrayName, arrayRanges] : region->GetHeader()->array_out) + //Region* newBlock = new Region(); + for (auto& [arrayName, arrayRanges] : region->getHeader()->array_out) { - for (Region* byBlock : region->GetBasickBlocks()) + for (Region* byBlock : region->getBasickBlocks()) { AccessingSet intersection = byBlock->array_def[arrayName].Intersect(arrayRanges); - newBlock->array_def[arrayName].Union(intersection); + region->array_def[arrayName].Union(intersection); } } - for (auto& byBlock : region->GetBasickBlocks()) { + for (auto& byBlock : region->getBasickBlocks()) { for (auto& [arrayName, arrayRanges] : byBlock->array_use) { AccessingSet diff = byBlock->array_use[arrayName].Diff(byBlock->array_in[arrayName]); - newBlock->array_use[arrayName].Union(diff); + region->array_use[arrayName].Union(diff); } } - for (Region* prevRegion : region->getPrevRegions()) { - prevRegion->setNextRegion(newBlock); + + for (Region* prevBlock : region->getHeader()->getPrevRegions()) + { + prevBlock->replaceInNextRegions(region, region->getHeader()); } - region->getNextRegion()->setPrevRegion(newBlock); + for (Region* nextBlock : region->getHeader()->getNextRegions()) + { + nextBlock->replaceInPrevRegions(region, region->getHeader()); + } +} + +static void SetConnections(unordered_map& bbToRegion, const unordered_set& blockSet) +{ + for (SAPFOR::BasicBlock* block : blockSet) + { + for (SAPFOR::BasicBlock* nextBlock : block->getNext()) + { + if (bbToRegion.find(nextBlock) != bbToRegion.end()) + { + bbToRegion[block]->addNextRegion(bbToRegion[nextBlock]); + } + } + for (SAPFOR::BasicBlock* prevBlock : block->getPrev()) + { + if (bbToRegion.find(prevBlock) != bbToRegion.end()) + { + bbToRegion[block]->addPrevRegion(bbToRegion[prevBlock]); + } + } + } +} + +static Region* CreateSubRegion(LoopGraph* loop, const vector& Blocks, const unordered_map& bbToRegion) +{ + Region* region = new Region; + auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); + for (SAPFOR::BasicBlock* block : Blocks) + { + region->addBasickBlocks(bbToRegion.at(block)); + } + for (LoopGraph* childLoop : loop->children) + { + region->addSubRegions(CreateSubRegion(childLoop, Blocks, bbToRegion)); + } + return region; +} + +Region::Region(LoopGraph* loop, vector& Blocks) +{ + auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); + unordered_map bbToRegion; + for (auto poiner : blockSet) + { + bbToRegion[poiner] = new Region(*poiner); + } + SetConnections(bbToRegion, blockSet); + //create subRegions + for (LoopGraph* childLoop : loop->children) + { + subRegions.insert(CreateSubRegion(childLoop, Blocks, bbToRegion)); + } +} + +void SolveDataFlow(Region* DFG) +{ + //SolveDataFlowIteratively(DFG) + for (Region* subRegion : DFG->getSubRegions()) + { + SolveDataFlow(subRegion); + } + Collapse(DFG); } void FindPrivateArrays(map> &loopGraph, map>& FullIR) diff --git a/src/PrivateAnalyzer/private_arrays_search.h b/src/PrivateAnalyzer/private_arrays_search.h index d146d52..9a5aa49 100644 --- a/src/PrivateAnalyzer/private_arrays_search.h +++ b/src/PrivateAnalyzer/private_arrays_search.h @@ -7,6 +7,8 @@ using std::vector; using std::map; using std::string; using std::set; +using std::unordered_set; +using std::pair; struct ArrayDimension { @@ -36,49 +38,85 @@ class Region: public SAPFOR::BasicBlock { public: Region() { - header = nullptr; - nextRegion = nullptr; + header = nullptr; } + Region(SAPFOR::BasicBlock block) : SAPFOR::BasicBlock::BasicBlock(block) { header = nullptr; - nextRegion = nullptr; - }; - //Region(LoopGraph* loop); - Region* GetHeader() + } + + Region(LoopGraph* loop, vector& Blocks); + + Region* getHeader() { return header; } - set GetBasickBlocks() + + unordered_set& getBasickBlocks() { return basickBlocks; } - vector getPrevRegions() + + void addBasickBlocks(Region* region) + { + basickBlocks.insert(region); + } + unordered_set getPrevRegions() { return prevRegions; } - Region* getNextRegion() + + unordered_set getNextRegions() { - return nextRegion; + return nextRegions; } - void setPrevRegion(Region* region) + + void addPrevRegion(Region* region) { - prevRegions.push_back(region); + prevRegions.insert(region); } - void setNextRegion(Region* region) + + void addNextRegion(Region* region) { - nextRegion = region; + nextRegions.insert(region); } + + void replaceInPrevRegions(Region* source, Region* destination) + { + prevRegions.erase(destination); + prevRegions.insert(source); + } + + void replaceInNextRegions(Region* source, Region* destination) + { + nextRegions.erase(destination); + nextRegions.insert(source); + } + + unordered_set getSubRegions() + { + return subRegions; + } + + void addSubRegions(Region* region) + { + subRegions.insert(region); + } + ArrayAccessingIndexes array_def, array_use, array_out, array_in; - + private: - set subRegions, basickBlocks; - Region* header; - Region* nextRegion; - vector prevRegions; + unordered_set subRegions, basickBlocks; + /*next Region which is BB for current BB Region*/ + unordered_set nextRegions; + /*prev Regions which is BBs for current BB Region*/ + unordered_set prevRegions; + Region* header; }; + void Collapse(Region* region); void FindPrivateArrays(map>& loopGraph, map>& FullIR); void GetDimensionInfo(LoopGraph* loop, map>>& loopDimensionsInfo, int level); -set GetBasicBlocksForLoop(LoopGraph* loop, vector); +pair> GetBasicBlocksForLoop(LoopGraph* loop, vector blocks); From 0df1d3d5fe85873b96303eba2b7a2f5e9403bca4 Mon Sep 17 00:00:00 2001 From: Oleg Nikitin Date: Tue, 29 Apr 2025 17:55:51 +0300 Subject: [PATCH 06/15] add dataflow solvation --- src/PrivateAnalyzer/private_arrays_search.cpp | 67 +++++++++++++++++-- src/PrivateAnalyzer/private_arrays_search.h | 3 +- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index 17bd679..f8ab229 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -464,10 +464,12 @@ void AccessingSet::Insert(const vector& element) allElements.insert(allElements.end(), tails.begin(), tails.end()); } -void AccessingSet::Union(const AccessingSet& source) { +AccessingSet AccessingSet::Union(const AccessingSet& source) { + AccessingSet result; for(auto& element: source.GetElements()) { - Insert(element); + result.Insert(element); } + return result; } AccessingSet AccessingSet::Intersect(const AccessingSet& secondSet) const @@ -506,6 +508,27 @@ AccessingSet AccessingSet::Diff(const AccessingSet& secondSet) const return uncovered; } +bool operator==(const ArrayDimension& lhs, const ArrayDimension& rhs) +{ + return lhs.start == rhs.start && lhs.step == rhs.step && lhs.tripCount == rhs.tripCount; +} + + +bool operator==(const AccessingSet& lhs, const AccessingSet& rhs) +{ + for (size_t i = 0; i < lhs.allElements.size(); i++) + { + for (size_t j = 0; j < lhs.allElements[i].size(); j++) + { + if (lhs.allElements[i][j] != rhs.allElements[i][j]) + { + return false; + } + } + } + return true; +} + void Collapse(Region* region) { //Region* newBlock = new Region(); @@ -514,7 +537,7 @@ void Collapse(Region* region) for (Region* byBlock : region->getBasickBlocks()) { AccessingSet intersection = byBlock->array_def[arrayName].Intersect(arrayRanges); - region->array_def[arrayName].Union(intersection); + region->array_def[arrayName] = region->array_def[arrayName].Union(intersection); } } @@ -522,7 +545,7 @@ void Collapse(Region* region) for (auto& [arrayName, arrayRanges] : byBlock->array_use) { AccessingSet diff = byBlock->array_use[arrayName].Diff(byBlock->array_in[arrayName]); - region->array_use[arrayName].Union(diff); + region->array_use[arrayName] = region->array_use[arrayName].Union(diff); } } @@ -580,6 +603,7 @@ Region::Region(LoopGraph* loop, vector& Blocks) { bbToRegion[poiner] = new Region(*poiner); } + this->header = bbToRegion[header]; SetConnections(bbToRegion, blockSet); //create subRegions for (LoopGraph* childLoop : loop->children) @@ -588,9 +612,42 @@ Region::Region(LoopGraph* loop, vector& Blocks) } } +void SolveDataFlowIteratively(Region* DFG) +{ + unordered_set worklist(DFG->getBasickBlocks()); + do + { + Region* b = *worklist.begin(); + ArrayAccessingIndexes newIn; + for (Region* prevBlock : b->getPrevRegions()) + { + for (const auto& [arrayName, accessSet] : prevBlock->array_out) + { + newIn[arrayName] = newIn[arrayName].Intersect(accessSet); + } + } + b->array_in = newIn; + ArrayAccessingIndexes newOut; + for (auto& [arrayName, accessSet] : b->array_in) + { + newOut[arrayName] = b->array_in[arrayName].Union(b->array_def[arrayName]); + } + /* can not differ */ + if (newOut != b->array_out) + { + b->array_out = newOut; + } + else + { + worklist.erase(b); + } + } + while (!worklist.empty()); +} + void SolveDataFlow(Region* DFG) { - //SolveDataFlowIteratively(DFG) + SolveDataFlowIteratively(DFG); for (Region* subRegion : DFG->getSubRegions()) { SolveDataFlow(subRegion); diff --git a/src/PrivateAnalyzer/private_arrays_search.h b/src/PrivateAnalyzer/private_arrays_search.h index 9a5aa49..f8508f3 100644 --- a/src/PrivateAnalyzer/private_arrays_search.h +++ b/src/PrivateAnalyzer/private_arrays_search.h @@ -24,12 +24,13 @@ class AccessingSet { AccessingSet() {}; vector> GetElements() const; void Insert(const vector& element); - void Union(const AccessingSet& source); + AccessingSet Union(const AccessingSet& source); AccessingSet Intersect(const AccessingSet& secondSet) const; AccessingSet Diff(const AccessingSet& secondSet) const; bool ContainsElement(const vector& element) const; void FindCoveredBy(const vector& element, vector>& result) const; void FindUncovered(const vector& element, vector>& result) const; + friend bool operator==(const AccessingSet& lhs, const AccessingSet& rhs); }; using ArrayAccessingIndexes = map; From f679666d0168d731ff57bdbc0585520f1c628f21 Mon Sep 17 00:00:00 2001 From: "O. Nikitin" Date: Mon, 5 May 2025 21:53:34 +0300 Subject: [PATCH 07/15] fix operator!= --- src/PrivateAnalyzer/private_arrays_search.cpp | 26 +++++++++++++++---- src/PrivateAnalyzer/private_arrays_search.h | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index f8ab229..ce70854 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -508,13 +508,13 @@ AccessingSet AccessingSet::Diff(const AccessingSet& secondSet) const return uncovered; } -bool operator==(const ArrayDimension& lhs, const ArrayDimension& rhs) +bool operator!=(const ArrayDimension& lhs, const ArrayDimension& rhs) { - return lhs.start == rhs.start && lhs.step == rhs.step && lhs.tripCount == rhs.tripCount; + return !(lhs.start == rhs.start && lhs.step == rhs.step && lhs.tripCount == rhs.tripCount); } -bool operator==(const AccessingSet& lhs, const AccessingSet& rhs) +bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs) { for (size_t i = 0; i < lhs.allElements.size(); i++) { @@ -522,11 +522,27 @@ bool operator==(const AccessingSet& lhs, const AccessingSet& rhs) { if (lhs.allElements[i][j] != rhs.allElements[i][j]) { - return false; + return true; } } } - return true; + return false; +} + +bool operator!=(const ArrayAccessingIndexes& lhs, const ArrayAccessingIndexes& rhs) +{ + if(lhs.size() != rhs.size()) + { + return true; + } + for(auto& [key, value]: lhs) + { + if(rhs.find(key) == rhs.end()) + { + return true; + } + } + return false; } void Collapse(Region* region) diff --git a/src/PrivateAnalyzer/private_arrays_search.h b/src/PrivateAnalyzer/private_arrays_search.h index f8508f3..983f3dc 100644 --- a/src/PrivateAnalyzer/private_arrays_search.h +++ b/src/PrivateAnalyzer/private_arrays_search.h @@ -30,7 +30,7 @@ class AccessingSet { bool ContainsElement(const vector& element) const; void FindCoveredBy(const vector& element, vector>& result) const; void FindUncovered(const vector& element, vector>& result) const; - friend bool operator==(const AccessingSet& lhs, const AccessingSet& rhs); + friend bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs); }; using ArrayAccessingIndexes = map; From 1973d095f53fcb5b20ebf4f125408ca1d5f35194 Mon Sep 17 00:00:00 2001 From: Oleg Nikitin Date: Mon, 5 May 2025 22:49:53 +0300 Subject: [PATCH 08/15] made functions static --- projects/dvm | 2 +- src/PrivateAnalyzer/private_arrays_search.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/projects/dvm b/projects/dvm index a711f8e..ca44a63 160000 --- a/projects/dvm +++ b/projects/dvm @@ -1 +1 @@ -Subproject commit a711f8ebfd023ca90a6d9c9bdf5a4726cd276d51 +Subproject commit ca44a63d14c456d4deeea8b9a89125cb942f4550 diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index ce70854..97c29cc 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -508,13 +508,13 @@ AccessingSet AccessingSet::Diff(const AccessingSet& secondSet) const return uncovered; } -bool operator!=(const ArrayDimension& lhs, const ArrayDimension& rhs) +static bool operator!=(const ArrayDimension& lhs, const ArrayDimension& rhs) { return !(lhs.start == rhs.start && lhs.step == rhs.step && lhs.tripCount == rhs.tripCount); } -bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs) +static bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs) { for (size_t i = 0; i < lhs.allElements.size(); i++) { @@ -529,7 +529,7 @@ bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs) return false; } -bool operator!=(const ArrayAccessingIndexes& lhs, const ArrayAccessingIndexes& rhs) +static bool operator!=(const ArrayAccessingIndexes& lhs, const ArrayAccessingIndexes& rhs) { if(lhs.size() != rhs.size()) { @@ -628,7 +628,7 @@ Region::Region(LoopGraph* loop, vector& Blocks) } } -void SolveDataFlowIteratively(Region* DFG) +static void SolveDataFlowIteratively(Region* DFG) { unordered_set worklist(DFG->getBasickBlocks()); do @@ -661,7 +661,7 @@ void SolveDataFlowIteratively(Region* DFG) while (!worklist.empty()); } -void SolveDataFlow(Region* DFG) +static void SolveDataFlow(Region* DFG) { SolveDataFlowIteratively(DFG); for (Region* subRegion : DFG->getSubRegions()) From 73d0b201f2d1060820c66da64c5151c722ed8e14 Mon Sep 17 00:00:00 2001 From: Oleg Nikitin Date: Mon, 19 May 2025 20:50:35 +0300 Subject: [PATCH 09/15] change passes --- projects/dvm | 2 +- src/PrivateAnalyzer/private_arrays_search.cpp | 101 ++++++++++++++---- src/PrivateAnalyzer/private_arrays_search.h | 50 +++++---- src/Sapfor.cpp | 5 + src/Sapfor.h | 3 + src/Utils/PassManager.h | 2 + 6 files changed, 116 insertions(+), 47 deletions(-) diff --git a/projects/dvm b/projects/dvm index ca44a63..a711f8e 160000 --- a/projects/dvm +++ b/projects/dvm @@ -1 +1 @@ -Subproject commit ca44a63d14c456d4deeea8b9a89125cb942f4550 +Subproject commit a711f8ebfd023ca90a6d9c9bdf5a4726cd276d51 diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index 97c29cc..4461d94 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -44,7 +44,7 @@ static bool isParentStmt(SgStatement* stmt, SgStatement* parent) } /*returns head block and loop*/ -pair> GetBasicBlocksForLoop(LoopGraph* loop, vector blocks) +pair> GetBasicBlocksForLoop(const LoopGraph* loop, const vector blocks) { unordered_set block_loop; SAPFOR::BasicBlock* head_block = nullptr; @@ -165,7 +165,7 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces SgArrayRefExp* ref = (SgArrayRefExp*)instruction->getInstruction()->getExpression(); vector> coefsForDims; - for (int i = 0; i < ref->numberOfSubscripts(); ++i) + for (int i = 0; ref && i < ref->numberOfSubscripts(); ++i) { const vector& coefs = getAttributes(ref->subscript(i), set{ INT_VAL }); if (coefs.size() == 1) @@ -175,6 +175,7 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces } } + cout << coefsForDims.size() << endl; while (!index_vars.empty()) { @@ -547,7 +548,12 @@ static bool operator!=(const ArrayAccessingIndexes& lhs, const ArrayAccessingInd void Collapse(Region* region) { - //Region* newBlock = new Region(); + if (region->getBasickBlocks().empty()) + return; + else + { + cout << region->getBasickBlocks().size(); + } for (auto& [arrayName, arrayRanges] : region->getHeader()->array_out) { for (Region* byBlock : region->getBasickBlocks()) @@ -557,13 +563,26 @@ void Collapse(Region* region) } } - for (auto& byBlock : region->getBasickBlocks()) { + for (auto& byBlock : region->getBasickBlocks()) + { for (auto& [arrayName, arrayRanges] : byBlock->array_use) { AccessingSet diff = byBlock->array_use[arrayName].Diff(byBlock->array_in[arrayName]); region->array_use[arrayName] = region->array_use[arrayName].Union(diff); } } + ArrayAccessingIndexes useUnion; + for (auto& byBlock : region->getBasickBlocks()) + { + for (auto& [arrayName, arrayRanges] : byBlock->array_use) + { + useUnion[arrayName] = useUnion[arrayName].Union(byBlock->array_use[arrayName]); + } + } + for (auto& [arrayName, arrayRanges] : useUnion) + { + region->array_priv[arrayName] = useUnion[arrayName].Diff(region->array_use[arrayName]); + } for (Region* prevBlock : region->getHeader()->getPrevRegions()) { @@ -573,6 +592,11 @@ void Collapse(Region* region) { nextBlock->replaceInPrevRegions(region, region->getHeader()); } + for (Region* bb : region->getBasickBlocks()) + { + delete(bb); + } + cout << "Collapse\n"; } static void SetConnections(unordered_map& bbToRegion, const unordered_set& blockSet) @@ -602,7 +626,10 @@ static Region* CreateSubRegion(LoopGraph* loop, const vectoraddBasickBlocks(bbToRegion.at(block)); + if (bbToRegion.find(block) != bbToRegion.end()) + { + region->addBasickBlocks(bbToRegion.at(block)); + } } for (LoopGraph* childLoop : loop->children) { @@ -611,13 +638,17 @@ static Region* CreateSubRegion(LoopGraph* loop, const vector& Blocks) +Region::Region(LoopGraph* loop, const vector& Blocks) { auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); unordered_map bbToRegion; for (auto poiner : blockSet) { bbToRegion[poiner] = new Region(*poiner); + this->basickBlocks.insert(bbToRegion[poiner]); + ArrayAccessingIndexes def, use; + GetDefUseArray(poiner, loop, bbToRegion[poiner]->array_def, bbToRegion[poiner]->array_use); + } this->header = bbToRegion[header]; SetConnections(bbToRegion, blockSet); @@ -639,14 +670,28 @@ static void SolveDataFlowIteratively(Region* DFG) { for (const auto& [arrayName, accessSet] : prevBlock->array_out) { - newIn[arrayName] = newIn[arrayName].Intersect(accessSet); + if (newIn.find(arrayName) != newIn.end()) + { + newIn[arrayName] = newIn[arrayName].Intersect(accessSet); + } + else + { + newIn[arrayName] = accessSet; + } } } b->array_in = newIn; ArrayAccessingIndexes newOut; - for (auto& [arrayName, accessSet] : b->array_in) + for (auto& [arrayName, accessSet] : b->array_def) { - newOut[arrayName] = b->array_in[arrayName].Union(b->array_def[arrayName]); + if (newOut.find(arrayName) != newOut.end()) + { + newOut[arrayName] = b->array_def[arrayName].Union(b->array_in[arrayName]); + } + else + { + newOut[arrayName] = accessSet; + } } /* can not differ */ if (newOut != b->array_out) @@ -659,6 +704,7 @@ static void SolveDataFlowIteratively(Region* DFG) } } while (!worklist.empty()); + cout << "solveDFIt\n"; } static void SolveDataFlow(Region* DFG) @@ -669,25 +715,40 @@ static void SolveDataFlow(Region* DFG) SolveDataFlow(subRegion); } Collapse(DFG); + cout << "SolveDF\n"; } -void FindPrivateArrays(map> &loopGraph, map>& FullIR) +map FindPrivateArrays(map> &loopGraph, map>& FullIR) { - for (const auto& curr_graph_pair: loopGraph) + map result; + for (const auto& [loopName, loops] : loopGraph) { - for (const auto& curr_loop : curr_graph_pair.second) + for (const auto& loop : loops) { - auto block_loop = GetBasicBlocksForLoop(curr_loop, (*FullIR.begin()).second); - for (const auto& bb : block_loop.second) { - ArrayAccessingIndexes def, use; - //GetDefUseArray(bb, curr_loop, def, use); + for (const auto& [funcInfo, blocks]: FullIR) + { + // + for (auto& bb : blocks) + { + ArrayAccessingIndexes def, use; + if (bb->getNumber() == 4) + { + GetDefUseArray(bb, loop, def, use); + return {}; + } + } + // + Region* loopRegion = new Region(loop, blocks); + SolveDataFlow(loopRegion); + result[loop] = loopRegion->array_priv; + delete(loopRegion); } - ArrayAccessingIndexes loopDimensionsInfo; - //GetDimensionInfo(curr_loop, loopDimensionsInfo, 0); - //print_info(curr_loop); } } - + vector A = { {1, 1, 2}, {0, 1, 6} }; + vector B = { {0, 1, 6}, {2, 1, 2} }; + vector C = { {1, 1, 2}, {2, 1, 2} }; + return result; } void GetDimensionInfo(LoopGraph* loop, map>>& loopDimensionsInfo, int level) diff --git a/src/PrivateAnalyzer/private_arrays_search.h b/src/PrivateAnalyzer/private_arrays_search.h index 983f3dc..cc26f78 100644 --- a/src/PrivateAnalyzer/private_arrays_search.h +++ b/src/PrivateAnalyzer/private_arrays_search.h @@ -1,14 +1,12 @@ #pragma once +#include +#include +#include + #include "../GraphLoop/graph_loops.h" #include "../CFGraph/CFGraph.h" -using std::vector; -using std::map; -using std::string; -using std::set; -using std::unordered_set; -using std::pair; struct ArrayDimension { @@ -17,23 +15,23 @@ struct ArrayDimension class AccessingSet { private: - vector> allElements; + std::vector> allElements; public: - AccessingSet(vector> input) : allElements(input) {}; + AccessingSet(std::vector> input) : allElements(input) {}; AccessingSet() {}; - vector> GetElements() const; - void Insert(const vector& element); + std::vector> GetElements() const; + void Insert(const std::vector& element); AccessingSet Union(const AccessingSet& source); AccessingSet Intersect(const AccessingSet& secondSet) const; AccessingSet Diff(const AccessingSet& secondSet) const; - bool ContainsElement(const vector& element) const; - void FindCoveredBy(const vector& element, vector>& result) const; - void FindUncovered(const vector& element, vector>& result) const; + bool ContainsElement(const std::vector& element) const; + void FindCoveredBy(const std::vector& element, std::vector>& result) const; + void FindUncovered(const std::vector& element, std::vector>& result) const; friend bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs); }; -using ArrayAccessingIndexes = map; +using ArrayAccessingIndexes = std::map; class Region: public SAPFOR::BasicBlock { public: @@ -47,14 +45,14 @@ class Region: public SAPFOR::BasicBlock { header = nullptr; } - Region(LoopGraph* loop, vector& Blocks); + Region(LoopGraph* loop, const std::vector& Blocks); Region* getHeader() { return header; } - unordered_set& getBasickBlocks() + std::unordered_set& getBasickBlocks() { return basickBlocks; } @@ -63,12 +61,12 @@ class Region: public SAPFOR::BasicBlock { { basickBlocks.insert(region); } - unordered_set getPrevRegions() + std::unordered_set getPrevRegions() { return prevRegions; } - unordered_set getNextRegions() + std::unordered_set getNextRegions() { return nextRegions; } @@ -95,7 +93,7 @@ class Region: public SAPFOR::BasicBlock { nextRegions.insert(source); } - unordered_set getSubRegions() + std::unordered_set getSubRegions() { return subRegions; } @@ -105,19 +103,19 @@ class Region: public SAPFOR::BasicBlock { subRegions.insert(region); } - ArrayAccessingIndexes array_def, array_use, array_out, array_in; + ArrayAccessingIndexes array_def, array_use, array_out, array_in, array_priv; private: - unordered_set subRegions, basickBlocks; + std::unordered_set subRegions, basickBlocks; /*next Region which is BB for current BB Region*/ - unordered_set nextRegions; + std::unordered_set nextRegions; /*prev Regions which is BBs for current BB Region*/ - unordered_set prevRegions; + std::unordered_set prevRegions; Region* header; }; void Collapse(Region* region); -void FindPrivateArrays(map>& loopGraph, map>& FullIR); -void GetDimensionInfo(LoopGraph* loop, map>>& loopDimensionsInfo, int level); -pair> GetBasicBlocksForLoop(LoopGraph* loop, vector blocks); +std::map FindPrivateArrays(std::map>& loopGraph, std::map>& FullIR); +void GetDimensionInfo(LoopGraph* loop, std::map>>& loopDimensionsInfo, int level); +std::pair> GetBasicBlocksForLoop(const LoopGraph* loop, const std::vector blocks); diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index b7e80b8..c106f6a 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -55,6 +55,7 @@ #include "VerificationCode/verifications.h" #include "Distribution/CreateDistributionDirs.h" #include "PrivateAnalyzer/private_analyzer.h" +#include "PrivateAnalyzer/private_arrays_search.h" #include "ExpressionTransform/expr_transform.h" #include "Predictor/PredictScheme.h" @@ -1025,6 +1026,10 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne if(func->funcPointer->variant() != ENTRY_STAT) countOfTransform += removeDeadCode(func->funcPointer, allFuncInfo, commonBlocks); } + else if (curr_regime == FIND_PRIVATE_ARRAYS) + { + FindPrivateArrays(loopGraph, fullIR); + } else if (curr_regime == TEST_PASS) { //test pass diff --git a/src/Sapfor.h b/src/Sapfor.h index b7f01d1..2ed5a83 100644 --- a/src/Sapfor.h +++ b/src/Sapfor.h @@ -183,6 +183,8 @@ enum passes { SET_IMPLICIT_NONE, RENAME_INLCUDES, + FIND_PRIVATE_ARRAYS, + TEST_PASS, EMPTY_PASS }; @@ -367,6 +369,7 @@ static void setPassValues() passNames[SET_IMPLICIT_NONE] = "SET_IMPLICIT_NONE"; passNames[RENAME_INLCUDES] = "RENAME_INLCUDES"; passNames[INSERT_NO_DISTR_FLAGS_FROM_GUI] = "INSERT_NO_DISTR_FLAGS_FROM_GUI"; + passNames[FIND_PRIVATE_ARRAYS] = "FIND_PRIVATE_ARRAYS"; passNames[TEST_PASS] = "TEST_PASS"; } diff --git a/src/Utils/PassManager.h b/src/Utils/PassManager.h index 78f4170..dad0a52 100644 --- a/src/Utils/PassManager.h +++ b/src/Utils/PassManager.h @@ -316,6 +316,8 @@ void InitPassesDependencies(map> &passDepsIn, set list({ VERIFY_INCLUDES, CORRECT_VAR_DECL }) <= Pass(SET_IMPLICIT_NONE); + list({ CALL_GRAPH2, CALL_GRAPH, BUILD_IR, LOOP_GRAPH }) <= Pass(FIND_PRIVATE_ARRAYS); + passesIgnoreStateDone.insert({ CREATE_PARALLEL_DIRS, INSERT_PARALLEL_DIRS, INSERT_SHADOW_DIRS, EXTRACT_PARALLEL_DIRS, EXTRACT_SHADOW_DIRS, CREATE_REMOTES, UNPARSE_FILE, REMOVE_AND_CALC_SHADOW, REVERSE_CREATED_NESTED_LOOPS, PREDICT_SCHEME, CALCULATE_STATS_SCHEME, REVERT_SPF_DIRS, CLEAR_SPF_DIRS, TRANSFORM_SHADOW_IF_FULL, From 11e9fab482799ac5fe830e7b88068be217a49789 Mon Sep 17 00:00:00 2001 From: Oleg Nikitin Date: Tue, 27 May 2025 02:25:39 +0300 Subject: [PATCH 10/15] change file structure --- CMakeLists.txt | 6 +- src/CFGraph/IR.cpp | 12 +- src/PrivateAnalyzer/private_arrays_search.cpp | 725 ++---------------- src/PrivateAnalyzer/private_arrays_search.h | 111 +-- src/PrivateAnalyzer/range_structures.cpp | 317 ++++++++ src/PrivateAnalyzer/range_structures.h | 36 + src/PrivateAnalyzer/region.cpp | 268 +++++++ src/PrivateAnalyzer/region.h | 60 ++ src/Utils/PassManager.h | 2 +- 9 files changed, 744 insertions(+), 793 deletions(-) create mode 100644 src/PrivateAnalyzer/range_structures.cpp create mode 100644 src/PrivateAnalyzer/range_structures.h create mode 100644 src/PrivateAnalyzer/region.cpp create mode 100644 src/PrivateAnalyzer/region.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b0f054..145d672 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -115,7 +115,11 @@ set(OMEGA src/SageAnalysisTool/OmegaForSage/add-assert.cpp set(PRIV src/PrivateAnalyzer/private_analyzer.cpp src/PrivateAnalyzer/private_analyzer.h src/PrivateAnalyzer/private_arrays_search.cpp - src/PrivateAnalyzer/private_arrays_search.h) + src/PrivateAnalyzer/private_arrays_search.h + src/PrivateAnalyzer/range_structures.cpp + src/PrivateAnalyzer/range_structures.h + src/PrivateAnalyzer/region.cpp + src/PrivateAnalyzer/region.h) set(FDVM ${fdvm_sources}/acc.cpp ${fdvm_sources}/acc_across.cpp diff --git a/src/CFGraph/IR.cpp b/src/CFGraph/IR.cpp index 5c8bd0c..6ead0e4 100644 --- a/src/CFGraph/IR.cpp +++ b/src/CFGraph/IR.cpp @@ -395,7 +395,7 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector& if (ex) { const int var = ex->variant(); - if ((var == VAR_REF || var == CONST_REF || var == LABEL_REF) && !ex->lhs() && !ex->rhs()) // + if ((var == VAR_REF || var == CONST_REF || var == LABEL_REF) && !ex->lhs() && !ex->rhs()) // ��������� � ���������� { if (var == CONST_REF) { @@ -450,7 +450,7 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector& return arg1; auto reg = isLeft ? NULL : createRegister(); - Instruction* instr = new Instruction(isLeft ? CFG_OP::STORE : CFG_OP::LOAD, arg1, createConstArg(numArgs), isLeft ? isLeft : reg); + Instruction* instr = new Instruction(isLeft ? CFG_OP::STORE : CFG_OP::LOAD, arg1, createConstArg(numArgs), isLeft ? isLeft : reg, NULL, ex); blocks.push_back(new IR_Block(instr)); return reg; } @@ -485,7 +485,7 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector& auto arg1 = arrayRef ? arrayRef : createArrayArg(ref, blocks, func, numArgs, commonVars); auto reg = isLeft ? NULL : createRegister(); - instr = new Instruction(isLeft ? CFG_OP::STORE : CFG_OP::LOAD, arg1, createConstArg(1), isLeft ? isLeft : reg); + instr = new Instruction(isLeft ? CFG_OP::STORE : CFG_OP::LOAD, arg1, createConstArg(1), isLeft ? isLeft : reg, NULL, ex); blocks.push_back(new IR_Block(instr)); return reg; } @@ -602,7 +602,7 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector& { if (returnReg == NULL) { - Instruction* instr = new Instruction(CFG_OP::LOAD, arg, NULL, reg); + Instruction* instr = new Instruction(CFG_OP::LOAD, arg, NULL, reg, NULL, ex); blocks.push_back(new IR_Block(instr)); } else @@ -1572,7 +1572,7 @@ vector buildIR(SgStatement* function, const FuncInfo* func, const vec else findReturn(0, blocks.size(), blocks, blocks.back()->getNumber()); - // GOTO + // ���������� ������ �� GOTO � ��������� for (int z = 0; z < blocks.size(); ++z) { auto op = blocks[z]->getInstruction()->getOperation(); @@ -1592,7 +1592,7 @@ vector buildIR(SgStatement* function, const FuncInfo* func, const vec blocks[z]->setJump(it->second); - // + // ������� ����� �� ����� ���������� arg->setValue(to_string(it->second->getNumber())); arg->setType(CFG_ARG_TYPE::INSTR); } diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index 4461d94..c603178 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -7,553 +7,19 @@ #include #include "private_arrays_search.h" +#include "range_structures.h" +#include "region.h" #include "../Utils/SgUtils.h" #include "../GraphLoop/graph_loops.h" #include "../CFGraph/CFGraph.h" using namespace std; -void print_info(LoopGraph* loop) -{ - cout << "loopSymbol: " << loop->loopSymbol << endl; - for (const auto& ops : loop->writeOpsForLoop) - { - cout << "Array name: " << ops.first->GetShortName() << endl; - for (const auto i : ops.second) - { - i.printInfo(); - } - } - if (!loop->children.empty()) - { - for (const auto child : loop->children) - { - print_info(child); - } - } -} - -static bool isParentStmt(SgStatement* stmt, SgStatement* parent) -{ - for (; stmt; stmt = stmt->controlParent()) - if (stmt == parent) - { - return true; - } - return false; -} - -/*returns head block and loop*/ -pair> GetBasicBlocksForLoop(const LoopGraph* loop, const vector blocks) -{ - unordered_set block_loop; - SAPFOR::BasicBlock* head_block = nullptr; - auto loop_operator = loop->loop->GetOriginal(); - for (const auto& block : blocks) - { - if (!block || (block->getInstructions().size() == 0)) - { - continue; - } - SgStatement* first = block->getInstructions().front()->getInstruction()->getOperator(); - SgStatement* last = block->getInstructions().back()->getInstruction()->getOperator(); - if (isParentStmt(first, loop_operator) && isParentStmt(last, loop_operator)) - { - block_loop.insert(block); - - if ((!head_block) && (first == loop_operator) && (last == loop_operator) && - (block->getInstructions().size() == 2) && - (block->getInstructions().back()->getInstruction()->getOperation() == SAPFOR::CFG_OP::JUMP_IF)) - { - head_block = block; - } - - } - } - return { head_block, block_loop }; -} - - -static void BuildLoopIndex(map& loopForIndex, LoopGraph* loop) { - string index = loop->loopSymbol; - loopForIndex[index] = loop; - for (const auto& childLoop : loop->children) { - BuildLoopIndex(loopForIndex, childLoop); - } -} - -static string FindIndexName(int pos, SAPFOR::BasicBlock* block, map& loopForIndex) { - unordered_set args = {block->getInstructions()[pos]->getInstruction()->getArg1()}; - - for (int i = pos-1; i >= 0; i--) { - SAPFOR::Argument* res = block->getInstructions()[i]->getInstruction()->getResult(); - if (res && args.find(res) != args.end()) { - SAPFOR::Argument* arg1 = block->getInstructions()[i]->getInstruction()->getArg1(); - SAPFOR::Argument* arg2 = block->getInstructions()[i]->getInstruction()->getArg2(); - if (arg1) { - string name = arg1->getValue(); - int idx = name.find('%'); - if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end()) - return name.substr(idx + 1); - else { - args.insert(arg1); - } - } - if (arg2) { - string name = arg2->getValue(); - int idx = name.find('%'); - if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end()) - return name.substr(idx + 1); - else { - args.insert(arg2); - } - } - } - } - return ""; -} - -static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAccessingIndexes& def, ArrayAccessingIndexes& use) { - auto instructions = block->getInstructions(); - map loopForIndex; - BuildLoopIndex(loopForIndex, loop); - for(int i = 0; i < instructions.size(); i++) - { - auto instruction = instructions[i]; - if(!instruction->getInstruction()->getArg1()) { - continue; - } - auto operation = instruction->getInstruction()->getOperation(); - auto type = instruction->getInstruction()->getArg1()->getType(); - if ((operation == SAPFOR::CFG_OP::STORE || operation == SAPFOR::CFG_OP::LOAD) && type == SAPFOR::CFG_ARG_TYPE::ARRAY) - { - vector index_vars; - vector refPos; - string array_name; - if (operation == SAPFOR::CFG_OP::STORE) - { - array_name = instruction->getInstruction()->getArg1()->getValue(); - } - else - { - array_name = instruction->getInstruction()->getArg2()->getValue(); - } - int j = i - 1; - while (j >= 0 && instructions[j]->getInstruction()->getOperation() == SAPFOR::CFG_OP::REF) - { - index_vars.push_back(instructions[j]->getInstruction()->getArg1()); - refPos.push_back(j); - j--; - } - /*to choose correct dimension*/ - int n = index_vars.size(); - vector accessPoint(n); - /*if (operation == SAPFOR::CFG_OP::STORE) - { - if (def[array_name].empty()) - { - def[array_name].resize(n); - } - } - else - { - if (use[array_name].empty()) - { - use[array_name].resize(n); - } - }*/ - - SgArrayRefExp* ref = (SgArrayRefExp*)instruction->getInstruction()->getExpression(); - vector> coefsForDims; - for (int i = 0; ref && i < ref->numberOfSubscripts(); ++i) - { - const vector& coefs = getAttributes(ref->subscript(i), set{ INT_VAL }); - if (coefs.size() == 1) - { - const pair coef(coefs[0][0], coefs[0][1]); - coefsForDims.push_back(coef); - } - - } - cout << coefsForDims.size() << endl; - - while (!index_vars.empty()) - { - auto var = index_vars.back(); - int currentVarPos = refPos.back(); - pair currentCoefs = coefsForDims.back(); - ArrayDimension current_dim; - if (var->getType() == SAPFOR::CFG_ARG_TYPE::CONST) { - current_dim = { stoul(var->getValue()), 1, 1 }; - } - else - { - string name, full_name = var->getValue(); - int pos = full_name.find('%'); - LoopGraph* currentLoop; - if (pos != -1) { - name = full_name.substr(pos+1); - if (loopForIndex.find(name) != loopForIndex.end()) { - currentLoop = loopForIndex[name]; - } - else { - return -1; - } - } - else { - name = FindIndexName(currentVarPos, block, loopForIndex); - if (name == "") { - return -1; - } - if (loopForIndex.find(name) != loopForIndex.end()) { - currentLoop = loopForIndex[name]; - } - else { - return -1; - } - } - uint64_t start = currentLoop->startVal * currentCoefs.first + currentCoefs.second; - uint64_t step = currentCoefs.first; - current_dim = { start, step, (uint64_t)currentLoop->calculatedCountOfIters }; - } - /*if (operation == SAPFOR::CFG_OP::STORE) - { - def[array_name][n - index_vars.size()].push_back(current_dim); - } - else - { - use[array_name][n - index_vars.size()].push_back(current_dim); - }*/ - accessPoint[n - index_vars.size()] = current_dim; - index_vars.pop_back(); - refPos.pop_back(); - coefsForDims.pop_back(); - } - if (operation == SAPFOR::CFG_OP::STORE) - { - def[array_name].Insert(accessPoint); - } - else - { - use[array_name].Insert(accessPoint); - } - } - } - return 0; - -} - -static vector FindParticularSolution(const ArrayDimension& dim1, const ArrayDimension& dim2) -{ - for (uint64_t i = 0; i < dim1.tripCount; i++) - { - uint64_t leftPart = dim1.start + i * dim1.step; - for (uint64_t j = 0; j < dim2.tripCount; j++) - { - uint64_t rightPart = dim2.start + j * dim2.step; - if (leftPart == rightPart) - { - return {i, j}; - } - } - } - return {}; -} - -/* dim1 /\ dim2 */ -static ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const ArrayDimension& dim2) -{ - vector partSolution = FindParticularSolution(dim1, dim2); - if (partSolution.empty()) - { - return NULL; - } - int64_t x0 = partSolution[0], y0 = partSolution[1]; - /* x = x_0 + c * t */ - /* y = y_0 + d * t */ - int64_t c = dim2.step / gcd(dim1.step, dim2.step); - int64_t d = dim1.step / gcd(dim1.step, dim2.step); - int64_t tXMin, tXMax, tYMin, tYMax; - tXMin = -x0 / c; - tXMax = (dim1.tripCount - 1 - x0) / c; - tYMin = -y0 / d; - tYMax = (dim2.tripCount - 1 - y0) / d; - int64_t tMin = max(tXMin, tYMin); - uint64_t tMax = min(tXMax, tYMax); - if (tMin > tMax) - { - return NULL; - } - uint64_t start3 = dim1.start + x0 * dim1.step; - uint64_t step3 = c * dim1.step; - ArrayDimension* result = new(ArrayDimension){ start3, step3, tMax + 1 }; - return result; -} - -/* dim1 / dim2 */ -static vector DimensionDifference(const ArrayDimension& dim1, const ArrayDimension& dim2) -{ - ArrayDimension* intersection = DimensionIntersection(dim1, dim2); - if (!intersection) - { - return {dim1}; - } - vector result; - /* add the part before intersection */ - if (dim1.start < intersection->start) - { - result.push_back({ dim1.start, dim1.step, (intersection->start - dim1.start) / dim1.step }); - } - /* add the parts between intersection steps */ - uint64_t start = (intersection->start - dim1.start) / dim1.step; - uint64_t interValue = intersection->start; - for (int64_t i = start; dim1.start + i * dim1.step <= intersection->start + intersection->step * (intersection->tripCount - 1); i++) - { - uint64_t centerValue = dim1.start + i * dim1.step; - if (centerValue == interValue) - { - if (i - start > 1) - { - result.push_back({ dim1.start + (start + 1) * dim1.step, dim1.step, i - start - 1 }); - start = i; - } - interValue += intersection->step; - } - } - /* add the part after intersection */ - if (intersection->start + intersection->step * (intersection->tripCount - 1) < dim1.start + dim1.step * (dim1.tripCount - 1)) - { - /* first value after intersection */ - uint64_t right_start = intersection->start + intersection->step * (intersection->tripCount - 1) + dim1.step; - uint64_t tripCount = (dim1.start + dim1.step * dim1.tripCount - right_start) / dim1.step; - result.push_back({right_start, dim1.step, tripCount}); - } - delete(intersection); - return result; -} - - -static vector DimensionUnion(const ArrayDimension& dim1, const ArrayDimension& dim2) -{ - vector res; - ArrayDimension* inter = DimensionIntersection(dim1, dim2); - if(!inter) - { - return { dim1, dim2 }; - } - res.push_back(*inter); - delete(inter); - vector diff1, diff2; - diff1 = DimensionDifference(dim1, dim2); - diff2 = DimensionDifference(dim2, dim1); - res.insert(res.end(), diff1.begin(), diff1.end()); - res.insert(res.end(), diff2.begin(), diff2.end()); - return res; -} - -static vector ElementsIntersection(const vector& firstElement, const vector& secondElement) -{ - if(firstElement.empty() || secondElement.empty()) { - return {}; - } - size_t dimAmount = firstElement.size(); - /* check if there is no intersecction */ - for(size_t i = 0; i < dimAmount; i++) - { - if(FindParticularSolution(firstElement[i], secondElement[i]).empty()){ - return {}; - } - } - vector result(dimAmount); - for(size_t i = 0; i < dimAmount; i++) - { - ArrayDimension* resPtr = DimensionIntersection(firstElement[i], secondElement[i]); - if(resPtr) - { - result[i] = *resPtr; - } - else - { - return {}; - } - } - return result; -} - -static vector> ElementsDifference(const vector& firstElement, - const vector& secondElement) -{ - if(firstElement.empty() || secondElement.empty()) { - return {}; - } - vector intersection = ElementsIntersection(firstElement, secondElement); - vector> result; - if(intersection.empty()) - { - return {firstElement}; - } - for(int i = 0; i < firstElement.size(); i++) - { - auto dimDiff = DimensionDifference(firstElement[i], secondElement[i]); - if(!dimDiff.empty()) - { - vector firstCopy = firstElement; - for(const auto& range: dimDiff) - { - firstCopy[i] = range; - result.push_back(firstCopy); - } - } - } - return result; -} - -static void ElementsUnion(const vector& firstElement, const vector& secondElement, - vector>& lc, vector>& rc, - vector& intersection) -{ - /* lc(rc) is a set of ranges, which only exist in first(second) element*/ - intersection = ElementsIntersection(firstElement, secondElement); - lc = ElementsDifference(firstElement, intersection); - rc = ElementsDifference(secondElement, intersection); -} - -void AccessingSet::FindUncovered(const vector& element, vector>& result) const{ - vector> newTails; - result.push_back(element); - for(const auto& currentElement: allElements) - { - for(const auto& tailLoc: result) - { - auto intersection = ElementsIntersection(tailLoc, currentElement); - auto diff = ElementsDifference(tailLoc, intersection); - if(!diff.empty()) { - newTails.insert(newTails.end(), diff.begin(), diff.end()); - } - } - result = move(newTails); - } -} - -bool AccessingSet::ContainsElement(const vector& element) const -{ - vector> tails; - FindUncovered(element, tails); - return !tails.empty(); -} - -void AccessingSet::FindCoveredBy(const vector& element, vector>& result) const -{ - for(const auto& currentElement: allElements) - { - auto intersection = ElementsIntersection(element, currentElement); - if(!intersection.empty()) { - result.push_back(intersection); - } - } -} - -vector> AccessingSet::GetElements() const -{ - return allElements; -} - -void AccessingSet::Insert(const vector& element) -{ - vector> tails; - FindUncovered(element, tails); - allElements.insert(allElements.end(), tails.begin(), tails.end()); -} - -AccessingSet AccessingSet::Union(const AccessingSet& source) { - AccessingSet result; - for(auto& element: source.GetElements()) { - result.Insert(element); - } - return result; -} - -AccessingSet AccessingSet::Intersect(const AccessingSet& secondSet) const -{ - vector> result; - for(const auto& element: allElements) - { - if(secondSet.ContainsElement(element)) - { - result.push_back(element); - } - else - { - vector> coveredBy; - secondSet.FindCoveredBy(element, coveredBy); - if(!coveredBy.empty()) - { - result.insert(result.end(), coveredBy.begin(), coveredBy.end()); - } - } - } - return AccessingSet(result); -} - -AccessingSet AccessingSet::Diff(const AccessingSet& secondSet) const -{ - AccessingSet intersection = this->Intersect(secondSet); - AccessingSet uncovered = *this; - vector> result; - for (const auto& element : intersection.GetElements()) - { - vector> current_uncovered; - uncovered.FindUncovered(element, current_uncovered); - uncovered = AccessingSet(current_uncovered); - } - return uncovered; -} - -static bool operator!=(const ArrayDimension& lhs, const ArrayDimension& rhs) -{ - return !(lhs.start == rhs.start && lhs.step == rhs.step && lhs.tripCount == rhs.tripCount); -} - - -static bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs) -{ - for (size_t i = 0; i < lhs.allElements.size(); i++) - { - for (size_t j = 0; j < lhs.allElements[i].size(); j++) - { - if (lhs.allElements[i][j] != rhs.allElements[i][j]) - { - return true; - } - } - } - return false; -} - -static bool operator!=(const ArrayAccessingIndexes& lhs, const ArrayAccessingIndexes& rhs) -{ - if(lhs.size() != rhs.size()) - { - return true; - } - for(auto& [key, value]: lhs) - { - if(rhs.find(key) == rhs.end()) - { - return true; - } - } - return false; -} - void Collapse(Region* region) { if (region->getBasickBlocks().empty()) return; - else - { - cout << region->getBasickBlocks().size(); - } + for (auto& [arrayName, arrayRanges] : region->getHeader()->array_out) { for (Region* byBlock : region->getBasickBlocks()) @@ -592,71 +58,6 @@ void Collapse(Region* region) { nextBlock->replaceInPrevRegions(region, region->getHeader()); } - for (Region* bb : region->getBasickBlocks()) - { - delete(bb); - } - cout << "Collapse\n"; -} - -static void SetConnections(unordered_map& bbToRegion, const unordered_set& blockSet) -{ - for (SAPFOR::BasicBlock* block : blockSet) - { - for (SAPFOR::BasicBlock* nextBlock : block->getNext()) - { - if (bbToRegion.find(nextBlock) != bbToRegion.end()) - { - bbToRegion[block]->addNextRegion(bbToRegion[nextBlock]); - } - } - for (SAPFOR::BasicBlock* prevBlock : block->getPrev()) - { - if (bbToRegion.find(prevBlock) != bbToRegion.end()) - { - bbToRegion[block]->addPrevRegion(bbToRegion[prevBlock]); - } - } - } -} - -static Region* CreateSubRegion(LoopGraph* loop, const vector& Blocks, const unordered_map& bbToRegion) -{ - Region* region = new Region; - auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); - for (SAPFOR::BasicBlock* block : Blocks) - { - if (bbToRegion.find(block) != bbToRegion.end()) - { - region->addBasickBlocks(bbToRegion.at(block)); - } - } - for (LoopGraph* childLoop : loop->children) - { - region->addSubRegions(CreateSubRegion(childLoop, Blocks, bbToRegion)); - } - return region; -} - -Region::Region(LoopGraph* loop, const vector& Blocks) -{ - auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); - unordered_map bbToRegion; - for (auto poiner : blockSet) - { - bbToRegion[poiner] = new Region(*poiner); - this->basickBlocks.insert(bbToRegion[poiner]); - ArrayAccessingIndexes def, use; - GetDefUseArray(poiner, loop, bbToRegion[poiner]->array_def, bbToRegion[poiner]->array_use); - - } - this->header = bbToRegion[header]; - SetConnections(bbToRegion, blockSet); - //create subRegions - for (LoopGraph* childLoop : loop->children) - { - subRegions.insert(CreateSubRegion(childLoop, Blocks, bbToRegion)); - } } static void SolveDataFlowIteratively(Region* DFG) @@ -666,31 +67,56 @@ static void SolveDataFlowIteratively(Region* DFG) { Region* b = *worklist.begin(); ArrayAccessingIndexes newIn; + bool flagFirst = true; for (Region* prevBlock : b->getPrevRegions()) { - for (const auto& [arrayName, accessSet] : prevBlock->array_out) + if (flagFirst) { - if (newIn.find(arrayName) != newIn.end()) - { - newIn[arrayName] = newIn[arrayName].Intersect(accessSet); - } - else - { - newIn[arrayName] = accessSet; - } - } - } - b->array_in = newIn; - ArrayAccessingIndexes newOut; - for (auto& [arrayName, accessSet] : b->array_def) - { - if (newOut.find(arrayName) != newOut.end()) - { - newOut[arrayName] = b->array_def[arrayName].Union(b->array_in[arrayName]); + newIn = prevBlock->array_out; + flagFirst = false; } else { - newOut[arrayName] = accessSet; + if (prevBlock->array_out.empty()) + { + newIn.clear(); + continue; + } + for (const auto& [arrayName, accessSet] : prevBlock->array_out) + { + if (newIn.find(arrayName) != newIn.end()) + { + newIn[arrayName] = newIn[arrayName].Intersect(accessSet); + } + else + { + newIn[arrayName] = AccessingSet(); + } + } + } + } + b->array_in = move(newIn); + ArrayAccessingIndexes newOut; + if (b->array_def.empty()) + { + newOut = b->array_in; + } + else if (b->array_in.empty()) + { + newOut = b->array_def; + } + else + { + for (auto& [arrayName, accessSet] : b->array_def) + { + if (newOut.find(arrayName) != newOut.end()) + { + newOut[arrayName] = b->array_def[arrayName].Union(b->array_in[arrayName]); + } + else + { + newOut[arrayName] = accessSet; + } } } /* can not differ */ @@ -704,18 +130,18 @@ static void SolveDataFlowIteratively(Region* DFG) } } while (!worklist.empty()); - cout << "solveDFIt\n"; } static void SolveDataFlow(Region* DFG) { + if (!DFG) + return; SolveDataFlowIteratively(DFG); for (Region* subRegion : DFG->getSubRegions()) { SolveDataFlow(subRegion); } Collapse(DFG); - cout << "SolveDF\n"; } map FindPrivateArrays(map> &loopGraph, map>& FullIR) @@ -727,17 +153,6 @@ map FindPrivateArrays(mapgetNumber() == 4) - { - GetDefUseArray(bb, loop, def, use); - return {}; - } - } - // Region* loopRegion = new Region(loop, blocks); SolveDataFlow(loopRegion); result[loop] = loopRegion->array_priv; @@ -745,47 +160,5 @@ map FindPrivateArrays(map A = { {1, 1, 2}, {0, 1, 6} }; - vector B = { {0, 1, 6}, {2, 1, 2} }; - vector C = { {1, 1, 2}, {2, 1, 2} }; return result; } - -void GetDimensionInfo(LoopGraph* loop, map>>& loopDimensionsInfo, int level) -{ - cout << "line_num: " << loop->lineNum << endl; - for (const auto& writeOpPairs : loop->writeOpsForLoop) - { - vector> arrayDimensions(writeOpPairs.first->GetDimSize()); - loopDimensionsInfo[writeOpPairs.first] = arrayDimensions; - for (const auto& writeOp : writeOpPairs.second) - { - for (const auto& coeficient_pair : writeOp.coefficients) - { - uint64_t start, step, tripCount; - start = loop->startVal * coeficient_pair.first.first + coeficient_pair.first.second; - step = loop->stepVal * coeficient_pair.first.first; - tripCount = (loop->endVal - coeficient_pair.first.second) / step; - if (start <= loop->endVal) - { - loopDimensionsInfo[writeOpPairs.first][level].push_back({start, step, tripCount}); - cout << "level: " << level << endl; - cout << "start: " << start << endl; - cout << "step: " << step << endl; - cout << "trip_count: " << tripCount << endl; - cout << endl; - } - - - } - } - } - cout << "line_num_after: " << loop->lineNumAfterLoop << endl; - if (!loop->children.empty()) - { - for (const auto& childLoop : loop->children) - { - GetDimensionInfo(childLoop, loopDimensionsInfo, level+1); - } - } -} diff --git a/src/PrivateAnalyzer/private_arrays_search.h b/src/PrivateAnalyzer/private_arrays_search.h index cc26f78..1b8dfdd 100644 --- a/src/PrivateAnalyzer/private_arrays_search.h +++ b/src/PrivateAnalyzer/private_arrays_search.h @@ -4,118 +4,11 @@ #include #include +#include "range_structures.h" +#include "region.h" #include "../GraphLoop/graph_loops.h" #include "../CFGraph/CFGraph.h" - -struct ArrayDimension -{ - uint64_t start, step, tripCount; -}; - -class AccessingSet { - private: - std::vector> allElements; - - public: - AccessingSet(std::vector> input) : allElements(input) {}; - AccessingSet() {}; - std::vector> GetElements() const; - void Insert(const std::vector& element); - AccessingSet Union(const AccessingSet& source); - AccessingSet Intersect(const AccessingSet& secondSet) const; - AccessingSet Diff(const AccessingSet& secondSet) const; - bool ContainsElement(const std::vector& element) const; - void FindCoveredBy(const std::vector& element, std::vector>& result) const; - void FindUncovered(const std::vector& element, std::vector>& result) const; - friend bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs); -}; - -using ArrayAccessingIndexes = std::map; - -class Region: public SAPFOR::BasicBlock { - public: - Region() - { - header = nullptr; - } - - Region(SAPFOR::BasicBlock block) : SAPFOR::BasicBlock::BasicBlock(block) - { - header = nullptr; - } - - Region(LoopGraph* loop, const std::vector& Blocks); - - Region* getHeader() - { - return header; - } - - std::unordered_set& getBasickBlocks() - { - return basickBlocks; - } - - void addBasickBlocks(Region* region) - { - basickBlocks.insert(region); - } - std::unordered_set getPrevRegions() - { - return prevRegions; - } - - std::unordered_set getNextRegions() - { - return nextRegions; - } - - void addPrevRegion(Region* region) - { - prevRegions.insert(region); - } - - void addNextRegion(Region* region) - { - nextRegions.insert(region); - } - - void replaceInPrevRegions(Region* source, Region* destination) - { - prevRegions.erase(destination); - prevRegions.insert(source); - } - - void replaceInNextRegions(Region* source, Region* destination) - { - nextRegions.erase(destination); - nextRegions.insert(source); - } - - std::unordered_set getSubRegions() - { - return subRegions; - } - - void addSubRegions(Region* region) - { - subRegions.insert(region); - } - - ArrayAccessingIndexes array_def, array_use, array_out, array_in, array_priv; - - private: - std::unordered_set subRegions, basickBlocks; - /*next Region which is BB for current BB Region*/ - std::unordered_set nextRegions; - /*prev Regions which is BBs for current BB Region*/ - std::unordered_set prevRegions; - Region* header; -}; - - void Collapse(Region* region); std::map FindPrivateArrays(std::map>& loopGraph, std::map>& FullIR); -void GetDimensionInfo(LoopGraph* loop, std::map>>& loopDimensionsInfo, int level); std::pair> GetBasicBlocksForLoop(const LoopGraph* loop, const std::vector blocks); diff --git a/src/PrivateAnalyzer/range_structures.cpp b/src/PrivateAnalyzer/range_structures.cpp new file mode 100644 index 0000000..618fcfa --- /dev/null +++ b/src/PrivateAnalyzer/range_structures.cpp @@ -0,0 +1,317 @@ +#include +#include +#include +#include +#include + +#include "range_structures.h" + +using namespace std; + +static vector FindParticularSolution(const ArrayDimension& dim1, const ArrayDimension& dim2) +{ + for (uint64_t i = 0; i < dim1.tripCount; i++) + { + uint64_t leftPart = dim1.start + i * dim1.step; + for (uint64_t j = 0; j < dim2.tripCount; j++) + { + uint64_t rightPart = dim2.start + j * dim2.step; + if (leftPart == rightPart) + { + return { i, j }; + } + } + } + return {}; +} + +/* dim1 /\ dim2 */ +static ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const ArrayDimension& dim2) +{ + vector partSolution = FindParticularSolution(dim1, dim2); + if (partSolution.empty()) + { + return NULL; + } + int64_t x0 = partSolution[0], y0 = partSolution[1]; + /* x = x_0 + c * t */ + /* y = y_0 + d * t */ + int64_t c = dim2.step / gcd(dim1.step, dim2.step); + int64_t d = dim1.step / gcd(dim1.step, dim2.step); + int64_t tXMin, tXMax, tYMin, tYMax; + tXMin = -x0 / c; + tXMax = (dim1.tripCount - 1 - x0) / c; + tYMin = -y0 / d; + tYMax = (dim2.tripCount - 1 - y0) / d; + int64_t tMin = max(tXMin, tYMin); + uint64_t tMax = min(tXMax, tYMax); + if (tMin > tMax) + { + return NULL; + } + uint64_t start3 = dim1.start + x0 * dim1.step; + uint64_t step3 = c * dim1.step; + ArrayDimension* result = new(ArrayDimension){ start3, step3, tMax + 1 }; + return result; +} + +/* dim1 / dim2 */ +static vector DimensionDifference(const ArrayDimension& dim1, const ArrayDimension& dim2) +{ + ArrayDimension* intersection = DimensionIntersection(dim1, dim2); + if (!intersection) + { + return { dim1 }; + } + vector result; + /* add the part before intersection */ + if (dim1.start < intersection->start) + { + result.push_back({ dim1.start, dim1.step, (intersection->start - dim1.start) / dim1.step }); + } + /* add the parts between intersection steps */ + uint64_t start = (intersection->start - dim1.start) / dim1.step; + uint64_t interValue = intersection->start; + for (int64_t i = start; dim1.start + i * dim1.step <= intersection->start + intersection->step * (intersection->tripCount - 1); i++) + { + uint64_t centerValue = dim1.start + i * dim1.step; + if (centerValue == interValue) + { + if (i - start > 1) + { + result.push_back({ dim1.start + (start + 1) * dim1.step, dim1.step, i - start - 1 }); + start = i; + } + interValue += intersection->step; + } + } + /* add the part after intersection */ + if (intersection->start + intersection->step * (intersection->tripCount - 1) < dim1.start + dim1.step * (dim1.tripCount - 1)) + { + /* first value after intersection */ + uint64_t right_start = intersection->start + intersection->step * (intersection->tripCount - 1) + dim1.step; + uint64_t tripCount = (dim1.start + dim1.step * dim1.tripCount - right_start) / dim1.step; + result.push_back({ right_start, dim1.step, tripCount }); + } + delete(intersection); + return result; +} + + +static vector DimensionUnion(const ArrayDimension& dim1, const ArrayDimension& dim2) +{ + vector res; + ArrayDimension* inter = DimensionIntersection(dim1, dim2); + if (!inter) + { + return { dim1, dim2 }; + } + res.push_back(*inter); + delete(inter); + vector diff1, diff2; + diff1 = DimensionDifference(dim1, dim2); + diff2 = DimensionDifference(dim2, dim1); + res.insert(res.end(), diff1.begin(), diff1.end()); + res.insert(res.end(), diff2.begin(), diff2.end()); + return res; +} + +static vector ElementsIntersection(const vector& firstElement, const vector& secondElement) +{ + if (firstElement.empty() || secondElement.empty()) { + return {}; + } + size_t dimAmount = firstElement.size(); + /* check if there is no intersecction */ + for (size_t i = 0; i < dimAmount; i++) + { + if (FindParticularSolution(firstElement[i], secondElement[i]).empty()) { + return {}; + } + } + vector result(dimAmount); + for (size_t i = 0; i < dimAmount; i++) + { + ArrayDimension* resPtr = DimensionIntersection(firstElement[i], secondElement[i]); + if (resPtr) + { + result[i] = *resPtr; + } + else + { + return {}; + } + } + return result; +} + +static vector> ElementsDifference(const vector& firstElement, + const vector& secondElement) +{ + if (firstElement.empty() || secondElement.empty()) { + return {}; + } + vector intersection = ElementsIntersection(firstElement, secondElement); + vector> result; + if (intersection.empty()) + { + return { firstElement }; + } + for (int i = 0; i < firstElement.size(); i++) + { + auto dimDiff = DimensionDifference(firstElement[i], secondElement[i]); + if (!dimDiff.empty()) + { + vector firstCopy = firstElement; + for (const auto& range : dimDiff) + { + firstCopy[i] = range; + result.push_back(firstCopy); + } + } + } + return result; +} + +static void ElementsUnion(const vector& firstElement, const vector& secondElement, + vector>& lc, vector>& rc, + vector& intersection) +{ + /* lc(rc) is a set of ranges, which only exist in first(second) element*/ + intersection = ElementsIntersection(firstElement, secondElement); + lc = ElementsDifference(firstElement, intersection); + rc = ElementsDifference(secondElement, intersection); +} + +void AccessingSet::FindUncovered(const vector& element, vector>& result) const { + vector> newTails; + result.push_back(element); + for (const auto& currentElement : allElements) + { + for (const auto& tailLoc : result) + { + auto intersection = ElementsIntersection(tailLoc, currentElement); + auto diff = ElementsDifference(tailLoc, intersection); + if (!diff.empty()) { + newTails.insert(newTails.end(), diff.begin(), diff.end()); + } + } + result = move(newTails); + } +} + +bool AccessingSet::ContainsElement(const vector& element) const +{ + vector> tails; + FindUncovered(element, tails); + return !tails.empty(); +} + +void AccessingSet::FindCoveredBy(const vector& element, vector>& result) const +{ + for (const auto& currentElement : allElements) + { + auto intersection = ElementsIntersection(element, currentElement); + if (!intersection.empty()) { + result.push_back(intersection); + } + } +} + +vector> AccessingSet::GetElements() const { return allElements; } + +void AccessingSet::Insert(const vector& element) +{ + vector> tails; + FindUncovered(element, tails); + allElements.insert(allElements.end(), tails.begin(), tails.end()); +} + +AccessingSet AccessingSet::Union(const AccessingSet& source) { + AccessingSet result; + for (auto& element : source.GetElements()) { + result.Insert(element); + } + for (auto& element : allElements) + { + result.Insert(element); + } + return result; +} + +AccessingSet AccessingSet::Intersect(const AccessingSet& secondSet) const +{ + vector> result; + if (secondSet.GetElements().empty() || this->allElements.empty()) + return AccessingSet(result); + for (const auto& element : allElements) + { + if (secondSet.ContainsElement(element)) + { + result.push_back(element); + } + else + { + vector> coveredBy; + secondSet.FindCoveredBy(element, coveredBy); + if (!coveredBy.empty()) + { + result.insert(result.end(), coveredBy.begin(), coveredBy.end()); + } + } + } + return AccessingSet(result); +} + +AccessingSet AccessingSet::Diff(const AccessingSet& secondSet) const +{ + if (secondSet.GetElements().empty() || allElements.empty()) + return *this; + AccessingSet intersection = this->Intersect(secondSet); + AccessingSet uncovered = *this; + vector> result; + for (const auto& element : intersection.GetElements()) + { + vector> current_uncovered; + uncovered.FindUncovered(element, current_uncovered); + uncovered = AccessingSet(current_uncovered); + } + return uncovered; +} + +bool operator!=(const ArrayDimension& lhs, const ArrayDimension& rhs) +{ + return !(lhs.start == rhs.start && lhs.step == rhs.step && lhs.tripCount == rhs.tripCount); +} + + +bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs) +{ + for (size_t i = 0; i < lhs.allElements.size(); i++) + { + for (size_t j = 0; j < lhs.allElements[i].size(); j++) + { + if (lhs.allElements[i][j] != rhs.allElements[i][j]) + { + return true; + } + } + } + return false; +} + +bool operator!=(const ArrayAccessingIndexes& lhs, const ArrayAccessingIndexes& rhs) +{ + if (lhs.size() != rhs.size()) + { + return true; + } + for (auto& [key, value] : lhs) + { + if (rhs.find(key) == rhs.end()) + { + return true; + } + } + return false; +} diff --git a/src/PrivateAnalyzer/range_structures.h b/src/PrivateAnalyzer/range_structures.h new file mode 100644 index 0000000..004f73e --- /dev/null +++ b/src/PrivateAnalyzer/range_structures.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include +#include + +struct ArrayDimension +{ + uint64_t start, step, tripCount; +}; + +class AccessingSet { +private: + std::vector> allElements; + +public: + AccessingSet(std::vector> input) : allElements(input) {}; + AccessingSet() {}; + AccessingSet(const AccessingSet& a) { allElements = a.GetElements(); }; + std::vector> GetElements() const; + void Insert(const std::vector& element); + AccessingSet Union(const AccessingSet& source); + AccessingSet Intersect(const AccessingSet& secondSet) const; + AccessingSet Diff(const AccessingSet& secondSet) const; + bool ContainsElement(const std::vector& element) const; + void FindCoveredBy(const std::vector& element, std::vector>& result) const; + void FindUncovered(const std::vector& element, std::vector>& result) const; + friend bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs); +}; + +using ArrayAccessingIndexes = std::map; + +bool operator!=(const ArrayDimension& lhs, const ArrayDimension& rhs); +bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs); +bool operator!=(const ArrayAccessingIndexes& lhs, const ArrayAccessingIndexes& rhs); \ No newline at end of file diff --git a/src/PrivateAnalyzer/region.cpp b/src/PrivateAnalyzer/region.cpp new file mode 100644 index 0000000..64375d9 --- /dev/null +++ b/src/PrivateAnalyzer/region.cpp @@ -0,0 +1,268 @@ +#include +#include +#include +#include +#include +#include + +#include "range_structures.h" +#include "region.h" + +#include "../Utils/SgUtils.h" + +using namespace std; + +static bool isParentStmt(SgStatement* stmt, SgStatement* parent) +{ + for (; stmt; stmt = stmt->controlParent()) + if (stmt == parent) + { + return true; + } + return false; +} + +/*returns head block and loop*/ +pair> GetBasicBlocksForLoop(const LoopGraph* loop, const vector blocks) +{ + unordered_set block_loop; + SAPFOR::BasicBlock* head_block = nullptr; + auto loop_operator = loop->loop->GetOriginal(); + for (const auto& block : blocks) + { + if (!block || (block->getInstructions().size() == 0)) + { + continue; + } + SgStatement* first = block->getInstructions().front()->getInstruction()->getOperator(); + SgStatement* last = block->getInstructions().back()->getInstruction()->getOperator(); + if (isParentStmt(first, loop_operator) && isParentStmt(last, loop_operator)) + { + block_loop.insert(block); + + if ((!head_block) && (first == loop_operator) && (last == loop_operator) && + (block->getInstructions().size() == 2) && + (block->getInstructions().back()->getInstruction()->getOperation() == SAPFOR::CFG_OP::JUMP_IF)) + { + head_block = block; + } + + } + } + return { head_block, block_loop }; +} + +static void BuildLoopIndex(map& loopForIndex, LoopGraph* loop) { + string index = loop->loopSymbol; + loopForIndex[index] = loop; + for (const auto& childLoop : loop->children) { + BuildLoopIndex(loopForIndex, childLoop); + } +} + +static string FindIndexName(int pos, SAPFOR::BasicBlock* block, map& loopForIndex) { + unordered_set args = { block->getInstructions()[pos]->getInstruction()->getArg1() }; + + for (int i = pos - 1; i >= 0; i--) { + SAPFOR::Argument* res = block->getInstructions()[i]->getInstruction()->getResult(); + if (res && args.find(res) != args.end()) { + SAPFOR::Argument* arg1 = block->getInstructions()[i]->getInstruction()->getArg1(); + SAPFOR::Argument* arg2 = block->getInstructions()[i]->getInstruction()->getArg2(); + if (arg1) { + string name = arg1->getValue(); + int idx = name.find('%'); + if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end()) + return name.substr(idx + 1); + else { + args.insert(arg1); + } + } + if (arg2) { + string name = arg2->getValue(); + int idx = name.find('%'); + if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end()) + return name.substr(idx + 1); + else { + args.insert(arg2); + } + } + } + } + return ""; +} + +static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAccessingIndexes& def, ArrayAccessingIndexes& use) { + auto instructions = block->getInstructions(); + map loopForIndex; + BuildLoopIndex(loopForIndex, loop); + for (int i = 0; i < instructions.size(); i++) + { + auto instruction = instructions[i]; + if (!instruction->getInstruction()->getArg1()) { + continue; + } + auto operation = instruction->getInstruction()->getOperation(); + auto type = instruction->getInstruction()->getArg1()->getType(); + if ((operation == SAPFOR::CFG_OP::STORE || operation == SAPFOR::CFG_OP::LOAD) && type == SAPFOR::CFG_ARG_TYPE::ARRAY) + { + vector index_vars; + vector refPos; + string array_name; + if (operation == SAPFOR::CFG_OP::STORE) + { + array_name = instruction->getInstruction()->getArg1()->getValue(); + } + else + { + array_name = instruction->getInstruction()->getArg2()->getValue(); + } + int j = i - 1; + while (j >= 0 && instructions[j]->getInstruction()->getOperation() == SAPFOR::CFG_OP::REF) + { + index_vars.push_back(instructions[j]->getInstruction()->getArg1()); + refPos.push_back(j); + j--; + } + /*to choose correct dimension*/ + int n = index_vars.size(); + vector accessPoint(n); + + auto* ref = isSgArrayRefExp(instruction->getInstruction()->getExpression()); + + vector> coefsForDims; + for (int i = 0; ref && i < ref->numberOfSubscripts(); ++i) + { + const vector& coefs = getAttributes(ref->subscript(i), set{ INT_VAL }); + if (coefs.size() == 1) + { + const pair coef(coefs[0][0], coefs[0][1]); + coefsForDims.push_back(coef); + } + + } + + while (!index_vars.empty()) + { + auto var = index_vars.back(); + int currentVarPos = refPos.back(); + pair currentCoefs = coefsForDims.back(); + ArrayDimension current_dim; + if (var->getType() == SAPFOR::CFG_ARG_TYPE::CONST) { + current_dim = { stoul(var->getValue()), 1, 1 }; + } + else + { + string name, full_name = var->getValue(); + int pos = full_name.find('%'); + LoopGraph* currentLoop; + if (pos != -1) { + name = full_name.substr(pos + 1); + if (loopForIndex.find(name) != loopForIndex.end()) { + currentLoop = loopForIndex[name]; + } + else { + return -1; + } + } + else { + name = FindIndexName(currentVarPos, block, loopForIndex); + if (name == "") { + return -1; + } + if (loopForIndex.find(name) != loopForIndex.end()) { + currentLoop = loopForIndex[name]; + } + else { + return -1; + } + } + uint64_t start = currentLoop->startVal * currentCoefs.first + currentCoefs.second; + uint64_t step = currentCoefs.first; + current_dim = { start, step, (uint64_t)currentLoop->calculatedCountOfIters }; + } + accessPoint[n - index_vars.size()] = current_dim; + index_vars.pop_back(); + refPos.pop_back(); + coefsForDims.pop_back(); + } + if (operation == SAPFOR::CFG_OP::STORE) + { + def[array_name].Insert(accessPoint); + } + else + { + use[array_name].Insert(accessPoint); + } + } + } + return 0; + +} + +static void SetConnections(unordered_map& bbToRegion, const unordered_set& blockSet) +{ + for (SAPFOR::BasicBlock* block : blockSet) + { + for (SAPFOR::BasicBlock* nextBlock : block->getNext()) + { + if (bbToRegion.find(nextBlock) != bbToRegion.end()) + { + bbToRegion[block]->addNextRegion(bbToRegion[nextBlock]); + } + } + for (SAPFOR::BasicBlock* prevBlock : block->getPrev()) + { + if (bbToRegion.find(prevBlock) != bbToRegion.end()) + { + bbToRegion[block]->addPrevRegion(bbToRegion[prevBlock]); + } + } + } +} + +static Region* CreateSubRegion(LoopGraph* loop, const vector& Blocks, const unordered_map& bbToRegion) +{ + Region* region = new Region; + auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); + if (bbToRegion.find(header) != bbToRegion.end()) + { + region->setHeader(bbToRegion.at(header)); + } + else + { + return NULL; + } + for (SAPFOR::BasicBlock* block : blockSet) + { + if (bbToRegion.find(block) != bbToRegion.end()) + { + region->addBasickBlocks(bbToRegion.at(block)); + } + } + for (LoopGraph* childLoop : loop->children) + { + region->addSubRegions(CreateSubRegion(childLoop, Blocks, bbToRegion)); + } + cout << header << endl; + return region; +} + +Region::Region(LoopGraph* loop, const vector& Blocks) +{ + auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); + unordered_map bbToRegion; + for (auto poiner : blockSet) + { + bbToRegion[poiner] = new Region(*poiner); + this->basickBlocks.insert(bbToRegion[poiner]); + GetDefUseArray(poiner, loop, bbToRegion[poiner]->array_def, bbToRegion[poiner]->array_use); + + } + this->header = bbToRegion[header]; + SetConnections(bbToRegion, blockSet); + //create subRegions + for (LoopGraph* childLoop : loop->children) + { + subRegions.insert(CreateSubRegion(childLoop, Blocks, bbToRegion)); + } +} diff --git a/src/PrivateAnalyzer/region.h b/src/PrivateAnalyzer/region.h new file mode 100644 index 0000000..5e85900 --- /dev/null +++ b/src/PrivateAnalyzer/region.h @@ -0,0 +1,60 @@ +#pragma once + +#include +#include +#include +#include + +#include "../GraphLoop/graph_loops.h" +#include "../CFGraph/CFGraph.h" + +class Region : public SAPFOR::BasicBlock { +public: + Region() { header = nullptr; } + + Region(SAPFOR::BasicBlock block) : SAPFOR::BasicBlock::BasicBlock(block) { header = nullptr; } + + Region(LoopGraph* loop, const std::vector& Blocks); + + Region* getHeader() { return header; } + + void setHeader(Region* region) { header = region; } + + std::unordered_set& getBasickBlocks() { return basickBlocks; } + + void addBasickBlocks(Region* region) { basickBlocks.insert(region); } + + const std::unordered_set& getPrevRegions() { return prevRegions; } + + std::unordered_set getNextRegions() { return nextRegions; } + + void addPrevRegion(Region* region) { prevRegions.insert(region); } + + void addNextRegion(Region* region) { nextRegions.insert(region); } + + void replaceInPrevRegions(Region* source, Region* destination) + { + prevRegions.erase(destination); + prevRegions.insert(source); + } + + void replaceInNextRegions(Region* source, Region* destination) + { + nextRegions.erase(destination); + nextRegions.insert(source); + } + + std::unordered_set getSubRegions() { return subRegions; } + + void addSubRegions(Region* region) { subRegions.insert(region); } + + ArrayAccessingIndexes array_def, array_use, array_out, array_in, array_priv; + +private: + std::unordered_set subRegions, basickBlocks; + /*next Region which is BB for current BB Region*/ + std::unordered_set nextRegions; + /*prev Regions which is BBs for current BB Region*/ + std::unordered_set prevRegions; + Region* header; +}; diff --git a/src/Utils/PassManager.h b/src/Utils/PassManager.h index dad0a52..482b949 100644 --- a/src/Utils/PassManager.h +++ b/src/Utils/PassManager.h @@ -316,7 +316,7 @@ void InitPassesDependencies(map> &passDepsIn, set list({ VERIFY_INCLUDES, CORRECT_VAR_DECL }) <= Pass(SET_IMPLICIT_NONE); - list({ CALL_GRAPH2, CALL_GRAPH, BUILD_IR, LOOP_GRAPH }) <= Pass(FIND_PRIVATE_ARRAYS); + list({ CALL_GRAPH2, CALL_GRAPH, BUILD_IR, LOOP_GRAPH, LOOP_ANALYZER_DATA_DIST_S2 }) <= Pass(FIND_PRIVATE_ARRAYS); passesIgnoreStateDone.insert({ CREATE_PARALLEL_DIRS, INSERT_PARALLEL_DIRS, INSERT_SHADOW_DIRS, EXTRACT_PARALLEL_DIRS, EXTRACT_SHADOW_DIRS, CREATE_REMOTES, UNPARSE_FILE, REMOVE_AND_CALC_SHADOW, From ba632b29ce34b3d77546256a8452428d40d8317c Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 29 May 2025 09:07:20 +0300 Subject: [PATCH 11/15] cleanup and replaced parseFortranDouble to strtod --- projects/libpredictor | 2 +- src/LoopAnalyzer/loop_analyzer.cpp | 8 +-- src/ParallelizationRegions/ParRegions.cpp | 81 ++--------------------- src/Utils/utils.cpp | 14 ++-- src/Utils/version.h | 1 - 5 files changed, 17 insertions(+), 89 deletions(-) diff --git a/projects/libpredictor b/projects/libpredictor index 840f9d9..d0772cd 160000 --- a/projects/libpredictor +++ b/projects/libpredictor @@ -1 +1 @@ -Subproject commit 840f9d9c1ad579825d81c46cdf70877c497fecca +Subproject commit d0772cdb57432b8c96ce634687641b1c8c76d21c diff --git a/src/LoopAnalyzer/loop_analyzer.cpp b/src/LoopAnalyzer/loop_analyzer.cpp index 4568c85..a544fca 100644 --- a/src/LoopAnalyzer/loop_analyzer.cpp +++ b/src/LoopAnalyzer/loop_analyzer.cpp @@ -1635,9 +1635,9 @@ void loopAnalyzer(SgFile *file, vector ®ions, mapfunctions(i)->symbol()->identifier(); #if _WIN32 if (file->functions(i)->variant() != MODULE_STMT) - sendMessage_2lvl(wstring(L"��������� ������� '") + wstring(fName.begin(), fName.end()) + L"'"); + sendMessage_2lvl(wstring(L"обработка функции '") + wstring(fName.begin(), fName.end()) + L"'"); else - sendMessage_2lvl(wstring(L"��������� ������ '") + wstring(fName.begin(), fName.end()) + L"'"); + sendMessage_2lvl(wstring(L"обработка модуля '") + wstring(fName.begin(), fName.end()) + L"'"); #else if (file->functions(i)->variant() != MODULE_STMT) sendMessage_2lvl(wstring(L"processing function '") + wstring(fName.begin(), fName.end()) + L"'"); @@ -1712,8 +1712,8 @@ void loopAnalyzer(SgFile *file, vector ®ions, map ®ions, mapfunctions(i)->symbol()->identifier(); #ifdef _WIN32 - sendMessage_2lvl(wstring(L"��������� ����� ") + std::to_wstring(idx) + L"/" + std::to_wstring(convertedLoopInfo.size())); + sendMessage_2lvl(wstring(L"обработка цикла ") + std::to_wstring(idx) + L"/" + std::to_wstring(convertedLoopInfo.size())); #else sendMessage_2lvl(wstring(L"processing loop ") + std::to_wstring(idx) + L"/" + std::to_wstring(convertedLoopInfo.size())); #endif diff --git a/src/ParallelizationRegions/ParRegions.cpp b/src/ParallelizationRegions/ParRegions.cpp index d185ea9..f01be39 100644 --- a/src/ParallelizationRegions/ParRegions.cpp +++ b/src/ParallelizationRegions/ParRegions.cpp @@ -106,7 +106,7 @@ static void updateRegionInfo(SgStatement *st, map calls_from_statement; + set callsFromStatement; if (st->variant() == PROC_STAT) { @@ -114,16 +114,16 @@ static void updateRegionInfo(SgStatement *st, mapexpr(z), calls_from_statement, containsPrefix, mapFuncs); + findFuncCalls(st->expr(z), callsFromStatement, containsPrefix, mapFuncs); string filename = st->fileName(); int line = st->lineNumber(); - for (const auto &func_name : calls_from_statement) + for (const auto &func_name : callsFromStatement) funcCallFromReg[func_name][filename].insert(line); } @@ -294,71 +294,6 @@ static void checkForEmpty(SgStatement *start, SgStatement *end, vector } } -static bool parseFortranDouble(const char* str, double &val) -{ - int base_sign = 1, exp_sign = 1; - - int integer_part = 0, power = 0; - double decimal_part = 0; - while (*str && *str != '.' && *str != 'd' && *str != 'D') - { - if (*str >= '0' && *str <= '9') - integer_part = integer_part * 10 + (*str - '0'); - else if (*str == '-') - base_sign = -1; - else if (*str == '+') - base_sign = 1; - - str++; - } - - if (*str == '.') - { - str++; - - int base = 10; - while (*str >= '0' && *str <= '9') - { - decimal_part += double(*str - '0') / base; - str++; - base *= 10; - } - } - - if (*str == 'd' || *str == 'D') - { - str++; - - while (*str == '+' || *str == '-' || *str >= '0' && *str <= '9') - { - if (*str >= '0' && *str <= '9') - power = power * 10 + (*str - '0'); - else if (*str == '-') - exp_sign = -1; - else if (*str == '+') - exp_sign = 1; - - str++; - } - } - - double result = integer_part + decimal_part; - - for(int i = 0; i < power; i++) - { - if (exp_sign > 0) - result *= 10; - else - result /= 10; - } - - if (base_sign < 0) - result = -result; - - val = result; - return true; -} - void fillRegionLines(SgFile *file, vector ®ions, vector& messagesForFile, vector *loops, vector *funcs) { map mapFuncs; @@ -453,7 +388,6 @@ void fillRegionLines(SgFile *file, vector ®ions, vectorlhs(); - if (curr) { __spf_print(1, "%s %d\n", curr->unparse(), curr->variant()); @@ -462,18 +396,15 @@ void fillRegionLines(SgFile *file, vector ®ions, vectorlhs() && isSgValueExp(curr->lhs()) && - isSgValueExp(curr->lhs())->doubleValue() && - parseFortranDouble(isSgValueExp(curr->lhs())->doubleValue(), fragmentWeight)) + isSgValueExp(curr->lhs())->doubleValue()) { + fragmentWeight = strtod(isSgValueExp(curr->lhs())->doubleValue(), NULL); __spf_print(1, "->> %lf\n", fragmentWeight); } else - { __spf_print(1, "WEIGHT clause without double argument\n"); - } } } - apply_fragment = apply_fragment->rhs(); } } diff --git a/src/Utils/utils.cpp b/src/Utils/utils.cpp index 7d99610..2ee26d1 100644 --- a/src/Utils/utils.cpp +++ b/src/Utils/utils.cpp @@ -1418,32 +1418,30 @@ ParallelRegion* getRegionByLine(const vector& regions, const st std::pair getRegionAndLinesByLine(const vector& regions, const string& file, const int line) { if (regions.size() == 1 && regions[0]->GetName() == "DEFAULT") // only default - return {regions[0], NULL}; - + return { regions[0], NULL }; else if (regions.size() > 0) { map regFound; - - const ParallelRegionLines* foundLines = nullptr; + const ParallelRegionLines* foundLines = NULL; for (int i = 0; i < regions.size(); ++i) if (regions[i]->HasThisLine(line, file, &foundLines)) regFound[regions[i]] = foundLines; if (regFound.size() == 0) - return {NULL, NULL}; + return { NULL, NULL }; else if (regFound.size() == 1) return *regFound.begin(); else { __spf_print(1, "WARN: this lines included in more than one region!!\n"); - return {NULL, NULL}; + return { NULL, NULL }; } } else - return {NULL, NULL}; + return { NULL, NULL }; - return {NULL, NULL}; + return { NULL, NULL }; } set getAllRegionsByLine(const vector& regions, const string& file, const int line) diff --git a/src/Utils/version.h b/src/Utils/version.h index fb91e23..43758a9 100644 --- a/src/Utils/version.h +++ b/src/Utils/version.h @@ -1,4 +1,3 @@ #pragma once #define VERSION_SPF "2422" - From bbb9823f1d78c6fb86e329beffd1aceea8daa6b0 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 30 May 2025 12:20:08 +0300 Subject: [PATCH 12/15] updated submodule --- projects/dvm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/dvm b/projects/dvm index 6a86c96..3a567d7 160000 --- a/projects/dvm +++ b/projects/dvm @@ -1 +1 @@ -Subproject commit 6a86c96abec37282bc2aaa8f11b047d1d3f0b84e +Subproject commit 3a567d7582d01ec80d026a320415820e32e82c7c From f7a78f96265cff4aa906bcbef5fa15bad955b944 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 30 May 2025 12:29:35 +0300 Subject: [PATCH 13/15] restored messages --- projects/dvm | 2 +- src/CFGraph/IR.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/projects/dvm b/projects/dvm index 3a567d7..6a86c96 160000 --- a/projects/dvm +++ b/projects/dvm @@ -1 +1 @@ -Subproject commit 3a567d7582d01ec80d026a320415820e32e82c7c +Subproject commit 6a86c96abec37282bc2aaa8f11b047d1d3f0b84e diff --git a/src/CFGraph/IR.cpp b/src/CFGraph/IR.cpp index 6ead0e4..a46226b 100644 --- a/src/CFGraph/IR.cpp +++ b/src/CFGraph/IR.cpp @@ -395,7 +395,7 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector& if (ex) { const int var = ex->variant(); - if ((var == VAR_REF || var == CONST_REF || var == LABEL_REF) && !ex->lhs() && !ex->rhs()) // ��������� � ���������� + if ((var == VAR_REF || var == CONST_REF || var == LABEL_REF) && !ex->lhs() && !ex->rhs()) // variable reference { if (var == CONST_REF) { @@ -1572,7 +1572,7 @@ vector buildIR(SgStatement* function, const FuncInfo* func, const vec else findReturn(0, blocks.size(), blocks, blocks.back()->getNumber()); - // ���������� ������ �� GOTO � ��������� + // adding links by GOTO and jumps for (int z = 0; z < blocks.size(); ++z) { auto op = blocks[z]->getInstruction()->getOperation(); @@ -1592,7 +1592,7 @@ vector buildIR(SgStatement* function, const FuncInfo* func, const vec blocks[z]->setJump(it->second); - // ������� ����� �� ����� ���������� + // replacing the label with the instruction number arg->setValue(to_string(it->second->getNumber())); arg->setType(CFG_ARG_TYPE::INSTR); } From 8dcbd587ec9ecd3a40f43b1959c01aab6826482d Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 30 May 2025 12:31:19 +0300 Subject: [PATCH 14/15] fixed submodule --- projects/dvm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/dvm b/projects/dvm index 6a86c96..3a567d7 160000 --- a/projects/dvm +++ b/projects/dvm @@ -1 +1 @@ -Subproject commit 6a86c96abec37282bc2aaa8f11b047d1d3f0b84e +Subproject commit 3a567d7582d01ec80d026a320415820e32e82c7c From 26e36bed46687afee96f93b7863c54b7c48afd04 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 30 May 2025 12:45:05 +0300 Subject: [PATCH 15/15] fixed code style --- src/PrivateAnalyzer/private_arrays_search.cpp | 41 ++----- src/PrivateAnalyzer/private_arrays_search.h | 6 +- src/PrivateAnalyzer/range_structures.cpp | 81 +++++-------- src/PrivateAnalyzer/range_structures.h | 8 +- src/PrivateAnalyzer/region.cpp | 106 +++++++----------- src/Sapfor.cpp | 2 - 6 files changed, 86 insertions(+), 158 deletions(-) diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index c603178..985baf6 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -39,25 +39,17 @@ void Collapse(Region* region) } ArrayAccessingIndexes useUnion; for (auto& byBlock : region->getBasickBlocks()) - { for (auto& [arrayName, arrayRanges] : byBlock->array_use) - { useUnion[arrayName] = useUnion[arrayName].Union(byBlock->array_use[arrayName]); - } - } - for (auto& [arrayName, arrayRanges] : useUnion) - { - region->array_priv[arrayName] = useUnion[arrayName].Diff(region->array_use[arrayName]); - } + for (auto& [arrayName, arrayRanges] : useUnion) + region->array_priv[arrayName] = useUnion[arrayName].Diff(region->array_use[arrayName]); + for (Region* prevBlock : region->getHeader()->getPrevRegions()) - { prevBlock->replaceInNextRegions(region, region->getHeader()); - } + for (Region* nextBlock : region->getHeader()->getNextRegions()) - { nextBlock->replaceInPrevRegions(region, region->getHeader()); - } } static void SolveDataFlowIteratively(Region* DFG) @@ -85,49 +77,35 @@ static void SolveDataFlowIteratively(Region* DFG) for (const auto& [arrayName, accessSet] : prevBlock->array_out) { if (newIn.find(arrayName) != newIn.end()) - { - newIn[arrayName] = newIn[arrayName].Intersect(accessSet); - } + newIn[arrayName] = newIn[arrayName].Intersect(accessSet); else - { newIn[arrayName] = AccessingSet(); - } } } } + b->array_in = move(newIn); ArrayAccessingIndexes newOut; - if (b->array_def.empty()) - { + if (b->array_def.empty()) newOut = b->array_in; - } else if (b->array_in.empty()) - { newOut = b->array_def; - } else { for (auto& [arrayName, accessSet] : b->array_def) { if (newOut.find(arrayName) != newOut.end()) - { newOut[arrayName] = b->array_def[arrayName].Union(b->array_in[arrayName]); - } else - { newOut[arrayName] = accessSet; - } } } + /* can not differ */ if (newOut != b->array_out) - { b->array_out = newOut; - } else - { worklist.erase(b); - } } while (!worklist.empty()); } @@ -136,11 +114,10 @@ static void SolveDataFlow(Region* DFG) { if (!DFG) return; + SolveDataFlowIteratively(DFG); for (Region* subRegion : DFG->getSubRegions()) - { SolveDataFlow(subRegion); - } Collapse(DFG); } diff --git a/src/PrivateAnalyzer/private_arrays_search.h b/src/PrivateAnalyzer/private_arrays_search.h index 1b8dfdd..2faaa27 100644 --- a/src/PrivateAnalyzer/private_arrays_search.h +++ b/src/PrivateAnalyzer/private_arrays_search.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include "range_structures.h" #include "region.h" diff --git a/src/PrivateAnalyzer/range_structures.cpp b/src/PrivateAnalyzer/range_structures.cpp index 618fcfa..9a2f437 100644 --- a/src/PrivateAnalyzer/range_structures.cpp +++ b/src/PrivateAnalyzer/range_structures.cpp @@ -1,7 +1,7 @@ -#include -#include -#include -#include +#include +#include +#include +#include #include #include "range_structures.h" @@ -17,9 +17,7 @@ static vector FindParticularSolution(const ArrayDimension& dim1, const { uint64_t rightPart = dim2.start + j * dim2.step; if (leftPart == rightPart) - { return { i, j }; - } } } return {}; @@ -30,9 +28,8 @@ static ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const A { vector partSolution = FindParticularSolution(dim1, dim2); if (partSolution.empty()) - { return NULL; - } + int64_t x0 = partSolution[0], y0 = partSolution[1]; /* x = x_0 + c * t */ /* y = y_0 + d * t */ @@ -46,9 +43,8 @@ static ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const A int64_t tMin = max(tXMin, tYMin); uint64_t tMax = min(tXMax, tYMax); if (tMin > tMax) - { return NULL; - } + uint64_t start3 = dim1.start + x0 * dim1.step; uint64_t step3 = c * dim1.step; ArrayDimension* result = new(ArrayDimension){ start3, step3, tMax + 1 }; @@ -60,15 +56,13 @@ static vector DimensionDifference(const ArrayDimension& dim1, co { ArrayDimension* intersection = DimensionIntersection(dim1, dim2); if (!intersection) - { return { dim1 }; - } + vector result; /* add the part before intersection */ - if (dim1.start < intersection->start) - { + if (dim1.start < intersection->start) result.push_back({ dim1.start, dim1.step, (intersection->start - dim1.start) / dim1.step }); - } + /* add the parts between intersection steps */ uint64_t start = (intersection->start - dim1.start) / dim1.step; uint64_t interValue = intersection->start; @@ -103,11 +97,11 @@ static vector DimensionUnion(const ArrayDimension& dim1, const A vector res; ArrayDimension* inter = DimensionIntersection(dim1, dim2); if (!inter) - { return { dim1, dim2 }; - } + res.push_back(*inter); delete(inter); + vector diff1, diff2; diff1 = DimensionDifference(dim1, dim2); diff2 = DimensionDifference(dim2, dim1); @@ -118,29 +112,24 @@ static vector DimensionUnion(const ArrayDimension& dim1, const A static vector ElementsIntersection(const vector& firstElement, const vector& secondElement) { - if (firstElement.empty() || secondElement.empty()) { + if (firstElement.empty() || secondElement.empty()) return {}; - } + size_t dimAmount = firstElement.size(); /* check if there is no intersecction */ for (size_t i = 0; i < dimAmount; i++) - { - if (FindParticularSolution(firstElement[i], secondElement[i]).empty()) { + if (FindParticularSolution(firstElement[i], secondElement[i]).empty()) return {}; - } - } + vector result(dimAmount); for (size_t i = 0; i < dimAmount; i++) { ArrayDimension* resPtr = DimensionIntersection(firstElement[i], secondElement[i]); if (resPtr) - { result[i] = *resPtr; - } else - { return {}; - } + } return result; } @@ -148,15 +137,14 @@ static vector ElementsIntersection(const vector& static vector> ElementsDifference(const vector& firstElement, const vector& secondElement) { - if (firstElement.empty() || secondElement.empty()) { + if (firstElement.empty() || secondElement.empty()) return {}; - } + vector intersection = ElementsIntersection(firstElement, secondElement); vector> result; if (intersection.empty()) - { return { firstElement }; - } + for (int i = 0; i < firstElement.size(); i++) { auto dimDiff = DimensionDifference(firstElement[i], secondElement[i]); @@ -212,9 +200,8 @@ void AccessingSet::FindCoveredBy(const vector& element, vector& element) AccessingSet AccessingSet::Union(const AccessingSet& source) { AccessingSet result; - for (auto& element : source.GetElements()) { + for (auto& element : source.GetElements()) result.Insert(element); - } + for (auto& element : allElements) - { result.Insert(element); - } return result; } @@ -244,22 +229,20 @@ AccessingSet AccessingSet::Intersect(const AccessingSet& secondSet) const vector> result; if (secondSet.GetElements().empty() || this->allElements.empty()) return AccessingSet(result); + for (const auto& element : allElements) { if (secondSet.ContainsElement(element)) - { result.push_back(element); - } else { vector> coveredBy; secondSet.FindCoveredBy(element, coveredBy); if (!coveredBy.empty()) - { result.insert(result.end(), coveredBy.begin(), coveredBy.end()); - } } } + return AccessingSet(result); } @@ -267,6 +250,7 @@ AccessingSet AccessingSet::Diff(const AccessingSet& secondSet) const { if (secondSet.GetElements().empty() || allElements.empty()) return *this; + AccessingSet intersection = this->Intersect(secondSet); AccessingSet uncovered = *this; vector> result; @@ -288,30 +272,21 @@ bool operator!=(const ArrayDimension& lhs, const ArrayDimension& rhs) bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs) { for (size_t i = 0; i < lhs.allElements.size(); i++) - { for (size_t j = 0; j < lhs.allElements[i].size(); j++) - { if (lhs.allElements[i][j] != rhs.allElements[i][j]) - { return true; - } - } - } + return false; } bool operator!=(const ArrayAccessingIndexes& lhs, const ArrayAccessingIndexes& rhs) { if (lhs.size() != rhs.size()) - { return true; - } + for (auto& [key, value] : lhs) - { if (rhs.find(key) == rhs.end()) - { return true; - } - } + return false; } diff --git a/src/PrivateAnalyzer/range_structures.h b/src/PrivateAnalyzer/range_structures.h index 004f73e..257fbcf 100644 --- a/src/PrivateAnalyzer/range_structures.h +++ b/src/PrivateAnalyzer/range_structures.h @@ -1,9 +1,9 @@ #pragma once -#include -#include -#include -#include +#include +#include +#include +#include struct ArrayDimension { diff --git a/src/PrivateAnalyzer/region.cpp b/src/PrivateAnalyzer/region.cpp index 64375d9..dfe30b1 100644 --- a/src/PrivateAnalyzer/region.cpp +++ b/src/PrivateAnalyzer/region.cpp @@ -16,9 +16,8 @@ static bool isParentStmt(SgStatement* stmt, SgStatement* parent) { for (; stmt; stmt = stmt->controlParent()) if (stmt == parent) - { return true; - } + return false; } @@ -31,9 +30,8 @@ pair> GetBasicBlocksForL for (const auto& block : blocks) { if (!block || (block->getInstructions().size() == 0)) - { continue; - } + SgStatement* first = block->getInstructions().front()->getInstruction()->getOperator(); SgStatement* last = block->getInstructions().back()->getInstruction()->getOperator(); if (isParentStmt(first, loop_operator) && isParentStmt(last, loop_operator)) @@ -55,36 +53,39 @@ pair> GetBasicBlocksForL static void BuildLoopIndex(map& loopForIndex, LoopGraph* loop) { string index = loop->loopSymbol; loopForIndex[index] = loop; - for (const auto& childLoop : loop->children) { + + for (const auto& childLoop : loop->children) BuildLoopIndex(loopForIndex, childLoop); - } } static string FindIndexName(int pos, SAPFOR::BasicBlock* block, map& loopForIndex) { unordered_set args = { block->getInstructions()[pos]->getInstruction()->getArg1() }; - for (int i = pos - 1; i >= 0; i--) { + for (int i = pos - 1; i >= 0; i--) + { SAPFOR::Argument* res = block->getInstructions()[i]->getInstruction()->getResult(); - if (res && args.find(res) != args.end()) { + if (res && args.find(res) != args.end()) + { SAPFOR::Argument* arg1 = block->getInstructions()[i]->getInstruction()->getArg1(); SAPFOR::Argument* arg2 = block->getInstructions()[i]->getInstruction()->getArg2(); - if (arg1) { + if (arg1) + { string name = arg1->getValue(); int idx = name.find('%'); if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end()) return name.substr(idx + 1); - else { + else args.insert(arg1); - } } - if (arg2) { + + if (arg2) + { string name = arg2->getValue(); int idx = name.find('%'); if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end()) return name.substr(idx + 1); - else { + else args.insert(arg2); - } } } } @@ -98,9 +99,9 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces for (int i = 0; i < instructions.size(); i++) { auto instruction = instructions[i]; - if (!instruction->getInstruction()->getArg1()) { + if (!instruction->getInstruction()->getArg1()) continue; - } + auto operation = instruction->getInstruction()->getOperation(); auto type = instruction->getInstruction()->getArg1()->getType(); if ((operation == SAPFOR::CFG_OP::STORE || operation == SAPFOR::CFG_OP::LOAD) && type == SAPFOR::CFG_ARG_TYPE::ARRAY) @@ -109,13 +110,10 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces vector refPos; string array_name; if (operation == SAPFOR::CFG_OP::STORE) - { - array_name = instruction->getInstruction()->getArg1()->getValue(); - } + array_name = instruction->getInstruction()->getArg1()->getValue(); else - { array_name = instruction->getInstruction()->getArg2()->getValue(); - } + int j = i - 1; while (j >= 0 && instructions[j]->getInstruction()->getOperation() == SAPFOR::CFG_OP::REF) { @@ -123,12 +121,12 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces refPos.push_back(j); j--; } + /*to choose correct dimension*/ int n = index_vars.size(); vector accessPoint(n); auto* ref = isSgArrayRefExp(instruction->getInstruction()->getExpression()); - vector> coefsForDims; for (int i = 0; ref && i < ref->numberOfSubscripts(); ++i) { @@ -147,52 +145,48 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces int currentVarPos = refPos.back(); pair currentCoefs = coefsForDims.back(); ArrayDimension current_dim; - if (var->getType() == SAPFOR::CFG_ARG_TYPE::CONST) { + if (var->getType() == SAPFOR::CFG_ARG_TYPE::CONST) current_dim = { stoul(var->getValue()), 1, 1 }; - } else { string name, full_name = var->getValue(); int pos = full_name.find('%'); LoopGraph* currentLoop; - if (pos != -1) { + if (pos != -1) + { name = full_name.substr(pos + 1); - if (loopForIndex.find(name) != loopForIndex.end()) { - currentLoop = loopForIndex[name]; - } - else { + if (loopForIndex.find(name) != loopForIndex.end()) + currentLoop = loopForIndex[name]; + else return -1; - } } - else { + else + { name = FindIndexName(currentVarPos, block, loopForIndex); - if (name == "") { + if (name == "") return -1; - } - if (loopForIndex.find(name) != loopForIndex.end()) { - currentLoop = loopForIndex[name]; - } - else { + + if (loopForIndex.find(name) != loopForIndex.end()) + currentLoop = loopForIndex[name]; + else return -1; - } } + uint64_t start = currentLoop->startVal * currentCoefs.first + currentCoefs.second; uint64_t step = currentCoefs.first; current_dim = { start, step, (uint64_t)currentLoop->calculatedCountOfIters }; } + accessPoint[n - index_vars.size()] = current_dim; index_vars.pop_back(); refPos.pop_back(); coefsForDims.pop_back(); } - if (operation == SAPFOR::CFG_OP::STORE) - { + + if (operation == SAPFOR::CFG_OP::STORE) def[array_name].Insert(accessPoint); - } else - { use[array_name].Insert(accessPoint); - } } } return 0; @@ -204,19 +198,12 @@ static void SetConnections(unordered_map& bbToRegi for (SAPFOR::BasicBlock* block : blockSet) { for (SAPFOR::BasicBlock* nextBlock : block->getNext()) - { if (bbToRegion.find(nextBlock) != bbToRegion.end()) - { bbToRegion[block]->addNextRegion(bbToRegion[nextBlock]); - } - } + for (SAPFOR::BasicBlock* prevBlock : block->getPrev()) - { if (bbToRegion.find(prevBlock) != bbToRegion.end()) - { bbToRegion[block]->addPrevRegion(bbToRegion[prevBlock]); - } - } } } @@ -225,24 +212,17 @@ static Region* CreateSubRegion(LoopGraph* loop, const vectorsetHeader(bbToRegion.at(header)); - } + region->setHeader(bbToRegion.at(header)); else - { return NULL; - } + for (SAPFOR::BasicBlock* block : blockSet) - { if (bbToRegion.find(block) != bbToRegion.end()) - { region->addBasickBlocks(bbToRegion.at(block)); - } - } + for (LoopGraph* childLoop : loop->children) - { region->addSubRegions(CreateSubRegion(childLoop, Blocks, bbToRegion)); - } + cout << header << endl; return region; } @@ -262,7 +242,5 @@ Region::Region(LoopGraph* loop, const vector& Blocks) SetConnections(bbToRegion, blockSet); //create subRegions for (LoopGraph* childLoop : loop->children) - { subRegions.insert(CreateSubRegion(childLoop, Blocks, bbToRegion)); - } } diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index 8583e19..9f5ac64 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -1030,9 +1030,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne countOfTransform += removeDeadCode(func->funcPointer, allFuncInfo, commonBlocks); } else if (curr_regime == FIND_PRIVATE_ARRAYS) - { FindPrivateArrays(loopGraph, fullIR); - } else if (curr_regime == TEST_PASS) { //test pass