replace_dist_arrays_in_io: copy declorations from includes, fix replacement bounds, improve performance, fix SgUtils #55

Merged
Alexander_KS merged 1 commits from replace_dist_arrays_in_io into master 2024-12-05 09:47:55 +00:00
2 changed files with 168 additions and 110 deletions
Showing only changes of commit 7d342c51a8 - Show all commits

View File

@@ -5,6 +5,7 @@
#include <string>
#include <map>
#include <set>
#include <ExpressionTransform/expr_transform.h>
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<SgSymbol*>& arrays)
}
}
static void populateDistributedIoArrays(map<DIST::Array*, set<SgStatement*>>& arrays, SgStatement* stat)
static void populateDistributedIoArrays(map<SgSymbol*, set<SgStatement*>>& arrays, SgStatement* stat)
{
auto var = stat->variant();
@@ -122,7 +124,7 @@ static void populateDistributedIoArrays(map<DIST::Array*, set<SgStatement*>>& 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;
@@ -195,7 +197,7 @@ static void replaceArrayRec(SgSymbol* arr, SgSymbol* replace_by, SgStatement* st
}
}
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<SgStatement*> usages, SgSymbol* replace_by, SgStatement* start, SgStatement* last, const string& filename)
static void replaceArrayInFragment(SgSymbol* replace_symb, const set<SgStatement*> 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<SgStatement*> 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<SgStatement*> 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<SgStatement*> 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<SgStatement*> 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<int> 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,12 +335,8 @@ 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;
}
@@ -333,8 +346,8 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& regions,
map<string, vector<Messages>>& SPF_messages,
map<string, map<int, set<string>>>& newDeclsToInclude)
{
map<DIST::Array*, SgSymbol*> created_copies;
map<string, map<int, set<string>>> copied;
map<SgSymbol*, SgSymbol*> created_copies;
map<string, map<int, set<string>>> copied_syms;
for (auto& region : regions)
{
@@ -344,12 +357,14 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& 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<ParallelRegion*>& regions,
end = lines.stats.second->GetOriginal()->lexNext();
}
map<DIST::Array*, set<SgStatement*>> need_replace;
map<SgSymbol*, set<SgStatement*>> need_replace;
SgStatement* last_io_bound = NULL;
@@ -395,8 +410,92 @@ void replaceDistributedArraysInIO(vector<ParallelRegion*>& 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<SgSymbol*, SgSymbol*> 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<ParallelRegion*>& 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();
}
}

View File

@@ -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());
}