From 110970caa6ea21803dfc4db5d42bd6f86d43afa6 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Mon, 11 Dec 2023 20:53:33 +0300 Subject: [PATCH 01/40] fixed inliner, version updated --- sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp | 2 +- sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp | 2 +- sapfor/experts/Sapfor_2017/_src/Utils/version.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp b/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp index c8ea081..c55acd2 100644 --- a/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp @@ -2386,7 +2386,7 @@ static void convertLinesToAbsolute(const map>& allFunc if (detCall.detailCallsFrom == make_pair(funcToInl, targetLine) && added.find(make_tuple(file, funcToInl, targetLine)) == added.end()) { - __spf_print(1, "%s %d (was %d) %s\n", funcToInl, targetLine, std::get<2>(inDataProc[z]), funcByFile.first.c_str()); + __spf_print(1, "%s %d (was %d) %s\n", funcToInl.c_str(), targetLine, std::get<2>(inDataProc[z]), funcByFile.first.c_str()); added.insert(make_tuple(file, funcToInl, targetLine)); absoluteLine = targetLine; break; diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp index 62dc4e0..432dfd2 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp @@ -2918,7 +2918,7 @@ private: { #ifdef _MSC_VER ret = pipe(pipes, 1024 * 1024 * 20, O_BINARY); // 20 MB -#else +#else ret = pipe(pipes) == -1; fcntl(*pipes, F_SETPIPE_SZ, 1024 * 1024 * 20); #endif diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 10b00ca..d049651 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 "2255" +#define VERSION_SPF "2256" From 1f3d9944b85e302f2cce336c24937e6e8de8cb0e Mon Sep 17 00:00:00 2001 From: ALEXks Date: Sat, 16 Dec 2023 17:42:11 +0300 Subject: [PATCH 02/40] improved and fixed FDVM, added ISNERT_INCLUDES pass --- dvm/fdvm/trunk/fdvm/acc.cpp | 3 + dvm/fdvm/trunk/fdvm/acc_across.cpp | 982 ++++++++---------- dvm/fdvm/trunk/fdvm/aks_structs.cpp | 134 ++- dvm/fdvm/trunk/fdvm/dvm.cpp | 6 +- dvm/fdvm/trunk/fdvm/funcall.cpp | 2 +- dvm/fdvm/trunk/fdvm/parloop.cpp | 4 +- dvm/fdvm/trunk/include/aks_structs.h | 240 ++--- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 12 +- sapfor/experts/Sapfor_2017/_src/SapforData.h | 3 +- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 10 files changed, 613 insertions(+), 775 deletions(-) diff --git a/dvm/fdvm/trunk/fdvm/acc.cpp b/dvm/fdvm/trunk/fdvm/acc.cpp index 81fbf61..143323c 100644 --- a/dvm/fdvm/trunk/fdvm/acc.cpp +++ b/dvm/fdvm/trunk/fdvm/acc.cpp @@ -2532,7 +2532,10 @@ void ACC_ParallelLoopEnd(SgStatement *pardo) } if (options.isOn(LOOP_ANALYSIS)) + { delete currentLoop; + currentLoop = NULL; + } } if (options.isOn(RTC)) diff --git a/dvm/fdvm/trunk/fdvm/acc_across.cpp b/dvm/fdvm/trunk/fdvm/acc_across.cpp index ac726b5..279fa0e 100644 --- a/dvm/fdvm/trunk/fdvm/acc_across.cpp +++ b/dvm/fdvm/trunk/fdvm/acc_across.cpp @@ -29,8 +29,8 @@ extern void searchIdxs(vector &allInfo, SgExpression *st); extern int warpSize; // local functions -ArgsForKernel **Create_C_Adapter_Function_Across_variants(SgSymbol*, SgSymbol*, const int, const int, const int, SageSymbols**, SageSymbols**); -ArgsForKernel **Create_C_Adapter_Function_Across_OneThread(SgSymbol*, SgSymbol*, const int, const int); +vector Create_C_Adapter_Function_Across_variants(SgSymbol*, SgSymbol*, const int, const int, const int, const vector&, const vector&); +vector Create_C_Adapter_Function_Across_OneThread(SgSymbol*, SgSymbol*, const int, const int); symb_list* AddToSymbList(symb_list*, SgSymbol*); symb_list* AddNewToSymbList(symb_list*, SgSymbol*); void CreateReductionBlocksAcross(SgStatement*, int, SgExpression*, SgSymbol*); @@ -51,8 +51,8 @@ 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"; -static const char *funcDvmhConvXYfortVerLong = " attributes(device) subroutine dvmh_convert_XY_llong(x,y,Rx,Ry,slash,idx)\n implicit none\n integer*8 ,value:: x\n integer*8 ,value:: y\n integer*8 ,value:: Rx\n integer*8 ,value:: Ry\n integer*8 ,value:: slash\n integer*8 ,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" ; +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"; +static const char *funcDvmhConvXYfortVerLong = " attributes(device) subroutine dvmh_convert_XY_llong(x,y,Rx,Ry,slash,idx)\n implicit none\n integer*8 ,value:: x\n integer*8 ,value:: y\n integer*8 ,value:: Rx\n integer*8 ,value:: Ry\n integer*8 ,value:: slash\n integer*8 ,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" ; static const char* fermiPreprocDir = "CUDA_FERMI_ARCH"; // local variables @@ -79,7 +79,7 @@ static inline void mywarn(const char *str) { #if debugMode printf("%s\n", str); -#endif +#endif } static char *getLoopLine(const char *sadapter) @@ -122,20 +122,6 @@ static SgExpression *RegisterReduction_forAcross(SgSymbol *s_loop_ref, SgSymbol return fe; } -SgStatement* makeSymbolDeclarationWithInit_T(SgSymbol *Init, SgSymbol *Value) -{ - SgStatement *st; - SgExpression *e; - st = new SgStatement(VAR_DECL); - - e = new SgExpression(CLASSINIT_OP); - e->setLhs(SgMakeDeclExp(Init, Init->type())); - e->setRhs(new SgExprListExp(*new SgVarRefExp(Value))); - st->setExpression(0, *new SgExprListExp(*e)); - - return st; -} - SgExpression *CreateBlocksThreadsSpec(SgSymbol *s_shared, SgSymbol *s_blocks, SgSymbol *s_threads, SgSymbol *s_stream) { SgExprListExp *el, *ell, *elm; @@ -159,7 +145,7 @@ SgExpression* CreateBlocksThreadsSpec(int size, SgSymbol *s_blocks, SgSymbol *s_ el = new SgExprListExp(*new SgVarRefExp(s_blocks)); ell = new SgExprListExp(*new SgVarRefExp(s_threads)); el->setRhs(ell); - //size==0 - parallel loop without reduction clause + //size==0 - parallel loop without reduction clause mult = size ? &((*ThreadsGridSize(s_threads)) * (*new SgValueExp(size))) : new SgValueExp(size); elm = new SgExprListExp(*mult); ell->setRhs(elm); @@ -181,7 +167,7 @@ static void getDefaultCudaBlock(int &x, int &y, int &z, int loopDep, int loopInd { if (loopDep == 0) { - if (loopIndep == 1) { x = 256; y = 1; z = 1; } + if (loopIndep == 1) { x = 256; y = 1; z = 1; } else if (loopIndep == 2) { x = 32; y = 14; z = 1; } else { x = 32; y = 7; z = 2; } } @@ -195,12 +181,12 @@ static void getDefaultCudaBlock(int &x, int &y, int &z, int loopDep, int loopInd else if (loopDep == 2) { if (loopIndep == 0) { x = 32; y = 1; z = 1; } - else if (loopIndep == 1) { x = 32; y = 4; z = 1; } + else if (loopIndep == 1) { x = 32; y = 4; z = 1; } else { x = 16; y = 8; z = 2; } } else if (loopDep >= 3) { - if (loopIndep == 0) { x = 32; y = 5; z = 1; } + if (loopIndep == 0) { x = 32; y = 5; z = 1; } else { x = 32; y = 5; z = 2; } } } @@ -291,7 +277,7 @@ static int getSizeOf() return ret; } -SgStatement *CreateKernelProcedureDevice(SgSymbol *skernel) +static SgStatement *CreateKernelProcedureDevice(SgSymbol *skernel) { SgStatement *st, *st_end; SgExpression *e; @@ -313,7 +299,7 @@ SgStatement *CreateKernelProcedureDevice(SgSymbol *skernel) return st; } -SgStatement* AssignStatement(SgExpression &lhs, SgExpression &rhs) +static SgStatement* AssignStatement(SgExpression &lhs, SgExpression &rhs) { SgStatement *st; if (options.isOn(C_CUDA)) @@ -323,36 +309,36 @@ SgStatement* AssignStatement(SgExpression &lhs, SgExpression &rhs) return st; } -SgSymbol *createVariantOfSAdapter(SgSymbol *sadapter, char *variant) +static char* createName(const char* oldName, const char* variant) { - SgSymbol *s_adapter, *s_tmp; - char *oldName = sadapter->identifier(); - char *correctName = new char[strlen(oldName) + strlen(variant) + 1]; + char* correctName = new char[strlen(oldName) + strlen(variant) + 1]; correctName[0] = '\0'; strcat(correctName, oldName); strcat(correctName, variant); - s_adapter = new SgSymbol(FUNCTION_NAME, correctName, *C_VoidType(), *block_C); - s_tmp = new SgSymbol(PROCEDURE_NAME, correctName, *current_file->firstStatement()); + + return correctName; +} + +static SgSymbol *createVariantOfSAdapter(SgSymbol *sadapter, const char *variant) +{ + SgSymbol *s_adapter; + const char *oldName = sadapter->identifier(); + s_adapter = new SgSymbol(FUNCTION_NAME, createName(oldName, variant), *C_VoidType(), *block_C); return s_adapter; } -SgSymbol *createVariantOfKernelSymbol(SgSymbol *kernel_symb, char *variant) +static SgSymbol *createVariantOfKernelSymbol(SgSymbol *kernel_symb, const char *variant) { SgSymbol *sk; char *oldName = kernel_symb->identifier(); - char *correctName = new char[strlen(oldName) + strlen(variant) + 1]; - correctName[0] = '\0'; - strcat(correctName, oldName); - strcat(correctName, variant); - - sk = new SgSymbol(PROCEDURE_NAME, correctName, *mod_gpu); + sk = new SgSymbol(PROCEDURE_NAME, createName(oldName, variant), *mod_gpu); if (options.isOn(C_CUDA)) sk->setType(C_VoidType()); return sk; } -void createNewAdapter(SgSymbol *sadapter, ParamsForAllVariants &newVar, char *str) +static void createNewAdapter(SgSymbol *sadapter, ParamsForAllVariants &newVar, char *str) { SgSymbol *s_adapter; char *nameOfNewSAdapter; @@ -366,7 +352,7 @@ void createNewAdapter(SgSymbol *sadapter, ParamsForAllVariants &newVar, char *st newVar.s_adapter = s_adapter; } -void createNewKernel(SgSymbol *kernel_symb, ParamsForAllVariants &newVar, char *str) +static void createNewKernel(SgSymbol *kernel_symb, ParamsForAllVariants &newVar, char *str) { SgSymbol *s_ks; char *nameOfNewSK; @@ -380,30 +366,6 @@ void createNewKernel(SgSymbol *kernel_symb, ParamsForAllVariants &newVar, char * newVar.s_kernel_symb = s_ks; } -static SgSymbol* getSymbByNum(SageSymbols *allSymb, int place) -{ - SageSymbols *tmp = allSymb; - for (int i = 0; ; ++i) - { - if (i == place) - return tmp->symb; - else - tmp = tmp->next; - } -} - -static int getLongByType(int type[], int num) -{ - int ret = 0; - int p = 1; - for (int i = num - 1; i >= 0; i--) - { - ret += type[i] * p; - p = p << 1; - } - return ret; -} - static int countBit(int num) { int ret = 0; @@ -431,9 +393,10 @@ static void generateAllBitmasks(int dep, int all, vector &out) } } -static void GetAllCombinations2(vector &allVariants, SgSymbol *sadapter, SgSymbol *kernel_symb, int numAcr, int sizeOfAllSymb, SageSymbols *allSymb) +static void GetAllCombinations2(vector &allVariants, SgSymbol *sadapter, SgSymbol *kernel_symb, int numAcr, + const vector& allSymb) { - int *bitmask = new int[(unsigned)sizeOfAllSymb]; + const unsigned sizeOfAllSymb = allSymb.size(); char *tmpstrAdapter = new char[16]; char *tmpstrKernel = new char[16]; @@ -442,20 +405,13 @@ static void GetAllCombinations2(vector &allVariants, SgSym ParamsForAllVariants newVar; newVar.allDims = sizeOfAllSymb; - newVar.loopSymb = new SageSymbols*[numLoopVars]; - newVar.loopAcrossSymb = new SageSymbols*[numLoopVars]; + newVar.loopSymb.resize(numLoopVars); + newVar.loopAcrossSymb.resize(numLoopVars); newVar.nameOfNewSAdapter = NULL; newVar.s_adapter = NULL; newVar.acrossV = numAcr; newVar.loopV = newVar.allDims - newVar.acrossV; - for (int bit = 0; bit < sizeOfAllSymb; ++bit) - { - if (bit > sizeOfAllSymb - numAcr - 1) - bitmask[bit] = 1; - else - bitmask[bit] = 0; - } - newVar.type = getLongByType(bitmask, sizeOfAllSymb); + newVar.type = (1 << numAcr) - 1; sprintf(tmpstrAdapter, "%d", newVar.type); strcat(tmpstrAdapter, "_case"); @@ -470,45 +426,40 @@ static void GetAllCombinations2(vector &allVariants, SgSym { if (r < numAcr) { - newVar.loopAcrossSymb[r] = new SageSymbols(); - newVar.loopAcrossSymb[r]->across_left = newVar.loopAcrossSymb[r]->across_right = 0; - newVar.loopAcrossSymb[r]->symb = getSymbByNum(allSymb, sizeOfAllSymb - r - 1); - newVar.loopAcrossSymb[r]->len = sizeOfAllSymb - r - 1; + newVar.loopAcrossSymb[r].across_left = newVar.loopAcrossSymb[r].across_right = 0; + newVar.loopAcrossSymb[r].symb = allSymb[sizeOfAllSymb - r - 1].symb; + newVar.loopAcrossSymb[r].len = sizeOfAllSymb - r - 1; } else { - newVar.loopSymb[k] = new SageSymbols(); - newVar.loopSymb[k]->across_left = newVar.loopSymb[k]->across_right = 0; - newVar.loopSymb[k]->symb = getSymbByNum(allSymb, sizeOfAllSymb - r - 1); - newVar.loopSymb[k]->len = sizeOfAllSymb - r - 1; + newVar.loopSymb[k].across_left = newVar.loopSymb[k].across_right = 0; + newVar.loopSymb[k].symb = allSymb[sizeOfAllSymb - r - 1].symb; + newVar.loopSymb[k].len = sizeOfAllSymb - r - 1; k++; } } allVariants.push_back(newVar); - - delete[]bitmask; } static void GetAllVariants2(vector &allVariants, SgSymbol *sadapter, SgSymbol *kernel_symb) { - int allDims = 0, acrossV = 0; + int acrossV = 0; - SageAcrossInfo *Info = GetLoopsWithParAndAcrDir(); - SageSymbols *allSymb = GetSymbInParalell(&allDims, dvm_parallel_dir->expr(2)); - SageArrayIdxs *idxInfo = Info->idx->next; - while (idxInfo && (acrossV < allDims)) + SageAcrossInfo Info = GetLoopsWithParAndAcrDir(); + vector allSymb = GetSymbInParalell(dvm_parallel_dir->expr(2)); + const int allDims = allSymb.size(); + + for (int z = 0; z < Info.idxs.size() && (acrossV < allDims); ++z) { - for (int i = 0; i < idxInfo->dim && (acrossV < allDims); ++i) - { - if (idxInfo->symb[i]->across_left != 0 || idxInfo->symb[i]->across_right != 0) + SageArrayIdxs& idxInfo = Info.idxs[z]; + for (int i = 0; i < idxInfo.dim && (acrossV < allDims); ++i) + if (idxInfo.symb[i].across_left != 0 || idxInfo.symb[i].across_right != 0) acrossV++; - } - idxInfo = idxInfo->next; } // correct dependencies lvl only for ACROSS with one dep SgStatement *st = loop_body; - + SgExpression* dvmDir = dvm_parallel_dir->expr(1); vector allInfo; bool nextStep = true; @@ -532,7 +483,7 @@ static void GetAllVariants2(vector &allVariants, SgSymbol } list = list->rhs(); } - + for (int i = 0; i < toAnalyze.size(); ++i) { SgExpression* array = toAnalyze[i]; @@ -590,7 +541,7 @@ static void GetAllVariants2(vector &allVariants, SgSymbol break; } } - + if (nextStep) { SgExpression* dvmDir = dvm_parallel_dir->expr(2); @@ -644,7 +595,7 @@ static void GetAllVariants2(vector &allVariants, SgSymbol } } for (int i = 1; i <= acrossV; ++i) - GetAllCombinations2(allVariants, sadapter, kernel_symb, i, allDims, allSymb); + GetAllCombinations2(allVariants, sadapter, kernel_symb, i, allSymb); } /*void printAllVars(vector &vectorT) @@ -668,11 +619,10 @@ static void GetAllVariants2(vector &allVariants, SgSymbol ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) { - ArgsForKernel **retValueForKernel = NULL; createBodyKernel = true; // clear information - allRegNames.clear(); + allRegNames.clear(); SgStatement *st_hedr, *st_end, *first_exec, *stmt; vector cuda_kernel; @@ -694,9 +644,10 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) if (options.isOn(ONE_THREAD)) { - int num = 0; - SageSymbols *tmpStr = GetSymbInParalell(&num, dvm_parallel_dir->expr(2)); - retValueForKernel = Create_C_Adapter_Function_Across_OneThread(sadapter, kernel_symb, num, 0); + const vector tmpStr = GetSymbInParalell(dvm_parallel_dir->expr(2)); + int num = tmpStr.size(); + + vector retValueForKernel = Create_C_Adapter_Function_Across_OneThread(sadapter, kernel_symb, num, 0); for (unsigned t = 0; t < countKernels; ++t) { @@ -704,9 +655,6 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) CopyOfBody.pop(); currentLoop = new Loop(loop_body, options.isOn(OPT_EXP_COMP)); - - num = 0; - tmpStr = GetSymbInParalell(&num, dvm_parallel_dir->expr(2)); SgType *typeParams = indexTypeInKernel(rtTypes[t]); for (int i = 0; i < num; ++i) @@ -714,26 +662,25 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) char *str = new char[64]; char *addL = new char[64]; str[0] = addL[0] = '\0'; - retValueForKernel[t]->otherVarsForOneTh.push_back(tmpStr->symb); - strcat(str, tmpStr->symb->identifier()); + retValueForKernel[t].otherVarsForOneTh.push_back(tmpStr[i].symb); + strcat(str, tmpStr[i].symb->identifier()); strcat(str, "_"); strcat(addL, str); strcat(addL, "low"); - retValueForKernel[t]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, addL, typeParams, kernel_symb->scope())); + retValueForKernel[t].otherVars.push_back(new SgSymbol(VARIABLE_NAME, addL, typeParams, kernel_symb->scope())); addL[0] = '\0'; strcat(addL, str); strcat(addL, "high"); - retValueForKernel[t]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, addL, typeParams, kernel_symb->scope())); + retValueForKernel[t].otherVars.push_back(new SgSymbol(VARIABLE_NAME, addL, typeParams, kernel_symb->scope())); addL[0] = '\0'; strcat(addL, str); strcat(addL, "idx"); - retValueForKernel[t]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, addL, typeParams, kernel_symb->scope())); - tmpStr = tmpStr->next; + retValueForKernel[t].otherVars.push_back(new SgSymbol(VARIABLE_NAME, addL, typeParams, kernel_symb->scope())); } - + string kernel_symbNew = kernel_symb->identifier(); if (rtTypes[t] == rt_INT) kernel_symbNew += "_int"; @@ -741,8 +688,8 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) kernel_symbNew += "_long"; else if (rtTypes[t] == rt_LLONG) kernel_symbNew += "_llong"; - - cuda_kernel[t] = CreateLoopKernelAcross(new SgSymbol(FUNCTION_NAME, kernel_symbNew.c_str(), *C_VoidType(), *block_C), retValueForKernel[t], indexTypeInKernel(rtTypes[t])); + + cuda_kernel[t] = CreateLoopKernelAcross(new SgSymbol(FUNCTION_NAME, kernel_symbNew.c_str(), *C_VoidType(), *block_C), &retValueForKernel[t], indexTypeInKernel(rtTypes[t])); if (options.isOn(RTC)) { acc_call_list = ACC_RTC_ExpandCallList(acc_call_list); @@ -755,6 +702,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) } delete currentLoop; + currentLoop = NULL; } if (options.isOn(RTC)) ACC_RTC_CompleteAllParams(); @@ -784,7 +732,8 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) printf("%d case\n", allVariants[i].type); #endif ParamsForAllVariants tmp = allVariants[i]; - + vector retValueForKernel; + for (unsigned k = 0; k < countKernels; ++k) { loop_body = CopyOfBody.top(); @@ -807,7 +756,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) { if (k == 0) // create CUDA handler once retValueForKernel = Create_C_Adapter_Function_Across_variants(tmp.s_adapter, tmp.s_kernel_symb, tmp.loopV, tmp.acrossV, tmp.allDims, tmp.loopSymb, tmp.loopAcrossSymb); - cuda_kernel[k] = CreateLoopKernelAcross(new SgSymbol(FUNCTION_NAME, kernel_symb.c_str(), *C_VoidType(), *block_C), retValueForKernel[k], tmp.acrossV, indexTypeInKernel(rtTypes[k])); + cuda_kernel[k] = CreateLoopKernelAcross(new SgSymbol(FUNCTION_NAME, kernel_symb.c_str(), *C_VoidType(), *block_C), &retValueForKernel[k], tmp.acrossV, indexTypeInKernel(rtTypes[k])); if (options.isOn(RTC)) acc_call_list = ACC_RTC_ExpandCallList(acc_call_list); } @@ -819,7 +768,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) if (k == 0) // create CUDA handler once retValueForKernel = Create_C_Adapter_Function_Across_variants(tmp.s_adapter, tmp.s_kernel_symb, tmp.loopV, tmp.acrossV, tmp.allDims, tmp.loopSymb, tmp.loopAcrossSymb); - cuda_kernel[k] = CreateLoopKernelAcross(new SgSymbol(FUNCTION_NAME, kernel_symb.c_str(), *C_VoidType(), *block_C), retValueForKernel[k], tmp.acrossV, indexTypeInKernel(rtTypes[k])); + cuda_kernel[k] = CreateLoopKernelAcross(new SgSymbol(FUNCTION_NAME, kernel_symb.c_str(), *C_VoidType(), *block_C), &retValueForKernel[k], tmp.acrossV, indexTypeInKernel(rtTypes[k])); if (options.isOn(RTC)) { acc_call_list = ACC_RTC_ExpandCallList(acc_call_list); @@ -828,7 +777,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) if (strstr(kernel_symb.c_str(), "_llong") != NULL) acc_call_list = AddNewToSymbList(acc_call_list, createNewFunctionSymbol("dvmh_convert_XY_llong")); else if (strstr(kernel_symb.c_str(), "_int") != NULL) - acc_call_list = AddNewToSymbList(acc_call_list, createNewFunctionSymbol("dvmh_convert_XY_int")); + acc_call_list = AddNewToSymbList(acc_call_list, createNewFunctionSymbol("dvmh_convert_XY_int")); } } } @@ -839,7 +788,10 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) newVars.clear(); } if (contitionOfOptimization) + { delete currentLoop; + currentLoop = NULL; + } } if (options.isOn(RTC)) { @@ -858,7 +810,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) kernel_symb += "_long"; else if (rtTypes[k] == rt_LLONG) kernel_symb += "_llong"; - + if (options.isOn(C_CUDA)) ACC_RTC_ConvertCudaKernel(cuda_kernel[k], kernel_symb.c_str()); else @@ -869,7 +821,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) } } - + mywarn(" end: create all VARIANTS"); //create new control function @@ -906,7 +858,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) arg_list = arg_list->rhs(); } - for (el = uses_list; el; el = el->rhs()) // + for (el = uses_list; el; el = el->rhs()) // { s = el->lhs()->symbol(); typ = C_PointerType(C_Type(s->type())); @@ -971,9 +923,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) allVarForIfBlock.push_back(tmp); funcCall = new SgFunctionCallExp(*createNewFunctionSymbol(allVariants[k].nameOfNewSAdapter)); for (size_t i = 0; i < argsForVariantFunction.size(); ++i) - { funcCall->addArg(*new SgVarRefExp(argsForVariantFunction[i])); - } funcCall->addArg(*new SgVarRefExp(which_run)); allFuncCalls.push_back(funcCall); } @@ -1011,7 +961,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) SgFunctionCallExp *eventF = new SgFunctionCallExp(*createNewFunctionSymbol("cudaEventCreate")); eventF->addArg(SgAddrOp(*new SgVarRefExp(cudaEventStart))); st_end->insertStmtBefore(*new SgCExpStmt(*eventF), *st_hedr); - + eventF = new SgFunctionCallExp(*createNewFunctionSymbol("cudaEventCreate")); eventF->addArg(SgAddrOp(*new SgVarRefExp(cudaEventStop))); st_end->insertStmtBefore(*new SgCExpStmt(*eventF), *st_hedr); @@ -1078,7 +1028,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) whileSt1->lastExecutable()->insertStmtBefore(*if_st); } } - + tmpF2 = new SgFunctionCallExp(*createNewFunctionSymbol("printf")); tmpF2->addArg(*new SgValueExp("It may be wrong!!\\n")); @@ -1159,7 +1109,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter) return NULL; } -ArgsForKernel** Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadapter, SgSymbol *kernel_symb, const int loopV, const int acrossV) +vector Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadapter, SgSymbol *kernel_symb, const int loopV, const int acrossV) { #if debugMode warn("PARALLEL directive with ACROSS clause in region", 420, dvm_parallel_dir); @@ -1190,7 +1140,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadapter, S // end of init block mywarn("start: create fuction header "); - // create fuction header + // create fuction header st_hedr = Create_C_Function(sadapter); st_hedr->addComment(Cuda_LoopHandlerComment()); st_end = st_hedr->lexNext(); @@ -1201,7 +1151,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadapter, S mywarn(" end: create fuction header "); mywarn("start: create dummy argument list "); - // create dummy argument list: loop_ref, , + // create dummy argument list: loop_ref, , typ = C_PointerType(C_Derived_Type(s_DvmhLoopRef)); s_loop_ref = new SgSymbol(VARIABLE_NAME, "loop_ref", *typ, *st_hedr); @@ -1228,7 +1178,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadapter, S h_first = sarg; } - for (el = uses_list, ln = 0; el; el = el->rhs(), ++ln) // + for (el = uses_list, ln = 0; el; el = el->rhs(), ++ln) // { s = el->lhs()->symbol(); typ = C_PointerType(C_Type(s->type())); @@ -1272,10 +1222,10 @@ ArgsForKernel** Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadapter, S s_loc_var = NULL; is_array = 0; ered = er->lhs(); // reduction (variant==ARRAY_OP) - ev = ered->rhs(); // reduction variable reference for reduction operations except MINLOC,MAXLOC + ev = ered->rhs(); // reduction variable reference for reduction operations except MINLOC,MAXLOC if (isSgExprListExp(ev)) { - ev = ev->lhs(); // reduction variable reference + ev = ev->lhs(); // reduction variable reference loc_var_ref = ered->rhs()->rhs()->lhs(); //location array reference en = ered->rhs()->rhs()->rhs()->lhs(); // number of elements in location array loc_el_num = LocElemNumber(en); @@ -1308,7 +1258,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadapter, S btype = loc_type->baseType(); else btype = loc_type; - //!printf("__112\n"); + //!printf("__112\n"); SgArrayType *typearray = new SgArrayType(*C_Type(btype)); typearray->addRange(*new SgValueExp(loc_el_num)); s_loc_var->setType(*typearray); @@ -1321,7 +1271,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadapter, S st_hedr->insertStmtAfter(*stmt, *st_hedr); } - //!printf("__113\n"); + //!printf("__113\n"); /*--- executable statements: register reductions in RTS ---*/ e = &SgAssignOp(*new SgVarRefExp(s_tmp_var), *new SgValueExp(ln+1)); stmt = new SgCExpStmt(*e); @@ -1542,7 +1492,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadapter, S funcCall->addArg(*toAdd); } else - funcCall->addArg(*new SgVarRefExp(s)); + funcCall->addArg(*new SgVarRefExp(s)); } else { @@ -1617,20 +1567,17 @@ ArgsForKernel** Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadapter, S stmt->addComment("// Free temporary variables"); } } - // create args for kernel and return it - ArgsForKernel **argsKernel = new ArgsForKernel*[countKernels]; + // create args for kernel and return it + vector argsKernel(countKernels); for (unsigned i = 0; i < countKernels; ++i) - { - argsKernel[i] = new ArgsForKernel(); - argsKernel[i]->st_header = st_hedr; - } + argsKernel[i].st_header = st_hedr; delete[]reduction_ptr; mywarn(" end Adapter Function"); return argsKernel; } -static inline void insertReductionArgs(SgSymbol **reduction_ptr, SgSymbol **reduction_loc_ptr, +static inline void insertReductionArgs(SgSymbol **reduction_ptr, SgSymbol **reduction_loc_ptr, SgSymbol **reduction_symb, SgSymbol **reduction_loc_symb, SgFunctionCallExp *funcCallKernel, SgSymbol* numBlocks, int &has_red_array) { @@ -1650,7 +1597,7 @@ static inline void insertReductionArgs(SgSymbol **reduction_ptr, SgSymbol **redu int i = 0; for (rsl = red_struct_list, s = red_first; rsl; rsl = rsl->next, ++i) //s!=s_blocks_info { - if (rsl->redvar_size == 0) //reduction variable is scalar + if (rsl->redvar_size == 0) //reduction variable is scalar { if (options.isOn(RTC)) { @@ -1663,7 +1610,7 @@ static inline void insertReductionArgs(SgSymbol **reduction_ptr, SgSymbol **redu } else //TODO!! { - has_red_array = 1; + has_red_array = 1; for (int k = 0; k < rsl->redvar_size; ++k) funcCallKernel->addArg(*new SgArrayRefExp(*reduction_symb[i], *new SgValueExp(k))); } @@ -1687,13 +1634,13 @@ static inline void insertReductionArgs(SgSymbol **reduction_ptr, SgSymbol **redu } -static void createArgsForKernelForTwodeps(SgFunctionCallExp*& funcCallKernel, SgSymbol* kernel_symb, SgExpression* espec, SgSymbol*& sg, SgSymbol* hgpu_first, - SgSymbol*& sb, SgSymbol* base_first, symb_list*& sl, int& ln, int num, SgExpression*& e, SgSymbol** reduction_ptr, - SgSymbol** reduction_loc_ptr, SgSymbol** reduction_symb, SgSymbol** reduction_loc_symb, SgSymbol* red_blocks, int& has_red_array, - SgSymbol* diag, const int& loopV, SgSymbol** num_elems, const int& acrossV, SgSymbol* acrossBase[16], SgSymbol* loopBase[16], - SgSymbol* idxI, SageSymbols** loopAcrossSymb, SageSymbols** loopSymb, SgSymbol*& s, SgSymbol* uses_first, SgSymbol*& sdev, - SgSymbol* scalar_first, int uses_num, vector& dvm_array_headers, SgSymbol** addressingParams, SgSymbol** outTypeOfTransformation, - SgSymbol* type_of_run, SgSymbol* bIdxs) +static void createArgsForKernelForTwoDeps(SgFunctionCallExp*& funcCallKernel, SgSymbol* kernel_symb, SgExpression* espec, SgSymbol*& sg, SgSymbol* hgpu_first, + SgSymbol*& sb, SgSymbol* base_first, symb_list*& sl, int& ln, int num, SgExpression*& e, SgSymbol** reduction_ptr, + SgSymbol** reduction_loc_ptr, SgSymbol** reduction_symb, SgSymbol** reduction_loc_symb, SgSymbol* red_blocks, int& has_red_array, + SgSymbol* diag, const int& loopV, SgSymbol** num_elems, const int& acrossV, SgSymbol* acrossBase[16], SgSymbol* loopBase[16], + SgSymbol* idxI, const vector& loopAcrossSymb, const vector& loopSymb, SgSymbol*& s, SgSymbol* uses_first, + SgSymbol*& sdev, SgSymbol* scalar_first, int uses_num, vector& dvm_array_headers, + SgSymbol** addressingParams, SgSymbol** outTypeOfTransformation, SgSymbol* type_of_run, SgSymbol* bIdxs) { funcCallKernel = CallKernel(kernel_symb, espec); @@ -1740,9 +1687,9 @@ static void createArgsForKernelForTwodeps(SgFunctionCallExp*& funcCallKernel, Sg for (int i = 0; i < loopV; ++i) funcCallKernel->addArg(*new SgVarRefExp(loopBase[i])); for (int i = 0; i < acrossV; ++i) - funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i]->len))); + funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i].len))); for (int i = 0; i < loopV; ++i) - funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[i]->len))); + funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[i].len))); for (s = uses_first, sdev = scalar_first, ln = 0; ln < uses_num; s = s->next(), ln++) // uses { @@ -1781,8 +1728,8 @@ static void createArgsForKernelForTwodeps(SgFunctionCallExp*& funcCallKernel, Sg funcCallKernel->addArg(*new SgArrayRefExp(*bIdxs, *new SgValueExp(i))); } -ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, SgSymbol *kernel_symb, const int loopV, const int acrossV, - const int allDims, SageSymbols **loopSymb, SageSymbols **loopAcrossSymb) +vector Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, SgSymbol *kernel_symb, const int loopV, const int acrossV, + const int allDims, const vector& loopSymb, const vector& loopAcrossSymb) { #if debugMode warn("PARALLEL directive with ACROSS clause in region", 420, dvm_parallel_dir); @@ -1825,14 +1772,14 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg //end of init block mywarn("start: create fuction header "); - // create fuction header + // create fuction header st_hedr = Create_C_Function(sadapter); st_hedr->addComment(Cuda_LoopHandlerComment()); st_end = st_hedr->lexNext(); fe = st_hedr->expr(0); first_exec = st_end; if (declaration_include) - { + { declaration_cmnt = "#include \n#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))\n#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))"; declaration_include = false; } @@ -1840,7 +1787,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg mywarn(" end: create fuction header "); mywarn("start: create dummy argument list "); - // create dummy argument list: loop_ref, , + // create dummy argument list: loop_ref, , typ = C_PointerType(C_Derived_Type(s_DvmhLoopRef)); s_loop_ref = new SgSymbol(VARIABLE_NAME, "loop_ref", *typ, *st_hedr); @@ -1867,7 +1814,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg h_first = sarg; } - for (el = uses_list, ln = 0; el; el = el->rhs(), ++ln) // + for (el = uses_list, ln = 0; el; el = el->rhs(), ++ln) // { s = el->lhs()->symbol(); typ = C_PointerType(C_Type(s->type())); @@ -1938,10 +1885,10 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg s_loc_var = NULL; is_array = 0; ered = er->lhs(); // reduction (variant==ARRAY_OP) - ev = ered->rhs(); // reduction variable reference for reduction operations except MINLOC,MAXLOC + ev = ered->rhs(); // reduction variable reference for reduction operations except MINLOC,MAXLOC if (isSgExprListExp(ev)) { - ev = ev->lhs(); // reduction variable reference + ev = ev->lhs(); // reduction variable reference loc_var_ref = ered->rhs()->rhs()->lhs(); //location array reference en = ered->rhs()->rhs()->rhs()->lhs(); // number of elements in location array loc_el_num = LocElemNumber(en); @@ -1975,7 +1922,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg btype = loc_type->baseType(); else btype = loc_type; - //!printf("__112\n"); + //!printf("__112\n"); SgArrayType *typearray = new SgArrayType(*C_Type(btype)); typearray->addRange(*new SgValueExp(loc_el_num)); s_loc_var->setType(*typearray); @@ -1988,7 +1935,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg st_hedr->insertStmtAfter(*stmt, *st_hedr); } - //!printf("__113\n"); + //!printf("__113\n"); /*--- executable statements: register reductions in RTS ---*/ e = &SgAssignOp(*new SgVarRefExp(s_tmp_var), *new SgValueExp(ln+1)); stmt = new SgCExpStmt(*e); @@ -2049,7 +1996,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg s->setType(tpArr); addDeclExpList(s, stmt->expr(0)); - idxTypeInKernel = s = new SgSymbol(VARIABLE_NAME, TestAndCorrectName("idxTypeInKernel"), *LongT, *st_hedr); + idxTypeInKernel = s = new SgSymbol(VARIABLE_NAME, TestAndCorrectName("idxTypeInKernel"), *LongT, *st_hedr); addDeclExpList(s, stmt->expr(0)); if (options.isOn(GPU_O0)) @@ -2246,7 +2193,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg for (int i = 0; i < acrossV; ++i) { acrossBase[i] = s = new SgSymbol(VARIABLE_NAME, TestAndCorrectName(strcat(strcpy(new char[20], "base_"), - loopAcrossSymb[i]->symb->identifier())), *LongT, *st_hedr); + loopAcrossSymb[i].symb->identifier())), *LongT, *st_hedr); if (i == 0) { stmt = makeSymbolDeclaration(s); @@ -2258,7 +2205,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg for (int i = 0; i < loopV; ++i) { loopBase[i] = s = new SgSymbol(VARIABLE_NAME, TestAndCorrectName(strcat(strcpy(new char[20], "base_"), - loopSymb[i]->symb->identifier())), *LongT, *st_hedr); + loopSymb[i].symb->identifier())), *LongT, *st_hedr); addDeclExpList(s, stmt->expr(0)); } // end @@ -2438,11 +2385,11 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg st_end->insertStmtBefore(*stmt, *st_hedr); stmt = new SgIfStmt(SgEqOp(*new SgVarRefExp(idxTypeInKernel), *new SgVarRefExp(*new SgSymbol(VARIABLE_NAME, "rt_LONG"))) - && + && SgEqOp(*sizeofL, *sizeofLL), *new SgCExpStmt(SgAssignOp(*new SgVarRefExp(idxTypeInKernel), *new SgVarRefExp(*new SgSymbol(VARIABLE_NAME, "rt_LLONG"))))); st_end->insertStmtBefore(*stmt, *st_hedr); - + /* -------- call loop_cuda_get_config_(loop_ref, &shared_mem, ®_per_th, &threads, &stream, &shared_mem); ------------*/ SgFunctionCallExp *tmpFunc = new SgFunctionCallExp(*createNewFunctionSymbol("dim3")); int x = 0, y = 0, z = 0; @@ -2475,7 +2422,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg std::string preproc = std::string("#ifdef ") + fermiPreprocDir; char* tmp = new char[preproc.size() + 1]; strcpy(tmp, preproc.data()); - + st_end->insertStmtBefore(*PreprocessorDirective(tmp), *st_hedr); e = &SgAssignOp(*new SgVarRefExp(shared_mem), *new SgValueExp(shared_mem_count)); stmt = new SgCExpStmt(*e); @@ -2497,7 +2444,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg string define_name_int = kernel_symb->identifier(); string define_name_long = kernel_symb->identifier(); - + define_name_int += "_int_regs"; define_name_long += "_llong_regs"; @@ -2584,7 +2531,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg st_end->insertStmtBefore(*stmt, *st_hedr); stmt->addComment("//Start method"); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0].len))); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); @@ -2594,7 +2541,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg for (int k = 0; k < MIN(2, loopV); ++k) { SgStatement *st1; - idx[k] = loopSymb[k]->len; + idx[k] = loopSymb[k].len; e = &SgAssignOp(*new SgVarRefExp(loopBase[k]), *new SgArrayRefExp(*lowI, *new SgValueExp(idx[k]))); stmt = new SgCExpStmt(*e); @@ -2623,7 +2570,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg for (int k = 2; k < loopV; ++k) { SgStatement *st1; - idx[k] = loopSymb[k]->len; + idx[k] = loopSymb[k].len; e = &SgAssignOp(*new SgVarRefExp(loopBase[k]), *new SgArrayRefExp(*lowI, *new SgValueExp(idx[k]))); stmt = new SgCExpStmt(*e); @@ -2650,7 +2597,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg { SgStatement *st1; int k = 2; - idx[k] = loopSymb[k]->len; + idx[k] = loopSymb[k].len; e = &SgAssignOp(*new SgVarRefExp(loopBase[k]), *new SgArrayRefExp(*lowI, *new SgValueExp(idx[k]))); stmt = new SgCExpStmt(*e); @@ -2688,8 +2635,8 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg if (loopV != 0) { // (blocks.x * blocks.y * blocks.z * threads.x * threads.y * threads.z) / warpSize) - e = &SgAssignOp(*new SgVarRefExp(*red_blocks), - (*new SgRecordRefExp(*s_blocks, "x") * *new SgRecordRefExp(*s_blocks, "y") * *new SgRecordRefExp(*s_blocks, "z") * + e = &SgAssignOp(*new SgVarRefExp(*red_blocks), + (*new SgRecordRefExp(*s_blocks, "x") * *new SgRecordRefExp(*s_blocks, "y") * *new SgRecordRefExp(*s_blocks, "z") * *new SgRecordRefExp(*s_threads, "x") * *new SgRecordRefExp(*s_threads, "y") * *new SgRecordRefExp(*s_threads, "z")) / *new SgValueExp(warpSize)); stmt = new SgCExpStmt(*e); @@ -2719,7 +2666,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg } e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgVarRefExp(acrossBase[0]) - + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len))); + + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len))); stmt = new SgCExpStmt(*e); @@ -2727,17 +2674,17 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg { SgFunctionCallExp *f1 = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); SgFunctionCallExp *f2 = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); - f1->addArg(*new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[0]->len)) - *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0]->len))); - f2->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len))); + f1->addArg(*new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[0].len)) - *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0].len))); + f2->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len))); - e = &SgAssignOp(*new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[0]->len)), (*f1 + *f2) / *f2); + e = &SgAssignOp(*new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[0].len)), (*f1 + *f2) / *f2); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); } - + if (options.isOn(GPU_O0)) { - e = &SgAssignOp(*new SgArrayRefExp(*steps, *new SgArrayRefExp(*bIdxs, *new SgValueExp(0))), *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len))); + e = &SgAssignOp(*new SgArrayRefExp(*steps, *new SgArrayRefExp(*bIdxs, *new SgValueExp(0))), *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len))); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); @@ -2787,9 +2734,9 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg for (int i = 0; i < loopV; ++i) funcCallKernel->addArg(*new SgVarRefExp(loopBase[i])); for (int i = 0; i < acrossV; ++i) - funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i]->len))); + funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i].len))); for (int i = 0; i < loopV; ++i) - funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[i]->len))); + funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[i].len))); for (s = uses_first, sdev = scalar_first, ln = 0; ln < uses_num; s = s->next(), ln++) // uses { @@ -2811,22 +2758,22 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg for (int i = 0; i < acrossV + loopV; ++i) funcCallKernel->addArg(*new SgArrayRefExp(*bIdxs, *new SgValueExp(i))); - char *cond_ = new char[strlen("cond_") + strlen(loopAcrossSymb[0]->symb->identifier()) + 1]; + char *cond_ = new char[strlen("cond_") + strlen(loopAcrossSymb[0].symb->identifier()) + 1]; cond_[0] = '\0'; strcat(cond_, "cond_"); - strcat(cond_, loopAcrossSymb[0]->symb->identifier()); - + strcat(cond_, loopAcrossSymb[0].symb->identifier()); + if (options.isOn(GPU_O0)) { - funcCallKernel->addArg(*new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[0]->len))); + funcCallKernel->addArg(*new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[0].len))); for (int i = loopV - 1; i >= 0; i--) - funcCallKernel->addArg(*new SgArrayRefExp(*steps, *new SgValueExp(loopSymb[i]->len))); - funcCallKernel->addArg(*new SgArrayRefExp(*steps, *new SgValueExp(loopAcrossSymb[0]->len))); + funcCallKernel->addArg(*new SgArrayRefExp(*steps, *new SgValueExp(loopSymb[i].len))); + funcCallKernel->addArg(*new SgArrayRefExp(*steps, *new SgValueExp(loopAcrossSymb[0].len))); } - + } mywarn(" end: out adding args section"); - + stmt = createKernelCallsInCudaHandler(funcCallKernel, s_loop_ref, idxTypeInKernel, s_blocks); if (options.isOn(GPU_O0)) @@ -2834,13 +2781,13 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg else { SgSymbol *tmpV = new SgSymbol(VARIABLE_NAME, "int tmpV"); - SgSymbol *tmpV1 = new SgSymbol(VARIABLE_NAME, "tmpV"); + SgSymbol *tmpV1 = new SgSymbol(VARIABLE_NAME, "tmpV"); SgExprListExp *expr = new SgExprListExp(); - expr->setLhs(SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgVarRefExp(acrossBase[0]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len)))); + expr->setLhs(SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgVarRefExp(acrossBase[0]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len)))); expr->setRhs(new SgExprListExp()); expr->rhs()->setLhs(SgAssignOp(*new SgVarRefExp(tmpV1), *new SgVarRefExp(tmpV1) + *new SgValueExp(1))); SgForStmt *simple; - simple = new SgForStmt(&SgAssignOp(*new SgVarRefExp(tmpV), *new SgValueExp(0)), &(*new SgVarRefExp(tmpV1) < *new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[0]->len))), expr, stmt); + simple = new SgForStmt(&SgAssignOp(*new SgVarRefExp(tmpV), *new SgValueExp(0)), &(*new SgVarRefExp(tmpV1) < *new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[0].len))), expr, stmt); st_end->insertStmtBefore(*simple); } } @@ -2862,8 +2809,8 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg { int idx[2]; SgStatement *st1, *st2; - idx[1] = loopAcrossSymb[1]->len; - idx[0] = loopAcrossSymb[0]->len; + idx[1] = loopAcrossSymb[1].len; + idx[0] = loopAcrossSymb[0].len; SgFunctionCallExp *f1 = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); funcCall = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); @@ -2889,8 +2836,8 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg { SgFunctionCallExp *tempF = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); funcCall = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); - funcCall->addArg((*new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[0]->len)) - *new SgArrayRefExp(*highI, *new SgValueExp(loopSymb[0]->len)))); - tempF->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[0]->len))); + funcCall->addArg((*new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[0].len)) - *new SgArrayRefExp(*highI, *new SgValueExp(loopSymb[0].len)))); + tempF->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[0].len))); e = &SgAssignOp(*new SgVarRefExp(num_elems[1]), (*funcCall + *new SgValueExp(1)) / *tempF + SgNeqOp((*funcCall + *new SgValueExp(1)) % *tempF, *new SgValueExp(0))); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); @@ -2900,8 +2847,8 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg { SgFunctionCallExp *tempF = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); funcCall = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); - funcCall->addArg((*new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[k + 1]->len)) - *new SgArrayRefExp(*highI, *new SgValueExp(loopSymb[k + 1]->len)))); - tempF->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[k + 1]->len))); + funcCall->addArg((*new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[k + 1].len)) - *new SgArrayRefExp(*highI, *new SgValueExp(loopSymb[k + 1].len)))); + tempF->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[k + 1].len))); e_z[k] = &((*funcCall + *new SgValueExp(1)) / *tempF + SgNeqOp((*funcCall + *new SgValueExp(1)) % *tempF, *new SgValueExp(0))); } if (loopV > 2) @@ -2976,7 +2923,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg { e = &SgAssignOp(*new SgVarRefExp(*red_blocks), (*new SgVarRefExp(q) / *new SgVarRefExp(nums[0]) + SgNeqOp(*new SgVarRefExp(q) % *new SgVarRefExp(nums[0]), *new SgValueExp(0))) * - *new SgRecordRefExp(*s_blocks, "y") * *new SgRecordRefExp(*s_blocks, "z") * + *new SgRecordRefExp(*s_blocks, "y") * *new SgRecordRefExp(*s_blocks, "z") * *new SgRecordRefExp(*s_threads, "x") * *new SgRecordRefExp(*s_threads, "y") * *new SgRecordRefExp(*s_threads, "z") / *new SgValueExp(warpSize)); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); @@ -3003,7 +2950,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg // init bases for (int i = 0; i < acrossV; ++i) { - e = &SgAssignOp(*new SgVarRefExp(acrossBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[i]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[i].len))); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); if (i == 0) @@ -3011,7 +2958,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg } for (int i = 0; i < loopV; ++i) { - e = &SgAssignOp(*new SgVarRefExp(loopBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[i]->len))); + e = &SgAssignOp(*new SgVarRefExp(loopBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[i].len))); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); } @@ -3030,16 +2977,16 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg st_end->insertStmtBefore(*while_st, *st_hedr); e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgVarRefExp(acrossBase[0]) + - *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len))); + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len))); stmt = new SgCExpStmt(*e); while_st->insertStmtAfter(*stmt); /* --------- add argument list to kernel call ----*/ - createArgsForKernelForTwodeps(funcCallKernel, kernel_symb, espec, sg, hgpu_first, sb, base_first, sl, ln, num, e, - reduction_ptr, reduction_loc_ptr, reduction_symb, reduction_loc_symb, red_blocks, - has_red_array, diag, loopV, num_elems, acrossV, acrossBase, loopBase, idxI, - loopAcrossSymb, loopSymb, s, uses_first, sdev, scalar_first, uses_num, dvm_array_headers, + createArgsForKernelForTwoDeps(funcCallKernel, kernel_symb, espec, sg, hgpu_first, sb, base_first, sl, ln, num, e, + reduction_ptr, reduction_loc_ptr, reduction_symb, reduction_loc_symb, red_blocks, + has_red_array, diag, loopV, num_elems, acrossV, acrossBase, loopBase, idxI, + loopAcrossSymb, loopSymb, s, uses_first, sdev, scalar_first, uses_num, dvm_array_headers, addressingParams, outTypeOfTransformation, type_of_run, bIdxs); stmt = createKernelCallsInCudaHandler(funcCallKernel, s_loop_ref, idxTypeInKernel, s_blocks); @@ -3074,7 +3021,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg if_st->falseBody()->insertStmtBefore(*new SgCExpStmt(SgAssignOp(*new SgVarRefExp(diag), *new SgValueExp(0)))); e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgVarRefExp(acrossBase[0]) - - *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len))); + - *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len))); stmt = new SgCExpStmt(*e); if_st->insertStmtAfter(*stmt); if_st->falseBody()->insertStmtBefore(stmt->copy()); @@ -3083,8 +3030,8 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg stmt = new SgCExpStmt(*e); if_st->lexNext()->insertStmtAfter(*stmt); if_st->falseBody()->lexNext()->lexNext()->lexNext()->insertStmtAfter(stmt->copy(), *if_st); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1]->len)) + - *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1].len)) + + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len))); stmt = new SgCExpStmt(*e); if_st->insertStmtAfter(*stmt); if_st->falseBody()->insertStmtBefore(stmt->copy()); @@ -3094,12 +3041,12 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgVarRefExp(acrossBase[0]) + - *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len))); + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len))); stmt = new SgCExpStmt(*e); while_st1->insertStmtAfter(*stmt); e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + - *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len))); + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len))); stmt = new SgCExpStmt(*e); while_st2->insertStmtAfter(*stmt); @@ -3115,7 +3062,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg while_st4->lastExecutable()->insertStmtAfter(stmt->copy()); /* --------- add argument list to kernel call ----*/ - createArgsForKernelForTwodeps(funcCallKernel, kernel_symb, espec, sg, hgpu_first, sb, base_first, sl, ln, num, e, + createArgsForKernelForTwoDeps(funcCallKernel, kernel_symb, espec, sg, hgpu_first, sb, base_first, sl, ln, num, e, reduction_ptr, reduction_loc_ptr, reduction_symb, reduction_loc_symb, red_blocks, has_red_array, q, loopV, num_elems, acrossV, acrossBase, loopBase, idxI, loopAcrossSymb, loopSymb, s, uses_first, sdev, scalar_first, uses_num, dvm_array_headers, @@ -3127,7 +3074,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg mywarn(" end: block3"); /* --------- add argument list to kernel call ----*/ - createArgsForKernelForTwodeps(funcCallKernel, kernel_symb, espec, sg, hgpu_first, sb, base_first, sl, ln, num, e, + createArgsForKernelForTwoDeps(funcCallKernel, kernel_symb, espec, sg, hgpu_first, sb, base_first, sl, ln, num, e, reduction_ptr, reduction_loc_ptr, reduction_symb, reduction_loc_symb, red_blocks, has_red_array, elem, loopV, num_elems, acrossV, acrossBase, loopBase, idxI, loopAcrossSymb, loopSymb, s, uses_first, sdev, scalar_first, uses_num, dvm_array_headers, @@ -3164,8 +3111,8 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg s_cuda_var[2] = tmpS; } - SgExpression* firstElem = new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len)); - SgExpression* secondElem = new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)); + SgExpression* firstElem = new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len)); + SgExpression* secondElem = new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)); SgIfStmt* if_stSwap = new SgIfStmt(*new SgVarRefExp(M1) > *new SgVarRefExp(M2), *new SgCExpStmt(*firstElem ^= *secondElem ^= *firstElem ^= *secondElem)); @@ -3196,9 +3143,9 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg for (int i = 0; i < loopV; ++i) funcCallKernel->addArg(*new SgVarRefExp(loopBase[i])); for (int i = 0; i < acrossV; ++i) - funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i]->len))); + funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i].len))); for (int i = 0; i < loopV; ++i) - funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[i]->len))); + funcCallKernel->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[i].len))); for (s = uses_first, sdev = scalar_first, ln = 0; ln < uses_num; s = s->next(), ln++) // uses { @@ -3271,7 +3218,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg int idx[3]; SgStatement *st1; for (int i = 0; i < 3; ++i) - idx[i] = loopAcrossSymb[i]->len; + idx[i] = loopAcrossSymb[i].len; for (int i = 0; i < 3; ++i) { @@ -3316,8 +3263,8 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg { SgFunctionCallExp *tempF = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); funcCall = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); - funcCall->addArg((*new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[0]->len)) - *new SgArrayRefExp(*highI, *new SgValueExp(loopSymb[0]->len)))); - tempF->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[0]->len))); + funcCall->addArg((*new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[0].len)) - *new SgArrayRefExp(*highI, *new SgValueExp(loopSymb[0].len)))); + tempF->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[0].len))); e = &SgAssignOp(*new SgVarRefExp(num_elems[0]), (*funcCall + *new SgValueExp(1)) / *tempF + SgNeqOp((*funcCall + *new SgValueExp(1)) % *tempF, *new SgValueExp(0))); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); @@ -3329,8 +3276,8 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg { SgFunctionCallExp *tempF = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); funcCall = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); - funcCall->addArg((*new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[k + 1]->len)) - *new SgArrayRefExp(*highI, *new SgValueExp(loopSymb[k + 1]->len)))); - tempF->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[k + 1]->len))); + funcCall->addArg((*new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[k + 1].len)) - *new SgArrayRefExp(*highI, *new SgValueExp(loopSymb[k + 1].len)))); + tempF->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopSymb[k + 1].len))); e_z[k] = &((*funcCall + *new SgValueExp(1)) / *tempF + SgNeqOp((*funcCall + *new SgValueExp(1)) % *tempF, *new SgValueExp(0))); } @@ -3422,7 +3369,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg int flag_comment = 0; for (int i = 3; i < acrossV; ++i) { - e = &SgAssignOp(*new SgVarRefExp(acrossBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[i]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[i].len))); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); if (i - 3 == 0) @@ -3436,7 +3383,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg { for (int i = 0; i < MIN(3, acrossV); ++i) { - e = &SgAssignOp(*new SgVarRefExp(acrossBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[i]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[i].len))); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); if (i == 0 && flag_comment == 0) @@ -3445,7 +3392,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg for (int i = 0; i < loopV; ++i) { - e = &SgAssignOp(*new SgVarRefExp(loopBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[i]->len))); + e = &SgAssignOp(*new SgVarRefExp(loopBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[i].len))); stmt = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); } @@ -3458,20 +3405,20 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg SgWhileStmt *tmp; for (int i = 3; i < acrossV; ++i) { - e = &SgAssignOp(*new SgVarRefExp(acrossBase[i]), *new SgVarRefExp(acrossBase[i]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[i]), *new SgVarRefExp(acrossBase[i]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i].len))); stmt = new SgCExpStmt(*e); SgExpression *e1 = NULL; SgFunctionCallExp *func = new SgFunctionCallExp(*createNewFunctionSymbol("abs")); - func->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i]->len))); - e1 = &(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i]->len)) / *func); + func->addArg(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i].len))); + e1 = &(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[i].len)) / *func); if (first) { - main_while_st = new SgWhileStmt(*e1 * *new SgVarRefExp(acrossBase[i]) <= *e1 * *new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[i]->len)), *stmt); + main_while_st = new SgWhileStmt(*e1 * *new SgVarRefExp(acrossBase[i]) <= *e1 * *new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[i].len)), *stmt); first = false; } else { - tmp = new SgWhileStmt(*new SgVarRefExp(acrossBase[i]) <= *new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[i]->len)), *stmt); + tmp = new SgWhileStmt(*new SgVarRefExp(acrossBase[i]) <= *new SgArrayRefExp(*highI, *new SgValueExp(loopAcrossSymb[i].len)), *stmt); main_while_st->insertStmtAfter(*tmp); main_while_st = tmp; } @@ -3492,14 +3439,14 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg for (int i = 0; i < MIN(3, acrossV); ++i) { - e = &SgAssignOp(*new SgVarRefExp(acrossBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[i]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[i].len))); stmt = new SgCExpStmt(*e); main_stmt->insertStmtBefore(*stmt, *main_while_st); } for (int i = 0; i < loopV; ++i) { - e = &SgAssignOp(*new SgVarRefExp(loopBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[i]->len))); + e = &SgAssignOp(*new SgVarRefExp(loopBase[i]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopSymb[i].len))); stmt = new SgCExpStmt(*e); main_stmt->insertStmtBefore(*stmt, *main_while_st); } @@ -3514,9 +3461,9 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg else main_stmt->insertStmtBefore(*while_st, *main_while_st); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[2]), *new SgVarRefExp(acrossBase[2]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[2]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[2]), *new SgVarRefExp(acrossBase[2]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[2].len))); stmt = new SgCExpStmt(*e); - while_st->insertStmtAfter(*stmt); + while_st->insertStmtAfter(*stmt); while_st->insertStmtAfter(*createKernelCallsInCudaHandler(funcCallKernel, s_loop_ref, idxTypeInKernel, s_blocks)); e = &SgAssignOp(*new SgRecordRefExp(*s_blocks, (char*)s_cuda_var[1]), *new SgVarRefExp(diag) / *new SgVarRefExp(nums[1]) + SgNeqOp(*new SgVarRefExp(diag) % *new SgVarRefExp(nums[1]), *new SgValueExp(0))); @@ -3554,7 +3501,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg SgWhileStmt *while_st = new SgWhileStmt(SgNeqOp(*new SgVarRefExp(diag) - *new SgValueExp(1), *new SgVarRefExp(M3)), *stmt); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[2]), *new SgVarRefExp(acrossBase[2]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[2]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[2]), *new SgVarRefExp(acrossBase[2]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[2].len))); stmt = new SgCExpStmt(*e); while_st->insertStmtAfter(*stmt, *while_st); @@ -3580,11 +3527,11 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg stmt = new SgCExpStmt(*e); if_st->insertStmtAfter(*stmt); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1]->len)) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2)) + *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0]->len)) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1].len)) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2)) + *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0].len)) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2))); stmt = new SgCExpStmt(*e); if_st->insertStmtAfter(*stmt); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0]->len)) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2)) + *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1]->len)) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0].len)) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2)) + *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1].len)) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2))); stmt = new SgCExpStmt(*e); if_st->insertStmtAfter(*stmt); } @@ -3617,21 +3564,21 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg else main_stmt->insertStmtBefore(*stmt, *main_while_st); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), (*new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0]->len)) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len))) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2)) + (*new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1]->len)) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len))) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), (*new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0].len)) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len))) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2)) + (*new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1].len)) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len))) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2))); stmt = new SgCExpStmt(*e); if (acrossV == 3) st_end->insertStmtBefore(*stmt, *st_hedr); else main_stmt->insertStmtBefore(*stmt, *main_while_st); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1]->len)) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2)) + *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0]->len)) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1].len)) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2)) + *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0].len)) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2))); stmt = new SgCExpStmt(*e); if (acrossV == 3) st_end->insertStmtBefore(*stmt, *st_hedr); else main_stmt->insertStmtBefore(*stmt, *main_while_st); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[2]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[2]->len)) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[2]->len)) * (*new SgVarRefExp(M3) - *new SgValueExp(1))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[2]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[2].len)) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[2].len)) * (*new SgVarRefExp(M3) - *new SgValueExp(1))); stmt = new SgCExpStmt(*e); if (acrossV == 3) st_end->insertStmtBefore(*stmt, *st_hedr); @@ -3647,7 +3594,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg else main_stmt->insertStmtBefore(*while_st, *main_while_st); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgVarRefExp(acrossBase[0]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len)) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2)) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgVarRefExp(acrossBase[0]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len)) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2)) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2))); stmt = new SgCExpStmt(*e); while_st->insertStmtAfter(*stmt, *while_st); @@ -3683,14 +3630,14 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg else main_stmt->insertStmtBefore(*stmt, *main_while_st); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0]->len)) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0]->len)) * (*new SgVarRefExp(M1) - *new SgValueExp(1))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[0]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[0].len)) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[0].len)) * (*new SgVarRefExp(M1) - *new SgValueExp(1))); stmt = new SgCExpStmt(*e); if (acrossV == 3) st_end->insertStmtBefore(*stmt, *st_hedr); else main_stmt->insertStmtBefore(*stmt, *main_while_st); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1]->len)) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2)) + *new SgVarRefExp(acrossBase[1]) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgArrayRefExp(*lowI, *new SgValueExp(loopAcrossSymb[1].len)) * (*new SgVarRefExp(M1) > *new SgVarRefExp(M2)) + *new SgVarRefExp(acrossBase[1]) * (*new SgVarRefExp(M1) <= *new SgVarRefExp(M2))); stmt = new SgCExpStmt(*e); if (acrossV == 3) st_end->insertStmtBefore(*stmt, *st_hedr); @@ -3703,31 +3650,31 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg SgExpression *e1 = NULL, *e2 = NULL; SgIfStmt *if_st1 = NULL; - e1 = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)) + *new SgVarRefExp(*Emax) - *new SgVarRefExp(*Emin) - *new SgValueExp(1)); - e2 = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)) - *new SgVarRefExp(*Emax) + *new SgVarRefExp(*Emin) + *new SgValueExp(1)); + e1 = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)) + *new SgVarRefExp(*Emax) - *new SgVarRefExp(*Emin) - *new SgValueExp(1)); + e2 = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)) - *new SgVarRefExp(*Emax) + *new SgVarRefExp(*Emin) + *new SgValueExp(1)); - if_st1 = new SgIfStmt(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)) > *new SgValueExp(0), *new SgCExpStmt(*e1), *new SgCExpStmt(*e2)); + if_st1 = new SgIfStmt(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)) > *new SgValueExp(0), *new SgCExpStmt(*e1), *new SgCExpStmt(*e2)); SgIfStmt *if_st = new SgIfStmt(*new SgVarRefExp(*M1) <= *new SgVarRefExp(*M2) && *new SgVarRefExp(*M3) > *new SgVarRefExp(*Emin), *if_st1); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len))); stmt = new SgCExpStmt(*e); if_st = new SgIfStmt(*new SgVarRefExp(*M1) > *new SgVarRefExp(*M2) && *new SgVarRefExp(*M3) > *new SgVarRefExp(*Emin), *stmt, *if_st); - e1 = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)) + *new SgVarRefExp(*Emax) - *new SgVarRefExp(*Emin) - *new SgValueExp(1) + *funcCall); - e2 = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)) - *new SgVarRefExp(*Emax) + *new SgVarRefExp(*Emin) + *new SgValueExp(1) + *new SgVarRefExp(M3) - *new SgVarRefExp(*Emin)); + e1 = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)) + *new SgVarRefExp(*Emax) - *new SgVarRefExp(*Emin) - *new SgValueExp(1) + *funcCall); + e2 = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)) - *new SgVarRefExp(*Emax) + *new SgVarRefExp(*Emin) + *new SgValueExp(1) + *new SgVarRefExp(M3) - *new SgVarRefExp(*Emin)); + + if_st1 = new SgIfStmt(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)) > *new SgValueExp(0), *new SgCExpStmt(*e1), *new SgCExpStmt(*e2)); - if_st1 = new SgIfStmt(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)) > *new SgValueExp(0), *new SgCExpStmt(*e1), *new SgCExpStmt(*e2)); - if_st = new SgIfStmt(*new SgVarRefExp(*M1) <= *new SgVarRefExp(*M2) && *new SgVarRefExp(*M3) <= *new SgVarRefExp(*Emin), *if_st1, *if_st); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)) + *funcCall); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)) + *funcCall); stmt = new SgCExpStmt(*e); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)) + *funcCall * *new SgValueExp(-1)); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)) + *funcCall * *new SgValueExp(-1)); SgStatement* stmtElse = new SgCExpStmt(*e); - if_st1 = new SgIfStmt(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len)) > *new SgValueExp(0), *stmt, *stmtElse); + if_st1 = new SgIfStmt(*new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len)) > *new SgValueExp(0), *stmt, *stmtElse); if_st = new SgIfStmt(*new SgVarRefExp(*M1) > *new SgVarRefExp(*M2) && *new SgVarRefExp(*M3) <= *new SgVarRefExp(*Emin), *if_st1, *if_st); @@ -3746,7 +3693,7 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg else main_stmt->insertStmtBefore(*while_st, *main_while_st); - e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1]->len))); + e = &SgAssignOp(*new SgVarRefExp(acrossBase[1]), *new SgVarRefExp(acrossBase[1]) + *new SgArrayRefExp(*idxI, *new SgValueExp(loopAcrossSymb[1].len))); stmt = new SgCExpStmt(*e); while_st->insertStmtAfter(*stmt, *while_st); @@ -3781,40 +3728,38 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg st_end->insertStmtBefore(*stmt, *st_hedr); } } - - // create args for kernel and return it - ArgsForKernel **argsKernel = new ArgsForKernel*[countKernels]; + + // create args for kernel and return it + vector argsKernel(countKernels); const int rtTypes[] = { rt_INT, rt_LLONG }; for (unsigned ck = 0; ck < countKernels; ++ck) { - argsKernel[ck] = new ArgsForKernel(); - - argsKernel[ck]->st_header = st_hedr; - argsKernel[ck]->cond_ = NULL; + argsKernel[ck].st_header = st_hedr; + argsKernel[ck].cond_ = NULL; SgType *typeParams = indexTypeInKernel(rtTypes[ck]); if (acrossV == 1) { - char *cond_ = new char[strlen("cond_") + strlen(loopAcrossSymb[0]->symb->identifier()) + 1]; + char *cond_ = new char[strlen("cond_") + strlen(loopAcrossSymb[0].symb->identifier()) + 1]; cond_[0] = '\0'; strcat(cond_, "cond_"); - strcat(cond_, loopAcrossSymb[0]->symb->identifier()); - argsKernel[ck]->cond_ = new SgSymbol(VARIABLE_NAME, cond_, typeParams, st_hedr); + strcat(cond_, loopAcrossSymb[0].symb->identifier()); + argsKernel[ck].cond_ = new SgSymbol(VARIABLE_NAME, cond_, typeParams, st_hedr); - char *st = new char[strlen("steps_") + strlen(loopAcrossSymb[0]->symb->identifier()) + 1]; + char *st = new char[strlen("steps_") + strlen(loopAcrossSymb[0].symb->identifier()) + 1]; st[0] = '\0'; strcat(st, "steps_"); - strcat(st, loopAcrossSymb[0]->symb->identifier()); - argsKernel[ck]->steps.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(st), typeParams, st_hedr)); + strcat(st, loopAcrossSymb[0].symb->identifier()); + argsKernel[ck].steps.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(st), typeParams, st_hedr)); for (int i = 0; i < loopV; ++i) { - st = new char[strlen("steps_") + strlen(loopSymb[i]->symb->identifier()) + 1]; + st = new char[strlen("steps_") + strlen(loopSymb[i].symb->identifier()) + 1]; st[0] = '\0'; strcat(st, "steps_"); - strcat(st, loopSymb[i]->symb->identifier()); - argsKernel[ck]->steps.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(st), typeParams, st_hedr)); + strcat(st, loopSymb[i].symb->identifier()); + argsKernel[ck].steps.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(st), typeParams, st_hedr)); } } @@ -3826,75 +3771,75 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg tmpS[0] = '\0'; strcat(tmpS, dvm_array_headers[i]); strcat(tmpS, "_x_axis"); - argsKernel[ck]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); + argsKernel[ck].otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); tmpS[0] = '\0'; strcat(tmpS, dvm_array_headers[i]); strcat(tmpS, "_offset_x"); - argsKernel[ck]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); + argsKernel[ck].otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); tmpS[0] = '\0'; strcat(tmpS, dvm_array_headers[i]); strcat(tmpS, "_Rx"); - argsKernel[ck]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); + argsKernel[ck].otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); tmpS[0] = '\0'; strcat(tmpS, dvm_array_headers[i]); strcat(tmpS, "_y_axis"); - argsKernel[ck]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); + argsKernel[ck].otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); tmpS[0] = '\0'; strcat(tmpS, dvm_array_headers[i]); strcat(tmpS, "_offset_y"); - argsKernel[ck]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); + argsKernel[ck].otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); tmpS[0] = '\0'; strcat(tmpS, dvm_array_headers[i]); strcat(tmpS, "_Ry"); - argsKernel[ck]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); + argsKernel[ck].otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); tmpS[0] = '\0'; strcat(tmpS, dvm_array_headers[i]); strcat(tmpS, "_slash"); - argsKernel[ck]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); - argsKernel[ck]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(outTypeOfTransformation[i]->identifier()), typeParams, st_hedr)); + argsKernel[ck].otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(tmpS), typeParams, st_hedr)); + argsKernel[ck].otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(outTypeOfTransformation[i]->identifier()), typeParams, st_hedr)); } - argsKernel[ck]->arrayNames = dvm_array_headers; + argsKernel[ck].arrayNames = dvm_array_headers; } if (acrossV == 2) - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("num_elem_across"), typeParams, st_hedr)); + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("num_elem_across"), typeParams, st_hedr)); else if (acrossV >= 3) { - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("max_z"), typeParams, st_hedr)); - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("SE"), typeParams, st_hedr)); // SE - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("var1"), typeParams, st_hedr)); // var1 - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("var2"), typeParams, st_hedr)); // var2 - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("var3"), typeParams, st_hedr)); // var3 - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("Emax"), typeParams, st_hedr)); // Emax - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("Emin"), typeParams, st_hedr)); // Emin - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("min_ij"), typeParams, st_hedr)); - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("swap_ij"), typeParams, st_hedr)); + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("max_z"), typeParams, st_hedr)); + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("SE"), typeParams, st_hedr)); // SE + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("var1"), typeParams, st_hedr)); // var1 + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("var2"), typeParams, st_hedr)); // var2 + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("var3"), typeParams, st_hedr)); // var3 + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("Emax"), typeParams, st_hedr)); // Emax + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("Emin"), typeParams, st_hedr)); // Emin + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("min_ij"), typeParams, st_hedr)); + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("swap_ij"), typeParams, st_hedr)); } char *str = new char[32]; for (int i = 0; i < acrossV; ++i) { - argsKernel[ck]->acrossS.push_back(new SgSymbol(VARIABLE_NAME, acrossBase[i]->identifier(), typeParams, st_hedr)); // acrossBase[i] - argsKernel[ck]->symb.push_back(loopAcrossSymb[i]); + argsKernel[ck].acrossS.push_back(new SgSymbol(VARIABLE_NAME, acrossBase[i]->identifier(), typeParams, st_hedr)); // acrossBase[i] + argsKernel[ck].symb.push_back(loopAcrossSymb[i]); strcpy(str, "step"); strcat(str, strchr(acrossBase[i]->identifier(), '_')); - argsKernel[ck]->idxAcross.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(str), typeParams, st_hedr)); + argsKernel[ck].idxAcross.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(str), typeParams, st_hedr)); } for (int i = 0; i < loopV; ++i) { - argsKernel[ck]->notAcrS.push_back(new SgSymbol(VARIABLE_NAME, loopBase[i]->identifier(), typeParams, st_hedr)); // loopBase[i] - argsKernel[ck]->nSymb.push_back(loopSymb[i]); + argsKernel[ck].notAcrossS.push_back(new SgSymbol(VARIABLE_NAME, loopBase[i]->identifier(), typeParams, st_hedr)); // loopBase[i] + argsKernel[ck].nSymb.push_back(loopSymb[i]); strcpy(str, "step"); strcat(str, strchr(loopBase[i]->identifier(), '_')); - argsKernel[ck]->idxNotAcross.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(str), typeParams, st_hedr)); + argsKernel[ck].idxNotAcross.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(str), typeParams, st_hedr)); strcpy(str, "num_elem"); strcat(str, strchr(loopBase[i]->identifier(), '_')); - argsKernel[ck]->sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(str), typeParams, st_hedr)); + argsKernel[ck].sizeVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(str), typeParams, st_hedr)); } if (acrossV == 1 || acrossV == 2 || acrossV >= 3) { - argsKernel[ck]->otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("type_of_run"), typeParams, st_hedr)); + argsKernel[ck].otherVars.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName("type_of_run"), typeParams, st_hedr)); char *t = new char[32]; for (int i = 0; i < acrossV + loopV; ++i) { @@ -3903,13 +3848,13 @@ ArgsForKernel** Create_C_Adapter_Function_Across_variants(SgSymbol *sadapter, Sg t[0] = '\0'; strcat(t, "idxs_"); strcat(t, p); - argsKernel[ck]->baseIdxsInKer.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(t), typeParams, st_hedr)); + argsKernel[ck].baseIdxsInKer.push_back(new SgSymbol(VARIABLE_NAME, TestAndCorrectName(t), typeParams, st_hedr)); } delete[]t; } delete[]str; - + } // end of creation args for kernel @@ -3928,11 +3873,11 @@ void MakeDeclarationsForKernel_On_C_Across(SgType *indexType) // declare do_variables DeclareDoVars(indexType); - // declare private(local in kernel) variables + // declare private(local in kernel) variables DeclarePrivateVars(); // declare variables, used in loop and passed by reference: - // & = *p_; + // & = *p_; DeclareUsedVars(); } @@ -3941,11 +3886,11 @@ void MakeDeclarationsForKernelAcross(SgType *indexType) #if debugMode mywarn("strat: MakeDeclarations Function"); #endif - + // declare do_variables DeclareDoVars(); - // declare private(local in kernel) variables + // declare private(local in kernel) variables DeclarePrivateVars(); // declare dummy arguments: @@ -3956,7 +3901,7 @@ void MakeDeclarationsForKernelAcross(SgType *indexType) // declare array coefficients DeclareArrayCoeffsInKernel(indexType); - // declare bases for arrays + // declare bases for arrays DeclareArrayBases(); // declare variables, used in loop @@ -3978,63 +3923,33 @@ SgExpression *CreateKernelDummyListAcross(ArgsForKernel *argsKer, SgType *idxTyp arg_list = AddListToList(CreateArrayDummyList(idxTypeInKernel), CreateRedDummyList(idxTypeInKernel)); // base_ref + ... - // + [+red_var_2+...+red_var_M] + _grid [ + ...] + // + [+red_var_2+...+red_var_M] + _grid [ + ...] // + 'blocks' if (argsKer->symb.size() < 3) { - for (list::iterator it = argsKer->sizeVars.begin(); it != argsKer->sizeVars.end(); ++it) - { - ae = new SgExprListExp(*new SgVarRefExp(*it)); - arg_list = AddListToList(arg_list, ae); - } + for (int it = 0; it < argsKer->sizeVars.size(); ++it) + arg_list = AddListToList(arg_list, new SgExprListExp(*new SgVarRefExp(argsKer->sizeVars[it]))); } - for (list::iterator it = argsKer->acrossS.begin(); it != argsKer->acrossS.end(); ++it) - { - ae = new SgExprListExp(*new SgVarRefExp(*it)); - arg_list = AddListToList(arg_list, ae); - } - for (list::iterator it = argsKer->notAcrS.begin(); it != argsKer->notAcrS.end(); ++it) - { - ae = new SgExprListExp(*new SgVarRefExp(*it)); - arg_list = AddListToList(arg_list, ae); - } - - for (list::iterator it = argsKer->idxAcross.begin(); it != argsKer->idxAcross.end(); ++it) - { - ae = new SgExprListExp(*new SgVarRefExp(*it)); - arg_list = AddListToList(arg_list, ae); - } - - for (list::iterator it = argsKer->idxNotAcross.begin(); it != argsKer->idxNotAcross.end(); ++it) - { - ae = new SgExprListExp(*new SgVarRefExp(*it)); - arg_list = AddListToList(arg_list, ae); - } - //ae = options.isOn(C_CUDA) ? new SgExprListExp(*new SgArrayRefExp(*s_blocks_k,*eln)) : new SgExprListExp(*new SgArrayRefExp(*s_blocks_k)); // + 'blocks' - // //ae = options.isOn(C_CUDA) ? new SgExprListExp(*new SgPointerDerefExp(*new SgVarRefExp(s_blocks_k))) : new SgExprListExp(*new SgVarRefExp(s_blocks_k)); - //arg_list = AddListToList(arg_list,ae); - //if(s_red_count_k) //[+ 'red_count'] - //{ ae = new SgExprListExp(*new SgVarRefExp(s_red_count_k)); - // arg_list = AddListToList(arg_list,ae); - //} - // //[+ 'overall_blocks'] - //if(s_overall_blocks) - //{ ae = new SgExprListExp(*new SgVarRefExp(s_overall_blocks)); - // arg_list = AddListToList(arg_list,ae); - //} + for (int it = 0; it < argsKer->acrossS.size(); ++it) + arg_list = AddListToList(arg_list, new SgExprListExp(*new SgVarRefExp(argsKer->acrossS[it]))); + + for (int it = 0; it < argsKer->notAcrossS.size(); ++it) + arg_list = AddListToList(arg_list, new SgExprListExp(*new SgVarRefExp(argsKer->notAcrossS[it]))); + + for (int it = 0; it < argsKer->idxAcross.size(); ++it) + arg_list = AddListToList(arg_list, new SgExprListExp(*new SgVarRefExp(argsKer->idxAcross[it]))); + + for (int it = 0; it < argsKer->idxNotAcross.size(); ++it) + arg_list = AddListToList(arg_list, new SgExprListExp(*new SgVarRefExp(argsKer->idxNotAcross[it]))); + if (uses_list) arg_list = AddListToList(arg_list, CreateUsesDummyList()); //[+ ] if (argsKer->symb.size() >= 3) - { - for (list::iterator it = argsKer->sizeVars.begin(); it != argsKer->sizeVars.end(); ++it) - { - ae = new SgExprListExp(*new SgVarRefExp(*it)); - arg_list = AddListToList(arg_list, ae); - } - } + for (int it = 0; it < argsKer->sizeVars.size(); ++it) + arg_list = AddListToList(arg_list, new SgExprListExp(*new SgVarRefExp(argsKer->sizeVars[it]))); if (argsKer->acrossS.size() != 1) { @@ -4075,7 +3990,7 @@ SgExpression *CreateKernelDummyListAcross(ArgsForKernel *argsKer, SgType *idxTyp return arg_list; } -SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, SgType *idxTypeInKernel) +SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel* argsKer, SgType *idxTypeInKernel) { #if debugMode mywarn("strat: CreateLoopKernelAcross"); @@ -4098,7 +4013,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, S return(NULL); nloop = ParLoopRank(); - // create kernel procedure for loop in Fortran-Cuda language or kernel function in C_Cuda + // create kernel procedure for loop in Fortran-Cuda language or kernel function in C_Cuda // creating Header and End Statement of Kernel if (options.isOn(C_CUDA)) { @@ -4121,12 +4036,12 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, S // create dummy argument list of kernel: if (options.isOn(C_CUDA)) - fe->setLhs(CreateKernelDummyListAcross(argsKer, longType)); //s_red_count_k, + fe->setLhs(CreateKernelDummyListAcross(argsKer, longType)); //s_red_count_k, else // create dummy argument list and add it to kernel header statement (Fortran-Cuda) - kernel_st->setExpression(0, *CreateKernelDummyListAcross(argsKer, longType)); //s_red_count_k, + kernel_st->setExpression(0, *CreateKernelDummyListAcross(argsKer, longType)); //s_red_count_k, - // generating block of index variables calculation + // generating block of index variables calculation #if debugMode mywarn("start: block4"); @@ -4154,7 +4069,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, S if (options.isOn(C_CUDA)) for_st = new SgForStmt(&SgAssignOp(*new SgVarRefExp(argsKer->otherVarsForOneTh[size - 1]), *new SgVarRefExp(argsKer->otherVars[size1 - 3])), &(*sign * *new SgVarRefExp(argsKer->otherVarsForOneTh[size - 1]) <= *sign * *new SgVarRefExp(argsKer->otherVars[size1 - 2])), &SgAssignOp(*new SgVarRefExp(argsKer->otherVarsForOneTh[size - 1]), *new SgVarRefExp(argsKer->otherVarsForOneTh[size - 1]) + *new SgVarRefExp(argsKer->otherVars[size1 - 1])), NULL); else - for_st = new SgForStmt(argsKer->otherVarsForOneTh[size - 1], new SgVarRefExp(argsKer->otherVars[size1 - 3]), new SgVarRefExp(argsKer->otherVars[size1 - 2]), new SgVarRefExp(argsKer->otherVars[size1 - 1]), NULL); + for_st = new SgForStmt(argsKer->otherVarsForOneTh[size - 1], new SgVarRefExp(argsKer->otherVars[size1 - 3]), new SgVarRefExp(argsKer->otherVars[size1 - 2]), new SgVarRefExp(argsKer->otherVars[size1 - 1]), NULL); inner_for_st = for_st; for (int i = size - 2; i >= 0; i--) @@ -4172,7 +4087,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, S } if_st = new SgIfStmt(SgEqOp(*new SgVarRefExp(*tid), *new SgValueExp(0)), *for_st); - cur_in_kernel->insertStmtAfter(*if_st, *kernel_st); + cur_in_kernel->insertStmtAfter(*if_st, *kernel_st); #if debugMode mywarn(" end: block4"); @@ -4215,7 +4130,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, S if (stk->variant() == CONTROL_END) { - if (stk->hasLabel() || stk == loop_body) // when body of DO_ENDDO loop is empty, stk == loop_body + if (stk->hasLabel() || stk == loop_body) // when body of DO_ENDDO loop is empty, stk == loop_body stk->setVariant(CONT_STAT); else { @@ -4238,11 +4153,11 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, S vector < stack < SgStatement*> > zero = vector < stack < SgStatement*> >(0); Translate_Fortran_To_C(inner_for_st, inner_for_st->lastNodeOfStmt(), zero, 0); } - + cur_st = last; createBodyKernel = false; } - + #if debugMode mywarn(" end: inserting loop body"); mywarn("start: create reduction block"); @@ -4309,7 +4224,20 @@ static SgStatement* makeBlockIdxAssigment(SgSymbol* tid, const char* XYZ) return st; } -SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, int acrossNum, SgType *idxTypeInKernel) +static void createDeclaration(SgSymbol* toDecl) +{ + SgStatement* st = toDecl->makeVarDeclStmt(); + st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); + kernel_st->insertStmtAfter(*st); +} + +static void createDeclaration(const vector& toDecl) +{ + for (int it = 0; it < toDecl.size(); ++it) + createDeclaration(toDecl[it]); +} + +SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel* argsKer, int acrossNum, SgType *idxTypeInKernel) { #if debugMode mywarn("strat: CreateLoopKernelAcross"); @@ -4327,15 +4255,15 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i SgSymbol *tid = NULL, *tid1 = NULL, *tid2 = NULL, *s_red_count_k = NULL, *coords = NULL; SgIfStmt *if_st = NULL, *if_st1 = NULL, *if_st2 = NULL; SgForStmt *mainFor = NULL; - SgSymbol *tmpvar1 = NULL; + SgSymbol *tmpvar1 = NULL; SgExpression **leftExprs, **rightExprs; SgType *longType = idxTypeInKernel; - + if (!skernel) return(NULL); nloop = ParLoopRank(); - // create kernel procedure for loop in Fortran-Cuda language or kernel function in C_Cuda + // create kernel procedure for loop in Fortran-Cuda language or kernel function in C_Cuda // creating Header and End Statement of Kernel if (options.isOn(C_CUDA)) { @@ -4358,7 +4286,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i st_end = kernel_st->lexNext(); cur_in_kernel = st = kernelScope = kernel_st; - // !!creating variables and making structures for reductions + // !!creating variables and making structures for reductions CompleteStructuresForReductionInKernel(); //CompleteStructuresForReductionInKernelAcross(); if (red_list) @@ -4371,7 +4299,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i // create dummy argument list and add it to kernel header statement (Fortran-Cuda) kernel_st->setExpression(0, *CreateKernelDummyListAcross(argsKer, idxTypeInKernel)); // s_red_count_k, - // generating block of index variables calculation + // generating block of index variables calculation #if debugMode mywarn("start: block4"); @@ -4380,7 +4308,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i SgArrayType *tpArr = new SgArrayType(*longType); SgValueExp *dimSize = new SgValueExp((int)(argsKer->symb.size() + argsKer->nSymb.size())); tpArr->addDimension(dimSize); - + coords = new SgSymbol(VARIABLE_NAME, TestAndCorrectName("coords"), *longType, *cur_in_kernel); coords->setType(tpArr); @@ -4468,38 +4396,38 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i int idx_exprs = 0; int count_of_dims = argsKer->nSymb.size() + argsKer->symb.size(); - list::iterator itAcr = argsKer->symb.begin(); - list::iterator it = argsKer->nSymb.begin(); - list::iterator itAcrS = argsKer->acrossS.begin(); - list::iterator itS = argsKer->notAcrS.begin(); - list::iterator it_sizeV = argsKer->sizeVars.begin(); - list::iterator itIdxAcr = argsKer->idxAcross.begin(); - list::iterator itIdx = argsKer->idxNotAcross.begin(); + vector::iterator itAcr = argsKer->symb.begin(); + vector::iterator it = argsKer->nSymb.begin(); + vector::iterator itAcrS = argsKer->acrossS.begin(); + vector::iterator itS = argsKer->notAcrossS.begin(); + vector::iterator it_sizeV = argsKer->sizeVars.begin(); + vector::iterator itIdxAcr = argsKer->idxAcross.begin(); + vector::iterator itIdx = argsKer->idxNotAcross.begin(); leftExprs = new SgExpression*[count_of_dims]; rightExprs = new SgExpression*[count_of_dims]; e = &(*new SgVarRefExp(*itAcrS)); - st = AssignStatement(*new SgVarRefExp((*itAcr)->symb), *e); + st = AssignStatement(*new SgVarRefExp((*itAcr).symb), *e); - leftExprs[idx_exprs] = &(*new SgVarRefExp((*itAcr)->symb)); + leftExprs[idx_exprs] = &(*new SgVarRefExp((*itAcr).symb)); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itAcrS)); idx_exprs++; if (argsKer->nSymb.size() == 1) { - st = AssignStatement(*new SgVarRefExp((*it)->symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdx)); + st = AssignStatement(*new SgVarRefExp((*it).symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdx)); - leftExprs[idx_exprs] = &(*new SgVarRefExp((*it)->symb)); + leftExprs[idx_exprs] = &(*new SgVarRefExp((*it).symb)); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdx)); idx_exprs++; } else if (argsKer->nSymb.size() == 2) { - st = AssignStatement(*new SgVarRefExp((*it)->symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdx)); + st = AssignStatement(*new SgVarRefExp((*it).symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdx)); - leftExprs[idx_exprs] = &(*new SgVarRefExp((*it)->symb)); + leftExprs[idx_exprs] = &(*new SgVarRefExp((*it).symb)); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdx)); idx_exprs++; @@ -4507,9 +4435,9 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i itIdx++; itS++; - st = AssignStatement(*new SgVarRefExp((*it)->symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*itIdx)); + st = AssignStatement(*new SgVarRefExp((*it).symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*itIdx)); - leftExprs[idx_exprs] = &(*new SgVarRefExp((*it)->symb)); + leftExprs[idx_exprs] = &(*new SgVarRefExp((*it).symb)); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*itIdx)); idx_exprs++; @@ -4519,9 +4447,9 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i } else if (argsKer->nSymb.size() >= 3) { - st = AssignStatement(*new SgVarRefExp((*it)->symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdx)); + st = AssignStatement(*new SgVarRefExp((*it).symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdx)); - leftExprs[idx_exprs] = &(*new SgVarRefExp((*it)->symb)); + leftExprs[idx_exprs] = &(*new SgVarRefExp((*it).symb)); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdx)); idx_exprs++; @@ -4529,9 +4457,9 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i itIdx++; itS++; - st = AssignStatement(*new SgVarRefExp((*it)->symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*itIdx)); + st = AssignStatement(*new SgVarRefExp((*it).symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*itIdx)); - leftExprs[idx_exprs] = &(*new SgVarRefExp((*it)->symb)); + leftExprs[idx_exprs] = &(*new SgVarRefExp((*it).symb)); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*itIdx)); idx_exprs++; @@ -4551,9 +4479,9 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i funCall->addArg(*e_z1); tmp_exp = &(*new SgVarRefExp(*itS) + *funCall * *new SgVarRefExp(*itIdx)); - st = AssignStatement(*new SgVarRefExp((*it)->symb), *tmp_exp); + st = AssignStatement(*new SgVarRefExp((*it).symb), *tmp_exp); - leftExprs[idx_exprs] = &(*new SgVarRefExp((*it)->symb)); + leftExprs[idx_exprs] = &(*new SgVarRefExp((*it).symb)); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *funCall * *new SgVarRefExp(*itIdx)); idx_exprs++; @@ -4575,9 +4503,9 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i tmp_exp = &(*new SgVarRefExp(*itS) + *funCall * *new SgVarRefExp(*itIdx)); } - st = AssignStatement(*new SgVarRefExp((*it)->symb), *tmp_exp); + st = AssignStatement(*new SgVarRefExp((*it).symb), *tmp_exp); - leftExprs[idx_exprs] = &(*new SgVarRefExp((*it)->symb)); + leftExprs[idx_exprs] = &(*new SgVarRefExp((*it).symb)); rightExprs[idx_exprs] = &(tmp_exp->copy()); idx_exprs++; @@ -4591,9 +4519,9 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i } else { - st = AssignStatement(*new SgVarRefExp((*it)->symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid2) * *new SgVarRefExp(*itIdx)); + st = AssignStatement(*new SgVarRefExp((*it).symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid2) * *new SgVarRefExp(*itIdx)); - leftExprs[idx_exprs] = &(*new SgVarRefExp((*it)->symb)); + leftExprs[idx_exprs] = &(*new SgVarRefExp((*it).symb)); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *new SgVarRefExp(*tid2) * *new SgVarRefExp(*itIdx)); idx_exprs++; @@ -4694,7 +4622,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i if (!options.isOn(C_CUDA) && options.isOn(GPU_O0)) { for (size_t i = 0; i < argsKer->baseIdxsInKer.size(); ++i) - mainFor->lastExecutable()->insertStmtAfter(*AssignStatement(*&leftExprs[i]->copy(), (*(&leftExprs[i]->copy())) + *new SgVarRefExp(argsKer->steps[i])), *mainFor); + mainFor->lastExecutable()->insertStmtAfter(*AssignStatement(*&leftExprs[i]->copy(), (*(&leftExprs[i]->copy())) + *new SgVarRefExp(argsKer->steps[i])), *mainFor); } delete []leftExprs; @@ -4716,17 +4644,17 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i leftExprs = new SgExpression*[count_of_dims]; rightExprs = new SgExpression*[count_of_dims]; - list::iterator itAcr = argsKer->symb.begin(); - list::iterator it = argsKer->nSymb.begin(); - list::iterator itAcrS = argsKer->acrossS.begin(); - list::iterator itS = argsKer->notAcrS.begin(); - list::iterator it_sizeV = argsKer->sizeVars.begin(); - list::iterator itIdxAcr = argsKer->idxAcross.begin(); - list::iterator itIdx = argsKer->idxNotAcross.begin(); + vector::iterator itAcr = argsKer->symb.begin(); + vector::iterator it = argsKer->nSymb.begin(); + vector::iterator itAcrS = argsKer->acrossS.begin(); + vector::iterator itS = argsKer->notAcrossS.begin(); + vector::iterator it_sizeV = argsKer->sizeVars.begin(); + vector::iterator itIdxAcr = argsKer->idxAcross.begin(); + vector::iterator itIdx = argsKer->idxNotAcross.begin(); e = &(*new SgVarRefExp(*itAcrS) - *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdxAcr)); - st = AssignStatement(*new SgVarRefExp((*itAcr)->symb), *e); - leftExprs[idx_exprs] = new SgVarRefExp((*itAcr)->symb); + st = AssignStatement(*new SgVarRefExp((*itAcr).symb), *e); + leftExprs[idx_exprs] = new SgVarRefExp((*itAcr).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itAcrS) - *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdxAcr)); idx_exprs++; @@ -4735,8 +4663,8 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i itIdxAcr++; e = &(*new SgVarRefExp(*itAcrS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdxAcr)); - st = AssignStatement(*new SgVarRefExp((*itAcr)->symb), *e); - leftExprs[idx_exprs] = new SgVarRefExp((*itAcr)->symb); + st = AssignStatement(*new SgVarRefExp((*itAcr).symb), *e); + leftExprs[idx_exprs] = new SgVarRefExp((*itAcr).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itAcrS) + *new SgVarRefExp(*tid) * *new SgVarRefExp(*itIdxAcr)); idx_exprs++; @@ -4746,17 +4674,17 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i if (argsKer->nSymb.size() == 1) { - st = AssignStatement(*new SgVarRefExp((*it)->symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * + st = AssignStatement(*new SgVarRefExp((*it).symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*itIdx)); - leftExprs[idx_exprs] = new SgVarRefExp((*it)->symb); + leftExprs[idx_exprs] = new SgVarRefExp((*it).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*itIdx)); idx_exprs++; } else if (argsKer->nSymb.size() >= 2) { - st = AssignStatement(*new SgVarRefExp((*it)->symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * + st = AssignStatement(*new SgVarRefExp((*it).symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*itIdx)); - leftExprs[idx_exprs] = new SgVarRefExp((*it)->symb); + leftExprs[idx_exprs] = new SgVarRefExp((*it).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*itIdx)); idx_exprs++; @@ -4776,8 +4704,8 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i funCall->addArg(*e_z1); tmp_exp = &(*new SgVarRefExp(*itS) + *funCall * *new SgVarRefExp(*itIdx)); - st = AssignStatement(*new SgVarRefExp((*it)->symb), *tmp_exp); - leftExprs[idx_exprs] = new SgVarRefExp((*it)->symb); + st = AssignStatement(*new SgVarRefExp((*it).symb), *tmp_exp); + leftExprs[idx_exprs] = new SgVarRefExp((*it).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *funCall * *new SgVarRefExp(*itIdx)); idx_exprs++; @@ -4805,8 +4733,8 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i } it--; - st = AssignStatement(*new SgVarRefExp((*it)->symb), *tmp_exp); - leftExprs[idx_exprs] = new SgVarRefExp((*it)->symb); + st = AssignStatement(*new SgVarRefExp((*it).symb), *tmp_exp); + leftExprs[idx_exprs] = new SgVarRefExp((*it).symb); idx_exprs++; e_z1 = &(*e_z1 * *e_z2); @@ -4822,9 +4750,9 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i else for (; it != argsKer->nSymb.end(); it++, itS++, itIdx++) { - st = AssignStatement(*new SgVarRefExp((*it)->symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid2) * + st = AssignStatement(*new SgVarRefExp((*it).symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(*tid2) * *new SgVarRefExp(*itIdx)); - leftExprs[idx_exprs] = new SgVarRefExp((*it)->symb); + leftExprs[idx_exprs] = new SgVarRefExp((*it).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *new SgVarRefExp(*tid2) * *new SgVarRefExp(*itIdx)); idx_exprs++; } @@ -4887,7 +4815,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i } else if (argsKer->symb.size() >= 3) // body for >3 dependence { - // attention!! adding to support all variants!! не проверено + // attention!! adding to support all variants!!не проверено if (argsKer->nSymb.size() >= 1) { @@ -4900,13 +4828,14 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i SgSymbol *max_z, *se, *emax, *emin, *v1, *v2, *v3, *min_ij, *swap_ij, *i, *j; SgSymbol **num_elems; SgIfStmt *if_st3; - list::iterator itAcr = argsKer->symb.begin(); - list::iterator it = argsKer->nSymb.begin(); - list::iterator itAcrS = argsKer->acrossS.begin(); - list::iterator itS = argsKer->notAcrS.begin(); - list::iterator it_sizeV = argsKer->sizeVars.begin(); - list::iterator itIdxAcr = argsKer->idxAcross.begin(); - list::iterator itIdx = argsKer->idxNotAcross.begin(); + + vector::iterator itAcr = argsKer->symb.begin(); + vector::iterator it = argsKer->nSymb.begin(); + vector::iterator itAcrS = argsKer->acrossS.begin(); + vector::iterator itS = argsKer->notAcrossS.begin(); + vector::iterator it_sizeV = argsKer->sizeVars.begin(); + vector::iterator itIdxAcr = argsKer->idxAcross.begin(); + vector::iterator itIdx = argsKer->idxNotAcross.begin(); SgExpression **leftExprs, **rightExprs; int idx_exprs = 0; @@ -4959,7 +4888,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i itAcrS--; itIdxAcr--; - if_st = new SgIfStmt(*new SgVarRefExp(*tid) < *new SgVarRefExp((*itAcr)->symb), *if_st2); + if_st = new SgIfStmt(*new SgVarRefExp(*tid) < *new SgVarRefExp((*itAcr).symb), *if_st2); if (argsKer->nSymb.size() == 0) if_st3 = new SgIfStmt(*new SgVarRefExp(*tid1) < *new SgVarRefExp(*max_z), *if_st); else @@ -4973,23 +4902,23 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i cur_in_kernel->insertStmtAfter(*if_st3, *kernel_st); cur_in_kernel = if_st->lexNext(); - st1 = AssignStatement(*new SgVarRefExp((*itAcr)->symb), *new SgVarRefExp(*min_ij)); + st1 = AssignStatement(*new SgVarRefExp((*itAcr).symb), *new SgVarRefExp(*min_ij)); - st = AssignStatement(*new SgVarRefExp((*itAcr)->symb), *new SgValueExp(2) * *new SgVarRefExp(*min_ij) - *new SgVarRefExp(se) - + st = AssignStatement(*new SgVarRefExp((*itAcr).symb), *new SgValueExp(2) * *new SgVarRefExp(*min_ij) - *new SgVarRefExp(se) - *new SgVarRefExp(tid1) + *new SgVarRefExp(emax) - *new SgVarRefExp(emin) - *new SgValueExp(1)); if_st1 = new SgIfStmt(*new SgVarRefExp(*tid1) + *new SgVarRefExp(se) < *new SgVarRefExp(*emax), *st1, *st); - st1 = AssignStatement(*new SgVarRefExp((*itAcr)->symb), *new SgVarRefExp(*tid1) + *new SgVarRefExp(se)); + st1 = AssignStatement(*new SgVarRefExp((*itAcr).symb), *new SgVarRefExp(*tid1) + *new SgVarRefExp(se)); if_st1 = new SgIfStmt(*new SgVarRefExp(*tid1) + *new SgVarRefExp(se) < *new SgVarRefExp(*emin), *st1, *if_st1); if_st3->insertStmtAfter(*if_st1); - i = (*itAcr)->symb; - st1 = AssignStatement(*new SgVarRefExp((*itAcr)->symb), *new SgVarRefExp(*itAcrS) + ((*new SgVarRefExp(tid1) * + i = (*itAcr).symb; + st1 = AssignStatement(*new SgVarRefExp((*itAcr).symb), *new SgVarRefExp(*itAcrS) + ((*new SgVarRefExp(tid1) * (*new SgVarRefExp(v1) + *new SgVarRefExp(v3)) - *new SgVarRefExp(tid))) * *new SgVarRefExp(*itIdxAcr)); - leftExprs[idx_exprs] = new SgVarRefExp((*itAcr)->symb); + leftExprs[idx_exprs] = new SgVarRefExp((*itAcr).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itAcrS) + ((*new SgVarRefExp(tid1) * (*new SgVarRefExp(v1) + *new SgVarRefExp(v3)) - *new SgVarRefExp(tid))) * *new SgVarRefExp(*itIdxAcr)); idx_exprs++; @@ -4999,11 +4928,11 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i itIdxAcr++; itAcr++; - j = (*itAcr)->symb; - st1 = AssignStatement(*new SgVarRefExp((*itAcr)->symb), *new SgVarRefExp(*itAcrS) + (*new SgVarRefExp(tid1) * + j = (*itAcr).symb; + st1 = AssignStatement(*new SgVarRefExp((*itAcr).symb), *new SgVarRefExp(*itAcrS) + (*new SgVarRefExp(tid1) * *new SgVarRefExp(v2) + *new SgVarRefExp(tid)) * *new SgVarRefExp(*itIdxAcr)); - leftExprs[idx_exprs] = new SgVarRefExp((*itAcr)->symb); + leftExprs[idx_exprs] = new SgVarRefExp((*itAcr).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itAcrS) + (*new SgVarRefExp(tid1) * *new SgVarRefExp(v2) + *new SgVarRefExp(tid)) * *new SgVarRefExp(*itIdxAcr)); idx_exprs++; @@ -5012,10 +4941,10 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i itIdxAcr++; itAcr++; - st1 = AssignStatement(*new SgVarRefExp((*itAcr)->symb), *new SgVarRefExp(*itAcrS) - *new SgVarRefExp(tid1) * + st1 = AssignStatement(*new SgVarRefExp((*itAcr).symb), *new SgVarRefExp(*itAcrS) - *new SgVarRefExp(tid1) * *new SgVarRefExp(*itIdxAcr)); - leftExprs[idx_exprs] = new SgVarRefExp((*itAcr)->symb); + leftExprs[idx_exprs] = new SgVarRefExp((*itAcr).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itAcrS) - *new SgVarRefExp(tid1) * *new SgVarRefExp(*itIdxAcr)); idx_exprs++; @@ -5027,7 +4956,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i itIdxAcr++; itAcr++; - leftExprs[idx_exprs] = new SgVarRefExp((*itAcr)->symb); + leftExprs[idx_exprs] = new SgVarRefExp((*itAcr).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itAcrS)); idx_exprs++; } @@ -5035,10 +4964,10 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i if (argsKer->nSymb.size() == 1) { - st1 = AssignStatement(*new SgVarRefExp((*it)->symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(tid2) * + st1 = AssignStatement(*new SgVarRefExp((*it).symb), *new SgVarRefExp(*itS) + *new SgVarRefExp(tid2) * *new SgVarRefExp(*itIdx)); - leftExprs[idx_exprs] = new SgVarRefExp((*it)->symb); + leftExprs[idx_exprs] = new SgVarRefExp((*it).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *new SgVarRefExp(tid2) * *new SgVarRefExp(*itIdx)); idx_exprs++; } @@ -5051,9 +4980,9 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i funCall->addArg(*e_z1); tmp_exp = &(*new SgVarRefExp(*itS) + *funCall * *new SgVarRefExp(*itIdx)); - st = AssignStatement(*new SgVarRefExp((*it)->symb), *tmp_exp); + st = AssignStatement(*new SgVarRefExp((*it).symb), *tmp_exp); - leftExprs[idx_exprs] = new SgVarRefExp((*it)->symb); + leftExprs[idx_exprs] = new SgVarRefExp((*it).symb); rightExprs[idx_exprs] = &(*new SgVarRefExp(*itS) + *funCall * *new SgVarRefExp(*itIdx)); idx_exprs++; @@ -5079,9 +5008,9 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i } it--; - st = AssignStatement(*new SgVarRefExp((*it)->symb), *tmp_exp); + st = AssignStatement(*new SgVarRefExp((*it).symb), *tmp_exp); - leftExprs[idx_exprs] = new SgVarRefExp((*it)->symb); + leftExprs[idx_exprs] = new SgVarRefExp((*it).symb); idx_exprs++; e_z1 = &(*e_z1 * *e_z2); @@ -5123,7 +5052,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i st1 = AssignStatement(new SgArrayRefExp(*coords, *new SgVarRefExp(argsKer->baseIdxsInKer[0]) + *new SgValueExp(1)), new SgVarRefExp(v3)); if_st2 = new SgIfStmt(*new SgVarRefExp(swap_ij) * *new SgVarRefExp(v3), *st1); - st1 = AssignStatement(new SgArrayRefExp(*coords, *new SgVarRefExp(argsKer->baseIdxsInKer[1]) + *new SgValueExp(1)), + st1 = AssignStatement(new SgArrayRefExp(*coords, *new SgVarRefExp(argsKer->baseIdxsInKer[1]) + *new SgValueExp(1)), new SgArrayRefExp(*coords, *new SgVarRefExp(argsKer->baseIdxsInKer[0]) + *new SgValueExp(1))); if_st2->insertStmtAfter(*st1); @@ -5160,12 +5089,12 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i SgStatement *currStForInsetGetXY = cur_in_kernel; vector forDeclarationInKernel; set uniqueNames; - + // create, insert, optimize and convert loop_body into kernel { SgStatement *stk, *last; vector allNewInfo; - + if (argsKer->symb.size() == 1) { if (options.isOn(GPU_O0)) @@ -5177,12 +5106,11 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i block = CreateIfForRedBlack(loop_body, nloop); - last = cur_in_kernel->lexNext(); + last = cur_in_kernel->lexNext(); if (argsKer->symb.size() == 1 && allNewInfo.size() != 0 && options.isOn(GPU_O0)) //insert needed assigns { - list::iterator itIdxAcr = argsKer->idxAcross.begin(); - SgIfStmt *ifSt = new SgIfStmt(*new SgVarRefExp(*itIdxAcr) > *new SgValueExp(0), *&allNewInfo[0].loadsBeforePlus[0]->copy(), *&allNewInfo[0].loadsBeforeMinus[0]->copy()); + SgIfStmt *ifSt = new SgIfStmt(*new SgVarRefExp(argsKer->idxAcross[0]) > *new SgValueExp(0), *&allNewInfo[0].loadsBeforePlus[0]->copy(), *&allNewInfo[0].loadsBeforeMinus[0]->copy()); for (size_t i = 0; i < allNewInfo.size(); ++i) { if (i == 0) @@ -5200,18 +5128,18 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i ifSt->insertStmtAfter(*&allNewInfo[i].loadsBeforePlus[k]->copy(), *ifSt); ifSt->falseBody()->insertStmtBefore(*&allNewInfo[i].loadsBeforeMinus[k]->copy(), *ifSt); } - } + } } mainFor->insertStmtBefore(*ifSt); } - + if (argsKer->symb.size() == 1 && options.isOn(GPU_O0)) cur_in_kernel->insertStmtAfter(*block, *mainFor); //cur_in_kernel is innermost FOR stmt else cur_in_kernel->insertStmtAfter(*block, *if_st); //cur_in_kernel is innermost IF statement if (options.isOn(C_CUDA)) - { + { if (block->comments() == NULL) block->addComment("// Loop body"); } @@ -5226,7 +5154,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i if (stk->variant() == CONTROL_END) { - if (stk->hasLabel() || stk == loop_body) // when body of DO_ENDDO loop is empty, stk == loop_body + if (stk->hasLabel() || stk == loop_body) // when body of DO_ENDDO loop is empty, stk == loop_body stk->setVariant(CONT_STAT); else { @@ -5243,8 +5171,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i if (argsKer->symb.size() == 1 && allNewInfo.size() != 0 && options.isOn(GPU_O0)) //insert needed assigns { - list::iterator itIdxAcr = argsKer->idxAcross.begin(); - SgIfStmt *ifSt = new SgIfStmt(*new SgVarRefExp(*itIdxAcr) > *new SgValueExp(0), *&allNewInfo[0].loadsInForPlus[0]->copy(), *&allNewInfo[0].loadsInForMinus[0]->copy()); + SgIfStmt *ifSt = new SgIfStmt(*new SgVarRefExp(argsKer->idxAcross[0]) > *new SgValueExp(0), *&allNewInfo[0].loadsInForPlus[0]->copy(), *&allNewInfo[0].loadsInForMinus[0]->copy()); for (size_t i = 0; i < allNewInfo.size(); ++i) { @@ -5272,12 +5199,12 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i else { for (size_t k = 0; k < allNewInfo[i].stores.size(); ++k) - mainFor->lastExecutable()->lexPrev()->lexPrev()->insertStmtBefore(*&allNewInfo[i].stores[k]->copy()); + mainFor->lastExecutable()->lexPrev()->lexPrev()->insertStmtBefore(*&allNewInfo[i].stores[k]->copy()); } - } + } size_t k = allNewInfo[0].swapsUp.size() - 1; - ifSt = new SgIfStmt(*new SgVarRefExp(*itIdxAcr) > *new SgValueExp(0), *&allNewInfo[0].swapsDown[k]->copy(), *&allNewInfo[0].swapsUp[k]->copy()); + ifSt = new SgIfStmt(*new SgVarRefExp(argsKer->idxAcross[0]) > *new SgValueExp(0), *&allNewInfo[0].swapsDown[k]->copy(), *&allNewInfo[0].swapsUp[k]->copy()); for (size_t i = 0; i < allNewInfo.size(); ++i) { size_t last; @@ -5412,7 +5339,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i else Translate_Fortran_To_C(if_st, if_st->lastNodeOfStmt(), copyOfBody, 0); // countOfCopies } - + cur_st = last; if (createBodyKernel == false) createBodyKernel = true; @@ -5529,7 +5456,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i int loc_el_num = 0; if (isSgExprListExp(red_expr_ref)) { - red_expr_ref = red_expr_ref->lhs(); // reduction variable reference + red_expr_ref = red_expr_ref->lhs(); // reduction variable reference loc_var_ref = er->lhs()->rhs()->rhs()->lhs(); //location array reference en = er->lhs()->rhs()->rhs()->rhs()->lhs(); // number of elements in location array loc_el_num = LocElemNumber(en); @@ -5591,14 +5518,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i SgSymbol *redGrid = new SgSymbol(VARIABLE_NAME, tmp_list->red_grid->identifier()); redGrid->setType(*new SgArrayType(*tmp_list->red_grid->type())); - list::iterator it_sizeV = argsKer->sizeVars.begin(); - it_sizeV++; - it_sizeV++; - it_sizeV++; - it_sizeV++; - it_sizeV++; - it_sizeV++; - SgSymbol *emin = *it_sizeV; + SgSymbol *emin = argsKer->sizeVars[6]; funcCall->addArg(*new SgArrayRefExp(*redGrid, *new SgVarRefExp(*tid) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*emin))); funcCall->addArg(*red_expr_ref); st = AssignStatement(*new SgArrayRefExp(*redGrid, *new SgVarRefExp(*tid) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*emin)), *funcCall); @@ -5620,14 +5540,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i } else { - list::iterator it_sizeV = argsKer->sizeVars.begin(); - it_sizeV++; - it_sizeV++; - it_sizeV++; - it_sizeV++; - it_sizeV++; - it_sizeV++; - SgSymbol *emin = *it_sizeV; + SgSymbol *emin = argsKer->sizeVars[6]; e1 = &(*new SgVarRefExp(*tid) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*emin)); } e = NULL; @@ -5636,11 +5549,11 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i redGrid->setType(*new SgArrayType(*tmp_list->red_grid->type())); SgExpression* red_ref = NULL; - + if (tmp_list->redvar_size == 0) red_ref = red_expr_ref; else // TODO - red_ref = new SgArrayRefExp(*red_expr_ref->symbol(), *new SgVarRefExp(freeS)); + red_ref = new SgArrayRefExp(*red_expr_ref->symbol(), *new SgVarRefExp(freeS)); if (num == 1) e = &(*new SgArrayRefExp(*redGrid, *e1) + *red_ref); @@ -5704,7 +5617,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i needComment = 0; } } - + DeclarationCreateReductionBlocksAcross(nloop, red_list); } else if (red_list && argsKer->nSymb.size() > 0) // generating reduction calculation blocks @@ -5765,88 +5678,41 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i if (!options.isOn(C_CUDA)) { - for (list::iterator it1 = argsKer->sizeVars.begin(); it1 != argsKer->sizeVars.end(); ++it1) - { - st = (*it1)->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); - } - - for (list::iterator it = argsKer->acrossS.begin(); it != argsKer->acrossS.end(); ++it) - { - st = (*it)->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); - } - - for (list::iterator it = argsKer->notAcrS.begin(); it != argsKer->notAcrS.end(); ++it) - { - st = (*it)->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); - } - - for (list::iterator it = argsKer->idxAcross.begin(); it != argsKer->idxAcross.end(); ++it) - { - st = (*it)->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); - } - - for (list::iterator it = argsKer->idxNotAcross.begin(); it != argsKer->idxNotAcross.end(); ++it) - { - st = (*it)->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); - } + createDeclaration(argsKer->sizeVars); + createDeclaration(argsKer->acrossS); + createDeclaration(argsKer->notAcrossS); + createDeclaration(argsKer->idxAcross); + createDeclaration(argsKer->idxNotAcross); for (size_t i = 0; i < argsKer->otherVars.size() / 8 * 8; i += 8) { - st = argsKer->otherVars[i]->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); + createDeclaration(argsKer->otherVars[i]); addDeclExpList(argsKer->otherVars[i + 3], st->expr(0)); - st = argsKer->otherVars[i + 1]->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); + createDeclaration(argsKer->otherVars[i + 1]); addDeclExpList(argsKer->otherVars[i + 4], st->expr(0)); - st = argsKer->otherVars[i + 2]->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); + createDeclaration(argsKer->otherVars[i + 2]); addDeclExpList(argsKer->otherVars[i + 5], st->expr(0)); - st = argsKer->otherVars[i + 6]->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); + createDeclaration(argsKer->otherVars[i + 6]); addDeclExpList(argsKer->otherVars[i + 7], st->expr(0)); } if (argsKer->otherVars.size() != 0 && argsKer->otherVars.size() % 8 != 0) - { - st = argsKer->otherVars[argsKer->otherVars.size() - 1]->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); - } + createDeclaration(argsKer->otherVars[argsKer->otherVars.size() - 1]); for (size_t i = 0; i < argsKer->baseIdxsInKer.size(); ++i) { if (i == 0) - { - st = argsKer->baseIdxsInKer[i]->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); - } + createDeclaration(argsKer->baseIdxsInKer[i]); else addDeclExpList(argsKer->baseIdxsInKer[i], st->expr(0)); } if (argsKer->cond_ != NULL) { - st = argsKer->cond_->makeVarDeclStmt(); - st->setExpression(2, *new SgExprListExp(*new SgExpression(ACC_VALUE_OP))); - kernel_st->insertStmtAfter(*st); + createDeclaration(argsKer->cond_); for (size_t i = 0; i < argsKer->steps.size(); ++i) addDeclExpList(argsKer->steps[i], st->expr(0)); } @@ -5860,7 +5726,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel *argsKer, i kernel_st->insertStmtAfter(*new SgStatement(IMPL_DECL), *kernel_st); ACROSS_MOD_IN_KERNEL = 0; - return(kernel_st); + return kernel_st; } @@ -5918,13 +5784,13 @@ void DeclarationOfReductionBlockInKernelAcross(SgExpression *ered, reduction_ope i = ind = loc_el_num = 0; //end of init block - // analys of reduction operation + // analys of reduction operation // ered - reduction operation (variant==ARRAY_OP) - ev = ered->rhs(); // reduction variable reference for reduction operations except MINLOC,MAXLOC - if (isSgExprListExp(ev)) // for MAXLOC,MINLOC + ev = ered->rhs(); // reduction variable reference for reduction operations except MINLOC,MAXLOC + if (isSgExprListExp(ev)) // for MAXLOC,MINLOC { loc_var = ev->rhs()->lhs()->symbol(); //location array reference - ev = ev->lhs(); // reduction variable reference + ev = ev->lhs(); // reduction variable reference } else loc_var = NULL; @@ -5935,7 +5801,7 @@ void DeclarationOfReductionBlockInKernelAcross(SgExpression *ered, reduction_ope // _block(i)%(1) = (1) // [_block(i)%(2) = (2) ] // . . . - // create and declare array '_block' + // create and declare array '_block' red_var = ev->symbol(); if (rsl->locvar) @@ -6022,7 +5888,7 @@ void CreateReductionBlocksAcross(SgStatement *stat, int nloop, SgExpression *red i_var = dost->symbol(); if (nloop > 1) j_var = dost->controlParent()->symbol(); - else + else { j_var = IndVarInKernel(i_var); newst = j_var->makeVarDeclStmt(); @@ -6032,26 +5898,16 @@ void CreateReductionBlocksAcross(SgStatement *stat, int nloop, SgExpression *red // declare '_block' array for each reduction var // = threadIdx%x -1 + [ (threadIdx%y - 1) * blockDim%x [ + (threadIdx%z - 1) * blockDim%x * blockDim%y ] ] // or C_Cuda - // = threadIdx%x + [ threadIdx%y * blockDim%x [ + threadIdx%z * blockDim%x * blockDim%y ] ] + // = threadIdx%x + [ threadIdx%y * blockDim%x [ + threadIdx%z * blockDim%x * blockDim%y ] ] re = ThreadIdxRefExpr("x"); if (nloop > 1) re = &(*re + (*ThreadIdxRefExpr("y")) * (*new SgRecordRefExp(*s_blockdim, "x"))); if (nloop > 2) re = &(*re + (*ThreadIdxRefExpr("z")) * (*new SgRecordRefExp(*s_blockdim, "x") * (*new SgRecordRefExp(*s_blockdim, "y")))); - + if (options.isOn(C_CUDA)) // global cuda index - { - /*SgExpression& globalX = (*new SgRecordRefExp(*s_blockdim, "x") * *new SgRecordRefExp(*s_blockidx, "x") + *new SgRecordRefExp(*s_threadidx, "x")); - SgExpression& globalY = (*new SgRecordRefExp(*s_blockdim, "y") * *new SgRecordRefExp(*s_blockidx, "y") + *new SgRecordRefExp(*s_threadidx, "y")); - SgExpression& globalZ = (*new SgRecordRefExp(*s_blockdim, "z") * *new SgRecordRefExp(*s_blockidx, "z") + *new SgRecordRefExp(*s_threadidx, "z")); - - SgExpression& globalDimX = (*new SgRecordRefExp(*s_griddim, "x") * *new SgRecordRefExp(*s_blockdim, "x")); - SgExpression& globalDimY = (*new SgRecordRefExp(*s_griddim, "y") * *new SgRecordRefExp(*s_blockdim, "y") * globalDimX); - - ass = new SgAssignStmt(*new SgVarRefExp(i_var), globalX + globalY * globalDimX + globalZ * globalDimY);*/ - - + { // gIDX = threadIdx.x + threadIdx.y * blockDim.x + threadIdx.z * blockDim.x * blockDim.y + (blockIdx.x + blockIdx.y * gridDim.x + blockIdx.z * gridDim.x * gridDim.y) * blockDim.x * blockDim.y * blockDim.z; SgExpression& thrX = *new SgRecordRefExp(*s_threadidx, "x"); SgExpression& thrY = *new SgRecordRefExp(*s_threadidx, "y"); diff --git a/dvm/fdvm/trunk/fdvm/aks_structs.cpp b/dvm/fdvm/trunk/fdvm/aks_structs.cpp index 935858b..ab4da20 100644 --- a/dvm/fdvm/trunk/fdvm/aks_structs.cpp +++ b/dvm/fdvm/trunk/fdvm/aks_structs.cpp @@ -1,5 +1,12 @@ #include "dvm.h" #include "aks_structs.h" +#include +#include +#include + +using std::vector; +using std::string; +using std::map; #define DEBUG_LV1 true #if 1 @@ -16,22 +23,20 @@ SgExpression* findDirect(SgExpression *inExpr, int DIR) if (inExpr) { if (inExpr->variant() == DIR) - { return inExpr; - } else { - if (inExpr->lhs()) - temp = findDirect(inExpr->lhs(), DIR); + if (inExpr->lhs()) + temp = findDirect(inExpr->lhs(), DIR); - if(temp == NULL && inExpr->rhs()) + if(temp == NULL && inExpr->rhs()) temp = findDirect(inExpr->rhs(), DIR); } } return temp; } -static SgSymbol** fillDataOfArray(SgExpression* on, int& dimInPar) +static vector fillDataOfArray(SgExpression* on, int& dimInPar) { dimInPar = 0; SgExpression* temp = on; @@ -40,7 +45,8 @@ static SgSymbol** fillDataOfArray(SgExpression* on, int& dimInPar) dimInPar++; temp = temp->rhs(); } - SgSymbol** symbInPar = new SgSymbol * [dimInPar]; + + vector symbInPar(dimInPar); temp = on; for (int i = 0; i < dimInPar; ++i) { @@ -50,20 +56,20 @@ static SgSymbol** fillDataOfArray(SgExpression* on, int& dimInPar) return symbInPar; } -SageArrayIdxs* GetIdxInParDir(const std::map& on, SgExpression *across, bool tie = false) +static void printError() { - SageArrayIdxs *ret = new SageArrayIdxs(); - SageArrayIdxs *act = ret; - int allDim = 0; - int dimInPar = 0; - SgSymbol** symbInPar = NULL; - ret->next = NULL; - ret->array_expr = NULL; - ret->read_write = -1; - ret->dim = 0; - ret->symb = NULL; + err("internal error in across", 424, first_do_par); + exit(-1); +} + +static vector GetIdxInParDir(const map& on, SgExpression *across, bool tie = false) +{ + vector ret; + + int dimInPar = 0; + vector symbInPar; + vector toAnalyze; - std::vector toAnalyze; if (across->lhs()->variant() == EXPR_LIST) toAnalyze.push_back(across->lhs()); else @@ -80,15 +86,12 @@ SageArrayIdxs* GetIdxInParDir(const std::map& on, Sg across = toAnalyze[i]; while (across) { - if (symbInPar == NULL) + if (symbInPar.size() == 0) { if (on.size() == 0) - { - fprintf(stderr, "internal error in across convertion for GPU\n"); - exit(-1); - } + printError(); else if (on.size() == 1) - symbInPar = fillDataOfArray(on.begin()->second, dimInPar); + symbInPar = fillDataOfArray(on.begin()->second, dimInPar); } SgExpression *t = across->lhs(); @@ -99,20 +102,14 @@ SageArrayIdxs* GetIdxInParDir(const std::map& on, Sg if (t->variant() == ARRAY_REF) { if (on.find(t->symbol()->identifier()) == on.end()) - { - fprintf(stderr, "internal error in across convertion for GPU\n"); - exit(-1); - } + printError(); else symbInPar = fillDataOfArray(on.find(t->symbol()->identifier())->second, dimInPar); } else if (t->variant() == ARRAY_OP) { if (on.find(t->lhs()->symbol()->identifier()) == on.end()) - { - fprintf(stderr, "internal error in across convertion for GPU\n"); - exit(-1); - } + printError(); else symbInPar = fillDataOfArray(on.find(t->lhs()->symbol()->identifier())->second, dimInPar); } @@ -134,53 +131,50 @@ SageArrayIdxs* GetIdxInParDir(const std::map& on, Sg dim++; tmp = tmp->rhs(); } - act->next = new SageArrayIdxs(); - act = act->next; - act->next = NULL; - act->symb = new SageSymbols*[dim]; - act->dim = dim; + + SageArrayIdxs act; + + act.symb.resize(dim); + act.dim = dim; for (int i = 0; i < dim; ++i) { - act->symb[i] = new SageSymbols(); - act->symb[i]->across_left = t->lhs()->lhs()->valueInteger(); - act->symb[i]->across_right = t->lhs()->rhs()->valueInteger(); - if (act->symb[i]->across_left != 0 || act->symb[i]->across_right != 0) - act->symb[i]->symb = symbInPar[i]; + act.symb[i].across_left = t->lhs()->lhs()->valueInteger(); + act.symb[i].across_right = t->lhs()->rhs()->valueInteger(); + if (act.symb[i].across_left != 0 || act.symb[i].across_right != 0) + act.symb[i].symb = symbInPar[i]; else if (i < dimInPar) - act->symb[i]->symb = symbInPar[i]; + act.symb[i].symb = symbInPar[i]; else - act->symb[i]->symb = NULL; - act->symb[i]->next = NULL; + act.symb[i].symb = NULL; t = t->rhs(); } - allDim++; + ret.push_back(act); across = across->rhs(); } } - ret->dim = allDim; + return ret; } -SageAcrossInfo* GetLoopsWithParAndAcrDir() +SageAcrossInfo GetLoopsWithParAndAcrDir() { - SageAcrossInfo *q = NULL; + SageAcrossInfo retVal; SgStatement *temp = dvm_parallel_dir; if (temp->variant() == DVM_PARALLEL_ON_DIR) { SgExpression *t = findDirect(temp->expr(1), ACROSS_OP); SgExpression *tie = findDirect(temp->expr(1), ACC_TIE_OP); - - std::map arrays; + + map arrays; if (t != NULL) { - q = new SageAcrossInfo(); if (temp->expr(0) && temp->expr(0)->lhs()) { arrays[temp->expr(0)->symbol()->identifier()] = temp->expr(0)->lhs(); - q->idx = GetIdxInParDir(arrays, t); - } + retVal.idxs = GetIdxInParDir(arrays, t); + } else if (tie) { SgExpression* list = tie->lhs(); @@ -189,34 +183,24 @@ SageAcrossInfo* GetLoopsWithParAndAcrDir() arrays[list->lhs()->symbol()->identifier()] = list->lhs()->lhs(); list = list->rhs(); } - q->idx = GetIdxInParDir(arrays, t, true); + retVal.idxs = GetIdxInParDir(arrays, t, true); } else - { - fprintf(stderr, "internal error in across convertion for GPU\n"); - exit(-1); - } - q->next = NULL; + printError(); } - } - return q; + } + return retVal; } -SageSymbols *GetSymbInParalell(int *n, SgExpression *first) +vector GetSymbInParalell(SgExpression *first) { - SageSymbols *retval; - SageSymbols *p_t = new SageSymbols(); - retval = p_t; + vector retval; while(first) { - SageSymbols *q = new SageSymbols(); - q->len = -1; - q->next = NULL; - q->symb = first->lhs()->symbol(); - p_t->next = q; - p_t = q; - n[0]++; + SageSymbols q(first->lhs()->symbol(), -1, 0, 0); + retval.push_back(q); + first = first->rhs(); } - return retval->next; + return retval; } diff --git a/dvm/fdvm/trunk/fdvm/dvm.cpp b/dvm/fdvm/trunk/fdvm/dvm.cpp index 2ed78be..c63cd24 100644 --- a/dvm/fdvm/trunk/fdvm/dvm.cpp +++ b/dvm/fdvm/trunk/fdvm/dvm.cpp @@ -4757,7 +4757,7 @@ void RedistributeArray(SgSymbol *das, int idisars, SgExpression *distr_rule_list if_st = doIfThenConstrForRedis(headref_flag,stdis,iamv); /*08.05.17*/ where = end_if = if_st->lexNext()->lexNext(); // reffer to ENDIF statement i1 = ndvm; - if(ACC_program) /*ACC*/ + if(ACC_program || parloop_by_handler) /*ACC*/ where->insertStmtBefore(*Redistribute_H(headref,sign),*where->controlParent()); amvref = (ia & TEMPLATE_BIT) ? headref : GetAMView( headref); //inserting after ELSE @@ -4838,7 +4838,7 @@ void RedistributeArray(SgSymbol *das, int idisars, SgExpression *distr_rule_list else { SgExpression *amvref; - if(ACC_program) /*ACC*/ + if(ACC_program || parloop_by_handler) /*ACC*/ where->insertStmtBefore(*Redistribute_H(headref,sign),*where->controlParent()); amvref = (ia & TEMPLATE_BIT) ? headref : GetAMView( headref); @@ -5108,7 +5108,7 @@ void RealignArray(SgSymbol *als, SgSymbol *tgs, int iaxis, int nr, SgExpression return; } iamv = ndvm; - if(ACC_program ) /*ACC*/ + if(ACC_program || parloop_by_handler) /*ACC*/ { if( !(ia & POSTPONE_BIT) ) doCallAfter(Realign_H(HeaderRef(als),new_sign)); else { diff --git a/dvm/fdvm/trunk/fdvm/funcall.cpp b/dvm/fdvm/trunk/fdvm/funcall.cpp index db93dc8..1c387d7 100644 --- a/dvm/fdvm/trunk/fdvm/funcall.cpp +++ b/dvm/fdvm/trunk/fdvm/funcall.cpp @@ -2668,7 +2668,7 @@ SgStatement *RTL_GPU_Init() SgCallStmt *call = new SgCallStmt(*fdvm[DVMH_INIT]); fmask[DVMH_INIT] = 2; call -> addArg(*DVM000(ndvm)); - if(!only_debug && ACC_program) + if(!only_debug && (ACC_program || parloop_by_handler)) call -> addComment(OpenMpComment_InitFlags(ndvm)); int flag = 1; diff --git a/dvm/fdvm/trunk/fdvm/parloop.cpp b/dvm/fdvm/trunk/fdvm/parloop.cpp index ec5ae39..6dc5acc 100644 --- a/dvm/fdvm/trunk/fdvm/parloop.cpp +++ b/dvm/fdvm/trunk/fdvm/parloop.cpp @@ -282,7 +282,7 @@ void EndOfParallelLoopNest(SgStatement *stmt, SgStatement *end_stmt, SgStatement ConsistentArraysStart(cons_list); if(iconsg) {//there is synchronous CONSISTENT clause in PARALLEL - if(IN_COMPUTE_REGION || parloop_by_handler) /*ACC*/ + if(IN_COMPUTE_REGION) /*ACC*/ // generating call statement: // call dvmh_handle_consistent(ConsistGroupRef) doCallAfter(HandleConsistent(consgref)); @@ -2227,6 +2227,8 @@ SgExpression *MappingList(SgStatement *stmt, SgExpression *aref) (el = new SgExprListExp(*e))->setRhs(arglist); arglist = el; } + (el = new SgExprListExp(*ConstRef(nt)))->setRhs(arglist); // add rank to axis list + arglist = el; return arglist; } diff --git a/dvm/fdvm/trunk/include/aks_structs.h b/dvm/fdvm/trunk/include/aks_structs.h index 83ea4c3..65f96c2 100644 --- a/dvm/fdvm/trunk/include/aks_structs.h +++ b/dvm/fdvm/trunk/include/aks_structs.h @@ -1,80 +1,65 @@ #pragma once #include "acc_data.h" - -struct SageStOp -{ - SgForStmt *loop_op; - SgStatement *comment_op; - SageStOp *inner_loops; - SageStOp *next; - int count_inner_loops; - int line_code; - int numChList1; - int numChList2; - int depth; - int LoopNest; -}; struct SageSymbols { - SgSymbol *symb; - int len; - SageSymbols *next; - int across_left; - int across_right; + SageSymbols() + { + across_left = across_right = 0; + len = -1; + symb = NULL; + } + + SageSymbols(SgSymbol* symb, int len, int across_left, int across_right) : + symb(symb), len(len), across_left(across_left), across_right(across_right) + { } + + SgSymbol *symb; + int len; + int across_left; + int across_right; }; struct SageArrayIdxs { - SageSymbols **symb; - int dim; - int read_write; - SgExpression *array_expr; - SageArrayIdxs *next; -}; - -struct Templates -{ - SageSymbols *first; - int count; - int read_write; - int count_write_read; - Templates *next; + std::vector symb; + int dim; + int read_write; + SgExpression *array_expr; }; struct SageAcrossInfo { - SageStOp *Op; - SageArrayIdxs *idx; - SageAcrossInfo *next; + std::vector idxs; }; struct ArgsForKernel { - SgStatement *st_header; - std::list symb; - std::list nSymb; - std::list sizeVars; - std::list acrossS; - std::list notAcrS; - std::list idxAcross; - std::list idxNotAcross; - std::vector otherVars; - std::vector arrayNames; - std::vector otherVarsForOneTh; - std::vector baseIdxsInKer; - SgSymbol *cond_; - std::vector steps; + SgStatement *st_header; + std::vector symb; + std::vector nSymb; + std::vector sizeVars; + std::vector acrossS; + std::vector notAcrossS; + std::vector idxAcross; + std::vector idxNotAcross; + + std::vector otherVars; + std::vector arrayNames; + std::vector otherVarsForOneTh; + std::vector baseIdxsInKer; + SgSymbol *cond_; + std::vector steps; }; /*struct GetXYInfo { - std::vector AllExp; - SgSymbol *varName; - char *arrayName; - long type; - int placeF; - int placeS; + std::vector AllExp; + SgSymbol *varName; + char *arrayName; + long type; + int placeF; + int placeS; };*/ @@ -91,133 +76,132 @@ void getIdxs(char*, int&, int&); struct ParamsForAllVariants { - SgSymbol *s_adapter; - SgSymbol *s_kernel_symb; - int loopV; - int acrossV; - int allDims; - SageSymbols **loopSymb; - SageSymbols **loopAcrossSymb; - char *nameOfNewSAdapter; - char *nameOfNewKernelSymb; - int type; + SgSymbol *s_adapter; + SgSymbol *s_kernel_symb; + int loopV; + int acrossV; + int allDims; + std::vector loopSymb; + std::vector loopAcrossSymb; + char *nameOfNewSAdapter; + char *nameOfNewKernelSymb; + int type; }; struct Bound { - int L; - int R; - bool exL; - bool exR; - bool ifDdot; - SgExpression *additionalExpr; + int L; + int R; + bool exL; + bool exR; + bool ifDdot; + SgExpression *additionalExpr; }; struct BestPattern { - std::vector what; - std::vector bounds; - SgExpression *bestPatt; - int count_of_pattern; + std::vector what; + std::vector bounds; + SgExpression *bestPatt; + int count_of_pattern; }; struct Pattern { - int count_read_op; - int count_write_op; - SgExpression *symbs; + int count_read_op; + int count_write_op; + SgExpression *symbs; }; struct AnalyzeStat { - SgSymbol *replaceSymbol; - int ifHasDim; - SgSymbol *name_of_array; - SgExpression *ex_name_of_array; - std::vector patterns; + SgSymbol *replaceSymbol; + int ifHasDim; + SgSymbol *name_of_array; + SgExpression *ex_name_of_array; + std::vector patterns; }; // struct acrossInfo { - char *nameOfArray; - SgSymbol *symbol; - int allDim; - int acrossPos; - int widthL; - int widthR; - int acrossNum; - std::vector dims; - std::vector symbs; + char *nameOfArray; + SgSymbol *symbol; + int allDim; + int acrossPos; + int widthL; + int widthR; + int acrossNum; + std::vector dims; + std::vector symbs; }; struct newInfo { - SgSymbol *newArray; - std::vector dimSize; - std::vector loadsBeforePlus; - std::vector loadsInForPlus; - std::vector loadsBeforeMinus; - std::vector loadsInForMinus; - std::vector stores; - std::vector swapsDown; - std::vector swapsUp; + SgSymbol *newArray; + std::vector dimSize; + std::vector loadsBeforePlus; + std::vector loadsInForPlus; + std::vector loadsBeforeMinus; + std::vector loadsInForMinus; + std::vector stores; + std::vector swapsDown; + std::vector swapsUp; }; // end // block struct Group { - char *strOfmain; // - SgExpression *mainPattern; - std::vector inGroup; - std::vector len; - std::vector sortLen; - newInfo replaceInfo; // replace info with all needed loads and swaps for optimization + char *strOfmain; // + SgExpression *mainPattern; + std::vector inGroup; + std::vector len; + std::vector sortLen; + newInfo replaceInfo; // replace info with all needed loads and swaps for optimization }; struct PositionGroup { - std::map tableReplace; // table of mapping new private variables to distributed arrays for replacing in loop body - std::map tableNewVars; // table of new private variables that is needed to add in cuda kernel - int position; // position of fixed variable in distributed loop, index 0 corresponds to the first variable. - SgExpression *idxInPos; // - std::vector allPosGr; // all groups of array access patterns with fixed loop variables, which is distributed + std::map tableReplace; // table of mapping new private variables to distributed arrays for replacing in loop body + std::map tableNewVars; // table of new private variables that is needed to add in cuda kernel + int position; // position of fixed variable in distributed loop, index 0 corresponds to the first variable. + SgExpression *idxInPos; // + std::vector allPosGr; // all groups of array access patterns with fixed loop variables, which is distributed }; struct ArrayGroup { - SgSymbol *arrayName; // name of distribute array - std::vector allGroups; // all groups, where one loop variable is fixed + SgSymbol *arrayName; // name of distribute array + std::vector allGroups; // all groups, where one loop variable is fixed }; // end of block struct LoopInfo { - std::vector loopSymbols; - std::vector lowBounds; - std::vector highBounds; - std::vector steps; - int lineNumber; + std::vector loopSymbols; + std::vector lowBounds; + std::vector highBounds; + std::vector steps; + int lineNumber; }; struct ArrayIntents { - std::vector arrayList; - std::vector intent; + std::vector arrayList; + std::vector intent; }; struct AnalyzeReturnGpuO1 { - std::vector allStat; - std::vector bestPatterns; - std::vector allArrayGroup; + std::vector allStat; + std::vector bestPatterns; + std::vector allArrayGroup; }; // functions SgExpression* findDirect(SgExpression*, int); -//SageArrayIdxs* GetIdxInParDir(SgExpression*, SgExpression*); -SageAcrossInfo* GetLoopsWithParAndAcrDir(); -SageSymbols *GetSymbInParalell(int*, SgExpression*); +SageAcrossInfo GetLoopsWithParAndAcrDir(); +std::vector GetSymbInParalell(SgExpression*); int GetIdxPlaceInParDir(SageSymbols*, SgSymbol*); diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index e80bf3f..125f73b 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -321,7 +321,8 @@ static string unparseProjectIfNeed(SgFile* file, const int curr_regime, const bo //TODO: add freeForm for each file if (curr_regime == INSERT_INCLUDES && filesToInclude.find(file_name) != filesToInclude.end()) { - unparseToBuf = removeIncludeStatsAndUnparse(file, file_name, fout_name.c_str(), allIncludeFiles, out_free_form == 1, moduleUsesByFile, moduleDecls, getObjectForFileFromMap(file_name, exctactedModuleStats), toString, true); //, + unparseToBuf = removeIncludeStatsAndUnparse(file, file_name, fout_name.c_str(), allIncludeFiles, out_free_form == 1, moduleUsesByFile, + moduleDecls, getObjectForFileFromMap(file_name, exctactedModuleStats), toString, true); auto itI = filesToInclude.find(file_name); for (auto& incl : itI->second) if (allIncludeFiles.find(incl) != allIncludeFiles.end()) @@ -329,7 +330,8 @@ static string unparseProjectIfNeed(SgFile* file, const int curr_regime, const bo } else { - unparseToBuf = removeIncludeStatsAndUnparse(file, file_name, fout_name.c_str(), allIncludeFiles, out_free_form == 1, moduleUsesByFile, moduleDecls, getObjectForFileFromMap(file_name, exctactedModuleStats), toString); + unparseToBuf = removeIncludeStatsAndUnparse(file, file_name, fout_name.c_str(), allIncludeFiles, out_free_form == 1, moduleUsesByFile, + moduleDecls, getObjectForFileFromMap(file_name, exctactedModuleStats), toString); // copy includes that have not changed if (folderName != NULL) @@ -995,6 +997,10 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne fileIt->second.insert(first->fileName()); } } + + if (inlcudeAllFiles) + if (fileIt->second.size()) + filesToInclude[file_name] = fileIt->second; } else if (curr_regime == REMOVE_AND_CALC_SHADOW) { @@ -2825,6 +2831,8 @@ int main(int argc, char **argv) debSh = 1; else if (string(curr_arg) == "-noLogo") noLogo = true; + else if (string(curr_arg) == "-includeAll") + inlcudeAllFiles = true; break; default: break; diff --git a/sapfor/experts/Sapfor_2017/_src/SapforData.h b/sapfor/experts/Sapfor_2017/_src/SapforData.h index 89b59a3..a94134e 100644 --- a/sapfor/experts/Sapfor_2017/_src/SapforData.h +++ b/sapfor/experts/Sapfor_2017/_src/SapforData.h @@ -46,6 +46,7 @@ bool ignoreArrayDistributeState = false; bool fullDepGraph = false; bool noLogo = false; bool withTemplateInfo = false; +bool inlcudeAllFiles = false; // for pass INSERT_INLCUDES uint64_t currentAvailMemory = 0; int QUALITY; // quality of conflicts search in graph @@ -82,7 +83,7 @@ std::map>> commentsToInclude; // //for INSERT_INCLUDES -std::map> filesToInclude; +std::map> filesToInclude; // file -> includes // //for PASSES DEPENDENSIES diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index d049651..4e2e3d5 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 "2256" +#define VERSION_SPF "2257" From 41fdfcf3f8a9e95b5b5642ffb43e72c971a19d43 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Mon, 18 Dec 2023 21:12:27 +0300 Subject: [PATCH 03/40] checkpoints refactored --- .../_src/Transformations/checkpoints.cpp | 1200 +++++++++-------- 1 file changed, 602 insertions(+), 598 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp index 468d224..9da8cbc 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp @@ -497,631 +497,231 @@ static void replaceExprByExprInSt(SgStatement* st, SgExpression* from, SgExpress } } - -void createCheckpoints(SgFile *file, const map& commonBlocks, const map& filesInfo, - const vector& allFuncInfo) +static SgStatement* createOpenJ_old(SgExpression* iostat, SgArrayRefExp* journal, SgExpression& unit) { - map inFileCp; - bool inFile = findSpfCpDir(file, inFileCp); + vector listSpec; + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("status"), *new SgValueExp("old"))); + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *journal)); + listSpec.push_back(&unit); - if (inFile) + SgIOControlStmt* openJ_old = new SgIOControlStmt(OPEN_STAT, *makeExprList(listSpec, false)); + return openJ_old; +} + +static SgStatement* createOpen(SgExpression* iostat, SgSymbol* files, SgExpression* fileIdx, SgExpression& unit) +{ + vector listSpec; + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("form"), *new SgValueExp("unformatted"))); + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("status"), *new SgValueExp("old"))); + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *new SgArrayRefExp(*files, *fileIdx))); + listSpec.push_back(&unit); + + SgIOControlStmt* open = new SgIOControlStmt(OPEN_STAT, *makeExprList(listSpec, false)); + return open; +} + +static void processFunctionCallChain(SgStatement* func, const vector& allFuncInfo, SgStatement* moduleF, + const vector& loadS, SgExpression* iostat, SgArrayRefExp* journal, + SgExpression& frmt, SgExpression& unit, + SgSymbol* files, SgExpression* fileIdx, + const int every, const vector& everyS) +{ + //find function structure + FuncInfo* funcI = NULL; + for (auto& funcs : allFuncInfo) + if (funcs->funcName == func->symbol()->identifier()) + funcI = funcs; + checkNull(funcI, convertFileName(__FILE__).c_str(), __LINE__); + + set> toProcess; + fillToProcess(funcI, toProcess); + + map processedFrom; + for (auto& pairs : toProcess) { - set commonVars; - for (auto& common : commonBlocks) + FuncInfo* funcTo = pairs.first; + FuncInfo* funcFrom = pairs.second; + + int callNum = 1; + SgStatement* hedrTo = funcTo->funcPointer->GetOriginal(); + SgStatement* hedrFrom = funcFrom->funcPointer->GetOriginal(); + SgStatement* lastDecl = hedrFrom->lexNext(); + while (lastDecl && !isSgExecutableStatement(lastDecl->lexNext())) + lastDecl = lastDecl->lexNext(); + + SgStatement* firstExec = lastDecl->lexNext(); + vector local; + map localParams; + set addedToList; + findLocalData(hedrFrom->lexNext(), firstExec, local, localParams, addedToList); + + if (!processedFrom.count(funcFrom)) { - for (auto& elem : common.second->getVariables()) + SgSymbol* modS = moduleF->symbol(); + SgStatement* useSt = new SgStatement(USE_STMT); + useSt->setSymbol(*modS); + hedrFrom->insertStmtAfter(*useSt, *hedrFrom); + + SgStatement* gotoBlock = new SgStatement(IF_NODE); + vector insertToGotoBlock; + + gotoBlock->addComment("!GOTO LOAD BLOCK \n"); + gotoBlock->setExpression(0, *new SgVarRefExp(loadS[0]) == *new SgValueExp(0)); + + if (funcFrom->isMain) { - if (elem->getType() != CONST) - commonVars.insert(elem->getName()); - } - } - - const vector moduleNames = findAllModules(); - - SgStatement* moduleF = NULL; - const string cpModule = "spf_module_checkpoint"; - SgSymbol* mainModuleCpS = new SgSymbol(MODULE_NAME, cpModule.c_str()); - moduleF = new SgStatement(MODULE_STMT, NULL, mainModuleCpS); - SgStatement* global = current_file->firstStatement(); - global->insertStmtAfter(*moduleF, *global); - - for (auto& checkps : inFileCp) - { - const int line = checkps.first; - const int every = checkps.second.every; - const int numOfFiles = checkps.second.numOfFiles; - const int unitNum = checkps.second.unitNum; - const string additional = string("_") + to_string(current_file_id) + "_" + to_string(line); - const typeEvery type = checkps.second.type; - SgStatement* lastDecl = NULL; - - int numF = file->numberOfFunctions(); - SgStatement* func = NULL; - SgStatement* point = NULL; - for (int z = 0; z < numF; ++z) - { - func = file->functions(z); - point = func->lexNext(); - while (point && point->lineNumber() < line && point != func->lastNodeOfStmt() && point->variant() != CONTAINS_STMT) - point = point->lexNext(); - - if (point != func->lastNodeOfStmt()) - break; - point = NULL; - func = NULL; - } - checkNull(func, convertFileName(__FILE__).c_str(), __LINE__); - checkNull(point, convertFileName(__FILE__).c_str(), __LINE__); - - if (func) - { - const int labNum = getNextFreeLabel(); - - bool createdModuleForIO = createForIOs(filesInfo, func); - - lastDecl = func->lexNext(); - while (lastDecl && !isSgExecutableStatement(lastDecl->lexNext())) - lastDecl = lastDecl->lexNext(); - - checkNull(lastDecl, convertFileName(__FILE__).c_str(), __LINE__); - SgStatement* firstExec = lastDecl->lexNext(); - - vector local; - map localParams; - set addedToList; - findLocalData(func->lexNext(), lastDecl, local, localParams, addedToList); - const vector useOfMods = findUseOfModules(func->lexNext(), lastDecl); - - SgStatement* loadBlock = new SgStatement(IF_NODE); - SgStatement* storeBlock = new SgStatement(IF_NODE); - - point->insertStmtBefore(*loadBlock, *point->controlParent()); - point->insertStmtBefore(*storeBlock, *point->controlParent()); - - loadBlock->addComment("! LOAD CHECKPOINT\n"); - auto loadblockLab = new SgLabel(labNum); - loadBlock->setLabel(*loadblockLab); - - string filesS = "spf_cp_files" + additional; - SgSymbol* files = new SgSymbol(VARIABLE_NAME, filesS.c_str(), createArrayCharType(32, numOfFiles + 1), func); - SgArrayRefExp* journal = new SgArrayRefExp(*files, *new SgValueExp(numOfFiles + 1)); - - //give max len, dont insert - int maxFileLen = insertInitNamesOfFiles(numOfFiles, additional, files, journal, NULL); - - vector everyS; - vector profS; - vector initS; - - string profSs = "spf_cp_prof_s" + additional; - string profEs = "spf_cp_prof_e" + additional; - profS.push_back(new SgSymbol(VARIABLE_NAME, profSs.c_str(), SgTypeFloat(), func)); - profS.push_back(new SgSymbol(VARIABLE_NAME, profEs.c_str(), SgTypeFloat(), func)); - - SgSymbol* timeF = new SgSymbol(FUNCTION_NAME, "omp_get_wtime", SgTypeDouble(),func); // OR dvtime - - SgStatement* profCallS = new SgAssignStmt(*new SgVarRefExp(profS[0]), *new SgFunctionCallExp(*timeF)); - SgStatement* profCallE = new SgAssignStmt(*new SgVarRefExp(profS[1]), *new SgFunctionCallExp(*timeF)); - - if (type == typeEvery::TIME) - { - string everySs = "spf_cp_start" + additional; - string everyEs = "spf_cp_end" + additional; - everyS.push_back(new SgSymbol(VARIABLE_NAME, everySs.c_str(), SgTypeFloat(), func)); - everyS.push_back(new SgSymbol(VARIABLE_NAME, everyEs.c_str(), SgTypeFloat(), func)); - - initS.push_back(NULL); - initS.push_back(NULL); - - SgStatement* call = new SgAssignStmt(*new SgVarRefExp(everyS[0]), *new SgFunctionCallExp(*timeF)); - lastDecl->insertStmtAfter(*call, *func); - lastDecl->insertStmtAfter(*new SgStatement(DVM_BARRIER_DIR), *func); - - storeBlock->insertStmtAfter(call->copy(), *storeBlock); - storeBlock->insertStmtAfter(*new SgStatement(DVM_BARRIER_DIR), *storeBlock); - - call = new SgAssignStmt(*new SgVarRefExp(everyS[1]), *new SgFunctionCallExp(*timeF)); - storeBlock->insertStmtBefore(*new SgStatement(DVM_BARRIER_DIR), *storeBlock->controlParent()); - storeBlock->insertStmtBefore(*call, *storeBlock->controlParent()); - call->lexPrev()->addComment("! STORE CHECKPOINT\n"); - - storeBlock->setExpression(0, *new SgVarRefExp(everyS[1]) - *new SgVarRefExp(everyS[0]) >= *new SgValueExp(every)); - } - else if (type == typeEvery::ITER) - { - string everyIs = "spf_cp_interval" + additional; - everyS.push_back(new SgSymbol(VARIABLE_NAME, everyIs.c_str(), SgTypeInt(), func)); - initS.push_back(new SgValueExp(0)); - SgAssignStmt* init = new SgAssignStmt(*new SgVarRefExp(everyS[0]), *new SgValueExp(0)); - storeBlock->insertStmtAfter(*init, *storeBlock); - - SgAssignStmt* inc = new SgAssignStmt(*new SgVarRefExp(everyS[0]), *new SgVarRefExp(everyS[0]) + *new SgValueExp(1)); - storeBlock->insertStmtBefore(*inc, *storeBlock->controlParent()); - inc->addComment("! STORE CHECKPOINT\n"); - - storeBlock->setExpression(0, *new SgVarRefExp(everyS[0]) >= *new SgValueExp(every)); - } - - vector loadS; - vector initLoadS; - vector insertToLoadS; - - string cpLoadS = "spf_cp_load" + additional; - loadS.push_back(new SgSymbol(VARIABLE_NAME, cpLoadS.c_str(), SgTypeInt(), func)); - initLoadS.push_back(new SgValueExp(0)); - - string fileNS = "spf_cp_file_n" + additional; - loadS.push_back(new SgSymbol(VARIABLE_NAME, fileNS.c_str(), SgTypeInt(), func)); - initLoadS.push_back(new SgValueExp(1)); - - string iostatS = "spf_cp_iostat" + additional; - loadS.push_back(new SgSymbol(VARIABLE_NAME, iostatS.c_str(), SgTypeInt(), func)); - initLoadS.push_back(NULL); - - /*string loadSaveS = "spf_cp_load_save" + additional; - loadS.push_back(new SgSymbol(VARIABLE_NAME, loadSaveS.c_str(), SgTypeInt(), func)); - initLoadS.push_back(new SgValueExp(0));*/ - - string loadLabelS = "spf_cp_load_label" + additional; - loadS.push_back(new SgSymbol(VARIABLE_NAME, loadLabelS.c_str(), SgTypeInt(), func)); - initLoadS.push_back(new SgValueExp(0)); - - string saveFS = "spf_cp_save_flag" + additional; - loadS.push_back(new SgSymbol(VARIABLE_NAME, saveFS.c_str(), SgTypeInt(), func)); - initLoadS.push_back(new SgValueExp(0)); - - loadBlock->setExpression(0, *new SgVarRefExp(loadS[0]) == *new SgValueExp(0)); - SgAssignStmt* init = new SgAssignStmt(*new SgVarRefExp(loadS[0]), *new SgValueExp(1)); - insertToLoadS.push_back(init); - - vector listSpec; - - SgExpression& unitNull = SgAssignOp(*new SgKeywordValExp("unit"), *new SgKeywordValExp("*")); - SgExpression& unit = SgAssignOp(*new SgKeywordValExp("unit"), *new SgValueExp(unitNum)); - SgExpression& frmt = SgAssignOp(*new SgKeywordValExp("fmt"), *new SgKeywordValExp("*")); - SgExpression& frmtProf = SgAssignOp(*new SgKeywordValExp("fmt"), *new SgValueExp(("(A,A" + to_string(maxFileLen) + ",F4.2,A)").c_str())); - - SgExpression* iostat = new SgVarRefExp(loadS[2]); - SgExpression* fileIdx = new SgVarRefExp(loadS[1]); - - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("status"), *new SgValueExp("old"))); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *journal)); - listSpec.push_back(&unit); - - SgIOControlStmt* openJ_old = new SgIOControlStmt(OPEN_STAT, *makeExprList(listSpec, false)); - insertToLoadS.push_back(openJ_old); + SgAssignStmt* loadOne = new SgAssignStmt(*new SgVarRefExp(loadS[0]), *new SgValueExp(1)); + insertToGotoBlock.push_back(loadOne); + insertToGotoBlock.push_back(createOpenJ_old(iostat, journal, unit)); vector insertToifLoadOk; SgIfStmt* ifLoadOk = new SgIfStmt(*iostat == *new SgValueExp(0)); - insertToLoadS.push_back(ifLoadOk); + insertToGotoBlock.push_back(ifLoadOk); SgInputOutputStmt* read = new SgInputOutputStmt(READ_STAT, *makeExprList({ &frmt, &unit }, false), *fileIdx); insertToifLoadOk.push_back(read); insertToifLoadOk.push_back(new SgIOControlStmt(CLOSE_STAT, unit)); + insertToifLoadOk.push_back(createOpen(iostat, files, fileIdx, unit)); - listSpec.clear(); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("form"), *new SgValueExp("unformatted"))); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("status"), *new SgValueExp("old"))); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *new SgArrayRefExp(*files, *fileIdx))); - listSpec.push_back(&unit); - - SgIOControlStmt* open = new SgIOControlStmt(OPEN_STAT, *makeExprList(listSpec, false)); - insertToifLoadOk.push_back(open); - - SgIfStmt* ifLoadOk1 = new SgIfStmt(*iostat == *new SgValueExp(0), *new SgIOControlStmt(CLOSE_STAT, unit)); - + read = new SgInputOutputStmt(READ_STAT, *makeExprList({ &frmt, &unit }, false), *new SgVarRefExp(loadS[3])); + SgIfStmt* ifLoadOk1 = new SgIfStmt(*iostat == *new SgValueExp(0), *read); + SgAssignStmt* loadZero = new SgAssignStmt(*new SgVarRefExp(loadS[0]), *new SgValueExp(0)); + ifLoadOk1->insertStmtAfter(*loadZero, *ifLoadOk1); insertToifLoadOk.push_back(ifLoadOk1); - ifLoadOk1->insertStmtAfter(*new SgIfStmt(*fileIdx == *new SgValueExp(numOfFiles + 1), *new SgAssignStmt(*fileIdx, *new SgValueExp(1))), *ifLoadOk1); - ifLoadOk1->insertStmtAfter(*new SgAssignStmt(*fileIdx, *fileIdx + *new SgValueExp(1)), *ifLoadOk1); - ifLoadOk1->addComment("! LOAD DATA FROM CHECKPOINT\n"); - vector commentArgs; - commentArgs.push_back(new SgValueExp(" SECONDS")); - commentArgs.push_back(&(*new SgVarRefExp(profS[1]) - *new SgVarRefExp(profS[0]))); - commentArgs.push_back(new SgArrayRefExp(*files, *fileIdx)); - commentArgs.push_back(new SgValueExp("SPF CHECKPOINT LOADED FROM ")); + firstExec->insertStmtBefore(*gotoBlock, *hedrFrom); - ifLoadOk1->insertStmtAfter(*new SgInputOutputStmt(WRITE_STAT, *makeExprList({ &frmtProf, &unitNull }, false), *makeExprList(commentArgs, false)), *ifLoadOk1); - ifLoadOk1->insertStmtAfter(profCallE->copy(), *ifLoadOk1); + for (int z = insertToGotoBlock.size() - 1; z >= 0; --z) + gotoBlock->insertStmtAfter(*insertToGotoBlock[z], *gotoBlock); - //open all files - if (createdModuleForIO) + for (int z = insertToifLoadOk.size() - 1; z >= 0; --z) + ifLoadOk->insertStmtAfter(*insertToifLoadOk[z], *ifLoadOk); + + processedFrom[funcFrom] = ifLoadOk1->lexNext(); + } + else + { + SgStatement* read = new SgInputOutputStmt(READ_STAT, *makeExprList({ &frmt, &unit }, false), *new SgVarRefExp(loadS[3])); + gotoBlock->insertStmtAfter(*read, *gotoBlock); + processedFrom[funcFrom] = gotoBlock->lexNext(); + firstExec->insertStmtBefore(*gotoBlock, *hedrFrom); + } + } + + for (auto& callInfo : funcFrom->callsFromDetailed) + { + auto& call = callInfo.pointerDetailCallsFrom; + SgStatement* st = NULL; + + if (isSgFuncHedrStmt(hedrTo) && call.second == FUNC_CALL) + { + SgFunctionCallExp* callExp = (SgFunctionCallExp*)call.first; + if (!strcmp(callExp->funName()->identifier(), funcTo->funcName.c_str())) { - SgCallStmt* call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, iosNames[1].c_str())); - ifLoadOk1->insertStmtAfter(*call, *ifLoadOk1); - } + st = SgStatement::getStatmentByExpression(callExp); + SgSymbol* buf = new SgSymbol(VARIABLE_NAME, ("SPF_CALL_" + funcTo->funcName + "_" + to_string(callNum)).c_str(), callExp->type(), funcFrom->funcPointer->GetOriginal()); + SgExpression* bufRef = new SgVarRefExp(buf); + SgStatement* bufAssign = new SgAssignStmt(*bufRef, *callExp); + st->insertStmtBefore(*bufAssign, *(st->controlParent())); + replaceExprByExprInSt(st, callExp, bufRef); + makeDeclaration(hedrFrom, { buf }); - //READ from modules - for (auto& mod : moduleNames) - { - SgCallStmt* call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, ("SPF_CP_" + mod).c_str())); - call->addArg(*new SgValueExp(unitNum)); - call->addArg(*new SgValueExp(0)); - ifLoadOk1->insertStmtAfter(*call, *ifLoadOk1); + for (int i = 0; i < callExp->numberOfArgs(); i++) + { + if (funcTo->funcParams.isArgInOut(i) || funcTo->funcParams.isArgOut(i)) + { + SgSymbol* s, * sl; + SgStatement* lst = hedrTo->lastNodeOfStmt(); + sl = lst->lexNext() ? lst->lexNext()->symbol() : NULL; + for (s = hedrTo->symbol(); s != sl && s; s = s->next()) + { + if (s->scope() == hedrTo && !strcmp(s->identifier(), funcTo->funcParams.identificators[i].c_str())) + { + break; + } + } + + SgSymbol* argBuf = new SgSymbol(VARIABLE_NAME, ("SPF_ARG_" + funcTo->funcName + "_" + to_string(callNum) + "_" + to_string(i)).c_str(), s->type(), funcFrom->funcPointer->GetOriginal()); + SgExpression* argBufRef = new SgVarRefExp(argBuf); + SgStatement* argBufAssign = new SgAssignStmt(*argBufRef, *(callExp->arg(i))); + bufAssign->insertStmtBefore(*argBufAssign, *(st->controlParent())); + SgStatement* decl = makeDeclaration(hedrFrom, { argBuf }); + + for (int i = 0; i < 3; i++) + { + SgExpression* e; + if (e = decl->expr(i)) + decl->setExpression(i, CalculateInteger(ReplaceConstant(e))); + } + + } + } + st = bufAssign; + callNum++; } + } + else if (isSgProcHedrStmt(hedrTo) && call.second == PROC_STAT) + { + //TODO: need to revise!! + /*SgCallStmt* callSt = (SgCallStmt*)call.first; + if (!strcmp(callSt->name()->identifier(), funcTo->funcName.c_str())) { + st = callSt; + for (int i = 0; i < callSt->numberOfArgs(); i++) + { + if (funcTo->funcParams.isArgInOut(i) || funcTo->funcParams.isArgOut(i)) + { + SgSymbol* buf1 = new SgSymbol(VARIABLE_NAME, "TEMP1", callSt->arg(i)->type(), hedrFrom); + SgExpression* buf1Ref = new SgVarRefExp(buf1); + SgStatement* buf1Assign = new SgAssignStmt(*buf1Ref, *(callSt->arg(i))); + st->insertStmtBefore(*buf1Assign, *(st->controlParent())); + } + } + }*/ + } + + if (st) + { + SgStatement* loadBlock = new SgStatement(IF_NODE); + SgStatement* loadBlockLast = NULL; + SgStatement* storeBlock = new SgStatement(IF_NODE); + + st->insertStmtBefore(*loadBlock, *st->controlParent()); + st->insertStmtAfter(*storeBlock, *st->controlParent()); + + loadBlock->addComment("! LOAD CHECKPOINT\n"); + const int labNum = getNextFreeLabel(); + auto loadblockLab = new SgLabel(labNum); + loadBlock->setLabel(*loadblockLab); + + SgIfStmt* gotoIf = new SgIfStmt(*new SgVarRefExp(loadS[3]) == *new SgValueExp(labNum), *new SgGotoStmt(*loadblockLab)); + processedFrom[funcFrom]->insertStmtAfter(*gotoIf, *(processedFrom[funcFrom]->controlParent())); + + storeBlock->addComment("! STORE CHECKPOINT\n"); + + //TODO: this block incorrect!! + storeBlock->setExpression(0, *new SgVarRefExp(everyS[0]) >= *new SgValueExp(every)); + + vector insertToLoadS; + + loadBlock->setExpression(0, *new SgVarRefExp(loadS[0]) == *new SgValueExp(0)); + loadBlock->addComment("! LOAD DATA FROM CHECKPOINT\n"); //READ DATA if (local.size()) { - auto dataRead = new SgInputOutputStmt(READ_STAT, unit, *makeExprList(local, false)); - ifLoadOk1->insertStmtAfter(*dataRead, *ifLoadOk1); - } - - ifLoadOk1->insertStmtAfter(profCallS->copy(), *ifLoadOk1); - - listSpec.clear(); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("form"), *new SgValueExp("unformatted"))); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *new SgArrayRefExp(*files, *fileIdx))); - listSpec.push_back(&unit); - - open = new SgIOControlStmt(OPEN_STAT, *makeExprList(listSpec, false)); - storeBlock->insertStmtAfter(*open, *storeBlock); - - SgIfStmt* ifStoreOk = new SgIfStmt(*iostat == *new SgValueExp(0)); - open->insertStmtAfter(*ifStoreOk, *storeBlock); - ifStoreOk->insertStmtAfter(*new SgIfStmt(*fileIdx == *new SgValueExp(numOfFiles + 1), *new SgAssignStmt(*fileIdx, *new SgValueExp(1))), *ifStoreOk); - ifStoreOk->insertStmtAfter(*new SgAssignStmt(*fileIdx, *fileIdx + *new SgValueExp(1)), *ifStoreOk); - ifStoreOk->addComment(("! STORE DATA TO CHECKPOINT " + to_string(local.size()) + " items\n").c_str()); - - commentArgs.clear(); - commentArgs.push_back(new SgValueExp(" SECONDS")); - commentArgs.push_back(&(*new SgVarRefExp(profS[1]) - *new SgVarRefExp(profS[0]))); - commentArgs.push_back(new SgArrayRefExp(*files, *fileIdx)); - commentArgs.push_back(new SgValueExp("SPF CHECKPOINT STORED TO ")); - - ifStoreOk->insertStmtAfter(*new SgInputOutputStmt(WRITE_STAT, *makeExprList({ &frmtProf, &unitNull }, false), *makeExprList(commentArgs, false)), *ifStoreOk); - ifStoreOk->insertStmtAfter(profCallE->copy(), *ifStoreOk); - - SgStatement* assign = ifStoreOk->lexNext(); - - //open all files - if (createdModuleForIO) - { - SgCallStmt* call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, iosNames[1].c_str())); - ifStoreOk->insertStmtAfter(*call, *ifStoreOk); - } - //WRITE from modules - for (auto& mod : moduleNames) - { - SgCallStmt* call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, ("SPF_CP_" + mod).c_str())); - call->addArg(*new SgValueExp(unitNum)); - call->addArg(*new SgValueExp(1)); - ifStoreOk->insertStmtAfter(*call, *ifStoreOk); - } - - //close and inc - if (createdModuleForIO) - { - SgCallStmt* call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, iosNames[2].c_str())); - ifStoreOk->insertStmtAfter(*call, *ifStoreOk); - call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, iosNames[0].c_str())); - ifStoreOk->insertStmtAfter(*call, *ifStoreOk); - } - - //WRITE DATA - if (local.size()) - { - SgStatement* dataWrite = new SgInputOutputStmt(WRITE_STAT, unit, *makeExprList(local, false)); - ifStoreOk->insertStmtAfter(*dataWrite, *ifStoreOk); - } - - listSpec.clear(); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *journal)); - listSpec.push_back(&unit); - - SgIOControlStmt* openJ_new = new SgIOControlStmt(OPEN_STAT, *makeExprList(listSpec, false)); - - assign->insertStmtBefore(*new SgIOControlStmt(CLOSE_STAT, unit), *ifStoreOk); - assign->insertStmtBefore(*openJ_new, *ifStoreOk); - assign->insertStmtBefore(*new SgInputOutputStmt(WRITE_STAT, *makeExprList({ &frmt, &unit }, false), *fileIdx), *ifStoreOk); - assign->insertStmtBefore(*new SgIOControlStmt(CLOSE_STAT, unit), *ifStoreOk); - - ifStoreOk->insertStmtAfter(profCallS->copy(), *ifStoreOk); - - SgStatement* copyForGoto = loadBlock->copyPtr(); - copyForGoto->deleteLabel(); - firstExec->insertStmtBefore(*copyForGoto, *func); - insertInitNamesOfFiles(numOfFiles, additional, files, journal, copyForGoto, true); - - copyForGoto->insertStmtAfter(insertToLoadS[insertToLoadS.size() - 1]->copy(), *copyForGoto); - copyForGoto->insertStmtAfter(insertToLoadS[insertToLoadS.size() - 2]->copy(), *copyForGoto); - - copyForGoto = copyForGoto->lexNext()->lexNext(); - - copyForGoto->insertStmtAfter(*new SgGotoStmt(*loadblockLab), *copyForGoto); - copyForGoto->insertStmtAfter(*new SgIOControlStmt(CLOSE_STAT, unit), *copyForGoto); - - for (int z = insertToLoadS.size() - 1; z >= 0; --z) - loadBlock->insertStmtAfter(*insertToLoadS[z], *loadBlock); - - for (int z = insertToifLoadOk.size() - 1; z >= 0; --z) - ifLoadOk->insertStmtAfter(*insertToifLoadOk[z], *ifLoadOk); - - - //TODO: - /*set elemNotDeclHere; - for (auto& elem : commonVars) - { - if (addedToList.find(elem) == addedToList.end()) - elemNotDeclHere.insert(elem); - } - for (auto& elem : elemNotDeclHere) - printf("%s\n", elem.c_str());*/ - - // make all new declarations - makeDeclaration(moduleF, everyS, &initS); - makeDeclaration(moduleF, loadS, &initLoadS); - makeDeclaration(moduleF, profS); - makeDeclaration(moduleF, { files }); - makeDeclaration(func, { timeF }); - - func->insertStmtAfter(*new SgStatement(USE_STMT, NULL, mainModuleCpS), *func); - - //check use - map modulesDone; - for (auto& elem : moduleNames) - modulesDone[elem] = false; - - //check with ONLY - for (auto& use : useOfMods) - { - const string modName = use->symbol()->identifier(); - if (!modulesDone[modName]) - { - if (use->expr(0)) // has ONLY - { - SgExpression* ex = use->expr(0); - if (ex && ex->variant() == ONLY_NODE) - { - modulesDone[modName] = true; - auto callName = new SgSymbol(VARIABLE_NAME, ("SPF_CP_" + modName).c_str()); - ex->setLhs(new SgExpression(EXPR_LIST, new SgVarRefExp(callName), ex->lhs())); - } - } - } - } - - //check other - for (auto& use : useOfMods) - { - const string modName = use->symbol()->identifier(); - if (!modulesDone[modName]) - modulesDone[modName] = true; - } - - for (auto& elem : modulesDone) - { - if (!elem.second) - { - auto callName = new SgSymbol(VARIABLE_NAME, ("SPF_CP_" + elem.first).c_str()); - SgSymbol* modS = new SgSymbol(VARIABLE_NAME, elem.first.c_str()); - - vector onlyList; - onlyList.push_back(new SgVarRefExp(callName)); - if (elem.first == "spf_module_ios") - for (int z = 0; z < 3; ++z) - onlyList.push_back(new SgVarRefExp(new SgSymbol(VARIABLE_NAME, iosNames[z].c_str()))); - - SgExpression* only = new SgExpression(ONLY_NODE, makeExprList(onlyList), NULL); - SgStatement* use = new SgStatement(USE_STMT, NULL, modS, only, NULL, NULL); - func->insertStmtAfter(*use, *func); - } - } - - FuncInfo* funcI = NULL; - for (auto& funcs : allFuncInfo) - if (funcs->funcName == func->symbol()->identifier()) - funcI = funcs; - - if (funcI == NULL) - checkNull(funcI, convertFileName(__FILE__).c_str(), __LINE__); - - set> toProcess; - fillToProcess(funcI, toProcess); - - map processedFrom; - for (auto j = toProcess.begin(); j != toProcess.end(); j++) - { - int callNum = 1; - SgStatement* hedrTo = (j->first)->funcPointer->GetOriginal(); - SgStatement* hedrFrom = (j->second)->funcPointer->GetOriginal(); - SgStatement* lastDecl = hedrFrom->lexNext(); - while (lastDecl && !isSgExecutableStatement(lastDecl->lexNext())) - lastDecl = lastDecl->lexNext(); - - SgStatement* firstExec = lastDecl->lexNext(); - vector local; - map localParams; - set addedToList; - findLocalData(hedrFrom->lexNext(), lastDecl, local, localParams, addedToList); - if (!processedFrom.count(j->second)) - { - SgSymbol* modS = moduleF->symbol(); - SgStatement* useSt = new SgStatement(USE_STMT); - useSt->setSymbol(*modS); - hedrFrom->insertStmtAfter(*useSt, *hedrFrom); - - SgStatement* gotoBlock = new SgStatement(IF_NODE); - vector insertToGotoBlock; - - gotoBlock->addComment("!GOTO LOAD BLOCK \n"); - gotoBlock->setExpression(0, *new SgVarRefExp(loadS[0]) == *new SgValueExp(0)); - - if (j->second->isMain) - { - SgAssignStmt* loadOne = new SgAssignStmt(*new SgVarRefExp(loadS[0]), *new SgValueExp(1)); - insertToGotoBlock.push_back(loadOne); - - listSpec.clear(); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("status"), *new SgValueExp("old"))); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *journal)); - listSpec.push_back(&unit); - - SgIOControlStmt* openJ_old = new SgIOControlStmt(OPEN_STAT, *makeExprList(listSpec, false)); - insertToGotoBlock.push_back(openJ_old); - - vector insertToifLoadOk; - SgIfStmt* ifLoadOk = new SgIfStmt(*iostat == *new SgValueExp(0)); - insertToGotoBlock.push_back(ifLoadOk); - - SgInputOutputStmt* read = new SgInputOutputStmt(READ_STAT, *makeExprList({ &frmt, &unit }, false), *fileIdx); - insertToifLoadOk.push_back(read); - insertToifLoadOk.push_back(new SgIOControlStmt(CLOSE_STAT, unit)); - - listSpec.clear(); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("form"), *new SgValueExp("unformatted"))); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("status"), *new SgValueExp("old"))); - listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *new SgArrayRefExp(*files, *fileIdx))); - listSpec.push_back(&unit); - - SgIOControlStmt* open = new SgIOControlStmt(OPEN_STAT, *makeExprList(listSpec, false)); - insertToifLoadOk.push_back(open); - - read = new SgInputOutputStmt(READ_STAT, *makeExprList({ &frmt, &unit }, false), *new SgVarRefExp(loadS[3])); - SgIfStmt* ifLoadOk1 = new SgIfStmt(*iostat == *new SgValueExp(0), *read); - SgAssignStmt* loadZero = new SgAssignStmt(*new SgVarRefExp(loadS[0]), *new SgValueExp(0)); - ifLoadOk1->insertStmtAfter(*loadZero, *ifLoadOk1); - - insertToifLoadOk.push_back(ifLoadOk1); - - firstExec->insertStmtBefore(*gotoBlock, *hedrFrom); - - for (int z = insertToGotoBlock.size() - 1; z >= 0; --z) - gotoBlock->insertStmtAfter(*insertToGotoBlock[z], *gotoBlock); - - for (int z = insertToifLoadOk.size() - 1; z >= 0; --z) - ifLoadOk->insertStmtAfter(*insertToifLoadOk[z], *ifLoadOk); - - processedFrom[j->second] = ifLoadOk1->lexNext(); - processedFrom[j->second]->unparsestdout(); - } - else - { - SgStatement * read = new SgInputOutputStmt(READ_STAT, *makeExprList({ &frmt, &unit }, false), *new SgVarRefExp(loadS[3])); - gotoBlock->insertStmtAfter(*read, *gotoBlock); - processedFrom[j->second] = gotoBlock->lexNext(); - } - - } - - for (auto& callInfo : j->second->callsFromDetailed) - { - auto& call = callInfo.pointerDetailCallsFrom; - SgStatement* st = NULL; - - if (isSgFuncHedrStmt(hedrTo) && call.second == FUNC_CALL) - { - SgFunctionCallExp* callExp = (SgFunctionCallExp*)call.first; - if (!strcmp(callExp->funName()->identifier(), (j->first)->funcName.c_str())) - { - st = SgStatement::getStatmentByExpression(callExp); - SgSymbol * buf = new SgSymbol(VARIABLE_NAME, ("SPF_CALL_"+ (j->first)->funcName + "_" + to_string(callNum)).c_str(), callExp->type(), (j->second)->funcPointer->GetOriginal()); - SgExpression* bufRef = new SgVarRefExp(buf); - SgStatement* bufAssign = new SgAssignStmt(*bufRef, *callExp); - st->insertStmtBefore(*bufAssign,* (st->controlParent())); - replaceExprByExprInSt(st, callExp, bufRef); - makeDeclaration(hedrFrom, { buf }); - for (int i = 0; i < callExp->numberOfArgs(); i++) - { - if ((j->first)->funcParams.isArgInOut(i) || (j->first)->funcParams.isArgOut(i)) - { - SgSymbol* s, * sl; - SgStatement* lst = hedrTo->lastNodeOfStmt(); - sl = lst->lexNext() ? lst->lexNext()->symbol() : NULL; - for (s = hedrTo->symbol(); s != sl && s; s = s->next()) - { - if (s->scope() == hedrTo && !strcmp(s->identifier(), (j->first)->funcParams.identificators[i].c_str())) - { - break; - } - - } - - SgSymbol* argBuf = new SgSymbol(VARIABLE_NAME, ("SPF_ARG_" + (j->first)->funcName+"_" + to_string(callNum) + "_" + to_string(i)).c_str(), s->type(), (j->second)->funcPointer->GetOriginal()); - SgExpression* argBufRef = new SgVarRefExp(argBuf); - SgStatement* argBufAssign = new SgAssignStmt(*argBufRef, *(callExp->arg(i))); - bufAssign->insertStmtBefore(*argBufAssign, *(st->controlParent())); - SgStatement* decl = makeDeclaration(hedrFrom, {argBuf}); - - for (int i = 0; i < 3; i++) - { - SgExpression* e; - if (e = decl->expr(i)) - decl->setExpression(i, CalculateInteger(ReplaceConstant(e))); - } - - } - - } - st = bufAssign; - callNum++; - } - } - else if (isSgProcHedrStmt(hedrTo) && call.second == PROC_STAT) - { - SgCallStmt* callSt = (SgCallStmt*)call.first; - if (!strcmp(callSt->name()->identifier(), (j->first)->funcName.c_str())) { - st = callSt; - for (int i = 0; i < callSt->numberOfArgs(); i++) - { - if ((j->first)->funcParams.isArgInOut(i) || (j->first)->funcParams.isArgOut(i)) - { - SgSymbol* buf1 = new SgSymbol(VARIABLE_NAME, "TEMP1", callSt->arg(i)->type(), hedrFrom); - SgExpression* buf1Ref = new SgVarRefExp(buf1); - SgStatement* buf1Assign = new SgAssignStmt(*buf1Ref, *(callSt->arg(i))); - st->insertStmtBefore(*buf1Assign, *(st->controlParent())); - } - - } - } - } - if (st) { - SgStatement* loadBlock = new SgStatement(IF_NODE); - SgStatement* loadBlockLast = NULL; - SgStatement* storeBlock = new SgStatement(IF_NODE); - - st->insertStmtBefore(*loadBlock, *st->controlParent()); - st->insertStmtAfter(*storeBlock, *st->controlParent()); - - loadBlock->addComment("! LOAD CHECKPOINT\n"); - const int labNum = getNextFreeLabel(); - auto loadblockLab = new SgLabel(labNum); - loadBlock->setLabel(*loadblockLab); - - SgIfStmt* gotoIf = new SgIfStmt(*new SgVarRefExp(loadS[3]) == *new SgValueExp(labNum), *new SgGotoStmt(*loadblockLab)); - processedFrom[j->second]->insertStmtAfter(*gotoIf,*(processedFrom[j->second]->controlParent())); - - storeBlock->addComment("! STORE CHECKPOINT\n"); - - storeBlock->setExpression(0, *new SgVarRefExp(everyS[0]) >= *new SgValueExp(every)); - - vector insertToLoadS; - - loadBlock->setExpression(0, *new SgVarRefExp(loadS[0]) == *new SgValueExp(0)); - loadBlock->addComment("! LOAD DATA FROM CHECKPOINT\n"); - - //READ DATA - if (local.size()) - { - auto dataRead = new SgInputOutputStmt(READ_STAT, unit, *makeExprList(local)); - loadBlock->insertStmtAfter(*dataRead, *loadBlock); - } - - } - } + auto dataRead = new SgInputOutputStmt(READ_STAT, unit, *makeExprList(local)); + loadBlock->insertStmtAfter(*dataRead, *loadBlock); } } } } - - //process modules +} + +static void processModules(SgFile* file) +{ vector modules; findModulesInFile(file, modules); for (auto& mod : modules) @@ -1149,7 +749,7 @@ void createCheckpoints(SgFile *file, const map& commonBloc newF->AddArg("unit_f", *SgTypeInt()); newF->AddArg("type_of_op", *SgTypeInt()); - + if (local.size()) { SgExpression& unit = SgAssignOp(*new SgKeywordValExp("unit"), *new SgVarRefExp(*newF->parameter(0))); @@ -1161,7 +761,411 @@ void createCheckpoints(SgFile *file, const map& commonBloc ifBlock->addComment(("!STORE OR LOAD " + to_string(local.size()) + " ITEMS\n").c_str()); } - } + } +} + +void createCheckpoints(SgFile* file, const map& commonBlocks, const map& filesInfo, + const vector& allFuncInfo) +{ + map inFileCp; + bool inFile = findSpfCpDir(file, inFileCp); + + if (!inFile) + { + processModules(file); + return; + } + + set commonVars; + for (auto& common : commonBlocks) + for (auto& elem : common.second->getVariables()) + if (elem->getType() != CONST) + commonVars.insert(elem->getName()); + + const vector moduleNames = findAllModules(); + + SgStatement* moduleF = NULL; + const string cpModule = "spf_module_checkpoint"; + SgSymbol* mainModuleCpS = new SgSymbol(MODULE_NAME, cpModule.c_str()); + moduleF = new SgStatement(MODULE_STMT, NULL, mainModuleCpS); + SgStatement* global = current_file->firstStatement(); + global->insertStmtAfter(*moduleF, *global); + + for (auto& checkps : inFileCp) + { + const int cpLine = checkps.first; + const int every = checkps.second.every; + const int numOfFiles = checkps.second.numOfFiles; + const int unitNum = checkps.second.unitNum; + const string additional = string("_") + to_string(current_file_id) + "_" + to_string(cpLine); + const typeEvery type = checkps.second.type; + + const int numF = file->numberOfFunctions(); + SgStatement* func = NULL; + SgStatement* point = NULL; + for (int z = 0; z < numF; ++z) + { + func = file->functions(z); + point = func->lexNext(); + while (point && point->lineNumber() < cpLine && point != func->lastNodeOfStmt() && point->variant() != CONTAINS_STMT) + point = point->lexNext(); + + //cp place was found + if (point != func->lastNodeOfStmt()) + break; + point = NULL; + func = NULL; + } + checkNull(func, convertFileName(__FILE__).c_str(), __LINE__); + checkNull(point, convertFileName(__FILE__).c_str(), __LINE__); + + const int labNum = getNextFreeLabel(); + bool createdModuleForIO = createForIOs(filesInfo, func); + + SgStatement* lastDecl = func->lexNext(); + while (lastDecl && !isSgExecutableStatement(lastDecl->lexNext())) + lastDecl = lastDecl->lexNext(); + + checkNull(lastDecl, convertFileName(__FILE__).c_str(), __LINE__); + SgStatement* firstExec = lastDecl->lexNext(); + + vector local; + map localParams; + set addedToList; + findLocalData(func->lexNext(), firstExec, local, localParams, addedToList); + const vector useOfMods = findUseOfModules(func->lexNext(), firstExec); + + SgStatement* loadBlock = new SgStatement(IF_NODE); + SgStatement* storeBlock = new SgStatement(IF_NODE); + + point->insertStmtBefore(*loadBlock, *point->controlParent()); + point->insertStmtBefore(*storeBlock, *point->controlParent()); + + loadBlock->addComment("! LOAD CHECKPOINT\n"); + auto loadblockLab = new SgLabel(labNum); + loadBlock->setLabel(*loadblockLab); + + string filesS = "spf_cp_files" + additional; + SgSymbol* files = new SgSymbol(VARIABLE_NAME, filesS.c_str(), createArrayCharType(32, numOfFiles + 1), func); + SgArrayRefExp* journal = new SgArrayRefExp(*files, *new SgValueExp(numOfFiles + 1)); + + //give max len, dont insert + int maxFileLen = insertInitNamesOfFiles(numOfFiles, additional, files, journal, NULL); + + vector everyS; + vector profS; + vector initS; + + string profSs = "spf_cp_prof_s" + additional; + string profEs = "spf_cp_prof_e" + additional; + profS.push_back(new SgSymbol(VARIABLE_NAME, profSs.c_str(), SgTypeFloat(), func)); + profS.push_back(new SgSymbol(VARIABLE_NAME, profEs.c_str(), SgTypeFloat(), func)); + + SgSymbol* timeF = new SgSymbol(FUNCTION_NAME, "omp_get_wtime", SgTypeDouble(), func); // OR dvtime + + SgStatement* profCallS = new SgAssignStmt(*new SgVarRefExp(profS[0]), *new SgFunctionCallExp(*timeF)); + SgStatement* profCallE = new SgAssignStmt(*new SgVarRefExp(profS[1]), *new SgFunctionCallExp(*timeF)); + + if (type == typeEvery::TIME) + { + string everySs = "spf_cp_start" + additional; + string everyEs = "spf_cp_end" + additional; + everyS.push_back(new SgSymbol(VARIABLE_NAME, everySs.c_str(), SgTypeFloat(), func)); + everyS.push_back(new SgSymbol(VARIABLE_NAME, everyEs.c_str(), SgTypeFloat(), func)); + + initS.push_back(NULL); + initS.push_back(NULL); + + SgStatement* call = new SgAssignStmt(*new SgVarRefExp(everyS[0]), *new SgFunctionCallExp(*timeF)); + lastDecl->insertStmtAfter(*call, *func); + lastDecl->insertStmtAfter(*new SgStatement(DVM_BARRIER_DIR), *func); + + storeBlock->insertStmtAfter(call->copy(), *storeBlock); + storeBlock->insertStmtAfter(*new SgStatement(DVM_BARRIER_DIR), *storeBlock); + + call = new SgAssignStmt(*new SgVarRefExp(everyS[1]), *new SgFunctionCallExp(*timeF)); + storeBlock->insertStmtBefore(*new SgStatement(DVM_BARRIER_DIR), *storeBlock->controlParent()); + storeBlock->insertStmtBefore(*call, *storeBlock->controlParent()); + call->lexPrev()->addComment("! STORE CHECKPOINT\n"); + + storeBlock->setExpression(0, *new SgVarRefExp(everyS[1]) - *new SgVarRefExp(everyS[0]) >= *new SgValueExp(every)); + } + else if (type == typeEvery::ITER) + { + string everyIs = "spf_cp_interval" + additional; + everyS.push_back(new SgSymbol(VARIABLE_NAME, everyIs.c_str(), SgTypeInt(), func)); + initS.push_back(new SgValueExp(0)); + SgAssignStmt* init = new SgAssignStmt(*new SgVarRefExp(everyS[0]), *new SgValueExp(0)); + storeBlock->insertStmtAfter(*init, *storeBlock); + + SgAssignStmt* inc = new SgAssignStmt(*new SgVarRefExp(everyS[0]), *new SgVarRefExp(everyS[0]) + *new SgValueExp(1)); + storeBlock->insertStmtBefore(*inc, *storeBlock->controlParent()); + inc->addComment("! STORE CHECKPOINT\n"); + + storeBlock->setExpression(0, *new SgVarRefExp(everyS[0]) >= *new SgValueExp(every)); + } + + vector loadS; + vector initLoadS; + vector insertToLoadS; + + string cpLoadS = "spf_cp_load" + additional; + loadS.push_back(new SgSymbol(VARIABLE_NAME, cpLoadS.c_str(), SgTypeInt(), func)); + initLoadS.push_back(new SgValueExp(0)); + + string fileNS = "spf_cp_file_n" + additional; + loadS.push_back(new SgSymbol(VARIABLE_NAME, fileNS.c_str(), SgTypeInt(), func)); + initLoadS.push_back(new SgValueExp(1)); + + string iostatS = "spf_cp_iostat" + additional; + loadS.push_back(new SgSymbol(VARIABLE_NAME, iostatS.c_str(), SgTypeInt(), func)); + initLoadS.push_back(NULL); + + /*string loadSaveS = "spf_cp_load_save" + additional; + loadS.push_back(new SgSymbol(VARIABLE_NAME, loadSaveS.c_str(), SgTypeInt(), func)); + initLoadS.push_back(new SgValueExp(0));*/ + + string loadLabelS = "spf_cp_load_label" + additional; + loadS.push_back(new SgSymbol(VARIABLE_NAME, loadLabelS.c_str(), SgTypeInt(), func)); + initLoadS.push_back(new SgValueExp(0)); + + string saveFS = "spf_cp_save_flag" + additional; + loadS.push_back(new SgSymbol(VARIABLE_NAME, saveFS.c_str(), SgTypeInt(), func)); + initLoadS.push_back(new SgValueExp(0)); + + loadBlock->setExpression(0, *new SgVarRefExp(loadS[0]) == *new SgValueExp(0)); + SgAssignStmt* init = new SgAssignStmt(*new SgVarRefExp(loadS[0]), *new SgValueExp(1)); + insertToLoadS.push_back(init); + + + + SgExpression& unitNull = SgAssignOp(*new SgKeywordValExp("unit"), *new SgKeywordValExp("*")); + SgExpression& unit = SgAssignOp(*new SgKeywordValExp("unit"), *new SgValueExp(unitNum)); + SgExpression& frmt = SgAssignOp(*new SgKeywordValExp("fmt"), *new SgKeywordValExp("*")); + SgExpression& frmtProf = SgAssignOp(*new SgKeywordValExp("fmt"), *new SgValueExp(("(A,A" + to_string(maxFileLen) + ",F4.2,A)").c_str())); + + SgExpression* iostat = new SgVarRefExp(loadS[2]); + SgExpression* fileIdx = new SgVarRefExp(loadS[1]); + + insertToLoadS.push_back(createOpenJ_old(iostat, journal, unit)); + + vector insertToifLoadOk; + SgIfStmt* ifLoadOk = new SgIfStmt(*iostat == *new SgValueExp(0)); + insertToLoadS.push_back(ifLoadOk); + + SgInputOutputStmt* read = new SgInputOutputStmt(READ_STAT, *makeExprList({ &frmt, &unit }, false), *fileIdx); + insertToifLoadOk.push_back(read); + insertToifLoadOk.push_back(new SgIOControlStmt(CLOSE_STAT, unit)); + insertToifLoadOk.push_back(createOpen(iostat, files, fileIdx, unit)); + + SgIfStmt* ifLoadOk1 = new SgIfStmt(*iostat == *new SgValueExp(0), *new SgIOControlStmt(CLOSE_STAT, unit)); + + + insertToifLoadOk.push_back(ifLoadOk1); + ifLoadOk1->insertStmtAfter(*new SgIfStmt(*fileIdx == *new SgValueExp(numOfFiles + 1), *new SgAssignStmt(*fileIdx, *new SgValueExp(1))), *ifLoadOk1); + ifLoadOk1->insertStmtAfter(*new SgAssignStmt(*fileIdx, *fileIdx + *new SgValueExp(1)), *ifLoadOk1); + ifLoadOk1->addComment("! LOAD DATA FROM CHECKPOINT\n"); + + vector commentArgs; + commentArgs.push_back(new SgValueExp(" SECONDS")); + commentArgs.push_back(&(*new SgVarRefExp(profS[1]) - *new SgVarRefExp(profS[0]))); + commentArgs.push_back(new SgArrayRefExp(*files, *fileIdx)); + commentArgs.push_back(new SgValueExp("SPF CHECKPOINT LOADED FROM ")); + + ifLoadOk1->insertStmtAfter(*new SgInputOutputStmt(WRITE_STAT, *makeExprList({ &frmtProf, &unitNull }, false), *makeExprList(commentArgs, false)), *ifLoadOk1); + ifLoadOk1->insertStmtAfter(profCallE->copy(), *ifLoadOk1); + + //open all files + if (createdModuleForIO) + { + SgCallStmt* call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, iosNames[1].c_str())); + ifLoadOk1->insertStmtAfter(*call, *ifLoadOk1); + } + + //READ from modules + for (auto& mod : moduleNames) + { + SgCallStmt* call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, ("SPF_CP_" + mod).c_str())); + call->addArg(*new SgValueExp(unitNum)); + call->addArg(*new SgValueExp(0)); + ifLoadOk1->insertStmtAfter(*call, *ifLoadOk1); + } + + //READ DATA + if (local.size()) + { + auto dataRead = new SgInputOutputStmt(READ_STAT, unit, *makeExprList(local, false)); + ifLoadOk1->insertStmtAfter(*dataRead, *ifLoadOk1); + } + + ifLoadOk1->insertStmtAfter(profCallS->copy(), *ifLoadOk1); + + vector listSpec; + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("form"), *new SgValueExp("unformatted"))); + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *new SgArrayRefExp(*files, *fileIdx))); + listSpec.push_back(&unit); + + SgIOControlStmt* open = new SgIOControlStmt(OPEN_STAT, *makeExprList(listSpec, false)); + storeBlock->insertStmtAfter(*open, *storeBlock); + + SgIfStmt* ifStoreOk = new SgIfStmt(*iostat == *new SgValueExp(0)); + open->insertStmtAfter(*ifStoreOk, *storeBlock); + ifStoreOk->insertStmtAfter(*new SgIfStmt(*fileIdx == *new SgValueExp(numOfFiles + 1), *new SgAssignStmt(*fileIdx, *new SgValueExp(1))), *ifStoreOk); + ifStoreOk->insertStmtAfter(*new SgAssignStmt(*fileIdx, *fileIdx + *new SgValueExp(1)), *ifStoreOk); + ifStoreOk->addComment(("! STORE DATA TO CHECKPOINT " + to_string(local.size()) + " items\n").c_str()); + + commentArgs.clear(); + commentArgs.push_back(new SgValueExp(" SECONDS")); + commentArgs.push_back(&(*new SgVarRefExp(profS[1]) - *new SgVarRefExp(profS[0]))); + commentArgs.push_back(new SgArrayRefExp(*files, *fileIdx)); + commentArgs.push_back(new SgValueExp("SPF CHECKPOINT STORED TO ")); + + ifStoreOk->insertStmtAfter(*new SgInputOutputStmt(WRITE_STAT, *makeExprList({ &frmtProf, &unitNull }, false), *makeExprList(commentArgs, false)), *ifStoreOk); + ifStoreOk->insertStmtAfter(profCallE->copy(), *ifStoreOk); + + SgStatement* assign = ifStoreOk->lexNext(); + + //open all files + if (createdModuleForIO) + { + SgCallStmt* call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, iosNames[1].c_str())); + ifStoreOk->insertStmtAfter(*call, *ifStoreOk); + } + //WRITE from modules + for (auto& mod : moduleNames) + { + SgCallStmt* call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, ("SPF_CP_" + mod).c_str())); + call->addArg(*new SgValueExp(unitNum)); + call->addArg(*new SgValueExp(1)); + ifStoreOk->insertStmtAfter(*call, *ifStoreOk); + } + + //close and inc + if (createdModuleForIO) + { + SgCallStmt* call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, iosNames[2].c_str())); + ifStoreOk->insertStmtAfter(*call, *ifStoreOk); + call = new SgCallStmt(*new SgSymbol(FUNCTION_NAME, iosNames[0].c_str())); + ifStoreOk->insertStmtAfter(*call, *ifStoreOk); + } + + //WRITE DATA + if (local.size()) + { + SgStatement* dataWrite = new SgInputOutputStmt(WRITE_STAT, unit, *makeExprList(local, false)); + ifStoreOk->insertStmtAfter(*dataWrite, *ifStoreOk); + } + + listSpec.clear(); + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("iostat"), *iostat)); + listSpec.push_back(&SgAssignOp(*new SgKeywordValExp("file"), *journal)); + listSpec.push_back(&unit); + + SgIOControlStmt* openJ_new = new SgIOControlStmt(OPEN_STAT, *makeExprList(listSpec, false)); + + assign->insertStmtBefore(*new SgIOControlStmt(CLOSE_STAT, unit), *ifStoreOk); + assign->insertStmtBefore(*openJ_new, *ifStoreOk); + assign->insertStmtBefore(*new SgInputOutputStmt(WRITE_STAT, *makeExprList({ &frmt, &unit }, false), *fileIdx), *ifStoreOk); + assign->insertStmtBefore(*new SgIOControlStmt(CLOSE_STAT, unit), *ifStoreOk); + + ifStoreOk->insertStmtAfter(profCallS->copy(), *ifStoreOk); + + SgStatement* copyForGoto = loadBlock->copyPtr(); + copyForGoto->deleteLabel(); + firstExec->insertStmtBefore(*copyForGoto, *func); + insertInitNamesOfFiles(numOfFiles, additional, files, journal, copyForGoto, true); + + copyForGoto->insertStmtAfter(insertToLoadS[insertToLoadS.size() - 1]->copy(), *copyForGoto); + copyForGoto->insertStmtAfter(insertToLoadS[insertToLoadS.size() - 2]->copy(), *copyForGoto); + + copyForGoto = copyForGoto->lexNext()->lexNext(); + + copyForGoto->insertStmtAfter(*new SgGotoStmt(*loadblockLab), *copyForGoto); + copyForGoto->insertStmtAfter(*new SgIOControlStmt(CLOSE_STAT, unit), *copyForGoto); + + for (int z = insertToLoadS.size() - 1; z >= 0; --z) + loadBlock->insertStmtAfter(*insertToLoadS[z], *loadBlock); + + for (int z = insertToifLoadOk.size() - 1; z >= 0; --z) + ifLoadOk->insertStmtAfter(*insertToifLoadOk[z], *ifLoadOk); + + + //TODO: + /*set elemNotDeclHere; + for (auto& elem : commonVars) + { + if (addedToList.find(elem) == addedToList.end()) + elemNotDeclHere.insert(elem); + } + for (auto& elem : elemNotDeclHere) + printf("%s\n", elem.c_str());*/ + + // make all new declarations + makeDeclaration(moduleF, everyS, &initS); + makeDeclaration(moduleF, loadS, &initLoadS); + makeDeclaration(moduleF, profS); + makeDeclaration(moduleF, { files }); + makeDeclaration(func, { timeF }); + + func->insertStmtAfter(*new SgStatement(USE_STMT, NULL, mainModuleCpS), *func); + + //check use + map modulesDone; + for (auto& elem : moduleNames) + modulesDone[elem] = false; + + //check with ONLY + for (auto& use : useOfMods) + { + const string modName = use->symbol()->identifier(); + if (!modulesDone[modName]) + { + if (use->expr(0)) // has ONLY + { + SgExpression* ex = use->expr(0); + if (ex && ex->variant() == ONLY_NODE) + { + modulesDone[modName] = true; + auto callName = new SgSymbol(VARIABLE_NAME, ("SPF_CP_" + modName).c_str()); + ex->setLhs(new SgExpression(EXPR_LIST, new SgVarRefExp(callName), ex->lhs())); + } + } + } + } + + //check other + for (auto& use : useOfMods) + { + const string modName = use->symbol()->identifier(); + if (!modulesDone[modName]) + modulesDone[modName] = true; + } + + for (auto& elem : modulesDone) + { + if (!elem.second) + { + auto callName = new SgSymbol(VARIABLE_NAME, ("SPF_CP_" + elem.first).c_str()); + SgSymbol* modS = new SgSymbol(VARIABLE_NAME, elem.first.c_str()); + + vector onlyList; + onlyList.push_back(new SgVarRefExp(callName)); + if (elem.first == "spf_module_ios") + for (int z = 0; z < 3; ++z) + onlyList.push_back(new SgVarRefExp(new SgSymbol(VARIABLE_NAME, iosNames[z].c_str()))); + + SgExpression* only = new SgExpression(ONLY_NODE, makeExprList(onlyList), NULL); + SgStatement* use = new SgStatement(USE_STMT, NULL, modS, only, NULL, NULL); + func->insertStmtAfter(*use, *func); + } + } + + //TODO: this function needs to be completely rewritten!! + //processFunctionCallChain(func, allFuncInfo, moduleF, loadS, iostat, journal, frmt, unit, files, fileIdx, every, everyS); + } + + processModules(file); } static string cuttingType(const string& all_decl_with_init) From 36e401b5d25c26152b62c7b57dc9c648b61a4faa Mon Sep 17 00:00:00 2001 From: AntonMilienkov Date: Wed, 20 Dec 2023 16:31:16 +0300 Subject: [PATCH 04/40] findlocaldata --- .../_src/Transformations/checkpoints.cpp | 57 ++++++++++++------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp index 9da8cbc..a093fb0 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "../Utils/SgUtils.h" #include "../Utils/utils.h" @@ -38,7 +39,7 @@ static SgType* createArrayCharType(int len, int dim) } static void findDecls(SgExpression* ex, vector& local, const map& localParams, - set& added) + set& added, std::set& IntentInadded) { if (ex) { @@ -47,7 +48,8 @@ static void findDecls(SgExpression* ex, vector& local, const map< if (ex->symbol()->variant() == VARIABLE_NAME && localParams.find(ex->symbol()->identifier()) == localParams.end()) { - if (added.find(ex->symbol()->identifier()) == added.end()) + if (added.find(ex->symbol()->identifier()) == added.end() && + IntentInadded.find(ex->symbol()->identifier()) == IntentInadded.end()) { added.insert(ex->symbol()->identifier()); local.push_back(ex); @@ -58,21 +60,42 @@ static void findDecls(SgExpression* ex, vector& local, const map< if (ex->variant() == ARRAY_REF) { if (ex->symbol()->variant() == VARIABLE_NAME && - added.find(ex->symbol()->identifier()) == added.end()) + added.find(ex->symbol()->identifier()) == added.end() && + IntentInadded.find(ex->symbol()->identifier()) == IntentInadded.end()) { added.insert(ex->symbol()->identifier()); local.push_back(new SgArrayRefExp(*ex->symbol())); } } - findDecls(ex->lhs(), local, localParams, added); - findDecls(ex->rhs(), local, localParams, added); + findDecls(ex->lhs(), local, localParams, added, IntentInadded); + findDecls(ex->rhs(), local, localParams, added, IntentInadded); } } -static void findLocalData(SgStatement* start, SgStatement* end, vector& local, - map& localParams, set& added) + +static FuncInfo* findFileInfoByName(SgStatement* func, const vector& allFuncInfo) { + FuncInfo* funcI = NULL; + for (auto& funcs : allFuncInfo) + if (funcs->funcName == func->symbol()->identifier()) + funcI = funcs; + checkNull(funcI, convertFileName(__FILE__).c_str(), __LINE__); + + return funcI; +} + +static void findLocalData(SgStatement* func, SgStatement* end, vector& local, + map& localParams, set& added, const vector& allFuncInfo) +{ + SgStatement* start = func->lexNext(); + + FuncInfo* funcI = findFileInfoByName(func, allFuncInfo); + std::set IntentInadded; + for (int i = 0; i < funcI->funcParams.countOfPars; ++i) + if (funcI->funcParams.isArgIn(i) && !funcI->funcParams.isArgOut(i)) + IntentInadded.insert(funcI->funcParams.identificators[i]); + for (SgStatement* st = start; st != end; st = st->lexNext()) { if (st->variant() == PARAM_DECL) @@ -92,7 +115,7 @@ static void findLocalData(SgStatement* start, SgStatement* end, vectorlineNumber(), st->fileName(), tag[st->variant()]); if (st->variant() == VAR_DECL || st->variant() == VAR_DECL_90) - findDecls(st->expr(0), local, localParams, added); + findDecls(st->expr(0), local, localParams, added, IntentInadded); } } @@ -529,11 +552,7 @@ static void processFunctionCallChain(SgStatement* func, const vector& const int every, const vector& everyS) { //find function structure - FuncInfo* funcI = NULL; - for (auto& funcs : allFuncInfo) - if (funcs->funcName == func->symbol()->identifier()) - funcI = funcs; - checkNull(funcI, convertFileName(__FILE__).c_str(), __LINE__); + FuncInfo* funcI = findFileInfoByName(func, allFuncInfo); set> toProcess; fillToProcess(funcI, toProcess); @@ -555,7 +574,7 @@ static void processFunctionCallChain(SgStatement* func, const vector& vector local; map localParams; set addedToList; - findLocalData(hedrFrom->lexNext(), firstExec, local, localParams, addedToList); + findLocalData(hedrFrom, firstExec, local, localParams, addedToList, allFuncInfo); if (!processedFrom.count(funcFrom)) { @@ -720,7 +739,7 @@ static void processFunctionCallChain(SgStatement* func, const vector& } } -static void processModules(SgFile* file) +static void processModules(SgFile* file, const vector& allFuncInfo) { vector modules; findModulesInFile(file, modules); @@ -740,7 +759,7 @@ static void processModules(SgFile* file) vector local; map localParams; set addedToList; - findLocalData(mod->lexNext(), hasContains ? st : mod->lastNodeOfStmt(), local, localParams, addedToList); + findLocalData(mod, hasContains ? st : mod->lastNodeOfStmt(), local, localParams, addedToList, allFuncInfo); SgProcHedrStmt* newF = new SgProcHedrStmt((string("spf_cp_") + mod->symbol()->identifier()).c_str()); if (!hasContains) @@ -772,7 +791,7 @@ void createCheckpoints(SgFile* file, const map& commonBloc if (!inFile) { - processModules(file); + processModules(file, allFuncInfo); return; } @@ -832,7 +851,7 @@ void createCheckpoints(SgFile* file, const map& commonBloc vector local; map localParams; set addedToList; - findLocalData(func->lexNext(), firstExec, local, localParams, addedToList); + findLocalData(func, firstExec, local, localParams, addedToList, allFuncInfo); const vector useOfMods = findUseOfModules(func->lexNext(), firstExec); SgStatement* loadBlock = new SgStatement(IF_NODE); @@ -1165,7 +1184,7 @@ void createCheckpoints(SgFile* file, const map& commonBloc //processFunctionCallChain(func, allFuncInfo, moduleF, loadS, iostat, journal, frmt, unit, files, fileIdx, every, everyS); } - processModules(file); + processModules(file, allFuncInfo); } static string cuttingType(const string& all_decl_with_init) From 6ae8dac71f164095619f8a714eef52437756bc40 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Wed, 20 Dec 2023 17:18:02 +0300 Subject: [PATCH 05/40] improved parsing --- .../experts/Sapfor_2017/_src/Utils/utils.cpp | 39 ++++++++++++------- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp index acc6ab5..009f6cd 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp @@ -114,6 +114,7 @@ string OnlyName(const char *filename) string OnlyExt(const char *filename) { char *extname = new char[1024]; + extname[0] = '\0'; int i; int len = (int)strlen(filename); @@ -1385,25 +1386,33 @@ pair, vector> splitCommandLineForParse(char** argv, int a //fdv|f|ftn|for|f90|f95|f03 for (int z = 0; z < argc; ++z) { - string isFile = argv[z]; - if (isFile.find(".for") != string::npos || - isFile.find(".f90") != string::npos || - isFile.find(".fdv") != string::npos || - isFile.find(".ftn") != string::npos || - isFile.find(".f95") != string::npos || - isFile.find(".f03") != string::npos || - isFile.find(".f") != string::npos) + string arg = argv[z]; + + string ext = OnlyExt(arg.c_str()); + convertToLower(ext); + if (ext.find("for") != string::npos || + ext.find("f90") != string::npos || + ext.find("fdv") != string::npos || + ext.find("ftn") != string::npos || + ext.find("f95") != string::npos || + ext.find("f03") != string::npos || + ext.find("f") != string::npos) { - if (isFile.find("*") == string::npos) - files.insert(isFile); + if (arg.find("*") == string::npos) + files.insert(arg); else { #if __cplusplus >= 201703L - fs::path ext = fs::path(isFile).filename().extension(); + fs::path ext = fs::path(arg).filename().extension(); + auto str_ext = ext.string(); + convertToLower(str_ext); + for (auto& file : filesInDir) { - auto cmp = file.extension().compare(ext); - if (cmp == 0) + auto cmp_ext = file.extension().string(); + convertToLower(cmp_ext); + + if (cmp_ext == str_ext) files.insert(file.filename().string()); } #endif @@ -1411,10 +1420,10 @@ pair, vector> splitCommandLineForParse(char** argv, int a } else { - if (isFile == "-inl") + if (arg == "-inl") isInline = true; else - options.push_back(isFile); + options.push_back(arg); } } diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 4e2e3d5..741a709 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 "2257" +#define VERSION_SPF "2258" From a52671d44c9e1d4acb7fa264c5221d2c4952384b Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 20 Dec 2023 20:30:32 +0300 Subject: [PATCH 06/40] improved parser --- .../experts/Sapfor_2017/_src/Utils/utils.cpp | 3183 +++++++++-------- 1 file changed, 1602 insertions(+), 1581 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp index 009f6cd..0c6792f 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp @@ -1,1582 +1,1603 @@ -#include "../Utils/leak_detector.h" - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "utils.h" -#include "errors.h" -#include "version.h" - -#include "../GraphLoop/graph_loops.h" -#include "../Distribution/Array.h" -#include "../Distribution/Arrays.h" -#include "../DynamicAnalysis/gcov_info.h" -#include "../ParallelizationRegions/ParRegions.h" - -#if __SPF -#include "acc_analyzer.h" -#endif - -using std::map; -using std::pair; -using std::tuple; -using std::set; -using std::vector; -using std::string; -using std::wstring; - -#if __cplusplus >= 201703L -#include -namespace fs = std::filesystem; -#else -#ifdef _WIN32 -#include -#else -#include -#endif -#endif - -void createMapLoopGraph(map &sortedLoopGraph, const vector *loopGraph) -{ - if (loopGraph) - { - for (int i = 0; i < (int)loopGraph->size(); ++i) - { - auto it = sortedLoopGraph.find((*loopGraph)[i]->lineNum); - if (it != sortedLoopGraph.end()) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - - sortedLoopGraph[(*loopGraph)[i]->lineNum] = (*loopGraph)[i]; - createMapLoopGraph(sortedLoopGraph, &((*loopGraph)[i]->children)); - } - } -} - -string FullNameWithExt(const char* filename) -{ - char* basename = new char[strlen(filename) + 1]; - int i; - - strcpy(basename, filename); - for (i = (int)strlen(filename) - 1; i >= 0; --i) - { - if (basename[i] == '.') - { - basename[i] = '_'; - break; - } - } - - string retVal(basename); - delete[] basename; - - return retVal; -} - -string OnlyName(const char *filename) -{ - char *basename = new char[strlen(filename) + 1]; - int i; - - strcpy(basename, filename); - for (i = (int)strlen(filename) - 1; i >= 0; --i) - { - if (basename[i] == '.') - { - basename[i] = '\0'; - break; - } - } - - string retVal(basename); - delete[] basename; - - return retVal; -} - -string OnlyExt(const char *filename) -{ - char *extname = new char[1024]; - extname[0] = '\0'; - int i; - - int len = (int)strlen(filename); - for (i = len - 1; i >= 0; --i) - { - if (filename[i] == '.') - { - i++; - int k; - int bound = len - i; - for (k = 0; k < bound; ++k, ++i) - extname[k] = filename[i]; - extname[k] = '\0'; - break; - } - } - - string retVal(extname); - delete[] extname; - return retVal; -} - -void printHelp(const char **passNames, const int lastPass) -{ - printf("Help info for passes.\n\n"); - printf(" -f90 free form\n"); - printf(" -sh turn on static shadow analysis\n"); - printf(" -shWidth maximum width of shadow in percent\n"); - printf(" -ver/-Ver version of SAPFOR\n"); - printf(" -priv turn on static private analysis\n"); - printf(" -keep keep temporary files\n"); - printf(" -keepSPF keep SPF directives\n"); - printf(" -keepDVM keep DVM directives\n"); - printf(" -allVars get all parallel versions\n"); - printf(" -var N get specific parallel version, N=1,2,..\n"); - printf(" -parse run parser with next option\n"); - printf(" -inlineH run hierarchical inlining for all functions called from 'funcName'\n"); - printf(" -inlineI run incremental inlining for function 'funcName' on 'lineNum' of 'fileName'\n"); - printf(" -passInfo print passes information\n"); - printf(" -dumpIR print IR information (works only with BUILD_IR pass)\n"); - printf(" -ignoreDistArray ingnore array distribution information (set to all arrays no distribution state\n"); - printf(" -mpi shared memory and mpi programs parallelization\n"); - printf("\n"); - - printf(" -F output to folder\n"); - printf(" -p \n"); - printf(" -pass \n"); - printf(" -passN \n"); - for (int i = 0; i < lastPass; ++i) - printf(" pass_num = %d: %s\n", i, passNames[i]); - printf("\n"); - printf(" -t \n"); - printf(" analysis_num = 13: insert parallel directives\n"); - - throw(-1); -} - -void printVersion(const string pref) -{ - printf("%sSAPFOR version is %s, build date: %s %s, c++ version %d\n", pref.c_str(), VERSION_SPF, __DATE__, __TIME__, __cplusplus); -} - -extern bool noLogo; -extern int staticShadowAnalysis; -extern int keepDvmDirectives; -extern int ignoreIO; -extern int keepSpfDirs; - -const string printVersionAsFortranComm() -{ - if (noLogo) - return ""; - - char buf[512]; - sprintf(buf, "! *** generated by SAPFOR with version %s and build date: %s %s\n", VERSION_SPF, __DATE__, __TIME__); - - string ret = buf; - ret += "! *** Enabled options ***:\n"; - if (staticShadowAnalysis) - ret += "! *** shadow optimization\n"; - if (keepDvmDirectives) - ret += "! *** consider DVMH directives\n"; - if (mpiProgram) - ret += "! *** MPI program regime (shared memory parallelization)\n"; - if (ignoreIO) - ret += "! *** ignore I/O checker for arrays (DVM I/O limitations)\n"; - if (keepSpfDirs) - ret += "! *** save SPF directives\n"; - - ret += "! *** generated by SAPFOR\n"; - - return ret; -} - -void convertToLower(string &str) -{ - std::locale loc; - - for (int i = 0; i < str.length(); ++i) - str[i] = std::tolower(str[i], loc); -} - -void convertToUpper(string &str) -{ - std::locale loc; - - for (int i = 0; i < str.length(); ++i) - str[i] = std::toupper(str[i], loc); -} - -void splitString(const string &strIn, const char delim, vector &result, bool withQuotes) -{ - std::stringstream ss; - ss.str(strIn); - - vector tmp_result; - string item; - while (std::getline(ss, item, delim)) - tmp_result.push_back(item); - - if (withQuotes) - { - bool quStarted = false; - item = ""; - for (auto& elem : tmp_result) - { - if (elem.size()) - { - if (quStarted) - { - item += " " + elem; - if (elem[elem.size() - 1] == '"') - { - quStarted = false; - result.push_back(item.erase(item.size() - 1, 1)); - } - } - else - { - if (elem[0] == '"') - { - quStarted = true; - item = elem.erase(0, 1); - - if (item[item.size() - 1] == '"') - { - quStarted = false; - result.push_back(item.erase(item.size() - 1, 1)); - } - } - else - result.push_back(elem); - - } - } - else if (quStarted) - item += " "; - } - } - else - result = tmp_result; -} - -void splitString(const wstring& strIn, const char delim, vector& result, bool withQuotes) -{ - std::wstringstream ss; - ss.str(strIn); - - vector tmp_result; - wstring item; - wchar_t buf[1024]; - while (ss.good()) - { - ss.getline(buf, 1024, delim); - tmp_result.push_back(buf); - } - - if (withQuotes) - { - bool quStarted = false; - item = L""; - for (auto& elem : tmp_result) - { - if (elem.size()) - { - if (quStarted) - { - item += elem; - if (elem[elem.size() - 1] == L'"') - { - quStarted = false; - result.push_back(item.erase(item.size() - 1, 1)); - } - } - else - { - if (elem[0] == '"') - { - quStarted = true; - item = elem.erase(0, 1); - - if (item[item.size() - 1] == L'"') - { - quStarted = false; - result.push_back(item.erase(item.size() - 1, 1)); - } - } - else - result.push_back(elem); - - } - } - else if (quStarted) - item += L" "; - } - } - else - result = tmp_result; -} - -void removeSubstrFromStr(string &str, const string &del) -{ - string::size_type pos = str.find(del); - while (pos != string::npos) - { - str.erase(pos, del.size()); - pos = str.find(del, pos + 1); - } -} - -string convertFileName(const char *file) -{ - string tmp(file); - std::reverse(tmp.begin(), tmp.end()); - int t = 0; - while (tmp[t] != '\\' && tmp[t] != '/' && t < tmp.size()) - t++; - tmp.erase(tmp.begin() + t, tmp.end()); - std::reverse(tmp.begin(), tmp.end()); - return tmp; -} - -void printBlanks(const int sizeOfBlank, const int countOfBlanks) -{ - for (int k = 0; k < countOfBlanks; ++k) - for (int m = 0; m < sizeOfBlank; ++m) - { - char buf[256]; - sprintf(buf, " "); - addToGlobalBufferAndPrint(buf); - } -} - -string globalOutputBuffer = ""; -int consoleMode = 0; -void addToGlobalBufferAndPrint(const string &toPrint) -{ - globalOutputBuffer += toPrint; - if (consoleMode) - { - printf("%s", toPrint.c_str()); - fflush(NULL); - } -} - -void clearGlobalBuffer() { globalOutputBuffer = ""; } -const string& getGlobalBuffer() { return globalOutputBuffer; } - -set allocated; -set allocatedInt; -static void convertGlobalBuffer(short *&result, int *&resultSize) -{ - const unsigned len = (unsigned)globalOutputBuffer.size(); - result = new short[len + 1]; - allocated.insert(result); - - result[len] = '\0'; - for (unsigned i = 0; i < len; ++i) - result[i] = globalOutputBuffer[i]; - - resultSize = new int[1]; - resultSize[0] = (int)len; -} - -extern map> SPF_messages; //file ->messages -void clearGlobalMessagesBuffer() -{ - //clear allocated memory - for (auto& elem : allocated) - delete[]elem; - for (auto& elem : allocatedInt) - delete[]elem; - allocated.clear(); - allocatedInt.clear(); - - SPF_messages.clear(); -} - -static map> removeCopies(map> in) -{ - map> out; - for (auto& byFile : in) - { - map, const Messages*> uniq; - for (auto& message : byFile.second) - { - auto key = make_tuple(message.type, message.group, message.line, message.value); - /*string tmp = ""; - for (auto& s : message.toString()) - tmp += (char)s; - __spf_print(1, "%s\n", tmp.c_str());*/ - uniq[key] = &message; - } - __spf_print(1, "messages filtering for file '%s': count before %d, count after %d\n", byFile.first.c_str(), byFile.second.size(), uniq.size()); - vector uniqV; - for (auto& elem : uniq) - { - /*string tmp = ""; - for (auto& s : elem.second->toString()) - tmp += (char)s; - __spf_print(1, "%s\n", tmp.c_str());*/ - uniqV.push_back(*elem.second); - } - - out[byFile.first] = uniqV; - } - - return out; -} - -static void convertGlobalMessagesBuffer(short *&result, int *&resultSize) -{ - auto copySPF_messages = removeCopies(SPF_messages); - for (auto &byFile : copySPF_messages) - { - vector newVal; - bool waschanged = false; - for (auto &message : byFile.second) - { - if (message.line > 0) - newVal.push_back(message); - else - waschanged = true; - } - - if (waschanged) - byFile.second = newVal; - } - - wstring val = L""; - val += std::to_wstring(copySPF_messages.size()); - for (auto it = copySPF_messages.begin(); it != copySPF_messages.end(); ++it) - { - val += L"|" + to_wstring(it->first.c_str()) + L"|" + std::to_wstring(it->second.size()); - for (int k = 0; k < it->second.size(); ++k) - val += it->second[k].toString(); - } - - const unsigned len = (unsigned)val.size(); - result = new short[len + 1]; - allocated.insert(result); - - result[len] = '\0'; - for (unsigned i = 0; i < len; ++i) - result[i] = val[i]; - - resultSize = new int[1]; - resultSize[0] = (int)len; -} - - -void convertBuffers(short*& resultM, int*& resultSizeM, short*& result, int*& resultSize) -{ - convertGlobalMessagesBuffer(resultM, resultSizeM); - convertGlobalBuffer(result, resultSize); -} - -bool isSPF_comment(const string &bufStr) -{ - bool spfStart = false; - if (bufStr.size() > 6) - spfStart = (bufStr[0] == '!' || bufStr[0] == 'c') && - bufStr[1] == '$' && bufStr[2] == 's' && - bufStr[3] == 'p' && bufStr[4] == 'f'; - - return spfStart; -} - -bool isDVM_comment(const string& bufStr) -{ - bool dvmStart = false; - if (bufStr.size() > 6) - { - dvmStart = (bufStr[0] == '!' || bufStr[0] == 'c') && - bufStr[1] == 'd' && bufStr[2] == 'v' && - bufStr[3] == 'm' && bufStr[4] == '$'; - } - return dvmStart; -} - -void copyIncludes(const set &allIncludeFiles, const map>> &commentsToInclude, - const map>>& newCopyDeclToIncl, - const char *folderName, bool keepSpfDirs, bool isFreeStyle, int removeDvmDirs) -{ - for (auto &include : allIncludeFiles) - { - if (commentsToInclude.find(include) != commentsToInclude.end()) - continue; - - string newCurrFile = string(folderName) + "/" + include; - - FILE *tryToOpen = fopen(newCurrFile.c_str(), "r"); - if (tryToOpen == NULL) - { - __spf_print(1, " try to copy file '%s' to '%s'\n", include.c_str(), newCurrFile.c_str()); - - FILE *copyFile = fopen(newCurrFile.c_str(), "w"); - FILE *oldFile = fopen(include.c_str(), "r"); - if (!copyFile) - { - __spf_print(1, " can not open file '%s' for read\n", include.c_str()); - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - } - - if (!oldFile) - { - __spf_print(1, " can not open file '%s' for write\n", newCurrFile.c_str()); - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - } - - while (!feof(oldFile)) - { - char buf[8192]; - char *res = fgets(buf, 16384, oldFile); - if (res == NULL) - break; - - const string orig(buf); - string bufStr(buf); - convertToLower(bufStr); - if (!keepSpfDirs) - { - bool spfStart = isSPF_comment(bufStr); - if (spfStart) - bufStr = "\n"; - } - //remove DVM dirs or save DVM dirs as comment - if (removeDvmDirs == 1 || removeDvmDirs == 2) - { - if (bufStr[0] == '!' || bufStr[0] == 'c') - { - if (bufStr[1] == 'd' && bufStr[2] == 'v' && bufStr[3] == 'm' && bufStr[4] == '$') - { - if (removeDvmDirs == 1) - bufStr = ""; - else if (removeDvmDirs == 2) - bufStr.insert(1, " "); - } - } - } - - // save original include name - if (bufStr.find("include") != string::npos) - fputs(orig.c_str(), copyFile); - else - { - if (bufStr != "") - fputs(bufStr.c_str(), copyFile); - } - } - - auto newDecls = newCopyDeclToIncl.find(include); - if (newDecls != newCopyDeclToIncl.end()) - for (auto& declByLine : newDecls->second) - for (auto& decl : declByLine.second) - fputs(decl.c_str(), copyFile); - - fclose(oldFile); - fclose(copyFile); - - //TODO: read options - /*string text = ""; - copyFile = fopen(newCurrFile.c_str(), "r"); - while (!feof(copyFile)) - { - char buf[8192]; - char* res = fgets(buf, 16384, copyFile); - if (res == NULL) - break; - text += res; - } - fclose(copyFile); - - FileInfo tmp; - tmp.text = text; - tmp.style = isFreeStyle ? 2 : 0; - tmp.fileName = newCurrFile; - - convertStyle(&tmp);*/ - fflush(NULL); - } - else - fclose(tryToOpen); - } -} - -string splitDirective(const string &in_) -{ - if (in_ == "") - return ""; - - string in(in_); - string lastEnd = ""; - if (in[in.size() - 1] == '\n') - { - in.erase(in.begin() + in_.size() - 1); - lastEnd = "\n"; - } - - string out = ""; - if (in.size() < 71) - out = in; - else - { - char buf[72]; - - int len = in.size() - 71; - int idx = 71; - - buf[in.copy(buf, 71, 0)] = '\0'; - out += buf; - if (len > 0) - out += "\n!DVM$&"; - - while (len > 65) - { - buf[in.copy(buf, 65, idx)] = '\0'; - out += buf; - len -= 65; - idx += 65; - - if (len > 0) - out += "\n!DVM$&"; - } - - if (len > 0) - { - buf[in.copy(buf, 65, idx)] = '\0'; - out += buf; - } - } - - return out + lastEnd; -} - -string splitDirectiveFull(const string &in_) -{ - if (in_ == "") - return ""; - - string in(in_); - string lastEnd = ""; - if (in[in.size() - 1] == '\n') - { - in.erase(in.begin() + in_.size() - 1); - lastEnd = "\n"; - } - - string out = ""; - vector splited; - splitString(in_, '\n', splited); - for (int z = 0; z < splited.size(); ++z) - { - if (z != 0) - out += "\n"; - out += splitDirective(splited[z]); - } - return out + lastEnd; -} - -void ExitFromOmegaTest(const int c) { throw c; } -extern "C" void ExitFromParser(const int c) { ExitFromOmegaTest(c); } - -void sortFilesBySize(const char *proj_name) -{ - FILE *proj = fopen(proj_name, "r"); - if (proj) - { - map, string> files; - - while (!feof(proj)) - { - char buf[512]; - fgets(buf, 512, proj); - - string filename(buf); - long size; - FILE *fin = fopen(filename.c_str(), "rb"); - FILE *p_fin = fin; - if (fin) - { - fseek(p_fin, 0, SEEK_END); - size = ftell(p_fin); - fclose(fin); - } - else - size = 0; - - files[make_pair(size, filename)] = filename; - } - - fclose(proj); - proj = fopen(proj_name, "w"); - for (auto it = files.rbegin(); it != files.rend(); ++it) - fwrite(it->second.c_str(), sizeof(char), it->second.size(), proj); - fclose(proj); - } - else - { - __spf_print(1, "project file '%s' does not exist\n", proj_name); - throw(-1); - } -} - -void uniteVectors(const vector, vector>>> &first, - const vector, vector>>> &second, - vector, vector>>> &result) -{ - int *uniteF = new int[first.size()]; - int *uniteS = new int[second.size()]; - memset(uniteF, 0, sizeof(int) * first.size()); - memset(uniteS, 0, sizeof(int) * second.size()); - - for (int i = 0; i < (int)first.size(); ++i) - { - const string &arrayName = first[i].first.second; - int k; - for (k = 0; k < (int)second.size(); ++k) - { - if (uniteS[k] == 0) - { - if (arrayName == second[k].first.second) - { - uniteS[k] = 1; - break; - } - } - } - - if (k != (int)second.size()) - { - uniteF[i] = 1; - assert(first[i].second.size() == second[k].second.size()); - result.push_back(make_pair(first[i].first, vector>(first[i].second.size()))); - - const vector> &boundsF = first[i].second; - const vector> &boundsS = second[k].second; - for (int z = 0; z < (int)boundsF.size(); ++z) - { - pair newBounds = std::make_pair(std::max(boundsF[z].first, boundsS[z].first), std::max(boundsF[z].second, boundsS[z].second)); - result.back().second[z] = newBounds; - } - } - } - - for (int i = 0; i < (int)first.size(); ++i) - { - if (uniteF[i] == 0) - result.push_back(first[i]); - } - - for (int i = 0; i < (int)second.size(); ++i) - { - if (uniteS[i] == 0) - result.push_back(second[i]); - } - - delete[]uniteF; - delete[]uniteS; -} - -#include - -#define FULL_ERROR_DUMP 1 - -// pointer -> type of alloc function -#if FULL_ERROR_DUMP -static std::unordered_map> pointerCollection; -static std::unordered_map> pointerCollectionLocal; -#else -static std::unordered_map> pointerCollection; -static std::unordered_map> pointerCollectionLocal; -#endif - -static bool deleteInProgress = false; -static void* currentPointer = NULL; -static set deleted; - -static bool storeInLocal = false; - -// type == 0 -> free, type == 1 -> delete, type == 2 -> delete[] -// acc_analyzer.h: ControlFlowItem = 3, doLoopItem = 4, doLoops = 5, LabelCFI = 6, CLAStatementItem = 7 -// VarItem = 8, VarSet = 9, DoLoopDataItem = 10, DoLoopDataList = 11 -// CVarEntryInfo = 12, CScalarVarEntryInfo = 13, CRecordVarEntryInfo = 14 -// ArraySubscriptData = 15, CArrayVarEntryInfo = 16, BasicBlockItem = 17, -// CallAnalysisLog = 18, CExprList = 19, SymbolKey = 20, CBasicBlock = 21 -// CommonVarSet = 22, AnalysedCallsList = 23, CallData = 24, CommonVarInfo = 25 -// CommonDataItem = 26, CommonData = 27, PrivateDelayedItem = 28, ActualDelayedData = 29 -// ControlFlowGraph = 30 -extern "C" void addToCollection(const int line, const char *file, void *pointer, int type) -{ -#if FULL_ERROR_DUMP - if (storeInLocal) - pointerCollectionLocal.insert(std::make_pair(pointer, std::make_tuple(type, line, file))); - pointerCollection.insert(std::make_pair(pointer, std::make_tuple(type, line, file))); -#else - if (storeInLocal) - pointerCollectionLocal.insert(std::make_pair(pointer, std::make_tuple(type))); - pointerCollection.insert(std::make_pair(pointer, std::make_tuple(type))); -#endif -} - -extern "C" void removeFromCollection(void *pointer) -{ - if (deleteInProgress) - { - if (pointer != currentPointer) - deleted.insert(pointer); - } - else - { - auto it = pointerCollection.find(pointer); - if (it != pointerCollection.end()) - pointerCollection.erase(it); - - if (storeInLocal) - { - auto it = pointerCollectionLocal.find(pointer); - if (it != pointerCollectionLocal.end()) - pointerCollectionLocal.erase(it); - } - } -} - -void startLocalColletion() { storeInLocal = true; } -void finishLocalColletion() { storeInLocal = false; } - -void deleteLeaks() -{ - map places; - auto copy = pointerCollection; - for (auto& elem : copy) - { - string place = std::get<2>(elem.second); - places[place]++; - if (place.find("make_nodes.c") != string::npos) - { - free((char*)(elem.first)); - - auto it = pointerCollection.find(elem.first); - if (it != pointerCollection.end()) - pointerCollection.erase(it); - } - } -} - -void deletePointerAllocatedData(bool delLocal) -{ - int leaks = 0; - int failed = 0; - deleteInProgress = true; - deleted.clear(); - int maxS = -1; - int z = -1; - -#if FULL_ERROR_DUMP - std::unordered_map>* toDelCollection = delLocal ? &pointerCollectionLocal : &pointerCollection; -#else - std::unordered_map>* toDelCollection = delLocal ? &pointerCollectionLocal : &pointerCollection; -#endif - for (auto &elem : *toDelCollection) - { - ++z; - maxS = std::max(maxS, (int)deleted.size()); - - auto itD = deleted.find(elem.first); - if (deleted.find(elem.first) != deleted.end()) - { - deleted.erase(itD); - continue; - } - - const pair pointer = std::make_pair(elem.first, std::get<0>(elem.second)); - currentPointer = pointer.first; - //printf("[%d]: %d %s\n", z, std::get<1>(elem.second), std::get<2>(elem.second)); - //fflush(NULL); - if (pointer.second == 0) - { - if (pointer.first) - { - free((char*)(pointer.first)); - leaks++; - } - else - failed++; - } - else if (pointer.second == 1) - { - if (pointer.first) - { - delete (char*)(pointer.first); - leaks++; - } - else - failed++; - } - else if (pointer.second == 2) - { - if (pointer.first) - { - delete [](char*)(pointer.first); - leaks++; - } - else - failed++; - } - else if (pointer.second >= 3 && pointer.second <= 30) - { - if (pointer.first) - { - switch (pointer.second) - { -#if __SPF - case 3: delete (ControlFlowItem*)pointer.first; break; - case 4: delete (doLoopItem*)pointer.first; break; - case 5: delete (doLoops*)pointer.first; break; - case 6: delete (LabelCFI*)pointer.first; break; - case 7: delete (CLAStatementItem*)pointer.first; break; - case 8: delete (VarItem*)pointer.first; break; - case 9: delete (VarSet*)pointer.first; break; - case 10: delete (DoLoopDataItem*)pointer.first; break; - case 11: delete (DoLoopDataList*)pointer.first; break; - case 12: delete (CVarEntryInfo*)pointer.first; break; - case 13: delete (CScalarVarEntryInfo*)pointer.first; break; - case 14: delete (CRecordVarEntryInfo*)pointer.first; break; - case 15: delete (ArraySubscriptData*)pointer.first; break; - case 16: delete (CArrayVarEntryInfo*)pointer.first; break; - case 17: delete (BasicBlockItem*)pointer.first; break; - case 18: delete (CallAnalysisLog*)pointer.first; break; - case 19: delete (CExprList*)pointer.first; break; - case 20: delete (SymbolKey*)pointer.first; break; - case 21: delete (CBasicBlock*)pointer.first; break; - case 22: delete (CommonVarSet*)pointer.first; break; - case 23: delete (AnalysedCallsList*)pointer.first; break; - case 24: delete (CallData*)pointer.first; break; - case 25: delete (CommonVarInfo*)pointer.first; break; - case 26: delete (CommonDataItem*)pointer.first; break; - case 27: delete (CommonData*)pointer.first; break; - case 28: delete (PrivateDelayedItem*)pointer.first; break; - case 29: delete (ActualDelayedData*)pointer.first; break; - case 30: delete (ControlFlowGraph*)pointer.first; break; -#endif - default: - break; - } - - leaks++; - } - else - failed++; - } - } - - if (leaks > 0) - printf("SAPFOR: detected %d leaks of memory\n", leaks); - if (failed > 0) - printf("SAPFOR: detected failed %d leaks of memory\n", failed); - printf("SAPFOR: deleted set size %d, maxS = %d\n", (int)deleted.size(), maxS); - - toDelCollection->clear(); - deleted.clear(); - deleteInProgress = false; - currentPointer = NULL; -} - -static unsigned arrayIdCounter = 0; -unsigned getUniqArrayId() { return arrayIdCounter++; } - -static bool isAllRulesEqualWA_l(const vector>>> &rules) -{ - if (rules.size() <= 1) - return true; - else - { - vector>> first = rules[0]; - for (auto &elem : rules) - { - int z = 0; - if (elem.size() != first.size()) - return false; - for (auto &item : elem) - if (std::get<2>(item) != std::get<2>(first[z++])) - return false; - } - return true; - } -} - -template -static bool isAllRulesEqual_l(T rules) -{ - if (rules.size() <= 1) - return true; - else - { - for (auto &elem : rules) - if (elem != rules[0]) - return false; - return true; - } -} - -template -static bool isAllRulesEqual_p(T rules) -{ - if (rules.size() <= 1) - return true; - else - { - for (auto &elem : rules) - if (elem != rules[0]) - return false; - return true; - } -} - -bool isAllRulesEqualWithoutArray(const vector>>> &allRules) -{ - return isAllRulesEqualWA_l(allRules); -} - -bool isAllRulesEqual(const vector>>> &allRules) -{ - return isAllRulesEqual_l(allRules); -} - -bool isAllRulesEqual(const vector> &allRules) -{ - return isAllRulesEqual_l(allRules); -} - -bool isAllRulesEqual(const vector>> &allRules) -{ - return isAllRulesEqual_p(allRules); -} - -static int newLineNumber = -2; // -1 is used for OMP -int getNextNegativeLineNumber() -{ - int ret = newLineNumber; - newLineNumber--; - return ret; -} - -void findAndReplaceDimentions(vector>> &rule, const DIST::Arrays &allArrays) -{ - for (int i = 0; i < rule.size(); ++i) - { - if (std::get<0>(rule[i]) == NULL) - continue; - int alignTo = -1; - int ok = allArrays.GetDimNumber(std::get<0>(rule[i]), (std::get<1>(rule[i])), alignTo); - if (ok != 0) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - - (std::get<1>(rule[i])) = alignTo; - } -} - -vector findLinksBetweenArrays(DIST::Array *from, DIST::Array *to, const uint64_t regionId, bool withCheck) -{ - vector retVal(from->GetDimSize()); - std::fill(retVal.begin(), retVal.end(), -1); - - if (to->IsTemplate()) - { - if (to != from->GetTemplateArray(regionId, withCheck)) - return retVal; - else - return from->GetLinksWithTemplate(regionId); - } - else - { - if (to->GetTemplateArray(regionId, withCheck) != from->GetTemplateArray(regionId, withCheck)) - { - string leftT = to->GetTemplateArray(regionId, withCheck) ? to->GetTemplateArray(regionId, withCheck)->GetShortName() : "nul"; - string rightT = from->GetTemplateArray(regionId, withCheck) ? from->GetTemplateArray(regionId, withCheck)->GetShortName() : "nul"; - __spf_print(1, "regionId = %lld: templates for array %s and %s not eq: %s != %s\n", - regionId, from->GetName().c_str(), to->GetName().c_str(), leftT.c_str(), rightT.c_str()); - } - else - { - auto ruleL = from->GetLinksWithTemplate(regionId); - auto ruleR = to->GetLinksWithTemplate(regionId); - - int currD = 0; - for (auto &elem1 : ruleL) - { - int idx = 0; - for (auto &elem2 : ruleR) - { - if (elem2 == elem1) - { - retVal[currD] = idx; - break; - } - ++idx; - } - ++currD; - } - } - - return retVal; - } -} - -std::wstring to_wstring(const std::string str) { return std::wstring(str.begin(), str.end()); } - -template -objT& getObjectForFileFromMap(const char *fileName, map &mapObject) -{ - auto it = mapObject.find(fileName); - if (it == mapObject.end()) - it = mapObject.insert(it, std::make_pair(fileName, objT())); - return it->second; -} - -template vector& getObjectForFileFromMap(const char *fileName, map>&); -template vector& getObjectForFileFromMap(const char *fileName, map>&); -template vector& getObjectForFileFromMap(const char *fileName, map>&); -template map& getObjectForFileFromMap(const char *fileName, map>&); -template map& getObjectForFileFromMap(const char *fileName, map>&); -template map>& getObjectForFileFromMap(const char* fileName, map>>&); - -static set mpiFunctions; -bool isMpiFunction(const string& func) -{ - if (mpiFunctions.size() == 0) - { - mpiFunctions.insert("mpi_abort"); - mpiFunctions.insert("mpi_address"); - mpiFunctions.insert("mpi_allgather"); - mpiFunctions.insert("mpi_allgatherv"); - mpiFunctions.insert("mpi_allreduce"); - mpiFunctions.insert("mpi_alltoall"); - mpiFunctions.insert("mpi_alltoallv"); - mpiFunctions.insert("mpi_barrier"); - mpiFunctions.insert("mpi_bcast"); - mpiFunctions.insert("mpi_bsend"); - mpiFunctions.insert("mpi_bsend_init"); - mpiFunctions.insert("mpi_buffer_attach"); - mpiFunctions.insert("mpi_buffer_detach"); - mpiFunctions.insert("mpi_cart_coords"); - mpiFunctions.insert("mpi_cart_create"); - mpiFunctions.insert("mpi_cart_get"); - mpiFunctions.insert("mpi_cart_rank"); - mpiFunctions.insert("mpi_cart_shift"); - mpiFunctions.insert("mpi_cart_sub"); - mpiFunctions.insert("mpi_cartdim_get"); - mpiFunctions.insert("mpi_comm_create"); - mpiFunctions.insert("mpi_comm_dup"); - mpiFunctions.insert("mpi_comm_free"); - mpiFunctions.insert("mpi_comm_group"); - mpiFunctions.insert("mpi_comm_rank"); - mpiFunctions.insert("mpi_comm_size"); - mpiFunctions.insert("mpi_comm_split"); - mpiFunctions.insert("mpi_dims_create"); - mpiFunctions.insert("mpi_finalize"); - mpiFunctions.insert("mpi_gather"); - mpiFunctions.insert("mpi_gatherv"); - mpiFunctions.insert("mpi_get_count"); - mpiFunctions.insert("mpi_get_processor_name"); - mpiFunctions.insert("mpi_graph_create"); - mpiFunctions.insert("mpi_graph_get"); - mpiFunctions.insert("mpi_graph_neighbors"); - mpiFunctions.insert("mpi_graph_neighbors_count"); - mpiFunctions.insert("mpi_graphdims_get"); - mpiFunctions.insert("mpi_group_compare"); - mpiFunctions.insert("mpi_group_difference"); - mpiFunctions.insert("mpi_group_excl"); - mpiFunctions.insert("mpi_group_free"); - mpiFunctions.insert("mpi_group_incl"); - mpiFunctions.insert("mpi_group_intersection"); - mpiFunctions.insert("mpi_group_rank"); - mpiFunctions.insert("mpi_group_size"); - mpiFunctions.insert("mpi_group_translate_ranks"); - mpiFunctions.insert("mpi_group_union"); - mpiFunctions.insert("mpi_ibsend"); - mpiFunctions.insert("mpi_init"); - mpiFunctions.insert("mpi_initialized"); - mpiFunctions.insert("mpi_iprobe"); - mpiFunctions.insert("mpi_irecv"); - mpiFunctions.insert("mpi_irsend"); - mpiFunctions.insert("mpi_isend"); - mpiFunctions.insert("mpi_issend"); - mpiFunctions.insert("mpi_op_create"); - mpiFunctions.insert("mpi_op_free"); - mpiFunctions.insert("mpi_pack"); - mpiFunctions.insert("mpi_pack_size"); - mpiFunctions.insert("mpi_probe"); - mpiFunctions.insert("mpi_recv"); - mpiFunctions.insert("mpi_recv_init"); - mpiFunctions.insert("mpi_reduce"); - mpiFunctions.insert("mpi_reduce_scatter"); - mpiFunctions.insert("mpi_request_free"); - mpiFunctions.insert("mpi_rsend"); - mpiFunctions.insert("mpi_rsend_init"); - mpiFunctions.insert("mpi_scan"); - mpiFunctions.insert("mpi_scatter"); - mpiFunctions.insert("mpi_scatterv"); - mpiFunctions.insert("mpi_send"); - mpiFunctions.insert("mpi_send_init"); - mpiFunctions.insert("mpi_sendrecv"); - mpiFunctions.insert("mpi_sendrecv_replace"); - mpiFunctions.insert("mpi_ssend"); - mpiFunctions.insert("mpi_ssend_init"); - mpiFunctions.insert("mpi_start"); - mpiFunctions.insert("mpi_startall"); - mpiFunctions.insert("mpi_test"); - mpiFunctions.insert("mpi_testall"); - mpiFunctions.insert("mpi_testany"); - mpiFunctions.insert("mpi_testsome"); - mpiFunctions.insert("mpi_topo_test"); - mpiFunctions.insert("mpi_type_commit"); - mpiFunctions.insert("mpi_type_contiguous"); - mpiFunctions.insert("mpi_type_extent"); - mpiFunctions.insert("mpi_type_free"); - mpiFunctions.insert("mpi_type_hindexed"); - mpiFunctions.insert("mpi_type_hvector"); - mpiFunctions.insert("mpi_type_indexed"); - mpiFunctions.insert("mpi_type_lb"); - mpiFunctions.insert("mpi_type_size"); - mpiFunctions.insert("mpi_type_struct"); - mpiFunctions.insert("mpi_type_ub"); - mpiFunctions.insert("mpi_type_vector"); - mpiFunctions.insert("mpi_unpack"); - mpiFunctions.insert("mpi_wait"); - mpiFunctions.insert("mpi_waitall"); - mpiFunctions.insert("mpi_waitany"); - mpiFunctions.insert("mpi_waitsome"); - mpiFunctions.insert("mpi_wtick"); - mpiFunctions.insert("mpi_wtime"); - } - - return mpiFunctions.find(func) != mpiFunctions.end(); -} - -map createMapOfArrayAccess(const map, pair> &declaredArrays) -{ - map out; - - for (auto& elem : declaredArrays) - out[elem.second.first] = elem.second.second; - return out; -} - -string readFileToStr(const string& name) -{ - string sf; - std::ifstream inf; - - inf.open(name); - if (!inf) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - - getline(inf, sf, '\0'); - inf.close(); - return sf; -} - -void writeFileFromStr(const string& name, const string &data) -{ - string sf; - std::ofstream outf; - - outf.open(name); - if (!outf) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - outf << data; - outf.close(); -} - -ParallelRegion* getRegionById(const vector& regions, const uint64_t regionId) -{ - for (auto& region : regions) - if (region->GetId() == regionId) - return region; - - return NULL; -} - -ParallelRegion* getRegionByName(const vector& regions, const string& regionName) -{ - string test = regionName; - convertToLower(test); - - for (auto& region : regions) - if (region->GetName() == test) - return region; - - return NULL; -} - - -ParallelRegion* getRegionByLine(const vector& regions, const string& file, const int line) -{ - if (regions.size() == 1 && regions[0]->GetName() == "DEFAULT") // only default - return regions[0]; - else if (regions.size() > 0) - { - set regFound; - - for (int i = 0; i < regions.size(); ++i) - if (regions[i]->HasThisLine(line, file)) - regFound.insert(regions[i]); - - if (regFound.size() == 0) - return NULL; - else if (regFound.size() == 1) - return *regFound.begin(); - else - { - __spf_print(1, "WARN: this lines included in more than one region!!\n"); - return NULL; - } - } - else - return NULL; - - return NULL; -} - -set getAllRegionsByLine(const vector& regions, const string& file, const int line) -{ - set regFound; - - if (regions.size() == 1 && regions[0]->GetName() == "DEFAULT") // only default - regFound.insert(regions[0]); - else if (regions.size() > 0) - { - for (int i = 0; i < regions.size(); ++i) - if (regions[i]->HasThisLine(line, file)) - regFound.insert(regions[i]); - } - - return regFound; -} - -pair, vector> splitCommandLineForParse(char** argv, int argc, bool& isInline) -{ -#if __cplusplus >= 201703L - vector filesInDir; - for (const auto& entry : fs::directory_iterator(fs::current_path())) - filesInDir.push_back(entry.path().filename()); -#endif - vector options; - set files; - - //fdv|f|ftn|for|f90|f95|f03 - for (int z = 0; z < argc; ++z) - { - string arg = argv[z]; - - string ext = OnlyExt(arg.c_str()); - convertToLower(ext); - if (ext.find("for") != string::npos || - ext.find("f90") != string::npos || - ext.find("fdv") != string::npos || - ext.find("ftn") != string::npos || - ext.find("f95") != string::npos || - ext.find("f03") != string::npos || - ext.find("f") != string::npos) - { - if (arg.find("*") == string::npos) - files.insert(arg); - else - { -#if __cplusplus >= 201703L - fs::path ext = fs::path(arg).filename().extension(); - auto str_ext = ext.string(); - convertToLower(str_ext); - - for (auto& file : filesInDir) - { - auto cmp_ext = file.extension().string(); - convertToLower(cmp_ext); - - if (cmp_ext == str_ext) - files.insert(file.filename().string()); - } -#endif - } - } - else - { - if (arg == "-inl") - isInline = true; - else - options.push_back(arg); - } - } - - vector filesV; - for (auto& elem : files) - { - FILE* check = fopen(elem.c_str(), "r"); - if (check) - { - filesV.push_back(elem); - printf(" parse file '%s'\n", elem.c_str()); - fclose(check); - } - else - printf(" skip not existing file '%s'\n", elem.c_str()); - } - - return make_pair(options, filesV); -} - -bool createDirectory(const string& name) -{ -#if __cplusplus >= 201703L - if (fs::exists(name)) - return true; - else - return fs::create_directories(name); -#else -#ifdef _WIN32 - if (_mkdir(name.c_str()) == 0) - return true; -#else - if (mkdir(name.c_str(), 0777) == 0) - return true; -#endif - return false; -#endif -} - -string getClearName(const string& in) -{ - char fromChar; - if (in.find('.') == string::npos) - { - if (in.find("::") == string::npos) - return in; - else - fromChar = ':'; - } - else - fromChar = '.'; - - string ret = in; - auto it = ret.rfind(fromChar); - if (it == string::npos) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - ret = ret.substr(it + 1); - return ret; -} - -wstring fixedLongFormat(const wchar_t* old_) -{ -/*#if __SPF - return old_ ? old_ : L""; -#endif*/ - if (old_ == NULL) - return L""; - - wstring old(old_); - //exclude russian codes - if (old_[0] == L'R' && (old_[1] == L'R' || (old_[1] >= L'0' && old_[1] <= L'9'))) - return old; - - wstring ret = L""; - for (int z = 0; z < old.size(); ++z) - { - if (old[z] == L'%') - { - if (z + 1 < old.size()) - { - if (old[z + 1] == L's') - { - ret += L"%ls"; - z++; - continue; - } - } - } - ret += old[z]; - } - return ret; -} - -string convertStyle(const FileInfo* file, bool needRewrite) -{ - string text = file->text; - - vector splited; - splitString(text, '\n', splited); - - text = ""; - int z = 0; - for (auto& line : splited) - { - if (line[0] == 'c' || line[0] == 'C' || line[0] == 'd' || line[0] == 'D' || line[0] == '*') - line[0] = '!'; - - bool needContinuation = false; - if (line[0] != '!' && line.size() > 6) - { - if (line[5] != ' ' && !(line[5] > '0' && line[5] < '9')) // not label - { - line[5] = ' '; - needContinuation = true;// line[5] = '&'; - } - - int p = 73; - if (file->style == 1) - p = 133; - if (line.size() > p) - { - while (line[p] != '\0' && line[p] != '\n' && line[p] != '!') - { - line[p] = ' '; - p++; - if (p >= line.size()) - break; - } - } - } - - if (needContinuation) - text += "&"; - text += (z != 0 ? "\n" : "") + line; - ++z; - } - - if (needRewrite) - writeFileFromStr(file->fileName, text); - - return text; -} - -const set getExcludedModules() { return { "omp_lib", "ifport" }; }; - -map sortArraysByName(const set& toSort) -{ - map sorted; - for (auto& array : toSort) - sorted[array->GetName()] = array; - - if (sorted.size() != toSort.size()) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - - return sorted; +#include "../Utils/leak_detector.h" + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "utils.h" +#include "errors.h" +#include "version.h" + +#include "../GraphLoop/graph_loops.h" +#include "../Distribution/Array.h" +#include "../Distribution/Arrays.h" +#include "../DynamicAnalysis/gcov_info.h" +#include "../ParallelizationRegions/ParRegions.h" + +#if __SPF +#include "acc_analyzer.h" +#endif + +using std::map; +using std::pair; +using std::tuple; +using std::set; +using std::vector; +using std::string; +using std::wstring; + +#if __cplusplus >= 201703L +#include +namespace fs = std::filesystem; +#else +#ifdef _WIN32 +#include +#else +#include +#endif +#endif + +void createMapLoopGraph(map &sortedLoopGraph, const vector *loopGraph) +{ + if (loopGraph) + { + for (int i = 0; i < (int)loopGraph->size(); ++i) + { + auto it = sortedLoopGraph.find((*loopGraph)[i]->lineNum); + if (it != sortedLoopGraph.end()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + sortedLoopGraph[(*loopGraph)[i]->lineNum] = (*loopGraph)[i]; + createMapLoopGraph(sortedLoopGraph, &((*loopGraph)[i]->children)); + } + } +} + +string FullNameWithExt(const char* filename) +{ + char* basename = new char[strlen(filename) + 1]; + int i; + + strcpy(basename, filename); + for (i = (int)strlen(filename) - 1; i >= 0; --i) + { + if (basename[i] == '.') + { + basename[i] = '_'; + break; + } + } + + string retVal(basename); + delete[] basename; + + return retVal; +} + +string OnlyName(const char *filename) +{ + char *basename = new char[strlen(filename) + 1]; + int i; + + strcpy(basename, filename); + for (i = (int)strlen(filename) - 1; i >= 0; --i) + { + if (basename[i] == '.') + { + basename[i] = '\0'; + break; + } + } + + string retVal(basename); + delete[] basename; + + return retVal; +} + +string OnlyExt(const char *filename) +{ + char *extname = new char[1024]; + extname[0] = '\0'; + int i; + + int len = (int)strlen(filename); + for (i = len - 1; i >= 0; --i) + { + if (filename[i] == '.') + { + i++; + int k; + int bound = len - i; + for (k = 0; k < bound; ++k, ++i) + extname[k] = filename[i]; + extname[k] = '\0'; + break; + } + } + + string retVal(extname); + delete[] extname; + return retVal; +} + +void printHelp(const char **passNames, const int lastPass) +{ + printf("Help info for passes.\n\n"); + printf(" -f90 free form\n"); + printf(" -sh turn on static shadow analysis\n"); + printf(" -shWidth maximum width of shadow in percent\n"); + printf(" -ver/-Ver version of SAPFOR\n"); + printf(" -priv turn on static private analysis\n"); + printf(" -keep keep temporary files\n"); + printf(" -keepSPF keep SPF directives\n"); + printf(" -keepDVM keep DVM directives\n"); + printf(" -allVars get all parallel versions\n"); + printf(" -var N get specific parallel version, N=1,2,..\n"); + printf(" -parse run parser with next option\n"); + printf(" -inlineH run hierarchical inlining for all functions called from 'funcName'\n"); + printf(" -inlineI run incremental inlining for function 'funcName' on 'lineNum' of 'fileName'\n"); + printf(" -passInfo print passes information\n"); + printf(" -dumpIR print IR information (works only with BUILD_IR pass)\n"); + printf(" -ignoreDistArray ingnore array distribution information (set to all arrays no distribution state\n"); + printf(" -mpi shared memory and mpi programs parallelization\n"); + printf("\n"); + + printf(" -F output to folder\n"); + printf(" -p \n"); + printf(" -pass \n"); + printf(" -passN \n"); + for (int i = 0; i < lastPass; ++i) + printf(" pass_num = %d: %s\n", i, passNames[i]); + printf("\n"); + printf(" -t \n"); + printf(" analysis_num = 13: insert parallel directives\n"); + + throw(-1); +} + +void printVersion(const string pref) +{ + printf("%sSAPFOR version is %s, build date: %s %s, c++ version %d\n", pref.c_str(), VERSION_SPF, __DATE__, __TIME__, __cplusplus); +} + +extern bool noLogo; +extern int staticShadowAnalysis; +extern int keepDvmDirectives; +extern int ignoreIO; +extern int keepSpfDirs; + +const string printVersionAsFortranComm() +{ + if (noLogo) + return ""; + + char buf[512]; + sprintf(buf, "! *** generated by SAPFOR with version %s and build date: %s %s\n", VERSION_SPF, __DATE__, __TIME__); + + string ret = buf; + ret += "! *** Enabled options ***:\n"; + if (staticShadowAnalysis) + ret += "! *** shadow optimization\n"; + if (keepDvmDirectives) + ret += "! *** consider DVMH directives\n"; + if (mpiProgram) + ret += "! *** MPI program regime (shared memory parallelization)\n"; + if (ignoreIO) + ret += "! *** ignore I/O checker for arrays (DVM I/O limitations)\n"; + if (keepSpfDirs) + ret += "! *** save SPF directives\n"; + + ret += "! *** generated by SAPFOR\n"; + + return ret; +} + +void convertToLower(string &str) +{ + std::locale loc; + + for (int i = 0; i < str.length(); ++i) + str[i] = std::tolower(str[i], loc); +} + +void convertToUpper(string &str) +{ + std::locale loc; + + for (int i = 0; i < str.length(); ++i) + str[i] = std::toupper(str[i], loc); +} + +void splitString(const string &strIn, const char delim, vector &result, bool withQuotes) +{ + std::stringstream ss; + ss.str(strIn); + + vector tmp_result; + string item; + while (std::getline(ss, item, delim)) + tmp_result.push_back(item); + + if (withQuotes) + { + bool quStarted = false; + item = ""; + for (auto& elem : tmp_result) + { + if (elem.size()) + { + if (quStarted) + { + item += " " + elem; + if (elem[elem.size() - 1] == '"') + { + quStarted = false; + result.push_back(item.erase(item.size() - 1, 1)); + } + } + else + { + if (elem[0] == '"') + { + quStarted = true; + item = elem.erase(0, 1); + + if (item[item.size() - 1] == '"') + { + quStarted = false; + result.push_back(item.erase(item.size() - 1, 1)); + } + } + else + result.push_back(elem); + + } + } + else if (quStarted) + item += " "; + } + } + else + result = tmp_result; +} + +void splitString(const wstring& strIn, const char delim, vector& result, bool withQuotes) +{ + std::wstringstream ss; + ss.str(strIn); + + vector tmp_result; + wstring item; + wchar_t buf[1024]; + while (ss.good()) + { + ss.getline(buf, 1024, delim); + tmp_result.push_back(buf); + } + + if (withQuotes) + { + bool quStarted = false; + item = L""; + for (auto& elem : tmp_result) + { + if (elem.size()) + { + if (quStarted) + { + item += elem; + if (elem[elem.size() - 1] == L'"') + { + quStarted = false; + result.push_back(item.erase(item.size() - 1, 1)); + } + } + else + { + if (elem[0] == '"') + { + quStarted = true; + item = elem.erase(0, 1); + + if (item[item.size() - 1] == L'"') + { + quStarted = false; + result.push_back(item.erase(item.size() - 1, 1)); + } + } + else + result.push_back(elem); + + } + } + else if (quStarted) + item += L" "; + } + } + else + result = tmp_result; +} + +void removeSubstrFromStr(string &str, const string &del) +{ + string::size_type pos = str.find(del); + while (pos != string::npos) + { + str.erase(pos, del.size()); + pos = str.find(del, pos + 1); + } +} + +string convertFileName(const char *file) +{ + string tmp(file); + std::reverse(tmp.begin(), tmp.end()); + int t = 0; + while (tmp[t] != '\\' && tmp[t] != '/' && t < tmp.size()) + t++; + tmp.erase(tmp.begin() + t, tmp.end()); + std::reverse(tmp.begin(), tmp.end()); + return tmp; +} + +void printBlanks(const int sizeOfBlank, const int countOfBlanks) +{ + for (int k = 0; k < countOfBlanks; ++k) + for (int m = 0; m < sizeOfBlank; ++m) + { + char buf[256]; + sprintf(buf, " "); + addToGlobalBufferAndPrint(buf); + } +} + +string globalOutputBuffer = ""; +int consoleMode = 0; +void addToGlobalBufferAndPrint(const string &toPrint) +{ + globalOutputBuffer += toPrint; + if (consoleMode) + { + printf("%s", toPrint.c_str()); + fflush(NULL); + } +} + +void clearGlobalBuffer() { globalOutputBuffer = ""; } +const string& getGlobalBuffer() { return globalOutputBuffer; } + +set allocated; +set allocatedInt; +static void convertGlobalBuffer(short *&result, int *&resultSize) +{ + const unsigned len = (unsigned)globalOutputBuffer.size(); + result = new short[len + 1]; + allocated.insert(result); + + result[len] = '\0'; + for (unsigned i = 0; i < len; ++i) + result[i] = globalOutputBuffer[i]; + + resultSize = new int[1]; + resultSize[0] = (int)len; +} + +extern map> SPF_messages; //file ->messages +void clearGlobalMessagesBuffer() +{ + //clear allocated memory + for (auto& elem : allocated) + delete[]elem; + for (auto& elem : allocatedInt) + delete[]elem; + allocated.clear(); + allocatedInt.clear(); + + SPF_messages.clear(); +} + +static map> removeCopies(map> in) +{ + map> out; + for (auto& byFile : in) + { + map, const Messages*> uniq; + for (auto& message : byFile.second) + { + auto key = make_tuple(message.type, message.group, message.line, message.value); + /*string tmp = ""; + for (auto& s : message.toString()) + tmp += (char)s; + __spf_print(1, "%s\n", tmp.c_str());*/ + uniq[key] = &message; + } + __spf_print(1, "messages filtering for file '%s': count before %d, count after %d\n", byFile.first.c_str(), byFile.second.size(), uniq.size()); + vector uniqV; + for (auto& elem : uniq) + { + /*string tmp = ""; + for (auto& s : elem.second->toString()) + tmp += (char)s; + __spf_print(1, "%s\n", tmp.c_str());*/ + uniqV.push_back(*elem.second); + } + + out[byFile.first] = uniqV; + } + + return out; +} + +static void convertGlobalMessagesBuffer(short *&result, int *&resultSize) +{ + auto copySPF_messages = removeCopies(SPF_messages); + for (auto &byFile : copySPF_messages) + { + vector newVal; + bool waschanged = false; + for (auto &message : byFile.second) + { + if (message.line > 0) + newVal.push_back(message); + else + waschanged = true; + } + + if (waschanged) + byFile.second = newVal; + } + + wstring val = L""; + val += std::to_wstring(copySPF_messages.size()); + for (auto it = copySPF_messages.begin(); it != copySPF_messages.end(); ++it) + { + val += L"|" + to_wstring(it->first.c_str()) + L"|" + std::to_wstring(it->second.size()); + for (int k = 0; k < it->second.size(); ++k) + val += it->second[k].toString(); + } + + const unsigned len = (unsigned)val.size(); + result = new short[len + 1]; + allocated.insert(result); + + result[len] = '\0'; + for (unsigned i = 0; i < len; ++i) + result[i] = val[i]; + + resultSize = new int[1]; + resultSize[0] = (int)len; +} + + +void convertBuffers(short*& resultM, int*& resultSizeM, short*& result, int*& resultSize) +{ + convertGlobalMessagesBuffer(resultM, resultSizeM); + convertGlobalBuffer(result, resultSize); +} + +bool isSPF_comment(const string &bufStr) +{ + bool spfStart = false; + if (bufStr.size() > 6) + spfStart = (bufStr[0] == '!' || bufStr[0] == 'c') && + bufStr[1] == '$' && bufStr[2] == 's' && + bufStr[3] == 'p' && bufStr[4] == 'f'; + + return spfStart; +} + +bool isDVM_comment(const string& bufStr) +{ + bool dvmStart = false; + if (bufStr.size() > 6) + { + dvmStart = (bufStr[0] == '!' || bufStr[0] == 'c') && + bufStr[1] == 'd' && bufStr[2] == 'v' && + bufStr[3] == 'm' && bufStr[4] == '$'; + } + return dvmStart; +} + +void copyIncludes(const set &allIncludeFiles, const map>> &commentsToInclude, + const map>>& newCopyDeclToIncl, + const char *folderName, bool keepSpfDirs, bool isFreeStyle, int removeDvmDirs) +{ + for (auto &include : allIncludeFiles) + { + if (commentsToInclude.find(include) != commentsToInclude.end()) + continue; + + string newCurrFile = string(folderName) + "/" + include; + + FILE *tryToOpen = fopen(newCurrFile.c_str(), "r"); + if (tryToOpen == NULL) + { + __spf_print(1, " try to copy file '%s' to '%s'\n", include.c_str(), newCurrFile.c_str()); + + FILE *copyFile = fopen(newCurrFile.c_str(), "w"); + FILE *oldFile = fopen(include.c_str(), "r"); + if (!copyFile) + { + __spf_print(1, " can not open file '%s' for read\n", include.c_str()); + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + } + + if (!oldFile) + { + __spf_print(1, " can not open file '%s' for write\n", newCurrFile.c_str()); + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + } + + while (!feof(oldFile)) + { + char buf[8192]; + char *res = fgets(buf, 16384, oldFile); + if (res == NULL) + break; + + const string orig(buf); + string bufStr(buf); + convertToLower(bufStr); + if (!keepSpfDirs) + { + bool spfStart = isSPF_comment(bufStr); + if (spfStart) + bufStr = "\n"; + } + //remove DVM dirs or save DVM dirs as comment + if (removeDvmDirs == 1 || removeDvmDirs == 2) + { + if (bufStr[0] == '!' || bufStr[0] == 'c') + { + if (bufStr[1] == 'd' && bufStr[2] == 'v' && bufStr[3] == 'm' && bufStr[4] == '$') + { + if (removeDvmDirs == 1) + bufStr = ""; + else if (removeDvmDirs == 2) + bufStr.insert(1, " "); + } + } + } + + // save original include name + if (bufStr.find("include") != string::npos) + fputs(orig.c_str(), copyFile); + else + { + if (bufStr != "") + fputs(bufStr.c_str(), copyFile); + } + } + + auto newDecls = newCopyDeclToIncl.find(include); + if (newDecls != newCopyDeclToIncl.end()) + for (auto& declByLine : newDecls->second) + for (auto& decl : declByLine.second) + fputs(decl.c_str(), copyFile); + + fclose(oldFile); + fclose(copyFile); + + //TODO: read options + /*string text = ""; + copyFile = fopen(newCurrFile.c_str(), "r"); + while (!feof(copyFile)) + { + char buf[8192]; + char* res = fgets(buf, 16384, copyFile); + if (res == NULL) + break; + text += res; + } + fclose(copyFile); + + FileInfo tmp; + tmp.text = text; + tmp.style = isFreeStyle ? 2 : 0; + tmp.fileName = newCurrFile; + + convertStyle(&tmp);*/ + fflush(NULL); + } + else + fclose(tryToOpen); + } +} + +string splitDirective(const string &in_) +{ + if (in_ == "") + return ""; + + string in(in_); + string lastEnd = ""; + if (in[in.size() - 1] == '\n') + { + in.erase(in.begin() + in_.size() - 1); + lastEnd = "\n"; + } + + string out = ""; + if (in.size() < 71) + out = in; + else + { + char buf[72]; + + int len = in.size() - 71; + int idx = 71; + + buf[in.copy(buf, 71, 0)] = '\0'; + out += buf; + if (len > 0) + out += "\n!DVM$&"; + + while (len > 65) + { + buf[in.copy(buf, 65, idx)] = '\0'; + out += buf; + len -= 65; + idx += 65; + + if (len > 0) + out += "\n!DVM$&"; + } + + if (len > 0) + { + buf[in.copy(buf, 65, idx)] = '\0'; + out += buf; + } + } + + return out + lastEnd; +} + +string splitDirectiveFull(const string &in_) +{ + if (in_ == "") + return ""; + + string in(in_); + string lastEnd = ""; + if (in[in.size() - 1] == '\n') + { + in.erase(in.begin() + in_.size() - 1); + lastEnd = "\n"; + } + + string out = ""; + vector splited; + splitString(in_, '\n', splited); + for (int z = 0; z < splited.size(); ++z) + { + if (z != 0) + out += "\n"; + out += splitDirective(splited[z]); + } + return out + lastEnd; +} + +void ExitFromOmegaTest(const int c) { throw c; } +extern "C" void ExitFromParser(const int c) { ExitFromOmegaTest(c); } + +void sortFilesBySize(const char *proj_name) +{ + FILE *proj = fopen(proj_name, "r"); + if (proj) + { + map, string> files; + + while (!feof(proj)) + { + char buf[512]; + fgets(buf, 512, proj); + + string filename(buf); + long size; + FILE *fin = fopen(filename.c_str(), "rb"); + FILE *p_fin = fin; + if (fin) + { + fseek(p_fin, 0, SEEK_END); + size = ftell(p_fin); + fclose(fin); + } + else + size = 0; + + files[make_pair(size, filename)] = filename; + } + + fclose(proj); + proj = fopen(proj_name, "w"); + for (auto it = files.rbegin(); it != files.rend(); ++it) + fwrite(it->second.c_str(), sizeof(char), it->second.size(), proj); + fclose(proj); + } + else + { + __spf_print(1, "project file '%s' does not exist\n", proj_name); + throw(-1); + } +} + +void uniteVectors(const vector, vector>>> &first, + const vector, vector>>> &second, + vector, vector>>> &result) +{ + int *uniteF = new int[first.size()]; + int *uniteS = new int[second.size()]; + memset(uniteF, 0, sizeof(int) * first.size()); + memset(uniteS, 0, sizeof(int) * second.size()); + + for (int i = 0; i < (int)first.size(); ++i) + { + const string &arrayName = first[i].first.second; + int k; + for (k = 0; k < (int)second.size(); ++k) + { + if (uniteS[k] == 0) + { + if (arrayName == second[k].first.second) + { + uniteS[k] = 1; + break; + } + } + } + + if (k != (int)second.size()) + { + uniteF[i] = 1; + assert(first[i].second.size() == second[k].second.size()); + result.push_back(make_pair(first[i].first, vector>(first[i].second.size()))); + + const vector> &boundsF = first[i].second; + const vector> &boundsS = second[k].second; + for (int z = 0; z < (int)boundsF.size(); ++z) + { + pair newBounds = std::make_pair(std::max(boundsF[z].first, boundsS[z].first), std::max(boundsF[z].second, boundsS[z].second)); + result.back().second[z] = newBounds; + } + } + } + + for (int i = 0; i < (int)first.size(); ++i) + { + if (uniteF[i] == 0) + result.push_back(first[i]); + } + + for (int i = 0; i < (int)second.size(); ++i) + { + if (uniteS[i] == 0) + result.push_back(second[i]); + } + + delete[]uniteF; + delete[]uniteS; +} + +#include + +#define FULL_ERROR_DUMP 1 + +// pointer -> type of alloc function +#if FULL_ERROR_DUMP +static std::unordered_map> pointerCollection; +static std::unordered_map> pointerCollectionLocal; +#else +static std::unordered_map> pointerCollection; +static std::unordered_map> pointerCollectionLocal; +#endif + +static bool deleteInProgress = false; +static void* currentPointer = NULL; +static set deleted; + +static bool storeInLocal = false; + +// type == 0 -> free, type == 1 -> delete, type == 2 -> delete[] +// acc_analyzer.h: ControlFlowItem = 3, doLoopItem = 4, doLoops = 5, LabelCFI = 6, CLAStatementItem = 7 +// VarItem = 8, VarSet = 9, DoLoopDataItem = 10, DoLoopDataList = 11 +// CVarEntryInfo = 12, CScalarVarEntryInfo = 13, CRecordVarEntryInfo = 14 +// ArraySubscriptData = 15, CArrayVarEntryInfo = 16, BasicBlockItem = 17, +// CallAnalysisLog = 18, CExprList = 19, SymbolKey = 20, CBasicBlock = 21 +// CommonVarSet = 22, AnalysedCallsList = 23, CallData = 24, CommonVarInfo = 25 +// CommonDataItem = 26, CommonData = 27, PrivateDelayedItem = 28, ActualDelayedData = 29 +// ControlFlowGraph = 30 +extern "C" void addToCollection(const int line, const char *file, void *pointer, int type) +{ +#if FULL_ERROR_DUMP + if (storeInLocal) + pointerCollectionLocal.insert(std::make_pair(pointer, std::make_tuple(type, line, file))); + pointerCollection.insert(std::make_pair(pointer, std::make_tuple(type, line, file))); +#else + if (storeInLocal) + pointerCollectionLocal.insert(std::make_pair(pointer, std::make_tuple(type))); + pointerCollection.insert(std::make_pair(pointer, std::make_tuple(type))); +#endif +} + +extern "C" void removeFromCollection(void *pointer) +{ + if (deleteInProgress) + { + if (pointer != currentPointer) + deleted.insert(pointer); + } + else + { + auto it = pointerCollection.find(pointer); + if (it != pointerCollection.end()) + pointerCollection.erase(it); + + if (storeInLocal) + { + auto it = pointerCollectionLocal.find(pointer); + if (it != pointerCollectionLocal.end()) + pointerCollectionLocal.erase(it); + } + } +} + +void startLocalColletion() { storeInLocal = true; } +void finishLocalColletion() { storeInLocal = false; } + +void deleteLeaks() +{ + map places; + auto copy = pointerCollection; + for (auto& elem : copy) + { + string place = std::get<2>(elem.second); + places[place]++; + if (place.find("make_nodes.c") != string::npos) + { + free((char*)(elem.first)); + + auto it = pointerCollection.find(elem.first); + if (it != pointerCollection.end()) + pointerCollection.erase(it); + } + } +} + +void deletePointerAllocatedData(bool delLocal) +{ + int leaks = 0; + int failed = 0; + deleteInProgress = true; + deleted.clear(); + int maxS = -1; + int z = -1; + +#if FULL_ERROR_DUMP + std::unordered_map>* toDelCollection = delLocal ? &pointerCollectionLocal : &pointerCollection; +#else + std::unordered_map>* toDelCollection = delLocal ? &pointerCollectionLocal : &pointerCollection; +#endif + for (auto &elem : *toDelCollection) + { + ++z; + maxS = std::max(maxS, (int)deleted.size()); + + auto itD = deleted.find(elem.first); + if (deleted.find(elem.first) != deleted.end()) + { + deleted.erase(itD); + continue; + } + + const pair pointer = std::make_pair(elem.first, std::get<0>(elem.second)); + currentPointer = pointer.first; + //printf("[%d]: %d %s\n", z, std::get<1>(elem.second), std::get<2>(elem.second)); + //fflush(NULL); + if (pointer.second == 0) + { + if (pointer.first) + { + free((char*)(pointer.first)); + leaks++; + } + else + failed++; + } + else if (pointer.second == 1) + { + if (pointer.first) + { + delete (char*)(pointer.first); + leaks++; + } + else + failed++; + } + else if (pointer.second == 2) + { + if (pointer.first) + { + delete [](char*)(pointer.first); + leaks++; + } + else + failed++; + } + else if (pointer.second >= 3 && pointer.second <= 30) + { + if (pointer.first) + { + switch (pointer.second) + { +#if __SPF + case 3: delete (ControlFlowItem*)pointer.first; break; + case 4: delete (doLoopItem*)pointer.first; break; + case 5: delete (doLoops*)pointer.first; break; + case 6: delete (LabelCFI*)pointer.first; break; + case 7: delete (CLAStatementItem*)pointer.first; break; + case 8: delete (VarItem*)pointer.first; break; + case 9: delete (VarSet*)pointer.first; break; + case 10: delete (DoLoopDataItem*)pointer.first; break; + case 11: delete (DoLoopDataList*)pointer.first; break; + case 12: delete (CVarEntryInfo*)pointer.first; break; + case 13: delete (CScalarVarEntryInfo*)pointer.first; break; + case 14: delete (CRecordVarEntryInfo*)pointer.first; break; + case 15: delete (ArraySubscriptData*)pointer.first; break; + case 16: delete (CArrayVarEntryInfo*)pointer.first; break; + case 17: delete (BasicBlockItem*)pointer.first; break; + case 18: delete (CallAnalysisLog*)pointer.first; break; + case 19: delete (CExprList*)pointer.first; break; + case 20: delete (SymbolKey*)pointer.first; break; + case 21: delete (CBasicBlock*)pointer.first; break; + case 22: delete (CommonVarSet*)pointer.first; break; + case 23: delete (AnalysedCallsList*)pointer.first; break; + case 24: delete (CallData*)pointer.first; break; + case 25: delete (CommonVarInfo*)pointer.first; break; + case 26: delete (CommonDataItem*)pointer.first; break; + case 27: delete (CommonData*)pointer.first; break; + case 28: delete (PrivateDelayedItem*)pointer.first; break; + case 29: delete (ActualDelayedData*)pointer.first; break; + case 30: delete (ControlFlowGraph*)pointer.first; break; +#endif + default: + break; + } + + leaks++; + } + else + failed++; + } + } + + if (leaks > 0) + printf("SAPFOR: detected %d leaks of memory\n", leaks); + if (failed > 0) + printf("SAPFOR: detected failed %d leaks of memory\n", failed); + printf("SAPFOR: deleted set size %d, maxS = %d\n", (int)deleted.size(), maxS); + + toDelCollection->clear(); + deleted.clear(); + deleteInProgress = false; + currentPointer = NULL; +} + +static unsigned arrayIdCounter = 0; +unsigned getUniqArrayId() { return arrayIdCounter++; } + +static bool isAllRulesEqualWA_l(const vector>>> &rules) +{ + if (rules.size() <= 1) + return true; + else + { + vector>> first = rules[0]; + for (auto &elem : rules) + { + int z = 0; + if (elem.size() != first.size()) + return false; + for (auto &item : elem) + if (std::get<2>(item) != std::get<2>(first[z++])) + return false; + } + return true; + } +} + +template +static bool isAllRulesEqual_l(T rules) +{ + if (rules.size() <= 1) + return true; + else + { + for (auto &elem : rules) + if (elem != rules[0]) + return false; + return true; + } +} + +template +static bool isAllRulesEqual_p(T rules) +{ + if (rules.size() <= 1) + return true; + else + { + for (auto &elem : rules) + if (elem != rules[0]) + return false; + return true; + } +} + +bool isAllRulesEqualWithoutArray(const vector>>> &allRules) +{ + return isAllRulesEqualWA_l(allRules); +} + +bool isAllRulesEqual(const vector>>> &allRules) +{ + return isAllRulesEqual_l(allRules); +} + +bool isAllRulesEqual(const vector> &allRules) +{ + return isAllRulesEqual_l(allRules); +} + +bool isAllRulesEqual(const vector>> &allRules) +{ + return isAllRulesEqual_p(allRules); +} + +static int newLineNumber = -2; // -1 is used for OMP +int getNextNegativeLineNumber() +{ + int ret = newLineNumber; + newLineNumber--; + return ret; +} + +void findAndReplaceDimentions(vector>> &rule, const DIST::Arrays &allArrays) +{ + for (int i = 0; i < rule.size(); ++i) + { + if (std::get<0>(rule[i]) == NULL) + continue; + int alignTo = -1; + int ok = allArrays.GetDimNumber(std::get<0>(rule[i]), (std::get<1>(rule[i])), alignTo); + if (ok != 0) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + (std::get<1>(rule[i])) = alignTo; + } +} + +vector findLinksBetweenArrays(DIST::Array *from, DIST::Array *to, const uint64_t regionId, bool withCheck) +{ + vector retVal(from->GetDimSize()); + std::fill(retVal.begin(), retVal.end(), -1); + + if (to->IsTemplate()) + { + if (to != from->GetTemplateArray(regionId, withCheck)) + return retVal; + else + return from->GetLinksWithTemplate(regionId); + } + else + { + if (to->GetTemplateArray(regionId, withCheck) != from->GetTemplateArray(regionId, withCheck)) + { + string leftT = to->GetTemplateArray(regionId, withCheck) ? to->GetTemplateArray(regionId, withCheck)->GetShortName() : "nul"; + string rightT = from->GetTemplateArray(regionId, withCheck) ? from->GetTemplateArray(regionId, withCheck)->GetShortName() : "nul"; + __spf_print(1, "regionId = %lld: templates for array %s and %s not eq: %s != %s\n", + regionId, from->GetName().c_str(), to->GetName().c_str(), leftT.c_str(), rightT.c_str()); + } + else + { + auto ruleL = from->GetLinksWithTemplate(regionId); + auto ruleR = to->GetLinksWithTemplate(regionId); + + int currD = 0; + for (auto &elem1 : ruleL) + { + int idx = 0; + for (auto &elem2 : ruleR) + { + if (elem2 == elem1) + { + retVal[currD] = idx; + break; + } + ++idx; + } + ++currD; + } + } + + return retVal; + } +} + +std::wstring to_wstring(const std::string str) { return std::wstring(str.begin(), str.end()); } + +template +objT& getObjectForFileFromMap(const char *fileName, map &mapObject) +{ + auto it = mapObject.find(fileName); + if (it == mapObject.end()) + it = mapObject.insert(it, std::make_pair(fileName, objT())); + return it->second; +} + +template vector& getObjectForFileFromMap(const char *fileName, map>&); +template vector& getObjectForFileFromMap(const char *fileName, map>&); +template vector& getObjectForFileFromMap(const char *fileName, map>&); +template map& getObjectForFileFromMap(const char *fileName, map>&); +template map& getObjectForFileFromMap(const char *fileName, map>&); +template map>& getObjectForFileFromMap(const char* fileName, map>>&); + +static set mpiFunctions; +bool isMpiFunction(const string& func) +{ + if (mpiFunctions.size() == 0) + { + mpiFunctions.insert("mpi_abort"); + mpiFunctions.insert("mpi_address"); + mpiFunctions.insert("mpi_allgather"); + mpiFunctions.insert("mpi_allgatherv"); + mpiFunctions.insert("mpi_allreduce"); + mpiFunctions.insert("mpi_alltoall"); + mpiFunctions.insert("mpi_alltoallv"); + mpiFunctions.insert("mpi_barrier"); + mpiFunctions.insert("mpi_bcast"); + mpiFunctions.insert("mpi_bsend"); + mpiFunctions.insert("mpi_bsend_init"); + mpiFunctions.insert("mpi_buffer_attach"); + mpiFunctions.insert("mpi_buffer_detach"); + mpiFunctions.insert("mpi_cart_coords"); + mpiFunctions.insert("mpi_cart_create"); + mpiFunctions.insert("mpi_cart_get"); + mpiFunctions.insert("mpi_cart_rank"); + mpiFunctions.insert("mpi_cart_shift"); + mpiFunctions.insert("mpi_cart_sub"); + mpiFunctions.insert("mpi_cartdim_get"); + mpiFunctions.insert("mpi_comm_create"); + mpiFunctions.insert("mpi_comm_dup"); + mpiFunctions.insert("mpi_comm_free"); + mpiFunctions.insert("mpi_comm_group"); + mpiFunctions.insert("mpi_comm_rank"); + mpiFunctions.insert("mpi_comm_size"); + mpiFunctions.insert("mpi_comm_split"); + mpiFunctions.insert("mpi_dims_create"); + mpiFunctions.insert("mpi_finalize"); + mpiFunctions.insert("mpi_gather"); + mpiFunctions.insert("mpi_gatherv"); + mpiFunctions.insert("mpi_get_count"); + mpiFunctions.insert("mpi_get_processor_name"); + mpiFunctions.insert("mpi_graph_create"); + mpiFunctions.insert("mpi_graph_get"); + mpiFunctions.insert("mpi_graph_neighbors"); + mpiFunctions.insert("mpi_graph_neighbors_count"); + mpiFunctions.insert("mpi_graphdims_get"); + mpiFunctions.insert("mpi_group_compare"); + mpiFunctions.insert("mpi_group_difference"); + mpiFunctions.insert("mpi_group_excl"); + mpiFunctions.insert("mpi_group_free"); + mpiFunctions.insert("mpi_group_incl"); + mpiFunctions.insert("mpi_group_intersection"); + mpiFunctions.insert("mpi_group_rank"); + mpiFunctions.insert("mpi_group_size"); + mpiFunctions.insert("mpi_group_translate_ranks"); + mpiFunctions.insert("mpi_group_union"); + mpiFunctions.insert("mpi_ibsend"); + mpiFunctions.insert("mpi_init"); + mpiFunctions.insert("mpi_initialized"); + mpiFunctions.insert("mpi_iprobe"); + mpiFunctions.insert("mpi_irecv"); + mpiFunctions.insert("mpi_irsend"); + mpiFunctions.insert("mpi_isend"); + mpiFunctions.insert("mpi_issend"); + mpiFunctions.insert("mpi_op_create"); + mpiFunctions.insert("mpi_op_free"); + mpiFunctions.insert("mpi_pack"); + mpiFunctions.insert("mpi_pack_size"); + mpiFunctions.insert("mpi_probe"); + mpiFunctions.insert("mpi_recv"); + mpiFunctions.insert("mpi_recv_init"); + mpiFunctions.insert("mpi_reduce"); + mpiFunctions.insert("mpi_reduce_scatter"); + mpiFunctions.insert("mpi_request_free"); + mpiFunctions.insert("mpi_rsend"); + mpiFunctions.insert("mpi_rsend_init"); + mpiFunctions.insert("mpi_scan"); + mpiFunctions.insert("mpi_scatter"); + mpiFunctions.insert("mpi_scatterv"); + mpiFunctions.insert("mpi_send"); + mpiFunctions.insert("mpi_send_init"); + mpiFunctions.insert("mpi_sendrecv"); + mpiFunctions.insert("mpi_sendrecv_replace"); + mpiFunctions.insert("mpi_ssend"); + mpiFunctions.insert("mpi_ssend_init"); + mpiFunctions.insert("mpi_start"); + mpiFunctions.insert("mpi_startall"); + mpiFunctions.insert("mpi_test"); + mpiFunctions.insert("mpi_testall"); + mpiFunctions.insert("mpi_testany"); + mpiFunctions.insert("mpi_testsome"); + mpiFunctions.insert("mpi_topo_test"); + mpiFunctions.insert("mpi_type_commit"); + mpiFunctions.insert("mpi_type_contiguous"); + mpiFunctions.insert("mpi_type_extent"); + mpiFunctions.insert("mpi_type_free"); + mpiFunctions.insert("mpi_type_hindexed"); + mpiFunctions.insert("mpi_type_hvector"); + mpiFunctions.insert("mpi_type_indexed"); + mpiFunctions.insert("mpi_type_lb"); + mpiFunctions.insert("mpi_type_size"); + mpiFunctions.insert("mpi_type_struct"); + mpiFunctions.insert("mpi_type_ub"); + mpiFunctions.insert("mpi_type_vector"); + mpiFunctions.insert("mpi_unpack"); + mpiFunctions.insert("mpi_wait"); + mpiFunctions.insert("mpi_waitall"); + mpiFunctions.insert("mpi_waitany"); + mpiFunctions.insert("mpi_waitsome"); + mpiFunctions.insert("mpi_wtick"); + mpiFunctions.insert("mpi_wtime"); + } + + return mpiFunctions.find(func) != mpiFunctions.end(); +} + +map createMapOfArrayAccess(const map, pair> &declaredArrays) +{ + map out; + + for (auto& elem : declaredArrays) + out[elem.second.first] = elem.second.second; + return out; +} + +string readFileToStr(const string& name) +{ + string sf; + std::ifstream inf; + + inf.open(name); + if (!inf) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + getline(inf, sf, '\0'); + inf.close(); + return sf; +} + +void writeFileFromStr(const string& name, const string &data) +{ + string sf; + std::ofstream outf; + + outf.open(name); + if (!outf) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + outf << data; + outf.close(); +} + +ParallelRegion* getRegionById(const vector& regions, const uint64_t regionId) +{ + for (auto& region : regions) + if (region->GetId() == regionId) + return region; + + return NULL; +} + +ParallelRegion* getRegionByName(const vector& regions, const string& regionName) +{ + string test = regionName; + convertToLower(test); + + for (auto& region : regions) + if (region->GetName() == test) + return region; + + return NULL; +} + + +ParallelRegion* getRegionByLine(const vector& regions, const string& file, const int line) +{ + if (regions.size() == 1 && regions[0]->GetName() == "DEFAULT") // only default + return regions[0]; + else if (regions.size() > 0) + { + set regFound; + + for (int i = 0; i < regions.size(); ++i) + if (regions[i]->HasThisLine(line, file)) + regFound.insert(regions[i]); + + if (regFound.size() == 0) + return NULL; + else if (regFound.size() == 1) + return *regFound.begin(); + else + { + __spf_print(1, "WARN: this lines included in more than one region!!\n"); + return NULL; + } + } + else + return NULL; + + return NULL; +} + +set getAllRegionsByLine(const vector& regions, const string& file, const int line) +{ + set regFound; + + if (regions.size() == 1 && regions[0]->GetName() == "DEFAULT") // only default + regFound.insert(regions[0]); + else if (regions.size() > 0) + { + for (int i = 0; i < regions.size(); ++i) + if (regions[i]->HasThisLine(line, file)) + regFound.insert(regions[i]); + } + + return regFound; +} + +static bool checkFormat(const string& toCheck, const string& given) +{ + if (given == "" || given == "*") + { + if (toCheck == "for" || toCheck == "f90" || toCheck == "fdv" || + toCheck == "ftn" || toCheck == "f95" || toCheck == "f03" || + toCheck == "f") + { + return true; + } + } + else if (toCheck == given) + return true; + + return false; +} + +pair, vector> splitCommandLineForParse(char** argv, int argc, bool& isInline) +{ +#if __cplusplus >= 201703L + vector filesInDir; + for (const auto& entry : fs::directory_iterator(fs::current_path())) + filesInDir.push_back(entry.path().filename()); +#endif + vector options; + set files; + + //fdv|f|ftn|for|f90|f95|f03 + for (int z = 0; z < argc; ++z) + { + string arg = argv[z]; + + string ext = OnlyExt(arg.c_str()); + convertToLower(ext); + if (arg.find("*") != string::npos || checkFormat(ext, "")) + { + if (arg.find("*") == string::npos) + { + if (checkFormat(ext, "")) + files.insert(arg); + else + printf("skip file %s\n", arg.c_str()); + } + else + { +#if __cplusplus >= 201703L + for (auto& file : filesInDir) + { + auto cmp_ext = file.extension().string(); + convertToLower(cmp_ext); + + if (checkFormat(cmp_ext, arg)) + files.insert(file.filename().string()); + else + printf(" skip file %s\n", arg.c_str()); + } +#endif + } + } + else + { + if (arg == "-inl") + isInline = true; + else if (arg.find("-") != string::npos) + options.push_back(arg); + else + printf(" skip option %s\n", arg.c_str()); + } + } + + vector filesV; + for (auto& elem : files) + { + FILE* check = fopen(elem.c_str(), "r"); + if (check) + { + filesV.push_back(elem); + printf(" parse file '%s'\n", elem.c_str()); + fclose(check); + } + else + printf(" skip not existing file '%s'\n", elem.c_str()); + } + + printf(" options: "); + for (auto& opt : options) + printf("%s ", opt.c_str()); + printf("\n"); + + return make_pair(options, filesV); +} + +bool createDirectory(const string& name) +{ +#if __cplusplus >= 201703L + if (fs::exists(name)) + return true; + else + return fs::create_directories(name); +#else +#ifdef _WIN32 + if (_mkdir(name.c_str()) == 0) + return true; +#else + if (mkdir(name.c_str(), 0777) == 0) + return true; +#endif + return false; +#endif +} + +string getClearName(const string& in) +{ + char fromChar; + if (in.find('.') == string::npos) + { + if (in.find("::") == string::npos) + return in; + else + fromChar = ':'; + } + else + fromChar = '.'; + + string ret = in; + auto it = ret.rfind(fromChar); + if (it == string::npos) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + ret = ret.substr(it + 1); + return ret; +} + +wstring fixedLongFormat(const wchar_t* old_) +{ +/*#if __SPF + return old_ ? old_ : L""; +#endif*/ + if (old_ == NULL) + return L""; + + wstring old(old_); + //exclude russian codes + if (old_[0] == L'R' && (old_[1] == L'R' || (old_[1] >= L'0' && old_[1] <= L'9'))) + return old; + + wstring ret = L""; + for (int z = 0; z < old.size(); ++z) + { + if (old[z] == L'%') + { + if (z + 1 < old.size()) + { + if (old[z + 1] == L's') + { + ret += L"%ls"; + z++; + continue; + } + } + } + ret += old[z]; + } + return ret; +} + +string convertStyle(const FileInfo* file, bool needRewrite) +{ + string text = file->text; + + vector splited; + splitString(text, '\n', splited); + + text = ""; + int z = 0; + for (auto& line : splited) + { + if (line[0] == 'c' || line[0] == 'C' || line[0] == 'd' || line[0] == 'D' || line[0] == '*') + line[0] = '!'; + + bool needContinuation = false; + if (line[0] != '!' && line.size() > 6) + { + if (line[5] != ' ' && !(line[5] > '0' && line[5] < '9')) // not label + { + line[5] = ' '; + needContinuation = true;// line[5] = '&'; + } + + int p = 73; + if (file->style == 1) + p = 133; + if (line.size() > p) + { + while (line[p] != '\0' && line[p] != '\n' && line[p] != '!') + { + line[p] = ' '; + p++; + if (p >= line.size()) + break; + } + } + } + + if (needContinuation) + text += "&"; + text += (z != 0 ? "\n" : "") + line; + ++z; + } + + if (needRewrite) + writeFileFromStr(file->fileName, text); + + return text; +} + +const set getExcludedModules() { return { "omp_lib", "ifport" }; }; + +map sortArraysByName(const set& toSort) +{ + map sorted; + for (auto& array : toSort) + sorted[array->GetName()] = array; + + if (sorted.size() != toSort.size()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + return sorted; } \ No newline at end of file From e51f9643a48282631dadfdee4c81b9f1e3401353 Mon Sep 17 00:00:00 2001 From: AntonMilienkov Date: Fri, 22 Dec 2023 07:08:48 +0300 Subject: [PATCH 07/40] IntentInadded -> IntentInParams --- .../_src/Transformations/checkpoints.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp index a093fb0..9167653 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include "../Utils/SgUtils.h" #include "../Utils/utils.h" @@ -39,7 +38,7 @@ static SgType* createArrayCharType(int len, int dim) } static void findDecls(SgExpression* ex, vector& local, const map& localParams, - set& added, std::set& IntentInadded) + set& added, set& IntentInParams) { if (ex) { @@ -68,8 +67,8 @@ static void findDecls(SgExpression* ex, vector& local, const map< } } - findDecls(ex->lhs(), local, localParams, added, IntentInadded); - findDecls(ex->rhs(), local, localParams, added, IntentInadded); + findDecls(ex->lhs(), local, localParams, added, IntentInParams); + findDecls(ex->rhs(), local, localParams, added, IntentInParams); } } @@ -91,10 +90,10 @@ static void findLocalData(SgStatement* func, SgStatement* end, vectorlexNext(); FuncInfo* funcI = findFileInfoByName(func, allFuncInfo); - std::set IntentInadded; + set IntentInParams; for (int i = 0; i < funcI->funcParams.countOfPars; ++i) if (funcI->funcParams.isArgIn(i) && !funcI->funcParams.isArgOut(i)) - IntentInadded.insert(funcI->funcParams.identificators[i]); + IntentInParams.insert(funcI->funcParams.identificators[i]); for (SgStatement* st = start; st != end; st = st->lexNext()) { @@ -115,7 +114,7 @@ static void findLocalData(SgStatement* func, SgStatement* end, vectorlineNumber(), st->fileName(), tag[st->variant()]); if (st->variant() == VAR_DECL || st->variant() == VAR_DECL_90) - findDecls(st->expr(0), local, localParams, added, IntentInadded); + findDecls(st->expr(0), local, localParams, added, IntentInParams); } } From 34f02144042bc788d606e9528b0e97e6d0f74a56 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Wed, 27 Dec 2023 12:57:00 +0300 Subject: [PATCH 08/40] fixed and improved SAPFOR and FDVM --- dvm/fdvm/trunk/fdvm/dvm.cpp | 12 +++++- dvm/fdvm/trunk/include/dvm.h | 2 + .../_src/DvmhRegions/DvmhRegionInserter.cpp | 12 +++++- .../_src/LoopAnalyzer/loop_analyzer.cpp | 22 +++++------ .../_src/LoopAnalyzer/loop_analyzer.h | 2 +- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 5 ++- sapfor/experts/Sapfor_2017/_src/Sapfor.h | 2 + sapfor/experts/Sapfor_2017/_src/SapforData.h | 2 +- .../_src/Transformations/checkpoints.cpp | 16 ++++---- .../Sapfor_2017/_src/Utils/PassManager.h | 2 +- .../Sapfor_2017/_src/Utils/SgUtils.cpp | 38 +++++++++++++++++++ .../experts/Sapfor_2017/_src/Utils/SgUtils.h | 4 +- .../experts/Sapfor_2017/_src/Utils/utils.cpp | 7 ++-- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- .../_src/VisualizerCalls/get_information.cpp | 16 ++++++-- 15 files changed, 109 insertions(+), 35 deletions(-) diff --git a/dvm/fdvm/trunk/fdvm/dvm.cpp b/dvm/fdvm/trunk/fdvm/dvm.cpp index c63cd24..debdf1d 100644 --- a/dvm/fdvm/trunk/fdvm/dvm.cpp +++ b/dvm/fdvm/trunk/fdvm/dvm.cpp @@ -12668,6 +12668,7 @@ int DistrArrayAssign(SgStatement *stmt) return(1); left_whole = !le->lhs(); right_whole = !re->lhs(); + CANCEL_RTS2_MODE; // switch to basic RTS interface ChangeDistArrayRef(le->lhs()); //replacing dvm-array references in subscript list ChangeDistArrayRef(re->lhs()); LINE_NUMBER_BEFORE(stmt,stmt); @@ -12709,13 +12710,15 @@ int DistrArrayAssign(SgStatement *stmt) doAssignStmtAfter(ArrayCopy(headr, from_init, from_init+rr, from_init+2*rr, headl, to_init, to_init+rl, to_init+2*rl, 0)); if(dvm_ind) doCallAfter(DeleteObject_H(DVM000(dvm_ind))); - SET_DVM(to_init); + SET_DVM(to_init); + RESUMPTION_RTS2_MODE; // return to RTS2 interface return(1); } // assignment statement of kind: = if(only_debug) return(1); + CANCEL_RTS2_MODE; // switch to basic RTS interface if(INTERFACE_RTS2 && !isWholeArray(stmt->expr(0))) err("Illegal array statement in -Opl2 mode", 642, stmt); @@ -12735,6 +12738,7 @@ int DistrArrayAssign(SgStatement *stmt) else doAssignStmtAfter(ArrayCopy(headr, to_init, to_init, to_init, headl, to_init, to_init+rl, to_init+2*rl, -1)); SET_DVM(to_init); + RESUMPTION_RTS2_MODE; // return to RTS2 interface return(1); } @@ -12753,6 +12757,7 @@ int AssignDistrArray(SgStatement *stmt) // assignment statement of kind: = if(only_debug) return(1); + CANCEL_RTS2_MODE; // switch to basic RTS interface left_whole = !le->lhs(); right_whole = !re->lhs(); @@ -12781,8 +12786,10 @@ int AssignDistrArray(SgStatement *stmt) typer = ar->type()->baseType(); rr = Rank(ar); headr = HeaderRef(ar); - if(!headr) // if there is error of dvm-array specification, header is not created + if(!headr) { // if there is error of dvm-array specification, header is not created + RESUMPTION_RTS2_MODE; // return to RTS2 interface return(0); + } if(!CompareTypes(typel,typer)) err("Different types of left and right side",620,stmt); @@ -12801,6 +12808,7 @@ int AssignDistrArray(SgStatement *stmt) doCallAfter(DeleteObject_H(DVM000(dvm_ind))); SET_DVM(dvm_ind ? dvm_ind : to_init) ; //SET_DVM(to_init); + RESUMPTION_RTS2_MODE; // return to RTS2 interface return(1); } diff --git a/dvm/fdvm/trunk/include/dvm.h b/dvm/fdvm/trunk/include/dvm.h index c25913e..bd4baf8 100644 --- a/dvm/fdvm/trunk/include/dvm.h +++ b/dvm/fdvm/trunk/include/dvm.h @@ -415,6 +415,8 @@ const int Logical_8 = 12; #define FILE_LAST_STATEMENT(ST) ((SgStatement **)(ST)->attributeValue(0,LAST_STATEMENT)) #define CALLED_FUNCTIONS(ST) ((symb_list **)(ST)->attributeValue(0,RTC_CALLS)) #define INTERFACE_RTS2 (parloop_by_handler == 2) +#define CANCEL_RTS2_MODE if(parloop_by_handler == 2) parloop_by_handler = -1 +#define RESUMPTION_RTS2_MODE if(parloop_by_handler == -1) parloop_by_handler = 2 #define HEADER_FOR_HANDLER(A) ( (SgSymbol **)(A)->attributeValue(0,HANDLER_HEADER) ) #define USE_STATEMENTS_ARE_REQUIRED ( (int *) first_do_par->attributeValue(0,MODULE_USE) ) #define DEFERRED_SHAPE_TEMPLATE(A) ( (ORIGINAL_SYMBOL(A))->attributeValue(0,DEFERRED_SHAPE) ) diff --git a/sapfor/experts/Sapfor_2017/_src/DvmhRegions/DvmhRegionInserter.cpp b/sapfor/experts/Sapfor_2017/_src/DvmhRegions/DvmhRegionInserter.cpp index 15323b2..23b5aa6 100644 --- a/sapfor/experts/Sapfor_2017/_src/DvmhRegions/DvmhRegionInserter.cpp +++ b/sapfor/experts/Sapfor_2017/_src/DvmhRegions/DvmhRegionInserter.cpp @@ -382,7 +382,17 @@ ArraySet DvmhRegionInserter::excludeRemotes(const ArraySet& block, SgStatement* printInternalError(convertFileName(__FILE__).c_str(), __LINE__); SgStatement* next = remoteDir->lexNext(); - const string leftS = next->variant() == ASSIGN_STAT ? OriginalSymbol(next->expr(0)->symbol())->identifier() : ""; + const string leftS = ""; + + //TODO: record ref! + if (next->variant() == ASSIGN_STAT) + { + auto ex = next->expr(0); + if (ex->variant() == ARRAY_REF) + OriginalSymbol(ex->symbol())->identifier(); + else if (ex->variant() == RECORD_REF && ex->rhs()->variant() == ARRAY_REF) + OriginalSymbol(ex->rhs()->symbol())->identifier(); + } set raArrays; diff --git a/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.cpp b/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.cpp index a3df4bf..f618651 100644 --- a/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.cpp +++ b/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.cpp @@ -2666,7 +2666,7 @@ static void findArrayRefs(SgExpression *ex, SgStatement *st, string fName, int p const vector &inRegion, const set &funcParNames, map>& ompThreadPrivate, - const map& keyValueFromGUI, + const map& distrStateFromGUI, const bool saveAllLocals, map>& currMessages, int& errorCount) @@ -2852,8 +2852,8 @@ static void findArrayRefs(SgExpression *ex, SgStatement *st, string fName, int p itNew->second.first->SetDistributeFlag(DIST::SPF_PRIV); else { - auto it = keyValueFromGUI.find(itNew->second.first->GetIndepUniqName()); - if (it != keyValueFromGUI.end()) + auto it = distrStateFromGUI.find(itNew->second.first->GetIndepUniqName()); + if (it != distrStateFromGUI.end()) { if (it->second != oldVal) { @@ -2910,7 +2910,7 @@ static void findArrayRefs(SgExpression *ex, SgStatement *st, string fName, int p //assume all arguments of function as OUT, except for inctrinsics bool isWriteN = intr ? false : true; //need to correct W/R usage with GraphCall map later - findArrayRefs(funcExp->arg(z), st, fName, z, isWriteN, commonBlocks, declaredArrays, declaratedArraysSt, privates, deprecatedByIO, isExecutable, currFunctionName, inRegion, funcParNames, ompThreadPrivate, keyValueFromGUI, saveAllLocals, currMessages, errorCount); + findArrayRefs(funcExp->arg(z), st, fName, z, isWriteN, commonBlocks, declaredArrays, declaratedArraysSt, privates, deprecatedByIO, isExecutable, currFunctionName, inRegion, funcParNames, ompThreadPrivate, distrStateFromGUI, saveAllLocals, currMessages, errorCount); } } else @@ -2920,8 +2920,8 @@ static void findArrayRefs(SgExpression *ex, SgStatement *st, string fName, int p queue.push(findInfo("", ex->lhs(), -1, isWriteN)); if (ex->rhs()) queue.push(findInfo("", ex->rhs(), -1, isWriteN)); - //findArrayRefs(ex->lhs(), st, "", -1, isWriteN, commonBlocks, declaredArrays, declaratedArraysSt, privates, deprecatedByIO, isExecutable, currFunctionName, inRegion, funcParNames, ompThreadPrivate, keyValueFromGUI, saveAllLocals, currMessages, errorCount); - //findArrayRefs(ex->rhs(), st, "", -1, isWriteN, commonBlocks, declaredArrays, declaratedArraysSt, privates, deprecatedByIO, isExecutable, currFunctionName, inRegion, funcParNames, ompThreadPrivate, keyValueFromGUI, saveAllLocals, currMessages, errorCount); + //findArrayRefs(ex->lhs(), st, "", -1, isWriteN, commonBlocks, declaredArrays, declaratedArraysSt, privates, deprecatedByIO, isExecutable, currFunctionName, inRegion, funcParNames, ompThreadPrivate, distrStateFromGUI, saveAllLocals, currMessages, errorCount); + //findArrayRefs(ex->rhs(), st, "", -1, isWriteN, commonBlocks, declaredArrays, declaratedArraysSt, privates, deprecatedByIO, isExecutable, currFunctionName, inRegion, funcParNames, ompThreadPrivate, distrStateFromGUI, saveAllLocals, currMessages, errorCount); } } } @@ -3036,7 +3036,7 @@ static void addPrivates(SgStatement *st, set& privates, map, pair> &declaredArrays, map>> &declaratedArraysSt, map> &currMessages, - const vector ®ions, const map& keyValueFromGUI) + const vector ®ions, const map& distrStateFromGUI) { int countErrors = 0; @@ -3243,7 +3243,7 @@ int getAllDeclaredArrays(SgFile *file, map, pairarg(z), st, fName, z, true, commonBlocks, declaredArrays, declaratedArraysSt, privates, deprecatedByIO, isSgExecutableStatement(st) ? true : false, currFunctionName, - regNames, funcParNames, ompThreadPrivate, keyValueFromGUI, saveAllLocals, + regNames, funcParNames, ompThreadPrivate, distrStateFromGUI, saveAllLocals, currMessages, countErrors); } } @@ -3253,7 +3253,7 @@ int getAllDeclaredArrays(SgFile *file, map, pairexpr(i), st, "", -1, (st->variant() == ASSIGN_STAT && i == 0) ? true : false, commonBlocks, declaredArrays, declaratedArraysSt, privates, deprecatedByIO, isSgExecutableStatement(st) ? true : false, currFunctionName, - regNames, funcParNames, ompThreadPrivate, keyValueFromGUI, saveAllLocals, + regNames, funcParNames, ompThreadPrivate, distrStateFromGUI, saveAllLocals, currMessages, countErrors); } } @@ -3290,7 +3290,7 @@ int getAllDeclaredArrays(SgFile *file, map, pairexpr(i), st, "", -1, false, commonBlocks, declaredArrays, declaratedArraysSt, privates, deprecatedByIO, - false, "NULL", regNames, funcParNames, ompThreadPrivate, keyValueFromGUI, false, + false, "NULL", regNames, funcParNames, ompThreadPrivate, distrStateFromGUI, false, currMessages, countErrors); } st = st->lexNext(); @@ -3328,7 +3328,7 @@ int getAllDeclaredArrays(SgFile *file, map, pairexpr(i), curr, "", -1, false, commonBlocks, declaredArrays, declaratedArraysSt, privates, deprecatedByIO, - false, blockName, regNames, funcParNames, ompThreadPrivate, keyValueFromGUI, false, + false, blockName, regNames, funcParNames, ompThreadPrivate, distrStateFromGUI, false, currMessages, countErrors); curr = curr->lexNext(); } diff --git a/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.h b/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.h index 3a17ea7..8fd2db9 100644 --- a/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.h +++ b/sapfor/experts/Sapfor_2017/_src/LoopAnalyzer/loop_analyzer.h @@ -48,7 +48,7 @@ std::string getShortName(const std::tuple &uniqKe int getAllDeclaredArrays(SgFile *file, std::map, std::pair> &declaredArrays, std::map>> &declaratedArraysSt, std::map> &currMessages, - const std::vector ®ions, const std::map& keyValueFromGUI); + const std::vector ®ions, const std::map& distrStateFromGUI); void insertSpfAnalysisBeforeParalleLoops(const std::vector &loops); void recalculateArraySizes(std::set &arraysDone, const std::set &allArrays, const std::map> &arrayLinksByFuncCalls, const std::map>& allFuncInfo); int getSizeOfType(SgType* t); diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index 125f73b..f64f5e8 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -943,7 +943,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne } else if (curr_regime == GET_ALL_ARRAY_DECL) { - int err = getAllDeclaredArrays(file, declaredArrays, declaratedArraysSt, SPF_messages, subs_parallelRegions, keyValueFromGUI); + int err = getAllDeclaredArrays(file, declaredArrays, declaratedArraysSt, SPF_messages, subs_parallelRegions, distrStateFromGUI); if (err != 0) internalExit = -1; } @@ -1167,6 +1167,8 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne getMaxMinBlockDistribution(file, min_max_block); else if (curr_regime == CONVERT_TO_C) covertToC(file); + else if (curr_regime == INSERT_NO_DISTR_FLAGS_FROM_GUI) + addPrivatesToArraysFromGUI(file, declaredArrays, distrStateFromGUI); else if (curr_regime == TEST_PASS) { //test pass @@ -2485,6 +2487,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case PURE_INTENT_INSERT: 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: diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.h b/sapfor/experts/Sapfor_2017/_src/Sapfor.h index 0d87ccf..670cd9c 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.h +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.h @@ -172,6 +172,7 @@ enum passes { REMOVE_COMMENTS, GET_MIN_MAX_BLOCK_DIST, CONVERT_TO_C, + INSERT_NO_DISTR_FLAGS_FROM_GUI, TEST_PASS, EMPTY_PASS @@ -349,6 +350,7 @@ static void setPassValues() passNames[REMOVE_COMMENTS] = "REMOVE_COMMENTS"; passNames[GET_MIN_MAX_BLOCK_DIST] = "GET_MIN_MAX_BLOCK_DIST"; passNames[CONVERT_TO_C] = "CONVERT_TO_C"; + passNames[INSERT_NO_DISTR_FLAGS_FROM_GUI] = "INSERT_NO_DISTR_FLAGS_FROM_GUI"; passNames[TEST_PASS] = "TEST_PASS"; } diff --git a/sapfor/experts/Sapfor_2017/_src/SapforData.h b/sapfor/experts/Sapfor_2017/_src/SapforData.h index a94134e..7835946 100644 --- a/sapfor/experts/Sapfor_2017/_src/SapforData.h +++ b/sapfor/experts/Sapfor_2017/_src/SapforData.h @@ -158,7 +158,7 @@ std::pair inOnlyForloopOnPlace; // //cache for declaration arrays state switching -std::map keyValueFromGUI; +std::map distrStateFromGUI; // //for PROCESS_IO diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp index 9167653..67d96a1 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/checkpoints.cpp @@ -38,7 +38,7 @@ static SgType* createArrayCharType(int len, int dim) } static void findDecls(SgExpression* ex, vector& local, const map& localParams, - set& added, set& IntentInParams) + set& added, set& intentInParams) { if (ex) { @@ -48,7 +48,7 @@ static void findDecls(SgExpression* ex, vector& local, const map< localParams.find(ex->symbol()->identifier()) == localParams.end()) { if (added.find(ex->symbol()->identifier()) == added.end() && - IntentInadded.find(ex->symbol()->identifier()) == IntentInadded.end()) + intentInParams.find(ex->symbol()->identifier()) == intentInParams.end()) { added.insert(ex->symbol()->identifier()); local.push_back(ex); @@ -60,15 +60,15 @@ static void findDecls(SgExpression* ex, vector& local, const map< { if (ex->symbol()->variant() == VARIABLE_NAME && added.find(ex->symbol()->identifier()) == added.end() && - IntentInadded.find(ex->symbol()->identifier()) == IntentInadded.end()) + intentInParams.find(ex->symbol()->identifier()) == intentInParams.end()) { added.insert(ex->symbol()->identifier()); local.push_back(new SgArrayRefExp(*ex->symbol())); } } - findDecls(ex->lhs(), local, localParams, added, IntentInParams); - findDecls(ex->rhs(), local, localParams, added, IntentInParams); + findDecls(ex->lhs(), local, localParams, added, intentInParams); + findDecls(ex->rhs(), local, localParams, added, intentInParams); } } @@ -90,10 +90,10 @@ static void findLocalData(SgStatement* func, SgStatement* end, vectorlexNext(); FuncInfo* funcI = findFileInfoByName(func, allFuncInfo); - set IntentInParams; + set intentInParams; for (int i = 0; i < funcI->funcParams.countOfPars; ++i) if (funcI->funcParams.isArgIn(i) && !funcI->funcParams.isArgOut(i)) - IntentInParams.insert(funcI->funcParams.identificators[i]); + intentInParams.insert(funcI->funcParams.identificators[i]); for (SgStatement* st = start; st != end; st = st->lexNext()) { @@ -114,7 +114,7 @@ static void findLocalData(SgStatement* func, SgStatement* end, vectorlineNumber(), st->fileName(), tag[st->variant()]); if (st->variant() == VAR_DECL || st->variant() == VAR_DECL_90) - findDecls(st->expr(0), local, localParams, added, IntentInParams); + findDecls(st->expr(0), local, localParams, added, intentInParams); } } diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h index 1085084..3e68a6a 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -216,7 +216,7 @@ void InitPassesDependencies(map> &passDepsIn, set Pass(RESTORE_LOOP_FROM_ASSIGN) <= list({ SUBST_EXPR_AND_UNPARSE, SUBST_EXPR_RD_AND_UNPARSE }); - Pass(GET_ALL_ARRAY_DECL) <= Pass(CALL_GRAPH_IR); + Pass(GET_ALL_ARRAY_DECL) <= list({ CALL_GRAPH_IR, INSERT_NO_DISTR_FLAGS_FROM_GUI }); Pass(LOOP_GRAPH) <= Pass(PRIVATE_CALL_GRAPH_STAGE3) <= list(FIND_FUNC_TO_INCLUDE, PRIVATE_ANALYSIS_IR) <= list({ LOOP_ANALYZER_DATA_DIST_S0, LOOP_ANALYZER_DATA_DIST_S1, ONLY_ARRAY_GRAPH, LOOP_ANALYZER_ALIGNS }); diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp index 432dfd2..6a16906 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp @@ -4396,6 +4396,7 @@ void removeSpecialCommentsFromProject(SgFile* file) } } +//TODO: for gen_block and other variations, and redistributions dirs void getMaxMinBlockDistribution(SgFile* file, pair& min_max) { SgStatement* st = file->firstStatement(); @@ -4445,3 +4446,40 @@ void getMaxMinBlockDistribution(SgFile* file, pair& min_max) st = st->lexNext(); } } + +void addPrivatesToArraysFromGUI(SgFile* file, const map, pair>& declaredArrays, + const map& distrStateFromGUI) +{ + map> added; + + for (auto& arrayPair : declaredArrays) + { + DIST::Array* array = arrayPair.second.first; + string key = array->GetIndepUniqName(); + auto it = distrStateFromGUI.find(key); + if (it == distrStateFromGUI.end()) + continue; + + if (it->second != DIST::NO_DISTR) + continue; + + for (auto& decl : array->GetDeclInfoWithSymb()) + { + auto& place = decl.first; + const auto& symb = decl.second->GetOriginal(); + + if (place.first != file->filename()) + continue; + + SgStatement* declSt = SgStatement::getStatementByFileAndLine(place.first, place.second); + checkNull(declSt, convertFileName(__FILE__).c_str(), __LINE__); + + string toAdd = "!$SPF ANALYSIS(PRIVATE(" + string(symb->identifier()) + "))\n"; + added[declSt].insert(toAdd); + } + } + + for (auto& toInsert : added) + for (auto& elem : toInsert.second) + toInsert.first->addComment(elem.c_str()); +} \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h index 02b12e7..b952a5b 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h @@ -109,4 +109,6 @@ std::set getAllFilesInProject(); void LogIftoIfThen(SgStatement* stmt); void removeSpecialCommentsFromProject(SgFile* file); -void getMaxMinBlockDistribution(SgFile* file, std::pair& min_max); \ No newline at end of file +void getMaxMinBlockDistribution(SgFile* file, std::pair& min_max); + +void addPrivatesToArraysFromGUI(SgFile* file, const std::map, std::pair>& declaredArrays, const std::map& distrStateFromGUI); \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp index 0c6792f..303a273 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp @@ -1404,7 +1404,6 @@ pair, vector> splitCommandLineForParse(char** argv, int a for (int z = 0; z < argc; ++z) { string arg = argv[z]; - string ext = OnlyExt(arg.c_str()); convertToLower(ext); if (arg.find("*") != string::npos || checkFormat(ext, "")) @@ -1423,11 +1422,11 @@ pair, vector> splitCommandLineForParse(char** argv, int a { auto cmp_ext = file.extension().string(); convertToLower(cmp_ext); + if (cmp_ext.size() && cmp_ext[0] == '.') + cmp_ext.erase(cmp_ext.begin()); - if (checkFormat(cmp_ext, arg)) + if (checkFormat(cmp_ext, ext)) files.insert(file.filename().string()); - else - printf(" skip file %s\n", arg.c_str()); } #endif } diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 741a709..5d8326d 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 "2258" +#define VERSION_SPF "2262" diff --git a/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp b/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp index 15575dd..826c2cf 100644 --- a/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp +++ b/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp @@ -1560,7 +1560,7 @@ int SPF_GetFileLineInfo(void*& context, int winHandler, short *options, short *p return retSize; } -extern map keyValueFromGUI; +extern map distrStateFromGUI; int SPF_SetDistributionFlagToArray(void*& context, char *key, int flag) { MessageManager::clearCache(); @@ -1589,7 +1589,7 @@ int SPF_SetDistributionFlagToArray(void*& context, char *key, int flag) } } else - keyValueFromGUI[keyStr] = flag; + distrStateFromGUI[keyStr] = flag; } catch (...) { @@ -1654,7 +1654,7 @@ int SPF_SetDistributionFlagToArrays(void*& context, const char* keys, const char int flagI = -1; if (sscanf(flagsS[z].c_str(), "%d", &flagI) == -1) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - keyValueFromGUI[keysS[z]] = flagI; + distrStateFromGUI[keysS[z]] = flagI; } } } @@ -1887,6 +1887,14 @@ int SPF_SharedMemoryParallelization(void*& context, int winHandler, short* optio return simpleTransformPass(INSERT_PARALLEL_DIRS_NODIST, options, projName, folderName, output, outputSize, outputMessage, outputMessageSize); } +int SPF_InsertPrivateFromGUI(void*& context, int winHandler, short* options, short* projName, short* folderName, short*& output, + int*& outputSize, short*& outputMessage, int*& outputMessageSize) +{ + MessageManager::clearCache(); + MessageManager::setWinHandler(winHandler); + return simpleTransformPass(INSERT_NO_DISTR_FLAGS_FROM_GUI, options, projName, folderName, output, outputSize, outputMessage, outputMessageSize); +} + static inline void convertBackSlash(char *str, int strL) { for (int z = 0; z < strL; ++z) @@ -2597,6 +2605,8 @@ const wstring Sapfor_RunTransformation(const char* transformName_c, const char* retCode = SPF_ResolveCommonBlockConflicts(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize); else if (whichRun == "SPF_SharedMemoryParallelization") retCode = SPF_SharedMemoryParallelization(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize); + else if (whichRun == "SPF_InsertPrivateFromGUI") + retCode = SPF_InsertPrivateFromGUI(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize); else { if (showDebug) From 85aaee900bd6624fa0f6b9cace5c1eb8498c572c Mon Sep 17 00:00:00 2001 From: ALEXks Date: Thu, 28 Dec 2023 20:41:31 +0300 Subject: [PATCH 09/40] fixed omp analysis --- .../DirectiveProcessing/directive_parser.cpp | 28 +++++++++++++++++-- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_parser.cpp b/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_parser.cpp index 9e35898..029fd3b 100644 --- a/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_parser.cpp +++ b/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_parser.cpp @@ -721,6 +721,27 @@ static inline void addToAttribute(SgStatement* st, int var, vectorvariant() != FOR_NODE) + return false; + + SgStatement* lastNode = st->lastNodeOfStmt(); + for (SgStatement* op = st->lexNext(); st != lastNode; st = st->lexNext()) + { + if (st->variant() == ASSIGN_STAT) + { + SgExpression* ex = st->expr(0); + if (ex->variant() == ARRAY_REF || ex->variant() == VAR_REF) + if (var == ex->symbol()->identifier()) + return true; + } + } + return false; +} + vector parseOmpDirs(void* stIn, const set &globalPriv, bool forDo) { SgStatement* st = (SgStatement*)stIn; @@ -825,8 +846,11 @@ vector parseOmpDirs(void* stIn, const set &globalPriv, bool forD { vector list; for (auto& var : globalPriv) - list.push_back(new SgVarRefExp(findSymbolOrCreate(current_file, var, NULL, getFuncStat(st)))); - addToAttribute(st, ACC_PRIVATE_OP, list); + if (is_private_in_do(st, var)) + list.push_back(new SgVarRefExp(findSymbolOrCreate(current_file, var, NULL, getFuncStat(st)))); + + if (list.size()) + addToAttribute(st, ACC_PRIVATE_OP, list); } } diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 5d8326d..bbe67a9 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 "2262" +#define VERSION_SPF "2263" From 3cd16c9d0fb334ddf18ae694a039d84e3e15ddc9 Mon Sep 17 00:00:00 2001 From: mkoch Date: Tue, 9 Jan 2024 17:28:28 +0300 Subject: [PATCH 10/40] extract data flow analysys part from live variable analysis --- sapfor/experts/Sapfor_2017/CMakeLists.txt | 7 + .../CFGraph/DataFlow/backward_data_flow.h | 108 ++++++ .../_src/CFGraph/DataFlow/data_flow.h | 161 +++++++++ .../_src/CFGraph/live_variable_analysis.cpp | 308 +++++------------- 4 files changed, 356 insertions(+), 228 deletions(-) create mode 100644 sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h create mode 100644 sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h diff --git a/sapfor/experts/Sapfor_2017/CMakeLists.txt b/sapfor/experts/Sapfor_2017/CMakeLists.txt index fd246f3..0ad33fe 100644 --- a/sapfor/experts/Sapfor_2017/CMakeLists.txt +++ b/sapfor/experts/Sapfor_2017/CMakeLists.txt @@ -218,6 +218,11 @@ set(CFG _src/CFGraph/IR.cpp _src/CFGraph/private_variables_analysis.h ) +set(DATA_FLOW + _src/CFGraph/DataFlow/data_flow.h + _src/CFGraph/DataFlow/backward_data_flow.h + ) + set(CREATE_INTER_T _src/CreateInterTree/CreateInterTree.cpp _src/CreateInterTree/CreateInterTree.h) @@ -377,6 +382,7 @@ set(ZLIB ${zlib_sources}/src/adler32.c set(SOURCE_EXE ${CFG} + ${DATA_FLOW} ${CREATE_INTER_T} ${DIRA} ${DISTR} @@ -406,6 +412,7 @@ set(SOURCE_EXE add_executable(Sapfor_F ${SOURCE_EXE}) source_group (CFGraph FILES ${CFG}) +source_group (CFGraph\\DataFlow FILES ${DATA_FLOW}) source_group (Transformations\\ExpressionSubstitution FILES ${EXPR_TRANSFORM}) source_group (Transformations\\CheckPoints FILES ${TR_CP}) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h new file mode 100644 index 0000000..dd0b3c3 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h @@ -0,0 +1,108 @@ +#pragma once +#include "data_flow.h" + +#include +#include +#include + +#include "../../Utils/SgUtils.h" +#include "../CFGraph.h" +#include "../IR.h" +#include "../RD_subst.h" + +template +class BackwardDataFlowAnalysis : public DataFlowAnalysis { + std::vector reorderSequence(const std::vector& blocks, + const std::set back_edge_sources); +public: + void fit(const std::vector& blocks); +}; + +// minimizes the number of blocks beween the ends of back edges +template +std::vector BackwardDataFlowAnalysis::reorderSequence(const std::vector& blocks, + const std::set back_edge_sources) +{ + std::vector res = { }; + + auto blocks_end = blocks.rend(); + for (auto it = blocks.rbegin(); it < blocks_end; it++) + { + SAPFOR::BasicBlock* curr = *it; + auto res_end = res.end(); + auto inserter = res.begin(); + if (back_edge_sources.count(curr) == 0) + { + auto curr_next_begin = curr->getNext().begin(); + auto curr_next_end = curr->getNext().end(); + while (inserter < res_end && std::find(curr_next_begin, curr_next_end, *inserter) == curr_next_end) + inserter++; + } + + res.insert(inserter, curr); + } + + return res; +} + +template +void BackwardDataFlowAnalysis::fit(const std::vector& blocks) +{ + std::set> back_edges = {}; + + bool returned = false; + std::map> back_edges_by_src; + + auto blocks_sorted = sortCfgNodes(blocks, &back_edges); + + std::set back_edge_sources; + + for (auto& edge : back_edges) + { + back_edges_by_src[edge.first].insert(edge.second); + back_edge_sources.insert(edge.first); + } + + back_edges.clear(); + + blocks_sorted = reorderSequence(blocks_sorted, back_edge_sources); + back_edge_sources.clear(); + + std::reverse(blocks_sorted.begin(), blocks_sorted.end()); + + nodes.clear(); + std::map node_by_block; + + for (auto block : blocks_sorted) + { + NodeType* node = createNode(block); + nodes.push_back(node); + node_by_block[block] = node; + } + + int nodes_size = nodes.size(); + + for (int i = 0; i < nodes_size; i++) + { + NodeType* node = nodes[i]; + + auto back_edges_by_src_it = back_edges_by_src.find(node->getBlock()); + if (back_edges_by_src_it != back_edges_by_src.end()) + { + // This node is a source for back edge + for (auto dest : back_edges_by_src_it->second) + { + auto node_by_block_it = node_by_block.find(dest); + if (node_by_block_it != node_by_block.end()) + node_by_block_it->second->getRollback().insert(i); + } + } + + for (auto next : node->getBlock()->getNext()) + { + auto node_by_block_it = node_by_block.find(next); + if (node_by_block_it != node_by_block.end()) + node->getPrevBlocks().insert(node_by_block_it->second); + } + } +} \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h new file mode 100644 index 0000000..d4ac744 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h @@ -0,0 +1,161 @@ +#pragma once +#include +#include + +#include "../../Utils/SgUtils.h" +#include "../CFGraph.h" +#include "../IR.h" + +enum +{ + CNT_NOTINIT = 0 +}; + +template +class DataFlowAnalysisNode { + int in_cnt = CNT_NOTINIT, out_cnt = CNT_NOTINIT; + + std::set rollback; + std::set ignore_rollback; + + std::set*> prev_blocks; + + SAPFOR::BasicBlock* bb; +public: + DataFlowAnalysisNode(); + + void doStep(); + + virtual DataType getIn() = 0; + virtual DataType getOut() = 0; + + virtual bool addIn(const DataType& data) = 0; + virtual bool addOut(const DataType& data) = 0; + + virtual bool forwardData(const DataType& data) = 0; + + bool newerThan(const DataFlowAnalysisNode* block) const { return out_cnt > block->in_cnt; } + + int getInCnt() { return in_cnt; } + int getOutCnt() { return out_cnt; } + + void setInCnt(int cnt) { in_cnt = cnt; } + void setOutCnt(int cnt) { out_cnt = cnt; } + + std::set& getRollback() { return rollback; } + std::set& getIgnoreRollback() { return ignore_rollback; } + + std::set*>& getPrevBlocks() { return prev_blocks; } + + SAPFOR::BasicBlock* getBlock() { return bb; } + void setBlock(SAPFOR::BasicBlock* b) { bb = b; } +}; + +template +class DataFlowAnalysis { +protected: + std::vector nodes; + + virtual NodeType* createNode(SAPFOR::BasicBlock* block) = 0; +public: + virtual void fit(const std::vector& blocks) = 0; + void analyze(); + + const std::vector& getNodes() { return nodes; } + + ~DataFlowAnalysis(); +}; + +template +DataFlowAnalysisNode::DataFlowAnalysisNode() { + getRollback() = {}; + getIgnoreRollback() = {}; + prev_blocks = {}; +} + +template +void DataFlowAnalysisNode::doStep() +{ + int in_max_cnt = CNT_NOTINIT, out_max_cnt = CNT_NOTINIT; + for (auto next : prev_blocks) + { + if (in_cnt < next->out_cnt) + { + for (const auto& byOut : next->getOut()) + { + bool inserted = addIn({ byOut }); + + if (inserted) + { + if (next->out_cnt > in_max_cnt) + in_max_cnt = next->out_cnt; + + inserted = forwardData({ byOut }); + + if (inserted && next->out_cnt > out_max_cnt) + out_max_cnt = next->out_cnt; + } + } + } + } + + bool was_notinit = (out_cnt == CNT_NOTINIT); + + if (out_max_cnt != CNT_NOTINIT) + out_cnt = out_max_cnt; + + if (in_max_cnt != CNT_NOTINIT) + in_cnt = in_max_cnt; + + // TODO: fix counter overflow + if (was_notinit) + { + out_cnt++; + in_cnt++; + } +} + +template +void DataFlowAnalysis::analyze() { + auto curr = 0; + auto stop = nodes.size(); + + while (curr != stop) + { + auto curr_bb = nodes[curr]; + curr_bb->doStep(); + + const auto& jumps = curr_bb->getRollback(); + if (jumps.size() != 0) + { + auto& ignored_jumps = curr_bb->getIgnoreRollback(); + + bool jump = false; + for (const auto& jump_to : jumps) + { + if (ignored_jumps.insert(jump_to).second && curr_bb->newerThan(nodes[jump_to])) + { + jump = true; + curr = jump_to; + break; + } + } + + if (!jump) + curr_bb->getIgnoreRollback().clear(); + else + continue; + } + + curr++; + } +} + +template +DataFlowAnalysis::~DataFlowAnalysis() +{ + for (DataFlowAnalysisNode* node : nodes) + delete node; + + nodes.clear(); +} \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index 4b40ff9..3452e8e 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -1,5 +1,6 @@ #include "live_variable_analysis.h" #include "RD_subst.h" +#include "DataFlow/backward_data_flow.h" #include #include @@ -197,85 +198,67 @@ static void buildUseDef(SAPFOR::BasicBlock* block, set& use, vector& formal_parameters, vector& fcalls, const map& funcByName); -enum -{ - CNT_NOTINIT = 0 +class LiveVarAnalysisNode : public DataFlowAnalysisNode>> { +private: + set live, dead; +public: + map> getIn() + { + return getBlock()->getLiveOut(); + }; + + map> getOut() + { + return getBlock()->getLiveIn(); + }; + + bool addIn(const map>& data) + { + return getBlock()->addLiveOut(data); + }; + + bool addOut(const map>& data) + { + return getBlock()->addLiveIn(data); + }; + + bool forwardData(const map>& data) + { + bool inserted = false; + + for (const auto& byArg : data) + if (live.find(byArg.first) == live.end() && dead.find(byArg.first) == dead.end()) + inserted |= getBlock()->addLiveIn({ byArg }); + + return inserted; + }; + + LiveVarAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters, + vector& fcalls, const map& funcByName) + { + setBlock(block); + + buildUseDef(getBlock(), live, dead, formal_parameters, fcalls, funcByName); + + for (SAPFOR::Argument* arg : live) + getBlock()->addLiveIn({ { arg, { getBlock() } } }); + } }; -struct BasicBlockNode -{ - SAPFOR::BasicBlock* bb; - set live, dead; - int in_cnt, out_cnt; - set next_blocks; +class LiveVarAnalysis : public BackwardDataFlowAnalysis>, LiveVarAnalysisNode> { +protected: + vector& formal_parameters; + vector& fcalls; + const map& funcByName; - set::reverse_iterator> rollback; - set::reverse_iterator> ignore_rollback; - - BasicBlockNode(SAPFOR::BasicBlock* block, vector& formal_parameters, - vector& fcalls, const map& funcByName) + LiveVarAnalysisNode* createNode(SAPFOR::BasicBlock* block) override { - bb = block; - out_cnt = in_cnt = CNT_NOTINIT; - - buildUseDef(bb, live, dead, formal_parameters, fcalls, funcByName); - for (SAPFOR::Argument* arg : live) - bb->addLiveIn({ { arg, { bb } } }); - - rollback = {}; - ignore_rollback = {}; - next_blocks = {}; - } - - void updateLive() - { - bool in_changed = false, out_changed = false; - int in_max_cnt = CNT_NOTINIT, out_max_cnt = CNT_NOTINIT; - for (auto next : next_blocks) - { - if (out_cnt < next->in_cnt) - { - for (const auto& byArg : next->bb->getLiveIn()) - { - bool inserted = bb->addLiveOut({ byArg }); - out_changed |= inserted; - - if (inserted) - { - if (next->in_cnt > out_max_cnt) - out_max_cnt = next->in_cnt; - - if (live.find(byArg.first) == live.end() && dead.find(byArg.first) == dead.end()) - { - inserted = bb->addLiveIn({ byArg }); - if (inserted && next->in_cnt > in_max_cnt) - { - in_max_cnt = next->in_cnt; - in_changed = true; - } - } - } - } - } - } - - bool was_notinit = (in_cnt == CNT_NOTINIT); - - if (in_max_cnt != CNT_NOTINIT) - in_cnt = in_max_cnt; - - if (out_max_cnt != CNT_NOTINIT) - out_cnt = out_max_cnt; - - // TODO: fix counter overflow - if (was_notinit) - { - out_cnt++; - in_cnt++; - } - } - - bool newerThan(const BasicBlockNode* block) const { return in_cnt > block->out_cnt; } + return new LiveVarAnalysisNode(block, formal_parameters, fcalls, funcByName); + }; +public: + LiveVarAnalysis(vector& formal_parameters, vector& fcalls, + const map& funcByName) : formal_parameters(formal_parameters), fcalls(fcalls), funcByName(funcByName) + { }; }; //Build use and def sets of block. Result are stored in use and def @@ -381,137 +364,6 @@ static void buildUseDef(SAPFOR::BasicBlock* block, set& use, def = tmp_def; } -// minimizes the number of blocks beween the ends of back edges -static vector reorderSequence(const vector& blocks, - const set back_edge_sources) -{ - vector res = { }; - - auto blocks_end = blocks.rend(); - for (auto it = blocks.rbegin(); it < blocks_end; it++) - { - SAPFOR::BasicBlock* curr = *it; - auto res_end = res.end(); - auto inserter = res.begin(); - if (back_edge_sources.count(curr) == 0) - { - auto curr_next_begin = curr->getNext().begin(); - auto curr_next_end = curr->getNext().end(); - while (inserter < res_end && std::find(curr_next_begin, curr_next_end, *inserter) == curr_next_end) - inserter++; - } - - res.insert(inserter, curr); - } - - return res; -} - -// finds back edges, reorders and converts blocks into vector of BasicBlockNode* -// fills vector of formal parameters for given function -// fills info about arguments which becomes live after calls of functions -static vector toBlocksWithCnt(const vector& blocks, - vector& formal_parameters, - vector& fcalls, const map& funcByName) -{ - set> back_edges = {}; - - bool returned = false; - map> back_edges_by_src; - - auto blocks_sorted = sortCfgNodes(blocks, &back_edges); - - set back_edge_sources; - - for (auto& edge : back_edges) - { - back_edges_by_src[edge.first].insert(edge.second); - back_edge_sources.insert(edge.first); - } - - back_edges.clear(); - - blocks_sorted = reorderSequence(blocks_sorted, back_edge_sources); - back_edge_sources.clear(); - - vector blocks_with_counters; - map node_by_block; - for (auto block : blocks_sorted) - { - BasicBlockNode* node = new BasicBlockNode(block, formal_parameters, fcalls, funcByName); - blocks_with_counters.push_back(node); - node_by_block[block] = node; - } - - for (auto r_it = blocks_with_counters.rbegin(); r_it != blocks_with_counters.rend(); r_it++) - { - auto back_edges_by_src_it = back_edges_by_src.find((*r_it)->bb); - if (back_edges_by_src_it != back_edges_by_src.end()) - { - // This node is a source for back edge - for (auto dest : back_edges_by_src_it->second) - { - auto node_by_block_it = node_by_block.find(dest); - if (node_by_block_it != node_by_block.end()) - node_by_block_it->second->rollback.insert(r_it); - } - } - - for (auto next : (*r_it)->bb->getNext()) - { - auto node_by_block_it = node_by_block.find(next); - if (node_by_block_it != node_by_block.end()) - (*r_it)->next_blocks.insert(node_by_block_it->second); - } - } - - return blocks_with_counters; -} - -// iterate over separated subset of blocks -static void analyzeSequence(const vector& blocks_with_counters) -{ - auto curr = blocks_with_counters.rbegin(); - auto stop = blocks_with_counters.rend(); - - while (curr != stop) - { - auto curr_bb = *curr; - curr_bb->updateLive(); - - const auto& jumps = curr_bb->rollback; - if (jumps.size() != 0) - { - auto& ignored_jumps = curr_bb->ignore_rollback; - - bool jump = false; - for (const auto& jump_to : jumps) - { - if (ignored_jumps.insert(jump_to).second && curr_bb->newerThan(*jump_to)) - { - jump = true; - curr = jump_to; - break; - } - } - - if (!jump) - curr_bb->ignore_rollback.clear(); - else - continue; - } - - curr++; - } -} - -// delete all nodes from vector -static void freeBlocksWithCnt(const vector& blocks_with_counters) -{ - for (auto to_free : blocks_with_counters) - delete to_free; -} - // prints info about live variables void doDumpLive(const map>& CFGraph_for_project) { @@ -682,11 +534,11 @@ void runLiveVariableAnalysis(const map>& callDeps[byFunc.first].insert(byFunc.first->callsFromV.begin(), byFunc.first->callsFromV.end()); funcByName[byFunc.first->funcName] = byFunc.first; } - + vector> scc; vector> callLvls = groupByCallDependencies(callDeps, scc); - map> func_to_blocks_with_cnt; + map func_to_analysis_object; map> func_to_parameters; list> live_for_fcalls; @@ -707,8 +559,9 @@ void runLiveVariableAnalysis(const map>& auto& params = func_to_parameters[byFunc->funcName] = vector(byFunc->funcParams.countOfPars, NULL); - auto& blocks_with_cnt = (func_to_blocks_with_cnt[byFunc->funcName] = toBlocksWithCnt(itCFG->second, params, curr_fcalls, funcByName)); - analyzeSequence(blocks_with_cnt); + LiveVarAnalysis* analysis_object = (func_to_analysis_object[byFunc->funcName] = new LiveVarAnalysis(params, curr_fcalls, funcByName)); + analysis_object->fit(itCFG->second); + analysis_object->analyze(); fillLiveDeadArgs(byFunc, itCFG->second); } @@ -728,7 +581,7 @@ void runLiveVariableAnalysis(const map>& auto it = assembled_fcalls.find(call.func); if (it == assembled_fcalls.end()) it = assembled_fcalls.insert({ call.func, fcall(call.func, call.block, {}) }).first; - + for (const auto& p : call.live_after) it->second.live_after[p.first].insert(p.second.begin(), p.second.end()); @@ -738,12 +591,12 @@ void runLiveVariableAnalysis(const map>& for (const auto& func : assembled_fcalls) { - auto func_it = func_to_blocks_with_cnt.find(func.first->funcName); - if (func_it == func_to_blocks_with_cnt.end()) + auto func_it = func_to_analysis_object.find(func.first->funcName); + if (func_it == func_to_analysis_object.end()) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); auto param_it = func_to_parameters.find(func.first->funcName); - if(param_it == func_to_parameters.end()) + if (param_it == func_to_parameters.end()) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); const vector& params = param_it->second; @@ -761,14 +614,14 @@ void runLiveVariableAnalysis(const map>& } } - set exits; + set exits; int max_cnt = CNT_NOTINIT; - for (auto block : func_it->second) + for (auto block : func_it->second->getNodes()) { - if (block->bb->getNext().size() == 0) + if (block->getBlock()->getNext().size() == 0) exits.insert(block); - if (block->out_cnt > max_cnt) - max_cnt = block->out_cnt; + if (block->getInCnt() > max_cnt) + max_cnt = block->getInCnt(); } max_cnt++; @@ -777,23 +630,22 @@ void runLiveVariableAnalysis(const map>& { for (const auto& byArg : live_after) { - if (exit->bb->addLiveOut({ byArg })) + if (exit->addIn({ byArg })) { - exit->out_cnt = max_cnt; - if (exit->live.find(byArg.first) == exit->live.end() && exit->dead.find(byArg.first) == exit->dead.end()) - if (exit->bb->addLiveIn({ byArg })) - exit->in_cnt = max_cnt; + exit->setInCnt(max_cnt); + if (exit->forwardData({ byArg })) + exit->setOutCnt(max_cnt); } } } - + // now we can update live sets in all blocks - analyzeSequence(func_it->second); + func_it->second->analyze(); } } - for (const auto& nodeByFunc : func_to_blocks_with_cnt) - freeBlocksWithCnt(nodeByFunc.second); + for (const auto& byFunc : func_to_analysis_object) + delete byFunc.second; for (auto& byFunc : CFGraph_for_project) for (auto& byBlock : byFunc.second) From 5f8940f34d032a593ed913be4cbcc9e1cd7ec354 Mon Sep 17 00:00:00 2001 From: mkoch Date: Tue, 9 Jan 2024 18:13:56 +0300 Subject: [PATCH 11/40] reorganize live variable analysis to use it's functions further --- .../_src/CFGraph/live_variable_analysis.cpp | 351 ++++++++++-------- .../_src/CFGraph/live_variable_analysis.h | 35 ++ 2 files changed, 227 insertions(+), 159 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index 3452e8e..ba8a0ae 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -17,6 +17,8 @@ using std::set; using std::unordered_map; using std::list; +using LIVE_VARIABLES::fcall; + namespace SAPFOR { bool BasicBlock::addLive(const map>& to_add, bool in) { @@ -28,7 +30,7 @@ namespace SAPFOR { const set& add_in_live = byNew.second; set new_in_live = {}; - + auto current_set_iter = current_set.find(byNew.first); if (current_set_iter == current_set.end()) @@ -106,7 +108,7 @@ namespace SAPFOR return res; } - + void BasicBlock::compressLives() { for (auto& bySrc : { &live_in, &live_out, &live_inout }) for (auto& byArg : *bySrc) @@ -114,89 +116,77 @@ namespace SAPFOR } } -struct fcall +bool fcall::tryInsert(set& dest, SAPFOR::BasicBlock* b) { -private: - bool tryInsert(set& dest, SAPFOR::BasicBlock* b) + if (b == block || dest.find(block) == dest.end()) { - if (b == block || dest.find(block) == dest.end()) - { - dest.insert(b); - return true; - } - - return false; + dest.insert(b); + return true; } -public: - FuncInfo* func; - - map> live_after; - set dead_after; + return false; +} - map> commons_live_after; - set commons_dead_after; +fcall::fcall(FuncInfo* f, SAPFOR::BasicBlock* b, const vector& p) +{ + block = b; + func = f; - vector params; - SAPFOR::BasicBlock* block; + int param_size = p.size(); + params = vector(param_size, NULL); - fcall(FuncInfo* f, SAPFOR::BasicBlock* b, const vector& p) - { - block = b; - func = f; - - int param_size = p.size(); - params = vector(param_size, NULL); + for (int i = 0; i < param_size; i++) + if (f->funcParams.isArgOut(i)) + params[i] = p[i]; +} - for (int i = 0; i < param_size; i++) - if (f->funcParams.isArgOut(i)) - params[i] = p[i]; +void fcall::make_live(SAPFOR::Argument* arg, SAPFOR::BasicBlock* b) +{ + if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::COMMON_ || arg->getMemType() == SAPFOR::CFG_MEM_TYPE::MODULE_) + { + if (commons_dead_after.find(arg) == commons_dead_after.end()) + tryInsert(commons_live_after[arg], b); } - void make_live(SAPFOR::Argument* arg, SAPFOR::BasicBlock* b) + auto it = find(params.begin(), params.end(), arg); + if (it != params.end()) { - if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::COMMON_ || arg->getMemType() == SAPFOR::CFG_MEM_TYPE::MODULE_) - { - if (commons_dead_after.find(arg) == commons_dead_after.end()) - tryInsert(commons_live_after[arg], b); - } - - auto it = find(params.begin(), params.end(), arg); - if (it != params.end()) - { - int idx = it - params.begin(); - if (dead_after.find(idx) == dead_after.end()) - tryInsert(live_after[idx], b); - } + int idx = it - params.begin(); + if (dead_after.find(idx) == dead_after.end()) + tryInsert(live_after[idx], b); } +} - void make_dead(SAPFOR::Argument* arg) +void fcall::make_dead(SAPFOR::Argument* arg) +{ + if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::COMMON_ || arg->getMemType() == SAPFOR::CFG_MEM_TYPE::MODULE_) { - if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::COMMON_ || arg->getMemType() == SAPFOR::CFG_MEM_TYPE::MODULE_) + if (commons_live_after.find(arg) == commons_live_after.end()) commons_dead_after.insert(arg); - - auto it = find(params.begin(), params.end(), arg); - if (it != params.end()) - { - int idx = it - params.begin(); - dead_after.insert(idx); - } } - void updateFromOut() + auto it = find(params.begin(), params.end(), arg); + if (it != params.end()) { - for (const auto& p : block->getLiveOut()) - for (auto b : p.second) - make_live(p.first, b); + int idx = it - params.begin(); + if (live_after.find(idx) == live_after.end()) + dead_after.insert(idx); } -}; +} + +void fcall::updateFromOut() +{ + for (const auto& p : block->getLiveOut()) + for (auto b : p.second) + make_live(p.first, b); +} static bool getLiveDead(const vector& params, const string& func_name, set& live, set& dead); static void buildUseDef(SAPFOR::BasicBlock* block, set& use, set& def, - vector& formal_parameters, vector& fcalls, - const map& funcByName); + vector& formal_parameters, vector& fcalls, + const map& funcByName, bool interprocedural); class LiveVarAnalysisNode : public DataFlowAnalysisNode>> { private: @@ -234,11 +224,11 @@ public: }; LiveVarAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters, - vector& fcalls, const map& funcByName) + vector& fcalls, const map& funcByName) { setBlock(block); - buildUseDef(getBlock(), live, dead, formal_parameters, fcalls, funcByName); + buildUseDef(getBlock(), live, dead, formal_parameters, fcalls, funcByName, true); for (SAPFOR::Argument* arg : live) getBlock()->addLiveIn({ { arg, { getBlock() } } }); @@ -251,8 +241,7 @@ protected: vector& fcalls; const map& funcByName; - LiveVarAnalysisNode* createNode(SAPFOR::BasicBlock* block) override - { + LiveVarAnalysisNode* createNode(SAPFOR::BasicBlock* block) override { return new LiveVarAnalysisNode(block, formal_parameters, fcalls, funcByName); }; public: @@ -261,107 +250,151 @@ public: { }; }; -//Build use and def sets of block. Result are stored in use and def -static void buildUseDef(SAPFOR::BasicBlock* block, set& use, set& def, - vector& formal_parameters, vector& fcalls, - const map& funcByName) +void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, + set& use, set& def, + vector& formal_parameters, vector& fcalls, + vector& lastParamRef, int& last_param_ref_index, int& last_param_ref_size, + string& fName, const map& funcByName, bool interprocedural) { - set tmp_use, tmp_def; + for (auto arg : { instr->getArg1(), instr->getArg2(), instr->getResult() }) + if (arg && arg->getMemType() == SAPFOR::CFG_MEM_TYPE::FUNC_PARAM_) + formal_parameters[getParamIndex(arg, formal_parameters.size())] = arg; - vector lastParamRef; - bool fcall_added; + SAPFOR::Argument* res_arg = NULL; - for (auto ir_block : block->getInstructions()) + static const set skip = { SAPFOR::CFG_OP::ENTRY }; + SAPFOR::CFG_OP instr_operation = instr->getOperation(); + if (hasStoreStructure(instr_operation)) { - fcall_added = false; - SAPFOR::Instruction* instr = ir_block->getInstruction(); - for (auto arg : { instr->getArg1(), instr->getArg2(), instr->getResult() }) - if (arg && arg->getMemType() == SAPFOR::CFG_MEM_TYPE::FUNC_PARAM_) - formal_parameters[getParamIndex(arg, formal_parameters.size())] = arg; + res_arg = instr->getArg1(); + set instr_args = { instr->getResult(), instr->getArg2() }; + use.insert(instr_args.begin(), instr_args.end()); + } + else if (instr_operation == SAPFOR::CFG_OP::PARAM) + { + lastParamRef[last_param_ref_index--] = instr->getArg1(); + } + else if (instr_operation == SAPFOR::CFG_OP::F_CALL) + { + res_arg = instr->getResult(); - set res = {}, args = {}; - SAPFOR::Argument* res_arg = NULL; + last_param_ref_size = stoi(instr->getArg2()->getValue()); - static const set skip = { SAPFOR::CFG_OP::ENTRY }; - SAPFOR::CFG_OP instr_operation = instr->getOperation(); - if (hasStoreStructure(instr_operation)) { - res_arg = instr->getArg1(); - std::set instr_args = { instr->getResult(), instr->getArg2()}; - insertIfVar(instr_args.begin(), instr_args.end(), args); - } - else if (instr_operation == SAPFOR::CFG_OP::PARAM) - lastParamRef.push_back(instr->getArg1()); - else if (instr_operation == SAPFOR::CFG_OP::F_CALL) + lastParamRef.clear(); + lastParamRef.resize(last_param_ref_size); + + last_param_ref_index = last_param_ref_size - 1; + + fName = instr->getArg1()->getValue(); + } + else if (skip.find(instr_operation) == skip.end()) + { + //default + res_arg = instr->getResult(); + std::set intr_args = { instr->getArg1(), instr->getArg2() }; + use.insert(intr_args.begin(), intr_args.end()); + } + else + { + //skip + return; + } + + if ((instr_operation == SAPFOR::CFG_OP::F_CALL || instr_operation == SAPFOR::CFG_OP::PARAM) && last_param_ref_index < 0) { + auto func_it = funcByName.find(fName); + if (func_it != funcByName.end()) { - res_arg = instr->getResult(); + fcalls.push_back(fcall(func_it->second, block, lastParamRef)); - int count = stoi(instr->getArg2()->getValue()); - if (lastParamRef.size() != count) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + auto r_it = fcalls.rbegin(); + auto r_end = fcalls.rend(); - const string& fName = instr->getArg1()->getValue(); - auto func_it = funcByName.find(fName); - if (func_it != funcByName.end()) - { - fcalls.push_back(fcall(func_it->second, block, lastParamRef)); - fcall_added = true; - } - - set make_live, make_dead; - if (fName == "_READ") - res.insert(lastParamRef.begin(), lastParamRef.end()); - else if (getLiveDead(lastParamRef, fName, make_live, make_dead)) - { - insertIfVar(make_live.begin(), make_live.end(), args); - insertIfVar(make_dead.begin(), make_dead.end(), res); - } - else - insertIfVar(lastParamRef.begin(), lastParamRef.end(), args); - - lastParamRef.clear(); - } - else if(skip.find(instr_operation) == skip.end()) - { - //default - res_arg = instr->getResult(); - std::set intr_args = { instr->getArg1(), instr->getArg2() }; - insertIfVar(intr_args.begin(), intr_args.end(), args); - } - else { - //skip - continue; - } - - if (res_arg && res_arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR) - res.insert(res_arg); - - for (auto e : args) - if (!tmp_def.count(e)) - tmp_use.insert(e); - - for(auto e : res) - if (!tmp_use.count(e)) - tmp_def.insert(e); - - auto r_it = fcalls.rbegin(); - auto r_end = fcalls.rend(); - if (fcall_added && r_it != r_end) - r_it++; - - while (r_it != r_end && r_it->block == block) - { - for (auto e : args) - r_it->make_live(e, block); - - for (auto e : res) + for (auto e : def) r_it->make_dead(e); - r_it++; + for (auto e : use) + r_it->make_live(e, block); + } + + set make_live, make_dead; + if (fName == "_READ") + def.insert(lastParamRef.begin(), lastParamRef.end()); + else if (interprocedural && getLiveDead(lastParamRef, fName, make_live, make_dead)) + { + use.insert(make_live.begin(), make_live.end()); + def.insert(make_dead.begin(), make_dead.end()); + } + else + use.insert(lastParamRef.begin(), lastParamRef.end()); + + last_param_ref_index = 0; + last_param_ref_size = 0; + + lastParamRef.clear(); + fName = ""; + } + + if (res_arg) + def.insert(res_arg); +} + +static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, + set& use, set& def, + vector& formal_parameters, vector& fcalls, + vector& lastParamRef, int& last_param_ref_index, int& last_param_ref_size, + string& fName, const map& funcByName, bool interprocedural) +{ + set res, args; + + getUseDefForInstruction(block, instr, + args, res, + formal_parameters, fcalls, + lastParamRef, last_param_ref_index, last_param_ref_size, + fName, funcByName, + interprocedural + ); + + for (auto e : res) + { + if (e && e->getType() == SAPFOR::CFG_ARG_TYPE::VAR) + { + def.insert(e); + use.erase(e); } } - use = tmp_use; - def = tmp_def; + for (auto e : args) + { + if (e && e->getType() == SAPFOR::CFG_ARG_TYPE::VAR) + { + use.insert(e); + def.erase(e); + } + } +} + +//Build use and def sets of block. Result are stored in use and def +static void buildUseDef(SAPFOR::BasicBlock* block, set& use, set& def, + vector& formal_parameters, vector& fcalls, + const map& funcByName, bool interprocedural) +{ + vector lastParamRef; + int last_param_ref_index = 0, last_param_ref_size = 0; + string fName; + + const auto& instructions = block->getInstructions(); + auto ir_block_it = instructions.rbegin(), ir_block_end = instructions.rend(); + + for (; ir_block_it != ir_block_end; ir_block_it++) + { + updateUseDefForInstruction(block, (*ir_block_it)->getInstruction(), + use, def, + formal_parameters, fcalls, + lastParamRef, last_param_ref_index, last_param_ref_size, + fName, funcByName, + interprocedural + ); + } } // prints info about live variables @@ -379,7 +412,7 @@ void doDumpLive(const map>& CFGraph_for_p for (const auto& live : byBB->getLiveIn()) { __spf_print(1, " %s:", live.first->getValue().c_str()); - for(auto use : live.second) + for (auto use : live.second) __spf_print(1, " %d", use->getNumber()); __spf_print(1, "\n"); } @@ -482,8 +515,8 @@ static void fillLiveDeadArgs(const FuncInfo* func, const vector& params, - const map, set>>& params_and_globals, - const string& func_name, set& result) + const map, set>>& params_and_globals, + const string& func_name, set& result) { auto globals_it = params_and_globals.find(func_name); @@ -498,7 +531,7 @@ bool joinGlobalsWithParameters(const vector& params, for (int idx : param_indexes) { if (idx < params_size) { - if(params[idx] && params[idx]->getType() == SAPFOR::CFG_ARG_TYPE::VAR) + if (params[idx] && params[idx]->getType() == SAPFOR::CFG_ARG_TYPE::VAR) result.insert(params[idx]); } else @@ -510,11 +543,11 @@ bool joinGlobalsWithParameters(const vector& params, } // fill sets of arguments wich becomes live or dead after call with parameters params -static bool getLiveDead(const vector& params, const string& func_name, - set& live, set& dead) +static bool getLiveDead(const vector& params, const string& func_name, + set& live, set& dead) { return joinGlobalsWithParameters(params, live_by_func, func_name, live) && - joinGlobalsWithParameters(params, dead_by_func, func_name, dead); + joinGlobalsWithParameters(params, dead_by_func, func_name, dead); } // entrypoint for live variable analysis pass diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h index 52e183b..a697273 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h @@ -1,6 +1,35 @@ +#pragma once + #include "../Utils/SgUtils.h" #include "CFGraph.h" +namespace LIVE_VARIABLES +{ + struct fcall + { + private: + bool tryInsert(std::set& dest, SAPFOR::BasicBlock* b); + public: + FuncInfo* func; + + std::map> live_after; + std::set dead_after; + + std::map> commons_live_after; + std::set commons_dead_after; + + std::vector params; + SAPFOR::BasicBlock* block; + + fcall(FuncInfo* f, SAPFOR::BasicBlock* b, const std::vector& p); + + void make_live(SAPFOR::Argument* arg, SAPFOR::BasicBlock* b); + void make_dead(SAPFOR::Argument* arg); + + void updateFromOut(); + }; +} + template void insertIfVar(IT begin, IT end, DEST& to) { for (auto it = begin; it != end; it++) @@ -8,6 +37,12 @@ void insertIfVar(IT begin, IT end, DEST& to) { to.insert(*it); }; +void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, + std::set& use, std::set& def, + std::vector& formal_parameters, std::vector& fcalls, + std::vector& lastParamRef, int& last_param_ref_index, int& last_param_ref_size, + std::string& fName, const std::map& funcByName, bool interprocedural); + void runLiveVariableAnalysis(const std::map>& CFGraph_for_project); void doDumpLive(const std::map>& CFGraph_for_project); From 14219cdfd58e96cb6d34f086d7a25de115739d29 Mon Sep 17 00:00:00 2001 From: mkoch Date: Tue, 9 Jan 2024 18:28:50 +0300 Subject: [PATCH 12/40] add REMOVE_DEAD_CODE_AND_UNPARSE (debug) pass --- sapfor/experts/Sapfor_2017/CMakeLists.txt | 6 +- .../Sapfor_2017/_src/CFGraph/CFGraph.h | 4 + .../_src/CFGraph/live_variable_analysis.cpp | 31 ++ sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 8 + sapfor/experts/Sapfor_2017/_src/Sapfor.h | 2 + .../_src/Transformations/dead_code.cpp | 436 ++++++++++++++++++ .../_src/Transformations/dead_code.h | 14 + .../Sapfor_2017/_src/Utils/PassManager.h | 2 + 8 files changed, 502 insertions(+), 1 deletion(-) create mode 100644 sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp create mode 100644 sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.h diff --git a/sapfor/experts/Sapfor_2017/CMakeLists.txt b/sapfor/experts/Sapfor_2017/CMakeLists.txt index 0ad33fe..94433f2 100644 --- a/sapfor/experts/Sapfor_2017/CMakeLists.txt +++ b/sapfor/experts/Sapfor_2017/CMakeLists.txt @@ -160,7 +160,9 @@ set(PARALLEL_REG _src/ParallelizationRegions/ParRegions.cpp _src/ParallelizationRegions/expand_extract_reg.h _src/ParallelizationRegions/resolve_par_reg_conflicts.cpp _src/ParallelizationRegions/resolve_par_reg_conflicts.h) - + +set(TR_DEAD_CODE _src/Transformations/dead_code.cpp + _src/Transformations/dead_code.h) set(TR_CP _src/Transformations/checkpoints.cpp _src/Transformations/checkpoints.h) set(TR_VECTOR _src/Transformations/array_assign_to_loop.cpp @@ -191,6 +193,7 @@ set(TR_CONV _src/Transformations/convert_to_c.cpp _src/Transformations/convert_to_c.h) set(TRANSFORMS + ${TR_DEAD_CODE} ${TR_CP} ${TR_VECTOR} ${TR_ENDDO_LOOP} @@ -414,6 +417,7 @@ add_executable(Sapfor_F ${SOURCE_EXE}) source_group (CFGraph FILES ${CFG}) source_group (CFGraph\\DataFlow FILES ${DATA_FLOW}) +source_group (Transformations\\DeadCodeRemoving FILES ${TR_DEAD_CODE}) source_group (Transformations\\ExpressionSubstitution FILES ${EXPR_TRANSFORM}) source_group (Transformations\\CheckPoints FILES ${TR_CP}) source_group (Transformations\\LoopEndDoConverter FILES ${TR_ENDDO_LOOP}) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h index 99b98dd..dbe790d 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h @@ -33,6 +33,7 @@ namespace SAPFOR bool addLive(const std::map>& to_add, bool in); std::map> getLive(bool in) const; + bool removeLive(SAPFOR::Argument* to_remove, bool in); public: BasicBlock() { num = lastNumBlock++; } BasicBlock(IR_Block* item); @@ -71,6 +72,9 @@ namespace SAPFOR */ bool addLiveIn(const std::map>& to_add) { return addLive(to_add, true); }; bool addLiveOut(const std::map>& to_add) { return addLive(to_add, false); }; + + bool removeLiveIn(SAPFOR::Argument* to_remove) { return removeLive(to_remove, true); }; + bool removeLiveOut(SAPFOR::Argument* to_remove) { return removeLive(to_remove, false); }; std::map> getLiveIn() const { return getLive(true); }; std::map> getLiveOut() const { return getLive(false); }; diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index ba8a0ae..ccd933e 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -97,6 +97,37 @@ namespace SAPFOR return inserted; }; + bool BasicBlock::removeLive(SAPFOR::Argument* to_remove, bool in) + { + std::map>& current_set = (in ? live_in : live_out); + std::map>& opposite_set = (!in ? live_in : live_out); + + bool removed = false; + + removed |= (current_set.erase(to_remove) != 0); + + if (!removed) + { + auto it = live_inout.find(to_remove); + + if (it != live_inout.end()) + { + auto& dest = opposite_set[to_remove]; + for (SAPFOR::BasicBlock* bb : it->second) + { + auto find_bb_from_dest = std::lower_bound(dest.begin(), dest.end(), bb); + + if (find_bb_from_dest == dest.end() || *find_bb_from_dest != bb) + dest.insert(find_bb_from_dest, bb); + } + + removed = true; + } + } + + return removed; + }; + map> BasicBlock::getLive(bool in) const { auto& current_set = in ? live_in : live_out; diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index f64f5e8..4a006d0 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -79,6 +79,7 @@ #include "Transformations/private_removing.h" #include "Transformations/fix_common_blocks.h" #include "Transformations/convert_to_c.h" +#include "Transformations/dead_code.h" #include "RenameSymbols/rename_symbols.h" #include "ProjectParameters/projectParameters.h" @@ -1169,6 +1170,12 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne covertToC(file); else if (curr_regime == INSERT_NO_DISTR_FLAGS_FROM_GUI) addPrivatesToArraysFromGUI(file, declaredArrays, distrStateFromGUI); + else if (curr_regime == REMOVE_DEAD_CODE_AND_UNPARSE) + { + auto funcsForFile = getObjectForFileFromMap(file_name, allFuncInfo_IR); + for (auto& func : funcsForFile) + removeDeadCode(func->funcPointer, allFuncInfo_IR, commonBlocks); + } else if (curr_regime == TEST_PASS) { //test pass @@ -2515,6 +2522,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam findFunctionsToInclude(true); break; // all these cases run UNPARSE_FILE after + case REMOVE_DEAD_CODE_AND_UNPARSE: case RENAME_SYMBOLS: case RESOLVE_PAR_REGIONS: case CREATE_PARALLEL_REGIONS: diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.h b/sapfor/experts/Sapfor_2017/_src/Sapfor.h index 670cd9c..a66f577 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.h +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.h @@ -165,6 +165,7 @@ enum passes { BUILD_IR, LIVE_ANALYSIS_IR, PRIVATE_ANALYSIS_IR, + REMOVE_DEAD_CODE_AND_UNPARSE, FIX_COMMON_BLOCKS, REMOVE_OMP_DIRS, @@ -343,6 +344,7 @@ static void setPassValues() passNames[CALL_GRAPH_IR] = "CALL_GRAPH_IR"; passNames[LIVE_ANALYSIS_IR] = "LIVE_ANALYSIS_IR"; passNames[PRIVATE_ANALYSIS_IR] = "PRIVATE_ANALYSIS_IR"; + passNames[REMOVE_DEAD_CODE_AND_UNPARSE] = "REMOVE_DEAD_CODE_AND_UNPARSE"; passNames[FIX_COMMON_BLOCKS] = "FIX_COMMON_BLOCKS"; passNames[REMOVE_OMP_DIRS] = "REMOVE_OMP_DIRS"; diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp new file mode 100644 index 0000000..3c9c548 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -0,0 +1,436 @@ +#include "dead_code.h" + +#include +#include +#include +#include + +using std::map; +using std::string; +using std::vector; +using std::set; + +static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, + set& use, set& def, + vector& formal_parameters, + vector& lastParamRef, int& last_param_ref_index, int& last_param_ref_size, + string& fName, + bool& useful, bool& last_fcall_useful, + set& usedByThisBlock) +{ + set res, args; + + if (fName == "") + last_fcall_useful = false; + + vector fcalls; + + + getUseDefForInstruction(block, instr, + args, res, + formal_parameters, fcalls, + lastParamRef, last_param_ref_index, last_param_ref_size, + fName, {}, + false + ); + + if (!useful) + { + for (SAPFOR::Argument* r : res) + { + if (use.find(r) != use.end() || r->getMemType() != SAPFOR::CFG_MEM_TYPE::LOCAL_) + { + useful = true; + break; + } + } + } + + if (!useful) + { + set always_useful = + { + SAPFOR::CFG_OP::POINTER_ASS, + SAPFOR::CFG_OP::STORE, + SAPFOR::CFG_OP::REC_REF_STORE, + SAPFOR::CFG_OP::RANGE, + SAPFOR::CFG_OP::ENTRY, + SAPFOR::CFG_OP::EXIT, + SAPFOR::CFG_OP::DVM_DIR, + SAPFOR::CFG_OP::SPF_DIR, + + SAPFOR::CFG_OP::F_CALL //TODO: handle pure functions + }; + + if (always_useful.find(instr->getOperation()) != always_useful.end()) + useful = true; + else if(instr->getOperation() == SAPFOR::CFG_OP::PARAM && last_fcall_useful) + useful = true; + } + + if (useful) + { + if (instr->getOperation() == SAPFOR::CFG_OP::F_CALL) + last_fcall_useful = true; + + for (auto e : res) + { + def.insert(e); + use.erase(e); + } + + for (auto e : args) + { + use.insert(e); + def.erase(e); + } + + insertIfVar(args.begin(), args.end(), usedByThisBlock); + } +} + + +//Build use and def sets of block. Result are stored in use and def +static void buildUseDef(SAPFOR::BasicBlock* block, set& use, set& def, + vector& formal_parameters, vector& useful, + set& usedByThisBlock) +{ + set use_with_regs = use, def_with_regs = def; + + vector lastParamRef; + int last_param_ref_index = 0, last_param_ref_size = 0; + string fName = ""; + bool last_fcall_useful = false; + + const auto& instructions = block->getInstructions(); + int instr_size = instructions.size(); + + for (int i = instr_size - 1; i >= 0; i--) + { + bool u = useful[i]; + + updateUseDefForInstruction(block, instructions[i]->getInstruction(), + use_with_regs, def_with_regs, + formal_parameters, + lastParamRef, last_param_ref_index, last_param_ref_size, + fName, + u, last_fcall_useful, + usedByThisBlock + ); + + useful[i] = u; + } + + insertIfVar(use_with_regs.begin(), use_with_regs.end(), use); + insertIfVar(def_with_regs.begin(), def_with_regs.end(), def); +} + + +class DeadCodeAnalysisNode : public DataFlowAnalysisNode>> { +private: + vector useful; + bool useful_block = false; + set next_notempty_in, next_notempty_out; + bool useful_jump = false; + + vector& formal_parameters; +public: + bool updateJumpStatus() + { + bool res = false; + const auto& prev = getPrevBlocks(); + if (prev.size() > 1 && !useful.back() && + getBlock()->getInstructions().back()->getInstruction()->getOperation() == SAPFOR::CFG_OP::JUMP_IF) + { + const auto& example = dynamic_cast(*prev.begin())->next_notempty_out; + + for (auto p : prev) + { + DeadCodeAnalysisNode* next = dynamic_cast(p); + + if (next->next_notempty_out != example) + { + useful.back() = true; + res = true; + break; + } + } + } + + return res; + } + + bool updateNextNotEmpty() + { + bool updated = false; + + if(!useful_block) + { + set current = {}; + + for (auto nextP : getPrevBlocks()) + { + DeadCodeAnalysisNode* next = dynamic_cast(nextP); + + if(!next) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + current.insert(next->next_notempty_out.begin(), next->next_notempty_out.end()); + } + + if (current != next_notempty_in) + { + next_notempty_in = current; + updated = true; + } + } + + return updated; + } + + map> getIn() { + return getBlock()->getLiveOut(); + }; + + map> getOut() { + return getBlock()->getLiveIn(); + }; + + bool addIn(const map>& data) { + bool inserted = getBlock()->addLiveOut(data); + + if (!useful_block) + inserted |= updateNextNotEmpty(); + + inserted |= updateJumpStatus(); + + return inserted; + }; + + bool addOut(const map>& data) { + return getBlock()->addLiveIn(data); + }; + + bool forwardData(const map>& data) { + bool inserted = false; + SAPFOR::BasicBlock* bb= getBlock(); + + set use, def; + + for (const auto& byArg : data) + use.insert(byArg.first); + + set usedByThisBlock; + buildUseDef(bb, use, def, this->formal_parameters, useful, usedByThisBlock); + + auto in = bb->getLiveIn(), out = bb->getLiveOut(); + + for (SAPFOR::Argument* arg : use) + { + bool this_block = usedByThisBlock.find(arg) != usedByThisBlock.end(); + + if (!this_block) + { + auto data_it = data.find(arg); + + if (data_it == data.end()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + inserted |= bb->addLiveIn({ *data_it }); + } + else + { + auto in_it = in.find(arg); + bool skip = false; + if (in_it != in.end()) + { + if (in_it->second.size() == 1 && *(in_it->second.begin()) == bb) + skip = true; // nothing to do, inserted = false + else + bb->removeLiveIn(arg); + } + if(!skip) + inserted |= bb->addLiveIn({ { arg, { bb } } }); + } + } + + if(!useful_block) + { + for(bool status : useful) + { + if (status) + { + useful_block = true; + + inserted = true; + next_notempty_out = { this }; + break; + } + } + } + + if (!useful_block) + { + if (next_notempty_in != next_notempty_out) + { + inserted = true; + next_notempty_out = next_notempty_in; + } + } + + return inserted; + }; + + DeadCodeAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters) : + formal_parameters(formal_parameters) + { + setBlock(block); + useful.resize(block->getInstructions().size(), false); + set use, def; + set usedByThisBlock; + + buildUseDef(getBlock(), use, def, this->formal_parameters, useful, usedByThisBlock); + + for (SAPFOR::Argument* arg : use) + getBlock()->addLiveIn({ { arg, { getBlock() } } }); + } + + const vector& getResult() { return useful; }; +}; + +class DeadCodeAnalysis : public BackwardDataFlowAnalysis>, DeadCodeAnalysisNode> { +protected: + vector& formal_parameters; + + DeadCodeAnalysisNode* createNode(SAPFOR::BasicBlock* block) override { + return new DeadCodeAnalysisNode(block, formal_parameters); + }; +public: + DeadCodeAnalysis(vector& formal_parameters) : + formal_parameters(formal_parameters) + { }; +}; + + +void removeDeadCode(SgStatement* func, + const map>& allFuncs, + const map& commonBlocks) +{ + SgProgHedrStmt* prog = isSgProgHedrStmt(func); + + if (prog) + __spf_print(1, "[analyze %s]\n", prog->name().identifier()); + else + __spf_print(1, "[cannot resolve name of function]\n"); + + + auto cfg = buildCFGforCurrentFunc(func, SAPFOR::CFG_Settings(true, false, false, false, false, false, false), commonBlocks, allFuncs); + + if(cfg.size() != 1) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + const auto& cfg_pair = *(cfg.begin()); + + vector func_parameters(cfg_pair.first->funcParams.countOfPars, NULL); + + DeadCodeAnalysis* analysis_object = new DeadCodeAnalysis(func_parameters); + + analysis_object->fit(cfg_pair.second); + analysis_object->analyze(); + + set useful; + + for (DeadCodeAnalysisNode* byNode : analysis_object->getNodes()) + { + const auto& instructions = byNode->getBlock()->getInstructions(); + const auto& statuses = byNode->getResult(); + + int size = instructions.size(); + + if(size != statuses.size()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + for (int i = 0; i < size; i++) + { + if(statuses[i]) + { + SgStatement* stmt = instructions[i]->getInstruction()->getOperator(); + + while(stmt && useful.insert(stmt).second) + { + __spf_print(1, "[Useful statement '%s' on line %d]\n", stmt->unparse(), stmt->lineNumber()); + stmt = stmt->controlParent(); + } + } + } + } + + SgStatement* end = func->lastNodeOfStmt(), *st = func; + + set removable = + { + ASSIGN_STAT + }; + + SgStatement* enclosing = st->controlParent(); + SgStatement* encl_end = enclosing ? enclosing->lastNodeOfStmt() : NULL; + + while (st != end) + { + SgStatement* next = st->lexNext(); + SgStatement* parent = NULL; + + if (removable.find(st->variant()) != removable.end() && useful.find(st) == useful.end()) + { + __spf_print(1, "[Useless statement '%s' on line %d]\n", st->unparse(), st->lineNumber()); + + parent = st->controlParent(); + + st->extractStmt()->deleteStmt(); + st = NULL; + } + else + { + if (st == encl_end) + { + if (enclosing) + { + bool empty_parent = false; + + switch (enclosing->variant()) + { + case IF_NODE: + empty_parent = + enclosing->lexNext() == encl_end || // IF THEN ENDIF + enclosing->lexNext()->variant() == CONTROL_END && + enclosing->lexNext()->lexNext() == encl_end; // IF THEN ELSE ENDIF + break; + default: + empty_parent = enclosing->lexNext() == encl_end; + break; + } + + if(empty_parent) + { + parent = enclosing->controlParent(); + enclosing->extractStmt()->deleteStmt(); + st->extractStmt()->deleteStmt(); + st = NULL; + } + } + + } + } + + if(!parent) + parent = st->controlParent(); + + if (parent != enclosing) + { + enclosing = parent; + encl_end = enclosing ? enclosing->lastNodeOfStmt() : NULL; + } + + st = next; + } +} \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.h b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.h new file mode 100644 index 0000000..7c83b1d --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +#include "../Utils/SgUtils.h" +#include "../CFGraph/CFGraph.h" +#include "../CFGraph/live_variable_analysis.h" +#include "../CFGraph/DataFlow/data_flow.h" +#include "../CFGraph/DataFlow/backward_data_flow.h" + +void removeDeadCode(SgStatement* func, + const std::map>&allFuncs, + const std::map& commonBlocks); \ 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 3e68a6a..88ed1ba 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -306,6 +306,8 @@ void InitPassesDependencies(map> &passDepsIn, set Pass(REMOVE_OMP_DIRS) <= Pass(REMOVE_OMP_DIRS_TRANSFORM); + Pass(BUILD_IR) <= 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, REVERSE_CREATED_NESTED_LOOPS, PREDICT_SCHEME, CALCULATE_STATS_SCHEME, REVERT_SPF_DIRS, CLEAR_SPF_DIRS, TRANSFORM_SHADOW_IF_FULL, From bd8690d54ada5776728dbb7df94010c196cdab01 Mon Sep 17 00:00:00 2001 From: mkoch Date: Wed, 10 Jan 2024 16:20:48 +0300 Subject: [PATCH 13/40] Live variable analysis: use vectors of blocks instead of sets --- .../Sapfor_2017/_src/CFGraph/CFGraph.h | 12 +++--- .../_src/CFGraph/live_variable_analysis.cpp | 38 +++++++++++-------- .../_src/Transformations/dead_code.cpp | 14 +++---- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h index dbe790d..e47447c 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h @@ -31,8 +31,8 @@ namespace SAPFOR //live variables [arg -> blocks with usages] std::map> live_in, live_out, live_inout; - bool addLive(const std::map>& to_add, bool in); - std::map> getLive(bool in) const; + bool addLive(const std::map>& to_add, bool in); + std::map> getLive(bool in) const; bool removeLive(SAPFOR::Argument* to_remove, bool in); public: BasicBlock() { num = lastNumBlock++; } @@ -70,14 +70,14 @@ namespace SAPFOR /* * FOR LIVE ANALYSIS */ - bool addLiveIn(const std::map>& to_add) { return addLive(to_add, true); }; - bool addLiveOut(const std::map>& to_add) { return addLive(to_add, false); }; + bool addLiveIn(const std::map>& to_add) { return addLive(to_add, true); }; + bool addLiveOut(const std::map>& to_add) { return addLive(to_add, false); }; bool removeLiveIn(SAPFOR::Argument* to_remove) { return removeLive(to_remove, true); }; bool removeLiveOut(SAPFOR::Argument* to_remove) { return removeLive(to_remove, false); }; - std::map> getLiveIn() const { return getLive(true); }; - std::map> getLiveOut() const { return getLive(false); }; + std::map> getLiveIn() const { return getLive(true); }; + std::map> getLiveOut() const { return getLive(false); }; void compressLives(); /* diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index ccd933e..0836b52 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -21,15 +21,15 @@ using LIVE_VARIABLES::fcall; namespace SAPFOR { - bool BasicBlock::addLive(const map>& to_add, bool in) { + bool BasicBlock::addLive(const map>& to_add, bool in) { std::map>& current_set = (in ? live_in : live_out); std::map>& opposite_set = (!in ? live_in : live_out); bool inserted = false; for (const auto& byNew : to_add) { - const set& add_in_live = byNew.second; - set new_in_live = {}; + const vector& add_in_live = byNew.second; + vector new_in_live = {}; auto current_set_iter = current_set.find(byNew.first); @@ -128,14 +128,19 @@ namespace SAPFOR return removed; }; - map> BasicBlock::getLive(bool in) const { + map> BasicBlock::getLive(bool in) const { auto& current_set = in ? live_in : live_out; - map> res; + map> res; for (auto& by_source : { current_set, live_inout }) + { for (auto& by_pair : by_source) - res[by_pair.first].insert(by_pair.second.begin(), by_pair.second.end()); + { + auto& dest = res[by_pair.first]; + dest.insert(dest.end(), by_pair.second.begin(), by_pair.second.end()); + } + } return res; } @@ -219,31 +224,31 @@ static void buildUseDef(SAPFOR::BasicBlock* block, set& use, vector& formal_parameters, vector& fcalls, const map& funcByName, bool interprocedural); -class LiveVarAnalysisNode : public DataFlowAnalysisNode>> { +class LiveVarAnalysisNode : public DataFlowAnalysisNode>> { private: set live, dead; public: - map> getIn() + map> getIn() { return getBlock()->getLiveOut(); }; - map> getOut() + map> getOut() { return getBlock()->getLiveIn(); }; - bool addIn(const map>& data) + bool addIn(const map>& data) { return getBlock()->addLiveOut(data); }; - bool addOut(const map>& data) + bool addOut(const map>& data) { return getBlock()->addLiveIn(data); }; - bool forwardData(const map>& data) + bool forwardData(const map>& data) { bool inserted = false; @@ -266,7 +271,7 @@ public: } }; -class LiveVarAnalysis : public BackwardDataFlowAnalysis>, LiveVarAnalysisNode> { +class LiveVarAnalysis : public BackwardDataFlowAnalysis>, LiveVarAnalysisNode> { protected: vector& formal_parameters; vector& fcalls; @@ -694,10 +699,13 @@ void runLiveVariableAnalysis(const map>& { for (const auto& byArg : live_after) { - if (exit->addIn({ byArg })) + map> converted; + converted[byArg.first] = vector(byArg.second.begin(), byArg.second.end()); + + if (exit->addIn(converted)) { exit->setInCnt(max_cnt); - if (exit->forwardData({ byArg })) + if (exit->forwardData(converted)) exit->setOutCnt(max_cnt); } } diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 3c9c548..9280ecd 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -126,7 +126,7 @@ static void buildUseDef(SAPFOR::BasicBlock* block, set& use, } -class DeadCodeAnalysisNode : public DataFlowAnalysisNode>> { +class DeadCodeAnalysisNode : public DataFlowAnalysisNode>> { private: vector useful; bool useful_block = false; @@ -188,15 +188,15 @@ public: return updated; } - map> getIn() { + map> getIn() { return getBlock()->getLiveOut(); }; - map> getOut() { + map> getOut() { return getBlock()->getLiveIn(); }; - bool addIn(const map>& data) { + bool addIn(const map>& data) { bool inserted = getBlock()->addLiveOut(data); if (!useful_block) @@ -207,11 +207,11 @@ public: return inserted; }; - bool addOut(const map>& data) { + bool addOut(const map>& data) { return getBlock()->addLiveIn(data); }; - bool forwardData(const map>& data) { + bool forwardData(const map>& data) { bool inserted = false; SAPFOR::BasicBlock* bb= getBlock(); @@ -298,7 +298,7 @@ public: const vector& getResult() { return useful; }; }; -class DeadCodeAnalysis : public BackwardDataFlowAnalysis>, DeadCodeAnalysisNode> { +class DeadCodeAnalysis : public BackwardDataFlowAnalysis>, DeadCodeAnalysisNode> { protected: vector& formal_parameters; From de6e3bbd55960f0c1c7729ab7cc9120dc5287f00 Mon Sep 17 00:00:00 2001 From: mkoch Date: Wed, 10 Jan 2024 23:53:12 +0300 Subject: [PATCH 14/40] DATA_FLOW code style improvements: move template definitions to separate files, encapsulate CNT_NOTINIT constant --- sapfor/experts/Sapfor_2017/CMakeLists.txt | 2 + .../CFGraph/DataFlow/backward_data_flow.h | 90 +------------- .../DataFlow/backward_data_flow_impl.h | 103 ++++++++++++++++ .../_src/CFGraph/DataFlow/data_flow.h | 102 +--------------- .../_src/CFGraph/DataFlow/data_flow_impl.h | 113 ++++++++++++++++++ .../_src/CFGraph/live_variable_analysis.cpp | 2 +- 6 files changed, 224 insertions(+), 188 deletions(-) create mode 100644 sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h create mode 100644 sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h diff --git a/sapfor/experts/Sapfor_2017/CMakeLists.txt b/sapfor/experts/Sapfor_2017/CMakeLists.txt index 94433f2..be43305 100644 --- a/sapfor/experts/Sapfor_2017/CMakeLists.txt +++ b/sapfor/experts/Sapfor_2017/CMakeLists.txt @@ -223,7 +223,9 @@ set(CFG _src/CFGraph/IR.cpp set(DATA_FLOW _src/CFGraph/DataFlow/data_flow.h + _src/CFGraph/DataFlow/data_flow_impl.h _src/CFGraph/DataFlow/backward_data_flow.h + _src/CFGraph/DataFlow/backward_data_flow_impl.h ) set(CREATE_INTER_T _src/CreateInterTree/CreateInterTree.cpp diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h index dd0b3c3..58009c9 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h @@ -8,7 +8,6 @@ #include "../../Utils/SgUtils.h" #include "../CFGraph.h" #include "../IR.h" -#include "../RD_subst.h" template class BackwardDataFlowAnalysis : public DataFlowAnalysis { @@ -18,91 +17,4 @@ public: void fit(const std::vector& blocks); }; -// minimizes the number of blocks beween the ends of back edges -template -std::vector BackwardDataFlowAnalysis::reorderSequence(const std::vector& blocks, - const std::set back_edge_sources) -{ - std::vector res = { }; - - auto blocks_end = blocks.rend(); - for (auto it = blocks.rbegin(); it < blocks_end; it++) - { - SAPFOR::BasicBlock* curr = *it; - auto res_end = res.end(); - auto inserter = res.begin(); - if (back_edge_sources.count(curr) == 0) - { - auto curr_next_begin = curr->getNext().begin(); - auto curr_next_end = curr->getNext().end(); - while (inserter < res_end && std::find(curr_next_begin, curr_next_end, *inserter) == curr_next_end) - inserter++; - } - - res.insert(inserter, curr); - } - - return res; -} - -template -void BackwardDataFlowAnalysis::fit(const std::vector& blocks) -{ - std::set> back_edges = {}; - - bool returned = false; - std::map> back_edges_by_src; - - auto blocks_sorted = sortCfgNodes(blocks, &back_edges); - - std::set back_edge_sources; - - for (auto& edge : back_edges) - { - back_edges_by_src[edge.first].insert(edge.second); - back_edge_sources.insert(edge.first); - } - - back_edges.clear(); - - blocks_sorted = reorderSequence(blocks_sorted, back_edge_sources); - back_edge_sources.clear(); - - std::reverse(blocks_sorted.begin(), blocks_sorted.end()); - - nodes.clear(); - std::map node_by_block; - - for (auto block : blocks_sorted) - { - NodeType* node = createNode(block); - nodes.push_back(node); - node_by_block[block] = node; - } - - int nodes_size = nodes.size(); - - for (int i = 0; i < nodes_size; i++) - { - NodeType* node = nodes[i]; - - auto back_edges_by_src_it = back_edges_by_src.find(node->getBlock()); - if (back_edges_by_src_it != back_edges_by_src.end()) - { - // This node is a source for back edge - for (auto dest : back_edges_by_src_it->second) - { - auto node_by_block_it = node_by_block.find(dest); - if (node_by_block_it != node_by_block.end()) - node_by_block_it->second->getRollback().insert(i); - } - } - - for (auto next : node->getBlock()->getNext()) - { - auto node_by_block_it = node_by_block.find(next); - if (node_by_block_it != node_by_block.end()) - node->getPrevBlocks().insert(node_by_block_it->second); - } - } -} \ No newline at end of file +#include "backward_data_flow_impl.h" \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h new file mode 100644 index 0000000..d1d4486 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h @@ -0,0 +1,103 @@ +#pragma once +#include "backward_data_flow.h" + +#include +#include +#include + +#include "../../Utils/SgUtils.h" +#include "../CFGraph.h" +#include "../IR.h" +#include "../RD_subst.h" + +/* Note: this file should be included in backward_data_flow.h to provide template definitions */ + +// minimizes the number of blocks beween the ends of back edges +template +std::vector +BackwardDataFlowAnalysis::reorderSequence(const std::vector& blocks, + const std::set back_edge_sources) +{ + std::vector res = { }; + + auto blocks_end = blocks.rend(); + for (auto it = blocks.rbegin(); it < blocks_end; it++) + { + SAPFOR::BasicBlock* curr = *it; + auto res_end = res.end(); + auto inserter = res.begin(); + if (back_edge_sources.count(curr) == 0) + { + auto curr_next_begin = curr->getNext().begin(); + auto curr_next_end = curr->getNext().end(); + while (inserter < res_end && std::find(curr_next_begin, curr_next_end, *inserter) == curr_next_end) + inserter++; + } + + res.insert(inserter, curr); + } + + return res; +} + +template +void BackwardDataFlowAnalysis::fit(const std::vector& blocks) +{ + std::set> back_edges = {}; + + bool returned = false; + std::map> back_edges_by_src; + + auto blocks_sorted = sortCfgNodes(blocks, &back_edges); + + std::set back_edge_sources; + + for (auto& edge : back_edges) + { + back_edges_by_src[edge.first].insert(edge.second); + back_edge_sources.insert(edge.first); + } + + back_edges.clear(); + + blocks_sorted = reorderSequence(blocks_sorted, back_edge_sources); + back_edge_sources.clear(); + + std::reverse(blocks_sorted.begin(), blocks_sorted.end()); + + nodes.clear(); + std::map node_by_block; + + for (auto block : blocks_sorted) + { + NodeType* node = createNode(block); + nodes.push_back(node); + node_by_block[block] = node; + } + + int nodes_size = nodes.size(); + + for (int i = 0; i < nodes_size; i++) + { + NodeType* node = nodes[i]; + + auto back_edges_by_src_it = back_edges_by_src.find(node->getBlock()); + if (back_edges_by_src_it != back_edges_by_src.end()) + { + // This node is a source for back edge + for (auto dest : back_edges_by_src_it->second) + { + auto node_by_block_it = node_by_block.find(dest); + if (node_by_block_it != node_by_block.end()) + node_by_block_it->second->getRollback().insert(i); + } + } + + for (auto next : node->getBlock()->getNext()) + { + auto node_by_block_it = node_by_block.find(next); + if (node_by_block_it != node_by_block.end()) + node->getPrevBlocks().insert(node_by_block_it->second); + } + } +} \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h index d4ac744..11493a0 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h @@ -6,13 +6,10 @@ #include "../CFGraph.h" #include "../IR.h" -enum -{ - CNT_NOTINIT = 0 -}; - template class DataFlowAnalysisNode { + static const int CNT_NOTINIT = 0; + int in_cnt = CNT_NOTINIT, out_cnt = CNT_NOTINIT; std::set rollback; @@ -41,6 +38,7 @@ public: void setInCnt(int cnt) { in_cnt = cnt; } void setOutCnt(int cnt) { out_cnt = cnt; } + static int getStartCounter() { return CNT_NOTINIT; } std::set& getRollback() { return rollback; } std::set& getIgnoreRollback() { return ignore_rollback; } @@ -66,96 +64,4 @@ public: ~DataFlowAnalysis(); }; -template -DataFlowAnalysisNode::DataFlowAnalysisNode() { - getRollback() = {}; - getIgnoreRollback() = {}; - prev_blocks = {}; -} - -template -void DataFlowAnalysisNode::doStep() -{ - int in_max_cnt = CNT_NOTINIT, out_max_cnt = CNT_NOTINIT; - for (auto next : prev_blocks) - { - if (in_cnt < next->out_cnt) - { - for (const auto& byOut : next->getOut()) - { - bool inserted = addIn({ byOut }); - - if (inserted) - { - if (next->out_cnt > in_max_cnt) - in_max_cnt = next->out_cnt; - - inserted = forwardData({ byOut }); - - if (inserted && next->out_cnt > out_max_cnt) - out_max_cnt = next->out_cnt; - } - } - } - } - - bool was_notinit = (out_cnt == CNT_NOTINIT); - - if (out_max_cnt != CNT_NOTINIT) - out_cnt = out_max_cnt; - - if (in_max_cnt != CNT_NOTINIT) - in_cnt = in_max_cnt; - - // TODO: fix counter overflow - if (was_notinit) - { - out_cnt++; - in_cnt++; - } -} - -template -void DataFlowAnalysis::analyze() { - auto curr = 0; - auto stop = nodes.size(); - - while (curr != stop) - { - auto curr_bb = nodes[curr]; - curr_bb->doStep(); - - const auto& jumps = curr_bb->getRollback(); - if (jumps.size() != 0) - { - auto& ignored_jumps = curr_bb->getIgnoreRollback(); - - bool jump = false; - for (const auto& jump_to : jumps) - { - if (ignored_jumps.insert(jump_to).second && curr_bb->newerThan(nodes[jump_to])) - { - jump = true; - curr = jump_to; - break; - } - } - - if (!jump) - curr_bb->getIgnoreRollback().clear(); - else - continue; - } - - curr++; - } -} - -template -DataFlowAnalysis::~DataFlowAnalysis() -{ - for (DataFlowAnalysisNode* node : nodes) - delete node; - - nodes.clear(); -} \ No newline at end of file +#include "data_flow_impl.h" \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h new file mode 100644 index 0000000..da5a5fb --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h @@ -0,0 +1,113 @@ +#pragma once +#include "data_flow.h" + +#include +#include + +#include "../../Utils/SgUtils.h" +#include "../CFGraph.h" +#include "../IR.h" + +/* Note: this file should be included in data_flow.h to provide template definitions */ + +/* definitions for DataFlowAnalysisNode class */ + +template +const int DataFlowAnalysisNode::CNT_NOTINIT; + +template +DataFlowAnalysisNode::DataFlowAnalysisNode() +{ + getRollback() = {}; + getIgnoreRollback() = {}; + prev_blocks = {}; +} + +template +void DataFlowAnalysisNode::doStep() +{ + int in_max_cnt = CNT_NOTINIT, out_max_cnt = CNT_NOTINIT; + for (auto next : prev_blocks) + { + if (in_cnt < next->out_cnt) + { + for (const auto& byOut : next->getOut()) + { + bool inserted = addIn({ byOut }); + + if (inserted) + { + if (next->out_cnt > in_max_cnt) + in_max_cnt = next->out_cnt; + + inserted = forwardData({ byOut }); + + if (inserted && next->out_cnt > out_max_cnt) + out_max_cnt = next->out_cnt; + } + } + } + } + + bool was_notinit = (out_cnt == CNT_NOTINIT); + + if (out_max_cnt != CNT_NOTINIT) + out_cnt = out_max_cnt; + + if (in_max_cnt != CNT_NOTINIT) + in_cnt = in_max_cnt; + + // TODO: fix counter overflow + if (was_notinit) + { + out_cnt++; + in_cnt++; + } +} + +/* definitions for DataFlowAnalysis class */ + +template +void DataFlowAnalysis::analyze() { + auto curr = 0; + auto stop = nodes.size(); + + while (curr != stop) + { + auto curr_bb = nodes[curr]; + curr_bb->doStep(); + + const auto& jumps = curr_bb->getRollback(); + if (jumps.size() != 0) + { + auto& ignored_jumps = curr_bb->getIgnoreRollback(); + + bool jump = false; + for (const auto& jump_to : jumps) + { + if (ignored_jumps.insert(jump_to).second && curr_bb->newerThan(nodes[jump_to])) + { + jump = true; + curr = jump_to; + break; + } + } + + if (!jump) + curr_bb->getIgnoreRollback().clear(); + else + continue; + } + + curr++; + } +} + +template +DataFlowAnalysis::~DataFlowAnalysis() +{ + for (DataFlowAnalysisNode* node : nodes) + delete node; + + nodes.clear(); +} \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index 0836b52..0919566 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -684,7 +684,7 @@ void runLiveVariableAnalysis(const map>& } set exits; - int max_cnt = CNT_NOTINIT; + int max_cnt = LiveVarAnalysisNode::getStartCounter(); for (auto block : func_it->second->getNodes()) { if (block->getBlock()->getNext().size() == 0) From dcb42889da1677c09f51bc519c70a1b5a0500573 Mon Sep 17 00:00:00 2001 From: mkoch Date: Fri, 12 Jan 2024 16:11:21 +0300 Subject: [PATCH 15/40] live variables analysis: rename fcall structure to LiveDeadVarsForCall class --- .../_src/CFGraph/live_variable_analysis.cpp | 34 +++++++++---------- .../_src/CFGraph/live_variable_analysis.h | 8 +++-- .../_src/Transformations/dead_code.cpp | 2 +- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index 0919566..48bdff8 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -17,7 +17,7 @@ using std::set; using std::unordered_map; using std::list; -using LIVE_VARIABLES::fcall; +using LIVE_VARIABLES::LiveDeadVarsForCall; namespace SAPFOR { @@ -152,7 +152,7 @@ namespace SAPFOR } } -bool fcall::tryInsert(set& dest, SAPFOR::BasicBlock* b) +bool LiveDeadVarsForCall::tryInsert(set& dest, SAPFOR::BasicBlock* b) { if (b == block || dest.find(block) == dest.end()) { @@ -163,7 +163,7 @@ bool fcall::tryInsert(set& dest, SAPFOR::BasicBlock* b) return false; } -fcall::fcall(FuncInfo* f, SAPFOR::BasicBlock* b, const vector& p) +LiveDeadVarsForCall::LiveDeadVarsForCall(FuncInfo* f, SAPFOR::BasicBlock* b, const vector& p) { block = b; func = f; @@ -176,7 +176,7 @@ fcall::fcall(FuncInfo* f, SAPFOR::BasicBlock* b, const vector params[i] = p[i]; } -void fcall::make_live(SAPFOR::Argument* arg, SAPFOR::BasicBlock* b) +void LiveDeadVarsForCall::make_live(SAPFOR::Argument* arg, SAPFOR::BasicBlock* b) { if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::COMMON_ || arg->getMemType() == SAPFOR::CFG_MEM_TYPE::MODULE_) { @@ -193,7 +193,7 @@ void fcall::make_live(SAPFOR::Argument* arg, SAPFOR::BasicBlock* b) } } -void fcall::make_dead(SAPFOR::Argument* arg) +void LiveDeadVarsForCall::make_dead(SAPFOR::Argument* arg) { if (arg->getMemType() == SAPFOR::CFG_MEM_TYPE::COMMON_ || arg->getMemType() == SAPFOR::CFG_MEM_TYPE::MODULE_) { @@ -210,7 +210,7 @@ void fcall::make_dead(SAPFOR::Argument* arg) } } -void fcall::updateFromOut() +void LiveDeadVarsForCall::updateFromOut() { for (const auto& p : block->getLiveOut()) for (auto b : p.second) @@ -221,7 +221,7 @@ static bool getLiveDead(const vector& params, const string& f set& live, set& dead); static void buildUseDef(SAPFOR::BasicBlock* block, set& use, set& def, - vector& formal_parameters, vector& fcalls, + vector& formal_parameters, vector& fcalls, const map& funcByName, bool interprocedural); class LiveVarAnalysisNode : public DataFlowAnalysisNode>> { @@ -260,7 +260,7 @@ public: }; LiveVarAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters, - vector& fcalls, const map& funcByName) + vector& fcalls, const map& funcByName) { setBlock(block); @@ -274,21 +274,21 @@ public: class LiveVarAnalysis : public BackwardDataFlowAnalysis>, LiveVarAnalysisNode> { protected: vector& formal_parameters; - vector& fcalls; + vector& fcalls; const map& funcByName; LiveVarAnalysisNode* createNode(SAPFOR::BasicBlock* block) override { return new LiveVarAnalysisNode(block, formal_parameters, fcalls, funcByName); }; public: - LiveVarAnalysis(vector& formal_parameters, vector& fcalls, + LiveVarAnalysis(vector& formal_parameters, vector& fcalls, const map& funcByName) : formal_parameters(formal_parameters), fcalls(fcalls), funcByName(funcByName) { }; }; void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, set& use, set& def, - vector& formal_parameters, vector& fcalls, + vector& formal_parameters, vector& fcalls, vector& lastParamRef, int& last_param_ref_index, int& last_param_ref_size, string& fName, const map& funcByName, bool interprocedural) { @@ -340,7 +340,7 @@ void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* ins auto func_it = funcByName.find(fName); if (func_it != funcByName.end()) { - fcalls.push_back(fcall(func_it->second, block, lastParamRef)); + fcalls.push_back(LiveDeadVarsForCall(func_it->second, block, lastParamRef)); auto r_it = fcalls.rbegin(); auto r_end = fcalls.rend(); @@ -376,7 +376,7 @@ void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* ins static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, set& use, set& def, - vector& formal_parameters, vector& fcalls, + vector& formal_parameters, vector& fcalls, vector& lastParamRef, int& last_param_ref_index, int& last_param_ref_size, string& fName, const map& funcByName, bool interprocedural) { @@ -411,7 +411,7 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru //Build use and def sets of block. Result are stored in use and def static void buildUseDef(SAPFOR::BasicBlock* block, set& use, set& def, - vector& formal_parameters, vector& fcalls, + vector& formal_parameters, vector& fcalls, const map& funcByName, bool interprocedural) { vector lastParamRef; @@ -610,7 +610,7 @@ void runLiveVariableAnalysis(const map>& map func_to_analysis_object; map> func_to_parameters; - list> live_for_fcalls; + list> live_for_fcalls; //TODO: take into account ssc structure // main stage @@ -639,7 +639,7 @@ void runLiveVariableAnalysis(const map>& // interprocedural analysis for (auto& calls_vector : live_for_fcalls) { - map assembled_fcalls; + map assembled_fcalls; for (auto& call : calls_vector) { call.updateFromOut(); @@ -649,7 +649,7 @@ void runLiveVariableAnalysis(const map>& auto it = assembled_fcalls.find(call.func); if (it == assembled_fcalls.end()) - it = assembled_fcalls.insert({ call.func, fcall(call.func, call.block, {}) }).first; + it = assembled_fcalls.insert({ call.func, LiveDeadVarsForCall(call.func, call.block, {}) }).first; for (const auto& p : call.live_after) it->second.live_after[p.first].insert(p.second.begin(), p.second.end()); diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h index a697273..d4a2c6a 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h @@ -5,7 +5,9 @@ namespace LIVE_VARIABLES { - struct fcall + /* Store information about live and dead variables after call operator */ + /* (needed for interprocedural part of live variable analysis) */ + class LiveDeadVarsForCall { private: bool tryInsert(std::set& dest, SAPFOR::BasicBlock* b); @@ -21,7 +23,7 @@ namespace LIVE_VARIABLES std::vector params; SAPFOR::BasicBlock* block; - fcall(FuncInfo* f, SAPFOR::BasicBlock* b, const std::vector& p); + LiveDeadVarsForCall(FuncInfo* f, SAPFOR::BasicBlock* b, const std::vector& p); void make_live(SAPFOR::Argument* arg, SAPFOR::BasicBlock* b); void make_dead(SAPFOR::Argument* arg); @@ -39,7 +41,7 @@ void insertIfVar(IT begin, IT end, DEST& to) { void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, std::set& use, std::set& def, - std::vector& formal_parameters, std::vector& fcalls, + std::vector& formal_parameters, std::vector& fcalls, std::vector& lastParamRef, int& last_param_ref_index, int& last_param_ref_size, std::string& fName, const std::map& funcByName, bool interprocedural); diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 9280ecd..f1ad920 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -23,7 +23,7 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru if (fName == "") last_fcall_useful = false; - vector fcalls; + vector fcalls; getUseDefForInstruction(block, instr, From 6dc0ad1080967178cb18b8429d891069ba8c88d1 Mon Sep 17 00:00:00 2001 From: mkoch Date: Fri, 12 Jan 2024 16:57:02 +0300 Subject: [PATCH 16/40] DataFlow: remove unused template parameter DataType from DataFlowAnalysis class --- .../_src/CFGraph/DataFlow/backward_data_flow.h | 6 +++--- .../_src/CFGraph/DataFlow/backward_data_flow_impl.h | 10 +++++----- .../Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h | 2 +- .../Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h | 10 +++++----- .../_src/CFGraph/live_variable_analysis.cpp | 2 +- .../Sapfor_2017/_src/Transformations/dead_code.cpp | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h index 58009c9..7f81751 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow.h @@ -9,10 +9,10 @@ #include "../CFGraph.h" #include "../IR.h" -template -class BackwardDataFlowAnalysis : public DataFlowAnalysis { +template +class BackwardDataFlowAnalysis : public DataFlowAnalysis { std::vector reorderSequence(const std::vector& blocks, - const std::set back_edge_sources); + const std::set back_edge_sources); public: void fit(const std::vector& blocks); }; diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h index d1d4486..e406432 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h @@ -13,10 +13,10 @@ /* Note: this file should be included in backward_data_flow.h to provide template definitions */ // minimizes the number of blocks beween the ends of back edges -template +template std::vector -BackwardDataFlowAnalysis::reorderSequence(const std::vector& blocks, - const std::set back_edge_sources) +BackwardDataFlowAnalysis::reorderSequence(const std::vector& blocks, + const std::set back_edge_sources) { std::vector res = { }; @@ -40,8 +40,8 @@ BackwardDataFlowAnalysis::reorderSequence(const std::vector< return res; } -template -void BackwardDataFlowAnalysis::fit(const std::vector& blocks) +template +void BackwardDataFlowAnalysis::fit(const std::vector& blocks) { std::set> back_edges = {}; diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h index 11493a0..8067bd7 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow.h @@ -49,7 +49,7 @@ public: void setBlock(SAPFOR::BasicBlock* b) { bb = b; } }; -template +template class DataFlowAnalysis { protected: std::vector nodes; diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h index da5a5fb..5e74e29 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/data_flow_impl.h @@ -67,8 +67,8 @@ void DataFlowAnalysisNode::doStep() /* definitions for DataFlowAnalysis class */ -template -void DataFlowAnalysis::analyze() { +template +void DataFlowAnalysis::analyze() { auto curr = 0; auto stop = nodes.size(); @@ -103,10 +103,10 @@ void DataFlowAnalysis::analyze() { } } -template -DataFlowAnalysis::~DataFlowAnalysis() +template +DataFlowAnalysis::~DataFlowAnalysis() { - for (DataFlowAnalysisNode* node : nodes) + for (NodeType* node : nodes) delete node; nodes.clear(); diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index 48bdff8..51b4d14 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -271,7 +271,7 @@ public: } }; -class LiveVarAnalysis : public BackwardDataFlowAnalysis>, LiveVarAnalysisNode> { +class LiveVarAnalysis : public BackwardDataFlowAnalysis { protected: vector& formal_parameters; vector& fcalls; diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index f1ad920..8cb4b18 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -298,7 +298,7 @@ public: const vector& getResult() { return useful; }; }; -class DeadCodeAnalysis : public BackwardDataFlowAnalysis>, DeadCodeAnalysisNode> { +class DeadCodeAnalysis : public BackwardDataFlowAnalysis { protected: vector& formal_parameters; From aa3c28847a1d5eabbb901a9a6b9eb4744979388c Mon Sep 17 00:00:00 2001 From: XNPSTER Date: Sat, 13 Jan 2024 13:41:31 +0300 Subject: [PATCH 17/40] DATA_FLOW: fix build for linux --- .../_src/CFGraph/DataFlow/backward_data_flow_impl.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h index e406432..4578e7b 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/DataFlow/backward_data_flow_impl.h @@ -65,21 +65,21 @@ void BackwardDataFlowAnalysis::fit(const std::vectornodes.clear(); std::map node_by_block; for (auto block : blocks_sorted) { - NodeType* node = createNode(block); - nodes.push_back(node); + NodeType* node = this->createNode(block); + this->nodes.push_back(node); node_by_block[block] = node; } - int nodes_size = nodes.size(); + int nodes_size = this->nodes.size(); for (int i = 0; i < nodes_size; i++) { - NodeType* node = nodes[i]; + NodeType* node = this->nodes[i]; auto back_edges_by_src_it = back_edges_by_src.find(node->getBlock()); if (back_edges_by_src_it != back_edges_by_src.end()) From 5728736bc2c87ca663c43456c7088b735b42c4ab Mon Sep 17 00:00:00 2001 From: mkoch Date: Sun, 14 Jan 2024 13:26:21 +0300 Subject: [PATCH 18/40] live analysis: remove redunant ';' --- .../_src/CFGraph/live_variable_analysis.cpp | 18 +++++++++--------- .../_src/CFGraph/live_variable_analysis.h | 2 +- .../_src/Transformations/dead_code.cpp | 16 ++++++++-------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index 51b4d14..5f79f52 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -95,7 +95,7 @@ namespace SAPFOR } return inserted; - }; + } bool BasicBlock::removeLive(SAPFOR::Argument* to_remove, bool in) { @@ -126,7 +126,7 @@ namespace SAPFOR } return removed; - }; + } map> BasicBlock::getLive(bool in) const { auto& current_set = in ? live_in : live_out; @@ -231,22 +231,22 @@ public: map> getIn() { return getBlock()->getLiveOut(); - }; + } map> getOut() { return getBlock()->getLiveIn(); - }; + } bool addIn(const map>& data) { return getBlock()->addLiveOut(data); - }; + } bool addOut(const map>& data) { return getBlock()->addLiveIn(data); - }; + } bool forwardData(const map>& data) { @@ -257,7 +257,7 @@ public: inserted |= getBlock()->addLiveIn({ byArg }); return inserted; - }; + } LiveVarAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters, vector& fcalls, const map& funcByName) @@ -279,11 +279,11 @@ protected: LiveVarAnalysisNode* createNode(SAPFOR::BasicBlock* block) override { return new LiveVarAnalysisNode(block, formal_parameters, fcalls, funcByName); - }; + } public: LiveVarAnalysis(vector& formal_parameters, vector& fcalls, const map& funcByName) : formal_parameters(formal_parameters), fcalls(fcalls), funcByName(funcByName) - { }; + { } }; void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h index d4a2c6a..7e6cd73 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.h @@ -37,7 +37,7 @@ void insertIfVar(IT begin, IT end, DEST& to) { for (auto it = begin; it != end; it++) if (*it && (*it)->getType() == SAPFOR::CFG_ARG_TYPE::VAR) to.insert(*it); -}; +} void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, std::set& use, std::set& def, diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 8cb4b18..9b6411f 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -190,11 +190,11 @@ public: map> getIn() { return getBlock()->getLiveOut(); - }; + } map> getOut() { return getBlock()->getLiveIn(); - }; + } bool addIn(const map>& data) { bool inserted = getBlock()->addLiveOut(data); @@ -205,11 +205,11 @@ public: inserted |= updateJumpStatus(); return inserted; - }; + } bool addOut(const map>& data) { return getBlock()->addLiveIn(data); - }; + } bool forwardData(const map>& data) { bool inserted = false; @@ -279,7 +279,7 @@ public: } return inserted; - }; + } DeadCodeAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters) : formal_parameters(formal_parameters) @@ -295,7 +295,7 @@ public: getBlock()->addLiveIn({ { arg, { getBlock() } } }); } - const vector& getResult() { return useful; }; + const vector& getResult() { return useful; } }; class DeadCodeAnalysis : public BackwardDataFlowAnalysis { @@ -304,11 +304,11 @@ protected: DeadCodeAnalysisNode* createNode(SAPFOR::BasicBlock* block) override { return new DeadCodeAnalysisNode(block, formal_parameters); - }; + } public: DeadCodeAnalysis(vector& formal_parameters) : formal_parameters(formal_parameters) - { }; + { } }; From 4809857318833bb2cc2505afe3ee62b2dbca5a3e Mon Sep 17 00:00:00 2001 From: mkoch Date: Sun, 14 Jan 2024 15:36:52 +0300 Subject: [PATCH 19/40] dead code: fixed memory leaks, simplified code for empty do/while/if removing, removing of 'else' branch --- .../_src/Transformations/dead_code.cpp | 80 +++++++------------ 1 file changed, 29 insertions(+), 51 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 9b6411f..01c76f8 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -10,6 +10,8 @@ using std::string; using std::vector; using std::set; +#define PRINT_USELESS_STATEMENTS 1 + static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, set& use, set& def, vector& formal_parameters, @@ -318,12 +320,6 @@ void removeDeadCode(SgStatement* func, { SgProgHedrStmt* prog = isSgProgHedrStmt(func); - if (prog) - __spf_print(1, "[analyze %s]\n", prog->name().identifier()); - else - __spf_print(1, "[cannot resolve name of function]\n"); - - auto cfg = buildCFGforCurrentFunc(func, SAPFOR::CFG_Settings(true, false, false, false, false, false, false), commonBlocks, allFuncs); if(cfg.size() != 1) @@ -333,14 +329,14 @@ void removeDeadCode(SgStatement* func, vector func_parameters(cfg_pair.first->funcParams.countOfPars, NULL); - DeadCodeAnalysis* analysis_object = new DeadCodeAnalysis(func_parameters); + DeadCodeAnalysis analysis_object(func_parameters); - analysis_object->fit(cfg_pair.second); - analysis_object->analyze(); + analysis_object.fit(cfg_pair.second); + analysis_object.analyze(); set useful; - for (DeadCodeAnalysisNode* byNode : analysis_object->getNodes()) + for (DeadCodeAnalysisNode* byNode : analysis_object.getNodes()) { const auto& instructions = byNode->getBlock()->getInstructions(); const auto& statuses = byNode->getResult(); @@ -357,10 +353,7 @@ void removeDeadCode(SgStatement* func, SgStatement* stmt = instructions[i]->getInstruction()->getOperator(); while(stmt && useful.insert(stmt).second) - { - __spf_print(1, "[Useful statement '%s' on line %d]\n", stmt->unparse(), stmt->lineNumber()); stmt = stmt->controlParent(); - } } } } @@ -372,65 +365,50 @@ void removeDeadCode(SgStatement* func, ASSIGN_STAT }; - SgStatement* enclosing = st->controlParent(); - SgStatement* encl_end = enclosing ? enclosing->lastNodeOfStmt() : NULL; - while (st != end) { SgStatement* next = st->lexNext(); - SgStatement* parent = NULL; if (removable.find(st->variant()) != removable.end() && useful.find(st) == useful.end()) { - __spf_print(1, "[Useless statement '%s' on line %d]\n", st->unparse(), st->lineNumber()); + __spf_print(PRINT_USELESS_STATEMENTS, "[Useless statement '%s' on line %d]\n", st->unparse(), st->lineNumber()); - parent = st->controlParent(); - - st->extractStmt()->deleteStmt(); - st = NULL; + st->deleteStmt(); } else { - if (st == encl_end) + if (isSgControlEndStmt(st)) { - if (enclosing) + SgStatement* parent = st->controlParent(); + SgStatement* parent_end; + + if (parent && (parent_end = parent->lastNodeOfStmt()) && parent_end == st) { bool empty_parent = false; - switch (enclosing->variant()) + switch (parent->variant()) { - case IF_NODE: - empty_parent = - enclosing->lexNext() == encl_end || // IF THEN ENDIF - enclosing->lexNext()->variant() == CONTROL_END && - enclosing->lexNext()->lexNext() == encl_end; // IF THEN ELSE ENDIF - break; - default: - empty_parent = enclosing->lexNext() == encl_end; - break; + 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 = enclosing->controlParent(); - enclosing->extractStmt()->deleteStmt(); - st->extractStmt()->deleteStmt(); - st = NULL; - } + if (empty_parent) + parent->deleteStmt(); + else if(isSgIfStmt(parent) && isSgControlEndStmt(parent_end->lexPrev())) // IF with empty ELSE branch + parent_end->deleteStmt(); } - } } - if(!parent) - parent = st->controlParent(); - - if (parent != enclosing) - { - enclosing = parent; - encl_end = enclosing ? enclosing->lastNodeOfStmt() : NULL; - } - st = next; } + + deleteCFG(cfg); } \ No newline at end of file From fbab3477c414425682ea84fe4e670c5db3438f8d Mon Sep 17 00:00:00 2001 From: ALEXks Date: Sun, 14 Jan 2024 15:47:02 +0300 Subject: [PATCH 20/40] 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 bbe67a9..f92efdc 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 "2263" +#define VERSION_SPF "2265" From 9774f83ae18cce147721d8bfda523816a9e229cd Mon Sep 17 00:00:00 2001 From: mkoch Date: Sun, 14 Jan 2024 15:47:41 +0300 Subject: [PATCH 21/40] dead code: turn off debug prints --- sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 01c76f8..0590d4e 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -10,7 +10,7 @@ using std::string; using std::vector; using std::set; -#define PRINT_USELESS_STATEMENTS 1 +#define PRINT_USELESS_STATEMENTS 0 static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, set& use, set& def, From 00c539eff43d93e135c4b061a2f14dec876440f8 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Tue, 23 Jan 2024 13:04:46 +0300 Subject: [PATCH 22/40] fixed inliner, improved dead_code --- sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp | 13 +++++++++---- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 4 ++-- sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h | 2 +- sapfor/experts/Sapfor_2017/_src/Utils/version.h | 2 +- .../_src/VisualizerCalls/get_information.cpp | 10 ++++++++++ 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp b/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp index c55acd2..a37d947 100644 --- a/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp @@ -137,6 +137,11 @@ static map createMapOfArgs(SgStatement* tempHedr, SgExpre return vars; } +static bool isAllocated(SgSymbol* s) +{ + return (s->attributes() & ALLOCATABLE_BIT) || (s->attributes() & POINTER_BIT); +} + static inline SgSymbol* createSymbAndDecl(const string& funcName, const string& varName, SgSymbol* newS, set& newSymbols, SgType* type = NULL) { SgSymbol* original = newS; @@ -208,7 +213,7 @@ static inline SgSymbol* createSymbAndDecl(const string& funcName, const string& { createdByFunc[key][varName] = newS; count[0] = nextCount; - if (original && (original->attributes() & ALLOCATABLE_BIT)) + if (original && isAllocated(original)) linkToOrig[newS] = original; } else @@ -266,7 +271,7 @@ static vector getLowBounds(SgSymbol* arrayS) SgExpression* list = NULL; - if (arrayS->attributes() & ALLOCATABLE_BIT) + if (isAllocated(arrayS)) { SgSymbol* copyFrom = NULL; if (linkToOrig.find(arrayS) == linkToOrig.end()) @@ -332,7 +337,7 @@ static vector getLowBounds(SgSymbol* arrayS) static vector getBoundsExpression(SgSymbol* arrayS) { - if (arrayS->attributes() & ALLOCATABLE_BIT) + if (isAllocated(arrayS)) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); auto type = isSgArrayType(arrayS->type()); @@ -2163,7 +2168,7 @@ static void createDeclarations(const map>& newSymbs if (globalType != 1) // not in COMMON saveRefs[toDec->identifier()] = toDec; - if (toDec->attributes() & ALLOCATABLE_BIT) + if (isAllocated(toDec)) allocatable.insert(toDec); if (toDec->attributes() & DATA_BIT) diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index 4a006d0..a03ca66 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -1172,9 +1172,9 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne addPrivatesToArraysFromGUI(file, declaredArrays, distrStateFromGUI); else if (curr_regime == REMOVE_DEAD_CODE_AND_UNPARSE) { - auto funcsForFile = getObjectForFileFromMap(file_name, allFuncInfo_IR); + auto funcsForFile = getObjectForFileFromMap(file_name, allFuncInfo); for (auto& func : funcsForFile) - removeDeadCode(func->funcPointer, allFuncInfo_IR, commonBlocks); + removeDeadCode(func->funcPointer, allFuncInfo, commonBlocks); } else if (curr_regime == TEST_PASS) { diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h index 88ed1ba..dc83bb7 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -306,7 +306,7 @@ void InitPassesDependencies(map> &passDepsIn, set Pass(REMOVE_OMP_DIRS) <= Pass(REMOVE_OMP_DIRS_TRANSFORM); - Pass(BUILD_IR) <= Pass(REMOVE_DEAD_CODE_AND_UNPARSE); + Pass(CALL_GRAPH2) <= 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, diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index f92efdc..97fb9d3 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 "2265" +#define VERSION_SPF "2267" diff --git a/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp b/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp index 826c2cf..6ec8dd9 100644 --- a/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp +++ b/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp @@ -1895,6 +1895,14 @@ int SPF_InsertPrivateFromGUI(void*& context, int winHandler, short* options, sho return simpleTransformPass(INSERT_NO_DISTR_FLAGS_FROM_GUI, options, projName, folderName, output, outputSize, outputMessage, outputMessageSize); } +int SPF_RemoveDeadCode(void*& context, int winHandler, short* options, short* projName, short* folderName, short*& output, + int*& outputSize, short*& outputMessage, int*& outputMessageSize) +{ + MessageManager::clearCache(); + MessageManager::setWinHandler(winHandler); + return simpleTransformPass(REMOVE_DEAD_CODE_AND_UNPARSE, options, projName, folderName, output, outputSize, outputMessage, outputMessageSize); +} + static inline void convertBackSlash(char *str, int strL) { for (int z = 0; z < strL; ++z) @@ -2607,6 +2615,8 @@ const wstring Sapfor_RunTransformation(const char* transformName_c, const char* retCode = SPF_SharedMemoryParallelization(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize); else if (whichRun == "SPF_InsertPrivateFromGUI") retCode = SPF_InsertPrivateFromGUI(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize); + else if (whichRun == "SPF_RemoveDeadCode") + retCode = SPF_RemoveDeadCode(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize); else { if (showDebug) From 3b4af6672067c16425e21a49ba1660798605b3c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=BE=D1=87?= =?UTF-8?q?=D0=B0=D1=80=D0=BC=D0=B8=D0=BD?= Date: Tue, 30 Jan 2024 19:40:14 +0300 Subject: [PATCH 23/40] dead code removing: unreachable code --- .../Sapfor_2017/_src/CFGraph/CFGraph.cpp | 16 ++++++ .../Sapfor_2017/_src/CFGraph/CFGraph.h | 3 ++ .../_src/Transformations/dead_code.cpp | 53 ++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.cpp index d98dc14..b00a2b1 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.cpp @@ -56,6 +56,22 @@ void BBlock::addInstruction(IR_Block* item) item->setBasicBlock(this); } +int BBlock::removePrev(BBlock* removed) +{ + auto it = std::remove(prev.begin(), prev.end(), removed); + auto r = prev.end() - it; + prev.erase(it, prev.end()); + return r; +} + +int BBlock::removeNext(BBlock* removed) +{ + auto it = std::remove(next.begin(), next.end(), removed); + auto r = next.end() - it; + next.erase(it, next.end()); + return r; +} + BBlock::~BasicBlock() { for (auto& instr : instructions) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h index e47447c..319b420 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/CFGraph.h @@ -42,6 +42,9 @@ namespace SAPFOR void addInstruction(IR_Block* item); void addPrev(BasicBlock* prev_) { prev.push_back(prev_); } void addNext(BasicBlock* next_) { next.push_back(next_); } + + int removePrev(BasicBlock* removed); + int removeNext(BasicBlock* removed); void replacePrevNext(const std::map& oldToNew) { diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 0590d4e..4df0ca5 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -4,12 +4,15 @@ #include #include #include +#include using std::map; using std::string; using std::vector; using std::set; +using std::remove_if; + #define PRINT_USELESS_STATEMENTS 0 static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, @@ -325,7 +328,51 @@ void removeDeadCode(SgStatement* func, if(cfg.size() != 1) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - const auto& cfg_pair = *(cfg.begin()); + auto& cfg_pair = *(cfg.begin()); + + // delete unreachable blocks + + set reachable; + + for (auto b : cfg_pair.second) + if(b->getInstructions().front()->isHeader()) + reachable.insert(b); + + set worklist = reachable; + while (worklist.size() != 0) + { + set to_insert; + + for(auto b : worklist) + for(auto next: b->getNext()) + if(reachable.insert(next).second) + to_insert.insert(next); + + worklist = to_insert; + } + + auto remove_unreachable_it = remove_if(cfg_pair.second.begin(), cfg_pair.second.end(), + [&reachable](SAPFOR::BasicBlock* b) + { + if (reachable.find(b) == reachable.end()) + { + for(auto next: b->getNext()) + if(reachable.find(next) != reachable.end()) + next->removePrev(b); + + delete b; + return true; + } + + return false; + } + ); + + reachable.clear(); + + cfg_pair.second.erase(remove_unreachable_it, cfg_pair.second.end()); + + // detect useless code vector func_parameters(cfg_pair.first->funcParams.countOfPars, NULL); @@ -334,6 +381,8 @@ void removeDeadCode(SgStatement* func, analysis_object.fit(cfg_pair.second); analysis_object.analyze(); + // detect dead statements + set useful; for (DeadCodeAnalysisNode* byNode : analysis_object.getNodes()) @@ -358,6 +407,8 @@ void removeDeadCode(SgStatement* func, } } + // remove dead statements + SgStatement* end = func->lastNodeOfStmt(), *st = func; set removable = From b4c2f50a41bce7d264b048a36c3a4e09edeceb2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=BE=D1=87?= =?UTF-8?q?=D0=B0=D1=80=D0=BC=D0=B8=D0=BD?= Date: Tue, 30 Jan 2024 19:45:27 +0300 Subject: [PATCH 24/40] improvements at expression substitution (wrong ifdef directives, bug with substitutions from loops fixed) --- .../Sapfor_2017/_src/CFGraph/RD_subst.cpp | 125 +++++++++--------- 1 file changed, 66 insertions(+), 59 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/RD_subst.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/RD_subst.cpp index a99bf77..387b142 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/RD_subst.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/RD_subst.cpp @@ -321,38 +321,6 @@ static bool getDependencies(SAPFOR::Argument* var, SAPFOR::Instruction* instr, c return true; } -static bool ifVariableValid(SAPFOR::Argument* var, const set>& defs, - SAPFOR::Argument* processed_var, const pair& def_of_processed_var) -{ - const auto& BB_of_def_of_processed_var = def_of_processed_var.second; - - if (defs.size() == 1) - { - const auto& def = *defs.begin(); - if (def.second == BB_of_def_of_processed_var && def.first->getNumber() < def_of_processed_var.first->getNumber()) - return true; - } - - const auto& RD_In_BB_of_def_of_processed_var = BB_of_def_of_processed_var->getRD_In(); - auto RD_of_var_from_BB_with_processed_var = RD_In_BB_of_def_of_processed_var.find(var); - - if (RD_of_var_from_BB_with_processed_var != RD_In_BB_of_def_of_processed_var.end() - && RD_of_var_from_BB_with_processed_var->second.size() == defs.size()) - { - for (const auto& def : defs) - { - int number_of_istruction_with_decl = def.first ? def.first->getNumber() : CFG_VAL::UNINIT; - - if (RD_of_var_from_BB_with_processed_var->second.count(number_of_istruction_with_decl) == 0) - return false; - } - - return true; - } - - return false; -} - static const set& func_affects(const FuncInfo* func, const vector& blocks) { static map> affects; @@ -1150,7 +1118,7 @@ vector sortCfgNodes(const vector& bloc if (!nodes_added && processing_stack.size() != 0) { //there is some blocks in the stack but no one can be processed //this code should be unreachable -#ifdef DEBUG_CHECKS +#if DEBUG_CHECKS printInternalError(convertFileName(__FILE__).c_str(), __LINE__); #endif // DEBUG_CHECKS auto block = processing_stack.back(); @@ -1167,7 +1135,7 @@ vector sortCfgNodes(const vector& bloc } } -#ifdef DEBUG_CHECKS +#if DEBUG_CHECKS set verify_unique; verify_unique.insert(result.begin(), result.end()); @@ -1176,7 +1144,7 @@ vector sortCfgNodes(const vector& bloc verify_unique.clear(); #endif // DEBUG_CHECKS -#ifdef DEBUG_CHECKS +#if DEBUG_CHECKS set all_blocks, res_blocks; all_blocks.insert(blocks.begin(), blocks.end()); @@ -1267,7 +1235,7 @@ bool isArgReaches(int decl_instr, SAPFOR::BasicBlock* decl_bb, if (RDs_for_arg.size() == 1) { const int rd = *RDs_for_arg.begin(); - if (rd >= decl_bb->getInstructions().front()->getNumber() && rd <= decl_instr) + if (rd >= decl_bb->getInstructions().front()->getNumber() && rd < decl_instr) return true; } @@ -1275,7 +1243,67 @@ bool isArgReaches(int decl_instr, SAPFOR::BasicBlock* decl_bb, if (arg_in_from_decl_it == decl_bb->getRD_In().end()) return false; - return arg_in_from_decl_it->second == RDs_for_arg; + if(arg_in_from_decl_it->second != RDs_for_arg) + return false; + + set reachable = { decl_bb }; + set banned_instructions; + for (int instr_def : arg_in_from_decl_it->second) + if (instr_def >= decl_instr || instr_def < decl_bb->getInstructions().front()->getNumber()) + //try to find way [decl_bb] -> [dest_bb] with redefining of var (that means that var value from decl_bb could be overwrited) + banned_instructions.insert(instr_def); + + set worklist = reachable, banned_blocks; + bool way_found = false; + while (worklist.size() != 0 && banned_instructions.size() != 0) + { + for (SAPFOR::BasicBlock* wl : worklist) + { + int start = wl->getInstructions().front()->getNumber(), end = wl->getInstructions().back()->getNumber(); + + for (auto banned_it = banned_instructions.begin(); banned_it != banned_instructions.end();) + { + if(start <= *banned_it && *banned_it <= end) + { + banned_it = banned_instructions.erase(banned_it); + banned_blocks.insert(wl); + } + else + { + banned_it++; + } + } + } + + set to_insert; + + for (auto b : worklist) + for (auto next : b->getNext()) + if (reachable.insert(next).second) + to_insert.insert(next); + + worklist = to_insert; + } + + reachable = banned_blocks; + worklist = reachable; + while (worklist.size() != 0 || banned_instructions.size() == 0) + { + if(worklist.find(dest_bb) != worklist.end()) + return false; + + set to_insert; + + for (auto b : worklist) + for (auto next : b->getNext()) + if(next != decl_bb) + if (reachable.insert(next).second) + to_insert.insert(next); + + worklist = to_insert; + } + + return true; } // return arg's definition if it really reaches dest_bb @@ -1597,28 +1625,7 @@ void buildSubstitutions(const map>& CFGra for (SAPFOR::Argument* dep : dependencies_of_var->second) { - if (dep->getType() != CFG_ARG_TYPE::VAR) - { - can_use = false; - break; - } - - const auto& RD_In_of_curr_BB = cur_BB->getRD_In(); - auto dep_from_curr_RD_In = RD_In_of_curr_BB.find(dep); - - if (dep_from_curr_RD_In == RD_In_of_curr_BB.end()) - { - can_use = false; - break; - } - - const auto& RDs_for_dep = dep_from_curr_RD_In->second; - - set> defs_of_dep; - for (int def_instruction_num : RDs_for_dep) - defs_of_dep.insert(getInstructionAndBlockByNumber(CFGraph_for_project, def_instruction_num)); - - if (!ifVariableValid(dep, defs_of_dep, in_val.first, instr_and_bb)) + if (!isArgReaches(instr_num, instr_and_bb.second, dep, cur_BB)) { can_use = false; break; From 0a423234d24c1a3d47229a78aa7371fa6f67192d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=BE=D1=87?= =?UTF-8?q?=D0=B0=D1=80=D0=BC=D0=B8=D0=BD?= Date: Tue, 30 Jan 2024 21:58:25 +0300 Subject: [PATCH 25/40] dead code removing: make jumps always useful --- sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 4df0ca5..98beffd 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -63,6 +63,8 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru SAPFOR::CFG_OP::EXIT, SAPFOR::CFG_OP::DVM_DIR, SAPFOR::CFG_OP::SPF_DIR, + SAPFOR::CFG_OP::JUMP, + SAPFOR::CFG_OP::JUMP_IF, SAPFOR::CFG_OP::F_CALL //TODO: handle pure functions }; From 42fe628ce08f7f8f6901b340c6e247bf3fd52072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=BE=D1=87?= =?UTF-8?q?=D0=B0=D1=80=D0=BC=D0=B8=D0=BD?= Date: Fri, 2 Feb 2024 15:52:36 +0300 Subject: [PATCH 26/40] dead code removing: handle dead call statements --- .../_src/CFGraph/live_variable_analysis.cpp | 14 +++- .../_src/Transformations/dead_code.cpp | 67 ++++++++++++------- 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp index 5f79f52..ad41626 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/live_variable_analysis.cpp @@ -338,7 +338,7 @@ void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* ins if ((instr_operation == SAPFOR::CFG_OP::F_CALL || instr_operation == SAPFOR::CFG_OP::PARAM) && last_param_ref_index < 0) { auto func_it = funcByName.find(fName); - if (func_it != funcByName.end()) + if (interprocedural && func_it != funcByName.end()) { fcalls.push_back(LiveDeadVarsForCall(func_it->second, block, lastParamRef)); @@ -360,6 +360,18 @@ void getUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* ins use.insert(make_live.begin(), make_live.end()); def.insert(make_dead.begin(), make_dead.end()); } + else if (func_it != funcByName.end()) + { + int arg_num = lastParamRef.size(); + for (int i = 0; i < arg_num; i++) + { + if(func_it->second->funcParams.isArgOut(i)) + def.insert(lastParamRef[i]); + + if (func_it->second->funcParams.isArgIn(i)) + use.insert(lastParamRef[i]); + } + } else use.insert(lastParamRef.begin(), lastParamRef.end()); diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 98beffd..e09ce14 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -19,23 +19,19 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru set& use, set& def, vector& formal_parameters, vector& lastParamRef, int& last_param_ref_index, int& last_param_ref_size, - string& fName, + string& fName, const map& funcByName, bool& useful, bool& last_fcall_useful, set& usedByThisBlock) { set res, args; - if (fName == "") - last_fcall_useful = false; - vector fcalls; - getUseDefForInstruction(block, instr, args, res, formal_parameters, fcalls, lastParamRef, last_param_ref_index, last_param_ref_size, - fName, {}, + fName, funcByName, false ); @@ -62,17 +58,18 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru SAPFOR::CFG_OP::ENTRY, SAPFOR::CFG_OP::EXIT, SAPFOR::CFG_OP::DVM_DIR, - SAPFOR::CFG_OP::SPF_DIR, - SAPFOR::CFG_OP::JUMP, - SAPFOR::CFG_OP::JUMP_IF, - - SAPFOR::CFG_OP::F_CALL //TODO: handle pure functions + SAPFOR::CFG_OP::SPF_DIR }; if (always_useful.find(instr->getOperation()) != always_useful.end()) useful = true; - else if(instr->getOperation() == SAPFOR::CFG_OP::PARAM && last_fcall_useful) - useful = true; + else if (instr->getOperation() == SAPFOR::CFG_OP::F_CALL) + { + auto func_it = funcByName.find(instr->getArg1()->getValue()); + useful |= func_it == funcByName.end() || !(func_it->second->isPure); + } + else if (instr->getOperation() == SAPFOR::CFG_OP::PARAM) + useful |= last_fcall_useful; } if (useful) @@ -94,13 +91,16 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru insertIfVar(args.begin(), args.end(), usedByThisBlock); } + + if ((instr->getOperation() == SAPFOR::CFG_OP::F_CALL || instr->getOperation() == SAPFOR::CFG_OP::PARAM) && fName == "") + last_fcall_useful = false; } //Build use and def sets of block. Result are stored in use and def static void buildUseDef(SAPFOR::BasicBlock* block, set& use, set& def, vector& formal_parameters, vector& useful, - set& usedByThisBlock) + set& usedByThisBlock, const map& funcByName) { set use_with_regs = use, def_with_regs = def; @@ -120,7 +120,7 @@ static void buildUseDef(SAPFOR::BasicBlock* block, set& use, use_with_regs, def_with_regs, formal_parameters, lastParamRef, last_param_ref_index, last_param_ref_size, - fName, + fName, funcByName, u, last_fcall_useful, usedByThisBlock ); @@ -141,6 +141,7 @@ private: bool useful_jump = false; vector& formal_parameters; + const map& funcByName; public: bool updateJumpStatus() { @@ -228,7 +229,7 @@ public: use.insert(byArg.first); set usedByThisBlock; - buildUseDef(bb, use, def, this->formal_parameters, useful, usedByThisBlock); + buildUseDef(bb, use, def, this->formal_parameters, useful, usedByThisBlock, funcByName); auto in = bb->getLiveIn(), out = bb->getLiveOut(); @@ -288,15 +289,19 @@ public: return inserted; } - DeadCodeAnalysisNode(SAPFOR::BasicBlock* block, vector& formal_parameters) : - formal_parameters(formal_parameters) + DeadCodeAnalysisNode(SAPFOR::BasicBlock* block, + vector& formal_parameters, + const map& funcByName) + : + formal_parameters(formal_parameters), + funcByName(funcByName) { setBlock(block); useful.resize(block->getInstructions().size(), false); set use, def; set usedByThisBlock; - buildUseDef(getBlock(), use, def, this->formal_parameters, useful, usedByThisBlock); + buildUseDef(getBlock(), use, def, this->formal_parameters, useful, usedByThisBlock, funcByName); for (SAPFOR::Argument* arg : use) getBlock()->addLiveIn({ { arg, { getBlock() } } }); @@ -308,13 +313,16 @@ public: class DeadCodeAnalysis : public BackwardDataFlowAnalysis { protected: vector& formal_parameters; + const map& funcByName; DeadCodeAnalysisNode* createNode(SAPFOR::BasicBlock* block) override { - return new DeadCodeAnalysisNode(block, formal_parameters); + return new DeadCodeAnalysisNode(block, formal_parameters, funcByName); } public: - DeadCodeAnalysis(vector& formal_parameters) : - formal_parameters(formal_parameters) + DeadCodeAnalysis(vector& formal_parameters, const map& funcByName) + : + formal_parameters(formal_parameters), + funcByName(funcByName) { } }; @@ -378,7 +386,13 @@ void removeDeadCode(SgStatement* func, vector func_parameters(cfg_pair.first->funcParams.countOfPars, NULL); - DeadCodeAnalysis analysis_object(func_parameters); + map funcByName; + + for (auto& byFile : allFuncs) + for (auto byFunc : byFile.second) + funcByName[byFunc->funcName] = byFunc; + + DeadCodeAnalysis analysis_object(func_parameters, funcByName); analysis_object.fit(cfg_pair.second); analysis_object.analyze(); @@ -413,9 +427,12 @@ void removeDeadCode(SgStatement* func, SgStatement* end = func->lastNodeOfStmt(), *st = func; - set removable = + set removable = { - ASSIGN_STAT + ASSIGN_STAT, + PROC_STAT, + WRITE_STAT, + READ_STAT }; while (st != end) From d6a61190a816f55250f971ee6d9c30a1d7762408 Mon Sep 17 00:00:00 2001 From: Alexander_KS Date: Fri, 2 Feb 2024 17:33:12 +0000 Subject: [PATCH 27/40] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20sapfor/experts/Sapfor=5F2017/=5Fsrc/Utils/versio?= =?UTF-8?q?n.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sapfor/experts/Sapfor_2017/_src/Utils/version.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 97fb9d3..f0d5623 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 "2267" +#pragma once + +#define VERSION_SPF "2268" From 95479c4740a9eec15b41ff491ea8a8e2727f990d Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 2 Feb 2024 20:35:44 +0300 Subject: [PATCH 28/40] fixed modules processing --- sapfor/experts/Sapfor_2017/_src/Utils/version.h | 2 +- .../Sapfor_2017/_src/VerificationCode/CorrectVarDecl.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index f0d5623..0fd6ff0 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 "2268" +#define VERSION_SPF "2269" diff --git a/sapfor/experts/Sapfor_2017/_src/VerificationCode/CorrectVarDecl.cpp b/sapfor/experts/Sapfor_2017/_src/VerificationCode/CorrectVarDecl.cpp index 7c1a4be..69d2607 100644 --- a/sapfor/experts/Sapfor_2017/_src/VerificationCode/CorrectVarDecl.cpp +++ b/sapfor/experts/Sapfor_2017/_src/VerificationCode/CorrectVarDecl.cpp @@ -150,6 +150,7 @@ void fillUseStatement(SgStatement *st, set &useMod, { SgExpression *ex = st->expr(0); string modName = st->symbol()->identifier(); + convertToLower(modName); useMod.insert(modName); if (ex) From 05609dadc0283c086027cd695449415ffb1a1214 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 2 Feb 2024 21:02:11 +0300 Subject: [PATCH 29/40] fixed pure --- sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.cpp | 4 +++- sapfor/experts/Sapfor_2017/_src/Utils/version.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.cpp b/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.cpp index 7e2c855..c26682a 100644 --- a/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.cpp +++ b/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.cpp @@ -911,7 +911,9 @@ static void fillFunctionPureStatus(SgStatement *header, FuncInfo *currInfo, vect if (currInfo->commonBlocks.size() == 0) { lines.clear(); - bool has = hasThisIds(header, lines, { DATA_DECL, SAVE_DECL, USE_STMT }); + bool has = hasThisIds(header, lines, { DATA_DECL, SAVE_DECL, USE_STMT, + WRITE_STAT, READ_STAT, OPEN_STAT, CLOSE_STAT, + PRINT_STAT, STOP_STAT, PAUSE_NODE }); if (!has || declaratedAsPure) currInfo->isPure = true; else diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 0fd6ff0..a241adb 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 "2269" +#define VERSION_SPF "2270" From 37435104a75b438c539cf65c3541cfa2872d3029 Mon Sep 17 00:00:00 2001 From: mkoch Date: Fri, 2 Feb 2024 22:43:46 +0300 Subject: [PATCH 30/40] expression substitution: hotfix for recent commits (typo at condition) --- sapfor/experts/Sapfor_2017/_src/CFGraph/RD_subst.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/RD_subst.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/RD_subst.cpp index 387b142..06a906c 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/RD_subst.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/RD_subst.cpp @@ -1287,7 +1287,7 @@ bool isArgReaches(int decl_instr, SAPFOR::BasicBlock* decl_bb, reachable = banned_blocks; worklist = reachable; - while (worklist.size() != 0 || banned_instructions.size() == 0) + while (worklist.size() != 0 && banned_instructions.size() != 0) { if(worklist.find(dest_bb) != worklist.end()) return false; From 147ca037331ddb2f3c4200db272ca7dd41d747d5 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 9 Feb 2024 12:43:52 +0300 Subject: [PATCH 31/40] improved dead code pass --- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 4 ++-- sapfor/experts/Sapfor_2017/_src/Sapfor.h | 2 ++ sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h | 3 ++- sapfor/experts/Sapfor_2017/_src/Utils/version.h | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index a03ca66..a989408 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -1170,7 +1170,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne covertToC(file); else if (curr_regime == INSERT_NO_DISTR_FLAGS_FROM_GUI) addPrivatesToArraysFromGUI(file, declaredArrays, distrStateFromGUI); - else if (curr_regime == REMOVE_DEAD_CODE_AND_UNPARSE) + else if (curr_regime == REMOVE_DEAD_CODE) { auto funcsForFile = getObjectForFileFromMap(file_name, allFuncInfo); for (auto& func : funcsForFile) @@ -2522,7 +2522,6 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam findFunctionsToInclude(true); break; // all these cases run UNPARSE_FILE after - case REMOVE_DEAD_CODE_AND_UNPARSE: case RENAME_SYMBOLS: case RESOLVE_PAR_REGIONS: case CREATE_PARALLEL_REGIONS: @@ -2540,6 +2539,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam runAnalysis(*project, curr_regime, false); case SUBST_EXPR_RD_AND_UNPARSE: case SUBST_EXPR_AND_UNPARSE: + case REMOVE_DEAD_CODE_AND_UNPARSE: if (folderName) runAnalysis(*project, UNPARSE_FILE, true, "", folderName); else diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.h b/sapfor/experts/Sapfor_2017/_src/Sapfor.h index a66f577..d6ba9a7 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.h +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.h @@ -165,6 +165,7 @@ enum passes { BUILD_IR, LIVE_ANALYSIS_IR, PRIVATE_ANALYSIS_IR, + REMOVE_DEAD_CODE, REMOVE_DEAD_CODE_AND_UNPARSE, FIX_COMMON_BLOCKS, @@ -344,6 +345,7 @@ static void setPassValues() passNames[CALL_GRAPH_IR] = "CALL_GRAPH_IR"; passNames[LIVE_ANALYSIS_IR] = "LIVE_ANALYSIS_IR"; passNames[PRIVATE_ANALYSIS_IR] = "PRIVATE_ANALYSIS_IR"; + passNames[REMOVE_DEAD_CODE] = "REMOVE_DEAD_CODE"; passNames[REMOVE_DEAD_CODE_AND_UNPARSE] = "REMOVE_DEAD_CODE_AND_UNPARSE"; passNames[FIX_COMMON_BLOCKS] = "FIX_COMMON_BLOCKS"; diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h index dc83bb7..9c63fb1 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/PassManager.h @@ -306,7 +306,8 @@ void InitPassesDependencies(map> &passDepsIn, set Pass(REMOVE_OMP_DIRS) <= Pass(REMOVE_OMP_DIRS_TRANSFORM); - Pass(CALL_GRAPH2) <= Pass(REMOVE_DEAD_CODE_AND_UNPARSE); + 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); 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, diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index a241adb..231d077 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 "2270" +#define VERSION_SPF "2271" From 05bd8379dbf02102452a0235165728c193f9befd Mon Sep 17 00:00:00 2001 From: ALEXks Date: Sun, 11 Feb 2024 21:09:44 +0300 Subject: [PATCH 32/40] fixed inliner --- .../Sapfor_2017/_src/Inliner/inliner.cpp | 31 ++++++++++++++----- .../Sapfor_2017/_src/Utils/SgUtils.cpp | 11 ++++--- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp b/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp index a37d947..5716f4f 100644 --- a/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp @@ -264,7 +264,6 @@ static SgValueExp* zeroExpr = NULL; static vector getLowBounds(SgSymbol* arrayS) { - if (oneExpr == NULL) oneExpr = new SgValueExp(1); @@ -314,6 +313,8 @@ static vector getLowBounds(SgSymbol* arrayS) if (consistInAllocates != 1) list = NULL; + if (list == NULL) + __spf_print(1, "find for %s, consistInAllocates = %d", arrayS->identifier(), consistInAllocates); checkNull(list, convertFileName(__FILE__).c_str(), __LINE__); } else @@ -365,10 +366,20 @@ static SgArrayRefExp* addressPass(SgArrayRefExp* result, SgArrayRefExp* arrayExp { checkNull(boundsOld[z], convertFileName(__FILE__).c_str(), __LINE__); checkNull(boundsNew[z], convertFileName(__FILE__).c_str(), __LINE__); + SgExpression* shift = NULL; + SgExpression& baseShift = (boundsNew[z]->copy() - boundsOld[z]->copy()); + + if (arrayExpNew->subscript(z) && + !isEqExpressions(arrayExpNew->subscript(z), boundsNew[z], collection)) + { + shift = &(arrayExpNew->subscript(z)->copy() - boundsNew[z]->copy()); + } + + SgExpression& oldSub = arrayExpOld->subscript(z)->copy(); if (isEqExpressions(boundsOld[z], boundsNew[z], collection)) - result->addSubscript(arrayExpOld->subscript(z)->copy()); + result->addSubscript(shift ? (oldSub + *shift) : oldSub); else - result->addSubscript(arrayExpOld->subscript(z)->copy() + (boundsNew[z]->copy() - boundsOld[z]->copy())); + result->addSubscript(shift ? (oldSub + baseShift + *shift) : (oldSub + baseShift)); } return result; } @@ -454,12 +465,19 @@ static SgExpression* doReplace(SgExpression* oldExp, SgExpression* newExp, mapunparsestdout(); - //arrayExpNew->unparsestdout(); - //printf("\n"); + /*arrayExpOld->unparsestdout(); + arrayExpNew->unparsestdout(); + printf("\n");*/ auto boundsOld = getLowBounds(oldExp->symbol()); auto boundsNew = getLowBounds(newExp->symbol()); + + /*for (auto& elem : boundsOld) + elem->unparsestdout(); + printf("--\n"); + for (auto& elem : boundsNew) + elem->unparsestdout(); + printf("--\n");*/ const int numOldSubs = arrayExpOld->numberOfSubscripts(); const int numNewSubs = arrayExpNew->numberOfSubscripts(); @@ -508,7 +526,6 @@ static SgExpression* doReplace(SgExpression* oldExp, SgExpression* newExp, mapaddSubscript(arrayExpNew->subscript(boundsOld.size() + z)->copy()); - retVal = result; } else diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp index 6a16906..fd45c3b 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp @@ -3080,11 +3080,15 @@ static set applyModuleDeclsForFile(FileInfo *forFile, const map included; for (auto& incl : toIncl) { if (included.find(incl) == included.end()) + { include += " include '" + incl + "'\n"; + includeCount++; + } included.insert(incl); } @@ -3117,8 +3121,8 @@ static set applyModuleDeclsForFile(FileInfo *forFile, const mapfileName.c_str()); @@ -4161,7 +4165,7 @@ SgProject* createProject(const char* proj_name, } } filterModuleUse(moduleUsesByFile, moduleDecls); - + map shifts; //shiftLines if modules included const string shiftInfo = "!SPF NUM FILES"; @@ -4200,7 +4204,6 @@ SgProject* createProject(const char* proj_name, for (int z = 0; z < project->numberOfFiles(); ++z) { SgFile* file = &(project->file(z)); - for (SgStatement* st = file->firstStatement()->lexNext(); st; st = st->lexNext()) { string currF = st->fileName(); diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 231d077..0bc7431 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 "2271" +#define VERSION_SPF "2273" From 220cd86bf7b608d01d511882284326ddd1173f99 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 16 Feb 2024 14:06:30 +0300 Subject: [PATCH 33/40] fixed dowhile --- .../directive_creator_base.cpp | 36 +++++++++---------- .../directive_creator_base_nodist.cpp | 22 ++++++------ .../directive_creator_internal.h | 13 +++---- .../directive_creator_nodist.h | 1 - .../_src/Distribution/DvmhDirective.h | 5 ++- .../Distribution/DvmhDirective_nodist.cpp | 5 ++- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 3 +- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 8 files changed, 38 insertions(+), 49 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_base.cpp b/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_base.cpp index 8a10268..69bde5b 100644 --- a/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_base.cpp +++ b/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_base.cpp @@ -1095,9 +1095,16 @@ static bool tryToResolveUnmatchedDims(const map> &dim tmpL = loop->parent; while (tmpL) { - if (!tmpL->isFor) // TODO: need to add all inductive variables! - ;// return false; + { + SgWhileStmt* dow = isSgWhileStmt(tmpL->loop->GetOriginal()); + if (dow->conditional()) + { + SgExpression* cond = dow->conditional(); + if (cond->lhs() && cond->lhs()->variant() == VAR_REF) + deprecateToMatch.insert(cond->lhs()->symbol()->identifier()); + } + } else deprecateToMatch.insert(tmpL->loopSymbol); tmpL = tmpL->parent; @@ -1802,14 +1809,6 @@ static bool addRedistributionDirs(File* file, const vector& reducedG, DIST::Arrays& allArrays, @@ -1826,13 +1825,12 @@ void selectParallelDirectiveForVariant(File* file, ParallelRegion* currParReg, for (int i = 0; i < loopGraph.size(); ++i) { LoopGraph* loop = loopGraph[i]; - const uint64_t loopRegId = (uint64_t)loop; const bool hasDirective = loop->directive; const bool noLimits = loop->hasLimitsToParallel() == false; const bool isMyRegion = loop->region == currParReg; const bool noUserDir = loop->userDvmDirective == NULL; DIST::Array* sameAlignTemplate = NULL; - const bool sameAligns = loop->isArrayTemplatesTheSame(sameAlignTemplate, getTrueRegId(regionId, loopRegId), arrayLinksByFuncCalls); + const bool sameAligns = loop->isArrayTemplatesTheSame(sameAlignTemplate, regionId, arrayLinksByFuncCalls); bool freeLoopDistr = true; if (hasDirective && loop->directive->arrayRef2->IsLoopArray()) @@ -1871,18 +1869,18 @@ void selectParallelDirectiveForVariant(File* file, ParallelRegion* currParReg, bool topCheck = isOnlyTopPerfect(loop, distribution); bool needToContinue = false; - if (topCheck && mpiProgram == 0) + if (topCheck) { // -> dims not mached map> dimsNotMatch; - if (!checkCorrectness(*parDirective, distribution, reducedG, allArrays, arrayLinksByFuncCalls, loop->getAllArraysInLoop(), messages, loop->lineNum, dimsNotMatch, getTrueRegId(regionId, loopRegId))) + if (!checkCorrectness(*parDirective, distribution, reducedG, allArrays, arrayLinksByFuncCalls, loop->getAllArraysInLoop(), messages, loop->lineNum, dimsNotMatch, regionId)) { - if (!tryToResolveUnmatchedDims(dimsNotMatch, loop, getTrueRegId(regionId, loopRegId), parDirective, reducedG, allArrays, arrayLinksByFuncCalls, distribution, mapFuncInfo)) - needToContinue = addRedistributionDirs(file, distribution, toInsert, loop, mapLoopsInFile, parDirective, getTrueRegId(regionId, loopRegId), messages, arrayLinksByFuncCalls, sameAlignTemplate); + if (!tryToResolveUnmatchedDims(dimsNotMatch, loop, regionId, parDirective, reducedG, allArrays, arrayLinksByFuncCalls, distribution, mapFuncInfo)) + needToContinue = addRedistributionDirs(file, distribution, toInsert, loop, mapLoopsInFile, parDirective, regionId, messages, arrayLinksByFuncCalls, sameAlignTemplate); } } - else if (mpiProgram == 0) - needToContinue = addRedistributionDirs(file, distribution, toInsert, loop, mapLoopsInFile, parDirective, getTrueRegId(regionId, loopRegId), messages, arrayLinksByFuncCalls, sameAlignTemplate); + else + needToContinue = addRedistributionDirs(file, distribution, toInsert, loop, mapLoopsInFile, parDirective, regionId, messages, arrayLinksByFuncCalls, sameAlignTemplate); if (needToContinue) continue; @@ -1890,7 +1888,7 @@ void selectParallelDirectiveForVariant(File* file, ParallelRegion* currParReg, vector> newRules; constructRules(newRules, distribution, loop); - Directive* dirImpl = parDirective->genDirective(file, newRules, loop, reducedG, allArrays, getTrueRegId(regionId, loopRegId), arrayLinksByFuncCalls); + Directive* dirImpl = parDirective->genDirective(file, newRules, loop, reducedG, allArrays, regionId, arrayLinksByFuncCalls); #if __SPF //move label before loop diff --git a/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_base_nodist.cpp b/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_base_nodist.cpp index e0a20ee..0387428 100644 --- a/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_base_nodist.cpp +++ b/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_base_nodist.cpp @@ -102,20 +102,18 @@ void createParallelDirectivesNoDist(const map& allArrays, - const vector& loopGraph, - const map& mapLoopsInFile, - const map& mapFuncInfo, - vector& toInsert, - const uint64_t regionId, - const map>& arrayLinksByFuncCalls, - const map& depInfoForLoopGraph, - vector& messages) + DIST::Arrays& allArrays, + const vector& loopGraph, + const map& mapLoopsInFile, + const map& mapFuncInfo, + vector& toInsert, + const map>& arrayLinksByFuncCalls, + const map& depInfoForLoopGraph, + vector& messages) { for (int i = 0; i < loopGraph.size(); ++i) { LoopGraph* loop = loopGraph[i]; - const uint64_t loopRegId = (uint64_t)loop; const bool hasDirective = loop->directive; const bool noLimits = loop->hasLimitsToParallel() == false; const bool isMyRegion = loop->region == currParReg; @@ -133,7 +131,7 @@ void selectParallelDirectiveForVariantNoDist(File* file, ParallelRegion* currPar parDirective = loop->recalculateParallelDirective(); // rewrite bool topCheck = isOnlyTopPerfect(loop, distribution); - Directive* dirImpl = parDirective->genDirectiveNoDist(file, loop, allArrays, getTrueRegId(regionId, loopRegId), arrayLinksByFuncCalls); + Directive* dirImpl = parDirective->genDirectiveNoDist(file, loop, allArrays, arrayLinksByFuncCalls); #if __SPF //move label before loop @@ -167,7 +165,7 @@ void selectParallelDirectiveForVariantNoDist(File* file, ParallelRegion* currPar { if (loopGraph[i]->children.size() != 0) selectParallelDirectiveForVariantNoDist(file, currParReg, allArrays, loopGraph[i]->children, mapLoopsInFile, mapFuncInfo, - toInsert, regionId, arrayLinksByFuncCalls,depInfoForLoopGraph, messages); + toInsert, arrayLinksByFuncCalls,depInfoForLoopGraph, messages); } } } diff --git a/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_internal.h b/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_internal.h index 0e26729..0061885 100644 --- a/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_internal.h +++ b/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_internal.h @@ -16,11 +16,8 @@ void addShadowFromAnalysis(ParallelDirective* dir, const std::map& currAccesses); -bool checkForConflict(const std::map& currAccesses, - const LoopGraph* currentLoop, - std::map>, DIST::ArrayComparator>& arrayWriteAcc, - const std::vector, std::vector>>>& acrossInfo, - std::set& acrossOutArrays); - -uint64_t getTrueRegId(const uint64_t regId, const uint64_t loopRegId); - +bool checkForConflict(const std::map& currAccesses, + const LoopGraph* currentLoop, + std::map>, DIST::ArrayComparator>& arrayWriteAcc, + const std::vector, std::vector>>>& acrossInfo, + std::set& acrossOutArrays); diff --git a/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_nodist.h b/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_nodist.h index 8a340a4..22e04ec 100644 --- a/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_nodist.h +++ b/sapfor/experts/Sapfor_2017/_src/DirectiveProcessing/directive_creator_nodist.h @@ -21,7 +21,6 @@ void selectParallelDirectiveForVariantNoDist(File* file, ParallelRegion* currPar const std::map& mapLoopsInFile, const std::map& mapFuncInfo, std::vector& toInsert, - const uint64_t regionId, const std::map>& arrayLinksByFuncCalls, const std::map& depInfoForLoopGraph, std::vector& messages); \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/Distribution/DvmhDirective.h b/sapfor/experts/Sapfor_2017/_src/Distribution/DvmhDirective.h index 32b5618..24a4e88 100644 --- a/sapfor/experts/Sapfor_2017/_src/Distribution/DvmhDirective.h +++ b/sapfor/experts/Sapfor_2017/_src/Distribution/DvmhDirective.h @@ -144,9 +144,8 @@ public: const std::map> &arrayLinksByFuncCalls); Directive* - genDirectiveNoDist(File* file, LoopGraph* currLoop, - DIST::Arrays& allArrays, const uint64_t regionId, - const std::map>& arrayLinksByFuncCalls); + genDirectiveNoDist(File* file, LoopGraph* currLoop, DIST::Arrays& allArrays, + const std::map>& arrayLinksByFuncCalls); friend ParallelDirective* operator+(const ParallelDirective &first, const ParallelDirective &second); diff --git a/sapfor/experts/Sapfor_2017/_src/Distribution/DvmhDirective_nodist.cpp b/sapfor/experts/Sapfor_2017/_src/Distribution/DvmhDirective_nodist.cpp index e31b61b..3860916 100644 --- a/sapfor/experts/Sapfor_2017/_src/Distribution/DvmhDirective_nodist.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Distribution/DvmhDirective_nodist.cpp @@ -91,9 +91,8 @@ compliteTieListNoDist(const LoopGraph* currLoop, const vector& loops } Directive* -ParallelDirective::genDirectiveNoDist(File* file, LoopGraph* currLoop, - DIST::Arrays& allArrays, const uint64_t regionId, - const map>& arrayLinksByFuncCalls) +ParallelDirective::genDirectiveNoDist(File* file, LoopGraph* currLoop, DIST::Arrays& allArrays, + const map>& arrayLinksByFuncCalls) { const set& acrossOutAttribute = currLoop->acrossOutAttribute; const map, vector>>& readOps = currLoop->readOps; diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index a989408..2dd6f25 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -729,8 +729,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne depInfoForLoopGraphV[elem.first] = elem.second; selectParallelDirectiveForVariantNoDist(new File(file), parallelRegions[z], allArrays, loopsInFile, mapLoopsInFile, mapFuncInfo, - toInsert, parallelRegions[z]->GetId(), arrayLinksByFuncCalls, - depInfoForLoopGraphV, getObjectForFileFromMap(file_name, SPF_messages)); + toInsert, arrayLinksByFuncCalls, depInfoForLoopGraphV, getObjectForFileFromMap(file_name, SPF_messages)); if (toInsert.size() > 0) { diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 0bc7431..6b1b8c9 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 "2273" +#define VERSION_SPF "2274" From 4c9de9e7578cbf1577f93929c7e217e8615cb1f8 Mon Sep 17 00:00:00 2001 From: Mikhail Kocharmin Date: Sun, 18 Feb 2024 23:55:47 +0300 Subject: [PATCH 34/40] enable directive filtering for shared memory case --- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index 2dd6f25..3edd5a6 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -2040,7 +2040,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne swapLoopsForParallel(loopGraph, SPF_messages, 1); else if (curr_regime == RESTORE_SWAP_LOOPS) swapLoopsForParallel(loopGraph, SPF_messages, -1); - else if (curr_regime == CREATE_PARALLEL_DIRS) + else if (curr_regime == CREATE_PARALLEL_DIRS || curr_regime == INSERT_PARALLEL_DIRS_NODIST) filterParallelDirectives(loopGraph, createdDirectives); else if (curr_regime == INLINE_PROCEDURES) callInliner(allFuncInfo, inDataProc, inDataChains, inDataChainsStart, SPF_messages, commonBlocks); From 3221934db8134a48a028ebfad4a99ef7a981f01e Mon Sep 17 00:00:00 2001 From: ALEXks Date: Mon, 19 Feb 2024 09:13:49 +0300 Subject: [PATCH 35/40] 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 6b1b8c9..1a06b02 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 "2274" +#define VERSION_SPF "2275" From 8402f8c7e9f17010d65e944bf507d779dcc97d87 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Tue, 20 Feb 2024 11:12:00 +0300 Subject: [PATCH 36/40] code refactoring --- dvm/fdvm/trunk/fdvm/acc.cpp | 350 +++++-- dvm/fdvm/trunk/fdvm/acc_across.cpp | 26 +- dvm/fdvm/trunk/fdvm/dvm.cpp | 50 +- dvm/fdvm/trunk/fdvm/funcall.cpp | 49 +- dvm/fdvm/trunk/fdvm/parloop.cpp | 31 +- dvm/fdvm/trunk/include/dvm.h | 13 +- dvm/fdvm/trunk/include/libdvm.h | 6 +- dvm/fdvm/trunk/include/libnum.h | 2 + sapfor/experts/Sapfor_2017/CMakeLists.txt | 12 +- .../_src/ProjectManipulation/FileInfo.cpp | 62 ++ .../_src/ProjectManipulation/FileInfo.h | 50 + .../_src/ProjectManipulation/ParseFiles.cpp | 789 +++++++++++++++ .../_src/ProjectManipulation/ParseFiles.h | 7 + .../_src/ProjectManipulation/PerfAnalyzer.cpp | 38 + .../_src/ProjectManipulation/PerfAnalyzer.h | 3 + .../_src/ProjectManipulation/StdCapture.h | 190 ++++ sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 62 +- sapfor/experts/Sapfor_2017/_src/SapforData.h | 1 + .../Sapfor_2017/_src/Utils/SgUtils.cpp | 922 +----------------- .../experts/Sapfor_2017/_src/Utils/SgUtils.h | 7 +- .../experts/Sapfor_2017/_src/Utils/utils.cpp | 75 +- sapfor/experts/Sapfor_2017/_src/Utils/utils.h | 48 +- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- .../_src/VisualizerCalls/get_information.cpp | 2 + 24 files changed, 1623 insertions(+), 1174 deletions(-) create mode 100644 sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.cpp create mode 100644 sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.h create mode 100644 sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp create mode 100644 sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.h create mode 100644 sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.cpp create mode 100644 sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.h create mode 100644 sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h diff --git a/dvm/fdvm/trunk/fdvm/acc.cpp b/dvm/fdvm/trunk/fdvm/acc.cpp index 143323c..995a215 100644 --- a/dvm/fdvm/trunk/fdvm/acc.cpp +++ b/dvm/fdvm/trunk/fdvm/acc.cpp @@ -655,6 +655,26 @@ SgSymbol *HostProcSymbol(SgStatement *st_do) return(s); } +SgSymbol *HostAcrossProcSymbol(SgSymbol *sHostProc, int dependency) +{ + SgSymbol *s; + char *sname = (char *)malloc((unsigned)(strlen(sHostProc->identifier())) + 5); + sprintf(sname, "%s_%d", sHostProc->identifier(), dependency); + s = new SgSymbol(PROCEDURE_NAME, sname, *current_file->firstStatement()); + acc_func_list = AddToSymbList(acc_func_list, s); + return(s); +} + +SgSymbol *HostProcSymbol_RA(SgSymbol *sHostProc) +{ + SgSymbol *s; + char *sname = (char *)malloc((unsigned)(strlen(sHostProc->identifier())) + 4); + sprintf(sname, "%s_%s", sHostProc->identifier(), "RA"); + s = new SgSymbol(PROCEDURE_NAME, sname, *current_file->firstStatement()); + acc_func_list = AddToSymbList(acc_func_list, s); + return(s); +} + SgSymbol *IndirectFunctionSymbol(SgStatement *stmt, char *name) { char *sname = (char *)malloc((unsigned)(strlen(stmt->fileName())) + 40); @@ -675,15 +695,6 @@ SgSymbol *GPUModuleSymb(SgStatement *global_st) return(mod_symb); } -SgSymbol *HostAcrossProcSymbol(SgSymbol *sHostProc, int dependency) -{ - SgSymbol *s; - char *sname = (char *)malloc((unsigned)(strlen(sHostProc->identifier())) + 5); - sprintf(sname, "%s_%d", sHostProc->identifier(), dependency); - s = new SgSymbol(PROCEDURE_NAME, sname, *current_file->firstStatement()); - acc_func_list = AddToSymbList(acc_func_list, s); - return(s); -} SgSymbol *CudaforSymb(SgStatement *global_st) { @@ -2545,7 +2556,7 @@ void ACC_ParallelLoopEnd(SgStatement *pardo) // creating host-handler for loop anyway if (!WithAcrossClause()) - Create_Host_Loop_Subroutine(hostproc_symb, 0); + Create_Host_Loop_Subroutine_Main(hostproc_symb); else { Create_Host_Across_Loop_Subroutine(hostproc_symb); @@ -2714,8 +2725,8 @@ void ACC_CreateParallelLoop(int ipl, SgStatement *first_do, int nloop, SgStateme number_of_loop_line = first_do->lineNumber(); // creating buffers for remote_access references (after creating GPU module) - if (rma && !rma->rmout && !rma->rml->symbol()) // there is synchronous REMOTE_ACCESS clause in PARALLEL directive - CreateRemoteAccessBuffers(); + //if (rma && !rma->rmout && !rma->rml->symbol()) // there is synchronous REMOTE_ACCESS clause in PARALLEL directive + CreateRemoteAccessBuffersUp(); if (cur_region) { // is first loop of compute region @@ -2785,7 +2796,7 @@ SgStatement *ACC_CreateStatementGroup(SgStatement *first_st) // Generating statements for block (sequence) in source program unit cur_st = first_st->lexPrev();//last_st; //doStatementsInSourceProgramUnit(first_st, 0, NULL, NULL, adapter_symb, hostproc_symb, 0, NULL, NULL, NULL, NULL); - doStatementsToPerformByHandler(CreateLoopForSequence(first_st),adapter_symb, hostproc_symb, 0, 1); + doStatementsToPerformByHandler(CreateLoopForSequence(first_st),adapter_symb, hostproc_symb, 0, parloop_by_handler); st_end = cur_st; // --------------------------------------------------- if ((cur_region->targets & CUDA_DEVICE)) //if(targets[CUDA]) @@ -3238,12 +3249,13 @@ void ACC_ReductionVarsAreActual() } } -void CreateRemoteAccessBuffers() +void CreateRemoteAccessBuffers(SgExpression *rml, int pl_flag) { SgExpression *el; rem_var *remv; coeffs *scoef; - for (el = rma->rml; el; el = el->rhs()) + int interface = parloop_by_handler == 2 && WhatInterface(dvm_parallel_dir) == 2 ? 2 : 1; + for (el = rml; el; el = el->rhs()) { remv = (rem_var *)(el->lhs())->attributeValue(0, REMOTE_VARIABLE); if(!remv) continue; // error case: illegal reference in REMOTE_ACCESS directive/clause @@ -3254,10 +3266,28 @@ void CreateRemoteAccessBuffers() // scoef = BufferCoeffs(remv->buffer,el->lhs()->symbol()); // adding the attribute (ARRAY_COEF) to buffer symbol remv->buffer->addAttribute(ARRAY_COEF, (void*)scoef, sizeof(coeffs)); + if (pl_flag && interface == 2) + remv->buffer->addAttribute(REMOTE_ACCESS_BUF, (void*)1, 0); } return; } +void CreateRemoteAccessBuffersUp() +{ + rem_acc *r; + //looking through the remote-access directive/clause list + for (r=rma; r; r=r->next) + { + //if (r->rml->symbol()) // asynchronous REMOTE_ACCESS clause/directive + // continue; + if (!r->rmout) // REMOTE_ACCESS clause in PARALLEL directive + CreateRemoteAccessBuffers(r->rml, 1); + else + CreateRemoteAccessBuffers(r->rml, 0); + } + return; +} + SgSymbol *CreateReplicatedArray(SgSymbol *s) { SgSymbol *ar; @@ -3508,16 +3538,19 @@ SgExpression *RemoteAccessHeaderList() { SgExpression *el, *l, *rma_list; rem_var *remv; + rem_acc *r; rma_list = NULL; - if (rma && !rma->rmout && !rma->rml->symbol()) // there is synchronous REMOTE_ACCESS clause in PARALLEL directive - for (el = rma->rml; el; el = el->rhs()) + for (r=rma; r; r=r->next) { - remv = (rem_var *)(el->lhs())->attributeValue(0, REMOTE_VARIABLE); - if(!remv) continue; // error case: illegal reference in REMOTE_ACCESS directive/clause - l = new SgExprListExp(*DVM000(remv->index)); - l->setRhs(rma_list); - rma_list = l; - //rma_list = AddListToList(rma_list, l ); + for (el = r->rml; el; el = el->rhs()) + { + remv = (rem_var *)(el->lhs())->attributeValue(0, REMOTE_VARIABLE); + if(!remv) continue; // error case: illegal reference in REMOTE_ACCESS directive/clause + l = new SgExprListExp(*DVM000(remv->index)); + l->setRhs(rma_list); + rma_list = l; + //rma_list = AddListToList(rma_list, l ); + } } return(rma_list); } @@ -3526,13 +3559,21 @@ void AddRemoteAccessBufferList_ToArrayList() { SgExpression *el; rem_var *remv; - if (rma && !rma->rmout && !rma->rml->symbol()) // there is synchronous REMOTE_ACCESS clause in PARALLEL directive - for (el = rma->rml; el; el = el->rhs()) + rem_acc *r; + //looking through the remote-access directive/clause list + for (r=rma; r; r=r->next) { - remv = (rem_var *)(el->lhs())->attributeValue(0, REMOTE_VARIABLE); - if(!remv) continue; // error case: illegal reference in REMOTE_ACCESS directive/clause - acc_array_list = AddNewToSymbList(acc_array_list, remv->buffer); + //if (r->rml->symbol()) // asynchronous REMOTE_ACCESS clause/directive + // continue; + for (el = r->rml; el; el = el->rhs()) + { + remv = (rem_var *)(el->lhs())->attributeValue(0, REMOTE_VARIABLE); + if (remv && remv->buffer) + acc_array_list = AddNewToSymbList(acc_array_list, remv->buffer); + } + } + return; } @@ -3570,13 +3611,15 @@ SgExpression *BaseArgumentList() { symb_list *sl, *array_list; SgExpression *el, *l, *base_list = NULL; + rem_acc *r; // create memory base list array_list = NULL; // create remote_access objects list - if (rma && !rma->rmout && !rma->rml->symbol()) // there is synchronous REMOTE_ACCESS clause in PARALLEL directive - for (el = rma->rml; el; el = el->rhs()) - array_list = AddToSymbList(array_list, el->lhs()->symbol()); - + for (r=rma; r; r=r->next) + { + for (el = r->rml; el; el = el->rhs()) + array_list = AddToSymbList(array_list, el->lhs()->symbol()); + } if (array_list) { base_list = ElementOfBaseList(NULL, array_list->symb); @@ -3633,7 +3676,7 @@ SgExpression *AddrArgumentList() symb_list *sl; SgExpression *el, *l, *addr_list = NULL, *ae, *rem_list = NULL; rem_var *remv; - + rem_acc *r; // create array address list if (acc_array_list) { @@ -3647,17 +3690,25 @@ SgExpression *AddrArgumentList() } } // create remote_access buffer address list and add it to addr_list - if (rma && !rma->rmout && !rma->rml->symbol()) // there is synchronous REMOTE_ACCESS clause in PARALLEL directive - for (el = rma->rml; el; el = el->rhs()) + + //looking through the remote-access directive/clause list + for (r=rma; r; r=r->next) { - remv = (rem_var *)(el->lhs())->attributeValue(0, REMOTE_VARIABLE); - if(!remv) continue; // error case: illegal reference in REMOTE_ACCESS directive/clause - ae = DVM000(remv->index + remv->ncolon + 1); - l = new SgExprListExp(*new SgArrayRefExp(*baseMemory(el->lhs()->symbol()->type()->baseType()), *ae)); - l->setRhs(rem_list); - rem_list = l; + for (el = r->rml; el; el = el->rhs()) + { + remv = (rem_var *)(el->lhs())->attributeValue(0, REMOTE_VARIABLE); + if(!remv) continue; // error case: illegal reference in REMOTE_ACCESS directive/clause + if (IS_REMOTE_ACCESS_BUFFER(remv->buffer) ) + l = new SgExprListExp(*new SgArrayRefExp(*baseMemory(el->lhs()->symbol()->type()->baseType()))); + else + { + ae = DVM000(remv->index + remv->ncolon + 1); + l = new SgExprListExp(*new SgArrayRefExp(*baseMemory(el->lhs()->symbol()->type()->baseType()), *ae)); + } + l->setRhs(rem_list); + rem_list = l; + } } - addr_list = AddListToList(rem_list, addr_list); return(addr_list); } @@ -5523,12 +5574,12 @@ SgStatement *Create_Host_Across_Loop_Subroutine(SgSymbol *sHostProc) { SgStatement *stmt = NULL, *st_end = NULL, *st_hedr = NULL, *cur = NULL, *last_decl = NULL; SgExpression *ae = NULL, *arg_list = NULL, *el = NULL, *de = NULL, *tail = NULL, *baseMem_list = NULL; - SgSymbol *s_loop_ref = NULL, *sarg = NULL, *h_first = NULL, *hl = NULL; + SgSymbol *s_loop_ref = NULL, *sarg = NULL, *h_first = NULL, *h_last = NULL,*hl = NULL; symb_list *sl = NULL; SgType *tdvm = NULL; - int ln; + int ln, nbuf = 0; char *name = NULL; - + SgExprListExp *list = isSgExprListExp(dvm_parallel_dir->expr(2)); // do_variables list SgSymbol *sHostAcrossProc; symb_list *acc_acr_call_list = NULL; @@ -5568,14 +5619,20 @@ SgStatement *Create_Host_Across_Loop_Subroutine(SgSymbol *sHostProc) if (!ln) h_first = sarg; } - + h_last = sarg; // add dvm-array-address list if (options.isOn(O_HOST)) { tail = arg_list; for (sl = acc_array_list, hl = h_first; sl; sl = sl->next, hl = hl->next()) { - sarg = DummyDvmArraySymbol(sl->symb, hl); + if (IS_REMOTE_ACCESS_BUFFER(sl->symb)) // case of RTS2 interface + { + sarg = DummyDvmBufferSymbol(sl->symb, hl); + nbuf++; + } + else + sarg = DummyDvmArraySymbol(sl->symb, hl); ae = new SgArrayRefExp(*sarg); arg_list->setRhs(*new SgExprListExp(*ae)); arg_list = arg_list->rhs(); @@ -5666,6 +5723,33 @@ SgStatement *Create_Host_Across_Loop_Subroutine(SgSymbol *sHostProc) //stmt = PrintStat(which_run_expr); //st_end->insertStmtBefore(*stmt, *st_hedr); + // create argument list of handler's call + SgExpression *new_arg_list = &st_hedr->expr(0)->copy(); + if (nbuf > 0) // there is REMOTE_ACCESS clause and RTS2 interface is used + // correct argument list of handler's call + { + el = new_arg_list->rhs(); + while(el->lhs()->symbol() != h_last->next()) + el = el->rhs(); + for (sl = acc_array_list, hl = h_first; sl; sl = sl->next, hl = hl->next(), el = el->rhs()) + { + if (IS_REMOTE_ACCESS_BUFFER(sl->symb)) + { + // correct argument: buffer => buffer(buf_header(Rank+2)) + SgArrayRefExp *buf_ref = new SgArrayRefExp(*hl,*new SgValueExp(Rank(sl->symb)+2)); + el->lhs()->setLhs(*new SgExprListExp(*buf_ref)); + // generate call statements of 'dvmh_loop_get_remote_buf' for remote access buffers + stmt = GetRemoteBuf(s_loop_ref, nbuf--, hl); + last_decl->insertStmtAfter(*stmt, *st_hedr); + } + } + // create external statement + stmt = new SgStatement(EXTERN_STAT); + el = new SgExprListExp(*new SgVarRefExp(fdvm[GET_REMOTE_BUF])); + stmt->setExpression(0, *el); + last_decl->insertStmtAfter(*stmt, *st_hedr); + } + SgIfStmt *ifstmt = NULL; SgStatement *falsestmt = NULL; int i = 0; @@ -5678,12 +5762,12 @@ SgStatement *Create_Host_Across_Loop_Subroutine(SgSymbol *sHostProc) fbtest->addArg(*new SgValueExp(i - 1)); if (i != 0) { - SgCallStmt *truestmt = new SgCallStmt(*sl->symb, *st_hedr->expr(0)); + SgCallStmt *truestmt = new SgCallStmt(*sl->symb, *new_arg_list); ifstmt = new SgIfStmt(*fbtest, *truestmt, *falsestmt); falsestmt = ifstmt; } else { - falsestmt = new SgCallStmt(*sl->symb, *st_hedr->expr(0)); + falsestmt = new SgCallStmt(*sl->symb, *new_arg_list); } i++; } @@ -5692,6 +5776,148 @@ SgStatement *Create_Host_Across_Loop_Subroutine(SgSymbol *sHostProc) return(st_hedr); } +SgStatement *Create_Host_Loop_Subroutine_Main (SgSymbol *sHostProc) +{ + SgStatement *stmt = NULL, *st_end = NULL, *st_hedr = NULL, *last_decl = NULL; + SgExpression *ae, *arg_list = NULL, *el = NULL, *de = NULL, *tail = NULL, *baseMem_list = NULL; + SgSymbol *s_loop_ref = NULL, *sarg = NULL, *h_first = NULL, *h_last = NULL, *hl = NULL, *bl = NULL; + SgSymbol *s = NULL; + symb_list *sl = NULL; + int ln, nbuf = 0; + SgSymbol *sHostProc_RA; + + if(rma && !rma->rmout && !rma->rml->symbol() && parloop_by_handler == 2 && WhatInterface(dvm_parallel_dir) == 2 )// there is synchronous REMOTE_ACCESS clause in PARALLEL directive and RTS2 interface is used + // create additional procedure for creating headers of remote access buffers + { + sHostProc_RA = HostProcSymbol_RA(sHostProc); + Create_Host_Loop_Subroutine (sHostProc_RA, 0); + } + else + return (Create_Host_Loop_Subroutine (sHostProc, 0)); + + // create Host procedure header and end for subroutine named by sHostProc + + st_hedr = CreateHostProcedure(sHostProc); + st_hedr->addComment(Host_LoopHandlerComment()); + st_end = st_hedr->lexNext(); + + // create dummy argument list + // loop_ref,,, + + s_loop_ref = new SgSymbol(VARIABLE_NAME, "loop_ref", *FortranDvmType(), *st_hedr); + + ae = new SgVarRefExp(s_loop_ref); + arg_list = new SgExprListExp(*ae); + st_hedr->setExpression(0, *arg_list); + + // add dvm-array-header list + for (sl = acc_array_list, ln = 0; sl; sl = sl->next, ln++) + { + sarg = DummyDvmHeaderSymbol(sl->symb,st_hedr); + ae = new SgArrayRefExp(*sarg); + arg_list->setRhs(*new SgExprListExp(*ae)); + arg_list = arg_list->rhs(); + if (!ln) + h_first = sarg; + } + h_last = sarg; + + // add dvm-array-address list + if (options.isOn(O_HOST)) + { + tail = arg_list; + for (sl = acc_array_list, hl = h_first; sl; sl = sl->next, hl = hl->next()) + { + if(IS_REMOTE_ACCESS_BUFFER(sl->symb)) + { + sarg = DummyDvmBufferSymbol(sl->symb, hl); + nbuf++; + } + else + sarg = DummyDvmArraySymbol(sl->symb, hl); + ae = new SgArrayRefExp(*sarg); + arg_list->setRhs(*new SgExprListExp(*ae)); + arg_list = arg_list->rhs(); + } + tail = tail->rhs(); + } + else + // create memory base list and add it to the dummy argument list + { + baseMem_list = tail = CreateBaseMemoryList(); + AddListToList(arg_list, baseMem_list); + } + + // add use's list to dummy argument list + if (uses_list) + { + AddListToList(arg_list, copy_uses_list = &(uses_list->copy())); + if (!tail) + tail = copy_uses_list; + } + if(red_list) + { + SgExpression * red_bound_list; + AddListToList(arg_list, red_bound_list = DummyListForReductionArrays(st_hedr)); + if(!tail) + tail = red_bound_list; + } + + // create external statement + stmt = new SgStatement(EXTERN_STAT); + el = new SgExprListExp(*new SgVarRefExp(fdvm[GET_REMOTE_BUF])); + el->setRhs(*new SgExprListExp(*new SgVarRefExp(sHostProc_RA))); + stmt->setExpression(0, *el); + st_hedr->insertStmtAfter(*stmt, *st_hedr); + + last_decl = stmt; + + // create dummy argument declarations + + for (el = tail; el; el = el->rhs()) + { + stmt = el->lhs()->symbol()->makeVarDeclStmt(); + ConstantSubstitutionInTypeSpec(stmt->expr(1)); + st_hedr->insertStmtAfter(*stmt, *st_hedr); + } + + el = st_hedr->expr(0); + stmt = el->lhs()->symbol()->makeVarDeclStmt(); + st_hedr->insertStmtAfter(*stmt, *st_hedr); + de = stmt->expr(0); + + for (el = el->rhs(); el != tail; el = el->rhs()) + { //printf("%s \n",el->lhs()->symbol()->identifier()); + de->setRhs(new SgExprListExp(*el->lhs()->symbol()->makeDeclExpr())); + de = de->rhs(); + } + + // generate IMPLICIT NONE statement + st_hedr->insertStmtAfter(*new SgStatement(IMPL_DECL), *st_hedr); + + // generate handler call + stmt = new SgCallStmt(*sHostProc_RA, (st_hedr->expr(0))->copy()); + last_decl->insertStmtAfter(*stmt, *st_hedr); + el = stmt->expr(0)->rhs(); + // correct argument list of handler call + while(el->lhs()->symbol() != h_last->next()) + el = el->rhs(); + for (sl = acc_array_list, hl = h_first; sl; sl = sl->next, hl = hl->next(), el = el->rhs()) + { + if (IS_REMOTE_ACCESS_BUFFER(sl->symb)) + { + // correct argument: buffer => buffer(buf_header(Rank+2)) + SgArrayRefExp *buf_ref = new SgArrayRefExp(*hl,*new SgValueExp(Rank(sl->symb)+2)); + el->lhs()->setLhs(*new SgExprListExp(*buf_ref)); + // generate call statements of 'dvmh_loop_get_remote_buf' for remote access buffers + stmt = GetRemoteBuf(s_loop_ref, nbuf--, hl); + last_decl->insertStmtAfter(*stmt, *st_hedr); + } + } + + return (st_hedr); +} + SgStatement *Create_Host_Loop_Subroutine(SgSymbol *sHostProc, int dependency) { SgStatement *stmt = NULL, *st_end = NULL, *st_hedr = NULL, *cur = NULL, *last_decl = NULL, *ass = NULL; @@ -5714,7 +5940,6 @@ SgStatement *Create_Host_Loop_Subroutine(SgSymbol *sHostProc, int dependency) addopenmp = 1; /* OpenMP */ // create Host procedure header and end - st_hedr = CreateHostProcedure(sHostProc); st_hedr->addComment(Host_LoopHandlerComment()); st_end = st_hedr->lexNext(); @@ -6866,6 +7091,13 @@ SgSymbol *DummyDvmArraySymbol(SgSymbol *ar, SgSymbol *header_symb) return(new SgSymbol(VARIABLE_NAME, ar->identifier(), *typearray, *header_symb->scope())); } +SgSymbol *DummyDvmBufferSymbol(SgSymbol *ar, SgSymbol *header_symb) +{ + SgArrayType *typearray = new SgArrayType(*ar->type()->baseType()); + typearray->addRange(*Dimension(header_symb, 1, 1)); + return(new SgSymbol(VARIABLE_NAME, ar->identifier(), *typearray, *header_symb->scope())); +} + SgExpression *Dimension(SgSymbol *hs, int i, int rank) { SgValueExp M0(0), M1(1); @@ -12762,7 +12994,7 @@ SgStatement *Create_C_Adapter_Function(SgSymbol *sadapter, int InternalPosition) SgStatement *Create_C_Adapter_Function(SgSymbol *sadapter) { symb_list *sl; - SgStatement *st_hedr, *st_end, *stmt, *do_while, *first_exec, *st_base = NULL, *st_call; + SgStatement *st_hedr, *st_end, *stmt, *do_while, *first_exec, *st_base = NULL, *st_call, *cur; SgExpression *fe, *ae, *arg_list, *el, *e, *er; SgExpression *espec; SgFunctionCallExp *fcall; @@ -12773,11 +13005,11 @@ SgStatement *Create_C_Adapter_Function(SgSymbol *sadapter) SgSymbol *s_num_of_red_blocks = NULL, *s_fill_flag = NULL, *s_red_num = NULL, *s_restBlocks = NULL, *s_addBlocks = NULL, *s_overallBlocks = NULL; SgSymbol *s_max_blocks; SgType *typ = NULL; - int ln, num, i, uses_num, shared_mem_count, has_red_array, use_device_num; + int ln, num, i, uses_num, shared_mem_count, has_red_array, use_device_num, nbuf; char *define_name; int pl_rank = ParLoopRank(); h_first = hgpu_first = base_first = red_first = uses_first = scalar_first = NULL; - has_red_array = 0; use_device_num = 0; + has_red_array = 0; use_device_num = 0; nbuf = 0; s_dev_num = NULL; s_shared_mem = NULL; @@ -12814,6 +13046,8 @@ SgStatement *Create_C_Adapter_Function(SgSymbol *sadapter) arg_list = arg_list->rhs(); if (!ln) h_first = sarg; + if (IS_REMOTE_ACCESS_BUFFER(sl->symb)) // case of RTS2 interface + nbuf++; } for (el = uses_list, ln = 0; el; el = el->rhs(), ln++) // uses { @@ -13101,12 +13335,18 @@ SgStatement *Create_C_Adapter_Function(SgSymbol *sadapter) /* -------- call dvmh_get_natural_base(long *deviceRef, long dvmDesc[] ) ----*/ - for (s = h_first, sb = base_first, ln = 0; ln < num; s = s->next(), sb = sb->next(), ln++) + for (sl = acc_array_list, s = h_first, sb = base_first, ln = 0; ln < num; sl = sl->next, s = s->next(), sb = sb->next(), ln++) { s_dev_num = doDeviceNumVar(st_hedr, first_exec, s_dev_num, s_loop_ref); e = &SgAssignOp(*new SgVarRefExp(sb), *GetNaturalBase(s_dev_num, s)); - stmt = new SgCExpStmt(*e); + stmt = cur = new SgCExpStmt(*e); st_end->insertStmtBefore(*stmt, *st_hedr); + if (IS_REMOTE_ACCESS_BUFFER(sl->symb)) // case of RTS2 interface + { + e = LoopGetRemoteBuf(s_loop_ref, nbuf--, s); + stmt = new SgCExpStmt(*e); + cur->insertStmtBefore(*stmt, *st_hedr); + } if (!ln) { stmt->addComment("// Get 'natural' bases"); diff --git a/dvm/fdvm/trunk/fdvm/acc_across.cpp b/dvm/fdvm/trunk/fdvm/acc_across.cpp index 279fa0e..76af6ff 100644 --- a/dvm/fdvm/trunk/fdvm/acc_across.cpp +++ b/dvm/fdvm/trunk/fdvm/acc_across.cpp @@ -1125,7 +1125,7 @@ vector Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadap SgType *typ; SgFunctionCallExp *funcCall; vector dvm_array_headers; - int ln, num, uses_num, has_red_array, use_device_num, num_of_red_arrays = 0; + int ln, num, uses_num, has_red_array, use_device_num, num_of_red_arrays = 0, nbuf = 0; // init block reduction_ptr = NULL; @@ -1176,6 +1176,8 @@ vector Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadap arg_list = arg_list->rhs(); if (!ln) h_first = sarg; + if (IS_REMOTE_ACCESS_BUFFER(sl->symb)) // case of RTS2 interface + nbuf++; } for (el = uses_list, ln = 0; el; el = el->rhs(), ++ln) // @@ -1394,12 +1396,19 @@ vector Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadap /* -------- call dvmh_get_natural_base(long *deviceRef, long dvmDesc[] ) ----*/ - for (s = h_first, sb = base_first, ln = 0; ln < num; s = s->next(), sb = sb->next(), ln++) + for (sl = acc_array_list, s = h_first, sb = base_first, ln = 0; ln < num; sl = sl->next, s = s->next(), sb = sb->next(), ln++) { s_dev_num = doDeviceNumVar(st_hedr, first_exec, s_dev_num, s_loop_ref); e = &SgAssignOp(*new SgVarRefExp(sb), *GetNaturalBase(s_dev_num, s)); stmt = new SgCExpStmt(*e); + SgStatement *cur = stmt; st_end->insertStmtBefore(*stmt, *st_hedr); + if (IS_REMOTE_ACCESS_BUFFER(sl->symb)) // case of RTS2 interface + { + e = LoopGetRemoteBuf(s_loop_ref, nbuf--, s); + stmt = new SgCExpStmt(*e); + cur->insertStmtBefore(*stmt, *st_hedr); + } if (!ln) stmt->addComment("// Get natural bases"); } @@ -1754,7 +1763,7 @@ vector Create_C_Adapter_Function_Across_variants(SgSymbol *sadapt SgType *typ; SgFunctionCallExp *funcCall, *funcCallKernel; vector dvm_array_headers; - int ln, num, uses_num, has_red_array, use_device_num, num_of_red_arrays; + int ln, num, uses_num, has_red_array, use_device_num, num_of_red_arrays, nbuf = 0; // init block lowI = highI = idxI = elem = red_blocks = shared_mem = stream_t = bIdxs = NULL; @@ -1812,6 +1821,8 @@ vector Create_C_Adapter_Function_Across_variants(SgSymbol *sadapt arg_list = arg_list->rhs(); if (!ln) h_first = sarg; + if (IS_REMOTE_ACCESS_BUFFER(sl->symb)) // case of RTS2 interface + nbuf++; } for (el = uses_list, ln = 0; el; el = el->rhs(), ++ln) // @@ -2297,12 +2308,19 @@ vector Create_C_Adapter_Function_Across_variants(SgSymbol *sadapt /* -------- call dvmh_get_natural_base(long *deviceRef, long dvmDesc[] ) ----*/ - for (s = h_first, sb = base_first, ln = 0; ln < num; s = s->next(), sb = sb->next(), ln++) + for (sl = acc_array_list, s = h_first, sb = base_first, ln = 0; ln < num; sl = sl->next, s = s->next(), sb = sb->next(), ln++) { s_dev_num = doDeviceNumVar(st_hedr, first_exec, s_dev_num, s_loop_ref); e = &SgAssignOp(*new SgVarRefExp(sb), *GetNaturalBase(s_dev_num, s)); stmt = new SgCExpStmt(*e); + SgStatement *cur = stmt; st_end->insertStmtBefore(*stmt, *st_hedr); + if (IS_REMOTE_ACCESS_BUFFER(sl->symb)) // case of RTS2 interface + { + e = LoopGetRemoteBuf(s_loop_ref, nbuf--, s); + stmt = new SgCExpStmt(*e); + cur->insertStmtBefore(*stmt, *st_hedr); + } if (!ln) stmt->addComment("// Get natural bases"); } diff --git a/dvm/fdvm/trunk/fdvm/dvm.cpp b/dvm/fdvm/trunk/fdvm/dvm.cpp index debdf1d..5087bd0 100644 --- a/dvm/fdvm/trunk/fdvm/dvm.cpp +++ b/dvm/fdvm/trunk/fdvm/dvm.cpp @@ -9683,6 +9683,21 @@ void RemoteAccessDirective(SgStatement *stmt) RemoteVariableList(stmt->symbol(),stmt->expr(0),stmt); } +SgExpression *AlignmentListForRemoteDir(int nt, SgExpression *axis[], SgExpression *coef[], SgExpression *cons[]) +{ // case of RTS2 interface + SgExpression *arglist=NULL, *el, *e; + + for(int i=0; isetRhs(arglist); + arglist = el; + } + (el = new SgExprListExp(*ConstRef(nt)))->setRhs(arglist); // add rank to axis list + arglist = el; + return arglist; +} + void RemoteVariableList1(SgSymbol *group,SgExpression *rml, SgStatement *stmt) { SgStatement *if_st,*end_st = NULL; SgExpression *el, *es; @@ -9751,7 +9766,10 @@ void RemoteVariableList(SgSymbol *group, SgExpression *rml, SgStatement *stmt) return; if(IN_COMPUTE_REGION && group) err("Asynchronous REMOTE_ACCESS clause in compute region",574,stmt); - + if(group && parloop_by_handler == 2 && stmt->variant() != DVM_PARALLEL_ON_DIR ) { // case of REMOTE_ACCESS directive + err("Illegal directive in -Opl2 mode. Asynchronous operations are not supported in this mode", 649, stmt); + group = NULL; + } if(group){ if_st = doIfThenConstrForRemAcc(group,cur_st); end_st = cur_st; //END IF @@ -9824,7 +9842,7 @@ void RemoteVariableList(SgSymbol *group, SgExpression *rml, SgStatement *stmt) } else { axis[n] = &c0.copy(); coef[n] = &c0.copy(); - cons[n] = &(es->lhs()->copy() - *Exprn( LowerBound(el->lhs()->symbol(),n))) ; + cons[n] = parloop_by_handler == 2 ? &es->lhs()->copy() : &(es->lhs()->copy() - *Exprn( LowerBound(el->lhs()->symbol(),n))) ; ind_deb[n] = &(cons[n]->copy()); //init[n] = &c0.copy(); //last[n] = &c0.copy(); @@ -9869,6 +9887,22 @@ void RemoteVariableList(SgSymbol *group, SgExpression *rml, SgStatement *stmt) //buffer_head = DVM000(ibuf); ar = NULL; } + // adding attribute REMOTE_VARIABLE + rem_var *remv = new rem_var; + remv->ncolon = nc; + remv->index = ibuf; + remv->amv = group ? 1 : iamv; + remv->buffer = NULL; /*ACC*/ + + (el->lhs())->addAttribute(REMOTE_VARIABLE,(void *) remv, sizeof(rem_var)); + + // case of RTS2-interface + if(parloop_by_handler==2) { + if(stmt->variant() != DVM_PARALLEL_ON_DIR) { + doCallAfter(RemoteAccess_H2(header_rf(ar,ibuf,1), el->lhs()->symbol(), HeaderRef(el->lhs()->symbol()), AlignmentListForRemoteDir(n,axis,coef,cons))); + } + continue; + } // creating buffer for remote elements of array iaxis = ndvm; if (stmt->variant() == DVM_PARALLEL_ON_DIR) { @@ -9920,17 +9954,9 @@ void RemoteVariableList(SgSymbol *group, SgExpression *rml, SgStatement *stmt) } InsertNewStatementAfter(D_RmBuf( HeaderRef(el->lhs()->symbol()),GetAddresDVM( header_rf(ar,ibuf,1)),n,ideb),cur_st,cur_st->controlParent()); } - SET_DVM(iaxis); - //adding attribute REMOTE_VARIABLE - rem_var *remv = new rem_var; - remv->ncolon = nc; - remv->index = ibuf; - remv->amv = group ? 1 : iamv; - remv->buffer = NULL; /*ACC*/ - - (el->lhs())->addAttribute(REMOTE_VARIABLE,(void *) remv, sizeof(rem_var)); - + SET_DVM(iaxis); } + if(group) { cur_st = cur_st->lexNext()->lexNext();//IF THEN after ELSE doAssignStmtAfter(WaitBG(GROUP_REF(group,1))); diff --git a/dvm/fdvm/trunk/fdvm/funcall.cpp b/dvm/fdvm/trunk/fdvm/funcall.cpp index 1c387d7..09b3d3a 100644 --- a/dvm/fdvm/trunk/fdvm/funcall.cpp +++ b/dvm/fdvm/trunk/fdvm/funcall.cpp @@ -3702,13 +3702,16 @@ SgStatement *Consistent_H (int il, SgExpression *hedr, SgExpression *axis_list) return(call); } -SgStatement *LoopRemoteAccess_H (int il, SgExpression *hedr, SgExpression *axis_list) -{// generating subroutine call: dvmh_loop_remote_access_(const DvmType *pCurLoop, const DvmType dvmDesc[], const DvmType *pRank, /* const DvmType *pAlignmentHelper */...) +SgStatement *LoopRemoteAccess_H (int il, SgExpression *hedr, SgSymbol *ar, SgExpression *axis_list) +{// generating subroutine call: dvmh_loop_remote_access_(const DvmType *pCurLoop, const DvmType dvmDesc[], const void *baseAddr, const DvmType *pRank, /* const DvmType *pAlignmentHelper */...) // DvmhLoopRef - result of dvmh_loop_create() SgCallStmt *call = new SgCallStmt(*fdvm[LOOP_REMOTE]); fmask[LOOP_REMOTE] = 2; call->addArg(*DVM000(il)); call->addArg(*hedr); + SgType *t = (isSgArrayType(ar->type())) ? ar->type()->baseType() : ar->type(); + SgExpression *base = (t->variant() != T_DERIVED_TYPE && t->variant() != T_STRING ) ? new SgArrayRefExp(*baseMemory(SgTypeInt())) : new SgArrayRefExp(*baseMemory(t)); + call->addArg(*base); AddListToList(call->expr(0), axis_list); return(call); } @@ -4411,6 +4414,16 @@ SgStatement *FillLocalPart_HH(SgSymbol *loop_s, SgSymbol *shead, SgSymbol *spart return(call); } +SgStatement *GetRemoteBuf (SgSymbol *loop_s, int n, SgSymbol *s_buf_head) +{// generating subroutine call: dvmh_loop_get_remote_buf_(const DvmType *pCurLoop, const DvmType *pRmaIndex, DvmType rmaDesc[]); + + SgCallStmt *call = new SgCallStmt(*fdvm[GET_REMOTE_BUF]); + fmask[GET_REMOTE_BUF] = 2; + call->addArg(*new SgVarRefExp(loop_s)); + call->addArg(*ConstRef_F95(n)); + call->addArg(*new SgArrayRefExp(*s_buf_head)); + return(call); +} //------ Calls from handlers for sequence of statements -------------------- @@ -4707,13 +4720,14 @@ SgExpression *GetDeviceNum(SgSymbol *s_loop_ref) { // generating function call: // DvmType loop_get_device_num_ (DvmhLoopRef *InDvmhLoop) // or when RTS2 is used - // DvmType dvmh_loop_get_device_num_(const DvmType *pCurLoop) + // DvmType dvmh_loop_get_device_num_C ( DvmType curLoop) - int fNum = INTERFACE_RTS2 ? GET_DEVICE_NUM_2 : GET_DEVICE_NUM ; + int fNum = INTERFACE_RTS2 ? GET_DEVICE_NUM_2 : GET_DEVICE_NUM ; SgFunctionCallExp *fe = new SgFunctionCallExp(*fdvm[fNum]); - - fe->addArg(* new SgVarRefExp(s_loop_ref)); - + if(INTERFACE_RTS2) + fe->addArg(SgDerefOp(*new SgVarRefExp(s_loop_ref))); + else + fe->addArg(* new SgVarRefExp(s_loop_ref)); return(fe); } @@ -4751,6 +4765,15 @@ SgExpression *FillBounds(SgSymbol *loop_s, SgSymbol *sBlow,SgSymbol *sBhigh,SgSy return(fe); } +SgExpression *LoopGetRemoteBuf(SgSymbol *loop_s, int n, SgSymbol *s_buf_head) +{// generating function call: dvmh_loop_get_remote_buf_(const DvmType *pCurLoop, const DvmType *pRmaIndex, DvmType rmaDesc[]); + SgFunctionCallExp *fe = new SgFunctionCallExp(*fdvm[GET_REMOTE_BUF_C]); + fe->addArg(SgDerefOp(*new SgVarRefExp(loop_s))); + fe->addArg(*new SgValueExp(n)); + fe->addArg(*new SgArrayRefExp(*s_buf_head)); + return(fe); +} + SgExpression *RedPost(SgSymbol *loop_s, SgSymbol *s_var_num, SgSymbol *sRed,SgSymbol *sLoc) {// generating function call: // void loop_red_post_(DvmhLoopRef *InDvmhLoop, DvmType *InRedNum, void *arrayPtr, void *locPtr) @@ -4761,7 +4784,7 @@ SgExpression *RedPost(SgSymbol *loop_s, SgSymbol *s_var_num, SgSymbol *sRed,SgSy int fNum = INTERFACE_RTS2 ? RED_POST_2 : RED_POST_C ; SgFunctionCallExp *fe = new SgFunctionCallExp(*fdvm[fNum]); - fe -> addArg(* new SgVarRefExp(loop_s)); + fe->addArg(* new SgVarRefExp(loop_s)); fe->addArg(SgAddrOp(* new SgVarRefExp(s_var_num))); fe->addArg(SgAddrOp(* new SgVarRefExp(sRed))); if(sLoc) @@ -4893,12 +4916,14 @@ SgExpression *GuessIndexType(SgSymbol *s_loop_ref) {// generating function call: // loop_guess_index_type_(DvmhLoopRef *InDvmhLoop) // or when RTS2 is used - // dvmh_loop_guess_index_type_(const DvmType *pCurLoop) + // dvmh_loop_guess_index_type_C(DvmType *curLoop) int fNum = INTERFACE_RTS2 ? GUESS_INDEX_TYPE_2 : GUESS_INDEX_TYPE ; - SgFunctionCallExp *fe = new SgFunctionCallExp(*fdvm[fNum]); - - fe -> addArg(* new SgVarRefExp(s_loop_ref)); + SgFunctionCallExp *fe = new SgFunctionCallExp(*fdvm[fNum]); + if(INTERFACE_RTS2) + fe->addArg(SgDerefOp(*new SgVarRefExp(s_loop_ref))); + else + fe->addArg(*new SgVarRefExp(s_loop_ref)); return(fe); } diff --git a/dvm/fdvm/trunk/fdvm/parloop.cpp b/dvm/fdvm/trunk/fdvm/parloop.cpp index 6dc5acc..fd70b5f 100644 --- a/dvm/fdvm/trunk/fdvm/parloop.cpp +++ b/dvm/fdvm/trunk/fdvm/parloop.cpp @@ -460,7 +460,11 @@ int WhatInterface(SgStatement *stmt) case ACC_TIE_OP: case CONSISTENT_OP: case STAGE_OP: - break; + case REMOTE_ACCESS_OP: + if(e->symbol()) // asynchronous REMOTE_ACCESS + return(1); + else + break; case REDUCTION_OP: if(TestReductionClause(e)) break; @@ -2213,6 +2217,21 @@ SgExpression *AxisList(SgStatement *stmt, SgExpression *tied_array_ref) return arglist; } +SgExpression *ArrayRefAddition(SgExpression *aref) +{ + if(!aref->lhs()) // without subscript list + { + // A => A(:,:,...,:) + SgExpression *arlist = NULL; + int n = Rank(aref->symbol()); + while(n--) + arlist = AddListToList(arlist, new SgExprListExp(*new SgExpression(DDOT))); + + aref->setLhs(arlist); + } + return aref; +} + SgExpression *MappingList(SgStatement *stmt, SgExpression *aref) { SgExpression *axis[MAX_LOOP_LEVEL], @@ -2280,6 +2299,16 @@ void Interface_2(SgStatement *stmt,SgExpression *clause[],SgExpression *init[],S for (SgExpression *el = clause[CONSISTENT_]->lhs(); el; el=el->rhs()) InsertNewStatementAfter(Consistent_H(ilh, HeaderForArrayInParallelDir(el->lhs()->symbol(), stmt, 0), MappingList(stmt, el->lhs())), cur_st, cur_st->controlParent()); + if (clause[REMOTE_ACCESS_]) //there is REMOTE_ACCESS clause + { int nbuf=1; + //adding new element to remote_access directive/clause list + AddRemoteAccess(clause[REMOTE_ACCESS_]->lhs(),NULL); + RemoteVariableList(clause[REMOTE_ACCESS_]->symbol(), clause[REMOTE_ACCESS_]->lhs(), stmt); + + for (SgExpression *el=clause[REMOTE_ACCESS_]->lhs(); el; el=el->rhs(),nbuf++) + InsertNewStatementAfter(LoopRemoteAccess_H(ilh, HeaderForArrayInParallelDir(el->lhs()->symbol(), stmt, 0), el->lhs()->symbol(), MappingList(stmt, ArrayRefAddition(el->lhs()))), cur_st, cur_st->controlParent()); + } + if (clause[SHADOW_COMPUTE_]) //there is SHADOW_COMPUTE clause { if ( (clause[SHADOW_COMPUTE_]->lhs())) diff --git a/dvm/fdvm/trunk/include/dvm.h b/dvm/fdvm/trunk/include/dvm.h index bd4baf8..785de47 100644 --- a/dvm/fdvm/trunk/include/dvm.h +++ b/dvm/fdvm/trunk/include/dvm.h @@ -260,6 +260,7 @@ const int DEFERRED_SHAPE = 1049; const int END_OF_USE_LIST = 1050; /*ACC*/ const int ROUTINE_ATTR = 1051; /*ACC*/ const int DATA_REGION_SYMB = 1052; /*ACC*/ +const int REMOTE_ACCESS_BUF = 1053; /*ACC*/ const int MAX_LOOP_LEVEL = 20; // 7 - maximal number of loops in parallel loop nest const int MAX_LOOP_NEST = 25; // maximal number of nested loops @@ -421,6 +422,7 @@ const int Logical_8 = 12; #define USE_STATEMENTS_ARE_REQUIRED ( (int *) first_do_par->attributeValue(0,MODULE_USE) ) #define DEFERRED_SHAPE_TEMPLATE(A) ( (ORIGINAL_SYMBOL(A))->attributeValue(0,DEFERRED_SHAPE) ) #define HAS_ROUTINE_ATTR(A) ((A)->attributeValue(0,ROUTINE_ATTR)) +#define IS_REMOTE_ACCESS_BUFFER(A) ((A)->attributeValue(0,REMOTE_ACCESS_BUF)) EXTERN SgFunctionSymb * fdvm [MAX_LIBFUN_NUM]; @@ -674,6 +676,7 @@ int CreateBufferArray(int rank, SgExpression *rme, int *amview, SgStatement *stm void CopyToBuffer(int rank, int ibuf, SgExpression *rme); void RemoteVariableList(SgSymbol *group,SgExpression *rml, SgStatement *stmt); void RemoteVariableList1(SgSymbol *group,SgExpression *rml, SgStatement *stmt); +SgExpression *AlignmentListForRemoteDir(int nt, SgExpression *axis[], SgExpression *coef[], SgExpression *cons[]); void DeleteBuffers(SgExpression *rml); void AddRemoteAccess(SgExpression *rml, SgStatement *rmout); void DelRemoteAccess(); @@ -1123,6 +1126,7 @@ void CudaBlockSize(SgExpression *cuda_block_list); void CudaBlockSize(SgExpression *cuda_block_list,SgExpression *esize[]); int ListElemNumber(SgExpression *list); SgStatement *Create_Host_Across_Loop_Subroutine(SgSymbol *sHostProc); +SgStatement *Create_Host_Loop_Subroutine_Main(SgSymbol *sHostProc); SgStatement *Create_Host_Loop_Subroutine(SgSymbol *sHostProc, int type); char * BoundName(SgSymbol *s, int i, int isLower); SgSymbol *DummyBoundSymbol(SgSymbol *rv, int i, int isLower, SgStatement *st_hedr); @@ -1207,7 +1211,8 @@ void RefIn_LoopHeaderExpr(SgExpression *e,SgStatement *dost); SgSymbol *RemoteAccessBufferInKernel(SgSymbol *ar,int rank); SgSymbol *isSameNameBuffer(char *name,SgExpression *rml); SgExpression *RemoteAccessHeaderList(); -void CreateRemoteAccessBuffers(); +void CreateRemoteAccessBuffersUp(); +void CreateRemoteAccessBuffers(SgExpression *rml, int pl_flag); coeffs *BufferCoeffs(SgSymbol *sbuf,SgSymbol *ar); void AddRemoteAccessBufferList_ToArrayList(); SgExpression * ExpressionListsUnion(SgExpression *list, SgExpression *alist); @@ -1378,6 +1383,7 @@ char *Header_DummyArgName(SgSymbol *s); SgExpression *Dimension(SgSymbol *hs, int i, int rank); SgSymbol *DummyDvmHeaderSymbol(SgSymbol *ar, SgStatement *st_hedr); SgSymbol *DummyDvmArraySymbol(SgSymbol *ar,SgSymbol *header_symb); +SgSymbol *DummyDvmBufferSymbol(SgSymbol *ar, SgSymbol *header_symb); SgExpression *ElementOfAddrArgumentList(SgSymbol *s); SgExpression *AddrArgumentList(); void CompareReductionAndPrivateList(); @@ -1821,6 +1827,7 @@ SgStatement *Realign_H(SgExpression *objref, int new_sign); SgExpression *GetOverallStep(SgSymbol *s_loop_ref); SgExpression *GetDeviceNum(SgSymbol *s_loop_ref); SgExpression *FillBounds(SgSymbol *loop_s, SgSymbol *sBlow,SgSymbol *sBhigh,SgSymbol *sBstep); +SgExpression *LoopGetRemoteBuf(SgSymbol *loop_s, int n, SgSymbol *s_buf_head); SgExpression *mallocFunction(SgExpression *arg, SgStatement *scope); SgExpression *freeFunction(SgExpression *arg, SgStatement *scope); SgExpression *RedPost(SgSymbol *loop_s, SgSymbol *s_var_num, SgSymbol *sRed,SgSymbol *sLoc); @@ -1888,8 +1895,10 @@ SgStatement *DvmhArrayCopyWhole( SgExpression *array_header_right, SgExpression SgStatement *Correspondence_H (int il, SgExpression *hedr, SgExpression *axis_list); SgStatement *DvmhArraySetValue( SgExpression *array_header_left, SgExpression *e_right ); SgStatement *Consistent_H (int il, SgExpression *hedr, SgExpression *axis_list); -SgStatement *LoopRemoteAccess_H (int il, SgExpression *hedr, SgExpression *axis_list); +SgStatement *LoopRemoteAccess_H (int il, SgExpression *hedr, SgSymbol *ar, SgExpression *axis_list); SgStatement *RemoteAccess_H2 (SgExpression *buf_hedr, SgSymbol *ar, SgExpression *ar_hedr, SgExpression *axis_list); +SgStatement *GetRemoteBuf (SgSymbol *loop_s, int n, SgSymbol *s_buf_head); + /* io.cpp */ void IO_ThroughBuffer(SgSymbol *ar, SgStatement *stmt, SgExpression *eiostat); int IOcontrol(SgExpression *e, SgExpression *ioc[],int type); diff --git a/dvm/fdvm/trunk/include/libdvm.h b/dvm/fdvm/trunk/include/libdvm.h index 6d7e3a7..cf521f5 100644 --- a/dvm/fdvm/trunk/include/libdvm.h +++ b/dvm/fdvm/trunk/include/libdvm.h @@ -264,6 +264,7 @@ name_dvm[SET_VALUE] = "dvmh_array_set_value"; name_dvm[LOOP_CONSISTENT] = "dvmh_loop_consistent"; name_dvm[DVMH_REMOTE2] = "dvmh_remote_access2"; name_dvm[LOOP_REMOTE] = "dvmh_loop_remote_access"; +name_dvm[GET_REMOTE_BUF] = "dvmh_loop_get_remote_buf"; name_dvm[FTN_OPEN] = "dvmh_ftn_open"; /* IO */ name_dvm[FTN_CLOSE] = "dvmh_ftn_close"; /* IO */ name_dvm[FTN_READ] = "dvmh_ftn_read_unf"; /* IO */ @@ -312,7 +313,7 @@ name_dvm[GET_DEVICE_ADDR_C]= "dvmh_get_device_addr_C"; name_dvm[GET_LOCAL_PART] = "loop_cuda_get_local_part"; name_dvm[GET_LOCAL_PART_C] = "dvmh_loop_cuda_get_local_part_C"; name_dvm[GET_DEVICE_NUM] = "loop_get_device_num_"; -name_dvm[GET_DEVICE_NUM_2] = "dvmh_loop_get_device_num_"; +name_dvm[GET_DEVICE_NUM_2] = "dvmh_loop_get_device_num_C"; name_dvm[GET_OVERALL_STEP] = "loop_cuda_get_red_step"; name_dvm[FILL_BOUNDS_C] = "loop_fill_bounds_"; name_dvm[FILL_BOUNDS_2] = "dvmh_loop_fill_bounds_"; @@ -330,5 +331,6 @@ name_dvm[GET_CONFIG] = "loop_cuda_get_config"; name_dvm[GET_CONFIG_C] = "dvmh_loop_cuda_get_config_C"; name_dvm[CHANGE_BOUNDS] = "dvmh_change_filled_bounds"; name_dvm[GUESS_INDEX_TYPE] = "loop_guess_index_type_"; -name_dvm[GUESS_INDEX_TYPE_2]= "dvmh_loop_guess_index_type_"; +name_dvm[GUESS_INDEX_TYPE_2]="dvmh_loop_guess_index_type_C"; name_dvm[RTC_SET_LANG] = "loop_cuda_rtc_set_lang"; +name_dvm[GET_REMOTE_BUF_C] = "dvmh_loop_get_remote_buf_C"; diff --git a/dvm/fdvm/trunk/include/libnum.h b/dvm/fdvm/trunk/include/libnum.h index 01a1f06..179ff55 100644 --- a/dvm/fdvm/trunk/include/libnum.h +++ b/dvm/fdvm/trunk/include/libnum.h @@ -265,6 +265,7 @@ enum { LOOP_CONSISTENT, DVMH_REMOTE2, LOOP_REMOTE, + GET_REMOTE_BUF, FTN_OPEN, FTN_CLOSE, FTN_READ, @@ -330,5 +331,6 @@ enum { GUESS_INDEX_TYPE, GUESS_INDEX_TYPE_2, RTC_SET_LANG, + GET_REMOTE_BUF_C, MAX_LIBFUN_NUM }; diff --git a/sapfor/experts/Sapfor_2017/CMakeLists.txt b/sapfor/experts/Sapfor_2017/CMakeLists.txt index be43305..d30abc3 100644 --- a/sapfor/experts/Sapfor_2017/CMakeLists.txt +++ b/sapfor/experts/Sapfor_2017/CMakeLists.txt @@ -331,6 +331,14 @@ set(MAIN _src/Sapfor.cpp set(PREDICTOR _src/Predictor/PredictScheme.cpp _src/Predictor/PredictScheme.h) +set(PROJ_MAN _src/ProjectManipulation/ParseFiles.cpp + _src/ProjectManipulation/ParseFiles.h + _src/ProjectManipulation/StdCapture.h + _src/ProjectManipulation/PerfAnalyzer.cpp + _src/ProjectManipulation/PerfAnalyzer.h + _src/ProjectManipulation/FileInfo.cpp + _src/ProjectManipulation/FileInfo.h) + set(PARSER ${parser_sources}/cftn.c ${parser_sources}/errors.c ${parser_sources}/gram1.tab.c @@ -413,7 +421,8 @@ set(SOURCE_EXE ${PPPA} ${ZLIB} ${GR_LAYOUT} - ${PR_PARAM}) + ${PR_PARAM} + ${PROJ_MAN}) add_executable(Sapfor_F ${SOURCE_EXE}) source_group (CFGraph FILES ${CFG}) @@ -454,6 +463,7 @@ source_group (SageExtension FILES ${OMEGA}) source_group (Utils FILES ${UTILS}) source_group (VerificationCode FILES ${VERIF}) source_group (ProjectParameters FILES ${PR_PARAM}) +source_group (ProjectManipulation FILES ${PROJ_MAN}) source_group (VisualizerCalls FILES ${VS_CALLS}) source_group (VisualizerCalls\\GraphLayout FILES ${GR_LAYOUT}) diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.cpp new file mode 100644 index 0000000..5e10ef0 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.cpp @@ -0,0 +1,62 @@ +#include "../Utils/leak_detector.h" + +#include +#include + +#include +#include + +#include "FileInfo.h" +#include "../Utils/utils.h" + +using namespace std; + +string FileInfo::convertStyle(bool needRewrite) +{ + string tmp_text = text; + + vector splited; + splitString(tmp_text, '\n', splited); + + tmp_text = ""; + int z = 0; + for (auto& line : splited) + { + if (line[0] == 'c' || line[0] == 'C' || line[0] == 'd' || line[0] == 'D' || line[0] == '*') + line[0] = '!'; + + bool needContinuation = false; + if (line[0] != '!' && line.size() > 6) + { + if (line[5] != ' ' && !(line[5] > '0' && line[5] < '9')) // not label + { + line[5] = ' '; + needContinuation = true;// line[5] = '&'; + } + + int p = 73; + if (style == 1) + p = 133; + if (line.size() > p) + { + while (line[p] != '\0' && line[p] != '\n' && line[p] != '!') + { + line[p] = ' '; + p++; + if (p >= line.size()) + break; + } + } + } + + if (needContinuation) + tmp_text += "&"; + tmp_text += (z != 0 ? "\n" : "") + line; + ++z; + } + + if (needRewrite) + writeFileFromStr(fileName, tmp_text); + + return tmp_text; +} \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.h b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.h new file mode 100644 index 0000000..4bcb728 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.h @@ -0,0 +1,50 @@ +#pragma once + +#include +#include + +struct FileInfo +{ + FileInfo() + { + fileName = ""; + options = ""; + errPath = ""; + outPath = ""; + outDepPath = ""; + text = ""; + error = -1; + includesAdded = 0; + style = -1; + lvl = 0; + } + + FileInfo(const std::string& _fileName, const std::string& _options, const std::string& _errPath, const std::string& _outPath, + const std::string& _outDepPath, const std::string& _text, int errorInit = -1) + { + fileName = _fileName; + options = _options; + errPath = _errPath; + outPath = _outPath; + outDepPath = _outDepPath; + text = _text; + error = errorInit; + includesAdded = 0; + style = -1; + lvl = 0; + } + + int error; + std::string fileName; + std::string options; + std::string errPath; + std::string outPath; + std::string outDepPath; + std::string text; + int style; // -1 unk, 0 fixed, 1 fixed ext, 2 free + int includesAdded; + std::set includes; + int lvl; + + std::string convertStyle(bool needRewrite = true); +}; diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp new file mode 100644 index 0000000..27c92ac --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp @@ -0,0 +1,789 @@ +#include "../Utils/leak_detector.h" + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../Utils/errors.h" +#include "../Utils/SgUtils.h" +#include "../VisualizerCalls/get_information.h" +#include "../VisualizerCalls/SendMessage.h" + +#include "ParseFiles.h" +#include "StdCapture.h" +#include "FileInfo.h" + +using namespace std; + +extern "C" int parse_file(int argc, char* argv[], char* proj_name); + +static void findModuleDeclInProject(const string& name, const vector& files, map& modDecls) +{ + char** filesList = new char* [files.size()]; + for (int z = 0; z < files.size(); ++z) + filesList[z] = (char*)files[z].c_str(); + + SgProject* tmpProj = new SgProject(name.c_str(), filesList, files.size()); + + int numF = tmpProj->numberOfFiles(); + set filesSg; + for (int z = 0; z < numF; ++z) + { + vector modules; + SgFile* currF = &tmpProj->file(z); + string fileName = currF->filename(); + convertToLower(fileName); + + filesSg.insert(currF); + + findModulesInFile(currF, modules); + for (auto& elem : modules) + { + if (string(elem->fileName()) == currF->filename()) + { + const string name = elem->symbol()->identifier(); + auto it = modDecls.find(name); + if (it != modDecls.end() && it->second != currF->filename()) + { + __spf_print(1, "found several module declaration of '%s' in files '%s' and '%s'\n", name.c_str(), it->second.c_str(), currF->filename()); + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + } + else + modDecls.insert(it, make_pair(name, currF->filename())); + } + } + } + + delete []filesList; + InitializeTable(); +} + +static void createIncludeOrder(vector &toIncl, + const map& moduleDelc, + const map>& modDirectOrder, + set &done, + const string &curr) +{ + if (done.find(curr) == done.end()) + { + for (auto& elem : modDirectOrder.find(curr)->second) + createIncludeOrder(toIncl, moduleDelc, modDirectOrder, done, elem); + + if (done.find(curr) == done.end()) + { + toIncl.push_back(moduleDelc.find(curr)->second); + done.insert(curr); + } + } +} + +static set applyModuleDeclsForFile(FileInfo *forFile, const map &mapFiles, + const map& moduleDelc, + const map>& mapModuleDeps, + const map>& modDirectOrder, + vector &optSplited, + bool includeForInline = false) +{ + set retFilesMod; + + auto itF = mapModuleDeps.find(forFile->fileName); + if (itF == mapModuleDeps.end() && !includeForInline) + return retFilesMod; + + vector toIncl; + set done; + if (itF != mapModuleDeps.end()) + { + for (auto& mod : itF->second) + if (moduleDelc.find(mod) != moduleDelc.end()) + createIncludeOrder(toIncl, moduleDelc, modDirectOrder, done, mod); + } + + //rewrite files to the next iter of parse + set allFiles; + bool needToConvertStyle = false; + for (auto& incl : toIncl) + { + if (mapFiles.find(incl) == mapFiles.end()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + allFiles.insert(mapFiles.find(incl)->second); + } + allFiles.insert(forFile); + + int style = forFile->style; + for (auto& elem : allFiles) + { + if (style != elem->style) + { + needToConvertStyle = true; + break; + } + } + + string mainText = forFile->text; + if (needToConvertStyle) + { + for (auto& elem : allFiles) + { + if (elem->style != 2) + { + retFilesMod.insert(elem); + if (elem != forFile) + elem->convertStyle(); + else + mainText = elem->convertStyle(false); + } + } + + if (forFile->style != 2) + { + for (auto& opt : optSplited) + { + if (opt == "-FI" || opt == "-extend_source") + opt = "-FR"; + } + } + } + + string include = ""; + int includeCount = 0; + set included; + for (auto& incl : toIncl) + { + if (included.find(incl) == included.end()) + { + include += " include '" + incl + "'\n"; + includeCount++; + } + included.insert(incl); + } + + vector toInclEnds; + string includeLast = ""; + + if (includeForInline) + { + //find needed modules first + vector filesWithModules; + for (auto& elem : moduleDelc) + filesWithModules.push_back(elem.second); + for (auto& file : filesWithModules) + { + if (file != forFile->fileName && included.find(file) == included.end()) + { + toInclEnds.push_back(file); + included.insert(file); + } + } + + for (auto& file : mapFiles) + if (file.second != forFile && included.find(file.second->fileName) == included.end()) + toInclEnds.push_back(file.second->fileName); + + if (toInclEnds.size()) + includeLast += "!SPF SHADOW FILES\n"; + + for (auto& incl : toInclEnds) + includeLast += " include '" + incl + "'\n"; + } + + if (includeCount) + include = "!SPF NUM FILES " + std::to_string(includeCount) + "\n" + include; + + const string data = include + mainText + includeLast; + __spf_print(1, "include to file %s before\n", forFile->fileName.c_str()); + __spf_print(1, "%s", include.c_str()); + __spf_print(1, "include to file %s after\n", forFile->fileName.c_str()); + __spf_print(1, "%s", includeLast.c_str()); + + writeFileFromStr(forFile->fileName, data); + + forFile->includesAdded = included.size(); + forFile->includes = included; + + retFilesMod.insert(forFile); + return retFilesMod; +} + +static void restoreOriginalText(const vector& listOfProject) +{ + for (auto& elem : listOfProject) + writeFileFromStr(elem.fileName, elem.text); + fflush(NULL); +} + +static inline void restoreOriginalText(const FileInfo& file) +{ + writeFileFromStr(file.fileName, file.text); +} + +static void checkRetCode(FileInfo& info, const string& errorMessage) +{ + if (info.error != 0) + info.lvl++; + + if (errorMessage.find("Warning 308") != string::npos) + if (info.error == 0) + info.error = 1; +} + +static vector parseList(vector& listOfProject, + bool needToInclude, bool needToIncludeForInline, + const map> &mapModuleDeps, + const map &moduleDelc, + const map> &modDirectOrder, bool isFromConsole = false) +{ + map mapFiles; + for (auto& elem : listOfProject) + mapFiles[elem.fileName] = &elem; + + vector errors; + int i = 1; + int N = listOfProject.size(); + for (auto& elem : listOfProject) + { + sendMessage_progress(std::to_wstring((int)(((double)(i++) / N) * 100))); + + string file = elem.fileName; + string options = elem.options; + vector optSplited = splitAndArgvCreate(options); + + char** toParse = new char* [optSplited.size() + 1]; + for (int z = 0; z < optSplited.size(); ++z) + { + toParse[z] = new char[optSplited[z].size() + 1]; + strcpy(toParse[z], optSplited[z].c_str()); + } + toParse[optSplited.size()] = new char[file.size() + 1]; + strcpy(toParse[optSplited.size()], file.c_str()); + + if (options.find("-FI") != string::npos) + elem.style = 0; + else if (options.find("-FR") != string::npos || options.find("-f90") != string::npos) + elem.style = 2; + else if (options.find("-extend_source") != string::npos) + elem.style = 1; + + for (int z = 0; z < optSplited.size(); ++z) + { + if (optSplited[z] == "-o") + { + if (z + 1 == optSplited.size()) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + elem.outDepPath = optSplited[z + 1]; + break; + } + } + + FILE* depPath = fopen(elem.outDepPath.c_str(), "r"); + if (depPath && !isFromConsole) + { + fclose(depPath); + if (elem.error <= 0) + { + elem.error = 0; + errors.push_back(""); + for (int z = 0; z <= optSplited.size(); ++z) + delete toParse[z]; + delete[] toParse; + continue; + } + } + +#ifdef _WIN32 + sendMessage_2lvl(L" обработка файла '" + to_wstring(file) + L"'"); +#else + sendMessage_2lvl(L" processing file '" + to_wstring(file) + L"'"); +#endif + StdCapture stdCapture; + stdCapture.Init(); + string errorMessage = ""; + try + { + set filesModified; + stdCapture.BeginCapture(); + if (needToInclude) + filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline); + else if (needToIncludeForInline) // TODO for modules + filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline); + + int retCode = parse_file(optSplited.size(), toParse, "dvm.proj"); + if (needToInclude || needToIncludeForInline) + { + for (auto &elem : filesModified) + restoreOriginalText(*elem); + fflush(NULL); + } + + elem.error = retCode; + stdCapture.EndCapture(); + errorMessage = stdCapture.GetCapture(); + checkRetCode(elem, errorMessage); + } + catch (int err) + { + stdCapture.EndCapture(); + errorMessage = stdCapture.GetCapture(); + + if (needToInclude || needToIncludeForInline) + restoreOriginalText(listOfProject); + } + catch (...) + { + stdCapture.EndCapture(); + errorMessage = stdCapture.GetCapture(); + + if (needToInclude || needToIncludeForInline) + restoreOriginalText(listOfProject); + } + errors.push_back(errorMessage); + for (int z = 0; z <= optSplited.size(); ++z) + delete toParse[z]; + delete[] toParse; + + createNeededException(); + } + return errors; +} + +static string shiftLines(const string &in, const map &mapOfFiles, const FileInfo* currF) +{ + int byNum = 0; + + auto it = in.find("on line "); + if (it != string::npos) + it += strlen("on line "); + + int d = 0; + sscanf(in.c_str() + it, "%d", &d); + + auto it1 = in.find("of", it + 1); + if (it1 == string::npos) + return in; + it1 += 3; + + string fileN = in.substr(it1, in.find(':', it1) - it1); + auto itF = mapOfFiles.find(fileN); + if (itF == mapOfFiles.end()) + return in; + if (itF->second != currF) + return in; + + byNum = itF->second->includesAdded; + if (byNum == 0) + return in; + + if (d - byNum <= 0) + { + //return in; + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + } + else + d -= byNum; + + string newStr = in.substr(0, it) + std::to_string(d) + in.substr(in.find(' ', it + 1)); + return newStr; +} + +static int dumpErrors(const vector& listOfProject, const vector& errors) +{ + int errorsCount = 0; + map mapOfFiles; + for (auto& elem : listOfProject) + mapOfFiles[elem.fileName] = &elem; + + int z = 0; + for (auto& file : listOfProject) + { + if (errors[z] == "") + { + FILE* ferr = fopen(file.errPath.c_str(), "w"); + if (!ferr) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + fclose(ferr); + ++z; + continue; + } + + FILE* ferr = fopen(file.errPath.c_str(), "w"); + FILE* fout = fopen(file.outPath.c_str(), "w"); + if (!ferr) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + if (!fout) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + string errS = "", outS = ""; + vector splited; + splitString(errors[z], '\n', splited); + for (auto& elem : splited) + { + if (elem.find("Warning 308") != string::npos) + outS += shiftLines(elem, mapOfFiles, &file) + "\n"; + else if (elem.find("Error") != string::npos) + { + errS += shiftLines(elem, mapOfFiles, &file) + "\n"; + errorsCount++; + } + } + + fprintf(fout, "%s", outS.c_str()); + fprintf(ferr, "%s", errS.c_str()); + + fflush(NULL); + + fclose(fout); + fclose(ferr); + ++z; + } + + return errorsCount; +} + +static int createMapOfUse(const vector& errors, const vector& listOfProject, map> &mapModuleDeps) +{ + int changed = 0; + for (int z = 0; z < listOfProject.size(); ++z) + { + if (listOfProject[z].error >= 0) + { + vector splited; + splitString(errors[z], '\n', splited); + for (auto& err : splited) + { + if (err.find("Warning 308") != string::npos && err.find(listOfProject[z].fileName) != string::npos) + { + auto pos = err.find("Unknown module"); + if (pos != string::npos) + { + pos += strlen("Unknown module") + 1; + string substr = ""; + while (err[pos] != ' ' && pos != err.size()) + substr += err[pos++]; + mapModuleDeps[listOfProject[z].fileName].insert(substr); + changed++; + } + } + } + } + } + + return changed; +} + +static map> createModuleOrder(const map &moduleDelc, const map> &mapModuleDeps) +{ + map> modDirectOrder; + for (auto& elem : moduleDelc) + modDirectOrder[elem.first] = set(); + + for (auto& elem : moduleDelc) + { + auto itF = mapModuleDeps.find(elem.second); + if (itF != mapModuleDeps.end()) + { + for (auto& inFile : itF->second) + { + if (moduleDelc.find(inFile) != moduleDelc.end()) + modDirectOrder[elem.first].insert(inFile); + } + } + } + + return modDirectOrder; +} + +static void printDebug(const map>& mapModuleDeps, const map>& modDirectOrder, + const vector& listOfProject, bool console = false) +{ + string toPrint = "MODULE DEPS:\n"; + for (auto& elem : mapModuleDeps) + { + toPrint += elem.first + '\n'; + for (auto& setEl : elem.second) + toPrint += " " + setEl + '\n'; + } + toPrint += "MODULE DIRECT ORDER:\n"; + for (auto& elem : modDirectOrder) + { + toPrint += elem.first + '\n'; + for (auto& setEl : elem.second) + toPrint += " " + setEl + '\n'; + } + toPrint += "FILES LVL:\n"; + for (auto& elem : listOfProject) + toPrint += elem.fileName + " " + elem.outDepPath + " lvl = " + std::to_string(elem.lvl) + '\n'; + if (console) + printf("%s\n", toPrint.c_str()); + __spf_print(1, "%s\n", toPrint.c_str()); +} + +static int parseFiles(vector& errors, vector& listOfProject, vector& filesCompilationOrder, + int parseForInlining, bool isFromConsole = false) +{ + int rethrow = 0; + int iters = 0; + int changed = 0; + int lastChanged = 0; + const string projName = "tmp"; + + map> mapModuleDeps; + map moduleDelc; + map> modDirectOrder; + + try + { + do + { +#ifdef _WIN32 + sendMessage_1lvl(L"выполняется " + std::to_wstring((iters + 1)) + L" итерация синтаксического анализа"); +#else + sendMessage_1lvl(L"running " + std::to_wstring((iters + 1)) + L" iteration of syntax analisys"); +#endif + errors = parseList(listOfProject, iters != 0, (parseForInlining != 0), mapModuleDeps, moduleDelc, modDirectOrder, isFromConsole); + changed = createMapOfUse(errors, listOfProject, mapModuleDeps); + if (iters != 0) + if (lastChanged <= changed) + break; + + createNeededException(); + + if (changed) + { + vector files; + for (auto& elem : listOfProject) + if (elem.error == 0) + files.push_back(elem.outDepPath); + if (files.size() == 0) + break; + findModuleDeclInProject(projName + std::to_string(iters++), files, moduleDelc); + modDirectOrder = createModuleOrder(moduleDelc, mapModuleDeps); + } + lastChanged = changed; + //printDebug(mapModuleDeps, modDirectOrder, listOfProject); + } while (changed); + + + //printDebug(mapModuleDeps, modDirectOrder, listOfProject); + + int added = 0; + int iter = 0; + vector files; + while (added != listOfProject.size()) + { + for (auto& elem : listOfProject) + { + if (elem.lvl == iter) + { + files.push_back(elem.fileName); + added++; + } + } + ++iter; + } + + map> fileDeps; + for (auto& file : files) + { + fileDeps[file] = set(); + if (mapModuleDeps.find(file) == mapModuleDeps.end()) + continue; + + for (auto& dep : mapModuleDeps[file]) + { + if (moduleDelc.find(dep) == moduleDelc.end()) + continue; + fileDeps[file].insert(moduleDelc[dep]); + } + } + + set addedFiles; + + added = 0; + while (added != fileDeps.size()) + { + for (auto& file : fileDeps) + { + bool depsAdded = true; + for (auto& dep : file.second) + if (addedFiles.find(dep) == addedFiles.end()) + depsAdded = false; + + if (depsAdded && addedFiles.find(file.first) == addedFiles.end()) + { + filesCompilationOrder.push_back(file.first); + addedFiles.insert(file.first); + added++; + } + } + } + + + __spf_print(1, "files compilation order:\n"); + for (auto& file : filesCompilationOrder) + __spf_print(1, " %s\n", file.c_str()); + } + catch (int err) + { + rethrow = err; + } + catch (...) + { + rethrow = -1; + } + return rethrow; +} + +int parseFiles(const char* proj, vector& filesCompilationOrder, int parseForInlining) +{ + FILE* list = fopen(proj, "r"); + if (!list) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + vector pathSplit; + if (string(proj).find('\\') != string::npos) + splitString(proj, '\\', pathSplit); + else + splitString(proj, '/', pathSplit); + + if (pathSplit.size() < 2) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + if (pathSplit[pathSplit.size() - 2] != "visualiser_data") + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + string fullPath = ""; + for (int z = 0; z < pathSplit.size() - 2; ++z) + fullPath += pathSplit[z] + "/"; + if (fullPath == "") + fullPath = "./"; + else + { + //change dir + if (chdir(fullPath.c_str()) != 0) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + } + + vector listOfProject; + while (!feof(list)) + { + char buf[1024]; + if (fgets(buf, 1024, list) == NULL) + continue; + + string toAdd = buf; + if (toAdd[toAdd.size() - 1] == '\n') + toAdd = toAdd.erase(toAdd.size() - 1); + + string fileNameFixed = ""; + auto idx = toAdd.find(fullPath); + if (idx != string::npos) + fileNameFixed = toAdd.substr(idx + fullPath.size()); + else + fileNameFixed = (toAdd.substr(0, 2) == "./") ? toAdd.substr(2) : toAdd; + + const string optPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".opt"; + const string errPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".err"; + const string outPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".out"; + + const string fileText = readFileToStr(toAdd); + + FILE* opt = fopen(optPath.c_str(), "r"); + if (!opt) + { + __spf_print(1, "can not open path %s\n", optPath.c_str()); + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + } + fgets(buf, 1024, opt); + string toAddOpt = buf; + if (toAddOpt[toAddOpt.size() - 1] == '\n') + toAddOpt = toAddOpt.erase(toAddOpt.size() - 1); + + fclose(opt); + listOfProject.push_back(FileInfo(fileNameFixed, toAddOpt, errPath, outPath, "", fileText)); + } + + fclose(list); + vector errors; + + int rethrow = parseFiles(errors, listOfProject, filesCompilationOrder, parseForInlining); + int errCount = dumpErrors(listOfProject, errors); + + if (rethrow != 0) + throw rethrow; + return -errCount; +} + +void parseFiles(int argc, char** argv) +{ + bool isInline = false; + auto result = splitCommandLineForParse(argv, argc, isInline); + if (result.second.size() == 0) + { + printf("Nothing to parse\n"); + exit(0); + } + + int code = 0; + + vector errors; + vector listOfProject; + + string toAddOpt = ""; + for (auto& opt : result.first) + toAddOpt += opt + " "; + + for (auto& file : result.second) + { + const string fileText = readFileToStr(file); + listOfProject.push_back(FileInfo(file, toAddOpt + "-o " + file + ".dep", "", "", "", fileText, 0)); + } + + vector filesCompilationOrder; + int rethrow = parseFiles(errors, listOfProject, filesCompilationOrder, isInline, true); + if (rethrow == 0) + { + for (auto& err : errors) + { + vector splited; + splitString(err, '\n', splited); + for (auto& elem : splited) + { + if (elem.find("Error") != string::npos) + { + printf("%s\n", elem.c_str()); + code++; + } + } + } + } + + if (code == 0 && rethrow == 0) + { + FILE* proj = fopen("dvm.proj", "w"); + if (proj == NULL) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + for (auto& file : result.second) + fprintf(proj, "%s.dep\n", file.c_str()); + printf("Parsing was completed successfully\n"); + } + else + printf("Parsing was completed with errors, throw code %d, errors count %d\n", rethrow, code); + exit(0); +} diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.h b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.h new file mode 100644 index 0000000..c9f908b --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.h @@ -0,0 +1,7 @@ +#pragma once + +#include +#include + +int parseFiles(const char* proj, std::vector& filesCompilationOrder, int parseForInlining); +void parseFiles(int argc, char** argv); diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.cpp new file mode 100644 index 0000000..e16206f --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.cpp @@ -0,0 +1,38 @@ +#include "../Utils/leak_detector.h" + +#include +#include + +#include +#include + +#include "../Utils/errors.h" +#include "../Utils/SgUtils.h" +#include "../VisualizerCalls/get_information.h" +#include "../VisualizerCalls/SendMessage.h" + +#include "StdCapture.h" + +using namespace std; + +extern int pppa_analyzer(int argv, char** argc); + +int pppaAnalyzer(const char* options) +{ + string optionsS(options); + vector splited = splitAndArgvCreate(optionsS); + + char** argv = new char* [splited.size()]; + for (int z = 0; z < splited.size(); ++z) + argv[z] = (char*)splited[z].c_str(); + + StdCapture stdCapture; + stdCapture.Init(); + string errorMessage = ""; + int retCode = pppa_analyzer(splited.size(), argv); + stdCapture.EndCapture(); + errorMessage = stdCapture.GetCapture(); + + delete []argv; + return retCode; +} diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.h b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.h new file mode 100644 index 0000000..5e81a81 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.h @@ -0,0 +1,3 @@ +#pragma once + +int pppaAnalyzer(const char* options); \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h new file mode 100644 index 0000000..b0ab35b --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h @@ -0,0 +1,190 @@ +#pragma once + +#include +#include +#include + +#ifdef _WIN32 +#include +#include + +#define popen _popen +#define pclose _pclose +#define stat _stat +#define dup _dup +#define dup2 _dup2 +#define fileno _fileno +#define close _close +#define pipe _pipe +#define read _read +#define eof _eof +#else +#include +#endif + +#ifndef STD_OUT_FD +#define STD_OUT_FD (fileno(stdout)) +#endif + +#ifndef STD_ERR_FD +#define STD_ERR_FD (fileno(stderr)) +#endif + +class StdCapture +{ + int m_pipe[2]; + int m_oldStdOut; + int m_oldStdErr; + bool m_capturing; + std::mutex m_mutex; + std::string m_captured; + + enum PIPES { READ, WRITE }; + + int secure_dup(int src) + { + int ret = -1; + bool fd_blocked = false; + do + { + ret = dup(src); + fd_blocked = (errno == EINTR || errno == EBUSY); + if (fd_blocked) + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } while (ret < 0); + return ret; + } + + void secure_pipe(int* pipes) + { + int ret = -1; + bool fd_blocked = false; + do + { +#ifdef _WIN32 + ret = pipe(pipes, 1024 * 1024 * 20, O_BINARY); // 20 MB +#else + ret = pipe(pipes) == -1; + fcntl(*pipes, F_SETPIPE_SZ, 1024 * 1024 * 20); +#endif + fd_blocked = (errno == EINTR || errno == EBUSY); + if (fd_blocked) + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } while (ret < 0); + } + + void secure_dup2(int src, int dest) + { + int ret = -1; + bool fd_blocked = false; + do + { + ret = dup2(src, dest); + fd_blocked = (errno == EINTR || errno == EBUSY); + if (fd_blocked) + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } while (ret < 0); + } + + void secure_close(int& fd) + { + int ret = -1; + bool fd_blocked = false; + do + { + ret = close(fd); + fd_blocked = (errno == EINTR); + if (fd_blocked) + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } while (ret < 0); + + fd = -1; + } + +public: + void Init() + { + // make stdout & stderr streams unbuffered + // so that we don't need to flush the streams + // before capture and after capture + // (fflush can cause a deadlock if the stream is currently being + std::lock_guard lock(m_mutex); + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + } + + void BeginCapture() + { + std::lock_guard lock(m_mutex); + if (m_capturing) + return; + + secure_pipe(m_pipe); + m_oldStdOut = secure_dup(STD_OUT_FD); + m_oldStdErr = secure_dup(STD_ERR_FD); + secure_dup2(m_pipe[WRITE], STD_OUT_FD); + secure_dup2(m_pipe[WRITE], STD_ERR_FD); + m_capturing = true; +#ifndef _WIN32 + secure_close(m_pipe[WRITE]); +#endif + } + + bool IsCapturing() + { + std::lock_guard lock(m_mutex); + return m_capturing; + } + + void EndCapture() + { + std::lock_guard lock(m_mutex); + if (!m_capturing) + return; + + m_captured.clear(); + secure_dup2(m_oldStdOut, STD_OUT_FD); + secure_dup2(m_oldStdErr, STD_ERR_FD); + + const int bufSize = 1025; + char buf[bufSize]; + int bytesRead = 0; + bool fd_blocked(false); + do + { + bytesRead = 0; + fd_blocked = false; +#ifdef _WIN32 + if (!eof(m_pipe[READ])) + bytesRead = read(m_pipe[READ], buf, bufSize - 1); +#else + bytesRead = read(m_pipe[READ], buf, bufSize - 1); +#endif + if (bytesRead > 0) + { + buf[bytesRead] = 0; + m_captured += buf; + } + else if (bytesRead < 0) + { + fd_blocked = (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR); + if (fd_blocked) + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + } while (fd_blocked || bytesRead == (bufSize - 1)); + + secure_close(m_oldStdOut); + secure_close(m_oldStdErr); + secure_close(m_pipe[READ]); +#ifdef _WIN32 + secure_close(m_pipe[WRITE]); +#endif + m_capturing = false; + } + + std::string GetCapture() + { + std::lock_guard lock(m_mutex); + return m_captured; + } +}; diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index 3edd5a6..ccd563d 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -33,6 +33,8 @@ #include "Utils/errors.h" #include "Utils/SgUtils.h" +#include "ProjectManipulation/ParseFiles.h" + #include "LoopAnalyzer/loop_analyzer.h" #include "LoopAnalyzer/loop_analyzer_nodist.h" @@ -2564,11 +2566,8 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam } } -extern "C" int parse_file(int argc, char* argv[], char* proj_name); extern int pppa_analyzer(int argv, char** argc); -bool runAsClient = false; - int main(int argc, char **argv) { int leakMemDump = 0; @@ -2730,62 +2729,7 @@ int main(int argc, char **argv) else if (string(curr_arg) == "-autoArray") parallizeFreeLoops = 1; else if (string(curr_arg) == "-parse") - { - bool isInline = false; - auto result = splitCommandLineForParse(argv + (i + 1), argc - (i + 1), isInline); - if (result.second.size() == 0) - { - printf("Nothing to parse\n"); - exit(0); - } - - int code = 0; - - vector errors; - vector listOfProject; - - string toAddOpt = ""; - for (auto& opt : result.first) - toAddOpt += opt + " "; - - for (auto& file : result.second) - { - const string fileText = readFileToStr(file); - listOfProject.push_back(FileInfo(file, toAddOpt + "-o " + file + ".dep", "", "", "", fileText, 0)); - } - - int rethrow = parseFiles(errors, listOfProject, filesCompilationOrder, isInline, true); - if (rethrow == 0) - { - for (auto& err : errors) - { - vector splited; - splitString(err, '\n', splited); - for (auto& elem : splited) - { - if (elem.find("Error") != string::npos) - { - printf("%s\n", elem.c_str()); - code++; - } - } - } - } - - if (code == 0 && rethrow == 0) - { - FILE* proj = fopen("dvm.proj", "w"); - if (proj == NULL) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - - for (auto& file : result.second) - fprintf(proj, "%s.dep\n", file.c_str()); - printf("Parsing was completed successfully\n"); - } - else - printf("Parsing was completed with errors, throw code %d, errors count %d\n", rethrow, code); - exit(0); - } + parseFiles(argc - (i + 1), argv + (i + 1)); else if (string(curr_arg) == "-mpi") mpiProgram = 1; else if (string(curr_arg) == "-pppa") diff --git a/sapfor/experts/Sapfor_2017/_src/SapforData.h b/sapfor/experts/Sapfor_2017/_src/SapforData.h index 7835946..d495e12 100644 --- a/sapfor/experts/Sapfor_2017/_src/SapforData.h +++ b/sapfor/experts/Sapfor_2017/_src/SapforData.h @@ -47,6 +47,7 @@ bool fullDepGraph = false; bool noLogo = false; bool withTemplateInfo = false; bool inlcudeAllFiles = false; // for pass INSERT_INLCUDES +bool runAsClient = false; // run console project as client for Visualizer uint64_t currentAvailMemory = 0; int QUALITY; // quality of conflicts search in graph diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp index fd45c3b..a395ec4 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp @@ -23,6 +23,7 @@ #include #include #include + #ifdef _WIN32 #include #else @@ -58,30 +59,6 @@ using std::make_tuple; using std::wstring; using std::stack; -#ifdef _MSC_VER -#include -#define popen _popen -#define pclose _pclose -#define stat _stat -#define dup _dup -#define dup2 _dup2 -#define fileno _fileno -#define close _close -#define pipe _pipe -#define read _read -#define eof _eof -#else -#include -#endif - -#ifndef STD_OUT_FD -#define STD_OUT_FD (fileno(stdout)) -#endif - -#ifndef STD_ERR_FD -#define STD_ERR_FD (fileno(stderr)) -#endif - static bool ifIntervalExists(const vector> &intervals, const pair &toFind, pair& inInterval) { bool retVal = false; @@ -2768,903 +2745,6 @@ vector makeDeclaration(const vector& symbolsToDeclare, return allDecls; } -static void findModuleDeclInProject(const string &name, const vector &files, map &modDecls) -{ - char** filesList = new char* [files.size()]; - for (int z = 0; z < files.size(); ++z) - filesList[z] = (char*)files[z].c_str(); - - SgProject* tmpProj = new SgProject(name.c_str(), filesList, files.size()); - - int numF = tmpProj->numberOfFiles(); - set filesSg; - for (int z = 0; z < numF; ++z) - { - vector modules; - SgFile* currF = &tmpProj->file(z); - string fileName = currF->filename(); - convertToLower(fileName); - - filesSg.insert(currF); - - findModulesInFile(currF, modules); - for (auto& elem : modules) - { - if (string(elem->fileName()) == currF->filename()) - { - const string name = elem->symbol()->identifier(); - auto it = modDecls.find(name); - if (it != modDecls.end() && it->second != currF->filename()) - { - __spf_print(1, "found several module declaration of '%s' in files '%s' and '%s'\n", name.c_str(), it->second.c_str(), currF->filename()); - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - } - else - modDecls.insert(it, make_pair(name, currF->filename())); - } - } - } - - delete[]filesList; - InitializeTable(); -} - -class StdCapture -{ -public: - static void Init() - { - // make stdout & stderr streams unbuffered - // so that we don't need to flush the streams - // before capture and after capture - // (fflush can cause a deadlock if the stream is currently being - std::lock_guard lock(m_mutex); - setvbuf(stdout, NULL, _IONBF, 0); - setvbuf(stderr, NULL, _IONBF, 0); - } - - static void BeginCapture() - { - std::lock_guard lock(m_mutex); - if (m_capturing) - return; - - secure_pipe(m_pipe); - m_oldStdOut = secure_dup(STD_OUT_FD); - m_oldStdErr = secure_dup(STD_ERR_FD); - secure_dup2(m_pipe[WRITE], STD_OUT_FD); - secure_dup2(m_pipe[WRITE], STD_ERR_FD); - m_capturing = true; -#ifndef _MSC_VER - secure_close(m_pipe[WRITE]); -#endif - } - static bool IsCapturing() - { - std::lock_guard lock(m_mutex); - return m_capturing; - } - static void EndCapture() - { - std::lock_guard lock(m_mutex); - if (!m_capturing) - return; - - m_captured.clear(); - secure_dup2(m_oldStdOut, STD_OUT_FD); - secure_dup2(m_oldStdErr, STD_ERR_FD); - - const int bufSize = 1025; - char buf[bufSize]; - int bytesRead = 0; - bool fd_blocked(false); - do - { - bytesRead = 0; - fd_blocked = false; -#ifdef _MSC_VER - if (!eof(m_pipe[READ])) - bytesRead = read(m_pipe[READ], buf, bufSize - 1); -#else - bytesRead = read(m_pipe[READ], buf, bufSize - 1); -#endif - if (bytesRead > 0) - { - buf[bytesRead] = 0; - m_captured += buf; - } - else if (bytesRead < 0) - { - fd_blocked = (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR); - if (fd_blocked) - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } - } while (fd_blocked || bytesRead == (bufSize - 1)); - - secure_close(m_oldStdOut); - secure_close(m_oldStdErr); - secure_close(m_pipe[READ]); -#ifdef _MSC_VER - secure_close(m_pipe[WRITE]); -#endif - m_capturing = false; - } - static std::string GetCapture() - { - std::lock_guard lock(m_mutex); - return m_captured; - } -private: - enum PIPES { READ, WRITE }; - - static int secure_dup(int src) - { - int ret = -1; - bool fd_blocked = false; - do - { - ret = dup(src); - fd_blocked = (errno == EINTR || errno == EBUSY); - if (fd_blocked) - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } while (ret < 0); - return ret; - } - static void secure_pipe(int* pipes) - { - int ret = -1; - bool fd_blocked = false; - do - { -#ifdef _MSC_VER - ret = pipe(pipes, 1024 * 1024 * 20, O_BINARY); // 20 MB -#else - ret = pipe(pipes) == -1; - fcntl(*pipes, F_SETPIPE_SZ, 1024 * 1024 * 20); -#endif - fd_blocked = (errno == EINTR || errno == EBUSY); - if (fd_blocked) - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } while (ret < 0); - } - static void secure_dup2(int src, int dest) - { - int ret = -1; - bool fd_blocked = false; - do - { - ret = dup2(src, dest); - fd_blocked = (errno == EINTR || errno == EBUSY); - if (fd_blocked) - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } while (ret < 0); - } - - static void secure_close(int& fd) - { - int ret = -1; - bool fd_blocked = false; - do - { - ret = close(fd); - fd_blocked = (errno == EINTR); - if (fd_blocked) - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } while (ret < 0); - - fd = -1; - } - - static int m_pipe[2]; - static int m_oldStdOut; - static int m_oldStdErr; - static bool m_capturing; - static std::mutex m_mutex; - static std::string m_captured; -}; - -// actually define vars. -int StdCapture::m_pipe[2]; -int StdCapture::m_oldStdOut; -int StdCapture::m_oldStdErr; -bool StdCapture::m_capturing; -std::mutex StdCapture::m_mutex; -std::string StdCapture::m_captured; - -static vector splitAndArgvCreate(const string& options) -{ - vector optSplited; - optSplited.push_back(""); - splitString(options, ' ', optSplited, true); - - vector optSplited1; - for (auto& elem : optSplited) - if (elem != "") - optSplited1.push_back(elem); - optSplited1.insert(optSplited1.begin(), ""); - - for (int z = 0; z < optSplited1.size(); ++z) - { - //printf("%s\n", optSplited1[z].c_str()); - if (optSplited1[z][0] == '"' && optSplited1[z][optSplited1[z].size() - 1] == '"') - optSplited1[z] = optSplited1[z].substr(1, optSplited1[z].size() - 2); - } - return optSplited1; -} - -static void createIncludeOrder(vector &toIncl, - const map& moduleDelc, - const map>& modDirectOrder, - set &done, - const string &curr) -{ - if (done.find(curr) == done.end()) - { - for (auto& elem : modDirectOrder.find(curr)->second) - createIncludeOrder(toIncl, moduleDelc, modDirectOrder, done, elem); - - if (done.find(curr) == done.end()) - { - toIncl.push_back(moduleDelc.find(curr)->second); - done.insert(curr); - } - } -} - -static set applyModuleDeclsForFile(FileInfo *forFile, const map &mapFiles, - const map& moduleDelc, - const map>& mapModuleDeps, - const map>& modDirectOrder, - vector &optSplited, - bool includeForInline = false) -{ - set retFilesMod; - - auto itF = mapModuleDeps.find(forFile->fileName); - if (itF == mapModuleDeps.end() && !includeForInline) - return retFilesMod; - - vector toIncl; - set done; - if (itF != mapModuleDeps.end()) - { - for (auto& mod : itF->second) - if (moduleDelc.find(mod) != moduleDelc.end()) - createIncludeOrder(toIncl, moduleDelc, modDirectOrder, done, mod); - } - - //rewrite files to the next iter of parse - set allFiles; - bool needToConvertStyle = false; - for (auto& incl : toIncl) - { - if (mapFiles.find(incl) == mapFiles.end()) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - allFiles.insert(mapFiles.find(incl)->second); - } - allFiles.insert(forFile); - - int style = forFile->style; - for (auto& elem : allFiles) - { - if (style != elem->style) - { - needToConvertStyle = true; - break; - } - } - - string mainText = forFile->text; - if (needToConvertStyle) - { - for (auto& elem : allFiles) - { - if (elem->style != 2) - { - retFilesMod.insert(elem); - if (elem != forFile) - auto tmp = convertStyle(elem); - else - mainText = convertStyle(elem, false); - } - } - - if (forFile->style != 2) - { - for (auto& opt : optSplited) - { - if (opt == "-FI" || opt == "-extend_source") - opt = "-FR"; - } - } - } - - string include = ""; - int includeCount = 0; - set included; - for (auto& incl : toIncl) - { - if (included.find(incl) == included.end()) - { - include += " include '" + incl + "'\n"; - includeCount++; - } - included.insert(incl); - } - - vector toInclEnds; - string includeLast = ""; - - if (includeForInline) - { - //find needed modules first - vector filesWithModules; - for (auto& elem : moduleDelc) - filesWithModules.push_back(elem.second); - for (auto& file : filesWithModules) - { - if (file != forFile->fileName && included.find(file) == included.end()) - { - toInclEnds.push_back(file); - included.insert(file); - } - } - - for (auto& file : mapFiles) - if (file.second != forFile && included.find(file.second->fileName) == included.end()) - toInclEnds.push_back(file.second->fileName); - - if (toInclEnds.size()) - includeLast += "!SPF SHADOW FILES\n"; - - for (auto& incl : toInclEnds) - includeLast += " include '" + incl + "'\n"; - } - - if (includeCount) - include = "!SPF NUM FILES " + std::to_string(includeCount) + "\n" + include; - - const string data = include + mainText + includeLast; - __spf_print(1, "include to file %s before\n", forFile->fileName.c_str()); - __spf_print(1, "%s", include.c_str()); - __spf_print(1, "include to file %s after\n", forFile->fileName.c_str()); - __spf_print(1, "%s", includeLast.c_str()); - - writeFileFromStr(forFile->fileName, data); - - forFile->includesAdded = included.size(); - forFile->includes = included; - - retFilesMod.insert(forFile); - return retFilesMod; -} - -static void restoreOriginalText(const vector& listOfProject) -{ - for (auto& elem : listOfProject) - writeFileFromStr(elem.fileName, elem.text); - fflush(NULL); -} - -static inline void restoreOriginalText(const FileInfo& file) -{ - writeFileFromStr(file.fileName, file.text); -} - -static void checkRetCode(FileInfo& info, const string& errorMessage) -{ - if (info.error != 0) - info.lvl++; - - if (errorMessage.find("Warning 308") != string::npos) - if (info.error == 0) - info.error = 1; -} - - -extern "C" int parse_file(int argc, char* argv[], char* proj_name); -static vector parseList(vector& listOfProject, - bool needToInclude, bool needToIncludeForInline, - const map> &mapModuleDeps, - const map &moduleDelc, - const map> &modDirectOrder, bool isFromConsole = false) -{ - map mapFiles; - for (auto& elem : listOfProject) - mapFiles[elem.fileName] = &elem; - - vector errors; - int i = 1; - int N = listOfProject.size(); - for (auto& elem : listOfProject) - { - sendMessage_progress(std::to_wstring((int)(((double)(i++) / N) * 100))); - - string file = elem.fileName; - string options = elem.options; - vector optSplited = splitAndArgvCreate(options); - - char** toParse = new char* [optSplited.size() + 1]; - for (int z = 0; z < optSplited.size(); ++z) - { - toParse[z] = new char[optSplited[z].size() + 1]; - strcpy(toParse[z], optSplited[z].c_str()); - } - toParse[optSplited.size()] = new char[file.size() + 1]; - strcpy(toParse[optSplited.size()], file.c_str()); - - if (options.find("-FI") != string::npos) - elem.style = 0; - else if (options.find("-FR") != string::npos || options.find("-f90") != string::npos) - elem.style = 2; - else if (options.find("-extend_source") != string::npos) - elem.style = 1; - - for (int z = 0; z < optSplited.size(); ++z) - { - if (optSplited[z] == "-o") - { - if (z + 1 == optSplited.size()) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - elem.outDepPath = optSplited[z + 1]; - break; - } - } - - FILE* depPath = fopen(elem.outDepPath.c_str(), "r"); - if (depPath && !isFromConsole) - { - fclose(depPath); - if (elem.error <= 0) - { - elem.error = 0; - errors.push_back(""); - for (int z = 0; z <= optSplited.size(); ++z) - delete toParse[z]; - delete[] toParse; - continue; - } - } - -#ifdef _WIN32 - sendMessage_2lvl(L" обработка файла '" + to_wstring(file) + L"'"); -#else - sendMessage_2lvl(L" processing file '" + to_wstring(file) + L"'"); -#endif - StdCapture::Init(); - string errorMessage = ""; - try - { - set filesModified; - StdCapture::BeginCapture(); - if (needToInclude) - filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline); - else if (needToIncludeForInline) // TODO for modules - filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline); - - int retCode = parse_file(optSplited.size(), toParse, "dvm.proj"); - if (needToInclude || needToIncludeForInline) - { - for (auto &elem : filesModified) - restoreOriginalText(*elem); - fflush(NULL); - } - - elem.error = retCode; - StdCapture::EndCapture(); - errorMessage = StdCapture::GetCapture(); - checkRetCode(elem, errorMessage); - } - catch (int err) - { - StdCapture::EndCapture(); - errorMessage = StdCapture::GetCapture(); - - if (needToInclude || needToIncludeForInline) - restoreOriginalText(listOfProject); - } - catch (...) - { - StdCapture::EndCapture(); - errorMessage = StdCapture::GetCapture(); - - if (needToInclude || needToIncludeForInline) - restoreOriginalText(listOfProject); - } - errors.push_back(errorMessage); - for (int z = 0; z <= optSplited.size(); ++z) - delete toParse[z]; - delete[] toParse; - - createNeededException(); - } - return errors; -} - -static string shiftLines(const string &in, const map &mapOfFiles, const FileInfo* currF) -{ - int byNum = 0; - - auto it = in.find("on line "); - if (it != string::npos) - it += strlen("on line "); - - int d = 0; - sscanf(in.c_str() + it, "%d", &d); - - auto it1 = in.find("of", it + 1); - if (it1 == string::npos) - return in; - it1 += 3; - - string fileN = in.substr(it1, in.find(':', it1) - it1); - auto itF = mapOfFiles.find(fileN); - if (itF == mapOfFiles.end()) - return in; - if (itF->second != currF) - return in; - - byNum = itF->second->includesAdded; - if (byNum == 0) - return in; - - if (d - byNum <= 0) - { - //return in; - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - } - else - d -= byNum; - - string newStr = in.substr(0, it) + std::to_string(d) + in.substr(in.find(' ', it + 1)); - return newStr; -} - -static int dumpErrors(const vector& listOfProject, const vector& errors) -{ - int errorsCount = 0; - map mapOfFiles; - for (auto& elem : listOfProject) - mapOfFiles[elem.fileName] = &elem; - - int z = 0; - for (auto& file : listOfProject) - { - if (errors[z] == "") - { - FILE* ferr = fopen(file.errPath.c_str(), "w"); - if (!ferr) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - fclose(ferr); - ++z; - continue; - } - - FILE* ferr = fopen(file.errPath.c_str(), "w"); - FILE* fout = fopen(file.outPath.c_str(), "w"); - if (!ferr) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - if (!fout) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - - string errS = "", outS = ""; - vector splited; - splitString(errors[z], '\n', splited); - for (auto& elem : splited) - { - if (elem.find("Warning 308") != string::npos) - outS += shiftLines(elem, mapOfFiles, &file) + "\n"; - else if (elem.find("Error") != string::npos) - { - errS += shiftLines(elem, mapOfFiles, &file) + "\n"; - errorsCount++; - } - } - - fprintf(fout, "%s", outS.c_str()); - fprintf(ferr, "%s", errS.c_str()); - - fflush(NULL); - - fclose(fout); - fclose(ferr); - ++z; - } - - return errorsCount; -} - -static int createMapOfUse(const vector& errors, const vector& listOfProject, map> &mapModuleDeps) -{ - int changed = 0; - for (int z = 0; z < listOfProject.size(); ++z) - { - if (listOfProject[z].error >= 0) - { - vector splited; - splitString(errors[z], '\n', splited); - for (auto& err : splited) - { - if (err.find("Warning 308") != string::npos && err.find(listOfProject[z].fileName) != string::npos) - { - auto pos = err.find("Unknown module"); - if (pos != string::npos) - { - pos += strlen("Unknown module") + 1; - string substr = ""; - while (err[pos] != ' ' && pos != err.size()) - substr += err[pos++]; - mapModuleDeps[listOfProject[z].fileName].insert(substr); - changed++; - } - } - } - } - } - - return changed; -} - -static map> createModuleOrder(const map &moduleDelc, const map> &mapModuleDeps) -{ - map> modDirectOrder; - for (auto& elem : moduleDelc) - modDirectOrder[elem.first] = set(); - - for (auto& elem : moduleDelc) - { - auto itF = mapModuleDeps.find(elem.second); - if (itF != mapModuleDeps.end()) - { - for (auto& inFile : itF->second) - { - if (moduleDelc.find(inFile) != moduleDelc.end()) - modDirectOrder[elem.first].insert(inFile); - } - } - } - - return modDirectOrder; -} - -static void printDebug(const map>& mapModuleDeps, const map>& modDirectOrder, - const vector& listOfProject, bool console = false) -{ - string toPrint = "MODULE DEPS:\n"; - for (auto& elem : mapModuleDeps) - { - toPrint += elem.first + '\n'; - for (auto& setEl : elem.second) - toPrint += " " + setEl + '\n'; - } - toPrint += "MODULE DIRECT ORDER:\n"; - for (auto& elem : modDirectOrder) - { - toPrint += elem.first + '\n'; - for (auto& setEl : elem.second) - toPrint += " " + setEl + '\n'; - } - toPrint += "FILES LVL:\n"; - for (auto& elem : listOfProject) - toPrint += elem.fileName + " " + elem.outDepPath + " lvl = " + std::to_string(elem.lvl) + '\n'; - if (console) - printf("%s\n", toPrint.c_str()); - __spf_print(1, "%s\n", toPrint.c_str()); -} - -int parseFiles(vector& errors, vector& listOfProject, vector& filesCompilationOrder, int parseForInlining, bool isFromConsole) -{ - int rethrow = 0; - int iters = 0; - int changed = 0; - int lastChanged = 0; - const string projName = "tmp"; - - map> mapModuleDeps; - map moduleDelc; - map> modDirectOrder; - - try - { - do - { -#ifdef _WIN32 - sendMessage_1lvl(L"выполняется " + std::to_wstring((iters + 1)) + L" итерация синтаксического анализа"); -#else - sendMessage_1lvl(L"running " + std::to_wstring((iters + 1)) + L" iteration of syntax analisys"); -#endif - errors = parseList(listOfProject, iters != 0, (parseForInlining != 0), mapModuleDeps, moduleDelc, modDirectOrder, isFromConsole); - changed = createMapOfUse(errors, listOfProject, mapModuleDeps); - if (iters != 0) - if (lastChanged <= changed) - break; - - createNeededException(); - - if (changed) - { - vector files; - for (auto& elem : listOfProject) - if (elem.error == 0) - files.push_back(elem.outDepPath); - if (files.size() == 0) - break; - findModuleDeclInProject(projName + std::to_string(iters++), files, moduleDelc); - modDirectOrder = createModuleOrder(moduleDelc, mapModuleDeps); - } - lastChanged = changed; - //printDebug(mapModuleDeps, modDirectOrder, listOfProject); - } while (changed); - - - //printDebug(mapModuleDeps, modDirectOrder, listOfProject); - - int added = 0; - int iter = 0; - vector files; - while (added != listOfProject.size()) - { - for (auto& elem : listOfProject) - { - if (elem.lvl == iter) - { - files.push_back(elem.fileName); - added++; - } - } - ++iter; - } - - map> fileDeps; - for (auto& file : files) - { - fileDeps[file] = set(); - if (mapModuleDeps.find(file) == mapModuleDeps.end()) - continue; - - for (auto& dep : mapModuleDeps[file]) - { - if (moduleDelc.find(dep) == moduleDelc.end()) - continue; - fileDeps[file].insert(moduleDelc[dep]); - } - } - - set addedFiles; - - added = 0; - while (added != fileDeps.size()) - { - for (auto& file : fileDeps) - { - bool depsAdded = true; - for (auto& dep : file.second) - if (addedFiles.find(dep) == addedFiles.end()) - depsAdded = false; - - if (depsAdded && addedFiles.find(file.first) == addedFiles.end()) - { - filesCompilationOrder.push_back(file.first); - addedFiles.insert(file.first); - added++; - } - } - } - - - __spf_print(1, "files compilation order:\n"); - for (auto& file : filesCompilationOrder) - __spf_print(1, " %s\n", file.c_str()); - } - catch (int err) - { - rethrow = err; - } - catch (...) - { - rethrow = -1; - } - return rethrow; -} - -int parseFiles(const char* proj, vector& filesCompilationOrder, int parseForInlining) -{ - FILE* list = fopen(proj, "r"); - if (!list) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - - vector pathSplit; - if (string(proj).find('\\') != string::npos) - splitString(proj, '\\', pathSplit); - else - splitString(proj, '/', pathSplit); - - if (pathSplit.size() < 2) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - if (pathSplit[pathSplit.size() - 2] != "visualiser_data") - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - string fullPath = ""; - for (int z = 0; z < pathSplit.size() - 2; ++z) - fullPath += pathSplit[z] + "/"; - if (fullPath == "") - fullPath = "./"; - else - { - //change dir - if (chdir(fullPath.c_str()) != 0) - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - } - - vector listOfProject; - while (!feof(list)) - { - char buf[1024]; - if (fgets(buf, 1024, list) == NULL) - continue; - - string toAdd = buf; - if (toAdd[toAdd.size() - 1] == '\n') - toAdd = toAdd.erase(toAdd.size() - 1); - - string fileNameFixed = ""; - auto idx = toAdd.find(fullPath); - if (idx != string::npos) - fileNameFixed = toAdd.substr(idx + fullPath.size()); - else - fileNameFixed = (toAdd.substr(0, 2) == "./") ? toAdd.substr(2) : toAdd; - - const string optPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".opt"; - const string errPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".err"; - const string outPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".out"; - - const string fileText = readFileToStr(toAdd); - - FILE* opt = fopen(optPath.c_str(), "r"); - if (!opt) - { - __spf_print(1, "can not open path %s\n", optPath.c_str()); - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - } - fgets(buf, 1024, opt); - string toAddOpt = buf; - if (toAddOpt[toAddOpt.size() - 1] == '\n') - toAddOpt = toAddOpt.erase(toAddOpt.size() - 1); - - fclose(opt); - listOfProject.push_back(FileInfo(fileNameFixed, toAddOpt, errPath, outPath, "", fileText)); - } - - fclose(list); - vector errors; - - int rethrow = parseFiles(errors, listOfProject, filesCompilationOrder, parseForInlining); - int errCount = dumpErrors(listOfProject, errors); - - if (rethrow != 0) - throw rethrow; - return -errCount; -} - -extern int pppa_analyzer(int argv, char** argc); -int pppaAnalyzer(const char* options) -{ - string optionsS(options); - vector splited = splitAndArgvCreate(optionsS); - - char** argv = new char* [splited.size()]; - for (int z = 0; z < splited.size(); ++z) - argv[z] = (char*)splited[z].c_str(); - - StdCapture::Init(); - string errorMessage = ""; - int retCode = pppa_analyzer(splited.size(), argv); - StdCapture::EndCapture(); - errorMessage = StdCapture::GetCapture(); - - delete[]argv; - return retCode; -} - int getNextFreeLabel() { PTR_LABEL lab; diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h index b952a5b..0222d18 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h @@ -74,11 +74,6 @@ std::string unparseProjectToString(SgFile* file, const int curr_regime); SgStatement* makeDeclaration(SgStatement* curr, const std::vector& s, std::vector* inits = NULL); std::vector makeDeclaration(const std::vector& symbolsToDeclare, SgStatement* where, std::vector* inits = NULL); -int parseFiles(const char* proj, std::vector& filesCompilationOrder, int parseForInlining); -int parseFiles(std::vector& errors, std::vector& listOfProject, std::vector& filesCompilationOrder, int parseForInlining, bool isFromConsole = false); - -int pppaAnalyzer(const char* options); - int getNextFreeLabel(); void fillUsedModulesInFunction(SgStatement *st, std::vector &useStats); @@ -111,4 +106,4 @@ void removeSpecialCommentsFromProject(SgFile* file); void getMaxMinBlockDistribution(SgFile* file, std::pair& min_max); -void addPrivatesToArraysFromGUI(SgFile* file, const std::map, std::pair>& declaredArrays, const std::map& distrStateFromGUI); \ No newline at end of file +void addPrivatesToArraysFromGUI(SgFile* file, const std::map, std::pair>& declaredArrays, const std::map& distrStateFromGUI); diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp index 303a273..90531e3 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp @@ -150,7 +150,7 @@ void printHelp(const char **passNames, const int lastPass) printf(" -keepDVM keep DVM directives\n"); printf(" -allVars get all parallel versions\n"); printf(" -var N get specific parallel version, N=1,2,..\n"); - printf(" -parse run parser with next option\n"); + printf(" -parse run parser with next option (-inl option allow to parse project for -inlineH/I regime)\n"); printf(" -inlineH run hierarchical inlining for all functions called from 'funcName'\n"); printf(" -inlineI run incremental inlining for function 'funcName' on 'lineNum' of 'fileName'\n"); printf(" -passInfo print passes information\n"); @@ -1537,56 +1537,6 @@ wstring fixedLongFormat(const wchar_t* old_) return ret; } -string convertStyle(const FileInfo* file, bool needRewrite) -{ - string text = file->text; - - vector splited; - splitString(text, '\n', splited); - - text = ""; - int z = 0; - for (auto& line : splited) - { - if (line[0] == 'c' || line[0] == 'C' || line[0] == 'd' || line[0] == 'D' || line[0] == '*') - line[0] = '!'; - - bool needContinuation = false; - if (line[0] != '!' && line.size() > 6) - { - if (line[5] != ' ' && !(line[5] > '0' && line[5] < '9')) // not label - { - line[5] = ' '; - needContinuation = true;// line[5] = '&'; - } - - int p = 73; - if (file->style == 1) - p = 133; - if (line.size() > p) - { - while (line[p] != '\0' && line[p] != '\n' && line[p] != '!') - { - line[p] = ' '; - p++; - if (p >= line.size()) - break; - } - } - } - - if (needContinuation) - text += "&"; - text += (z != 0 ? "\n" : "") + line; - ++z; - } - - if (needRewrite) - writeFileFromStr(file->fileName, text); - - return text; -} - const set getExcludedModules() { return { "omp_lib", "ifport" }; }; map sortArraysByName(const set& toSort) @@ -1599,4 +1549,25 @@ map sortArraysByName(const set& toSort) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); return sorted; -} \ No newline at end of file +} + +vector splitAndArgvCreate(const string& options) +{ + vector optSplited; + optSplited.push_back(""); + splitString(options, ' ', optSplited, true); + + vector optSplited1; + for (auto& elem : optSplited) + if (elem != "") + optSplited1.push_back(elem); + optSplited1.insert(optSplited1.begin(), ""); + + for (int z = 0; z < optSplited1.size(); ++z) + { + //printf("%s\n", optSplited1[z].c_str()); + if (optSplited1[z][0] == '"' && optSplited1[z][optSplited1[z].size() - 1] == '"') + optSplited1[z] = optSplited1[z].substr(1, optSplited1[z].size() - 2); + } + return optSplited1; +} diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/utils.h b/sapfor/experts/Sapfor_2017/_src/Utils/utils.h index d3643d9..e878329 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/utils.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/utils.h @@ -87,50 +87,6 @@ std::pair, std::vector> splitCommandLineFo std::string getClearName(const std::string& in); std::wstring fixedLongFormat(const wchar_t* old); -struct FileInfo -{ - FileInfo() - { - fileName = ""; - options = ""; - errPath = ""; - outPath = ""; - outDepPath = ""; - text = ""; - error = -1; - includesAdded = 0; - style = -1; - lvl = 0; - } - - FileInfo(const std::string& _fileName, const std::string& _options, const std::string& _errPath, const std::string& _outPath, - const std::string& _outDepPath, const std::string& _text, int errorInit = -1) - { - fileName = _fileName; - options = _options; - errPath = _errPath; - outPath = _outPath; - outDepPath = _outDepPath; - text = _text; - error = errorInit; - includesAdded = 0; - style = -1; - lvl = 0; - } - - int error; - std::string fileName; - std::string options; - std::string errPath; - std::string outPath; - std::string outDepPath; - std::string text; - int style; // -1 unk, 0 fixed, 1 fixed ext, 2 free - int includesAdded; - std::set includes; - int lvl; -}; - -std::string convertStyle(const FileInfo* file, bool needRewrite = true); std::map sortArraysByName(const std::set& toSort); -bool createDirectory(const std::string& name); \ No newline at end of file +bool createDirectory(const std::string& name); +std::vector splitAndArgvCreate(const std::string& options); diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 1a06b02..855d4b4 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 "2275" +#define VERSION_SPF "2276" diff --git a/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp b/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp index 6ec8dd9..eadc920 100644 --- a/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp +++ b/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp @@ -40,6 +40,8 @@ #include "../Distribution/CreateDistributionDirs.h" #include "../LoopAnalyzer/loop_analyzer.h" #include "../DirectiveProcessing/insert_directive.h" +#include "../ProjectManipulation/PerfAnalyzer.h" + #include "BuildGraph.h" #ifdef _WIN32 From 13326020430e2ad5657c93604d3dd4d7703a4729 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Tue, 20 Feb 2024 12:42:35 +0300 Subject: [PATCH 37/40] code refactoring --- sapfor/experts/Sapfor_2017/CMakeLists.txt | 4 ++- .../_src/ProjectManipulation/ConvertFiles.cpp | 21 ++++++++++++ .../_src/ProjectManipulation/ConvertFiles.h | 3 ++ .../_src/ProjectManipulation/ParseFiles.cpp | 19 ++++++----- .../_src/ProjectManipulation/PerfAnalyzer.cpp | 19 +++++++---- .../_src/ProjectManipulation/PerfAnalyzer.h | 3 +- .../_src/ProjectManipulation/StdCapture.h | 32 +++++++++---------- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 17 ++++------ .../experts/Sapfor_2017/_src/Utils/utils.cpp | 2 ++ .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 10 files changed, 76 insertions(+), 46 deletions(-) create mode 100644 sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp create mode 100644 sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.h diff --git a/sapfor/experts/Sapfor_2017/CMakeLists.txt b/sapfor/experts/Sapfor_2017/CMakeLists.txt index d30abc3..ddf04a6 100644 --- a/sapfor/experts/Sapfor_2017/CMakeLists.txt +++ b/sapfor/experts/Sapfor_2017/CMakeLists.txt @@ -337,7 +337,9 @@ set(PROJ_MAN _src/ProjectManipulation/ParseFiles.cpp _src/ProjectManipulation/PerfAnalyzer.cpp _src/ProjectManipulation/PerfAnalyzer.h _src/ProjectManipulation/FileInfo.cpp - _src/ProjectManipulation/FileInfo.h) + _src/ProjectManipulation/FileInfo.h + _src/ProjectManipulation/ConvertFiles.cpp + _src/ProjectManipulation/ConvertFiles.h) set(PARSER ${parser_sources}/cftn.c ${parser_sources}/errors.c diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp new file mode 100644 index 0000000..c4ba01e --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp @@ -0,0 +1,21 @@ +#include "../Utils/leak_detector.h" + +#include +#include + +#include +#include + +#include "../Utils/errors.h" +#include "../Utils/SgUtils.h" + +#include "ConvertFiles.h" + +using namespace std; + +extern int convert_file(int argc, char* argv[], const char* proj_name); + +void convertFiles(int argc, char* argv[], const char* proj_name) +{ + convert_file(argc, argv, proj_name); +} \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.h b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.h new file mode 100644 index 0000000..30c2046 --- /dev/null +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.h @@ -0,0 +1,3 @@ +#pragma once + +void convertFiles(int argc, char* argv[], const char* proj_name = "dvm.proj"); diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp index 27c92ac..a039c0f 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp @@ -312,14 +312,13 @@ static vector parseList(vector& listOfProject, sendMessage_2lvl(L" обработка файла '" + to_wstring(file) + L"'"); #else sendMessage_2lvl(L" processing file '" + to_wstring(file) + L"'"); -#endif - StdCapture stdCapture; - stdCapture.Init(); +#endif + StdCapture::Init(); string errorMessage = ""; try { set filesModified; - stdCapture.BeginCapture(); + StdCapture::BeginCapture(); if (needToInclude) filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline); else if (needToIncludeForInline) // TODO for modules @@ -334,22 +333,22 @@ static vector parseList(vector& listOfProject, } elem.error = retCode; - stdCapture.EndCapture(); - errorMessage = stdCapture.GetCapture(); + StdCapture::EndCapture(); + errorMessage = StdCapture::GetCapture(); checkRetCode(elem, errorMessage); } catch (int err) { - stdCapture.EndCapture(); - errorMessage = stdCapture.GetCapture(); + StdCapture::EndCapture(); + errorMessage = StdCapture::GetCapture(); if (needToInclude || needToIncludeForInline) restoreOriginalText(listOfProject); } catch (...) { - stdCapture.EndCapture(); - errorMessage = stdCapture.GetCapture(); + StdCapture::EndCapture(); + errorMessage = StdCapture::GetCapture(); if (needToInclude || needToIncludeForInline) restoreOriginalText(listOfProject); diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.cpp index e16206f..c3ee28c 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.cpp @@ -8,8 +8,6 @@ #include "../Utils/errors.h" #include "../Utils/SgUtils.h" -#include "../VisualizerCalls/get_information.h" -#include "../VisualizerCalls/SendMessage.h" #include "StdCapture.h" @@ -26,13 +24,22 @@ int pppaAnalyzer(const char* options) for (int z = 0; z < splited.size(); ++z) argv[z] = (char*)splited[z].c_str(); - StdCapture stdCapture; - stdCapture.Init(); + StdCapture::Init(); string errorMessage = ""; int retCode = pppa_analyzer(splited.size(), argv); - stdCapture.EndCapture(); - errorMessage = stdCapture.GetCapture(); + StdCapture::EndCapture(); + errorMessage = StdCapture::GetCapture(); delete []argv; return retCode; } + +void pppaAnalyzer(int argc, char** argv) +{ + int code = pppa_analyzer(argc, argv); + if (code == 0) + printf("PPPA was completed successfully\n"); + else + printf("PPPA was completed with error code %d\n", code); + exit(0); +} diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.h b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.h index 5e81a81..70f806d 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.h +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/PerfAnalyzer.h @@ -1,3 +1,4 @@ #pragma once -int pppaAnalyzer(const char* options); \ No newline at end of file +int pppaAnalyzer(const char* options); +void pppaAnalyzer(int argc, char** argv); diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h index b0ab35b..8a3b8c9 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h @@ -30,18 +30,18 @@ #define STD_ERR_FD (fileno(stderr)) #endif +static int m_pipe[2]; +static int m_oldStdOut; +static int m_oldStdErr; +static bool m_capturing; +static std::mutex m_mutex; +static std::string m_captured; + class StdCapture { - int m_pipe[2]; - int m_oldStdOut; - int m_oldStdErr; - bool m_capturing; - std::mutex m_mutex; - std::string m_captured; - enum PIPES { READ, WRITE }; - int secure_dup(int src) + static int secure_dup(int src) { int ret = -1; bool fd_blocked = false; @@ -55,7 +55,7 @@ class StdCapture return ret; } - void secure_pipe(int* pipes) + static void secure_pipe(int* pipes) { int ret = -1; bool fd_blocked = false; @@ -73,7 +73,7 @@ class StdCapture } while (ret < 0); } - void secure_dup2(int src, int dest) + static void secure_dup2(int src, int dest) { int ret = -1; bool fd_blocked = false; @@ -86,7 +86,7 @@ class StdCapture } while (ret < 0); } - void secure_close(int& fd) + static void secure_close(int& fd) { int ret = -1; bool fd_blocked = false; @@ -102,7 +102,7 @@ class StdCapture } public: - void Init() + static void Init() { // make stdout & stderr streams unbuffered // so that we don't need to flush the streams @@ -113,7 +113,7 @@ public: setvbuf(stderr, NULL, _IONBF, 0); } - void BeginCapture() + static void BeginCapture() { std::lock_guard lock(m_mutex); if (m_capturing) @@ -130,13 +130,13 @@ public: #endif } - bool IsCapturing() + static bool IsCapturing() { std::lock_guard lock(m_mutex); return m_capturing; } - void EndCapture() + static void EndCapture() { std::lock_guard lock(m_mutex); if (!m_capturing) @@ -182,7 +182,7 @@ public: m_capturing = false; } - std::string GetCapture() + static std::string GetCapture() { std::lock_guard lock(m_mutex); return m_captured; diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index ccd563d..c7851fe 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -34,6 +34,8 @@ #include "Utils/errors.h" #include "Utils/SgUtils.h" #include "ProjectManipulation/ParseFiles.h" +#include "ProjectManipulation/PerfAnalyzer.h" +#include "ProjectManipulation/ConvertFiles.h" #include "LoopAnalyzer/loop_analyzer.h" #include "LoopAnalyzer/loop_analyzer_nodist.h" @@ -2566,8 +2568,6 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam } } -extern int pppa_analyzer(int argv, char** argc); - int main(int argc, char **argv) { int leakMemDump = 0; @@ -2730,17 +2730,12 @@ int main(int argc, char **argv) parallizeFreeLoops = 1; else if (string(curr_arg) == "-parse") parseFiles(argc - (i + 1), argv + (i + 1)); + else if (string(curr_arg) == "-pppa") + pppaAnalyzer(argc - (i + 1), argv + (i + 1)); + else if (string(curr_arg) == "-fdvm") + convertFiles(argc - (i + 1), argv + (i + 1)); else if (string(curr_arg) == "-mpi") mpiProgram = 1; - else if (string(curr_arg) == "-pppa") - { - int code = pppa_analyzer(argc - i, argv + i); - if (code == 0) - printf("PPPA was completed successfully\n"); - else - printf("PPPA was completed with error code %d\n", code); - exit(0); - } else if (string(curr_arg) == "-client") { runAsClient = true; diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp index 90531e3..61e438b 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp @@ -151,6 +151,8 @@ void printHelp(const char **passNames, const int lastPass) printf(" -allVars get all parallel versions\n"); printf(" -var N get specific parallel version, N=1,2,..\n"); printf(" -parse run parser with next option (-inl option allow to parse project for -inlineH/I regime)\n"); + printf(" -pppa run performance statistic analyzer \n"); + printf(" -fdvm run fdvm convertation\n"); printf(" -inlineH run hierarchical inlining for all functions called from 'funcName'\n"); printf(" -inlineI run incremental inlining for function 'funcName' on 'lineNum' of 'fileName'\n"); printf(" -passInfo print passes information\n"); diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 855d4b4..21fd553 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 "2276" +#define VERSION_SPF "2277" From deaeb4a670c876a435ab340a9662695bfb967297 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Tue, 20 Feb 2024 13:15:05 +0300 Subject: [PATCH 38/40] fixed --- sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h | 1 + 1 file changed, 1 insertion(+) diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h index 8a3b8c9..4867c7a 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/StdCapture.h @@ -2,6 +2,7 @@ #include #include +#include #include #ifdef _WIN32 From 4386eb70416a66787ebaf068194c4948801a16dd Mon Sep 17 00:00:00 2001 From: ALEXks Date: Sun, 25 Feb 2024 11:16:56 +0300 Subject: [PATCH 39/40] improved parser, fixed convertation --- dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c | 9 +- dvm/fdvm/trunk/fdvm/dvm.cpp | 14 +- dvm/fdvm/trunk/parser/cftn.c | 2 + dvm/fdvm/trunk/parser/sym.c | 2 + .../Sapfor_2017/_src/GraphCall/graph_calls.h | 3 + .../Sapfor_2017/_src/Inliner/inliner.cpp | 41 ++- .../_src/ProjectManipulation/ConvertFiles.cpp | 12 +- .../_src/ProjectManipulation/FileInfo.cpp | 45 ++- .../_src/ProjectManipulation/FileInfo.h | 3 +- .../_src/ProjectManipulation/ParseFiles.cpp | 268 +++++++++--------- sapfor/experts/Sapfor_2017/_src/Sapfor.cpp | 4 +- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- .../_src/VisualizerCalls/get_information.cpp | 6 + 13 files changed, 259 insertions(+), 152 deletions(-) diff --git a/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c b/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c index 54fb28e..92326cf 100644 --- a/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c +++ b/dvm/fdvm/trunk/Sage/lib/newsrc/low_level.c @@ -80,6 +80,7 @@ extern void BufferAllocate(); int out_free_form; 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; static int CountNullBifNext = 0; /* for internal debugging */ @@ -1305,14 +1306,14 @@ char* filter(char *s) { char c; int i = 1, quote = 0; - + // 14.10.2016 Kolganov. Switch constant buffer to dynamic int temp_size = 4096; char *temp = (char*)malloc(sizeof(char) * temp_size); #ifdef __SPF addToCollection(__LINE__, __FILE__,temp, 0); #endif - + // out_line_length = 132 if -ffo option is used or out_line_length = 72 if -uniForm option is used int temp_i = 0; int buf_i = 0; int commentline = 0; @@ -1442,7 +1443,7 @@ char* filter(char *s) } else { - if (((!out_free_form && temp_i == 71) || (out_free_form && !out_line_unlimit && temp_i == 131)) && !commentline && (s[i + 1] != '\n')) + if (((!out_free_form && temp_i == 71) || (out_free_form && !out_line_unlimit && temp_i == out_line_length - 1)) && !commentline && (s[i + 1] != '\n')) { if (buf_i + 1 > temp_size) { @@ -1479,7 +1480,7 @@ char* filter(char *s) buf_i = -1; } - if (((!out_free_form && temp_i == 71) || (out_free_form && !out_line_unlimit && temp_i == 131)) && commentline && (s[i + 1] != '\n') && ((OMP == 1) || (OMP == 2) || (DVM == 1) || (SPF == 1))) /*07.08.17*/ + if (((!out_free_form && temp_i == 71) || (out_free_form && !out_line_unlimit && temp_i == out_line_length - 1)) && commentline && (s[i + 1] != '\n') && ((OMP == 1) || (OMP == 2) || (DVM == 1) || (SPF == 1))) /*07.08.17*/ { if (buf_i + 1 > temp_size) { diff --git a/dvm/fdvm/trunk/fdvm/dvm.cpp b/dvm/fdvm/trunk/fdvm/dvm.cpp index 5087bd0..97a838d 100644 --- a/dvm/fdvm/trunk/fdvm/dvm.cpp +++ b/dvm/fdvm/trunk/fdvm/dvm.cpp @@ -80,6 +80,7 @@ extern int iacross; extern "C" int out_free_form; 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; Options options; @@ -280,6 +281,11 @@ int main(int argc, char *argv[]) out_upper_case = 1; else if (!strcmp(argv[0], "-noLimitLine")) out_line_unlimit = 1; + else if (!strcmp(argv[0], "-uniForm")) + { + out_free_form = 1; + out_line_length = 72; + } else if (!strcmp(argv[0], "-noRemote")) options.setOn(NO_REMOTE); else if (!strcmp(argv[0], "-lgstd")) @@ -365,6 +371,11 @@ int main(int argc, char *argv[]) (void)fprintf(stderr, "Warning: -Ohost option is set to -Opl2 mode\n"); options.setOn(O_HOST); } + if(out_free_form == 1 && out_line_length == 72 && out_line_unlimit == 1) + { + (void)fprintf(stderr, "Warning: -noLimitLine and -uniForm options are incompatible; -noLimitLine option is ignored\n"); + out_line_unlimit = 0; + } if (v_print) (void)fprintf(stderr, "<<<<< Translating >>>>>\n"); @@ -392,7 +403,7 @@ int main(int argc, char *argv[]) // for call of function 'tpoint' //added one symbol to input-file name //printf("%s",fin_name); //!!! debug - if(a_mode && project.numberOfFiles()>1) + if(a_mode || project.numberOfFiles()>1) fout_name = doOutFileName(file->filename()); //project.fileName(id); else if (fout_name && source_name && !strcmp(source_name, fout_name)) { @@ -582,6 +593,7 @@ void initialize() out_free_form = 0; out_upper_case = 0; out_line_unlimit = 0; + out_line_length = 132; default_integer_size = 4; default_real_size = 4; unparse_functions = 0; //set to 1 by option -byFunUnparse diff --git a/dvm/fdvm/trunk/parser/cftn.c b/dvm/fdvm/trunk/parser/cftn.c index 174c660..871c9d4 100644 --- a/dvm/fdvm/trunk/parser/cftn.c +++ b/dvm/fdvm/trunk/parser/cftn.c @@ -507,6 +507,8 @@ int main(int argc, char *argv[]) ; else if (!strcmp(argv[0], "-noLimitLine")) ; + else if (!strcmp(argv[0], "-uniForm")) + ; else if (!strcmp(argv[0], "-noRemote")) ; else if (!strcmp(argv[0], "-lgstd")) diff --git a/dvm/fdvm/trunk/parser/sym.c b/dvm/fdvm/trunk/parser/sym.c index ab14245..caa1bb6 100644 --- a/dvm/fdvm/trunk/parser/sym.c +++ b/dvm/fdvm/trunk/parser/sym.c @@ -1945,6 +1945,8 @@ copy_module_scope(sym_mod,list) for(source=sym_mod->entry.Template.next; source; source=source->entry.Template.next) { if((source->attr & PRIVATE_BIT) && (!(source->attr & PUBLIC_BIT)) ) continue; + if(source->variant == FUNCTION_NAME && source->decl != YES) /* intrinsic function called from specification expression */ /* podd 24.02.24 */ + continue; if(list && in_rename_list(source,list)) continue; if((copy=just_look_up_sym_in_scope(cur_scope(),source->ident)) && copy->id_attr && copy->id_attr->entry.Template.tag==sym_mod->entry.Template.func_hedr->id) diff --git a/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.h b/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.h index c376dfe..07e933f 100644 --- a/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.h +++ b/sapfor/experts/Sapfor_2017/_src/GraphCall/graph_calls.h @@ -222,6 +222,9 @@ struct FuncInfo std::string getCallName(const std::pair& call_info, const std::string& name, int line) { + if (line <= 0) + return name; + std::set names; for (auto& call : callsFromDetailed) diff --git a/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp b/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp index 5716f4f..4e1c584 100644 --- a/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Inliner/inliner.cpp @@ -268,7 +268,6 @@ static vector getLowBounds(SgSymbol* arrayS) oneExpr = new SgValueExp(1); SgExpression* list = NULL; - if (isAllocated(arrayS)) { @@ -284,7 +283,27 @@ static vector getLowBounds(SgSymbol* arrayS) int consistInAllocates = 0; const string origName = OriginalSymbol(copyFrom)->identifier(); - for (auto data : getAttributes(decl, set{ ALLOCATE_STMT })) + auto allocData = getAttributes(decl, set{ ALLOCATE_STMT }); + if (allocData.size() == 0) // try to find statements in original file + { + string declFile(decl->fileName()); + int line = decl->lineNumber(); + string file_name = current_file->filename(); + + if (current_file->filename() != declFile) + { + auto trueDecl = SgStatement::getStatementByFileAndLine(declFile, line); + if (trueDecl) + { + auto trueData = getAttributes(trueDecl, set{ ALLOCATE_STMT }); + if (trueData.size()) + allocData = trueData; + } + } + SgFile::switchToFile(file_name); + } + + for (auto data : allocData) { if (data->variant() != ALLOCATE_STMT) continue; @@ -314,7 +333,7 @@ static vector getLowBounds(SgSymbol* arrayS) list = NULL; if (list == NULL) - __spf_print(1, "find for %s, consistInAllocates = %d", arrayS->identifier(), consistInAllocates); + __spf_print(1, "find for %s, consistInAllocates = %d\n", arrayS->identifier(), consistInAllocates); checkNull(list, convertFileName(__FILE__).c_str(), __LINE__); } else @@ -961,10 +980,14 @@ static void insert(SgStatement* callSt, SgStatement* tempHedr, SgStatement* begi auto next = prev->lexNext(); if (callSt->comments()) { - if (next->comments()) - next->setComments((string(callSt->comments()) + next->comments()).c_str()); + string callCom(callSt->comments()); + if (next->comments()) { + string newCom = callCom + next->comments(); + next->setComments(newCom.c_str()); + } else - next->addComment(callSt->comments()); + next->addComment(callCom.c_str()); + callSt->delComments(); } auto ident = string(tempHedr->symbol()->identifier()); @@ -1737,7 +1760,7 @@ static bool inliner(const string& fileName_in, const string& funcName, const int point.currLvl = 0; point.currCall = func->funcName; - __spf_print(1, " INLINE %s\n", func->funcName.c_str()); + __spf_print(1, " INLINE %s - ", func->funcName.c_str()); #ifdef _WIN32 sendMessage_2lvl(wstring(L"подстановка функции '") + wstring(func->funcName.begin(), func->funcName.end()) + L"'"); #else @@ -1745,6 +1768,7 @@ static bool inliner(const string& fileName_in, const string& funcName, const int #endif //1 level bool isInlined = run_inliner(funcMap, toInsert, SPF_messages, fileName, func, newSymbsToDeclare, point, commonBlocks); + __spf_print(1, "%s\n", isInlined ? "done" : "fault"); if (isInlined == false) { @@ -1776,8 +1800,9 @@ static bool inliner(const string& fileName_in, const string& funcName, const int point.currLvl = currDeep; point.currCall = next.first->funcName; - __spf_print(1, " INLINE %s\n", next.first->funcName.c_str()); + __spf_print(1, " INLINE %s - ", next.first->funcName.c_str()); bool isInlined = run_inliner(funcMap, next.second, SPF_messages, fileName, next.first, newSymbsToDeclare, point, commonBlocks); + __spf_print(1, "%s\n", isInlined ? "done" : "fault"); changed |= isInlined; } } diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp index c4ba01e..746f53b 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp @@ -17,5 +17,15 @@ extern int convert_file(int argc, char* argv[], const char* proj_name); void convertFiles(int argc, char* argv[], const char* proj_name) { - convert_file(argc, argv, proj_name); + vector args_v; + for (int z = 0; z < argc; ++z) + args_v.push_back(argv[z]); + + args_v.push_back("-a"); + args_v.push_back((char*)proj_name); + + args_v.push_back(NULL); + + int code = convert_file(args_v.size() - 1, args_v.data(), proj_name); + exit(code); } \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.cpp index 5e10ef0..aa95940 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.cpp @@ -8,9 +8,52 @@ #include "FileInfo.h" #include "../Utils/utils.h" +#include "../Utils/errors.h" using namespace std; +static int tmp_id = 0; +static string tmp_name = "tmp_conv_"; + +extern "C" int out_free_form; +extern "C" int out_line_length; + +//convert through unparce +void FileInfo::convertToUniform() +{ + int old_free = out_free_form; + int old_line = out_line_length; + + out_free_form = 1; + out_line_length = 72; + + __spf_print(1, "covnert to uniform %s file\n", fileName.c_str()); + + if (error != 0) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + if (outDepPath == "") + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + vector filesList; + filesList.push_back((char*)outDepPath.c_str()); + + const string name = tmp_name + to_string(tmp_id++); + SgProject* tmpProj = new SgProject(name.c_str(), filesList.data(), 1); + + if (tmpProj == NULL) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + SgFile* currF = &tmpProj->file(0); + + string text = string(currF->firstStatement()->unparse()); + writeFileFromStr(fileName, text); + + out_free_form = old_free; + out_line_length = old_line; + + InitializeTable(); +} + string FileInfo::convertStyle(bool needRewrite) { string tmp_text = text; @@ -59,4 +102,4 @@ string FileInfo::convertStyle(bool needRewrite) writeFileFromStr(fileName, tmp_text); return tmp_text; -} \ No newline at end of file +} diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.h b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.h index 4bcb728..fa40412 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.h +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/FileInfo.h @@ -41,10 +41,11 @@ struct FileInfo std::string outPath; std::string outDepPath; std::string text; - int style; // -1 unk, 0 fixed, 1 fixed ext, 2 free + int style; // -1 unk, 0 fixed, 1 fixed ext, 2 free, 3 uniform int includesAdded; std::set includes; int lvl; std::string convertStyle(bool needRewrite = true); + void convertToUniform(); }; diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp index a039c0f..198e7bd 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp @@ -37,14 +37,13 @@ extern "C" int parse_file(int argc, char* argv[], char* proj_name); static void findModuleDeclInProject(const string& name, const vector& files, map& modDecls) { - char** filesList = new char* [files.size()]; + vector filesList; for (int z = 0; z < files.size(); ++z) - filesList[z] = (char*)files[z].c_str(); + filesList.push_back((char*)files[z].c_str()); - SgProject* tmpProj = new SgProject(name.c_str(), filesList, files.size()); + SgProject* tmpProj = new SgProject(name.c_str(), filesList.data(), files.size()); int numF = tmpProj->numberOfFiles(); - set filesSg; for (int z = 0; z < numF; ++z) { vector modules; @@ -52,8 +51,6 @@ static void findModuleDeclInProject(const string& name, const vector& fi string fileName = currF->filename(); convertToLower(fileName); - filesSg.insert(currF); - findModulesInFile(currF, modules); for (auto& elem : modules) { @@ -72,7 +69,6 @@ static void findModuleDeclInProject(const string& name, const vector& fi } } - delete []filesList; InitializeTable(); } @@ -119,7 +115,6 @@ static set applyModuleDeclsForFile(FileInfo *forFile, const map allFiles; - bool needToConvertStyle = false; for (auto& incl : toIncl) { if (mapFiles.find(incl) == mapFiles.end()) @@ -128,40 +123,15 @@ static set applyModuleDeclsForFile(FileInfo *forFile, const mapstyle; - for (auto& elem : allFiles) - { - if (style != elem->style) - { - needToConvertStyle = true; - break; - } - } + set toConvert; + int mainStyle = forFile->style; + for (auto& file : allFiles) + if (mainStyle != file->style && file->style != 3) + toConvert.insert(file); - string mainText = forFile->text; - if (needToConvertStyle) - { - for (auto& elem : allFiles) - { - if (elem->style != 2) - { - retFilesMod.insert(elem); - if (elem != forFile) - elem->convertStyle(); - else - mainText = elem->convertStyle(false); - } - } - - if (forFile->style != 2) - { - for (auto& opt : optSplited) - { - if (opt == "-FI" || opt == "-extend_source") - opt = "-FR"; - } - } - } + const string mainText = forFile->text; + for (auto& file : toConvert) + file->convertToUniform(); string include = ""; int includeCount = 0; @@ -277,10 +247,21 @@ static vector parseList(vector& listOfProject, if (options.find("-FI") != string::npos) elem.style = 0; - else if (options.find("-FR") != string::npos || options.find("-f90") != string::npos) - elem.style = 2; else if (options.find("-extend_source") != string::npos) elem.style = 1; + else if (options.find("-FR") != string::npos || options.find("-f90") != string::npos) + elem.style = 2; + else + { //fdv|f|ftn|for|f90|f95|f03 + static set fixed_exts = { "for", "f", "ftn" }; + static set free_exts = { "f90", "f95", "f03" }; + string ext = OnlyExt(file.c_str()); + + if (fixed_exts.find(ext) != fixed_exts.end()) + elem.style = 0; + else if (free_exts.find(ext) != free_exts.end()) + elem.style = 2; + } for (int z = 0; z < optSplited.size(); ++z) { @@ -534,14 +515,115 @@ static void printDebug(const map>& mapModuleDeps, const map< __spf_print(1, "%s\n", toPrint.c_str()); } +static void parseFiles(int& iters, vector& errors, vector& listOfProject, + map>& mapModuleDeps, map& moduleDelc, map>& modDirectOrder, + int parseForInlining, bool isFromConsole) +{ + int changed = 0; + int lastChanged = 0; + const string projName = "tmp"; + + do + { +#ifdef _WIN32 + sendMessage_1lvl(L"выполняется " + std::to_wstring((iters + 1)) + L" итерация синтаксического анализа"); +#else + sendMessage_1lvl(L"running " + std::to_wstring((iters + 1)) + L" iteration of syntax analisys"); +#endif + errors = parseList(listOfProject, iters != 0, parseForInlining, mapModuleDeps, moduleDelc, modDirectOrder, isFromConsole); + changed = createMapOfUse(errors, listOfProject, mapModuleDeps); + if (iters != 0) + if (lastChanged <= changed) + break; + + createNeededException(); + + if (changed) + { + vector files; + for (auto& elem : listOfProject) + if (elem.error == 0) + files.push_back(elem.outDepPath); + if (files.size() == 0) + break; + findModuleDeclInProject(projName + std::to_string(iters++), files, moduleDelc); + modDirectOrder = createModuleOrder(moduleDelc, mapModuleDeps); + } + lastChanged = changed; + //printDebug(mapModuleDeps, modDirectOrder, listOfProject); + } while (changed); +} + +static vector finalyzeParsing(const vector& listOfProject, + const map> mapModuleDeps, + const map moduleDelc) +{ + vector filesCompilationOrder; + int added = 0; + int iter = 0; + vector files; + + while (added != listOfProject.size()) + { + for (auto& elem : listOfProject) + { + if (elem.lvl == iter) + { + files.push_back(elem.fileName); + added++; + } + } + ++iter; + } + + map> fileDeps; + for (auto& file : files) + { + fileDeps[file] = set(); + if (mapModuleDeps.find(file) == mapModuleDeps.end()) + continue; + + for (auto& dep : mapModuleDeps.at(file)) + { + if (moduleDelc.find(dep) == moduleDelc.end()) + continue; + fileDeps[file].insert(moduleDelc.at(dep)); + } + } + + set addedFiles; + + added = 0; + while (added != fileDeps.size()) + { + for (auto& file : fileDeps) + { + bool depsAdded = true; + for (auto& dep : file.second) + if (addedFiles.find(dep) == addedFiles.end()) + depsAdded = false; + + if (depsAdded && addedFiles.find(file.first) == addedFiles.end()) + { + filesCompilationOrder.push_back(file.first); + addedFiles.insert(file.first); + added++; + } + } + } + + __spf_print(1, "files compilation order:\n"); + for (auto& file : filesCompilationOrder) + __spf_print(1, " %s\n", file.c_str()); + + return filesCompilationOrder; +} + static int parseFiles(vector& errors, vector& listOfProject, vector& filesCompilationOrder, int parseForInlining, bool isFromConsole = false) { int rethrow = 0; int iters = 0; - int changed = 0; - int lastChanged = 0; - const string projName = "tmp"; map> mapModuleDeps; map moduleDelc; @@ -549,95 +631,15 @@ static int parseFiles(vector& errors, vector& listOfProject, v try { - do + parseFiles(iters, errors, listOfProject, mapModuleDeps, moduleDelc, modDirectOrder, false, isFromConsole); + filesCompilationOrder = finalyzeParsing(listOfProject, mapModuleDeps, moduleDelc); + + if (parseForInlining) { -#ifdef _WIN32 - sendMessage_1lvl(L"выполняется " + std::to_wstring((iters + 1)) + L" итерация синтаксического анализа"); -#else - sendMessage_1lvl(L"running " + std::to_wstring((iters + 1)) + L" iteration of syntax analisys"); -#endif - errors = parseList(listOfProject, iters != 0, (parseForInlining != 0), mapModuleDeps, moduleDelc, modDirectOrder, isFromConsole); - changed = createMapOfUse(errors, listOfProject, mapModuleDeps); - if (iters != 0) - if (lastChanged <= changed) - break; - - createNeededException(); - - if (changed) - { - vector files; - for (auto& elem : listOfProject) - if (elem.error == 0) - files.push_back(elem.outDepPath); - if (files.size() == 0) - break; - findModuleDeclInProject(projName + std::to_string(iters++), files, moduleDelc); - modDirectOrder = createModuleOrder(moduleDelc, mapModuleDeps); - } - lastChanged = changed; - //printDebug(mapModuleDeps, modDirectOrder, listOfProject); - } while (changed); - - - //printDebug(mapModuleDeps, modDirectOrder, listOfProject); - - int added = 0; - int iter = 0; - vector files; - while (added != listOfProject.size()) - { - for (auto& elem : listOfProject) - { - if (elem.lvl == iter) - { - files.push_back(elem.fileName); - added++; - } - } - ++iter; + for (auto& file : listOfProject) + file.outDepPath = ""; + parseFiles(iters, errors, listOfProject, mapModuleDeps, moduleDelc, modDirectOrder, true, isFromConsole); } - - map> fileDeps; - for (auto& file : files) - { - fileDeps[file] = set(); - if (mapModuleDeps.find(file) == mapModuleDeps.end()) - continue; - - for (auto& dep : mapModuleDeps[file]) - { - if (moduleDelc.find(dep) == moduleDelc.end()) - continue; - fileDeps[file].insert(moduleDelc[dep]); - } - } - - set addedFiles; - - added = 0; - while (added != fileDeps.size()) - { - for (auto& file : fileDeps) - { - bool depsAdded = true; - for (auto& dep : file.second) - if (addedFiles.find(dep) == addedFiles.end()) - depsAdded = false; - - if (depsAdded && addedFiles.find(file.first) == addedFiles.end()) - { - filesCompilationOrder.push_back(file.first); - addedFiles.insert(file.first); - added++; - } - } - } - - - __spf_print(1, "files compilation order:\n"); - for (auto& file : filesCompilationOrder) - __spf_print(1, " %s\n", file.c_str()); } catch (int err) { diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index c7851fe..0dcb946 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -2731,9 +2731,9 @@ int main(int argc, char **argv) else if (string(curr_arg) == "-parse") parseFiles(argc - (i + 1), argv + (i + 1)); else if (string(curr_arg) == "-pppa") - pppaAnalyzer(argc - (i + 1), argv + (i + 1)); + pppaAnalyzer(argc - i, argv + i); else if (string(curr_arg) == "-fdvm") - convertFiles(argc - (i + 1), argv + (i + 1)); + convertFiles(argc - i, argv + i); else if (string(curr_arg) == "-mpi") mpiProgram = 1; else if (string(curr_arg) == "-client") diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 21fd553..7f5b76e 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 "2277" +#define VERSION_SPF "2282" diff --git a/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp b/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp index eadc920..dc3a5a9 100644 --- a/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp +++ b/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp @@ -153,6 +153,9 @@ static void setOptions(const short* options, bool isBuildParallel = false, const staticShadowAnalysis = intOptions[STATIC_SHADOW_ANALYSIS]; staticPrivateAnalysis = intOptions[STATIC_PRIVATE_ANALYSIS]; out_free_form = intOptions[FREE_FORM]; + if (out_free_form == 1) + out_line_unlimit = 1; + if (isBuildParallel) keepSpfDirs = intOptions[KEEP_SPF_DIRECTIVES]; else @@ -182,7 +185,10 @@ static void setOptions(const short* options, bool isBuildParallel = false, const else if (elem == STATIC_PRIVATE_ANALYSIS) staticPrivateAnalysis = 0; else if (elem == FREE_FORM) + { out_free_form = 0; + out_line_unlimit = 0; + } else if (elem == KEEP_DVM_DIRECTIVES) keepDvmDirectives = 0; else if (elem == KEEP_SPF_DIRECTIVES) From 8a92f305f415174c492ac456bd5f00e33c814b14 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Wed, 28 Feb 2024 17:38:02 +0300 Subject: [PATCH 40/40] added convertation to sapfor --- dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp | 111 ++-- dvm/fdvm/trunk/Sage/lib/include/libSage++.h | 4 + .../_src/ProjectManipulation/ConvertFiles.cpp | 481 +++++++++++++++++- .../_src/ProjectManipulation/ParseFiles.cpp | 17 +- .../Sapfor_2017/_src/Utils/SgUtils.cpp | 165 +++--- .../experts/Sapfor_2017/_src/Utils/SgUtils.h | 3 + .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 7 files changed, 637 insertions(+), 146 deletions(-) diff --git a/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp b/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp index 0592ef1..4c39cd8 100644 --- a/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp +++ b/dvm/fdvm/trunk/Sage/Sage++/libSage++.cpp @@ -1793,6 +1793,7 @@ bool SgStatement::consistentCheckIsActivated = false; bool SgStatement::deprecatedCheck = false; std::string SgStatement::currProcessFile = ""; int SgStatement::currProcessLine = -1; +bool SgStatement::sapfor_regime = false; void SgStatement::checkConsistence() { @@ -4497,76 +4498,78 @@ SgExecutableStatement* isSgExecutableStatement(SgStatement *pt) return NULL; if (!isADeclBif(BIF_CODE(pt->thebif))) { -#if __SPF - const int var = pt->variant(); - if (var == CONTROL_END) + if (SgStatement::isSapforRegime()) { - SgStatement *cp = pt->controlParent(); - if (cp->variant() == PROG_HEDR || cp->variant() == PROC_HEDR || cp->variant() == FUNC_HEDR) + const int var = pt->variant(); + if (var == CONTROL_END) { - SgStatement* cpcp = cp->controlParent(); - if (cpcp && cpcp->variant() == INTERFACE_STMT) - return NULL; + SgStatement* cp = pt->controlParent(); + if (cp->variant() == PROG_HEDR || cp->variant() == PROC_HEDR || cp->variant() == FUNC_HEDR) + { + SgStatement* cpcp = cp->controlParent(); + if (cpcp && cpcp->variant() == INTERFACE_STMT) + return NULL; + else + return (SgExecutableStatement*)pt; + } else - return (SgExecutableStatement*)pt; + return isSgExecutableStatement(cp); } + else if (var == DVM_INHERIT_DIR || var == DVM_ALIGN_DIR || var == DVM_DYNAMIC_DIR || + var == DVM_DISTRIBUTE_DIR || var == DVM_VAR_DECL || var == DVM_SHADOW_DIR || + var == DVM_HEAP_DIR || var == DVM_CONSISTENT_DIR || var == DVM_POINTER_DIR || + var == HPF_TEMPLATE_STAT || var == HPF_PROCESSORS_STAT || var == DVM_TASK_DIR || + var == DVM_INDIRECT_GROUP_DIR || var == DVM_REMOTE_GROUP_DIR || var == DVM_REDUCTION_GROUP_DIR || + var == DVM_CONSISTENT_GROUP_DIR || var == DVM_ASYNCID_DIR || var == ACC_ROUTINE_DIR) + return NULL; + else if (var == SPF_ANALYSIS_DIR || var == FORMAT_STAT) + return isSgExecutableStatement(pt->lexNext()); else - return isSgExecutableStatement(cp); + return (SgExecutableStatement*)pt; } - else if (var == DVM_INHERIT_DIR || var == DVM_ALIGN_DIR || var == DVM_DYNAMIC_DIR || - var == DVM_DISTRIBUTE_DIR || var == DVM_VAR_DECL || var == DVM_SHADOW_DIR || - var == DVM_HEAP_DIR || var == DVM_CONSISTENT_DIR || var == DVM_POINTER_DIR || - var == HPF_TEMPLATE_STAT || var == HPF_PROCESSORS_STAT || var == DVM_TASK_DIR || - var == DVM_INDIRECT_GROUP_DIR || var == DVM_REMOTE_GROUP_DIR || var == DVM_REDUCTION_GROUP_DIR || - var == DVM_CONSISTENT_GROUP_DIR || var == DVM_ASYNCID_DIR || var == ACC_ROUTINE_DIR) - return NULL; - else if (var == SPF_ANALYSIS_DIR || var == FORMAT_STAT) - return isSgExecutableStatement(pt->lexNext()); else return (SgExecutableStatement*)pt; -#else - return (SgExecutableStatement*)pt; -#endif } else { -#if __SPF - const int var = pt->variant(); - if (var == SPF_PARALLEL_DIR) - return (SgExecutableStatement *)pt; - if (var == SPF_ANALYSIS_DIR || var == SPF_PARALLEL_REG_DIR) - return isSgExecutableStatement(pt->lexNext()); - if (var == SPF_END_PARALLEL_REG_DIR) - return isSgExecutableStatement(pt->lexPrev()); - if (var == SPF_TRANSFORM_DIR) + if (SgStatement::isSapforRegime()) { - SgExpression *ex = pt->expr(0); - while (ex) + const int var = pt->variant(); + if (var == SPF_PARALLEL_DIR) + return (SgExecutableStatement*)pt; + if (var == SPF_ANALYSIS_DIR || var == SPF_PARALLEL_REG_DIR) + return isSgExecutableStatement(pt->lexNext()); + if (var == SPF_END_PARALLEL_REG_DIR) + return isSgExecutableStatement(pt->lexPrev()); + if (var == SPF_TRANSFORM_DIR) { - if (ex->lhs()->variant() == SPF_NOINLINE_OP) - return NULL; - else if (ex->lhs()->variant() == SPF_FISSION_OP || ex->lhs()->variant() == SPF_EXPAND_OP) - return (SgExecutableStatement *)pt; + SgExpression* ex = pt->expr(0); + while (ex) + { + if (ex->lhs()->variant() == SPF_NOINLINE_OP) + return NULL; + else if (ex->lhs()->variant() == SPF_FISSION_OP || ex->lhs()->variant() == SPF_EXPAND_OP) + return (SgExecutableStatement*)pt; - ex = ex->rhs(); + ex = ex->rhs(); + } } - } - if (var == DVM_PARALLEL_ON_DIR || var == ACC_REGION_DIR || var == ACC_END_REGION_DIR || var == DVM_EXIT_INTERVAL_DIR) - return (SgExecutableStatement *)pt; - if (var == DVM_INTERVAL_DIR) - return isSgExecutableStatement(pt->lexNext()); - if (var == DVM_ENDINTERVAL_DIR) - return isSgExecutableStatement(pt->lexPrev()); - if (var == DVM_BARRIER_DIR) - return (SgExecutableStatement *)pt; - if (var == DVM_INHERIT_DIR) - return NULL; - if (var == DVM_INHERIT_DIR || var == DVM_ALIGN_DIR || var == DVM_DYNAMIC_DIR || - var == DVM_DISTRIBUTE_DIR || var == DVM_VAR_DECL || var == DVM_SHADOW_DIR || - var == DVM_HEAP_DIR || var == DVM_CONSISTENT_DIR || var == DVM_POINTER_DIR) - return NULL; -#endif + if (var == DVM_PARALLEL_ON_DIR || var == ACC_REGION_DIR || var == ACC_END_REGION_DIR || var == DVM_EXIT_INTERVAL_DIR) + return (SgExecutableStatement*)pt; + if (var == DVM_INTERVAL_DIR) + return isSgExecutableStatement(pt->lexNext()); + if (var == DVM_ENDINTERVAL_DIR) + return isSgExecutableStatement(pt->lexPrev()); + if (var == DVM_BARRIER_DIR) + return (SgExecutableStatement*)pt; + if (var == DVM_INHERIT_DIR) + return NULL; + if (var == DVM_INHERIT_DIR || var == DVM_ALIGN_DIR || var == DVM_DYNAMIC_DIR || + var == DVM_DISTRIBUTE_DIR || var == DVM_VAR_DECL || var == DVM_SHADOW_DIR || + var == DVM_HEAP_DIR || var == DVM_CONSISTENT_DIR || var == DVM_POINTER_DIR) + return NULL; + } return NULL; } } diff --git a/dvm/fdvm/trunk/Sage/lib/include/libSage++.h b/dvm/fdvm/trunk/Sage/lib/include/libSage++.h index 432ad00..97d6425 100644 --- a/dvm/fdvm/trunk/Sage/lib/include/libSage++.h +++ b/dvm/fdvm/trunk/Sage/lib/include/libSage++.h @@ -126,6 +126,7 @@ private: SgProject *project; bool unparseIgnore; + static bool sapfor_regime; static std::string currProcessFile; static int currProcessLine; static bool deprecatedCheck; @@ -335,6 +336,9 @@ public: static void setCurrProcessLine(const int line) { currProcessLine = line; } static std::string getCurrProcessFile() { return currProcessFile; } static int getCurrProcessLine() { return currProcessLine; } + + static void setSapforRegime() { sapfor_regime = true; } + static bool isSapforRegime() { return sapfor_regime; } }; class SgExpression diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp index 746f53b..aa52cef 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ConvertFiles.cpp @@ -8,24 +8,489 @@ #include "../Utils/errors.h" #include "../Utils/SgUtils.h" +#include "../Utils/utils.h" + +#include "../VerificationCode/verifications.h" #include "ConvertFiles.h" +#include "calls.h" using namespace std; -extern int convert_file(int argc, char* argv[], const char* proj_name); +extern int v_print; +extern int warn_all; +extern int unparse_functions; +extern int opt_base; +extern int opt_loop_range; + +extern graph_node* cur_node; +extern graph_node* node_list; + +extern "C" int out_free_form; +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; + +static int convertFile(int argc, char* argv[], const string& fileToConv, + 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"; + char* fout_name_cuf; /*ACC*/ + char* fout_name_C_cu; /*ACC*/ + char* fout_name_info_C; /*ACC*/ + + char* source_name; + int level, hpf, openmp, isz, dvm_type_size; + int a_mode = 0; + + initialize(); + + openmp = hpf = 0; dvm_type_size = 0; + + argv++; + while ((argc > 1) && (*argv)[0] == '-') + { + if ((*argv)[1] == 'o' && ((*argv)[2] == '\0')) { + fout_name = argv[1]; + argv++; + argc--; + } + else if (!strcmp(argv[0], "-dc")) + check_regim = 1; + else if (!strcmp(argv[0], "-dbif1")) + dbg_if_regim = 1; + else if (!strcmp(argv[0], "-dbif2")) + dbg_if_regim = 2; + else if (!strcmp(argv[0], "-speedL0")) /* for dedugging ACROSS-scheme */ + options.setOn(SPEED_TEST_L0); /*ACC*/ + else if (!strcmp(argv[0], "-speedL1")) /* for dedugging ACROSS-scheme */ + options.setOn(SPEED_TEST_L1); /*ACC*/ + else if (!strcmp(argv[0], "-dmpi")) + deb_mpi = 1; + else if (!strcmp(argv[0], "-dnoind")) + d_no_index = 1; + else if (!strcmp(argv[0], "-dperf")) { + debug_regim = 1; + omp_debug = DPERF; + } + else if (!strcmp(argv[0], "-dvmLoopAnalysisEC")) /*ACC*/ + { + options.setOn(LOOP_ANALYSIS); + options.setOn(OPT_EXP_COMP); + } + else if (!strcmp(argv[0], "-dvmIrregAnalysis")) /*ACC*/ + { + options.setOn(LOOP_ANALYSIS); + options.setOn(OPT_EXP_COMP); + options.setOn(GPU_IRR_ACC); + } + else if (!strcmp(argv[0], "-dvmLoopAnalysis")) /*ACC*/ + options.setOn(LOOP_ANALYSIS); + else if (!strcmp(argv[0], "-dvmPrivateAnalysis")) /*ACC*/ + options.setOn(PRIVATE_ANALYSIS); + else if ((*argv)[1] == 'd') { + switch ((*argv)[2]) { + case '0': level = 0; break; + case '1': level = 1; omp_debug = D1; /*OMP*/ break; + case '2': level = 2; omp_debug = D2; /*OMP*/ break; + case '3': level = 3; omp_debug = D3; /*OMP*/ break; + case '4': level = 4; omp_debug = D4; /*OMP*/ break; + case '5': level = 5; omp_debug = D5; /*OMP*/ break; + /* case '5': level = -1; many_files=1; break;*/ + default: level = -1; + } + if (level > 0) + debug_regim = 1; + if ((*argv)[3] == '\0') + AddToFragmentList(0, 0, level, -1); + else if ((*argv)[3] == ':') + FragmentList(*argv + 4, level, -1); + } + else if ((*argv)[1] == 'e') { + switch ((*argv)[2]) { + case '0': level = 0; break; + case '1': level = 1; break; + case '2': level = 2; break; + case '3': level = 3; break; + case '4': level = 4; break; + case 'm': omp_perf = 1; break; + default: level = -1; + } + if ((*argv)[3] == '\0') + AddToFragmentList(0, 0, -1, level); + else if ((*argv)[3] == ':') + FragmentList(*argv + 4, -1, level); + } + else if (!strcmp(argv[0], "-spf")) + { + fprintf(stderr, "Illegal option -spf \n"); + return 1; + } + else if (!strcmp(argv[0], "-p")) { + only_debug = 0; hpf = 0; + } + else if (!strcmp(argv[0], "-s")) { + only_debug = 1; hpf = 0; + } + else if (!strcmp(argv[0], "-v")) + v_print = 1; + else if (!strcmp(argv[0], "-w")) + warn_all = 1; + else if (!strcmp(argv[0], "-bind0")) + bind_ = 0; + else if (!strcmp(argv[0], "-bind1")) + bind_ = 1; + else if (!strcmp(argv[0], "-t8")) + dvm_type_size = 8; + else if (!strcmp(argv[0], "-t4")) + dvm_type_size = 4; + else if (!strcmp(argv[0], "-r8")) + default_real_size = 8; + else if (!strcmp(argv[0], "-i8")) + default_integer_size = 8; + else if (!strcmp(argv[0], "-hpf") || !strcmp(argv[0], "-hpf1") || !strcmp(argv[0], "-hpf2")) + hpf = 1; + else if (!strcmp(argv[0], "-mp")) { + OMP_program = 1; /*OMP*/ + openmp = 1; + } + //else if (!strcmp(argv[0],"-ta")) + // ACC_program = 1; + else if (!strcmp(argv[0], "-noH")) + ACC_program = 0; + else if (!strcmp(argv[0], "-noCudaType")) /*ACC*/ + undefined_Tcuda = 1; + else if (!strcmp(argv[0], "-noCuda")) + options.setOn(NO_CUDA); /*ACC*/ + else if (!strcmp(argv[0], "-noPureFunc")) + options.setOn(NO_PURE_FUNC); /*ACC*/ + else if (!strcmp(argv[0], "-C_Cuda")) /*ACC*/ + options.setOn(C_CUDA); + else if (!strcmp(argv[0], "-FTN_Cuda") || !strcmp(argv[0], "-F_Cuda")) /*ACC*/ + options.setOff(C_CUDA); + else if (!strcmp(argv[0], "-no_blocks_info") || !strcmp(argv[0], "-noBI")) + options.setOn(NO_BL_INFO); /*ACC*/ + else if (!strcmp(argv[0], "-cacheIdx")) + options.setOff(NO_BL_INFO); /*ACC*/ + else if (!strcmp(argv[0], "-Ohost")) /*ACC*/ + options.setOn(O_HOST); + else if (!strcmp(argv[0], "-noOhost")) /*ACC*/ + options.setOff(O_HOST); + else if (!strcmp(argv[0], "-Opl2")) /*ACC*/ + { + parloop_by_handler = 2; + options.setOn(O_HOST); + options.setOn(O_PL2); + // options.setOn(NO_CUDA); + } + else if (!strcmp(argv[0], "-Opl")) /*ACC*/ + { + parloop_by_handler = 1; + options.setOn(O_PL); + } + else if (!strcmp(argv[0], "-oneThread")) /*ACC*/ + options.setOn(ONE_THREAD); + else if (!strcmp(argv[0], "-noTfm")) /*ACC*/ + options.setOff(AUTO_TFM); + else if (!strcmp(argv[0], "-autoTfm")) /*ACC*/ + options.setOn(AUTO_TFM); + else if (!strcmp(argv[0], "-gpuO0")) /*ACC*/ + options.setOn(GPU_O0); + else if (!strcmp(argv[0], "-gpuO1")) /*ACC*/ + options.setOn(GPU_O1); + else if (!strcmp(argv[0], "-rtc")) /*ACC*/ + options.setOn(RTC); //for NVRTC compilation and execution + else if (!strcmp(argv[0], "-ffo")) + out_free_form = 1; + else if (!strcmp(argv[0], "-upcase")) + out_upper_case = 1; + else if (!strcmp(argv[0], "-noLimitLine")) + out_line_unlimit = 1; + else if (!strcmp(argv[0], "-uniForm")) + { + out_free_form = 1; + out_line_length = 72; + } + else if (!strcmp(argv[0], "-noRemote")) + options.setOn(NO_REMOTE); + else if (!strcmp(argv[0], "-lgstd")) + { + (void)fprintf(stderr, "Illegal option -lgstd \n"); + return 1; + } + else if (!strcmp(argv[0], "-byFunUnparse")) + unparse_functions = 1; + else if (!strncmp(argv[0], "-bufio", 6)) { + if ((*argv)[6] != '\0' && (isz = is_integer_value(*argv + 6))) + IOBufSize = isz; + } + else if (!strncmp(argv[0], "-bufUnparser", 12)) { + if ((*argv)[12] != '\0' && (isz = is_integer_value(*argv + 12))) + UnparserBufSize = isz * 1024 * 1024; + } + else if (!strcmp(argv[0], "-ioRTS")) + options.setOn(IO_RTS); + else if (!strcmp(argv[0], "-read_all")) + options.setOn(READ_ALL); + else if (!strcmp(argv[0], "-Obase")) + opt_base = 1; + else if (!strcmp(argv[0], "-Oloop_range")) + opt_loop_range = 1; + else if ((*argv)[1] == 'H') { + if ((*argv)[2] == 's' && (*argv)[3] == 'h' && (*argv)[4] == 'w') { + if ((*argv)[5] != '\0' && (all_sh_width = is_integer_value(*argv + 5))) + ; + } + else if (!strcmp(*argv + 2, "nora")) + no_rma = 1; + else if (!strcmp(*argv + 2, "oneq")) + one_inquiry = 1; + else if (!strcmp(*argv + 2, "onlyl")) + only_local = 1; + } + else if (!strncmp(argv[0], "-collapse", 9)) + if ((*argv)[9] != '\0' && (collapse_loop_count = is_integer_value(*argv + 9))); + argc--; + argv++; + } + + // Check options combinations + options.checkCombinations(); + + // definition of DvmType size: len_DvmType + // len_DvmType==0, if DvmType-size == default_integer_size == 4 + if (bind_ == 1) + len_DvmType = 8; //sizeof(long) == 8 + if (dvm_type_size) + len_DvmType = dvm_type_size; + if (len_DvmType == 0 && default_integer_size == 8) + len_DvmType = 4; + + if (ACC_program && debug_regim && !only_debug) + { + fprintf(stderr, "Warning: -noH option is set to debug mode\n"); + ACC_program = 0; + } + if (parloop_by_handler > 0 && debug_regim) + { + fprintf(stderr, "Warning: -Opl/Opl2 option is ignored in debug mode\n"); + parloop_by_handler = 0; + options.setOff(O_PL); + options.setOff(O_PL2); + } + + if (openmp && ACC_program) + { + fprintf(stderr, "Warning: -noH option is set to -mp mode\n"); + ACC_program = 0; + } + if (parloop_by_handler == 2 && !options.isOn(O_HOST)) + { + fprintf(stderr, "Warning: -Ohost option is set to -Opl2 mode\n"); + options.setOn(O_HOST); + } + if (out_free_form == 1 && out_line_length == 72 && out_line_unlimit == 1) + { + fprintf(stderr, "Warning: -noLimitLine and -uniForm options are incompatible; -noLimitLine option is ignored\n"); + out_line_unlimit = 0; + } + + if (v_print) + (void)fprintf(stderr, "<<<<< Translating >>>>>\n"); + + //------------------------------------------------------------------------------ + + string proj_name = "fdvm__"; + static int id = 0; + + vector filesList; + filesList.push_back((char*)fileToConv.c_str()); + + SgProject project((proj_name + to_string(id++)).c_str(), filesList.data(), filesList.size()); + + shiftLines(&project, false); + for (int z = 0; z < project.numberOfFiles(); ++z) { + vector tmp; + removeExecutableFromModuleDeclaration(&(project.file(z)), filesInProj, tmp); + } + + SgFile* file; + addNumberOfFileToAttribute(&project); + + //---------------------------- + 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), " "); + fout_name = doOutFileName(file->filename()); //project.fileName(id); + + if (fout_name && source_name && !strcmp(source_name, fout_name)) + { + fprintf(stderr, "Output file has the same name as source file\n"); + return 1; + } + + 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(); + initLibNames(); //for every file + InitDVM(file); //for every file + current_file = file; // global variable (used in SgTypeComplex) + max_lab = getLastLabelId(); + + if (dbg_if_regim) + GetLabel(); + + TranslateFileDVM(file); + + if (err_cnt) { + fprintf(stderr, "%d error(s)\n", err_cnt); + return 1; + } + + if (!fout_name) { + file->unparsestdout(); + return 0; + } + + //writing result of converting into file + if ((fout = fopen(fout_name, "w")) == NULL) { + fprintf(stderr, "Can't open file %s for write\n", fout_name); + return 1; + } + + if (GeneratedForCuda()) /*ACC*/ + { + if ((fout_C_cu = fopen(fout_name_C_cu, "w")) == NULL) { + fprintf(stderr, "Can't open file %s for write\n", fout_name_C_cu); + return 1; + } + + if (!options.isOn(C_CUDA)) { + if ((fout_cuf = fopen(fout_name_cuf, "w")) == NULL) { + fprintf(stderr, "Can't open file %s for write\n", fout_name_cuf); + return 1; + } + } + + if ((fout_info = fopen(fout_name_info_C, "w")) == NULL) { + fprintf(stderr, "Can't open file %s for write\n", fout_name_info_C); + return 1; + } + } + + + if (v_print) + fprintf(stderr, "<<<<< Unparsing %s >>>>>\n", fout_name); + + if (mod_gpu) /*ACC*/ + UnparseTo_CufAndCu_Files(file, fout_cuf, fout_C_cu, fout_info); + + const string fileN = file->filename(); + hideUnnecessary(file, fileN, moduleDeclsInFiles, true); + + if (unparse_functions) + UnparseFunctionsOfFile(file, fout); + else if (UnparserBufSize) + file->unparseS(fout, UnparserBufSize); + else + file->unparse(fout); + + if ((fclose(fout)) < 0) { + fprintf(stderr, "Could not close %s\n", fout_name); + return 1; + } + + if (GeneratedForCuda()) { + if ((fclose(fout_C_cu)) < 0) { + fprintf(stderr, "Could not close %s\n", fout_name_C_cu); + return 1; + } + + if (!options.isOn(C_CUDA)) { + if ((fclose(fout_cuf)) < 0) { + fprintf(stderr, "Could not close %s\n", fout_name_cuf); + return 1; + } + } + + if ((fclose(fout_info)) < 0) { + fprintf(stderr, "Could not close %s\n", fout_name_info_C); + return 1; + } + } + + } + + if (v_print) + fprintf(stderr, "\n***** Done *****\n"); + return 0; +} + +static void preprocess(const vector& files, map>& moduleUsesByFile, map& moduleDecls) +{ + vector filesList; + for (int z = 0; z < files.size(); ++z) + filesList.push_back((char*)files[z].c_str()); + + SgProject tmpProj("preproc", filesList.data(), files.size()); + + for (int z = 0; z < tmpProj.numberOfFiles(); ++z) + { + SgFile* file = &(tmpProj.file(z)); + fillModuleUse(file, moduleUsesByFile, moduleDecls); + } + + InitializeTable(); +} void convertFiles(int argc, char* argv[], const char* proj_name) { vector args_v; for (int z = 0; z < argc; ++z) args_v.push_back(argv[z]); - - args_v.push_back("-a"); - args_v.push_back((char*)proj_name); - - args_v.push_back(NULL); - int code = convert_file(args_v.size() - 1, args_v.data(), proj_name); - exit(code); + const string fileText = readFileToStr(proj_name); + vector files; + set filesinSet; + splitString(fileText, '\n', files); + + for (auto& file : files) + filesinSet.insert(file); + + map> moduleUsesByFile; + map moduleDecls; + set moduleDeclsInFiles; + + preprocess(files, moduleUsesByFile, moduleDecls); + for (auto& elem : moduleDecls) + moduleDeclsInFiles.insert(elem.second); + + int codes = 0; + for (auto& file : files) + { + codes += convertFile(args_v.size() - 1, args_v.data(), file, filesinSet, moduleDeclsInFiles); + + cur_node = node_list = NULL; + InitializeTable(); + } + + if (codes == 0) + printf("Convertation was completed successfully\n"); + exit(0); } \ No newline at end of file diff --git a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp index 198e7bd..cfb469e 100644 --- a/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp +++ b/sapfor/experts/Sapfor_2017/_src/ProjectManipulation/ParseFiles.cpp @@ -186,6 +186,10 @@ static set applyModuleDeclsForFile(FileInfo *forFile, const mapfileName, data); + /*static string tmp = "tmp__"; + static int id = 0; + writeFileFromStr(tmp + to_string(id++) + ".ftn", data);*/ + forFile->includesAdded = included.size(); forFile->includes = included; @@ -275,7 +279,7 @@ static vector parseList(vector& listOfProject, } FILE* depPath = fopen(elem.outDepPath.c_str(), "r"); - if (depPath && !isFromConsole) + if (depPath && !isFromConsole && !needToIncludeForInline) { fclose(depPath); if (elem.error <= 0) @@ -302,7 +306,7 @@ static vector parseList(vector& listOfProject, StdCapture::BeginCapture(); if (needToInclude) filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline); - else if (needToIncludeForInline) // TODO for modules + else if (needToIncludeForInline) filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline); int retCode = parse_file(optSplited.size(), toParse, "dvm.proj"); @@ -521,7 +525,7 @@ static void parseFiles(int& iters, vector& errors, vector& lis { int changed = 0; int lastChanged = 0; - const string projName = "tmp"; + const string projName = (parseForInlining == 0) ? "tmp" : "tmp_inl"; do { @@ -635,11 +639,7 @@ static int parseFiles(vector& errors, vector& listOfProject, v filesCompilationOrder = finalyzeParsing(listOfProject, mapModuleDeps, moduleDelc); if (parseForInlining) - { - for (auto& file : listOfProject) - file.outDepPath = ""; parseFiles(iters, errors, listOfProject, mapModuleDeps, moduleDelc, modDirectOrder, true, isFromConsole); - } } catch (int err) { @@ -756,6 +756,9 @@ void parseFiles(int argc, char** argv) } vector filesCompilationOrder; + if (isInline) + printf(" run parsing for inlining\n"); + int rethrow = parseFiles(errors, listOfProject, filesCompilationOrder, isInline, true); if (rethrow == 0) { diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp index a395ec4..2adeab4 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp @@ -271,6 +271,41 @@ static int reverseVar(SgStatement* st) static map>> insertedIncludes; static set genVersionDone; +set hideUnnecessary(SgFile* file, const string& fileN, const set& moduleDeclsInFiles, bool dontReplaceIncludes) +{ + set changedVars; + for (SgStatement* st = file->firstStatement(); st; st = st->lexNext()) + { + if (dontReplaceIncludes == false) + { + if (st->fileName() != fileN || st->getUnparseIgnore()) + if (st->variant() > 0) + changedVars.insert(reverseVar(st)); + } + else + { + if (st->getUnparseIgnore()) + { + if (st->variant() > 0) + changedVars.insert(reverseVar(st)); + } + else if (st->fileName() != fileN) + { + if (st->variant() == MODULE_STMT && moduleDeclsInFiles.find(st->fileName()) != moduleDeclsInFiles.end()) + { + for (auto mSt = st; mSt != st->lastNodeOfStmt(); mSt = mSt->lexNext()) + changedVars.insert(reverseVar(mSt)); + + st = st->lastNodeOfStmt(); + changedVars.insert(reverseVar(st)); + } + } + } + } + + return changedVars; +} + //TODO: read includes and find last lines, all included files string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const char *fout, set &allIncludeFiles, bool outFree, @@ -573,38 +608,9 @@ string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const ch } } - - set changedVars; //remove unnecessary //XXX: use Sage hack!! - for (SgStatement* st = file->firstStatement(); st; st = st->lexNext()) - { - if (dontReplaceIncludes == false) - { - if (st->fileName() != fileN || st->getUnparseIgnore()) - if (st->variant() > 0) - changedVars.insert(reverseVar(st)); - } - else - { - if (st->getUnparseIgnore()) - { - if (st->variant() > 0) - changedVars.insert(reverseVar(st)); - } - else if (st->fileName() != fileN) - { - if (st->variant() == MODULE_STMT && moduleDeclsInFiles.find(st->fileName()) != moduleDeclsInFiles.end()) - { - for (auto mSt = st; mSt != st->lastNodeOfStmt(); mSt = mSt->lexNext()) - changedVars.insert(reverseVar(mSt)); - - st = st->lastNodeOfStmt(); - changedVars.insert(reverseVar(st)); - } - } - } - } + set changedVars = hideUnnecessary(file, fileN, moduleDeclsInFiles, dontReplaceIncludes); set checkPositions; for (auto& elem : includeFiles) @@ -3069,6 +3075,56 @@ set getAllVariables(SgStatement* stFrom, SgStatement* stTo, const set& v template set getAllVariables(SgStatement* stFrom, SgStatement* stTo, const set& variants); template set getAllVariables(SgStatement* stFrom, SgStatement* stTo, const set& variants); +void shiftLines(SgProject* project, bool print) +{ + map shifts; + //shift lines if modules included + const string shiftInfo = "!SPF NUM FILES"; + for (int z = 0; z < project->numberOfFiles(); ++z) + { + SgFile* file = &(project->file(z)); + const string fileN = file->filename(); + + int shiftN = 0; + for (auto st = file->firstStatement(); st; st = st->lexNext()) + { + if (st->comments()) + { + string comms = st->comments(); + auto it = comms.find(shiftInfo); + if (it != string::npos) + { + it += shiftInfo.size() + 1; + + if (sscanf(comms.c_str() + it, "%d", &shiftN) == -1) + { + __spf_print(print, "comm %s, shifted %s\n", comms.c_str(), comms.c_str() + it); + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + } + break; + } + } + } + + if (shiftN == 0) + continue; + shifts[fileN] = shiftN + 1; // with comment !SPF NUM FILES + __spf_print(print, " shift by %d for %s\n", shiftN, fileN.c_str()); + } + + for (int z = 0; z < project->numberOfFiles(); ++z) + { + SgFile* file = &(project->file(z)); + for (SgStatement* st = file->firstStatement()->lexNext(); st; st = st->lexNext()) + { + string currF = st->fileName(); + auto it = shifts.find(currF); + if (it != shifts.end() && file->filename() == currF) + st->setlineNumber(st->lineNumber() - it->second); + } + } +} + SgProject* createProject(const char* proj_name, vector& parallelRegions, vector& subs_parallelRegions, @@ -3078,6 +3134,7 @@ SgProject* createProject(const char* proj_name, map& moduleDecls, map>>& exctactedModuleStats) { + Statement::setSapforRegime(); Statement::deactiveConsistentchecker(); sortFilesBySize(proj_name); SgProject* project = new SgProject(proj_name); @@ -3245,53 +3302,9 @@ SgProject* createProject(const char* proj_name, } } filterModuleUse(moduleUsesByFile, moduleDecls); - - map shifts; - //shiftLines if modules included - const string shiftInfo = "!SPF NUM FILES"; - for (int z = 0; z < project->numberOfFiles(); ++z) - { - SgFile* file = &(project->file(z)); - const string fileN = file->filename(); - - int shiftN = 0; - for (auto st = file->firstStatement(); st; st = st->lexNext()) - { - if (st->comments()) - { - string comms = st->comments(); - auto it = comms.find(shiftInfo); - if (it != string::npos) - { - it += shiftInfo.size() + 1; - if (sscanf(comms.c_str() + it, "%d", &shiftN) == -1) - { - __spf_print(1, "comm %s, shifted %s\n", comms.c_str(), comms.c_str() + it); - printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - } - break; - } - } - } - - if (shiftN == 0) - continue; - shifts[fileN] = shiftN + 1; // with comment !SPF NUM FILES - __spf_print(1, " shift by %d for %s\n", shiftN, fileN.c_str()); - } - - for (int z = 0; z < project->numberOfFiles(); ++z) - { - SgFile* file = &(project->file(z)); - for (SgStatement* st = file->firstStatement()->lexNext(); st; st = st->lexNext()) - { - string currF = st->fileName(); - auto it = shifts.find(currF); - if (it != shifts.end() && file->filename() == currF) - st->setlineNumber(st->lineNumber() - it->second); - } - } + //shift lines if modules included + shiftLines(project); map> functions; for (int z = 0; z < project->numberOfFiles(); ++z) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h index 0222d18..176a5cf 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h @@ -11,6 +11,7 @@ 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); @@ -107,3 +108,5 @@ void removeSpecialCommentsFromProject(SgFile* file); void getMaxMinBlockDistribution(SgFile* file, std::pair& min_max); void addPrivatesToArraysFromGUI(SgFile* file, const std::map, std::pair>& declaredArrays, const std::map& distrStateFromGUI); + +void shiftLines(SgProject* project, bool print = true); \ 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 7f5b76e..cab6d43 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 "2282" +#define VERSION_SPF "2285"