private_removing: add dead code removing

This commit is contained in:
2024-03-16 00:10:28 +03:00
parent c35fd07766
commit 1d82af702e
4 changed files with 92 additions and 89 deletions

View File

@@ -1106,11 +1106,13 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
{ {
auto itFound = loopGraph.find(file->filename()); auto itFound = loopGraph.find(file->filename());
if (itFound != loopGraph.end()) if (itFound != loopGraph.end())
removePrivatesAnalysis(itFound->second, getObjectForFileFromMap(file_name, SPF_messages), usersDirectives, commonBlocks, allFuncInfo); removePrivatesAnalysis(file_name, itFound->second, getObjectForFileFromMap(file_name, SPF_messages),
usersDirectives, commonBlocks, allFuncInfo);
} }
else if (curr_regime == PRIVATE_REMOVING) else if (curr_regime == PRIVATE_REMOVING)
{ {
removePrivates(file, getObjectForFileFromMap(file_name, SPF_messages), commonBlocks, allFuncInfo, countOfTransform); removePrivates(file_name, getObjectForFileFromMap(file_name, SPF_messages),
commonBlocks, allFuncInfo, countOfTransform);
} }
else if (curr_regime == CREATE_INTER_TREE) else if (curr_regime == CREATE_INTER_TREE)
{ {
@@ -2498,15 +2500,8 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam
case REMOVE_OMP_DIRS_TRANSFORM: case REMOVE_OMP_DIRS_TRANSFORM:
case REMOVE_COMMENTS: case REMOVE_COMMENTS:
case INSERT_NO_DISTR_FLAGS_FROM_GUI: case INSERT_NO_DISTR_FLAGS_FROM_GUI:
runAnalysis(*project, curr_regime, true, "", folderName);
break;
case PRIVATE_REMOVING: case PRIVATE_REMOVING:
runAnalysis(*project, curr_regime, false, "", folderName); runAnalysis(*project, curr_regime, true, "", folderName);
runPass(REVERT_SUBST_EXPR_RD, proj_name, folderName);
if (folderName)
runAnalysis(*project, UNPARSE_FILE, true, "", folderName);
else
__spf_print(1, "can not run UNPARSE_FILE - folder name is null\n");
break; break;
case INLINE_PROCEDURES: case INLINE_PROCEDURES:
runAnalysis(*project, curr_regime, false); runAnalysis(*project, curr_regime, false);

View File

@@ -599,38 +599,39 @@ static bool isVarChangedBetween(string var, SgStatement* first, SgStatement* sec
return false; return false;
} }
// TODO: remove if needless
// removeDeadCodeFromLoop removes assign statements to private scalar vars which are not read in loop // removeDeadCodeFromLoop removes assign statements to private scalar vars which are not read in loop
static void removeDeadCodeFromLoop(LoopGraph* loop) //static void removeDeadCodeFromLoop(LoopGraph* loop)
{ //{
SgForStmt* loopStmt = (SgForStmt*) loop->loop->GetOriginal(); // SgForStmt* loopStmt = (SgForStmt*) loop->loop->GetOriginal();
set<Symbol*> privateVars; // set<Symbol*> privateVars;
for (auto data : getAttributes<SgStatement*, SgStatement*>(loopStmt, set<int>{ SPF_ANALYSIS_DIR })) // for (auto data : getAttributes<SgStatement*, SgStatement*>(loopStmt, set<int>{ SPF_ANALYSIS_DIR }))
fillPrivatesFromComment(new Statement(data), privateVars); // fillPrivatesFromComment(new Statement(data), privateVars);
//
set<string> privates; // set<string> privates;
for (Symbol* symbol : privateVars) // for (Symbol* symbol : privateVars)
privates.insert(OriginalSymbol((SgSymbol*)symbol)->identifier()); // privates.insert(OriginalSymbol((SgSymbol*)symbol)->identifier());
//
vector<SgStatement*> stmtsToDelete; // vector<SgStatement*> stmtsToDelete;
for (SgStatement* st = loopStmt->lexNext(); st != loopStmt->lastNodeOfStmt(); st = st->lexNext()) // for (SgStatement* st = loopStmt->lexNext(); st != loopStmt->lastNodeOfStmt(); st = st->lexNext())
{ // {
if (st->variant() != ASSIGN_STAT) // if (st->variant() != ASSIGN_STAT)
continue; // continue;
//
SgSymbol* var = st->expr(0)->symbol(); // SgSymbol* var = st->expr(0)->symbol();
if (var == nullptr || var->variant() != VARIABLE_NAME) // if (var == nullptr || var->variant() != VARIABLE_NAME)
continue; // continue;
//
if (privates.find(var->identifier()) != privates.end() && !isVarReadInLoop(var, loopStmt)) // if (privates.find(var->identifier()) != privates.end() && !isVarReadInLoop(var, loopStmt))
stmtsToDelete.push_back(st); // stmtsToDelete.push_back(st);
} // }
//
for (auto stmt : stmtsToDelete) // for (auto stmt : stmtsToDelete)
stmt->deleteStmt(); // stmt->deleteStmt();
//
for (auto childLoop : loop->children) // for (auto childLoop : loop->children)
removeDeadCodeFromLoop(childLoop); // removeDeadCodeFromLoop(childLoop);
} //}
// fillReadShortFixedSumscripts fills all short fixed subscripts vectors of array var, // fillReadShortFixedSumscripts fills all short fixed subscripts vectors of array var,
// which are used for reading from array var in exp // which are used for reading from array var in exp
@@ -697,30 +698,31 @@ static void removeExcessiveDefs(const PrivateToRemove& var)
st->deleteStmt(); st->deleteStmt();
} }
// TODO: remove is needless
// removeEmptyLoops removes loops with empty body and create messages // removeEmptyLoops removes loops with empty body and create messages
static void removeEmptyLoops(LoopGraph* loop, vector<Messages>& messages) //static void removeEmptyLoops(LoopGraph* loop, vector<Messages>& messages)
{ //{
vector<LoopGraph*> loopsToDelete; // vector<LoopGraph*> loopsToDelete;
vector<LoopGraph*> newChildrenVector; // vector<LoopGraph*> newChildrenVector;
for (auto childLoop : loop->children) // for (auto childLoop : loop->children)
{ // {
SgStatement* loopStmt = childLoop->loop->GetOriginal(); // SgStatement* loopStmt = childLoop->loop->GetOriginal();
if (loopStmt->lastNodeOfStmt() == loopStmt->lexNext()) // if (loopStmt->lastNodeOfStmt() == loopStmt->lexNext())
loopsToDelete.push_back(childLoop); // loopsToDelete.push_back(childLoop);
else // else
newChildrenVector.push_back(childLoop); // newChildrenVector.push_back(childLoop);
} // }
//
for (auto loopToDelete : loopsToDelete) // for (auto loopToDelete : loopsToDelete)
{ // {
addMessageRemoveLoop(messages, loopToDelete->lineNum); // addMessageRemoveLoop(messages, loopToDelete->lineNum);
loopToDelete->loop->extractStmt(); // loopToDelete->loop->extractStmt();
} // }
//
loop->children.swap(newChildrenVector); // loop->children.swap(newChildrenVector);
for (auto childLoop : loop->children) // for (auto childLoop : loop->children)
removeEmptyLoops(childLoop, messages); // removeEmptyLoops(childLoop, messages);
} //}
// removeVarFromPrivateAttributes removes var from SPF ANALYSIS PRIVATE attributes of loop // removeVarFromPrivateAttributes removes var from SPF ANALYSIS PRIVATE attributes of loop
static void removeVarFromPrivateAttributes(SgSymbol* var, LoopGraph* loop) static void removeVarFromPrivateAttributes(SgSymbol* var, LoopGraph* loop)
@@ -820,39 +822,35 @@ static set<vector<int>> removeArray(string filename, const PrivateToRemove& arra
SgExpression* substExp = substituteExpressions(useStmt->expr(i), refToExpMap); SgExpression* substExp = substituteExpressions(useStmt->expr(i), refToExpMap);
useStmt->setExpression(i, *substExp); useStmt->setExpression(i, *substExp);
// don't revert substitutions in useStmt:
cancelRevertionForStatement(filename, useStmt, i);
} }
} }
return removedFixedSubscripts; return removedFixedSubscripts;
} }
void removePrivates(SgFile* file, vector<Messages>& messages, void removePrivates(string filename, vector<Messages>& messages,
const map<string, CommonBlock*>& commonBlocks, const map<string, CommonBlock*>& commonBlocks,
const map<string, vector<FuncInfo*>>& allFuncInfo, const map<string, vector<FuncInfo*>>& allFuncInfo,
int& countOfTransform) int& countOfTransform)
{ {
for (auto& varToRemove : privatesToRemoveGlobal) for (auto& varToRemove : privatesToRemoveGlobal)
{ {
if (string(file->filename()) != varToRemove.loop->fileName) if (filename != varToRemove.loop->fileName)
continue; continue;
auto removedDimensions = removeArray(file->filename(), varToRemove); auto removedDimensions = removeArray(filename, varToRemove);
countOfTransform++; countOfTransform++;
//removeDeadCodeFromLoop(varToRemove.loop); // TODO: problem with reverting substitution //removeDeadCodeFromLoop(varToRemove.loop);
removeExcessiveDefs(varToRemove); removeExcessiveDefs(varToRemove);
removeEmptyLoops(varToRemove.loop, messages); // removing is made by REMOVE_DEAD_CODE pass //removeEmptyLoops(varToRemove.loop, messages);
SgForStmt* loopStmt = (SgForStmt*)varToRemove.loop->loop->GetOriginal(); SgForStmt* loopStmt = (SgForStmt*)varToRemove.loop->loop->GetOriginal();
FuncInfo* currFunc = getCurrectFunc(loopStmt, allFuncInfo); FuncInfo* currFunc = getCurrectFunc(loopStmt, allFuncInfo);
if (currFunc == nullptr) if (currFunc == nullptr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__); printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
// TODO: problem with removing dead code before reverting substitution removeDeadCode(currFunc->funcPointer, allFuncInfo, commonBlocks);
//removeDeadCode(currFunc->funcPointer, allFuncInfo, commonBlocks);
vector<SgArrayRefExp*> varRefs = getDirectArrayRefs(loopStmt, varToRemove.varSymbol); vector<SgArrayRefExp*> varRefs = getDirectArrayRefs(loopStmt, varToRemove.varSymbol);
int loopLineNum = varToRemove.loop->lineNum; int loopLineNum = varToRemove.loop->lineNum;
@@ -904,6 +902,7 @@ struct Context {
const map<string, vector<FuncInfo*>>& allFuncInfo; const map<string, vector<FuncInfo*>>& allFuncInfo;
const map<string, CommonBlock*>& commonBlocks; const map<string, CommonBlock*>& commonBlocks;
vector<Messages>& messages; vector<Messages>& messages;
string filename;
Regime regime; Regime regime;
LoopGraph* loop; LoopGraph* loop;
@@ -2083,9 +2082,19 @@ void removePrivateAnalyze(Context *ctx)
{ {
vector<DefUseStmtsPair> resultDefUsePairs; vector<DefUseStmtsPair> resultDefUsePairs;
for (auto& pair : defUseStmtsPairs) for (auto& pair : defUseStmtsPairs)
{
if (checkDefUsePair(ctx, pair, CFG_ForFunc)) if (checkDefUsePair(ctx, pair, CFG_ForFunc))
{
resultDefUsePairs.push_back(pair); resultDefUsePairs.push_back(pair);
// don't revert substitutions in use and def stmts:
cancelRevertionForStatement(ctx->filename, pair.first, 1);
cancelRevertionForStatement(ctx->filename, pair.second, 0);
cancelRevertionForStatement(ctx->filename, pair.second, 1);
cancelRevertionForStatement(ctx->filename, pair.second, 2);
}
}
PrivateToRemove newPrivateToRemove; PrivateToRemove newPrivateToRemove;
newPrivateToRemove.loop = ctx->loop; newPrivateToRemove.loop = ctx->loop;
newPrivateToRemove.varSymbol = ctx->arraySymbol; newPrivateToRemove.varSymbol = ctx->arraySymbol;
@@ -2104,7 +2113,8 @@ void removePrivateAnalyze(Context *ctx)
deleteCFG(CFG_ForFunc); deleteCFG(CFG_ForFunc);
} }
void removePrivatesAnalysis(vector<LoopGraph*>& loopGraphs, void removePrivatesAnalysis(string filename,
vector<LoopGraph*>& loopGraphs,
vector<Messages>& messages, vector<Messages>& messages,
const UsersDirectives& usersDirectives, const UsersDirectives& usersDirectives,
const map<string, CommonBlock*>& commonBlocks, const map<string, CommonBlock*>& commonBlocks,
@@ -2151,7 +2161,7 @@ void removePrivatesAnalysis(vector<LoopGraph*>& loopGraphs,
if (arrayToRemove == nullptr) // no array to remove if (arrayToRemove == nullptr) // no array to remove
break; break;
Context context = Context{allFuncInfo, commonBlocks, messages}; Context context = Context{allFuncInfo, commonBlocks, messages, filename};
context.regime = Regime::DEFLT; context.regime = Regime::DEFLT;
context.loop = loop; context.loop = loop;
context.loopStmt = loopStmt; context.loopStmt = loopStmt;
@@ -2173,20 +2183,17 @@ void removePrivatesAnalysis(vector<LoopGraph*>& loopGraphs,
{ {
removePrivateAnalyze(&context); removePrivateAnalyze(&context);
} }
else else if (checkRegularIndexRefs(&context))
{ {
if (checkRegularIndexRefs(&context)) context.regime = Regime::REGULAR_INDEXES;
{ context.fixedDimensionsMask = vector<bool>{};
context.regime = Regime::REGULAR_INDEXES; removePrivateAnalyze(&context);
context.fixedDimensionsMask = vector<bool>{};
removePrivateAnalyze(&context);
}
else
addMessageDoesNotMatchMask(messages, context.arraySymbol->identifier(), context.loop->lineNum);
} }
else
addMessageDoesNotMatchMask(messages, context.arraySymbol->identifier(), context.loop->lineNum);
} }
} }
for (LoopGraph* loop : loopGraphs) for (LoopGraph* loop : loopGraphs)
removePrivatesAnalysis(loop->children, messages, usersDirectives, commonBlocks, allFuncInfo); removePrivatesAnalysis(filename, loop->children, messages, usersDirectives, commonBlocks, allFuncInfo);
} }

View File

@@ -22,7 +22,7 @@ struct PrivateToRemove {
// removePrivates removes all privates from vector privatesToRemoveGloval // removePrivates removes all privates from vector privatesToRemoveGloval
// and add info messages // and add info messages
void removePrivates(SgFile* file, std::vector<Messages>& messages, void removePrivates(std::string filename, std::vector<Messages>& messages,
const std::map<std::string, CommonBlock*>& commonBlocks, const std::map<std::string, CommonBlock*>& commonBlocks,
const std::map<std::string, std::vector<FuncInfo*>>& allFuncInfo, const std::map<std::string, std::vector<FuncInfo*>>& allFuncInfo,
int& countOfTransform); int& countOfTransform);
@@ -30,7 +30,8 @@ void removePrivates(SgFile* file, std::vector<Messages>& messages,
// removePrivatesAnalysis checks all private variables in loopGraphs specified by usersDirectives // removePrivatesAnalysis checks all private variables in loopGraphs specified by usersDirectives
// if they can be removed, and adds those that can to vector privatesToRemoveGloval. // if they can be removed, and adds those that can to vector privatesToRemoveGloval.
// if private var cannot be removed, the error message is returned with vector messages // if private var cannot be removed, the error message is returned with vector messages
void removePrivatesAnalysis(std::vector<LoopGraph*>& loopGraphs, void removePrivatesAnalysis(std::string filename,
std::vector<LoopGraph*>& loopGraphs,
std::vector<Messages>& messages, std::vector<Messages>& messages,
const std::map<std::pair<std::string, int>, std::set<SgStatement*>>& usersDirectives, const std::map<std::pair<std::string, int>, std::set<SgStatement*>>& usersDirectives,
const std::map<std::string, CommonBlock*>& commonBlocks, const std::map<std::string, CommonBlock*>& commonBlocks,

View File

@@ -212,7 +212,7 @@ void InitPassesDependencies(map<passes, vector<passes>> &passDepsIn, set<passes>
Pass(BUILD_IR) <= Pass(SUBST_EXPR_RD) <= Pass(SUBST_EXPR_RD_AND_UNPARSE); Pass(BUILD_IR) <= Pass(SUBST_EXPR_RD) <= Pass(SUBST_EXPR_RD_AND_UNPARSE);
list({ LOOP_ANALYZER_DATA_DIST_S1, SUBST_EXPR_RD } ) <= Pass(PRIVATE_REMOVING_ANALYSIS) <= Pass(PRIVATE_REMOVING); list({ LOOP_ANALYZER_DATA_DIST_S1, SUBST_EXPR_RD } ) <= Pass(PRIVATE_REMOVING_ANALYSIS) <= Pass(REVERT_SUBST_EXPR_RD) <= Pass(PRIVATE_REMOVING);
Pass(RESTORE_LOOP_FROM_ASSIGN) <= list({ SUBST_EXPR_AND_UNPARSE, SUBST_EXPR_RD_AND_UNPARSE }); Pass(RESTORE_LOOP_FROM_ASSIGN) <= list({ SUBST_EXPR_AND_UNPARSE, SUBST_EXPR_RD_AND_UNPARSE });