From a882be75e952669a42bef23f582f2cfab2d91c90 Mon Sep 17 00:00:00 2001 From: DenisDudarenko Date: Mon, 11 Dec 2023 21:41:31 +0300 Subject: [PATCH] WIP : Add new pass - set-implisit-none --- sapfor/experts/Sapfor_2017/CMakeLists.txt | 7 +- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 4 + sapfor/experts/Sapfor_2017/_src/Sapfor.h | 3 + .../Transformations/set_implicit_none.cpp | 187 ++++++++++++++++++ .../_src/Transformations/set_implicit_none.h | 7 + .../Sapfor_2017/_src/Utils/PassManager.h | 2 + 6 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp create mode 100644 sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.h diff --git a/sapfor/experts/Sapfor_2017/CMakeLists.txt b/sapfor/experts/Sapfor_2017/CMakeLists.txt index d6c259b..5010d0c 100644 --- a/sapfor/experts/Sapfor_2017/CMakeLists.txt +++ b/sapfor/experts/Sapfor_2017/CMakeLists.txt @@ -187,6 +187,9 @@ set(TR_FUNC_PURE _src/Transformations/function_purifying.cpp _src/Transformations/function_purifying.h) set(TR_GV _src/Transformations/fix_common_blocks.cpp _src/Transformations/fix_common_blocks.h) +set(TR_IMPLICIT_NONE _src/Transformations/set_implicit_none.cpp + _src/Transformations/set_implicit_none.h) + set(TRANSFORMS ${TR_CP} ${TR_VECTOR} @@ -200,7 +203,8 @@ set(TRANSFORMS ${TR_FUNC_PURE} ${TR_LOOP_UNROLL} ${TR_GV} - ${TR_PRIV_DEL}) + ${TR_PRIV_DEL} + ${TR_IMPLICIT_NONE}) set(CFG _src/CFGraph/IR.cpp _src/CFGraph/IR.h @@ -419,6 +423,7 @@ source_group (Transformations\\PrivateArrayRemoving FILES ${TR_PRIV_DEL}) source_group (Transformations\\VectorAssignToLoop FILES ${TR_VECTOR}) source_group (Transformations\\RenameSymbols FILES ${RENAME_SYMBOLS}) source_group (Transformations\\GlobalVariables FILES ${TR_GV}) +source_group (Transformations\\SetImplicitNone FILES ${TR_IMPLICIT_NONE}) source_group (CreateIntervals FILES ${CREATE_INTER_T}) diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index 9dda347..46f802b 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -78,6 +78,7 @@ #include "Transformations/function_purifying.h" #include "Transformations/private_removing.h" #include "Transformations/fix_common_blocks.h" +#include "Transformations/set_implicit_none.h" #include "RenameSymbols/rename_symbols.h" #include "ProjectParameters/projectParameters.h" @@ -2175,6 +2176,8 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne runPrivateVariableAnalysis(loopGraph, fullIR, commonBlocks, SPF_messages); else if (curr_regime == FIX_COMMON_BLOCKS) fixCommonBlocks(allFuncInfo, commonBlocks, &project); + else if (curr_regime == SET_IMPLICIT_NONE) + ImplicitCheck(&project); else if (curr_regime == SELECT_ARRAY_DIM_CONF) { map> localMessages; @@ -2608,6 +2611,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case LOOPS_SPLITTER: case LOOPS_COMBINER: case FIX_COMMON_BLOCKS: + case SET_IMPLICIT_NONE: case TEST_PASS: runAnalysis(*project, curr_regime, false); case SUBST_EXPR_RD_AND_UNPARSE: diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.h b/sapfor/experts/Sapfor_2017/_src/Sapfor.h index 957a1f8..62ad7ef 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.h +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.h @@ -168,6 +168,8 @@ enum passes { FIX_COMMON_BLOCKS, + SET_IMPLICIT_NONE, + TEST_PASS, EMPTY_PASS }; @@ -339,6 +341,7 @@ static void setPassValues() passNames[PRIVATE_ANALYSIS_IR] = "PRIVATE_ANALYSIS_IR"; passNames[FIX_COMMON_BLOCKS] = "FIX_COMMON_BLOCKS"; + passNames[SET_IMPLICIT_NONE] = "SET_IMPLICIT_NONE"; passNames[TEST_PASS] = "TEST_PASS"; } diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp new file mode 100644 index 0000000..6c4de80 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp @@ -0,0 +1,187 @@ +#include +#include +#include +#include + +#include "Utils/SgUtils.h" +#include "set_implicit_none.h" + +using std::vector; +using std::map; +using std::multimap; +using std::set; +using std::make_pair; +using std::string; +using std::to_string; + +map implicit; +set vars; +vector toDecl; + +static void InsertToMap(SgType* type, SgExpression* letters) { + + while (letters != NULL && letters->lhs() != NULL) { + + string letter; + + switch (letters->lhs()->sunparse().size()) { + + case 3: + letter = letters->lhs()->sunparse(); + implicit[letter[1]] = type; + + break; + case 7: + letter = letters->lhs()->sunparse(); + + for (char i = letter[1]; i <= letter[5]; i++) { + implicit[i] = type; + } + break; + default : + //printf("IMPLICIT bad format (can bew only {letter : letter} or {letter})"); + throw; + } + + letters = letters->rhs(); + } + +} + +static void InsertDefaultToMap() { + SgType* intType = new SgType(T_INT); + + SgType* realType = new SgType(T_FLOAT); + + for (char i = 'a'; i <= 'z'; i++) { + implicit[i] = realType; + } + + for (char i = 'i'; i <= 'n'; i++) { + implicit[i] = intType; + } +} + +static void CheckVariables(SgStatement* function) { + + for (int k = 0; k < function->numberOfChildrenList1(); k++) { + SgStatement* state = function->childList1(k); + + + if (state->expr(0) && (state->expr(0)->variant() == VAR_REF || state->expr(0)->variant() == ARRAY_REF)) { + SgSymbol* symbol = state->expr(0)->symbol(); + auto x = declaratedInStmt(symbol, NULL, false); + + if (vars.find(symbol) == vars.end() && x == NULL) { + vars.insert(symbol); + + char firstLetter = *symbol->identifier(); + + if (implicit.find(firstLetter) != implicit.end()) { + if (implicit[firstLetter]->variant() != symbol->type()->variant()) { + symbol->setType(implicit[firstLetter]); + + if (symbol->declaredInStmt() == NULL) { + toDecl.push_back(symbol); + } + } + else { + toDecl.push_back(symbol); + } + } + else { + //printf("Variable - "); + //printf(symbol->identifier()); + //printf(" - not found in IMPLICIT\n"); + throw; + } + } + } + } + + makeDeclaration(toDecl, function, NULL); +} + + +void ImplicitCheck(SgProject* project) { + + /* + printf("IMPLICIT\n"); + */ + + for (int i = 0; i < project->numberOfFiles(); i++) { + SgFile file = project->file(i); + int coutOfFunctions; + for (int j = 0; j < file.numberOfFunctions(); j++) { + SgStatement* function = file.functions(j); + + implicit.clear(); + vars.clear(); + toDecl.clear(); + + //fill implicit + + for (int k = 0; k < function->numberOfChildrenList1(); k++) { + SgStatement* state = function->childList1(k); + + + + if (state->variant() == IMPL_DECL) { + + if (state->expr(0) == NULL) { + + } + else { + SgExpression* expression = state->expr(0); + int n = 0; + SgExpression* one = expression; + + while (one != NULL && one->lhs() != NULL) { + + string what_type = one->lhs()->sunparse(); + + if (what_type.find("kind=") != std::string::npos) { + + SgType* baseType = new SgType(one->lhs()->type()->variant()); + + SgType* type = new SgType(T_ARRAY, NULL, baseType); + + SgExpression* letters = one->lhs()->operand(1); + + InsertToMap(type, letters); + } + else { + + SgType* type = new SgType(one->lhs()->type()->variant()); + + SgExpression* letters = one->lhs()->operand(1); + + InsertToMap(type, letters); + } + + one = one->rhs(); + } + + } + + break; + } + + if (state->variant() == CONTROL_END) { + + SgStatement* state = new SgStatement(IMPL_DECL); + auto cp = function->childList1(0)->controlParent(); + function->childList1(0)->insertStmtBefore(*state, *cp); + + + InsertDefaultToMap(); + + break; + } + } + + CheckVariables(function); + + } + } +} diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.h b/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.h new file mode 100644 index 0000000..985c6d5 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.h @@ -0,0 +1,7 @@ +#pragma once +#include "Utils/SgUtils.h" + +#include "string" +#include "map" + +void ImplicitCheck(SgProject* project); \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h index 7b584e5..ac40cc4 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -302,6 +302,8 @@ void InitPassesDependencies(map> &passDepsIn, set Pass(CALL_GRAPH2) <= Pass(FIX_COMMON_BLOCKS); + Pass(CALL_GRAPH2) <= Pass(SET_IMPLICIT_NONE); + 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, REVERSE_CREATED_NESTED_LOOPS, PREDICT_SCHEME, CALCULATE_STATS_SCHEME, REVERT_SPF_DIRS, CLEAR_SPF_DIRS, TRANSFORM_SHADOW_IF_FULL,