From 7d342c51a8e8dceee5354ec5b3f85bf36f209677 Mon Sep 17 00:00:00 2001 From: Mikhail Kocharmin Date: Mon, 2 Dec 2024 23:17:58 +0300 Subject: [PATCH] replace_dist_arrays_in_io: copy declorations from includes, fix replacement bounds, improve performance, fix SgUtils --- .../replace_dist_arrays_in_io.cpp | 276 +++++++++++------- .../Sapfor_2017/_src/Utils/SgUtils.cpp | 2 +- 2 files changed, 168 insertions(+), 110 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/replace_dist_arrays_in_io.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/replace_dist_arrays_in_io.cpp index 7cc53d1..efb63e4 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/replace_dist_arrays_in_io.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/replace_dist_arrays_in_io.cpp @@ -5,6 +5,7 @@ #include #include #include +#include using std::map; using std::set; @@ -12,6 +13,7 @@ using std::string; using std::vector; using std::to_string; using std::make_pair; +using std::pair; #define DEBUG_TRACE 0 @@ -27,7 +29,7 @@ static void findArrays(SgExpression* exp, set& arrays) } } -static void populateDistributedIoArrays(map>& arrays, SgStatement* stat) +static void populateDistributedIoArrays(map>& arrays, SgStatement* stat) { auto var = stat->variant(); @@ -122,7 +124,7 @@ static void populateDistributedIoArrays(map>& ar { string array_name = string(by_symb->identifier()); DIST::Array* array_p = getArrayFromDeclarated(declaratedInStmt(by_symb), array_name); - if (array_p && array_p->GetDistributeFlagVal() == Distribution::distFlag::IO_PRIV && arrays[array_p].insert(stat).second) + if (array_p && array_p->GetDistributeFlagVal() == Distribution::distFlag::IO_PRIV && arrays[by_symb].insert(stat).second) __spf_print(DEBUG_TRACE, "[%d]: add array %s\n", stat->lineNumber(), array_p->GetName().c_str()); } @@ -134,7 +136,7 @@ static void replaceArrayRec(SgSymbol* arr, SgSymbol* replace_by, SgExpression* e if (!exp) return; - if (exp->symbol() && strcmp(exp->symbol()->identifier(), arr->identifier()) == 0) + if (exp->symbol() && exp->symbol()->identifier() && strcmp(exp->symbol()->identifier(), arr->identifier()) == 0) { has_read |= from_read; has_write |= from_write; @@ -143,24 +145,24 @@ static void replaceArrayRec(SgSymbol* arr, SgSymbol* replace_by, SgExpression* e switch (exp->variant()) { - case FUNC_CALL: - { - replaceArrayRec(arr, replace_by, exp->rhs(), has_read, has_write, true, false); - replaceArrayRec(arr, replace_by, exp->lhs(), has_read, has_write, true, true); - break; - } - case EXPR_LIST: - { - replaceArrayRec(arr, replace_by, exp->lhs(), has_read, has_write, from_read, from_write); - replaceArrayRec(arr, replace_by, exp->rhs(), has_read, has_write, from_read, from_write); - break; - } - default: - { - replaceArrayRec(arr, replace_by, exp->lhs(), has_read, has_write, true, false); - replaceArrayRec(arr, replace_by, exp->rhs(), has_read, has_write, true, false); - break; - } + case FUNC_CALL: + { + replaceArrayRec(arr, replace_by, exp->rhs(), has_read, has_write, true, false); + replaceArrayRec(arr, replace_by, exp->lhs(), has_read, has_write, true, true); + break; + } + case EXPR_LIST: + { + replaceArrayRec(arr, replace_by, exp->lhs(), has_read, has_write, from_read, from_write); + replaceArrayRec(arr, replace_by, exp->rhs(), has_read, has_write, from_read, from_write); + break; + } + default: + { + replaceArrayRec(arr, replace_by, exp->lhs(), has_read, has_write, true, false); + replaceArrayRec(arr, replace_by, exp->rhs(), has_read, has_write, true, false); + break; + } } } @@ -171,31 +173,31 @@ static void replaceArrayRec(SgSymbol* arr, SgSymbol* replace_by, SgStatement* st switch (st->variant()) { - case ASSIGN_STAT: - case READ_STAT: - { - replaceArrayRec(arr, replace_by, st->expr(0), has_read, has_write, false, true); - replaceArrayRec(arr, replace_by, st->expr(1), has_read, has_write, true, false); - break; - } - case PROC_STAT: - case FUNC_STAT: - { - replaceArrayRec(arr, replace_by, st->expr(0), has_read, has_write, true, false); - replaceArrayRec(arr, replace_by, st->expr(1), has_read, has_write, true, true); - break; - } - default: - { - for (int i = 0; i < 3; i++) - replaceArrayRec(arr, replace_by, st->expr(i), has_read, has_write, true, false); + case ASSIGN_STAT: + case READ_STAT: + { + replaceArrayRec(arr, replace_by, st->expr(0), has_read, has_write, false, true); + replaceArrayRec(arr, replace_by, st->expr(1), has_read, has_write, true, false); + break; + } + case PROC_STAT: + case FUNC_STAT: + { + replaceArrayRec(arr, replace_by, st->expr(0), has_read, has_write, true, false); + replaceArrayRec(arr, replace_by, st->expr(1), has_read, has_write, true, true); + break; + } + default: + { + for (int i = 0; i < 3; i++) + replaceArrayRec(arr, replace_by, st->expr(i), has_read, has_write, true, false); - break; - } + break; + } } } -static void copyArrayBetweenStatements(SgSymbol* replace_symb, SgSymbol* replace_by, SgStatement* start, SgStatement* last) +static void copyArrayBetweenStatements(SgSymbol* replace_symb, SgSymbol* replace_by, SgStatement* start, SgStatement* last, bool start_is_scope) { while (start->lexNext() && !isSgExecutableStatement(start->lexNext())) start = start->lexNext(); @@ -204,7 +206,7 @@ static void copyArrayBetweenStatements(SgSymbol* replace_symb, SgSymbol* replace bool has_read = false, has_write = false; - for (auto* st = start; st != stop; st = st->lexNext()) + for (auto* st = start->lexNext(); st && st != stop->lexPrev(); st = st->lexNext()) replaceArrayRec(replace_symb, replace_by, st, has_read, has_write); @@ -213,7 +215,7 @@ static void copyArrayBetweenStatements(SgSymbol* replace_symb, SgSymbol* replace // A_copy = A SgAssignStmt* assign = new SgAssignStmt(*new SgArrayRefExp(*replace_by), *new SgArrayRefExp(*replace_symb)); assign->setlineNumber(getNextNegativeLineNumber()); // before region - auto* parent = start->controlParent(); + auto* parent = start_is_scope ? start : start->controlParent(); if (parent && parent->lastNodeOfStmt() == start) parent = parent->controlParent(); @@ -230,13 +232,11 @@ static void copyArrayBetweenStatements(SgSymbol* replace_symb, SgSymbol* replace } } -static void replaceArrayInFragment(DIST::Array* arr, const set usages, SgSymbol* replace_by, SgStatement* start, SgStatement* last, const string& filename) +static void replaceArrayInFragment(SgSymbol* replace_symb, const set usages, SgSymbol* replace_by, SgStatement* start, SgStatement* last, const string& filename) { while (start->lexNext() && !isSgExecutableStatement(start->lexNext())) start = start->lexNext(); - auto* replace_symb = arr->GetDeclSymbol(); - set not_opened, not_closed, copied; for (auto* it = start; it; it = it->controlParent()) @@ -271,9 +271,9 @@ static void replaceArrayInFragment(DIST::Array* arr, const set usa auto* scope_start = copy_scope, * scope_end = copy_scope->lastNodeOfStmt(); __spf_print(DEBUG_TRACE, "[scope to copy] %d\n", copy_scope->lineNumber()); - if (not_opened.find(copy_scope) != not_opened.end()) + if (not_opened.find(copy_scope) != not_opened.end() && start != copy_scope) { - auto* from = start->lastNodeOfStmt() ? start->lastNodeOfStmt() : start; + auto* from = start; for (auto* st = from; st; st = st->controlParent()) { __spf_print(DEBUG_TRACE, "[find start of parent %d] %d\n", copy_scope->lineNumber(), st->lineNumber()); @@ -286,7 +286,7 @@ static void replaceArrayInFragment(DIST::Array* arr, const set usa } } - if (not_closed.find(copy_scope) != not_closed.end()) + if (not_closed.find(copy_scope) != not_closed.end() && last != copy_scope) { for (auto* st = last; st; st = st->controlParent()) { @@ -300,17 +300,34 @@ static void replaceArrayInFragment(DIST::Array* arr, const set usa } } - copyArrayBetweenStatements(replace_symb, replace_by, scope_start, scope_end); - __spf_print(DEBUG_TRACE, "[copy %s] [%d, %d]\n", arr->GetName().c_str(), scope_start->lineNumber(), scope_end->lineNumber()); + __spf_print(DEBUG_TRACE, "[copy %s] [%d, %d]\n", replace_symb->identifier(), scope_start->lineNumber(), scope_end->lineNumber()); + copyArrayBetweenStatements(replace_symb, replace_by, scope_start, scope_end, copy_scope == scope_start); copied.insert(copy_scope); } } -static bool ioReginBound(SgStatement* stat, SgStatement* last_io_bound) +static bool ioReginBorder(SgStatement* stat, SgStatement* last_io_bound) { auto var = stat->variant(); - if (var == PROC_STAT || var == FUNC_STAT || var == PROG_HEDR || var == FUNC_HEDR || var == PROC_HEDR || var == FUNC_STAT || var == FOR_NODE || var == LOOP_NODE) + static const set border_stats = + { + PROC_STAT, + FUNC_STAT, + PROG_HEDR, + FUNC_HEDR, + PROC_HEDR, + FOR_NODE, + LOOP_NODE, + RETURN_NODE, + RETURN_STAT, + STOP_STAT, + STOP_NODE, + EXIT_STMT, + EXIT_NODE + }; + + if (border_stats.find(var) != border_stats.end()) return true; if (last_io_bound && last_io_bound->lastNodeOfStmt() && last_io_bound->lastNodeOfStmt() == stat) @@ -318,23 +335,19 @@ static bool ioReginBound(SgStatement* stat, SgStatement* last_io_bound) int parent_var; - if (var == CONTROL_END && - ((parent_var = stat->controlParent()->variant()) == PROG_HEDR || - parent_var == PROC_HEDR || parent_var == FUNC_HEDR)) - { + if (var == CONTROL_END && border_stats.find(stat->controlParent()->variant()) != border_stats.end()) return true; - } return false; } void replaceDistributedArraysInIO(vector& regions, - const map>& allFuncInfo, - map>& SPF_messages, - map>>& newDeclsToInclude) + const map>& allFuncInfo, + map>& SPF_messages, + map>>& newDeclsToInclude) { - map created_copies; - map>> copied; + map created_copies; + map>> copied_syms; for (auto& region : regions) { @@ -344,12 +357,14 @@ void replaceDistributedArraysInIO(vector& regions, { const auto& filename = linesByFile.first; - if (SgFile::switchToFile(filename) < 0) { + if (SgFile::switchToFile(filename) < 0) + { printInternalError(convertFileName(__FILE__).c_str(), __LINE__); return; } - for (auto& lines : linesByFile.second) { + for (auto& lines : linesByFile.second) + { __spf_print(DEBUG_TRACE, "[fragment] %s: %d:%d %d\n", filename.c_str(), lines.lines.first, lines.lines.second, lines.isImplicit()); @@ -370,7 +385,7 @@ void replaceDistributedArraysInIO(vector& regions, end = lines.stats.second->GetOriginal()->lexNext(); } - map> need_replace; + map> need_replace; SgStatement* last_io_bound = NULL; @@ -395,8 +410,92 @@ void replaceDistributedArraysInIO(vector& regions, break; } - if (ioReginBound(curr_stmt, last_io_bound)) + if (ioReginBorder(curr_stmt, last_io_bound)) { + for (const auto& by_array_to_copy : need_replace) + { + auto* array_to_copy = by_array_to_copy.first; + auto it = created_copies.find(array_to_copy); + + if (it == created_copies.end()) + { + string array_name = string(array_to_copy->identifier()); + DIST::Array* array_p = getArrayFromDeclarated(declaratedInStmt(array_to_copy), array_name); + + bool fromModule = (array_p->GetLocation().first == DIST::l_MODULE); + const string locationName = array_p->GetLocation().second; + + auto place = *array_p->GetDeclInfo().begin(); + string fileName = place.first; + string suffix = "_io_l"; + + if (fromModule) + suffix = "_io_m"; + + pair copied; + copied.first = array_to_copy; + + if (SgFile::switchToFile(fileName) == -1) + { + auto* func_stmt = curr_stmt->getScopeForDeclare(); + + SgStatement* insertPlace = NULL; + for (auto iterator = func_stmt->lexNext(); + !isSgExecutableStatement(iterator) || isSPF_stat(iterator) && + !(iterator->variant() == SPF_PARALLEL_REG_DIR || iterator->variant() == SPF_END_PARALLEL_REG_DIR); + iterator = iterator->lexNext()) + { + insertPlace = iterator; + } + + //NULL - no decl stats in function! + if (!insertPlace) + insertPlace = func_stmt; + + auto st = insertPlace->controlParent(); + if (st->variant() == GLOBAL) + st = insertPlace; + + auto& copied_symb = array_to_copy->copy(); + copied.second = &copied_symb; + + auto new_name = string(array_to_copy->identifier()) + "_io_c"; + copied_symb.changeName(new_name.c_str()); + + auto stat = array_to_copy->makeVarDeclStmt(); + auto res = CalculateInteger(stat->expr(0)->copyPtr()); + res->lhs()->setSymbol(copied_symb); + stat->setExpression(0, res); + + insertPlace->insertStmtAfter(*stat, *st); + } + else + { + copied = copyArray(place, array_p, linesByFile.second, suffix + to_string(region->GetId()), fileName, newDeclsToInclude, copied_syms); + } + + SgStatement* decl = SgStatement::getStatementByFileAndLine(place.first, place.second); + + if (decl) + decl = decl->lexNext(); + + if (decl) + { + string dir_str; + if (decl->comments()) + { + string str_comment = string(decl->comments()); + if (str_comment.size() && str_comment.back() != '\n') + dir_str += "\n"; + } + + dir_str += "!$SPF ANALYSIS(PROCESS_PRIVATE(" + string(copied.second->identifier()) + "))\n"; + decl->addComment(dir_str.c_str()); + } + created_copies.insert({ array_to_copy, copied.second }); + } + } + if (last_io_bound) { __spf_print(DEBUG_TRACE, "[io region] [%d, %d]\n", last_io_bound->lineNumber(), curr_stmt->lineNumber()); @@ -429,47 +528,6 @@ void replaceDistributedArraysInIO(vector& regions, } populateDistributedIoArrays(need_replace, curr_stmt); - - for (const auto& by_array_to_copy : need_replace) - { - auto* array_to_copy = by_array_to_copy.first; - auto it = created_copies.find(array_to_copy); - - if (it == created_copies.end()) - { - bool fromModule = (array_to_copy->GetLocation().first == DIST::l_MODULE); - const string locationName = array_to_copy->GetLocation().second; - - auto place = *array_to_copy->GetDeclInfo().begin(); - string fileName = place.first; - string suffix = "_io_l"; - - if (fromModule) - suffix = "_io_m"; - - auto origCopy = copyArray(place, array_to_copy, linesByFile.second, suffix + to_string(region->GetId()), fileName, newDeclsToInclude, copied); - SgStatement* decl = SgStatement::getStatementByFileAndLine(place.first, place.second); - - if(decl) - decl = decl->lexNext(); - - if(decl) - { - string dir_str; - if (decl->comments()) - { - string str_comment = string(decl->comments()); - if(str_comment.size() && str_comment.back() != '\n') - dir_str += "\n"; - } - - dir_str += "!$SPF ANALYSIS(PROCESS_PRIVATE(" + string(origCopy.second->identifier()) + "))\n"; - decl->addComment(dir_str.c_str()); - } - created_copies.insert({ array_to_copy, origCopy.second }); - } - } - curr_stmt = curr_stmt->lexNext(); } } diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp index 9dcd3f3..5f1c00b 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp @@ -648,7 +648,7 @@ string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const ch if (wasDeleted) { - if (str.back() != '\n') + if (str.size() || str.back() != '\n') str += '\n'; st->setComments(str.c_str()); }