From 9d74330aba14b68df67a2cdcae6fe192c78b3246 Mon Sep 17 00:00:00 2001 From: Grigorii Gusev Date: Fri, 19 Apr 2024 17:52:00 +0300 Subject: [PATCH 1/2] private_removing: small fix --- .../_src/Transformations/private_removing.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp index 58bb712..fe6278c 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp @@ -354,8 +354,9 @@ static FuncInfo* getCurrentFunc(SgStatement* stmt, const map& vars) { if (stmt == nullptr) @@ -364,7 +365,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); } @@ -2100,7 +2101,7 @@ static bool checkDefUsePair(Context* ctx, const DefUseStmtsPair& defUse, const C } vector 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(); From 6022ed11e8ae66d5b27bf5369386fae443c7554e Mon Sep 17 00:00:00 2001 From: Grigorii Gusev Date: Sat, 8 Jun 2024 21:18:50 +0300 Subject: [PATCH 2/2] private_removing: some small fixes --- .../_src/Transformations/private_removing.cpp | 229 +++++++----------- .../_src/Transformations/private_removing.h | 1 - 2 files changed, 90 insertions(+), 140 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp index fe6278c..06d7453 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp @@ -20,11 +20,12 @@ using UsersDirectives = map, set>; // 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 getShortFixedSubscriptsVector(SgArrayRefExp* arrayRef, const vector& fixedDimensionsMask, - Regime regime, vector iterationVars) + Regime regime) { if (regime == Regime::DEFLT) { @@ -251,14 +255,12 @@ static vector getShortFixedSubscriptsVector(SgArrayRefExp* arrayRef, { vector subscriptsVector; SgExprListExp* indexExprList = (SgExprListExp*)arrayRef->lhs(); - if (iterationVars.size() < indexExprList->length()) - return vector{}; 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{}; subscriptsVector.push_back(regularExpr.coefA); @@ -271,38 +273,15 @@ static vector getShortFixedSubscriptsVector(SgArrayRefExp* arrayRef, return vector{}; } -static vector getShortFixedSubscriptsVector(SgArrayRefExp* arrayRef, const PrivateToRemove& varToRemove) -{ - vector 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 removeDuplicateArrayRefs(const vector& arrayRefs, const vector& fixedDimensionsMask, - Regime regime, - const map>& arrayRefToIterVarsMap) + Regime regime) { map, SgArrayRefExp*> uniqueRefs; for (SgArrayRefExp* arrayRef : arrayRefs) { - vector iterationVars; - if (regime == Regime::REGULAR_INDEXES) - { - auto vars = arrayRefToIterVarsMap.find(arrayRef); - if (vars != arrayRefToIterVarsMap.end()) - iterationVars = vars->second; - } - - vector subscripts = getShortFixedSubscriptsVector(arrayRef, fixedDimensionsMask, regime, iterationVars); + vector subscripts = getShortFixedSubscriptsVector(arrayRef, fixedDimensionsMask, regime); if (uniqueRefs.find(subscripts) == uniqueRefs.end()) uniqueRefs.insert(make_pair(subscripts, arrayRef)); } @@ -330,6 +309,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>& allFuncInfo) { for (const auto& fileFuncs : allFuncInfo) @@ -340,6 +320,7 @@ static FuncInfo* findFuncByName(string funcName, const map>& allFuncInfo) { auto fileInfo = allFuncInfo.find(stmt->fileName()); @@ -659,6 +640,11 @@ static bool isVarChangedBetween(string var, SgStatement* first, SgStatement* sec return false; } +static vector 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, @@ -765,12 +751,27 @@ static void removeVarFromPrivateAttributes(SgSymbol* var, LoopGraph* loop) // getVarToExpMap returns map SgSymbol* from defRef -> SgExpression* from useRef static map getVarToExpMap(SgArrayRefExp* defRef, SgArrayRefExp* useRef, - const vector& fixedDimensions) + const vector& fixedDimensions, Regime regime) { map 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; } @@ -781,19 +782,6 @@ static set> removeArray(string filename, PrivateToRemove& arrayToRem { set> 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 iterationVars; - fillIterationVars(st, loopStmt, iterationVars); - - vector 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) { @@ -824,7 +812,7 @@ static set> 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); @@ -868,8 +856,7 @@ void removePrivates(string filename, vector& messages, } else { - varRefs = removeDuplicateArrayRefs(varRefs, fixedDimensions, varToRemove.regime, - varToRemove.arrayRefToIterationVarsMap); + varRefs = removeDuplicateArrayRefs(varRefs, fixedDimensions, varToRemove.regime); for (auto& varRef : varRefs) { vector subscripts = getShortFixedSubscriptsVector(varRef, varToRemove); @@ -892,6 +879,7 @@ void removePrivates(string filename, vector& messages, } } + // remove dead code from loop: for (auto& dcLoopRem : removeDC) { auto loopStmt = dcLoopRem->loop->GetOriginal(); @@ -928,7 +916,6 @@ struct Context { int dimensionsNum; vector explicitArrayRefs; vector fixedDimensionsMask; - map> arrayRefToIterationVarsMap; }; // ReducedArrayVars represents mapping of array reference to reduced scalar var: @@ -995,15 +982,7 @@ static vector::const_iterator findInsertedStmt(const vector getShortFixedSubscriptsVector(Context* ctx, SgArrayRefExp* arrayRef) { - vector 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 @@ -1225,26 +1204,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; + } + } + + if (!isIterationVar) return false; } - - // 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))) - return false; - - iterationVars.resize(indexExprList->length()); - ctx->arrayRefToIterationVarsMap.insert(make_pair(arrayRef, iterationVars)); } } @@ -1522,7 +1509,8 @@ static vector> checkIndirectUsage(Context* ctx) if (currentFunc == nullptr) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - vector commonBlockGroupedVar = getCommonBlockGroupedVar(currentFunc, ctx->arraySymbol, ctx->commonBlocks); + const auto& blocks = ctx->commonBlocks; + vector commonBlockGroupedVar = getCommonBlockGroupedVar(currentFunc, ctx->arraySymbol, blocks); if (commonBlockGroupedVar.empty()) return indirectUsageMasks; @@ -1623,9 +1611,11 @@ static ReducedArrayVarsMap getReducedArrayVars(Context* ctx) { string name = getReducedArrayVarName(arrayRef->symbol(), subscripts); - int nameNumber = checkSymbNameAndCorrect(name + "__", 0); - if (nameNumber != 0) + if (checkSymbNameAndCorrect(name, "_") != name) + { + int nameNumber = checkSymbNameAndCorrect(name + "__", 0); name = name + "__" + std::to_string(nameNumber); + } SgSymbol* newSymbol = new SgSymbol(VARIABLE_NAME, name.c_str(), type, scope); reducedArrayVars.insert(subscripts, newSymbol); @@ -1700,8 +1690,7 @@ static vector insertReducedArrayVarStmts(Context* ctx, { vector 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; @@ -1774,7 +1763,7 @@ static vector buildDefUsePairs(Context* ctx, const CFG_Type& CF set 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 tmpRD_defArgs; for (int defArgNum : RD_defArgs) { @@ -1787,66 +1776,38 @@ static vector 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 + + // 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(); + for (auto& instruction : blockInstructionsVector) { - if (defArg == SAPFOR::CFG_VAL::UNINIT) - continue; + SgStatement* stmt = instruction->getInstruction()->getOperator(); + if (stmt == useInsertedStmt.insertedStmt) + break; - 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()) + if (stmt->variant() == ASSIGN_STAT + && stmt->expr(0)->symbol()->identifier() == defVarName + && !isVarChangedBetween(defVarName, stmt, useInsertedStmt.insertedStmt)) { defIsFound = true; - auto defInsAndBlock = getInstructionAndBlockByNumber(CFGraph, defArg); - defStmt = defInsAndBlock.first->getOperator(); + defStmt = stmt; 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(); - for (auto& instruction : blockInstructionsVector) - { - SgStatement* stmt = instruction->getInstruction()->getOperator(); - if (stmt == useInsertedStmt.insertedStmt) - break; - - if (stmt->variant() == ASSIGN_STAT - && stmt->expr(0)->symbol()->identifier() == defVarName - && !isVarChangedBetween(defVarName, stmt, useInsertedStmt.insertedStmt)) - { - defIsFound = true; - defStmt = stmt; - break; - } - } - } - if (!defIsFound) { addMessageCannotFindRD(ctx->messages, arrayName, useLineNum); @@ -1884,15 +1845,6 @@ static vector 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) { @@ -2063,7 +2015,8 @@ static bool areDifferentRefs(Context* ctx, SgExpression* exp, const pair> findVarInRDSet(const map>& RD_In, const string& var) +static pair> findVarInRDSet(const map>& RD_In, + const string& var) { for (auto& RD_InElem : RD_In) { @@ -2093,7 +2046,9 @@ static bool checkDefUsePair(Context* ctx, const DefUseStmtsPair& defUse, const C vector arrayUseRefs = getDirectArrayRefsFromSingleStmt(defUse.second, ctx->arraySymbol); for (auto useRef : arrayUseRefs) { - map varToExpMap = getVarToExpMap(defRef, useRef, ctx->fixedDimensionsMask); + map varToExpMap; + varToExpMap = getVarToExpMap(defRef, useRef, ctx->fixedDimensionsMask, ctx->regime); + SgExpression* expToSubst = defUse.first->rhs()->copyPtr(); expToSubst = replaceVarsWithExps(expToSubst, varToExpMap); @@ -2282,7 +2237,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); } @@ -2349,6 +2303,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); @@ -2358,8 +2314,7 @@ void removePrivatesAnalysis(string filename, if (!checkLoopAlignmentMatching(&context)) { - addMessageVarNotAlignedWithLoop(messages, context.arraySymbol->identifier(), - context.loop->lineNum); + addMessageVarNotAlignedWithLoop(messages, arrayName, context.loop->lineNum); continue; } @@ -2374,14 +2329,10 @@ void removePrivatesAnalysis(string filename, else if (checkRegularIndexRefs(&context)) { context.regime = Regime::REGULAR_INDEXES; - context.fixedDimensionsMask = vector{}; removePrivateAnalyze(&context); } else - { - addMessageDoesNotMatchMask(messages, context.arraySymbol->identifier(), - context.loop->lineNum); - } + addMessageDoesNotMatchMask(messages, arrayName, context.loop->lineNum); } } diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h index e29ec4b..4196bc7 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h @@ -17,7 +17,6 @@ struct PrivateToRemove { Regime regime; std::vector> defUseStmtsPairs; std::vector fixedDimensions; - std::map> arrayRefToIterationVarsMap; }; // removePrivates removes all privates from vector privatesToRemoveGloval