From 400c378691080b76b04f7c5e908d0024a56d4be3 Mon Sep 17 00:00:00 2001 From: Oleg Nikitin Date: Thu, 21 Nov 2024 14:42:55 +0300 Subject: [PATCH] get def use for array --- sapfor/experts/Sapfor_2017/CMakeLists.txt | 8 +- .../PrivateArrays/private_arrays_search.cpp | 195 ++++++++++++++++++ .../PrivateArrays/private_arrays_search.h | 15 ++ sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 6 +- sapfor/experts/Sapfor_2017/_src/Sapfor.h | 4 + .../Sapfor_2017/_src/Utils/PassManager.h | 2 + 6 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 sapfor/experts/Sapfor_2017/_src/PrivateArrays/private_arrays_search.cpp create mode 100644 sapfor/experts/Sapfor_2017/_src/PrivateArrays/private_arrays_search.h diff --git a/sapfor/experts/Sapfor_2017/CMakeLists.txt b/sapfor/experts/Sapfor_2017/CMakeLists.txt index 2a26a8a..e6b61e4 100644 --- a/sapfor/experts/Sapfor_2017/CMakeLists.txt +++ b/sapfor/experts/Sapfor_2017/CMakeLists.txt @@ -195,6 +195,9 @@ set(TR_IMPLICIT_NONE _src/Transformations/set_implicit_none.cpp _src/Transformations/set_implicit_none.h) set(TR_REPLACE_ARRAYS_IN_IO _src/Transformations/replace_dist_arrays_in_io.cpp _src/Transformations/replace_dist_arrays_in_io.h) +set(FIND_PRIVATE_ARRAYS _src/PrivateArrays/private_arrays_search.cpp + _src/PrivateArrays/private_arrays_search.h) + set(TRANSFORMS ${TR_DEAD_CODE} @@ -423,7 +426,8 @@ set(SOURCE_EXE ${ZLIB} ${GR_LAYOUT} ${PR_PARAM} - ${PROJ_MAN}) + ${PROJ_MAN} + ${FIND_PRIVATE_ARRAYS}) add_executable(Sapfor_F ${SOURCE_EXE}) source_group (CFGraph FILES ${CFG}) @@ -476,6 +480,8 @@ source_group (Predictor FILES ${PREDICTOR}) source_group (Parser FILES ${PARSER}) source_group (PPPA\\PPPA FILES ${PPPA}) source_group (PPPA\\ZLib FILES ${ZLIB}) + +source_group (PrivateArrays FILES ${FIND_PRIVATE_ARRAYS}) if (MSVC_IDE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /Zc:__cplusplus") diff --git a/sapfor/experts/Sapfor_2017/_src/PrivateArrays/private_arrays_search.cpp b/sapfor/experts/Sapfor_2017/_src/PrivateArrays/private_arrays_search.cpp new file mode 100644 index 0000000..4f4b830 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/PrivateArrays/private_arrays_search.cpp @@ -0,0 +1,195 @@ +#include +#include +#include +#include +#include + +#include "private_arrays_search.h" + +void print_info(LoopGraph* loop) +{ + std::cout << "loopSymbol: " << loop->loopSymbol << std::endl; + for (const auto ops : loop->writeOpsForLoop) + { + std::cout << "Array name: " << ops.first->GetShortName() << std::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*/ +std::pair> GetBasicBlocksForLoop(LoopGraph* loop, std::vector blocks) +{ + std::unordered_set block_loop; + SAPFOR::BasicBlock* head_block = nullptr; + auto loop_operator = loop->loop->GetOriginal(); + for (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 }; +} + +LoopGraph* FindLoopForIndex(std::string var_name, LoopGraph* loop) +{ + if (loop->loopSymbol == var_name) + return loop; + else if (loop->children.empty()) { + return nullptr; + } + else + { + for (auto& child : loop->children) + { + LoopGraph* res = FindLoopForIndex(var_name, child); + if (res) + return res; + } + } +} + +void GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAccessingIndexes& def, ArrayAccessingIndexes& use) { + + auto instructions = block->getInstructions(); + for(int i = 0; i < instructions.size(); i++) + { + auto instruction = instructions[i]; + if ((instruction->getInstruction()->getOperation() == SAPFOR::CFG_OP::STORE && instruction->getInstruction()->getArg1()->getType() == SAPFOR::CFG_ARG_TYPE::ARRAY) || + (instruction->getInstruction()->getOperation() == SAPFOR::CFG_OP::LOAD && instruction->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::ARRAY)) + { + SAPFOR::CFG_OP operation = instruction->getInstruction()->getOperation(); + std::queue index_vars; + std::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(instructions[j]->getInstruction()->getArg1()); + } + /*to choose correct dimension*/ + int n = index_vars.size(); + 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); + } + while (!index_vars.empty()) + { + auto var = index_vars.front(); + ArrayDimension current_dim; + if (var->getType() == SAPFOR::CFG_ARG_TYPE::CONST) { + current_dim = { stol(var->getValue()), 0, 1 }; + } + else + { + std::string full_name = var->getValue(); + int pos = full_name.find('%'); + LoopGraph* current_loop = FindLoopForIndex(full_name.substr(pos), loop); + current_dim = { loop->startVal, loop->stepVal, loop->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); + index_vars.pop(); + } + } + } + +} + + + + +void FindPrivateArrays(std::map> &loopGraph, std::map>& FullIR) +{ + for (const auto& curr_graph_pair: loopGraph) + { + for (const auto& curr_loop : curr_graph_pair.second) + { + ArrayAccessingIndexes loopDimensionsInfo; + //GetDimensionInfo(curr_loop, loopDimensionsInfo, 0); + //print_info(curr_loop); + } + } +} + +void GetDimensionInfo(LoopGraph* loop, std::map>>& loopDimensionsInfo, int level) +{ + std::cout << "line_num: " << loop->lineNum << std::endl; + for (const auto& writeOpPairs : loop->writeOpsForLoop) + { + std::vector> arrayDimensions(writeOpPairs.first->GetDimSize()); + loopDimensionsInfo[writeOpPairs.first] = arrayDimensions; + for (const auto& writeOp : writeOpPairs.second) + { + for (const auto& coeficient_pair : writeOp.coefficients) + { + int64_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}); + std::cout << "level: " << level << std::endl; + std::cout << "start: " << start << std::endl; + std::cout << "step: " << step << std::endl; + std::cout << "trip_count: " << tripCount << std::endl; + std::cout << std::endl; + } + + + } + } + } + std::cout << "line_num_after: " << loop->lineNumAfterLoop << std::endl; + if (!loop->children.empty()) + { + for (const auto& childLoop : loop->children) + { + GetDimensionInfo(childLoop, loopDimensionsInfo, level+1); + } + } +} diff --git a/sapfor/experts/Sapfor_2017/_src/PrivateArrays/private_arrays_search.h b/sapfor/experts/Sapfor_2017/_src/PrivateArrays/private_arrays_search.h new file mode 100644 index 0000000..3251b47 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/PrivateArrays/private_arrays_search.h @@ -0,0 +1,15 @@ +#pragma once + +#include "../GraphLoop/graph_loops.h" +#include "../CFGraph/CFGraph.h" + +struct ArrayDimension +{ + int64_t start, step, tripCount; +}; + +typedef std::map>> ArrayAccessingIndexes; + +void FindPrivateArrays(std::map>& loopGraph, std::map>& FullIR); +void GetDimensionInfo(LoopGraph* loop, std::map>>& loopDimensionsInfo, int level); +std::set GetBasicBlocksForLoop(LoopGraph* loop, std::vector); diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index 9781d75..59871b9 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -98,6 +98,8 @@ #include "Inliner/inliner.h" +#include "PrivateArrays/private_arrays_search.h" + #include "dvm.h" #include "Sapfor.h" #include "Utils/PassManager.h" @@ -1902,7 +1904,9 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne calculateStatsForPredictor(allFuncInfo, gCovInfo); parseDvmDirForPredictor(declaredArrays, commonBlocks, allFuncInfo, gCovInfo); } - + else if (curr_regime == FIND_PRIVATE_ARRAYS) { + FindPrivateArrays(loopGraph, fullIR); + } const float elapsed = duration_cast(high_resolution_clock::now() - timeForPass).count() / 1000.; const float elapsedGlobal = duration_cast(high_resolution_clock::now() - globalTime).count() / 1000.; __spf_print(1, "PROFILE: time for this pass = %f sec (total %f sec)\n", elapsed, elapsedGlobal); diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.h b/sapfor/experts/Sapfor_2017/_src/Sapfor.h index d37b0fb..250d8a5 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.h +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.h @@ -182,6 +182,8 @@ enum passes { SET_IMPLICIT_NONE, RENAME_INLCUDES, + FIND_PRIVATE_ARRAYS, + TEST_PASS, EMPTY_PASS }; @@ -366,6 +368,8 @@ static void setPassValues() 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/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h index 7e44619..f241a40 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -314,6 +314,8 @@ void InitPassesDependencies(map> &passDepsIn, set list({ VERIFY_INCLUDES, CORRECT_VAR_DECL }) <= Pass(SET_IMPLICIT_NONE); + list({ CALL_GRAPH2, LOOP_ANALYZER_DATA_DIST_S1 }) <= 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,