2 Commits

Author SHA1 Message Date
d0f5c73fb2 private_removing: some small fixes 2024-06-09 13:08:08 +03:00
9d74330aba private_removing: small fix 2024-06-08 21:19:33 +03:00
2 changed files with 95 additions and 142 deletions

View File

@@ -20,11 +20,12 @@ using UsersDirectives = map<pair<string, int>, set<SgStatement*>>;
// RegularExpr represents expressions like ( coefA * I + coefB ),
// where I is a variable and coefA or coefB can be equal to zero
struct RegularExpr {
int coefA = 0;
int coefA;
int coefB;
string var;
SgSymbol* varSymbol;
RegularExpr(): coefA(0), coefB(0), var("") {}
RegularExpr(): coefA(0), coefB(0), var(""), varSymbol(nullptr) {}
string toString() const
{
@@ -218,7 +219,10 @@ static bool checkAndFillRegularExpr(SgExpression* expr, RegularExpr& regularExpr
regularExpr.coefA = retCoefs.first;
regularExpr.coefB = retCoefs.second;
if (!deleteTmpVar)
{
regularExpr.var = iterationVar->identifier();
regularExpr.varSymbol = iterationVar;
}
if (deleteTmpVar)
delete iterationVar;
@@ -236,7 +240,7 @@ static bool checkAndFillRegularExpr(SgExpression* expr, RegularExpr& regularExpr
// getShortFixedSubscriptsVector returns vector of fixed INT_VAL subscripts of arrayRef
static vector<int> getShortFixedSubscriptsVector(SgArrayRefExp* arrayRef,
const vector<bool>& fixedDimensionsMask,
Regime regime, vector<SgSymbol*> iterationVars)
Regime regime)
{
if (regime == Regime::DEFLT)
{
@@ -251,14 +255,12 @@ static vector<int> getShortFixedSubscriptsVector(SgArrayRefExp* arrayRef,
{
vector<int> subscriptsVector;
SgExprListExp* indexExprList = (SgExprListExp*)arrayRef->lhs();
if (iterationVars.size() < indexExprList->length())
return vector<int>{};
for (int i = 0; i < indexExprList->length(); ++i)
{
SgExpression* indexExpr = indexExprList->elem(i);
RegularExpr regularExpr;
if (!checkAndFillRegularExpr(indexExpr, regularExpr, iterationVars[i]))
if (!checkAndFillRegularExpr(indexExpr, regularExpr, nullptr))
return vector<int>{};
subscriptsVector.push_back(regularExpr.coefA);
@@ -271,38 +273,17 @@ static vector<int> getShortFixedSubscriptsVector(SgArrayRefExp* arrayRef,
return vector<int>{};
}
static vector<int> getShortFixedSubscriptsVector(SgArrayRefExp* arrayRef, const PrivateToRemove& varToRemove)
{
vector<SgSymbol*> iterationVars;
if (varToRemove.regime == Regime::REGULAR_INDEXES)
{
auto vars = varToRemove.arrayRefToIterationVarsMap.find(arrayRef);
if (vars != varToRemove.arrayRefToIterationVarsMap.end())
iterationVars = vars->second;
}
return getShortFixedSubscriptsVector(arrayRef, varToRemove.fixedDimensions,
varToRemove.regime, iterationVars);
}
// removeDuplicateArrayRefs returns unique array refereces in fixed dimensions
static vector<SgArrayRefExp*> removeDuplicateArrayRefs(const vector<SgArrayRefExp*>& arrayRefs,
const vector<bool>& fixedDimensionsMask,
Regime regime,
const map<SgArrayRefExp*, vector<SgSymbol*>>& arrayRefToIterVarsMap)
Regime regime)
{
map<vector<int>, SgArrayRefExp*> uniqueRefs;
for (SgArrayRefExp* arrayRef : arrayRefs)
{
vector<SgSymbol*> iterationVars;
if (regime == Regime::REGULAR_INDEXES)
{
auto vars = arrayRefToIterVarsMap.find(arrayRef);
if (vars != arrayRefToIterVarsMap.end())
iterationVars = vars->second;
}
vector<int> subscripts = getShortFixedSubscriptsVector(arrayRef, fixedDimensionsMask, regime, iterationVars);
vector<int> subscripts = getShortFixedSubscriptsVector(arrayRef, fixedDimensionsMask, regime);
if (uniqueRefs.find(subscripts) == uniqueRefs.end())
uniqueRefs.insert(make_pair(subscripts, arrayRef));
}
@@ -330,6 +311,7 @@ static bool isSymbolInExpression(SgSymbol* symbol, SgExpression* exp)
isSymbolInExpression(symbol, exp->rhs());
}
// findFuncByName searches function by its name among all functions (and subroutines) in program
static FuncInfo* findFuncByName(string funcName, const map<string, vector<FuncInfo*>>& allFuncInfo)
{
for (const auto& fileFuncs : allFuncInfo)
@@ -340,6 +322,7 @@ static FuncInfo* findFuncByName(string funcName, const map<string, vector<FuncIn
return nullptr;
}
// getCurrentFunc return FuncInfo about current function for stmt
static FuncInfo* getCurrentFunc(SgStatement* stmt, const map<string, vector<FuncInfo*>>& allFuncInfo)
{
auto fileInfo = allFuncInfo.find(stmt->fileName());
@@ -354,8 +337,9 @@ static FuncInfo* getCurrentFunc(SgStatement* stmt, const map<string, vector<Func
return nullptr;
}
// fillIterationVariables fill vars set with iteration variables of all loops
// from stmt to outerLoopStmt
// fillIterationVariables fills SgSymbol* set with iteration variables of all loops
// from stmt to outerLoopStmt. If outerLoopStmt is nullptr, it fill with iteration variables
// from all outer loops
static void fillIterationVars(SgStatement* stmt, SgStatement* outerLoopStmt, vector<SgSymbol*>& vars)
{
if (stmt == nullptr)
@@ -364,7 +348,7 @@ static void fillIterationVars(SgStatement* stmt, SgStatement* outerLoopStmt, vec
if (stmt->variant() == FOR_NODE)
vars.push_back(((SgForStmt*)stmt)->doName());
if (stmt->id() != outerLoopStmt->id())
if (stmt != outerLoopStmt)
fillIterationVars(stmt->controlParent(), outerLoopStmt, vars);
}
@@ -658,6 +642,11 @@ static bool isVarChangedBetween(string var, SgStatement* first, SgStatement* sec
return false;
}
static vector<int> getShortFixedSubscriptsVector(SgArrayRefExp* arrayRef, const PrivateToRemove& varToRemove)
{
return getShortFixedSubscriptsVector(arrayRef, varToRemove.fixedDimensions, varToRemove.regime);
}
// fillReadShortFixedSumscripts fills all short fixed subscripts vectors of array var,
// which are used for reading from array var in exp
static void fillReadShortFixedSubscripts(SgExpression* exp, const PrivateToRemove& var,
@@ -764,12 +753,27 @@ static void removeVarFromPrivateAttributes(SgSymbol* var, LoopGraph* loop)
// getVarToExpMap returns map SgSymbol* from defRef -> SgExpression* from useRef
static map<SgSymbol*, SgExpression*> getVarToExpMap(SgArrayRefExp* defRef, SgArrayRefExp* useRef,
const vector<bool>& fixedDimensions)
const vector<bool>& fixedDimensions, Regime regime)
{
map<SgSymbol*, SgExpression*> varToExpMap;
for (int i = 0; i < fixedDimensions.size(); ++i)
if (!fixedDimensions[i])
{
if (fixedDimensions[i])
continue;
// check not fixed dimension:
if (regime == Regime::REGULAR_INDEXES)
{
RegularExpr useExpr;
checkAndFillRegularExpr(useRef->subscript(i), useExpr, nullptr);
RegularExpr defExpr;
checkAndFillRegularExpr(defRef->subscript(i), defExpr, nullptr);
varToExpMap.insert(make_pair(defExpr.varSymbol, new SgVarRefExp(useExpr.varSymbol)));
}
else
varToExpMap.insert(make_pair(defRef->subscript(i)->symbol(), useRef->subscript(i)));
}
return varToExpMap;
}
@@ -780,19 +784,6 @@ static set<vector<int>> removeArray(string filename, PrivateToRemove& arrayToRem
{
set<vector<int>> removedFixedSubscripts;
// again fill itaration vars:
arrayToRemove.arrayRefToIterationVarsMap.clear();
SgStatement* loopStmt = arrayToRemove.loop->loop->GetOriginal();
for (SgStatement* st = loopStmt->lexNext(); st != loopStmt->lastNodeOfStmt(); st = st->lexNext())
{
vector<SgSymbol*> iterationVars;
fillIterationVars(st, loopStmt, iterationVars);
vector<SgArrayRefExp*> arrayRefs = getDirectArrayRefsFromSingleStmt(st, arrayToRemove.varSymbol);
for (SgArrayRefExp* arrayRef : arrayRefs)
arrayToRemove.arrayRefToIterationVarsMap.insert(make_pair(arrayRef, iterationVars));
}
auto& fixedDimensions = arrayToRemove.fixedDimensions;
for (auto& defUsePair : arrayToRemove.defUseStmtsPairs)
{
@@ -823,7 +814,7 @@ static set<vector<int>> removeArray(string filename, PrivateToRemove& arrayToRem
removedFixedSubscripts.insert(useFixedSubscripts);
auto varToExpMap = getVarToExpMap(defRef, useRef, fixedDimensions);
auto varToExpMap = getVarToExpMap(defRef, useRef, fixedDimensions, arrayToRemove.regime);
SgExpression* expToSubst = defStmt->rhs()->copyPtr();
expToSubst = replaceVarsWithExps(expToSubst, varToExpMap);
@@ -867,8 +858,7 @@ void removePrivates(string filename, vector<Messages>& messages,
}
else
{
varRefs = removeDuplicateArrayRefs(varRefs, fixedDimensions, varToRemove.regime,
varToRemove.arrayRefToIterationVarsMap);
varRefs = removeDuplicateArrayRefs(varRefs, fixedDimensions, varToRemove.regime);
for (auto& varRef : varRefs)
{
vector<int> subscripts = getShortFixedSubscriptsVector(varRef, varToRemove);
@@ -891,6 +881,7 @@ void removePrivates(string filename, vector<Messages>& messages,
}
}
// remove dead code from loop:
for (auto& dcLoopRem : removeDC)
{
auto loopStmt = dcLoopRem->loop->GetOriginal();
@@ -927,7 +918,6 @@ struct Context {
int dimensionsNum;
vector<SgArrayRefExp*> explicitArrayRefs;
vector<bool> fixedDimensionsMask;
map<SgArrayRefExp*, vector<SgSymbol*>> arrayRefToIterationVarsMap;
};
// ReducedArrayVars represents mapping of array reference to reduced scalar var:
@@ -994,15 +984,7 @@ static vector<InsertedStatement>::const_iterator findInsertedStmt(const vector<I
static vector<int> getShortFixedSubscriptsVector(Context* ctx, SgArrayRefExp* arrayRef)
{
vector<SgSymbol*> iterationVars;
if (ctx->regime == Regime::REGULAR_INDEXES)
{
auto vars = ctx->arrayRefToIterationVarsMap.find(arrayRef);
if (vars != ctx->arrayRefToIterationVarsMap.end())
iterationVars = vars->second;
}
return getShortFixedSubscriptsVector(arrayRef, ctx->fixedDimensionsMask, ctx->regime, iterationVars);
return getShortFixedSubscriptsVector(arrayRef, ctx->fixedDimensionsMask, ctx->regime);
}
// getLoopsInfo return vector of pair (string, int) - doName and level for each loop
@@ -1224,26 +1206,34 @@ static bool checkRegularIndexRefs(Context* ctx)
if (!isArrayRefInVector(arrayRef, ctx->explicitArrayRefs))
continue;
// check if unfixed dimension index contains iteration var:
SgExprListExp* indexExprList = (SgExprListExp*)arrayRef->lhs();
if (iterationVars.size() < indexExprList->length())
return false;
for (int i = 0; i < indexExprList->length(); ++i)
{
if (ctx->fixedDimensionsMask[i])
continue;
SgExpression* indexExpr = indexExprList->elem(i);
RegularExpr regularExpr;
if (!checkAndFillRegularExpr(indexExpr, regularExpr, iterationVars[i]))
if (!checkAndFillRegularExpr(indexExpr, regularExpr, nullptr))
return false;
if (regularExpr.coefA == 0)
return false;
bool isIterationVar = false;
for (SgSymbol* iterationVar : iterationVars)
{
if (iterationVar->identifier() == regularExpr.var)
{
isIterationVar = true;
break;
}
}
// TODO: possibly can be removed:
if (st->variant() == ASSIGN_STAT && isEqSymbols(st->expr(0)->symbol(), ctx->arraySymbol))
for (auto iterationVar : iterationVars)
if (isSymbolInExpression(iterationVar, st->expr(1)))
if (!isIterationVar)
return false;
iterationVars.resize(indexExprList->length());
ctx->arrayRefToIterationVarsMap.insert(make_pair(arrayRef, iterationVars));
}
}
}
@@ -1521,7 +1511,8 @@ static vector<vector<ArraySubscript>> checkIndirectUsage(Context* ctx)
if (currentFunc == nullptr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
vector<Variable*> commonBlockGroupedVar = getCommonBlockGroupedVar(currentFunc, ctx->arraySymbol, ctx->commonBlocks);
const auto& blocks = ctx->commonBlocks;
vector<Variable*> commonBlockGroupedVar = getCommonBlockGroupedVar(currentFunc, ctx->arraySymbol, blocks);
if (commonBlockGroupedVar.empty())
return indirectUsageMasks;
@@ -1622,9 +1613,11 @@ static ReducedArrayVarsMap getReducedArrayVars(Context* ctx)
{
string name = getReducedArrayVarName(arrayRef->symbol(), subscripts);
if (checkSymbNameAndCorrect(name, "_") != name)
{
int nameNumber = checkSymbNameAndCorrect(name + "__", 0);
if (nameNumber != 0)
name = name + "__" + std::to_string(nameNumber);
}
SgSymbol* newSymbol = new SgSymbol(VARIABLE_NAME, name.c_str(), type, scope);
reducedArrayVars.insert(subscripts, newSymbol);
@@ -1699,8 +1692,7 @@ static vector<InsertedStatement> insertReducedArrayVarStmts(Context* ctx,
{
vector<SgArrayRefExp*> arrayRefs;
fillDirectArrayRefs(st->expr(i), ctx->arraySymbol, arrayRefs);
arrayRefs = removeDuplicateArrayRefs(arrayRefs, ctx->fixedDimensionsMask, ctx->regime,
ctx->arrayRefToIterationVarsMap);
arrayRefs = removeDuplicateArrayRefs(arrayRefs, ctx->fixedDimensionsMask, ctx->regime);
if (!arrayRefs.empty())
isUseStmt = true;
@@ -1773,7 +1765,7 @@ static vector<DefUseStmtsPair> buildDefUsePairs(Context* ctx, const CFG_Type& CF
set<int> RD_defArgs = RD_forUseArg->second; // make copy
// delete recursive and uninit definition from RD def args:
// delete recursive, uninit and definitions that cannot reach use stmt from RD def args:
set<int> tmpRD_defArgs;
for (int defArgNum : RD_defArgs)
{
@@ -1786,46 +1778,19 @@ static vector<DefUseStmtsPair> buildDefUsePairs(Context* ctx, const CFG_Type& CF
SgStatement* defStmt = defInsAndBlock.first->getOperator();
auto defInsertedStmt = findInsertedStmt(insertedStmts, defStmt);
if (useInsertedStmt.relatedToStmt == defInsertedStmt->relatedToStmt)
if (useLineNum <= defInsertedStmt->relatedToStmt->lineNumber())
continue;
tmpRD_defArgs.insert(defArgNum);
}
RD_defArgs.swap(tmpRD_defArgs);
if (RD_defArgs.size() == 0) // argument is not initialized
{
addMessageCannotFindRD(ctx->messages, arrayName, useLineNum);
continue;
}
SgStatement* defStmt = nullptr;
if (RD_defArgs.size() > 1)
if (RD_defArgs.size() != 1)
{
bool defIsFound = false;
for (int defArg : RD_defArgs) // try to find the real definition from RD_defArgs
{
if (defArg == SAPFOR::CFG_VAL::UNINIT)
continue;
auto defInsAndBlock = getInstructionAndBlockByNumber(CFGraph, defArg);
if (defInsAndBlock.first == nullptr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
auto defInsertedStmt = findInsertedStmt(insertedStmts, defInsAndBlock.first->getOperator());
if (defInsertedStmt->relatedToStmt->lineNumber() < useLineNum &&
useInsAndBlock.second->getNumber() == defInsAndBlock.second->getNumber())
{
defIsFound = true;
auto defInsAndBlock = getInstructionAndBlockByNumber(CFGraph, defArg);
defStmt = defInsAndBlock.first->getOperator();
break;
}
}
if (!defIsFound)
{
// try to find definition not from RD_defArgs, by search in the block instructions:
string defVarName = useInsertedStmt.insertedStmt->expr(1)->symbol()->identifier();
const auto& blockInstructionsVector = useInsAndBlock.second->getInstructions();
@@ -1844,7 +1809,6 @@ static vector<DefUseStmtsPair> buildDefUsePairs(Context* ctx, const CFG_Type& CF
break;
}
}
}
if (!defIsFound)
{
@@ -1883,15 +1847,6 @@ static vector<DefUseStmtsPair> buildDefUsePairs(Context* ctx, const CFG_Type& CF
return defUsePairs;
}
//// getScopeLoopStmt returns least outer scope loop statement
//static SgForStmt* getScopeLoopStmt(SgStatement* stmt)
//{
// while (stmt != nullptr && stmt->variant() != FOR_NODE)
// stmt = stmt->controlParent();
//
// return (SgForStmt*)stmt;
//}
// findChildLoop returns LoopGraph for provided loop statement
static LoopGraph* findLoop(LoopGraph* outerLoop, SgForStmt* loopStmt)
{
@@ -2062,7 +2017,8 @@ static bool areDifferentRefs(Context* ctx, SgExpression* exp, const pair<string,
return false;
}
static pair<SAPFOR::Argument*, set<int>> findVarInRDSet(const map<SAPFOR::Argument*, set<int>>& RD_In, const string& var)
static pair<SAPFOR::Argument*, set<int>> findVarInRDSet(const map<SAPFOR::Argument*, set<int>>& RD_In,
const string& var)
{
for (auto& RD_InElem : RD_In)
{
@@ -2092,7 +2048,9 @@ static bool checkDefUsePair(Context* ctx, const DefUseStmtsPair& defUse, const C
vector<SgArrayRefExp*> arrayUseRefs = getDirectArrayRefsFromSingleStmt(defUse.second, ctx->arraySymbol);
for (auto useRef : arrayUseRefs)
{
map<SgSymbol*, SgExpression*> varToExpMap = getVarToExpMap(defRef, useRef, ctx->fixedDimensionsMask);
map<SgSymbol*, SgExpression*> varToExpMap;
varToExpMap = getVarToExpMap(defRef, useRef, ctx->fixedDimensionsMask, ctx->regime);
SgExpression* expToSubst = defUse.first->rhs()->copyPtr();
expToSubst = replaceVarsWithExps(expToSubst, varToExpMap);
@@ -2100,7 +2058,7 @@ static bool checkDefUsePair(Context* ctx, const DefUseStmtsPair& defUse, const C
}
vector<SgSymbol*> iterationVars{};
fillIterationVars(defUse.second, ctx->loopStmt, iterationVars);
fillIterationVars(defUse.second, nullptr, iterationVars);
auto defInsAndBlock = getInstructionAndBlockByStatement(CFGraph, defUse.first);
const auto& defRD_In = defInsAndBlock.second->getRD_In();
@@ -2281,7 +2239,6 @@ void removePrivateAnalyze(Context *ctx)
newPrivateToRemove.regime = ctx->regime;
newPrivateToRemove.defUseStmtsPairs.swap(resultDefUsePairs);
newPrivateToRemove.fixedDimensions.swap(ctx->fixedDimensionsMask);
newPrivateToRemove.arrayRefToIterationVarsMap = ctx->arrayRefToIterationVarsMap;
privatesToRemoveGlobal.push_back(newPrivateToRemove);
}
@@ -2348,6 +2305,8 @@ void removePrivatesAnalysis(string filename,
context.dimensionsNum = ((SgArrayType*)arrayToRemove->type())->dimension();
context.arraySymbol = arrayToRemove;
string arrayName = arrayToRemove->identifier();
auto filterMasks = checkImplicitAndIndirectUsage(&context);
filterArrayRefs(&context, arrayRefs, filterMasks);
@@ -2357,8 +2316,7 @@ void removePrivatesAnalysis(string filename,
if (!checkLoopAlignmentMatching(&context))
{
addMessageVarNotAlignedWithLoop(messages, context.arraySymbol->identifier(),
context.loop->lineNum);
addMessageVarNotAlignedWithLoop(messages, arrayName, context.loop->lineNum);
continue;
}
@@ -2373,14 +2331,10 @@ void removePrivatesAnalysis(string filename,
else if (checkRegularIndexRefs(&context))
{
context.regime = Regime::REGULAR_INDEXES;
context.fixedDimensionsMask = vector<bool>{};
removePrivateAnalyze(&context);
}
else
{
addMessageDoesNotMatchMask(messages, context.arraySymbol->identifier(),
context.loop->lineNum);
}
addMessageDoesNotMatchMask(messages, arrayName, context.loop->lineNum);
}
}

View File

@@ -17,7 +17,6 @@ struct PrivateToRemove {
Regime regime;
std::vector<std::pair<SgAssignStmt*, SgStatement*>> defUseStmtsPairs;
std::vector<bool> fixedDimensions;
std::map<SgArrayRefExp*, std::vector<SgSymbol*>> arrayRefToIterationVarsMap;
};
// removePrivates removes all privates from vector privatesToRemoveGloval