REMOVE_DIST_ARRAYS_FROM_IO: do not process arrays from headers, copy from correct declarations, improve style

This commit is contained in:
2025-09-13 20:48:24 +03:00
parent 16b2c6b42b
commit 3bc9351641

View File

@@ -17,20 +17,6 @@ using std::pair;
#define DEBUG_TRACE 0 #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) static bool checkDynamicArray(DIST::Array *array)
{ {
for (const auto &bounds : array->GetSizes()) for (const auto &bounds : array->GetSizes())
@@ -59,27 +45,69 @@ static SgExpression* findExprWithVariant(SgExpression* exp, int variant)
return NULL; return NULL;
} }
static bool checkAssumedSize(SgStatement *st, const string &arrayName, const string &currentFile) bool switchToDeclarationFile(DIST::Array* array_p,
pair<string, int>& decl_place,
const string &current_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 &current_file_name, FuncInfo *current_func)
{
if (!array_p)
return true; // raise true to prevent array from copying
bool found = false; bool found = false;
DIST::Array* array_p = getArrayFromDeclarated(st, arrayName); pair<string, int> decl_place;
auto place = *array_p->GetDeclInfo().begin(); if (!switchToDeclarationFile(array_p, decl_place, current_file_name, current_func))
auto decl_file_name = place.first; return true;
SgFile::switchToFile(decl_file_name); auto *st = SgStatement::getStatementByFileAndLine(decl_place.first, decl_place.second);
SgExpression* list = st->expr(0); SgExpression* list = st->expr(0);
while (list) 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)) if(findExprWithVariant(list->lhs(), STAR_RANGE))
found = true; found = true;
break; break;
} }
list = list->rhs();
} }
if (!found) if (!found)
@@ -89,7 +117,8 @@ static bool checkAssumedSize(SgStatement *st, const string &arrayName, const str
found = true; 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; return found;
} }
@@ -106,7 +135,10 @@ static void findArrays(SgExpression* exp, set<SgSymbol*>& arrays)
} }
} }
static void populateDistributedIoArrays(map<SgSymbol*, set<SgStatement*>>& arrays, SgStatement* stat, const string& current_file) static void populateDistributedIoArrays(map<SgSymbol*, set<SgStatement*>>& arrays,
SgStatement* stat,
const string& current_file_name,
FuncInfo *current_func)
{ {
auto var = stat->variant(); auto var = stat->variant();
@@ -209,11 +241,13 @@ static void populateDistributedIoArrays(map<SgSymbol*, set<SgStatement*>>& array
DIST::Array* array_p = getArrayFromDeclarated(declaratedInStmt(by_symb), array_name); DIST::Array* array_p = getArrayFromDeclarated(declaratedInStmt(by_symb), array_name);
if (array_p && if (array_p &&
array_p->GetDistributeFlagVal() == Distribution::distFlag::IO_PRIV && array_p->GetDistributeFlagVal() == Distribution::distFlag::IO_PRIV &&
!checkAssumedSize(declaratedInStmt(by_symb), array_name, current_file) && !checkAssumedSize(array_name, array_p, current_file_name, current_func)
arrays[by_symb].insert(stat).second
) )
{ {
__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, SgSymbol* replace_by,
SgStatement* start, SgStatement* start,
SgStatement* last, SgStatement* last,
FuncInfo *func_info, FuncInfo *func_info)
const string& filename)
{ {
while (start->lexNext() && (!isSgExecutableStatement(start->lexNext()) || start->lexNext()->variant() == ALLOCATE_STMT)) while (start->lexNext() && (!isSgExecutableStatement(start->lexNext()) || start->lexNext()->variant() == ALLOCATE_STMT))
start = start->lexNext(); start = start->lexNext();
@@ -464,14 +497,14 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& regions,
{ {
__spf_print(DEBUG_TRACE, "[%s]: enter region\n", region->GetName().c_str()); __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__); 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()) if (func_info_it == allFuncInfo.end())
{ {
printInternalError(convertFileName(__FILE__).c_str(), __LINE__); printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
@@ -481,9 +514,9 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& regions,
auto *lbound_symb = new SgSymbol(PROCEDURE_NAME, "lbound"); auto *lbound_symb = new SgSymbol(PROCEDURE_NAME, "lbound");
auto *ubound_symb = new SgSymbol(PROCEDURE_NAME, "ubound"); 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()); lines.lines.second, lines.isImplicit());
SgStatement* curr_stmt, * end; SgStatement* curr_stmt, * end;
@@ -555,90 +588,70 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& regions,
string array_name = string(array_to_copy->identifier()); string array_name = string(array_to_copy->identifier());
DIST::Array* array_p = getArrayFromDeclarated(declaratedInStmt(array_to_copy), array_name); DIST::Array* array_p = getArrayFromDeclarated(declaratedInStmt(array_to_copy), array_name);
bool fromModule = (array_p->GetLocation().first == DIST::l_MODULE); // at this point all considered arrays must have DIST::Array* references
const string locationName = array_p->GetLocation().second; if (!array_p)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
// ... and be declared in switchable files (not in headers)
pair<string, int> 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"; string suffix = "_io_l";
if (fromModule) switch (array_p->GetLocation().first)
{
case DIST::l_MODULE:
suffix = "_io_m"; suffix = "_io_m";
break;
pair<SgSymbol*, SgSymbol*> copied; case DIST::l_COMMON:
copied.first = array_to_copy; suffix = "_io_c";
break;
if (SgFile::switchToFile(decl_file_name) == -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! auto copied_symbol = copyArray(decl_place,
if (!insertPlace) array_p,
insertPlace = func_stmt; lines_by_file.second,
suffix + to_string(region->GetId()),
decl_place.first,
newDeclsToInclude,
copied_syms);
auto st = insertPlace->controlParent();
if (st->variant() == GLOBAL)
st = insertPlace;
auto& copied_symb = array_to_copy->copy(); auto* decl_stmt = SgStatement::getStatementByFileAndLine(decl_place.first, decl_place.second);
copied.second = &copied_symb;
auto new_name = string(array_to_copy->identifier()) + "_io_c"; // created declaration statement located right after original one
copied_symb.changeName(new_name.c_str()); if (!decl_stmt || !decl_stmt->lexNext())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
auto stat = array_to_copy->makeVarDeclStmt(); decl_stmt = decl_stmt->lexNext();
auto res = CalculateInteger(stat->expr(0)->copyPtr());
res->lhs()->setSymbol(copied_symb);
stat->setExpression(0, res);
insertPlace->insertStmtAfter(*stat, *st); // insert !$SPF ANALYSIS(PROCESS_PRIVATE(array)) directive before declaration statement
}
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; string dir_str;
if (decl->comments()) if (decl_stmt->comments())
{ {
string str_comment = string(decl->comments()); auto str_comment = string(decl_stmt->comments());
if (str_comment.size() && str_comment.back() != '\n') if (str_comment.size() && str_comment.back() != '\n')
dir_str += "\n"; dir_str += "\n";
} }
dir_str += "!$SPF ANALYSIS(PROCESS_PRIVATE(" + string(copied.second->identifier()) + "))\n"; dir_str += "!$SPF ANALYSIS(PROCESS_PRIVATE(" + string(copied_symbol.second->identifier()) + "))\n";
decl->addComment(dir_str.c_str()); decl_stmt->addComment(dir_str.c_str());
}
created_copies.insert({ array_to_copy, copied.second }); created_copies.insert({ array_to_copy, copied_symbol.second });
// make array-copy allocatable in case of main array shape not constant // make array copy allocatable in case of main array shape not constant
if(checkDynamicArray(array_p)) if(checkDynamicArray(array_p))
{ {
// insert allocatable keyword in declaration // insert allocatable keyword in declaration
auto *kword_list = decl->expr(2); auto *kword_list = decl_stmt->expr(2);
if (!findAlloatableKeyword(kword_list)) if (!findExprWithVariant(kword_list, ALLOCATABLE_OP))
{ {
if (!kword_list) if (!kword_list)
{ {
kword_list = new SgExprListExp(); kword_list = new SgExprListExp();
decl->setExpression(2, *kword_list); decl_stmt->setExpression(2, *kword_list);
} }
while (kword_list->rhs()) while (kword_list->rhs())
@@ -653,29 +666,30 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& regions,
kword_list->setLhs(new SgExpression(ALLOCATABLE_OP)); 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 // insert allocate(a_l(lbound(a, 1):ubound(a,1),...)) statement
SgFile::switchToFile(filename); SgStatement* allocate_stmt_place = NULL;
SgStatement* insertPlace = NULL;
auto* func_stmt = curr_stmt->getScopeForDeclare(); auto* func_stmt = curr_stmt->getScopeForDeclare();
for (auto iterator = func_stmt->lexNext(); for (auto iterator = func_stmt->lexNext();
!isSgExecutableStatement(iterator) || isSPF_stat(iterator) && iterator && !isSgExecutableStatement(iterator);
!(iterator->variant() == SPF_PARALLEL_REG_DIR || iterator->variant() == SPF_END_PARALLEL_REG_DIR);
iterator = iterator->lexNext()) iterator = iterator->lexNext())
{ {
insertPlace = iterator; allocate_stmt_place = iterator;
} }
//NULL - no decl stats in function! //NULL - no decl stats in function!
if (!insertPlace) if (!allocate_stmt_place)
insertPlace = func_stmt; allocate_stmt_place = func_stmt;
auto st = insertPlace->controlParent(); auto *allocate_stmt_parent = allocate_stmt_place->controlParent();
if (st->variant() == GLOBAL) if (allocate_stmt_parent->variant() == GLOBAL)
st = insertPlace; allocate_stmt_parent = allocate_stmt_place;
auto *stat = new SgStatement(ALLOCATE_STMT); auto *created_array_ref = new SgArrayRefExp(*copied_symbol.second);
auto *created_array_ref = new SgArrayRefExp(*copied.second);
auto* dim_list = new SgExprListExp(); auto* dim_list = new SgExprListExp();
created_array_ref->setLhs(dim_list); created_array_ref->setLhs(dim_list);
@@ -708,9 +722,10 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& 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 // insert deallocate statemens before all returns
auto *find_return_stmt = func_stmt; auto *find_return_stmt = func_stmt;
@@ -728,7 +743,7 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& regions,
auto *dealloc_stmt = new SgStatement(DEALLOCATE_STMT); auto *dealloc_stmt = new SgStatement(DEALLOCATE_STMT);
dealloc_stmt->setExpression(0, new SgExprListExp()); 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()); find_return_stmt->insertStmtAfter(*dealloc_stmt, *next->controlParent());
@@ -740,9 +755,9 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& 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<ParallelRegion*>& regions,
{ {
auto it = created_copies.find(p.first); auto it = created_copies.find(p.first);
if (it != created_copies.end()) 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 else
printInternalError(convertFileName(__FILE__).c_str(), __LINE__); printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
} }
@@ -774,7 +789,7 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& regions,
} }
} }
populateDistributedIoArrays(need_replace, curr_stmt, filename); populateDistributedIoArrays(need_replace, curr_stmt, current_file_name, current_func_info);
curr_stmt = curr_stmt->lexNext(); curr_stmt = curr_stmt->lexNext();
} }
} }