From 1e9e1db084fc0b0498f19cbe3b61c7c8e4b95ecf Mon Sep 17 00:00:00 2001 From: Grigorii Gusev Date: Sat, 24 Feb 2024 18:11:28 +0300 Subject: [PATCH 1/9] private_removing: bugreport_1703086913 --- .../_src/Transformations/private_removing.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp index c5d94f7..96e4ba3 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp @@ -1474,7 +1474,8 @@ static vector buildDefUsePairs(Context* ctx, const CFG_Type& CF set RD_defArgs = RD_forUseArg->second; // make copy - // delete recursive definition from RD def args: + // delete recursive and uninit definition from RD def args: + set tmpRD_defArgs; for (int defArgNum : RD_defArgs) { if (defArgNum == SAPFOR::CFG_VAL::UNINIT) @@ -1487,11 +1488,11 @@ static vector buildDefUsePairs(Context* ctx, const CFG_Type& CF SgStatement* defStmt = defInsAndBlock.first->getOperator(); auto defInsertedStmt = findInsertedStmt(insertedStmts, defStmt); if (useInsertedStmt.relatedToStmt == defInsertedStmt->relatedToStmt) - { - RD_defArgs.erase(defArgNum); - break; - } + continue; + + tmpRD_defArgs.insert(defArgNum); } + RD_defArgs.swap(tmpRD_defArgs); if (RD_defArgs.size() == 0) // argument is not initialized { @@ -1551,6 +1552,9 @@ static vector buildDefUsePairs(Context* ctx, const CFG_Type& CF else { auto defInsAndBlock = getInstructionAndBlockByNumber(CFGraph, *RD_defArgs.begin()); + if (defInsAndBlock.first == nullptr) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + defStmt = defInsAndBlock.first->getOperator(); } From 0d61856cefda25a8b7ea0c3ec1e4610b2bc33b58 Mon Sep 17 00:00:00 2001 From: Grigorii Gusev Date: Sat, 24 Feb 2024 21:26:05 +0300 Subject: [PATCH 2/9] private_removing: update for LU test --- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 6 +- .../_src/Transformations/private_removing.cpp | 323 +++++++++++++----- .../_src/Transformations/private_removing.h | 5 +- 3 files changed, 254 insertions(+), 80 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index 0dcb946..3b2f0d3 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -1106,11 +1106,13 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne { auto itFound = loopGraph.find(file->filename()); if (itFound != loopGraph.end()) - removePrivatesAnalysis(itFound->second, getObjectForFileFromMap(file_name, SPF_messages), usersDirectives, commonBlocks, allFuncInfo); + removePrivatesAnalysis(itFound->second, getObjectForFileFromMap(file_name, SPF_messages), + usersDirectives, commonBlocks, allFuncInfo); } else if (curr_regime == PRIVATE_REMOVING) { - removePrivates(file, getObjectForFileFromMap(file_name, SPF_messages), countOfTransform); + removePrivates(file, getObjectForFileFromMap(file_name, SPF_messages), + commonBlocks, allFuncInfo, countOfTransform); } else if (curr_regime == CREATE_INTER_TREE) { diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp index 96e4ba3..c1d87e3 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp @@ -4,6 +4,7 @@ #include "../Utils/SgUtils.h" #include "../Utils/utils.h" #include "../ExpressionTransform/expr_transform.h" +#include "dead_code.h" using std::make_pair; using std::map; @@ -16,17 +17,48 @@ using std::wstring; using CFG_Type = map>; 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 coefB; + string var; + + RegularExpr(): coefA(0), coefB(0), var("") {} +}; + +static bool operator==(const RegularExpr& left, const RegularExpr& right) +{ + return left.var == right.var && left.coefA == right.coefA && left.coefB == right.coefB; +} + +static bool operator<(const RegularExpr& left, const RegularExpr& right) +{ + if (left.var == right.var && left.coefA < right.coefA && left.coefB <= right.coefB) + return true; + + if (left.var == right.var && left.coefA == right.coefA && left.coefB < right.coefB) + return true; + + return false; +} + // FixedSubscript represents subscript of array. Subscript is fixed if it is INT_VAL value struct FixedSubscript { bool isFixed; int value; -}; -// 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; - int coefB; + bool isRegIndex; + RegularExpr regExprStart; + RegularExpr regExprEnd; + + FixedSubscript() { + isFixed = false; + value = 0; + isRegIndex = false; + regExprStart = RegularExpr{}; + regExprEnd = RegularExpr{}; + } }; // DefUseStmtsPair represents pair of DEF and USE statements for private variable @@ -106,15 +138,50 @@ static bool isArrayRefInVector(SgArrayRefExp* ref, const vector& return false; } +// findAnyVar returns first found VAR_REF in expr +static SgSymbol* findAnyVar(SgExpression* expr) +{ + if (expr == nullptr) + return nullptr; + + if (expr->variant() == VAR_REF) + return expr->symbol(); + + SgSymbol* res = findAnyVar(expr->lhs()); + if (res != nullptr) + return res; + + return findAnyVar(expr->rhs()); +} + // checkAndFillRegularExpr checks if expr is regular and fills regularExpr struct // with info about expr static bool checkAndFillRegularExpr(SgExpression* expr, RegularExpr& regularExpr, SgSymbol* iterationVar) { + if (expr == nullptr) + return false; + + // hack for cases when iterationVar doesn't matter: + if (iterationVar == nullptr) + iterationVar = findAnyVar(expr); + + bool deleteTmpVar = false; + if (iterationVar == nullptr) + { + iterationVar = new SgSymbol(VARIABLE_NAME, "tmp"); + deleteTmpVar = true; + } + pair retCoefs; getCoefsOfSubscript(retCoefs, expr, iterationVar); regularExpr.coefA = retCoefs.first; regularExpr.coefB = retCoefs.second; + if (!deleteTmpVar) + regularExpr.var = iterationVar->identifier(); + + if (deleteTmpVar) + delete iterationVar; if (retCoefs.first != 0 || retCoefs.second != 0) return true; @@ -218,6 +285,33 @@ static bool isSymbolInExpression(SgSymbol* symbol, SgExpression* exp) isSymbolInExpression(symbol, exp->rhs()); } +static FuncInfo* findFunc(string fileName, string funcName, const map>& allFuncInfo) +{ + auto fileInfo = allFuncInfo.find(fileName); + if (fileInfo == allFuncInfo.end()) + return nullptr; + + for (auto funcInfo : fileInfo->second) + if (funcInfo->funcName == funcName) + return funcInfo; + + return nullptr; +} + +static FuncInfo* getCurrectFunc(SgStatement* stmt, const map>& allFuncInfo) +{ + auto fileInfo = allFuncInfo.find(stmt->fileName()); + if (fileInfo == allFuncInfo.end()) + return nullptr; + + int stmtLine = stmt->lineNumber(); + for (auto funcInfo : fileInfo->second) + if (funcInfo->linesNum.first <= stmtLine && stmtLine <= funcInfo->linesNum.second) + return funcInfo; + + return nullptr; +} + /* ************************************** * * End of block of common used functions: * * ************************************** */ @@ -735,7 +829,10 @@ static set> removeArray(string filename, const PrivateToRemove& arra return removedFixedSubscripts; } -void removePrivates(SgFile* file, vector& messages, int& countOfTransform) +void removePrivates(SgFile* file, vector& messages, + const map& commonBlocks, + const map>& allFuncInfo, + int& countOfTransform) { for (auto& varToRemove : privatesToRemoveGlobal) { @@ -747,9 +844,16 @@ void removePrivates(SgFile* file, vector& messages, int& countOfTransf //removeDeadCodeFromLoop(varToRemove.loop); // TODO: problem with reverting substitution removeExcessiveDefs(varToRemove); - removeEmptyLoops(varToRemove.loop, messages); + removeEmptyLoops(varToRemove.loop, messages); // removing is made by REMOVE_DEAD_CODE pass SgForStmt* loopStmt = (SgForStmt*)varToRemove.loop->loop->GetOriginal(); + FuncInfo* currFunc = getCurrectFunc(loopStmt, allFuncInfo); + if (currFunc == nullptr) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + // TODO: problem with removing dead code before reverting substitution + //removeDeadCode(currFunc->funcPointer, allFuncInfo, commonBlocks); + vector varRefs = getDirectArrayRefs(loopStmt, varToRemove.varSymbol); int loopLineNum = varToRemove.loop->lineNum; string varName = varToRemove.varSymbol->identifier(); @@ -780,7 +884,7 @@ void removePrivates(SgFile* file, vector& messages, int& countOfTransf { varName = getDimensionVarName(varToRemove.varSymbol, removedDimension, fixedDimensions, varToRemove.regime); - addMessageRemovePrivateVarPart(messages, varName, loopLineNum); + addMessageRemovePrivateVar(messages, varName, loopLineNum); } } } @@ -1042,9 +1146,35 @@ static vector getFixedDimensionsMask(Context* ctx) return resultMask; } +// 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; +} + +// getLoopStmtForVar searches for loop with iteration var equal loopVar starting from from stmt +static SgForStmt* getLoopStmtForVar(SgStatement* stmt, string loopVar) +{ + while (stmt != nullptr) + { + SgForStmt* loopStmt = getScopeLoopStmt(stmt); + + if (loopStmt->doName()->identifier() == loopVar) + return loopStmt; + + stmt = stmt->controlParent(); + } + + return nullptr; +} + // getFixedSubscriptsVector returns vector of fixed INT_VAL subscripts of arrayRef // true - subscript is fixed, false - it isn't -static vector getFixedSubscriptsVector(SgArrayRefExp* arrayRef, int dimensionsNum = 0) +static vector getFixedSubscriptsVector(SgArrayRefExp* arrayRef, int dimensionsNum = 0, + SgStatement* stmt = nullptr) { if (arrayRef->numberOfSubscripts() == 0) return vector(dimensionsNum); @@ -1052,42 +1182,50 @@ static vector getFixedSubscriptsVector(SgArrayRefExp* arrayRef, vector subscriptsVector; for (int i = 0; i < arrayRef->numberOfSubscripts(); ++i) { - if (arrayRef->subscript(i)->variant() == INT_VAL) - subscriptsVector.push_back(FixedSubscript{ true, arrayRef->subscript(i)->valueInteger() }); - else - subscriptsVector.push_back(FixedSubscript{ false, 0 }); + SgExpression* subscriptExpr = arrayRef->subscript(i); + FixedSubscript sub; + + if (subscriptExpr->variant() == INT_VAL) + { + sub.isFixed = true; + sub.value = subscriptExpr->valueInteger(); + } + else if (stmt != nullptr && subscriptExpr->variant() == VAR_REF) + { + SgForStmt* loopStmt = getLoopStmtForVar(stmt, subscriptExpr->symbol()->identifier()); + if (loopStmt != nullptr) + { + RegularExpr regExprStep; + SgExpression* step = loopStmt->step(); + if (step == nullptr || checkAndFillRegularExpr(step, regExprStep, nullptr) && regExprStep.coefA == 0) + { + SgExpression* start = loopStmt->start(); + SgExpression* end = loopStmt->end(); + if (regExprStep.coefB < 0) + std::swap(start, end); + + bool isRegular = true; + isRegular = isRegular && checkAndFillRegularExpr(start, sub.regExprStart, nullptr); + isRegular = isRegular && checkAndFillRegularExpr(end, sub.regExprEnd, nullptr); + sub.isRegIndex = isRegular; + } + } + } + else { + RegularExpr regExpr; + if (checkAndFillRegularExpr(subscriptExpr, regExpr, nullptr)) { + sub.isRegIndex = true; + sub.regExprStart = regExpr; + sub.regExprEnd = regExpr; + } + } + + subscriptsVector.push_back(sub); } return subscriptsVector; } -static FuncInfo* findFunc(string fileName, string funcName, const map>& allFuncInfo) -{ - auto fileInfo = allFuncInfo.find(fileName); - if (fileInfo == allFuncInfo.end()) - return nullptr; - - for (auto funcInfo : fileInfo->second) - if (funcInfo->funcName == funcName) - return funcInfo; - - return nullptr; -} - -static FuncInfo* getCurrectFunc(SgStatement* stmt, const map>& allFuncInfo) -{ - auto fileInfo = allFuncInfo.find(stmt->fileName()); - if (fileInfo == allFuncInfo.end()) - return nullptr; - - int stmtLine = stmt->lineNumber(); - for (auto funcInfo : fileInfo->second) - if (funcInfo->linesNum.first <= stmtLine && stmtLine <= funcInfo->linesNum.second) - return funcInfo; - - return nullptr; -} - // checkImplicitDirectUsage returns masks of array implicit usage (as out argument) // in any function call in exp and writes message about each usage static void checkImplicitDirectUsage(Context* ctx, SgExpression* exp, int stmtLineNum, @@ -1580,14 +1718,14 @@ 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; -} +//// 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) @@ -1653,7 +1791,8 @@ static LoopGraph* leastCommonAncestor(LoopGraph* a, LoopGraph* b, LoopGraph* par // fillFullFixedSubscriptsVectorsOfAllVars return vector of pairs (name of var, its fixed subscripts vector) // of all VAR_REF and ARRAY_REF vars in exp static void fillFixedSubscriptsVectorsOfAllVars(SgExpression* exp, - vector>>& vec) + vector>>& vec, + SgStatement* stmt = nullptr) { if (exp == nullptr) return; @@ -1671,22 +1810,54 @@ static void fillFixedSubscriptsVectorsOfAllVars(SgExpression* exp, else if (exp->variant() == ARRAY_REF) { vec.push_back(make_pair(exp->symbol()->identifier(), - getFixedSubscriptsVector((SgArrayRefExp*)exp))); + getFixedSubscriptsVector((SgArrayRefExp*)exp, 0, stmt))); SgExprListExp* exprList = (SgExprListExp*)exp->lhs(); for (int i = 0; i < exprList->length(); ++i) - fillFixedSubscriptsVectorsOfAllVars(exprList->elem(i), vec); + fillFixedSubscriptsVectorsOfAllVars(exprList->elem(i), vec, stmt); } return; } - fillFixedSubscriptsVectorsOfAllVars(exp->lhs(), vec); - fillFixedSubscriptsVectorsOfAllVars(exp->rhs(), vec); + fillFixedSubscriptsVectorsOfAllVars(exp->lhs(), vec, stmt); + fillFixedSubscriptsVectorsOfAllVars(exp->rhs(), vec, stmt); +} + +// fixedSubscriptLess checks if left FixedSubscript is less than right +static bool fixedSubscriptLess(const FixedSubscript& left, const FixedSubscript& right) +{ + if (left.isFixed && right.isFixed && left.value < right.value) + return true; + + if (left.isFixed && right.isRegIndex + && right.regExprStart.coefA == 0 && left.value < right.regExprStart.coefB) + return true; + + if (left.isRegIndex && right.isFixed + && left.regExprEnd.coefA == 0 && left.regExprEnd.coefB < right.value) + return true; + + if (left.isRegIndex && right.isRegIndex) + return left.regExprEnd < right.regExprStart; + + return false; +} + +// fixedSubscriptLess checks if left and right FixedSubscripts are different, +// using empirical methods +static bool possibleDifferent(FixedSubscript left, FixedSubscript right) +{ + // TODO: add warning? + if (left.isFixed && right.isRegIndex && right.regExprStart == right.regExprEnd) { + return true; // in general, this is not true + } + + return false; } // isDifferentRefs checks if exp (var reference) is different from var. Refs are different // if they has at least one different fixed subscript: arr(i, 1) is different from arr(j, 2) -static bool isDifferentRefs(SgExpression* exp, const pair>& var) +static bool isDifferentRefs(SgExpression* exp, const pair>& var, SgStatement* stmt) { if (exp->symbol()->identifier() != var.first) return true; @@ -1694,16 +1865,25 @@ static bool isDifferentRefs(SgExpression* exp, const pairvariant() == VAR_REF) return false; - vector leftVec = getFixedSubscriptsVector((SgArrayRefExp*)exp); + vector leftVec = getFixedSubscriptsVector((SgArrayRefExp*)exp, 0, stmt); + + if (leftVec.size() != var.second.size()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); for (int i = 0; i < leftVec.size(); i++) - if (leftVec[i].isFixed && var.second[i].isFixed && leftVec[i].value != var.second[i].value) + { + if (fixedSubscriptLess(leftVec[i], var.second[i]) + || fixedSubscriptLess(var.second[i], leftVec[i]) + || possibleDifferent(leftVec[i], var.second[i])) + { return true; + } + } return false; } -pair> findVarInRDSet(map> RD_In, string var) +static pair> findVarInRDSet(const map> RD_In, string var) { for (auto& RD_InElem : RD_In) { @@ -1721,7 +1901,7 @@ pair> findVarInRDSet(map // checkDefUsePair checks if def statement from pair can be substituted into use statement // and creates messages -static bool checkDefUsePair(Context* ctx, DefUseStmtsPair defUse, const CFG_Type& CFGraph) +static bool checkDefUsePair(Context* ctx, const DefUseStmtsPair& defUse, const CFG_Type& CFGraph) { if (defUse.first->lineNumber() > defUse.second->lineNumber()) return false; @@ -1737,7 +1917,7 @@ static bool checkDefUsePair(Context* ctx, DefUseStmtsPair defUse, const CFG_Type SgExpression* expToSubst = defUse.first->rhs()->copyPtr(); expToSubst = replaceVarsWithExps(expToSubst, varToExpMap); - fillFixedSubscriptsVectorsOfAllVars(expToSubst, dependOnVars); + fillFixedSubscriptsVectorsOfAllVars(expToSubst, dependOnVars, defUse.second); } vector iterationVars{}; @@ -1784,29 +1964,16 @@ static bool checkDefUsePair(Context* ctx, DefUseStmtsPair defUse, const CFG_Type } } - return true; - // TODO: ^^^^^ - // проверка массивов отключена, поскольку является слишком консервативной и не позволяет - // выполнить удаление массива RTMP в функции RHS теста LU. - // требуется выполнять более сложный анализ, с определением границ циклов, - // чтобы отличать, что все присваивания в переменную rsd в этом примере - // являются присваиваниями в разные элементы массива: - // rsd(1,i,j,2) = ... - // do k = 4, nz - 3 - // rsd(1, i, j, k) = ... - // enddo - // rsd(1, i, j, nz - 2) = ... - // checking arrays: auto defLoopStmt = getScopeLoopStmt(defUse.first); auto useLoopStmt = getScopeLoopStmt(defUse.second); LoopGraph* loop = ctx->loop; - while (loop->perfectLoop != 1) // (what is it? - может быть это не нужно) + while (loop->perfectLoop != 1) // (what is it? - TODO: may be remove it) loop = loop->children[0]; - auto defLoop = findLoop(loop, defLoopStmt); - auto useLoop = findLoop(loop, useLoopStmt); + LoopGraph* defLoop = findLoop(loop, defLoopStmt); + LoopGraph* useLoop = findLoop(loop, useLoopStmt); if (!defLoopStmt || !useLoopStmt || !defLoop || !useLoop) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); @@ -1850,7 +2017,9 @@ static bool checkDefUsePair(Context* ctx, DefUseStmtsPair defUse, const CFG_Type { for (SgStatement* st = startStmt; st != endStmt; st = st->lexNext()) { - if (st->variant() == ASSIGN_STAT && !isDifferentRefs(st->expr(0), var)) + if (st == defUse.second) + continue; + if (st->variant() == ASSIGN_STAT && !isDifferentRefs(st->expr(0), var, st)) { addMessageDependOnNonInvariant(ctx->messages, arrayName, var.first, defUse.first->lineNumber()); diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h index 84c0584..63fe5c3 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h @@ -22,7 +22,10 @@ struct PrivateToRemove { // removePrivates removes all privates from vector privatesToRemoveGloval // and add info messages -void removePrivates(SgFile* file, std::vector& messages, int& countOfTransform); +void removePrivates(SgFile* file, std::vector& messages, + const std::map& commonBlocks, + const std::map>& allFuncInfo, + int& countOfTransform); // removePrivatesAnalysis checks all private variables in loopGraphs specified by usersDirectives // if they can be removed, and adds those that can to vector privatesToRemoveGloval. From c35fd0776669fc23e68fd42d19f52fcb632e1e89 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Thu, 14 Mar 2024 10:32:36 +0300 Subject: [PATCH 3/9] trivial --- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 6 ++---- .../Sapfor_2017/_src/Transformations/private_removing.cpp | 2 +- sapfor/experts/Sapfor_2017/_src/Utils/version.h | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index 3b2f0d3..d3f76e8 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -1106,13 +1106,11 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne { auto itFound = loopGraph.find(file->filename()); if (itFound != loopGraph.end()) - removePrivatesAnalysis(itFound->second, getObjectForFileFromMap(file_name, SPF_messages), - usersDirectives, commonBlocks, allFuncInfo); + removePrivatesAnalysis(itFound->second, getObjectForFileFromMap(file_name, SPF_messages), usersDirectives, commonBlocks, allFuncInfo); } else if (curr_regime == PRIVATE_REMOVING) { - removePrivates(file, getObjectForFileFromMap(file_name, SPF_messages), - commonBlocks, allFuncInfo, countOfTransform); + removePrivates(file, getObjectForFileFromMap(file_name, SPF_messages), commonBlocks, allFuncInfo, countOfTransform); } else if (curr_regime == CREATE_INTER_TREE) { diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp index c1d87e3..fe12532 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp @@ -1883,7 +1883,7 @@ static bool isDifferentRefs(SgExpression* exp, const pair> findVarInRDSet(const map> RD_In, string var) +static pair> findVarInRDSet(const map>& RD_In, const string& var) { for (auto& RD_InElem : RD_In) { diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index cab6d43..98963f6 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/version.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2285" +#define VERSION_SPF "2286" From 1d82af702e6f5d7e32419ccc4520ee4acdd1fae8 Mon Sep 17 00:00:00 2001 From: Grigorii Gusev Date: Sat, 16 Mar 2024 00:10:28 +0300 Subject: [PATCH 4/9] private_removing: add dead code removing --- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 15 +- .../_src/Transformations/private_removing.cpp | 159 +++++++++--------- .../_src/Transformations/private_removing.h | 5 +- .../Sapfor_2017/_src/Utils/PassManager.h | 2 +- 4 files changed, 92 insertions(+), 89 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index d3f76e8..d15c7f8 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -1106,11 +1106,13 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne { auto itFound = loopGraph.find(file->filename()); 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) { - 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) { @@ -2498,15 +2500,8 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case REMOVE_OMP_DIRS_TRANSFORM: case REMOVE_COMMENTS: case INSERT_NO_DISTR_FLAGS_FROM_GUI: - runAnalysis(*project, curr_regime, true, "", folderName); - break; case PRIVATE_REMOVING: - runAnalysis(*project, curr_regime, false, "", 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"); + runAnalysis(*project, curr_regime, true, "", folderName); break; case INLINE_PROCEDURES: runAnalysis(*project, curr_regime, false); diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp index fe12532..eef5fca 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp @@ -599,38 +599,39 @@ static bool isVarChangedBetween(string var, SgStatement* first, SgStatement* sec return false; } +// TODO: remove if needless // removeDeadCodeFromLoop removes assign statements to private scalar vars which are not read in loop -static void removeDeadCodeFromLoop(LoopGraph* loop) -{ - SgForStmt* loopStmt = (SgForStmt*) loop->loop->GetOriginal(); - set privateVars; - for (auto data : getAttributes(loopStmt, set{ SPF_ANALYSIS_DIR })) - fillPrivatesFromComment(new Statement(data), privateVars); - - set privates; - for (Symbol* symbol : privateVars) - privates.insert(OriginalSymbol((SgSymbol*)symbol)->identifier()); - - vector stmtsToDelete; - for (SgStatement* st = loopStmt->lexNext(); st != loopStmt->lastNodeOfStmt(); st = st->lexNext()) - { - if (st->variant() != ASSIGN_STAT) - continue; - - SgSymbol* var = st->expr(0)->symbol(); - if (var == nullptr || var->variant() != VARIABLE_NAME) - continue; - - if (privates.find(var->identifier()) != privates.end() && !isVarReadInLoop(var, loopStmt)) - stmtsToDelete.push_back(st); - } - - for (auto stmt : stmtsToDelete) - stmt->deleteStmt(); - - for (auto childLoop : loop->children) - removeDeadCodeFromLoop(childLoop); -} +//static void removeDeadCodeFromLoop(LoopGraph* loop) +//{ +// SgForStmt* loopStmt = (SgForStmt*) loop->loop->GetOriginal(); +// set privateVars; +// for (auto data : getAttributes(loopStmt, set{ SPF_ANALYSIS_DIR })) +// fillPrivatesFromComment(new Statement(data), privateVars); +// +// set privates; +// for (Symbol* symbol : privateVars) +// privates.insert(OriginalSymbol((SgSymbol*)symbol)->identifier()); +// +// vector stmtsToDelete; +// for (SgStatement* st = loopStmt->lexNext(); st != loopStmt->lastNodeOfStmt(); st = st->lexNext()) +// { +// if (st->variant() != ASSIGN_STAT) +// continue; +// +// SgSymbol* var = st->expr(0)->symbol(); +// if (var == nullptr || var->variant() != VARIABLE_NAME) +// continue; +// +// if (privates.find(var->identifier()) != privates.end() && !isVarReadInLoop(var, loopStmt)) +// stmtsToDelete.push_back(st); +// } +// +// for (auto stmt : stmtsToDelete) +// stmt->deleteStmt(); +// +// for (auto childLoop : loop->children) +// removeDeadCodeFromLoop(childLoop); +//} // fillReadShortFixedSumscripts fills all short fixed subscripts vectors of array var, // which are used for reading from array var in exp @@ -697,30 +698,31 @@ static void removeExcessiveDefs(const PrivateToRemove& var) st->deleteStmt(); } +// TODO: remove is needless // removeEmptyLoops removes loops with empty body and create messages -static void removeEmptyLoops(LoopGraph* loop, vector& messages) -{ - vector loopsToDelete; - vector newChildrenVector; - for (auto childLoop : loop->children) - { - SgStatement* loopStmt = childLoop->loop->GetOriginal(); - if (loopStmt->lastNodeOfStmt() == loopStmt->lexNext()) - loopsToDelete.push_back(childLoop); - else - newChildrenVector.push_back(childLoop); - } - - for (auto loopToDelete : loopsToDelete) - { - addMessageRemoveLoop(messages, loopToDelete->lineNum); - loopToDelete->loop->extractStmt(); - } - - loop->children.swap(newChildrenVector); - for (auto childLoop : loop->children) - removeEmptyLoops(childLoop, messages); -} +//static void removeEmptyLoops(LoopGraph* loop, vector& messages) +//{ +// vector loopsToDelete; +// vector newChildrenVector; +// for (auto childLoop : loop->children) +// { +// SgStatement* loopStmt = childLoop->loop->GetOriginal(); +// if (loopStmt->lastNodeOfStmt() == loopStmt->lexNext()) +// loopsToDelete.push_back(childLoop); +// else +// newChildrenVector.push_back(childLoop); +// } +// +// for (auto loopToDelete : loopsToDelete) +// { +// addMessageRemoveLoop(messages, loopToDelete->lineNum); +// loopToDelete->loop->extractStmt(); +// } +// +// loop->children.swap(newChildrenVector); +// for (auto childLoop : loop->children) +// removeEmptyLoops(childLoop, messages); +//} // removeVarFromPrivateAttributes removes var from SPF ANALYSIS PRIVATE attributes of loop static void removeVarFromPrivateAttributes(SgSymbol* var, LoopGraph* loop) @@ -820,39 +822,35 @@ static set> removeArray(string filename, const PrivateToRemove& arra SgExpression* substExp = substituteExpressions(useStmt->expr(i), refToExpMap); useStmt->setExpression(i, *substExp); - - // don't revert substitutions in useStmt: - cancelRevertionForStatement(filename, useStmt, i); } } return removedFixedSubscripts; } -void removePrivates(SgFile* file, vector& messages, +void removePrivates(string filename, vector& messages, const map& commonBlocks, const map>& allFuncInfo, int& countOfTransform) { for (auto& varToRemove : privatesToRemoveGlobal) { - if (string(file->filename()) != varToRemove.loop->fileName) + if (filename != varToRemove.loop->fileName) continue; - auto removedDimensions = removeArray(file->filename(), varToRemove); + auto removedDimensions = removeArray(filename, varToRemove); countOfTransform++; - //removeDeadCodeFromLoop(varToRemove.loop); // TODO: problem with reverting substitution + //removeDeadCodeFromLoop(varToRemove.loop); 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(); FuncInfo* currFunc = getCurrectFunc(loopStmt, allFuncInfo); if (currFunc == nullptr) 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 varRefs = getDirectArrayRefs(loopStmt, varToRemove.varSymbol); int loopLineNum = varToRemove.loop->lineNum; @@ -904,6 +902,7 @@ struct Context { const map>& allFuncInfo; const map& commonBlocks; vector& messages; + string filename; Regime regime; LoopGraph* loop; @@ -2083,9 +2082,19 @@ void removePrivateAnalyze(Context *ctx) { vector resultDefUsePairs; for (auto& pair : defUseStmtsPairs) + { if (checkDefUsePair(ctx, pair, CFG_ForFunc)) + { 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; newPrivateToRemove.loop = ctx->loop; newPrivateToRemove.varSymbol = ctx->arraySymbol; @@ -2104,7 +2113,8 @@ void removePrivateAnalyze(Context *ctx) deleteCFG(CFG_ForFunc); } -void removePrivatesAnalysis(vector& loopGraphs, +void removePrivatesAnalysis(string filename, + vector& loopGraphs, vector& messages, const UsersDirectives& usersDirectives, const map& commonBlocks, @@ -2151,7 +2161,7 @@ void removePrivatesAnalysis(vector& loopGraphs, if (arrayToRemove == nullptr) // no array to remove break; - Context context = Context{allFuncInfo, commonBlocks, messages}; + Context context = Context{allFuncInfo, commonBlocks, messages, filename}; context.regime = Regime::DEFLT; context.loop = loop; context.loopStmt = loopStmt; @@ -2173,20 +2183,17 @@ void removePrivatesAnalysis(vector& loopGraphs, { removePrivateAnalyze(&context); } - else + else if (checkRegularIndexRefs(&context)) { - if (checkRegularIndexRefs(&context)) - { - context.regime = Regime::REGULAR_INDEXES; - context.fixedDimensionsMask = vector{}; - removePrivateAnalyze(&context); - } - else - addMessageDoesNotMatchMask(messages, context.arraySymbol->identifier(), context.loop->lineNum); + context.regime = Regime::REGULAR_INDEXES; + context.fixedDimensionsMask = vector{}; + removePrivateAnalyze(&context); } + else + addMessageDoesNotMatchMask(messages, context.arraySymbol->identifier(), context.loop->lineNum); } } for (LoopGraph* loop : loopGraphs) - removePrivatesAnalysis(loop->children, messages, usersDirectives, commonBlocks, allFuncInfo); + removePrivatesAnalysis(filename, loop->children, messages, usersDirectives, commonBlocks, allFuncInfo); } diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h index 63fe5c3..e29ec4b 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.h @@ -22,7 +22,7 @@ struct PrivateToRemove { // removePrivates removes all privates from vector privatesToRemoveGloval // and add info messages -void removePrivates(SgFile* file, std::vector& messages, +void removePrivates(std::string filename, std::vector& messages, const std::map& commonBlocks, const std::map>& allFuncInfo, int& countOfTransform); @@ -30,7 +30,8 @@ void removePrivates(SgFile* file, std::vector& messages, // removePrivatesAnalysis checks all private variables in loopGraphs specified by usersDirectives // 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 -void removePrivatesAnalysis(std::vector& loopGraphs, +void removePrivatesAnalysis(std::string filename, + std::vector& loopGraphs, std::vector& messages, const std::map, std::set>& usersDirectives, const std::map& commonBlocks, diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h index 9c63fb1..feff4fd 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -212,7 +212,7 @@ void InitPassesDependencies(map> &passDepsIn, set 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 }); From 11888f2ff9d3963bea2c1aff5303c2cdd2cecec3 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Sat, 16 Mar 2024 17:35:51 +0300 Subject: [PATCH 5/9] dvm updated, version of sapfor updated --- dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c | 32 +++++++++-- dvm/fdvm/trunk/Sage/lib/newsrc/unparse.c | 24 ++++---- dvm/fdvm/trunk/fdvm/acc.cpp | 3 + dvm/fdvm/trunk/fdvm/acc_across.cpp | 23 ++++---- dvm/fdvm/trunk/fdvm/dvm.cpp | 16 +++--- dvm/fdvm/trunk/include/dvm.h | 1 + .../_src/ProjectManipulation/ConvertFiles.cpp | 55 +++++++++++++------ .../Sapfor_2017/_src/Utils/SgUtils.cpp | 2 +- .../experts/Sapfor_2017/_src/Utils/SgUtils.h | 1 - .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 10 files changed, 108 insertions(+), 51 deletions(-) diff --git a/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c b/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c index 92326cf..3b95b45 100644 --- a/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c +++ b/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c @@ -54,6 +54,7 @@ int getLastLabelId(); int isItInSection(); int Init_Tool_Box(); void Message(); + PTR_BFND rec_num_near_search(); PTR_BFND Redo_Bif_Next_Chain_Internal(); PTR_SYMB duplicateSymbol(); @@ -82,6 +83,12 @@ int out_upper_case; int out_line_unlimit; int out_line_length; // out_line_length = 132 for -ffo mode; out_line_length = 72 for -uniForm mode PTR_SYMB last_file_symbol; +struct file_symbol { + char *fname; + PTR_SYMB last_symb; + struct file_symbol *next; +} ; +struct file_symbol *file_last_symbol = NULL; static int CountNullBifNext = 0; /* for internal debugging */ @@ -94,7 +101,6 @@ enum typenode node_code_kind[LAST_CODE]; char info_type[LAST_CODE][MAXFIELDTYPE]; char info_symb[LAST_CODE][MAXFIELDSYMB]; char general_info[LAST_CODE][MAXFIELDSYMB]; - /*static struct bif_stack_level *stack_level = NULL;*/ /*static struct bif_stack_level *current_level = NULL;*/ @@ -247,6 +253,24 @@ char* mymalloc(int size) return pt1; } +PTR_SYMB FileLastSymbol(char *file_name) +{ + struct file_symbol *fsl; + for(fsl=file_last_symbol; fsl; fsl=fsl->next) + if(!strcmp(file_name, fsl->fname)) + return fsl->last_symb; + return NULL; +} + +void addFileSymbolList(char *file_name, PTR_SYMB symb) +{ + struct file_symbol *fsl; + fsl = (struct file_symbol *) xmalloc(sizeof (struct file_symbol)); + fsl->last_symb = symb; + fsl->fname = file_name; + fsl->next = file_last_symbol; + file_last_symbol = fsl; +} /***************** Provides infos on nodes ******************************** * * * based on the table info in include dir *.def * @@ -549,6 +573,7 @@ void Message(char *s, int l) #endif } + /***************************************************************************/ /* A set of functions for dealing with a free list for low_level node */ /***************************************************************************/ @@ -1203,14 +1228,13 @@ int Init_Tool_Box() number_of_ll_node = CUR_FILE_NUM_LLNDS() + 1; number_of_bif_node = CUR_FILE_NUM_BIFS() + 1; number_of_symb_node = CUR_FILE_NUM_SYMBS() + 1; - last_file_symbol = CUR_FILE_CUR_SYMB(); /* podd 23.06.15 */ - + addFileSymbolList(CUR_FILE_NAME(), CUR_FILE_CUR_SYMB()); /* podd 01.03.24 */ if (CUR_FILE_NAME()) strcpy(Current_File_name, CUR_FILE_NAME()); if (ToolBOX_INIT) return 0; ToolBOX_INIT = 1; - + make_a_malloc_stack(); /* initialisation des noeuds */ diff --git a/dvm/fdvm/trunk/Sage/lib/newsrc/unparse.c b/dvm/fdvm/trunk/Sage/lib/newsrc/unparse.c index eb5e47c..cc70fb9 100644 --- a/dvm/fdvm/trunk/Sage/lib/newsrc/unparse.c +++ b/dvm/fdvm/trunk/Sage/lib/newsrc/unparse.c @@ -736,10 +736,11 @@ void Init_Unparser() /* set the first tabulation */ TabNumber = 1; } - } else - { - if (Parser_Initiated != C_Initialized) - { + } + else + { + if (Parser_Initiated != C_Initialized) + { #define DEFNODECODE(SYM, NAME, TYPE, LENGTH, NT) Unparse_Def[SYM].str = create_unp_str( NAME); #include"unparseC++.def" #undef DEFNODECODE @@ -747,12 +748,15 @@ void Init_Unparser() #define DEFNODECODE(SYM, NAME, TYPE, LENGTH, NT) Unparse_Def[SYM].fct = NULL; #include"unparseC++.def" #undef DEFNODECODE - Parser_Initiated = C_Initialized; - /* init precedence table of operators for C++ */ - for(i=BIT_COMPLEMENT_OP - EQ_OP; i<=RSHIFT_ASSGN_OP-EQ_OP;i++) - precedence_C[i] = precedence2_C[i-BIT_COMPLEMENT_OP+EQ_OP]; - } - } + Parser_Initiated = C_Initialized; + /* set the first tabulation */ + TabNumber = 0; + + /* init precedence table of operators for C++ */ + for(i=BIT_COMPLEMENT_OP - EQ_OP; i<=RSHIFT_ASSGN_OP-EQ_OP;i++) + precedence_C[i] = precedence2_C[i-BIT_COMPLEMENT_OP+EQ_OP]; + } + } /* initialize the number of flag */ diff --git a/dvm/fdvm/trunk/fdvm/acc.cpp b/dvm/fdvm/trunk/fdvm/acc.cpp index 995a215..44cef68 100644 --- a/dvm/fdvm/trunk/fdvm/acc.cpp +++ b/dvm/fdvm/trunk/fdvm/acc.cpp @@ -83,11 +83,14 @@ void InitializeACC() type_CudaIndexType = NULL; type_with_len_DvmType = NULL; declaration_cmnt = NULL; + indexType_int = indexType_long = indexType_llong = NULL; dvmh_targets = options.isOn(NO_CUDA) ? HOST_DEVICE : HOST_DEVICE | CUDA_DEVICE; SpecialSymbols.insert(std::pair('\n', "\\n\"\n\"")); SpecialSymbols.insert(std::pair('"', "\\\"")); SpecialSymbols.insert(std::pair('\\', "\\\\")); + + InitializeAcrossACC(); } char *filenameACC() diff --git a/dvm/fdvm/trunk/fdvm/acc_across.cpp b/dvm/fdvm/trunk/fdvm/acc_across.cpp index 76af6ff..82aee5e 100644 --- a/dvm/fdvm/trunk/fdvm/acc_across.cpp +++ b/dvm/fdvm/trunk/fdvm/acc_across.cpp @@ -40,15 +40,13 @@ void DeclarationCreateReductionBlocksAcross(int, SgExpression*); AnalyzeReturnGpuO1 analyzeLoopBody(int type); // local static variables -static SgSymbol *red_first; -static bool declaration_include = true; +static SgSymbol *red_first = NULL; static bool createBodyKernel = false; static bool createConvert_XY = true; static const int numLoopVars = 16; static bool ifReadLvlMode = false; static vector > copyOfBody; static vector allRegNames; -static unsigned countOfCopies; static vector allVariants; static const char *funcDvmhConvXYfortVer = " attributes(device) subroutine dvmh_convert_XY_int(x,y,Rx,Ry,slash,idx)\n implicit none\n integer ,value:: x\n integer ,value:: y\n integer ,value:: Rx\n integer ,value:: Ry\n integer ,value:: slash\n integer ,device:: idx \n \n if(slash .eq. 0) then\n if(Rx .eq. Ry) then\n if(x + y .lt. Rx) then\n idx = y + (1+x+y)*(x+y)/2\n else\n idx = Rx*(Rx-1)+x-(2*Rx-x-y-1)*(2*Rx-x-y-2)/2\n endif \n elseif(Rx .lt. Ry) then\n if(x + y .lt. Rx) then\n idx = y + ((1+x+y)*(x+y)) / 2\n elseif(x + y .lt. Ry) then\n idx = ((1+Rx)*Rx) / 2 + Rx - x - 1 + Rx * (x+y-Rx)\n else\n idx = Rx*Ry-Ry+y-(((Rx+Ry-y-x-1)*(Rx+Ry-y-x-2))/2)\n endif\n else\n if(x + y .lt. Ry) then\n idx = x + (1+x+y)*(x+y) / 2\n elseif(x + y .lt. Rx) then\n idx = (1+Ry)*Ry/2 + (Ry-y-1) + Ry * (x+y-Ry)\n else\n idx = Rx*Ry-Rx+x-((Rx+Ry-y-x-1)*(Rx+Ry-y-x-2)/2)\n endif\n endif\n else\n if(Rx .eq. Ry) then\n if(x + Rx-1-y .lt. Rx) then\n idx = Rx-1-y + (x+Rx-y)*(x+Rx-1-y)/2\n else\n idx = Rx*(Rx-1) + x - (Rx-x+y)*(Rx-x+y-1)/2\n endif\n elseif(Rx .lt. Ry) then\n if(x + Ry-1-y .lt. Rx) then \n idx = Ry-1-y + ((x+Ry-y)*(x+Ry-1-y)) / 2\n elseif(x + Ry-1-y .lt. Ry) then\n idx = ((1+Rx)*Rx)/2+Rx-x-1+Rx*(x+Ry-1-y-Rx)\n else\n idx = Rx*Ry-1-y-(((Rx+y-x)*(Rx+y-x-1))/2)\n endif\n else\n if(x + Ry-1-y .lt. Ry) then\n idx = x + (1+x+Ry-1-y)*(x+Ry-1-y)/2\n elseif(x + Ry-1-y .lt. Rx) then\n idx = (1+Ry)*Ry/2 + y + Ry * (x-y-1)\n else\n idx = Rx*Ry-Rx+x-((Rx+y-x)*(Rx+y-x-1)/2)\n endif\n endif\n endif\n end subroutine\n"; @@ -58,6 +56,17 @@ static const char* fermiPreprocDir = "CUDA_FERMI_ARCH"; // local variables SgStatement *kernelScope, *block; +void InitializeAcrossACC() +{ + red_first = NULL; + createBodyKernel = false; + createConvert_XY = true; + ifReadLvlMode = false; + copyOfBody.clear(); + allRegNames.clear(); + allVariants.clear(); +} + static inline int pow(int n) { int tmp = 1; @@ -716,9 +725,6 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) { if (allVariants[i].acrossV != 1) ifOne = false; - - if ((unsigned)allVariants[i].acrossV == countOfCopies + 1) - countOfCopies++; } // set global if true if (ifOne) @@ -1787,11 +1793,8 @@ vector Create_C_Adapter_Function_Across_variants(SgSymbol *sadapt st_end = st_hedr->lexNext(); fe = st_hedr->expr(0); first_exec = st_end; - if (declaration_include) - { + if (declaration_cmnt == NULL) declaration_cmnt = "#include \n#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))\n#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))"; - declaration_include = false; - } mywarn(" end: create fuction header "); mywarn("start: create dummy argument list "); diff --git a/dvm/fdvm/trunk/fdvm/dvm.cpp b/dvm/fdvm/trunk/fdvm/dvm.cpp index 97a838d..58d9b7b 100644 --- a/dvm/fdvm/trunk/fdvm/dvm.cpp +++ b/dvm/fdvm/trunk/fdvm/dvm.cpp @@ -82,6 +82,7 @@ extern "C" int out_upper_case; extern "C" int out_line_unlimit; extern "C" int out_line_length; extern "C" PTR_SYMB last_file_symbol; +extern "C" PTR_SYMB FileLastSymbol(...); Options options; @@ -99,7 +100,7 @@ int main(int argc, char *argv[]) { FILE *fout = NULL; FILE *fout_cuf = NULL, *fout_C_cu = NULL, *fout_info = NULL; /*ACC*/ - const char *fout_name = "out.DVMH.f"; + const char *fout_name = NULL; char *fout_name_cuf; /*ACC*/ char *fout_name_C_cu; /*ACC*/ char *fout_name_info_C; /*ACC*/ @@ -389,22 +390,22 @@ int main(int argc, char *argv[]) ProjectStructure(project); Private_Vars_Project_Analyzer(); //---------------------------- - + initVariantNames(); //for project initIntrinsicFunctionNames(); //for project initSupportedVars(); // for project, acc_f2c.cpp initF2C_FunctionCalls(); // for project, acc_f2c.cpp for(int id=project.numberOfFiles()-1; id >= 0; id--) - { + { file = &(project.file(id)); //file->unparsestdout(); fin_name = new char[strlen(project.fileName(id))+2]; - sprintf(fin_name, "%s%s", project.fileName(id), " "); + sprintf(fin_name, "%s%s", project.fileName(id), " "); //fin_name = strcat(project.fileName(0)," "); // for call of function 'tpoint' //added one symbol to input-file name //printf("%s",fin_name); //!!! debug - if(a_mode || project.numberOfFiles()>1) - fout_name = doOutFileName(file->filename()); //project.fileName(id); + if(!fout_name) + fout_name = doOutFileName(file->filename()); else if (fout_name && source_name && !strcmp(source_name, fout_name)) { (void)fprintf(stderr, "Output file has the same name as source file\n"); @@ -417,7 +418,7 @@ int main(int argc, char *argv[]) fout_name_info_C = ChangeFto_info_C(fout_name); /*ACC*/ //set the last symbol of file - last_file_symbol = CUR_FILE_CUR_SYMB(); //LastSymbolOfFile(file)->thesymb; //for low_level.c + last_file_symbol = FileLastSymbol(file->filename()); //for low_level.c and not only initLibNames(); //for every file InitDVM(file); //for every file current_file = file; // global variable (used in SgTypeComplex) @@ -542,6 +543,7 @@ int main(int argc, char *argv[]) } } + fout_name = NULL; } if (v_print) diff --git a/dvm/fdvm/trunk/include/dvm.h b/dvm/fdvm/trunk/include/dvm.h index 785de47..b43f09f 100644 --- a/dvm/fdvm/trunk/include/dvm.h +++ b/dvm/fdvm/trunk/include/dvm.h @@ -1004,6 +1004,7 @@ SgStatement *doIfThenConstrForLoop_GPU(SgExpression *ref,SgStatement *endhost,Sg SgSymbol *KernelSymbol(SgStatement *st_do); void Blocks_Off_Symbol(); void InitializeACC(); +void InitializeAcrossACC(); void InitializeInFuncACC(); SgSymbol *GPUModuleSymb(SgStatement *global_st); void CreateGPUModule(); diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp index aa52cef..db7a1ce 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp @@ -31,13 +31,13 @@ extern "C" int out_upper_case; extern "C" int out_line_unlimit; extern "C" int out_line_length; extern "C" PTR_SYMB last_file_symbol; +extern "C" PTR_SYMB FileLastSymbol(...); -static int convertFile(int argc, char* argv[], const string& fileToConv, - const set& filesInProj, const set& moduleDeclsInFiles) +static int convertFile(int argc, char* argv[], const set& filesInProj, const set& moduleDeclsInFiles) { FILE* fout = NULL; FILE* fout_cuf = NULL, * fout_C_cu = NULL, * fout_info = NULL; /*ACC*/ - const char* fout_name = "out.DVMH.f"; + const char* fout_name = NULL; char* fout_name_cuf; /*ACC*/ char* fout_name_C_cu; /*ACC*/ char* fout_name_info_C; /*ACC*/ @@ -54,7 +54,8 @@ static int convertFile(int argc, char* argv[], const string& fileToConv, while ((argc > 1) && (*argv)[0] == '-') { if ((*argv)[1] == 'o' && ((*argv)[2] == '\0')) { - fout_name = argv[1]; + if (filesInProj.size() == 1) + fout_name = argv[1]; argv++; argc--; } @@ -307,7 +308,8 @@ static int convertFile(int argc, char* argv[], const string& fileToConv, static int id = 0; vector filesList; - filesList.push_back((char*)fileToConv.c_str()); + for (auto& file : filesInProj) + filesList.push_back((char*)file.c_str()); SgProject project((proj_name + to_string(id++)).c_str(), filesList.data(), filesList.size()); @@ -317,7 +319,7 @@ static int convertFile(int argc, char* argv[], const string& fileToConv, removeExecutableFromModuleDeclaration(&(project.file(z)), filesInProj, tmp); } - SgFile* file; + SgFile* file = NULL; addNumberOfFileToAttribute(&project); //---------------------------- @@ -331,23 +333,27 @@ static int convertFile(int argc, char* argv[], const string& fileToConv, initF2C_FunctionCalls(); // for project, acc_f2c.cpp for (int id = project.numberOfFiles() - 1; id >= 0; id--) { - file = &(project.file(id)); //file->unparsestdout(); + file = &(project.file(id)); fin_name = new char[strlen(project.fileName(id)) + 2]; sprintf(fin_name, "%s%s", project.fileName(id), " "); - fout_name = doOutFileName(file->filename()); //project.fileName(id); + + if (fout_name == NULL) + fout_name = doOutFileName(file->filename()); if (fout_name && source_name && !strcmp(source_name, fout_name)) { fprintf(stderr, "Output file has the same name as source file\n"); return 1; } - + + printf("convert %d to %s\n", id, fout_name); + fout_name_cuf = ChangeFtoCuf(fout_name); /*ACC*/ fout_name_C_cu = ChangeFto_C_Cu(fout_name); /*ACC*/ fout_name_info_C = ChangeFto_info_C(fout_name); /*ACC*/ //set the last symbol of file - last_file_symbol = CUR_FILE_CUR_SYMB(); + last_file_symbol = FileLastSymbol(file->filename()); initLibNames(); //for every file InitDVM(file); //for every file current_file = file; // global variable (used in SgTypeComplex) @@ -402,7 +408,22 @@ static int convertFile(int argc, char* argv[], const string& fileToConv, UnparseTo_CufAndCu_Files(file, fout_cuf, fout_C_cu, fout_info); const string fileN = file->filename(); - hideUnnecessary(file, fileN, moduleDeclsInFiles, true); + set toRemove; + + for (SgStatement* st = file->firstStatement(); st; st = st->lexNext()) + { + if (st->fileName() != fileN) + { + if (st->variant() == MODULE_STMT && moduleDeclsInFiles.find(st->fileName()) != moduleDeclsInFiles.end()) + { + toRemove.insert(st); + st = st->lastNodeOfStmt(); + } + } + } + + for (auto& toRem : toRemove) + toRem->extractStmt(); if (unparse_functions) UnparseFunctionsOfFile(file, fout); @@ -434,7 +455,7 @@ static int convertFile(int argc, char* argv[], const string& fileToConv, return 1; } } - + fout_name = NULL; } if (v_print) @@ -467,11 +488,11 @@ void convertFiles(int argc, char* argv[], const char* proj_name) const string fileText = readFileToStr(proj_name); vector files; - set filesinSet; + set filesInSet; splitString(fileText, '\n', files); for (auto& file : files) - filesinSet.insert(file); + filesInSet.insert(file); map> moduleUsesByFile; map moduleDecls; @@ -482,9 +503,9 @@ void convertFiles(int argc, char* argv[], const char* proj_name) moduleDeclsInFiles.insert(elem.second); int codes = 0; - for (auto& file : files) - { - codes += convertFile(args_v.size() - 1, args_v.data(), file, filesinSet, moduleDeclsInFiles); + //for (auto& file : files) + { + codes += convertFile(args_v.size(), args_v.data(), filesInSet, moduleDeclsInFiles); cur_node = node_list = NULL; InitializeTable(); diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp index 2adeab4..b9113d1 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp @@ -271,7 +271,7 @@ static int reverseVar(SgStatement* st) static map>> insertedIncludes; static set genVersionDone; -set hideUnnecessary(SgFile* file, const string& fileN, const set& moduleDeclsInFiles, bool dontReplaceIncludes) +static set hideUnnecessary(SgFile* file, const string& fileN, const set& moduleDeclsInFiles, bool dontReplaceIncludes) { set changedVars; for (SgStatement* st = file->firstStatement(); st; st = st->lexNext()) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h index 176a5cf..fcf0be8 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h @@ -11,7 +11,6 @@ SgStatement* declaratedInStmt(SgSymbol *toFind, std::vector *allDe #include "DefUseList.h" #include "CommonBlock.h" -std::set hideUnnecessary(SgFile* file, const std::string& fileN, const std::set& moduleDeclsInFiles, const bool dontReplaceIncludes); std::string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const char *fout, std::set &allIncludeFiles, bool outFree, const std::map> &moduleUsesByFile, const std::map& moduleDelcs, const std::map>& exctactedModuleStats, bool toString, bool dontReplaceIncls = false); SgSymbol* findSymbolOrCreate(SgFile *file, const std::string toFind, SgType *type = NULL, SgStatement *scope = NULL); void recExpressionPrint(SgExpression *exp); diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 98963f6..94bf617 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/version.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2286" +#define VERSION_SPF "2287" From 968491ee29d945d49ee62b688e2df636a06324f3 Mon Sep 17 00:00:00 2001 From: Grigorii Gusev Date: Sun, 17 Mar 2024 18:49:59 +0300 Subject: [PATCH 6/9] private_removing: fix pass manager --- sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h index feff4fd..c284eba 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -212,7 +212,8 @@ void InitPassesDependencies(map> &passDepsIn, set 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(REVERT_SUBST_EXPR_RD) <= Pass(PRIVATE_REMOVING); + list({ LOOP_ANALYZER_DATA_DIST_S1, SUBST_EXPR_RD } ) <= Pass(PRIVATE_REMOVING_ANALYSIS); + list({ PRIVATE_REMOVING_ANALYSIS, REVERT_SUBST_EXPR_RD }) <= Pass(PRIVATE_REMOVING); Pass(RESTORE_LOOP_FROM_ASSIGN) <= list({ SUBST_EXPR_AND_UNPARSE, SUBST_EXPR_RD_AND_UNPARSE }); From 093abbbd555b050e4ae5f8520d4bde894bd2ed6b Mon Sep 17 00:00:00 2001 From: ALEXks Date: Sun, 17 Mar 2024 19:15:58 +0300 Subject: [PATCH 7/9] version updated --- sapfor/experts/Sapfor_2017/_src/Utils/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 94bf617..0d65114 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/version.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2287" +#define VERSION_SPF "2288" From 0f39f6474d15f0ae1850401d34d4e1cf38d2113d Mon Sep 17 00:00:00 2001 From: ALEXks Date: Mon, 18 Mar 2024 14:49:04 +0300 Subject: [PATCH 8/9] fixed files switching --- dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp | 5 ++++ .../Sapfor_2017/_src/Utils/PassManager.h | 26 +++++++++---------- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp b/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp index 4c39cd8..c748f71 100644 --- a/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp +++ b/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp @@ -25,6 +25,8 @@ extern "C" void exit(int status); #include "extcxx_low.h" extern "C" int number_of_ll_node; +extern "C" PTR_SYMB last_file_symbol; +extern "C" PTR_SYMB FileLastSymbol(...); #undef USER @@ -1641,9 +1643,11 @@ SgFile &SgProject::file(int i) current_file_id = i; current_file = pt; + #ifdef __SPF SgStatement::setCurrProcessFile(pt->filename()); SgStatement::setCurrProcessLine(0); + last_file_symbol = FileLastSymbol(pt->filename()); #endif return *pt; } @@ -1777,6 +1781,7 @@ int SgFile::switchToFile(const std::string &name) } } + last_file_symbol = FileLastSymbol(name.c_str()); return it->second.second; } diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h index c284eba..7a4a3f2 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -243,7 +243,7 @@ void InitPassesDependencies(map> &passDepsIn, set Pass(MACRO_EXPANSION) <= Pass(CALL_GRAPH); - list({ PREPROC_SPF, PROCESS_IO, CALL_GRAPH2, CONVERT_SAVE_TO_MODULE, REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD }) <= Pass(CREATE_CHECKPOINTS); + list({ PREPROC_SPF, PROCESS_IO, CALL_GRAPH2, CONVERT_SAVE_TO_MODULE, REVERT_SUBST_EXPR_RD }) <= Pass(CREATE_CHECKPOINTS); Pass(FILL_PAR_REGIONS_LINES) <= Pass(VERIFY_EQUIVALENCE); @@ -255,9 +255,9 @@ void InitPassesDependencies(map> &passDepsIn, set list({ PREPROC_SPF, CALL_GRAPH2, FILL_PAR_REGIONS_LINES }) <= Pass(FILL_PAR_REGIONS) <= Pass(RESOLVE_PAR_REGIONS); - list({ REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN }) <= Pass(RESOLVE_PAR_REGIONS); + list({ REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN }) <= Pass(RESOLVE_PAR_REGIONS); - list({ REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD }) <= Pass(EXPAND_EXTRACT_PAR_REGION); + Pass(REVERT_SUBST_EXPR_RD) <= Pass(EXPAND_EXTRACT_PAR_REGION); Pass(FILL_PAR_REGIONS) <= Pass(PRINT_PAR_REGIONS_ERRORS); @@ -269,33 +269,33 @@ void InitPassesDependencies(map> &passDepsIn, set Pass(CALL_GRAPH2) <= list({ PURE_SAVE_TO_PARAMS, PURE_MODULE_TO_PARAMS, PURE_COMMON_TO_PARAMS, PURE_INTENT_INSERT }); - list({ REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD }) <= list({ PURE_SAVE_TO_PARAMS, PURE_MODULE_TO_PARAMS, PURE_COMMON_TO_PARAMS, PURE_INTENT_INSERT }); + Pass(REVERT_SUBST_EXPR_RD) <= list({ PURE_SAVE_TO_PARAMS, PURE_MODULE_TO_PARAMS, PURE_COMMON_TO_PARAMS, PURE_INTENT_INSERT }); - list({ CORRECT_VAR_DECL, REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD, VERIFY_INCLUDE }) <= list({ CONVERT_TO_ENDDO, CORRECT_CODE_STYLE, REMOVE_DVM_DIRS, REMOVE_DVM_DIRS_TO_COMMENTS, REMOVE_DVM_INTERVALS }); + list({ CORRECT_VAR_DECL, REVERT_SUBST_EXPR_RD, VERIFY_INCLUDE }) <= list({ CONVERT_TO_ENDDO, CORRECT_CODE_STYLE, REMOVE_DVM_DIRS, REMOVE_DVM_DIRS_TO_COMMENTS, REMOVE_DVM_INTERVALS }); - list({ CALL_GRAPH2, CONVERT_LOOP_TO_ASSIGN, REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD, RESTORE_LOOP_FROM_ASSIGN }) <= Pass(INLINE_PROCEDURES); + list({ CALL_GRAPH2, CONVERT_LOOP_TO_ASSIGN, REVERT_SUBST_EXPR_RD, RESTORE_LOOP_FROM_ASSIGN }) <= Pass(INLINE_PROCEDURES); list({ CONVERT_LOOP_TO_ASSIGN, CORRECT_FORMAT_PLACE }) <= list({ CONVERT_TO_ENDDO, CORRECT_CODE_STYLE, INSERT_INCLUDES, REMOVE_DVM_DIRS, REMOVE_DVM_DIRS_TO_COMMENTS, REMOVE_DVM_INTERVALS }); - list({ CORRECT_VAR_DECL, REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD }) <= list({ INSERT_INCLUDES, UNPARSE_FILE, SET_TO_ALL_DECL_INIT_ZERO }); + list({ CORRECT_VAR_DECL, REVERT_SUBST_EXPR_RD }) <= list({ INSERT_INCLUDES, UNPARSE_FILE, SET_TO_ALL_DECL_INIT_ZERO }); Pass(CALL_GRAPH2) <= Pass(PRIVATE_ARRAYS_SHRINKING_ANALYSIS) <= Pass(PRIVATE_ARRAYS_SHRINKING); - list({ CALL_GRAPH2, LOOP_ANALYZER_ALIGNS, REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD }) <= list({ PRIVATE_ARRAYS_EXPANSION, PRIVATE_ARRAYS_SHRINKING }); + list({ CALL_GRAPH2, LOOP_ANALYZER_ALIGNS, REVERT_SUBST_EXPR_RD }) <= list({ PRIVATE_ARRAYS_EXPANSION, PRIVATE_ARRAYS_SHRINKING }); list({ GCOV_PARSER, CREATE_INTER_TREE, CALL_GRAPH, CALL_GRAPH2 }) <= Pass(CREATE_PARALLEL_REGIONS); list({ PRIVATE_CALL_GRAPH_STAGE1, PRIVATE_CALL_GRAPH_STAGE2, MACRO_EXPANSION, CONVERT_ASSIGN_TO_LOOP, DEF_USE_STAGE1, DEF_USE_STAGE2, LOOP_GRAPH, CALL_GRAPH, PRIVATE_ANALYSIS_IR, FIND_FUNC_TO_INCLUDE }) <= Pass(INSERT_REGIONS); - list({ LOOP_ANALYZER_DATA_DIST_S1, REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD }) <= list({ LOOPS_SPLITTER, LOOPS_COMBINER, UNROLL_LOOPS, INSERT_REGIONS }); + list({ LOOP_ANALYZER_DATA_DIST_S1, REVERT_SUBST_EXPR_RD }) <= list({ LOOPS_SPLITTER, LOOPS_COMBINER, UNROLL_LOOPS, INSERT_REGIONS }); - list({ CALL_GRAPH2, REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= list({ DUPLICATE_FUNCTIONS, REMOVE_UNUSED_FUNCTIONS }); + list({ CALL_GRAPH2, REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= list({ DUPLICATE_FUNCTIONS, REMOVE_UNUSED_FUNCTIONS }); list({ CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= list({ LOOPS_SPLITTER, LOOPS_COMBINER, PRIVATE_ARRAYS_EXPANSION, PRIVATE_ARRAYS_SHRINKING, CREATE_PARALLEL_REGIONS, PURE_SAVE_TO_PARAMS, PURE_MODULE_TO_PARAMS, PURE_COMMON_TO_PARAMS, PURE_INTENT_INSERT }); list({ GET_ALL_ARRAY_DECL, FILL_PARALLEL_REG_IR }) <= Pass(CONVERT_ASSIGN_TO_LOOP); - list({ CALL_GRAPH2, REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD }) <= Pass(RENAME_SYMBOLS); + list({ CALL_GRAPH2, REVERT_SUBST_EXPR_RD }) <= Pass(RENAME_SYMBOLS); list({ BUILD_IR, CALL_GRAPH }) <= Pass(LIVE_ANALYSIS_IR); @@ -308,7 +308,7 @@ void InitPassesDependencies(map> &passDepsIn, set Pass(REMOVE_OMP_DIRS) <= Pass(REMOVE_OMP_DIRS_TRANSFORM); Pass(CALL_GRAPH2) <= Pass(REMOVE_DEAD_CODE); - list({ REMOVE_DEAD_CODE, REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= Pass(REMOVE_DEAD_CODE_AND_UNPARSE); + list({ REMOVE_DEAD_CODE, REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= Pass(REMOVE_DEAD_CODE_AND_UNPARSE); passesIgnoreStateDone.insert({ CREATE_PARALLEL_DIRS, INSERT_PARALLEL_DIRS, INSERT_SHADOW_DIRS, EXTRACT_PARALLEL_DIRS, EXTRACT_SHADOW_DIRS, CREATE_REMOTES, UNPARSE_FILE, REMOVE_AND_CALC_SHADOW, @@ -319,7 +319,7 @@ void InitPassesDependencies(map> &passDepsIn, set //only for print if (printTree) { - list({ CREATE_PARALLEL_DIRS, PRIVATE_ANALYSIS_IR, CREATE_REMOTES, REVERT_SUBST_EXPR, REVERT_SUBST_EXPR_RD, UNPARSE_FILE, EXTRACT_PARALLEL_DIRS }) <= Pass(INSERT_PARALLEL_DIRS); + list({ CREATE_PARALLEL_DIRS, PRIVATE_ANALYSIS_IR, CREATE_REMOTES, REVERT_SUBST_EXPR_RD, UNPARSE_FILE, EXTRACT_PARALLEL_DIRS }) <= Pass(INSERT_PARALLEL_DIRS); depsToGraphViz(passDepsIn); exit(0); } diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 0d65114..bd7a65f 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/version.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2288" +#define VERSION_SPF "2289" From d27b9d1ed3effb33755ef3387fc0a0a925fbaaf8 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 22 Mar 2024 12:00:01 +0300 Subject: [PATCH 9/9] dvm updated, fixed and improved dead_code pass --- dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp | 9 +- dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c | 25 ----- dvm/fdvm/trunk/fdvm/dvm.cpp | 3 +- .../_src/ProjectManipulation/ConvertFiles.cpp | 2 - .../_src/Transformations/dead_code.cpp | 100 ++++++++++-------- .../_src/Transformations/dead_code.h | 3 +- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 7 files changed, 64 insertions(+), 80 deletions(-) diff --git a/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp b/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp index c748f71..dc7874e 100644 --- a/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp +++ b/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp @@ -26,7 +26,6 @@ extern "C" void exit(int status); #include "extcxx_low.h" extern "C" int number_of_ll_node; extern "C" PTR_SYMB last_file_symbol; -extern "C" PTR_SYMB FileLastSymbol(...); #undef USER @@ -1647,7 +1646,7 @@ SgFile &SgProject::file(int i) #ifdef __SPF SgStatement::setCurrProcessFile(pt->filename()); SgStatement::setCurrProcessLine(0); - last_file_symbol = FileLastSymbol(pt->filename()); + last_file_symbol = file->cur_symb; #endif return *pt; } @@ -1775,13 +1774,13 @@ int SgFile::switchToFile(const std::string &name) SgFile *file = &(CurrentProject->file(it->second.second)); current_file_id = it->second.second; current_file = file; - + SgStatement::setCurrProcessFile(file->filename()); SgStatement::setCurrProcessLine(0); + last_file_symbol = current_file->filept->cur_symb; } } - - last_file_symbol = FileLastSymbol(name.c_str()); + return it->second.second; } diff --git a/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c b/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c index 3b95b45..02bf326 100644 --- a/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c +++ b/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c @@ -83,12 +83,6 @@ int out_upper_case; int out_line_unlimit; int out_line_length; // out_line_length = 132 for -ffo mode; out_line_length = 72 for -uniForm mode PTR_SYMB last_file_symbol; -struct file_symbol { - char *fname; - PTR_SYMB last_symb; - struct file_symbol *next; -} ; -struct file_symbol *file_last_symbol = NULL; static int CountNullBifNext = 0; /* for internal debugging */ @@ -253,24 +247,6 @@ char* mymalloc(int size) return pt1; } -PTR_SYMB FileLastSymbol(char *file_name) -{ - struct file_symbol *fsl; - for(fsl=file_last_symbol; fsl; fsl=fsl->next) - if(!strcmp(file_name, fsl->fname)) - return fsl->last_symb; - return NULL; -} - -void addFileSymbolList(char *file_name, PTR_SYMB symb) -{ - struct file_symbol *fsl; - fsl = (struct file_symbol *) xmalloc(sizeof (struct file_symbol)); - fsl->last_symb = symb; - fsl->fname = file_name; - fsl->next = file_last_symbol; - file_last_symbol = fsl; -} /***************** Provides infos on nodes ******************************** * * * based on the table info in include dir *.def * @@ -1228,7 +1204,6 @@ int Init_Tool_Box() number_of_ll_node = CUR_FILE_NUM_LLNDS() + 1; number_of_bif_node = CUR_FILE_NUM_BIFS() + 1; number_of_symb_node = CUR_FILE_NUM_SYMBS() + 1; - addFileSymbolList(CUR_FILE_NAME(), CUR_FILE_CUR_SYMB()); /* podd 01.03.24 */ if (CUR_FILE_NAME()) strcpy(Current_File_name, CUR_FILE_NAME()); if (ToolBOX_INIT) return 0; diff --git a/dvm/fdvm/trunk/fdvm/dvm.cpp b/dvm/fdvm/trunk/fdvm/dvm.cpp index 58d9b7b..2244f69 100644 --- a/dvm/fdvm/trunk/fdvm/dvm.cpp +++ b/dvm/fdvm/trunk/fdvm/dvm.cpp @@ -82,7 +82,6 @@ extern "C" int out_upper_case; extern "C" int out_line_unlimit; extern "C" int out_line_length; extern "C" PTR_SYMB last_file_symbol; -extern "C" PTR_SYMB FileLastSymbol(...); Options options; @@ -418,7 +417,7 @@ int main(int argc, char *argv[]) fout_name_info_C = ChangeFto_info_C(fout_name); /*ACC*/ //set the last symbol of file - last_file_symbol = FileLastSymbol(file->filename()); //for low_level.c and not only + last_file_symbol = file->filept->cur_symb; //for low_level.c and not only initLibNames(); //for every file InitDVM(file); //for every file current_file = file; // global variable (used in SgTypeComplex) diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp index db7a1ce..78610d8 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp @@ -31,7 +31,6 @@ extern "C" int out_upper_case; extern "C" int out_line_unlimit; extern "C" int out_line_length; extern "C" PTR_SYMB last_file_symbol; -extern "C" PTR_SYMB FileLastSymbol(...); static int convertFile(int argc, char* argv[], const set& filesInProj, const set& moduleDeclsInFiles) { @@ -353,7 +352,6 @@ static int convertFile(int argc, char* argv[], const set& filesInProj, c fout_name_info_C = ChangeFto_info_C(fout_name); /*ACC*/ //set the last symbol of file - last_file_symbol = FileLastSymbol(file->filename()); initLibNames(); //for every file InitDVM(file); //for every file current_file = file; // global variable (used in SgTypeComplex) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index e09ce14..1ec6519 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -13,7 +13,7 @@ using std::set; using std::remove_if; -#define PRINT_USELESS_STATEMENTS 0 +#define PRINT_USELESS_STATEMENTS 1 static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, set& use, set& def, @@ -329,10 +329,22 @@ public: void removeDeadCode(SgStatement* func, const map>& allFuncs, - const map& commonBlocks) + const map& commonBlocks, + SgStatement* intervalDelStart, SgStatement* intervalDelEnd) { + if (intervalDelStart && !intervalDelEnd || !intervalDelStart && intervalDelEnd) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + SgProgHedrStmt* prog = isSgProgHedrStmt(func); + if (intervalDelStart) + if (intervalDelStart->lineNumber() < prog->lineNumber() || intervalDelStart->lineNumber() > prog->lastNodeOfStmt()->lineNumber()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + if (intervalDelEnd) + if (intervalDelEnd->lineNumber() < prog->lineNumber() || intervalDelEnd->lineNumber() > prog->lastNodeOfStmt()->lineNumber()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + auto cfg = buildCFGforCurrentFunc(func, SAPFOR::CFG_Settings(true, false, false, false, false, false, false), commonBlocks, allFuncs); if(cfg.size() != 1) @@ -422,11 +434,8 @@ void removeDeadCode(SgStatement* func, } } } - +//TODO: need to add [intervalDelStart; intervalDelEnd] // remove dead statements - - SgStatement* end = func->lastNodeOfStmt(), *st = func; - set removable = { ASSIGN_STAT, @@ -435,49 +444,52 @@ void removeDeadCode(SgStatement* func, READ_STAT }; - while (st != end) + vector remove; + for (auto st = func; st != func->lastNodeOfStmt(); st = st->lexNext()) { - SgStatement* next = st->lexNext(); - if (removable.find(st->variant()) != removable.end() && useful.find(st) == useful.end()) + { + remove.push_back(st); + st = st->lastNodeOfStmt(); + } + } + + for (auto& rem : remove) + { + __spf_print(PRINT_USELESS_STATEMENTS, "[Useless statement on line %d and file %s]\n", rem->lineNumber(), rem->fileName()); + rem->deleteStmt(); + } + + remove.clear(); + //remove empty blocks + for (auto st = func; st != func->lastNodeOfStmt(); st = st->lexNext()) + { + const int var = st->variant(); + if ((var == FOR_NODE || var == WHILE_NODE || var == IF_NODE || var == SWITCH_NODE) && + st->lexNext()->variant() == CONTROL_END) { - __spf_print(PRINT_USELESS_STATEMENTS, "[Useless statement '%s' on line %d]\n", st->unparse(), st->lineNumber()); - - st->deleteStmt(); - } - else - { - if (isSgControlEndStmt(st)) - { - SgStatement* parent = st->controlParent(); - SgStatement* parent_end; - - if (parent && (parent_end = parent->lastNodeOfStmt()) && parent_end == st) - { - bool empty_parent = false; - - switch (parent->variant()) - { - case IF_NODE: - empty_parent = - parent->lexNext() == parent_end || // IF THEN ENDIF - isSgControlEndStmt(parent->lexNext()) && - parent->lexNext()->lexNext() == parent_end; // IF THEN ELSE ENDIF - break; - default: - empty_parent = parent->lexNext() == parent_end; // DO, WHILE - break; - } - - if (empty_parent) - parent->deleteStmt(); - else if(isSgIfStmt(parent) && isSgControlEndStmt(parent_end->lexPrev())) // IF with empty ELSE branch - parent_end->deleteStmt(); - } - } + remove.push_back(st); + continue; } - st = next; + if (var == IF_NODE) + { + SgStatement* ifS = st; + while (ifS->lexNext()->variant() == ELSEIF_NODE) + ifS = ifS->lexNext(); + + if (ifS->lexNext()->variant() == CONTROL_END) + remove.push_back(st); + } + + //TODO: SWITCH and other block statements + } + + + for (auto& rem : remove) + { + __spf_print(PRINT_USELESS_STATEMENTS, "[Useless block statement on line %d and file %s]\n", rem->lineNumber(), rem->fileName()); + rem->deleteStmt(); } deleteCFG(cfg); diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.h b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.h index 7c83b1d..6f3ef35 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.h +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.h @@ -11,4 +11,5 @@ void removeDeadCode(SgStatement* func, const std::map>&allFuncs, - const std::map& commonBlocks); \ No newline at end of file + const std::map& commonBlocks, + SgStatement* intervalDelStart = NULL, SgStatement* intervalDelEnd = NULL); \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index bd7a65f..3b5edc5 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/version.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2289" +#define VERSION_SPF "2290"