From de842a1e2c8060b3c86cb6d4b5abf900d911a9f4 Mon Sep 17 00:00:00 2001 From: xnpster Date: Thu, 11 Sep 2025 12:40:05 +0300 Subject: [PATCH] REMOVE_DIST_ARRAYS_FROM_IO: do not process arrays from headers, copy from correct declarations, improve style --- .../replace_dist_arrays_in_io.cpp | 267 +++++++++--------- 1 file changed, 141 insertions(+), 126 deletions(-) diff --git a/src/Transformations/ReplaceArraysInIO/replace_dist_arrays_in_io.cpp b/src/Transformations/ReplaceArraysInIO/replace_dist_arrays_in_io.cpp index c5f8335..1ed6393 100644 --- a/src/Transformations/ReplaceArraysInIO/replace_dist_arrays_in_io.cpp +++ b/src/Transformations/ReplaceArraysInIO/replace_dist_arrays_in_io.cpp @@ -17,20 +17,6 @@ using std::pair; #define DEBUG_TRACE 0 -static bool findAlloatableKeyword(SgExpression* exp) -{ - if (exp) - { - if (exp->variant() == ALLOCATABLE_OP) - return true; - - return findAlloatableKeyword(exp->lhs()) || findAlloatableKeyword(exp->rhs()); - } - - return false; -} - - static bool checkDynamicArray(DIST::Array *array) { for (const auto &bounds : array->GetSizes()) @@ -59,27 +45,69 @@ static SgExpression* findExprWithVariant(SgExpression* exp, int variant) return NULL; } -static bool checkAssumedSize(SgStatement *st, const string &arrayName, const string ¤tFile) +bool switchToDeclarationFile(DIST::Array* array_p, + pair& decl_place, + const string ¤t_file_name, + FuncInfo *current_func) { + if (!array_p) + return false; + + + // try to find declaration from current function + for (const auto &p : array_p->GetDeclInfo()) + { + if (p.first == current_file_name && + current_func->linesNum.first <= p.second && + p.second <= current_func->linesNum.second) + { + decl_place = p; + return true; + } + } + + for (const auto &p : array_p->GetDeclInfo()) + { + if (SgFile::switchToFile(p.first) != -1) + { + decl_place = p; + return true; + } + } + + return false; +} + +static bool checkAssumedSize(const string &array_name, DIST::Array* array_p, const string ¤t_file_name, FuncInfo *current_func) +{ + if (!array_p) + return true; // raise true to prevent array from copying + bool found = false; - DIST::Array* array_p = getArrayFromDeclarated(st, arrayName); + pair decl_place; - auto place = *array_p->GetDeclInfo().begin(); - auto decl_file_name = place.first; + if (!switchToDeclarationFile(array_p, decl_place, current_file_name, current_func)) + return true; - SgFile::switchToFile(decl_file_name); + auto *st = SgStatement::getStatementByFileAndLine(decl_place.first, decl_place.second); SgExpression* list = st->expr(0); while (list) { - if (list->lhs() && list->lhs()->symbol()->identifier() == arrayName) + if (list->lhs() && + list->lhs()->symbol() && + list->lhs()->symbol()->identifier() && + list->lhs()->symbol()->identifier() == array_name + ) { if(findExprWithVariant(list->lhs(), STAR_RANGE)) found = true; break; } + + list = list->rhs(); } if (!found) @@ -89,7 +117,8 @@ static bool checkAssumedSize(SgStatement *st, const string &arrayName, const str found = true; } - SgFile::switchToFile(currentFile); + if (SgFile::switchToFile(current_file_name) == -1) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); // can't switch back to file with array usage return found; } @@ -106,7 +135,10 @@ static void findArrays(SgExpression* exp, set& arrays) } } -static void populateDistributedIoArrays(map>& arrays, SgStatement* stat, const string& current_file) +static void populateDistributedIoArrays(map>& arrays, + SgStatement* stat, + const string& current_file_name, + FuncInfo *current_func) { auto var = stat->variant(); @@ -209,11 +241,13 @@ static void populateDistributedIoArrays(map>& array DIST::Array* array_p = getArrayFromDeclarated(declaratedInStmt(by_symb), array_name); if (array_p && array_p->GetDistributeFlagVal() == Distribution::distFlag::IO_PRIV && - !checkAssumedSize(declaratedInStmt(by_symb), array_name, current_file) && - arrays[by_symb].insert(stat).second + !checkAssumedSize(array_name, array_p, current_file_name, current_func) ) { - __spf_print(DEBUG_TRACE, "[%d]: add array %s\n", stat->lineNumber(), array_p->GetName().c_str()); + auto inserted = arrays[by_symb].insert(stat).second; + + if (inserted) + __spf_print(DEBUG_TRACE, "[%d]: add array %s %p\n", stat->lineNumber(), array_p->GetName().c_str(), by_symb); } } @@ -342,8 +376,7 @@ static void replaceArrayInFragment(SgSymbol* replace_symb, SgSymbol* replace_by, SgStatement* start, SgStatement* last, - FuncInfo *func_info, - const string& filename) + FuncInfo *func_info) { while (start->lexNext() && (!isSgExecutableStatement(start->lexNext()) || start->lexNext()->variant() == ALLOCATE_STMT)) start = start->lexNext(); @@ -453,9 +486,9 @@ static bool ioReginBorder(SgStatement* stat, SgStatement* last_io_bound) } 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_syms; @@ -464,14 +497,14 @@ void replaceDistributedArraysInIO(vector& regions, { __spf_print(DEBUG_TRACE, "[%s]: enter region\n", region->GetName().c_str()); - for (auto& linesByFile : region->GetAllLinesToModify()) + for (auto& lines_by_file : region->GetAllLinesToModify()) { - const auto& filename = linesByFile.first; + const auto& current_file_name = lines_by_file.first; - if (SgFile::switchToFile(filename) == -1) + if (SgFile::switchToFile(current_file_name) == -1) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - auto func_info_it = allFuncInfo.find(filename); + auto func_info_it = allFuncInfo.find(current_file_name); if (func_info_it == allFuncInfo.end()) { printInternalError(convertFileName(__FILE__).c_str(), __LINE__); @@ -481,9 +514,9 @@ void replaceDistributedArraysInIO(vector& regions, auto *lbound_symb = new SgSymbol(PROCEDURE_NAME, "lbound"); auto *ubound_symb = new SgSymbol(PROCEDURE_NAME, "ubound"); - for (auto& lines : linesByFile.second) + for (auto& lines : lines_by_file.second) { - __spf_print(DEBUG_TRACE, "[fragment] %s: %d:%d %d\n", filename.c_str(), lines.lines.first, + __spf_print(DEBUG_TRACE, "[fragment] %s: %d:%d %d\n", current_file_name.c_str(), lines.lines.first, lines.lines.second, lines.isImplicit()); SgStatement* curr_stmt, * end; @@ -555,90 +588,70 @@ void replaceDistributedArraysInIO(vector& regions, 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; + // at this point all considered arrays must have DIST::Array* references + if (!array_p) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + // ... and be declared in switchable files (not in headers) + pair decl_place; + if (!switchToDeclarationFile(array_p, decl_place, current_file_name, current_func_info)) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - auto place = *array_p->GetDeclInfo().begin(); - auto decl_file_name = place.first; string suffix = "_io_l"; - if (fromModule) - suffix = "_io_m"; - - pair copied; - copied.first = array_to_copy; - - if (SgFile::switchToFile(decl_file_name) == -1) + switch (array_p->GetLocation().first) { - 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()), decl_file_name, 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()); + case DIST::l_MODULE: + suffix = "_io_m"; + break; + case DIST::l_COMMON: + suffix = "_io_c"; + break; } - created_copies.insert({ array_to_copy, copied.second }); + auto copied_symbol = copyArray(decl_place, + array_p, + lines_by_file.second, + suffix + to_string(region->GetId()), + decl_place.first, + newDeclsToInclude, + copied_syms); + - // make array-copy allocatable in case of main array shape not constant + auto* decl_stmt = SgStatement::getStatementByFileAndLine(decl_place.first, decl_place.second); + + // created declaration statement located right after original one + if (!decl_stmt || !decl_stmt->lexNext()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + decl_stmt = decl_stmt->lexNext(); + + // insert !$SPF ANALYSIS(PROCESS_PRIVATE(array)) directive before declaration statement + + string dir_str; + if (decl_stmt->comments()) + { + auto str_comment = string(decl_stmt->comments()); + if (str_comment.size() && str_comment.back() != '\n') + dir_str += "\n"; + } + + dir_str += "!$SPF ANALYSIS(PROCESS_PRIVATE(" + string(copied_symbol.second->identifier()) + "))\n"; + decl_stmt->addComment(dir_str.c_str()); + + created_copies.insert({ array_to_copy, copied_symbol.second }); + + // make array copy allocatable in case of main array shape not constant if(checkDynamicArray(array_p)) { // insert allocatable keyword in declaration - auto *kword_list = decl->expr(2); - if (!findAlloatableKeyword(kword_list)) + auto *kword_list = decl_stmt->expr(2); + if (!findExprWithVariant(kword_list, ALLOCATABLE_OP)) { if (!kword_list) { kword_list = new SgExprListExp(); - decl->setExpression(2, *kword_list); + decl_stmt->setExpression(2, *kword_list); } while (kword_list->rhs()) @@ -653,29 +666,30 @@ void replaceDistributedArraysInIO(vector& regions, kword_list->setLhs(new SgExpression(ALLOCATABLE_OP)); } + // can't switch back to file with array usage + if (SgFile::switchToFile(current_file_name) == -1) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + // insert allocate(a_l(lbound(a, 1):ubound(a,1),...)) statement - SgFile::switchToFile(filename); - SgStatement* insertPlace = NULL; + SgStatement* allocate_stmt_place = NULL; auto* func_stmt = curr_stmt->getScopeForDeclare(); 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 && !isSgExecutableStatement(iterator); iterator = iterator->lexNext()) { - insertPlace = iterator; + allocate_stmt_place = iterator; } //NULL - no decl stats in function! - if (!insertPlace) - insertPlace = func_stmt; + if (!allocate_stmt_place) + allocate_stmt_place = func_stmt; - auto st = insertPlace->controlParent(); - if (st->variant() == GLOBAL) - st = insertPlace; + auto *allocate_stmt_parent = allocate_stmt_place->controlParent(); + if (allocate_stmt_parent->variant() == GLOBAL) + allocate_stmt_parent = allocate_stmt_place; - auto *stat = new SgStatement(ALLOCATE_STMT); - auto *created_array_ref = new SgArrayRefExp(*copied.second); + auto *created_array_ref = new SgArrayRefExp(*copied_symbol.second); auto* dim_list = new SgExprListExp(); created_array_ref->setLhs(dim_list); @@ -708,9 +722,10 @@ void replaceDistributedArraysInIO(vector& regions, } } - stat->setExpression(0, created_array_ref); + auto *allocate_stmt = new SgStatement(ALLOCATE_STMT); + allocate_stmt->setExpression(0, created_array_ref); - insertPlace->insertStmtAfter(*stat, *st); + allocate_stmt_place->insertStmtAfter(*allocate_stmt, *allocate_stmt_parent); // insert deallocate statemens before all returns auto *find_return_stmt = func_stmt; @@ -728,7 +743,7 @@ void replaceDistributedArraysInIO(vector& regions, auto *dealloc_stmt = new SgStatement(DEALLOCATE_STMT); dealloc_stmt->setExpression(0, new SgExprListExp()); - dealloc_stmt->expr(0)->setLhs(new SgArrayRefExp(*copied.second)); + dealloc_stmt->expr(0)->setLhs(new SgArrayRefExp(*copied_symbol.second)); find_return_stmt->insertStmtAfter(*dealloc_stmt, *next->controlParent()); @@ -740,9 +755,9 @@ void replaceDistributedArraysInIO(vector& regions, } } - SgFile::switchToFile(filename); - - + // can't switch back to file with array usage + if (SgFile::switchToFile(current_file_name) == -1) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); } } @@ -754,7 +769,7 @@ void replaceDistributedArraysInIO(vector& regions, { auto it = created_copies.find(p.first); if (it != created_copies.end()) - replaceArrayInFragment(p.first, p.second, it->second, last_io_bound, curr_stmt, current_func_info, filename); + replaceArrayInFragment(p.first, p.second, it->second, last_io_bound, curr_stmt, current_func_info); else printInternalError(convertFileName(__FILE__).c_str(), __LINE__); } @@ -774,7 +789,7 @@ void replaceDistributedArraysInIO(vector& regions, } } - populateDistributedIoArrays(need_replace, curr_stmt, filename); + populateDistributedIoArrays(need_replace, curr_stmt, current_file_name, current_func_info); curr_stmt = curr_stmt->lexNext(); } }