13 Commits

12 changed files with 442 additions and 153 deletions

View File

@@ -2561,7 +2561,7 @@ void ACC_ParallelLoopEnd(SgStatement *pardo)
if (!WithAcrossClause()) if (!WithAcrossClause())
Create_Host_Loop_Subroutine_Main(hostproc_symb); Create_Host_Loop_Subroutine_Main(hostproc_symb);
else else
{ {
Create_Host_Across_Loop_Subroutine(hostproc_symb); Create_Host_Across_Loop_Subroutine(hostproc_symb);
first_do_par->extractStmt(); first_do_par->extractStmt();
} }
@@ -4572,6 +4572,15 @@ symb_list *isInSymbList(SgSymbol *s, symb_list *slist)
return(NULL); return(NULL);
} }
symb_list *isInSymbListByChar(SgSymbol *s, symb_list *slist)
{
symb_list *sl;
for (sl = slist; sl; sl = sl->next)
if (!strcmp(sl->symb->identifier(), s->identifier()))
return(sl);
return(NULL);
}
int ListElemNumber(SgExpression *list) int ListElemNumber(SgExpression *list)
{ {
SgExpression *l; SgExpression *l;
@@ -5208,7 +5217,6 @@ int Create_New_File(char *file_name, SgFile *file, char *fout_name)
char *new_file_name, *dep_file_name; char *new_file_name, *dep_file_name;
int ll; int ll;
// old file // old file
printf(" in 0 Create_New_File\n");
mod_gpu->extractStmt(); mod_gpu->extractStmt();
ll = strlen(file_name) + 1; ll = strlen(file_name) + 1;
dep_file_name = (char *)malloc((unsigned)ll); dep_file_name = (char *)malloc((unsigned)ll);
@@ -5219,13 +5227,10 @@ int Create_New_File(char *file_name, SgFile *file, char *fout_name)
file->saveDepFile(dep_file_name); file->saveDepFile(dep_file_name);
// new file // new file
printf(" in 1 Create_New_File\n");
fcuf = new SgFile(0, "dvm_gpu"); fcuf = new SgFile(0, "dvm_gpu");
fcuf->firstStatement()->insertStmtAfter(*mod_gpu); fcuf->firstStatement()->insertStmtAfter(*mod_gpu);
fcuf->saveDepFile("dvm_gpu.dep"); fcuf->saveDepFile("dvm_gpu.dep");
printf(" in 2 Create_New_File\n");
//fcuf->unparsestdout();
new_file_name = (char *)malloc((unsigned)(strlen(file_name) + 10)); new_file_name = (char *)malloc((unsigned)(strlen(file_name) + 10));
sprintf(new_file_name, "dvm_gpu_%s", fout_name); sprintf(new_file_name, "dvm_gpu_%s", fout_name);
@@ -5236,7 +5241,7 @@ int Create_New_File(char *file_name, SgFile *file, char *fout_name)
} }
fcuf->unparse(fout); fcuf->unparse(fout);
fclose(fout); fclose(fout);
printf(" in 3 Create_New_File \n");
return 0; return 0;
} }
@@ -6106,7 +6111,7 @@ SgStatement *Create_Host_Loop_Subroutine(SgSymbol *sHostProc, int dependency)
SgExpression *ev, *ered, *er, *red; SgExpression *ev, *ered, *er, *red;
SgSymbol *loc_var; SgSymbol *loc_var;
reduction_operation_list *rl; reduction_operation_list *rl;
red = TranslateReductionToOpenmp(&red_list->copy()); /* OpenMP */ red = TranslateReductionToOpenmp(&red_list->copy()); /* OpenMP */
if (red != NULL) parallellist->append(*red); /* OpenMP */ if (red != NULL) parallellist->append(*red); /* OpenMP */
else addopenmp = 0; /* OpenMP */ else addopenmp = 0; /* OpenMP */
@@ -8090,7 +8095,6 @@ void CreateGPUModule()
KernelWorkSymbols(); KernelWorkSymbols();
return; return;
} }
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
// create CUDA kernel // create CUDA kernel
@@ -8212,7 +8216,8 @@ SgStatement *CreateLoopKernel(SgSymbol *skernel, SgType *indexTypeInKernel)
// inserting IMPLICIT NONE // inserting IMPLICIT NONE
if (!options.isOn(C_CUDA)) // Fortran-Cuda if (!options.isOn(C_CUDA)) // Fortran-Cuda
kernel_st->insertStmtAfter(*new SgStatement(IMPL_DECL), *kernel_st); kernel_st->insertStmtAfter(*new SgStatement(IMPL_DECL), *kernel_st);
if (options.isOn(C_CUDA))
RenamingCudaFunctionVariables(kernel_st, skernel, 1); RenamingCudaFunctionVariables(kernel_st, skernel, 1);
for_kernel = 0; for_kernel = 0;
@@ -8600,6 +8605,9 @@ SgStatement *CreateLoopKernel(SgSymbol *skernel, AnalyzeReturnGpuO1 &infoGpuO1,
// inserting IMPLICIT NONE // inserting IMPLICIT NONE
if (!options.isOn(C_CUDA)) // Fortran-Cuda if (!options.isOn(C_CUDA)) // Fortran-Cuda
kernel_st->insertStmtAfter(*new SgStatement(IMPL_DECL), *kernel_st); kernel_st->insertStmtAfter(*new SgStatement(IMPL_DECL), *kernel_st);
if (options.isOn(C_CUDA))
RenamingCudaFunctionVariables(kernel_st, skernel, 1);
for_kernel = 0; for_kernel = 0;
@@ -8802,7 +8810,7 @@ SgStatement * CreateKernel_ForSequence(SgSymbol *kernel_symb, SgStatement *first
if (options.isOn(C_CUDA)) if (options.isOn(C_CUDA))
{ {
swapDimentionsInprivateList(); swapDimentionsInprivateList();
std::vector < std::stack < SgStatement*> > zero = std::vector < std::stack < SgStatement*> >(0); std::vector < std::stack < SgStatement*> > zero = std::vector < std::stack < SgStatement*> >(0);
Translate_Fortran_To_C(kernel_st, kernel_st->lastNodeOfStmt(), zero, 0); Translate_Fortran_To_C(kernel_st, kernel_st->lastNodeOfStmt(), zero, 0);
} }
@@ -8823,7 +8831,8 @@ SgStatement * CreateKernel_ForSequence(SgSymbol *kernel_symb, SgStatement *first
if (!options.isOn(C_CUDA)) // Fortran-Cuda if (!options.isOn(C_CUDA)) // Fortran-Cuda
// inserting IMPLICIT NONE // inserting IMPLICIT NONE
kernel_st->insertStmtAfter(*new SgStatement(IMPL_DECL), *kernel_st); kernel_st->insertStmtAfter(*new SgStatement(IMPL_DECL), *kernel_st);
if (options.isOn(C_CUDA))
RenamingCudaFunctionVariables(kernel_st, kernel_symb, 1); RenamingCudaFunctionVariables(kernel_st, kernel_symb, 1);
return(kernel_st); return(kernel_st);
} }
@@ -13401,9 +13410,9 @@ SgStatement *Create_C_Adapter_Function(SgSymbol *sadapter)
SgFunctionCallExp *sizeofL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof")); SgFunctionCallExp *sizeofL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof"));
SgFunctionCallExp *sizeofLL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof")); SgFunctionCallExp *sizeofLL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof"));
SgFunctionCallExp *sizeofI = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof")); SgFunctionCallExp *sizeofI = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof"));
sizeofL->addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long"))); sizeofL->addArg(*new SgKeywordValExp("long")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long")));
sizeofLL->addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long long"))); sizeofLL->addArg(*new SgKeywordValExp("long long")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long long")));
sizeofI->addArg(*new SgKeywordValExp("int")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "int"))); sizeofI->addArg(*new SgKeywordValExp("int")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "int")));
stmt = new SgIfStmt(SgEqOp(*new SgVarRefExp(s_idxTypeInKernel), *new SgVarRefExp(*new SgSymbol(VARIABLE_NAME, "rt_LONG"))) stmt = new SgIfStmt(SgEqOp(*new SgVarRefExp(s_idxTypeInKernel), *new SgVarRefExp(*new SgSymbol(VARIABLE_NAME, "rt_LONG")))
@@ -13698,6 +13707,9 @@ SgStatement *Create_C_Adapter_Function(SgSymbol *sadapter)
if (red_list) if (red_list)
InsertFinishReductionCalls(st_end, s_loop_ref, s_red_num); InsertFinishReductionCalls(st_end, s_loop_ref, s_red_num);
} }
if (options.isOn(C_CUDA))
RenamingCudaFunctionVariables(st_hedr, s_loop_ref, 0);
return(st_hedr); return(st_hedr);
} }
@@ -13887,9 +13899,9 @@ SgStatement *Create_C_Adapter_Function_For_Sequence(SgSymbol *sadapter, SgStatem
SgFunctionCallExp *sizeofL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof")); SgFunctionCallExp *sizeofL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof"));
SgFunctionCallExp *sizeofLL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof")); SgFunctionCallExp *sizeofLL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof"));
SgFunctionCallExp *sizeofI = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof")); SgFunctionCallExp *sizeofI = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof"));
sizeofL->addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long"))); sizeofL->addArg(*new SgKeywordValExp("long")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long")));
sizeofLL->addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long long"))); sizeofLL->addArg(*new SgKeywordValExp("long long")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long long")));
sizeofI->addArg(*new SgKeywordValExp("int")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "int"))); sizeofI->addArg(*new SgKeywordValExp("int")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "int")));
stmt = new SgIfStmt(SgEqOp(*new SgVarRefExp(s_idxTypeInKernel), *new SgVarRefExp(*new SgSymbol(VARIABLE_NAME, "rt_LONG"))) stmt = new SgIfStmt(SgEqOp(*new SgVarRefExp(s_idxTypeInKernel), *new SgVarRefExp(*new SgSymbol(VARIABLE_NAME, "rt_LONG")))

View File

@@ -637,7 +637,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter)
vector<SgStatement*> cuda_kernel; vector<SgStatement*> cuda_kernel;
SgExpression *fe, *ae, *el, *arg_list; SgExpression *fe, *ae, *el, *arg_list;
SgType *typ; SgType *typ;
SgSymbol *s_loop_ref, *sarg, *s; SgSymbol *s_loop_ref, *sarg, *s, *current_symbol;
symb_list *sl; symb_list *sl;
vector<SgSymbol *> argsForVariantFunction; vector<SgSymbol *> argsForVariantFunction;
@@ -650,6 +650,7 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter)
mywarn(" end: getAllVars"); mywarn(" end: getAllVars");
cuda_kernel.resize(countKernels); cuda_kernel.resize(countKernels);
current_symbol = SymbMapping(current_file->filept->cur_symb); //CUR_FILE_CUR_SYMB();
if (options.isOn(ONE_THREAD)) if (options.isOn(ONE_THREAD))
{ {
@@ -1111,7 +1112,9 @@ ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter)
mywarn(" end: create IF BLOCK "); mywarn(" end: create IF BLOCK ");
} }
if (options.isOn(C_CUDA))
RenamingCudaFunctionVariables(st_hedr, s_loop_ref, 0); //(st_hedr, current_symbol->next(), 0);
return NULL; return NULL;
} }
@@ -1464,9 +1467,9 @@ vector<ArgsForKernel> Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadap
SgFunctionCallExp *sizeofLL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof")); SgFunctionCallExp *sizeofLL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof"));
SgFunctionCallExp *sizeofI = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof")); SgFunctionCallExp *sizeofI = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof"));
sizeofL->addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long"))); sizeofL->addArg(*new SgKeywordValExp("long")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long")));
sizeofLL->addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long long"))); sizeofLL->addArg(*new SgKeywordValExp("long long")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long long")));
sizeofI->addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "int"))); sizeofI->addArg(*new SgKeywordValExp("int")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "int")));
stmt = new SgIfStmt(SgEqOp(*new SgVarRefExp(idxTypeInKernel), *new SgVarRefExp(*new SgSymbol(VARIABLE_NAME, "rt_LONG"))) stmt = new SgIfStmt(SgEqOp(*new SgVarRefExp(idxTypeInKernel), *new SgVarRefExp(*new SgSymbol(VARIABLE_NAME, "rt_LONG")))
&& &&
@@ -1589,6 +1592,8 @@ vector<ArgsForKernel> Create_C_Adapter_Function_Across_OneThread(SgSymbol *sadap
delete[]reduction_ptr; delete[]reduction_ptr;
mywarn(" end Adapter Function"); mywarn(" end Adapter Function");
if (options.isOn(C_CUDA))
RenamingCudaFunctionVariables(st_hedr, s_loop_ref, 0);
return argsKernel; return argsKernel;
} }
@@ -1638,8 +1643,8 @@ static inline void insertReductionArgs(SgSymbol **reduction_ptr, SgSymbol **redu
//TODO!! //TODO!!
if (rsl->locvar) //MAXLOC,MINLOC if (rsl->locvar) //MAXLOC,MINLOC
{ {
for (int i = 0; i < rsl->number; ++i) for (int k = 0; k < rsl->number; ++k)
funcCallKernel->addArg(*new SgArrayRefExp(*s, *new SgValueExp(i))); funcCallKernel->addArg(*new SgArrayRefExp(*reduction_loc_symb[i], *new SgValueExp(k)));
s = s->next(); s = s->next();
e = new SgCastExp(*C_PointerType(options.isOn(C_CUDA) ? C_Type(rsl->locvar->type()) : new SgDescriptType(*SgTypeChar(), BIT_SIGNED)), *new SgVarRefExp(s)); e = new SgCastExp(*C_PointerType(options.isOn(C_CUDA) ? C_Type(rsl->locvar->type()) : new SgDescriptType(*SgTypeChar(), BIT_SIGNED)), *new SgVarRefExp(s));
funcCallKernel->addArg(*e); funcCallKernel->addArg(*e);
@@ -1943,6 +1948,7 @@ vector<ArgsForKernel> Create_C_Adapter_Function_Across_variants(SgSymbol *sadapt
SYMB_SCOPE(s->thesymb) = st_hedr->thebif; SYMB_SCOPE(s->thesymb) = st_hedr->thebif;
stmt = makeSymbolDeclaration(s); stmt = makeSymbolDeclaration(s);
st_hedr->insertStmtAfter(*stmt, *st_hedr); st_hedr->insertStmtAfter(*stmt, *st_hedr);
reduction_loc_symb[ln] = s_loc_var;
s = sgrid_loc = GridSymbolForRedInAdapter(s, st_hedr); s = sgrid_loc = GridSymbolForRedInAdapter(s, st_hedr);
stmt = makeSymbolDeclaration(s); stmt = makeSymbolDeclaration(s);
@@ -2395,9 +2401,9 @@ vector<ArgsForKernel> Create_C_Adapter_Function_Across_variants(SgSymbol *sadapt
SgFunctionCallExp *sizeofLL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof")); SgFunctionCallExp *sizeofLL = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof"));
SgFunctionCallExp *sizeofI = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof")); SgFunctionCallExp *sizeofI = new SgFunctionCallExp(*createNewFunctionSymbol("sizeof"));
sizeofL->addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long"))); sizeofL->addArg(*new SgKeywordValExp("long")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long")));
sizeofLL->addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long long"))); sizeofLL->addArg(*new SgKeywordValExp("long long")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "long long")));
sizeofI->addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "int"))); sizeofI->addArg(*new SgKeywordValExp("int")); //addArg(*new SgVarRefExp(new SgSymbol(VARIABLE_NAME, "int")));
stmt = new SgIfStmt(SgEqOp(*new SgVarRefExp(idxTypeInKernel), *new SgVarRefExp(*new SgSymbol(VARIABLE_NAME, "rt_LONG"))) stmt = new SgIfStmt(SgEqOp(*new SgVarRefExp(idxTypeInKernel), *new SgVarRefExp(*new SgSymbol(VARIABLE_NAME, "rt_LONG")))
&& &&
@@ -3885,6 +3891,8 @@ vector<ArgsForKernel> Create_C_Adapter_Function_Across_variants(SgSymbol *sadapt
delete[]reduction_symb; delete[]reduction_symb;
delete[]num_elems; delete[]num_elems;
mywarn(" end Adapter Function"); mywarn(" end Adapter Function");
if (options.isOn(C_CUDA))
RenamingCudaFunctionVariables(st_hedr, s_loop_ref, 0);
return argsKernel; return argsKernel;
} }
@@ -4197,7 +4205,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel* argsKer, S
SgSymbol *redGrid = new SgSymbol(VARIABLE_NAME, tmp_list->red_grid->identifier()); SgSymbol *redGrid = new SgSymbol(VARIABLE_NAME, tmp_list->red_grid->identifier());
redGrid->setType(*new SgArrayType(*tmp_list->red_grid->type())); redGrid->setType(*new SgArrayType(*tmp_list->red_grid->type()));
st = AssignStatement(*new SgArrayRefExp(*redGrid, *new SgValueExp(0)), *red_expr_ref); st = AssignStatement(*new SgArrayRefExp(*redGrid, *new SgValueExp(0)), red_expr_ref->copy());
if_st->lastExecutable()->insertStmtAfter(*st); if_st->lastExecutable()->insertStmtAfter(*st);
tmp_list = tmp_list->next; tmp_list = tmp_list->next;
} }
@@ -4227,7 +4235,8 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel* argsKer, S
#if debugMode #if debugMode
mywarn(" end: CreateLoopKernelAcross"); mywarn(" end: CreateLoopKernelAcross");
#endif #endif
if (options.isOn(C_CUDA))
RenamingCudaFunctionVariables(kernel_st, skernel, 1);
ACROSS_MOD_IN_KERNEL = 0; ACROSS_MOD_IN_KERNEL = 0;
return kernel_st; return kernel_st;
} }
@@ -5353,7 +5362,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel* argsKer, i
//get info of arrays in private and locvar lists //get info of arrays in private and locvar lists
swapDimentionsInprivateList(); swapDimentionsInprivateList();
if (argsKer->symb.size() == 1 && options.isOn(GPU_O0)) if (argsKer->symb.size() == 1 && options.isOn(GPU_O0))
{ {
Translate_Fortran_To_C(mainFor->lexPrev()->controlParent()); Translate_Fortran_To_C(mainFor->lexPrev()->controlParent());
Translate_Fortran_To_C(mainFor, mainFor->lastNodeOfStmt(), copyOfBody, 0); //countOfCopies Translate_Fortran_To_C(mainFor, mainFor->lastNodeOfStmt(), copyOfBody, 0); //countOfCopies
} }
@@ -5515,7 +5524,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel* argsKer, i
if (tmp_list->redvar_size == 0) if (tmp_list->redvar_size == 0)
{ {
funcCall->addArg(*new SgArrayRefExp(*redGrid, *new SgVarRefExp(*tid))); funcCall->addArg(*new SgArrayRefExp(*redGrid, *new SgVarRefExp(*tid)));
funcCall->addArg(*red_expr_ref); funcCall->addArg(*new SgVarRefExp(*red_expr_ref->symbol()));
st = AssignStatement(*new SgArrayRefExp(*redGrid, *new SgVarRefExp(*tid)), *funcCall); st = AssignStatement(*new SgArrayRefExp(*redGrid, *new SgVarRefExp(*tid)), *funcCall);
} }
else if (tmp_list->redvar_size > 0 && options.isOn(C_CUDA)) //TODO for Fortran else if (tmp_list->redvar_size > 0 && options.isOn(C_CUDA)) //TODO for Fortran
@@ -5541,7 +5550,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel* argsKer, i
SgSymbol *emin = argsKer->sizeVars[6]; SgSymbol *emin = argsKer->sizeVars[6];
funcCall->addArg(*new SgArrayRefExp(*redGrid, *new SgVarRefExp(*tid) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*emin))); funcCall->addArg(*new SgArrayRefExp(*redGrid, *new SgVarRefExp(*tid) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*emin)));
funcCall->addArg(*red_expr_ref); funcCall->addArg(*new SgVarRefExp(red_expr_ref->symbol()));
st = AssignStatement(*new SgArrayRefExp(*redGrid, *new SgVarRefExp(*tid) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*emin)), *funcCall); st = AssignStatement(*new SgArrayRefExp(*redGrid, *new SgVarRefExp(*tid) + *new SgVarRefExp(*tid1) * *new SgVarRefExp(*emin)), *funcCall);
} }
} }
@@ -5572,7 +5581,7 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel* argsKer, i
SgExpression* red_ref = NULL; SgExpression* red_ref = NULL;
if (tmp_list->redvar_size == 0) if (tmp_list->redvar_size == 0)
red_ref = red_expr_ref; red_ref = &red_expr_ref->copy();
else // TODO else // TODO
red_ref = new SgArrayRefExp(*red_expr_ref->symbol(), *new SgVarRefExp(freeS)); red_ref = new SgArrayRefExp(*red_expr_ref->symbol(), *new SgVarRefExp(freeS));
@@ -5590,12 +5599,12 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel* argsKer, i
e = &SgEqOp(*new SgArrayRefExp(*redGrid, *e1), *red_ref); e = &SgEqOp(*new SgArrayRefExp(*redGrid, *e1), *red_ref);
else if (num == 9 || num == 10) else if (num == 9 || num == 10)
{ {
st = AssignStatement(*new SgArrayRefExp(*redGrid, *e1), *red_expr_ref); st = AssignStatement(*new SgArrayRefExp(*redGrid, *e1), red_expr_ref->copy());
ifSt = new SgIfStmt(*red_expr_ref > *new SgArrayRefExp(*redGrid, *e1), *st); ifSt = new SgIfStmt(red_expr_ref->copy() > *new SgArrayRefExp(*redGrid, *e1), *st);
for (int i = loc_el_num - 1; i >= 0; i--) for (int i = loc_el_num - 1; i >= 0; i--)
{ {
SgSymbol *locGrid = new SgSymbol(VARIABLE_NAME, tmp_list->loc_grid->identifier()); SgSymbol *locGrid = new SgSymbol(VARIABLE_NAME, tmp_list->loc_grid->identifier());
redGrid->setType(*new SgArrayType(*tmp_list->loc_grid->type())); locGrid->setType(*new SgArrayType(*tmp_list->loc_grid->type()));
if (options.isOn(C_CUDA)) if (options.isOn(C_CUDA))
st = AssignStatement(*new SgArrayRefExp(*locGrid, *new SgValueExp(i), *e1), *new SgArrayRefExp(*loc_var_ref->symbol(), *new SgValueExp(i))); st = AssignStatement(*new SgArrayRefExp(*locGrid, *new SgValueExp(i), *e1), *new SgArrayRefExp(*loc_var_ref->symbol(), *new SgValueExp(i)));
@@ -5745,6 +5754,8 @@ SgStatement *CreateLoopKernelAcross(SgSymbol *skernel, ArgsForKernel* argsKer, i
// inserting IMPLICIT NONE // inserting IMPLICIT NONE
if (!options.isOn(C_CUDA)) // Fortran-Cuda if (!options.isOn(C_CUDA)) // Fortran-Cuda
kernel_st->insertStmtAfter(*new SgStatement(IMPL_DECL), *kernel_st); kernel_st->insertStmtAfter(*new SgStatement(IMPL_DECL), *kernel_st);
if (options.isOn(C_CUDA))
RenamingCudaFunctionVariables(kernel_st, skernel, 1);
ACROSS_MOD_IN_KERNEL = 0; ACROSS_MOD_IN_KERNEL = 0;
return kernel_st; return kernel_st;

View File

@@ -172,7 +172,7 @@ static char* getNewCycleVar(const char *oldVar)
{ {
char *str = new char[strlen(oldVar) + 2]; char *str = new char[strlen(oldVar) + 2];
str[0] = '\0'; str[0] = '\0';
strcat(str, "_"); strcat(str, "__");
strcat(str, oldVar); strcat(str, oldVar);
return str; return str;
} }
@@ -3179,7 +3179,7 @@ void initF2C_FunctionCalls()
handlersOfFunction[string("sinh")] = FunctionParam("sinh", 1, &createNewFCall); handlersOfFunction[string("sinh")] = FunctionParam("sinh", 1, &createNewFCall);
handlersOfFunction[string("shifta")] = FunctionParam("shifta", 2, &createNewFCall); handlersOfFunction[string("shifta")] = FunctionParam("shifta", 2, &createNewFCall);
handlersOfFunction[string("shiftl")] = FunctionParam("lshft", 2, &createNewFCall); handlersOfFunction[string("shiftl")] = FunctionParam("lshft", 2, &createNewFCall);
handlersOfFunction[string("shiftr")] = FunctionParam("rshft", 2, &createNewFCall); handlersOfFunction[string("shiftr")] = FunctionParam("shiftr", 2, &createNewFCall);
handlersOfFunction[string("tan")] = FunctionParam("tan", 1, &createNewFCall); handlersOfFunction[string("tan")] = FunctionParam("tan", 1, &createNewFCall);
handlersOfFunction[string("tand")] = FunctionParam("tan", 0, &__sindcosdtand_handler); handlersOfFunction[string("tand")] = FunctionParam("tan", 0, &__sindcosdtand_handler);
handlersOfFunction[string("tanh")] = FunctionParam("tanh", 1, &createNewFCall); handlersOfFunction[string("tanh")] = FunctionParam("tanh", 1, &createNewFCall);
@@ -3391,3 +3391,185 @@ void Translate_Fortran_To_C(SgStatement *firstStmt, SgStatement *lastStmt, vecto
printf("END: CONVERTION OF BODY ON LINE %d\n", number_of_loop_line); printf("END: CONVERTION OF BODY ON LINE %d\n", number_of_loop_line);
#endif #endif
} }
void ChangeSymbolName(SgSymbol *symb)
{
char *name = new char[strlen(symb->identifier())+2];
sprintf(name, "_%s", symb->identifier());
SYMB_IDENT(symb->thesymb) = name;
}
void RenamingNewProcedureVariables(SgSymbol *proc_name)
{
// replacing new procedure names to avoid conflicts with C language keywords and intrinsic function names
SgSymbol *sl;
for(sl = proc_name; sl; sl = sl->next())
switch(sl->variant())
{
case VARIABLE_NAME:
case CONST_NAME:
case FIELD_NAME:
case TYPE_NAME:
case LABEL_VAR:
case COMMON_NAME:
case NAMELIST_NAME:
ChangeSymbolName(sl);
break;
default:
break;
}
}
SgSymbol *hasSameNameAsSource(SgSymbol *symb)
{
symb_list *sl;
if (!symb)
return NULL;
if (sl=isInSymbListByChar(symb, acc_array_list))
return sl->symb;
SgExpression *el;
if (newVars.size() != 0)
{
correctPrivateList(RESTORE);
newVars.clear();
}
for (el = private_list; el; el = el->rhs())
if (!strcmp(el->lhs()->symbol()->identifier(), symb->identifier()))
return el->lhs()->symbol();
if (el=isInUsesListByChar(symb->identifier()))
return el->lhs()->symbol();
for (el = dvm_parallel_dir->expr(2); el; el = el->rhs())
if (!strcmp(el->lhs()->symbol()->identifier(), symb->identifier()))
return el->lhs()->symbol();
reduction_operation_list *rl;
for (rl = red_struct_list; rl; rl = rl->next)
{
if(rl->redvar && !strcmp(rl->redvar->identifier(), symb->identifier()))
return rl->redvar;
if(rl->locvar && !strcmp(rl->locvar->identifier(), symb->identifier()))
return rl->locvar;
}
return NULL;
}
/*
void RenamingCudaFunctionVariables(SgStatement *first, SgStatement *last, SgSymbol *k_symb)
{ // replacing kernel names to avoid conflicts with C language keywords and intrinsic function names
SgSymbol *sl;
for (sl=k_symb->next(); sl; sl=sl->next())
{
SgSymbol *s_symb = hasSameNameAsSource(sl);
if (s_symb)
{
replaceSymbInStmts(first->thebif, last->thebif, s_symb->thesymb, sl->thesymb);
ChangeSymbolName(sl);
}
}
}
void ReplacingVariablesInTranslatedCode(SgStatement *first, SgSymbol *k_symb)
{ // replacing source symbols by kernel symbols of the same name
SgSymbol *sl;
for (sl=k_symb->next(); sl; sl=sl->next())
{
if (sl->scope() != kernel_st || sl->variant() != VARIABLE_NAME)
continue;
SgSymbol *s_symb = hasSameNameAsSource(sl);
if (s_symb)
{ //printf("::: %s id:%d variant:%d\n",sl->identifier(), sl->id(), sl->variant());
//replaceSymbInStmtsSameName(first->thebif, last->thebif, s_symb->thesymb, sl->thesymb);
first->replaceSymbBySymbSameName(s_symb,sl);
}
}
}
void RenamingCudaFunctionVariables(SgStatement *first, SgStatement *first_body, SgSymbol *k_symb, int replace_flag)
{ // replacing kernel names to avoid conflicts with C language keywords and intrinsic function names
if (first_body)
ReplacingVariablesInTranslatedCode(first_body, k_symb);
for (sl=k_symb->next(); sl; sl=sl->next())
{
if (sl->scope() != kernel_st || sl->variant() != VARIABLE_NAME)
continue;
SgSymbol *s_symb = hasSameNameAsSource(sl);
if (s_symb)
{
if (replace_flag)
first->replaceSymbBySymb(s_symb,sl);
//replaceSymbInStmts(first->thebif, last->thebif, s_symb->thesymb, sl->thesymb);
ChangeSymbolName(sl);
}
}
}
int isCudaVariable(SgSymbol *symb)
{
if(!strcmp(symb->identifier(), "blocks")) return 1;
if(!strcmp(symb->identifier(), "threads")) return 1;
if(!strcmp(symb->identifier(), "threadIdx")) return 1;
if(!strcmp(symb->identifier(), "blockIdx")) return 1;
if(!strcmp(symb->identifier(), "blockDim")) return 1;
if(!strcmp(symb->identifier(), "gridDim")) return 1;
return 0;
}
*/
int sameVariableName(SgSymbol *symb1, SgSymbol *symb2)
{
if (!symb1 || !symb2 || (symb1->variant() != VARIABLE_NAME && symb1->variant() != CONST_NAME) || symb2->variant() != VARIABLE_NAME && symb2->variant() != CONST_NAME)
return 0;
if (!strcmp (symb1->identifier(), symb2->identifier()))
return 1;
else
return 0;
}
void replaceSymbolSameNameInExpr(SgExpression *expr, SgSymbol *symb, SgSymbol *s_new)
{
//SgRecordRefExp *re;
if (!expr || !symb || !s_new)
return;
//if (isSgRecordRefExp(expr)) //&& isCudaVariable(re->recordName()))
// replaceSymbolSameNameInExpr(expr->lhs(), symb, s_new);
if (expr->symbol())
if (sameVariableName(expr->symbol(), symb))
expr->setSymbol(s_new);
replaceSymbolSameNameInExpr(expr->lhs(), symb, s_new);
replaceSymbolSameNameInExpr(expr->rhs(), symb, s_new);
}
void replaceVariableSymbSameNameInStatements(SgStatement *first, SgStatement *last, SgSymbol *symb, SgSymbol *s_new, int replace_flag)
{
SgStatement *stmt;
for (stmt=first; stmt; stmt = stmt->lexNext())
{
if (sameVariableName (stmt->symbol(), symb))
stmt->setSymbol(*s_new);
replaceSymbolSameNameInExpr(stmt->expr(0), symb, s_new);
replaceSymbolSameNameInExpr(stmt->expr(1), symb, s_new);
replaceSymbolSameNameInExpr(stmt->expr(2), symb, s_new);
if (last && stmt == last)
break;
}
}
void RenamingCudaFunctionVariables(SgStatement *first, SgSymbol *k_symb, int replace_flag)
{ // replacing kernel names to avoid conflicts with C language keywords and intrinsic function names
SgSymbol *sl;
for (sl=k_symb->next(); sl; sl=sl->next())
{
if (sl->scope() != first || sl->variant() != VARIABLE_NAME)
continue;
SgSymbol *s_symb = hasSameNameAsSource(sl);
if (s_symb)
{
if (replace_flag)
replaceVariableSymbSameNameInStatements(first,first->lastNodeOfStmt(), s_symb, sl, replace_flag);
// first->replaceSymbBySymb(s_symb,sl);
//replaceSymbInStmts(first->thebif, last->thebif, s_symb->thesymb, sl->thesymb);
ChangeSymbolName(sl);
}
}
}

View File

@@ -357,6 +357,7 @@ SgStatement *InsertProcedureCopy(SgStatement *st_header, SgSymbol *sproc, int is
if (options.isOn(C_CUDA)) if (options.isOn(C_CUDA))
{ {
RenamingNewProcedureVariables(new_sproc); // to avoid conflicts with C language keywords
int flagHasDerivedTypeVariables = HasDerivedTypeVariables(new_header); int flagHasDerivedTypeVariables = HasDerivedTypeVariables(new_header);
end_st = new_header->lastNodeOfStmt(); end_st = new_header->lastNodeOfStmt();

View File

@@ -1219,6 +1219,7 @@ void AddRemoteAccessBufferList_ToArrayList();
void AddRemoteAccessBufferList_ToArrayList(); void AddRemoteAccessBufferList_ToArrayList();
SgExpression * ExpressionListsUnion(SgExpression *list, SgExpression *alist); SgExpression * ExpressionListsUnion(SgExpression *list, SgExpression *alist);
SgExpression *isInExprList(SgExpression *e,SgExpression *list); SgExpression *isInExprList(SgExpression *e,SgExpression *list);
symb_list *isInSymbList(SgSymbol *s, symb_list *slist);
symb_list *isInSymbListByChar(SgSymbol *s, symb_list *slist); symb_list *isInSymbListByChar(SgSymbol *s, symb_list *slist);
symb_list *SymbolListsUnion(symb_list *slist1, symb_list *slist2); symb_list *SymbolListsUnion(symb_list *slist1, symb_list *slist2);
void UnregisterVariables(int begin_block); void UnregisterVariables(int begin_block);
@@ -2097,7 +2098,11 @@ void convertExpr(SgExpression*, SgExpression*&);
void convertExpr(SgExpression*, SgExpression*&); void convertExpr(SgExpression*, SgExpression*&);
void initSupportedVars(void); void initSupportedVars(void);
void initF2C_FunctionCalls(void); void initF2C_FunctionCalls(void);
void initIntrinsicFunctionNames(); void initIntrinsicFunctionNames();
void ChangeSymbolName(SgSymbol *symb);
void RenamingNewProcedureVariables(SgSymbol *proc_name);
SgSymbol *hasSameNameAsSource(SgSymbol *symb);
void RenamingCudaFunctionVariables(SgStatement *first, SgSymbol *k_symb, int replace_flag);
void replaceVariableSymbSameNameInStatements(SgStatement *first, SgStatement *last, SgSymbol *symb, SgSymbol *s_new, int replace_flag); void replaceVariableSymbSameNameInStatements(SgStatement *first, SgStatement *last, SgSymbol *symb, SgSymbol *s_new, int replace_flag);
/* acc_across.cpp */ /* acc_across.cpp */
ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter); ArgsForKernel *Create_C_Adapter_Function_Across(SgSymbol *sadapter);

View File

@@ -10,7 +10,8 @@
#include "../IR.h" #include "../IR.h"
template <class NodeType> template <class NodeType>
class BackwardDataFlowAnalysis : public DataFlowAnalysis<NodeType> { class BackwardDataFlowAnalysis : public DataFlowAnalysis<NodeType>
{
std::vector<SAPFOR::BasicBlock*> reorderSequence(const std::vector<SAPFOR::BasicBlock*>& blocks, std::vector<SAPFOR::BasicBlock*> reorderSequence(const std::vector<SAPFOR::BasicBlock*>& blocks,
const std::set<SAPFOR::BasicBlock*> back_edge_sources); const std::set<SAPFOR::BasicBlock*> back_edge_sources);
public: public:

View File

@@ -29,6 +29,7 @@ public:
virtual bool addIn(const DataType& data) = 0; virtual bool addIn(const DataType& data) = 0;
virtual bool addOut(const DataType& data) = 0; virtual bool addOut(const DataType& data) = 0;
virtual bool updateState() { return false; }
virtual bool forwardData(const DataType& data) = 0; virtual bool forwardData(const DataType& data) = 0;
bool newerThan(const DataFlowAnalysisNode<DataType>* block) const { return out_cnt > block->in_cnt; } bool newerThan(const DataFlowAnalysisNode<DataType>* block) const { return out_cnt > block->in_cnt; }

View File

@@ -27,6 +27,8 @@ template <class DataType>
void DataFlowAnalysisNode<DataType>::doStep() void DataFlowAnalysisNode<DataType>::doStep()
{ {
int in_max_cnt = CNT_NOTINIT, out_max_cnt = CNT_NOTINIT; int in_max_cnt = CNT_NOTINIT, out_max_cnt = CNT_NOTINIT;
bool uniq_change = updateState();
for (auto next : prev_blocks) for (auto next : prev_blocks)
{ {
if (in_cnt < next->out_cnt) if (in_cnt < next->out_cnt)
@@ -49,7 +51,7 @@ void DataFlowAnalysisNode<DataType>::doStep()
} }
} }
bool was_notinit = (out_cnt == CNT_NOTINIT); uniq_change |= (out_cnt == CNT_NOTINIT);
if (out_max_cnt != CNT_NOTINIT) if (out_max_cnt != CNT_NOTINIT)
out_cnt = out_max_cnt; out_cnt = out_max_cnt;
@@ -58,7 +60,7 @@ void DataFlowAnalysisNode<DataType>::doStep()
in_cnt = in_max_cnt; in_cnt = in_max_cnt;
// TODO: fix counter overflow // TODO: fix counter overflow
if (was_notinit) if (uniq_change)
{ {
out_cnt++; out_cnt++;
in_cnt++; in_cnt++;
@@ -68,7 +70,8 @@ void DataFlowAnalysisNode<DataType>::doStep()
/* definitions for DataFlowAnalysis class */ /* definitions for DataFlowAnalysis class */
template <class NodeType> template <class NodeType>
void DataFlowAnalysis<NodeType>::analyze() { void DataFlowAnalysis<NodeType>::analyze()
{
auto curr = 0; auto curr = 0;
auto stop = nodes.size(); auto stop = nodes.size();

View File

@@ -14,6 +14,40 @@ using std::set;
using std::remove_if; using std::remove_if;
#define PRINT_USELESS_STATEMENTS 1 #define PRINT_USELESS_STATEMENTS 1
#define DEBUG_IR 0
// detect wich registers are used at more than one block
// such registers should participate in live analysis to spread information about useful instructions
// such registers appears at loops
static void fillSharedRegs(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& cfg, set<SAPFOR::Argument*>& shared_regs)
{
map<SAPFOR::Argument*, SAPFOR::BasicBlock*> used_at;
for (const auto& byFunc : cfg)
{
for (const auto& byBlock : byFunc.second)
{
for (const auto& byIrBlock : byBlock->getInstructions())
{
auto instr = byIrBlock->getInstruction();
set<SAPFOR::Argument*> used = { instr->getResult(), instr->getArg1(), instr->getArg2() };
for (auto arg : used)
{
if(arg && arg->getType() == SAPFOR::CFG_ARG_TYPE::REG)
{
auto it = used_at.find(arg);
if (it == used_at.end())
used_at[arg] = byBlock;
else if(it->second != byBlock)
shared_regs.insert(arg);
}
}
}
}
}
}
static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr,
set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def, set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def,
@@ -39,7 +73,9 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru
{ {
for (SAPFOR::Argument* r : res) for (SAPFOR::Argument* r : res)
{ {
if (use.find(r) != use.end() || r->getMemType() != SAPFOR::CFG_MEM_TYPE::LOCAL_) if (use.find(r) != use.end() ||
r->getMemType() != SAPFOR::CFG_MEM_TYPE::LOCAL_ ||
!(r->getType() == SAPFOR::CFG_ARG_TYPE::VAR || r->getType() == SAPFOR::CFG_ARG_TYPE::REG))
{ {
useful = true; useful = true;
break; break;
@@ -88,8 +124,8 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru
use.insert(e); use.insert(e);
def.erase(e); def.erase(e);
} }
insertIfVar(args.begin(), args.end(), usedByThisBlock); usedByThisBlock.insert(args.begin(), args.end());
} }
if ((instr->getOperation() == SAPFOR::CFG_OP::F_CALL || instr->getOperation() == SAPFOR::CFG_OP::PARAM) && fName == "") if ((instr->getOperation() == SAPFOR::CFG_OP::F_CALL || instr->getOperation() == SAPFOR::CFG_OP::PARAM) && fName == "")
@@ -128,12 +164,13 @@ static void buildUseDef(SAPFOR::BasicBlock* block, set<SAPFOR::Argument*>& use,
useful[i] = u; useful[i] = u;
} }
insertIfVar(use_with_regs.begin(), use_with_regs.end(), use); use.insert(use_with_regs.begin(), use_with_regs.end());
insertIfVar(def_with_regs.begin(), def_with_regs.end(), def); def.insert(def_with_regs.begin(), def_with_regs.end());
} }
class DeadCodeAnalysisNode : public DataFlowAnalysisNode<map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>> { class DeadCodeAnalysisNode : public DataFlowAnalysisNode<map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>>
{
private: private:
vector<bool> useful; vector<bool> useful;
bool useful_block = false; bool useful_block = false;
@@ -142,6 +179,7 @@ private:
vector<SAPFOR::Argument*>& formal_parameters; vector<SAPFOR::Argument*>& formal_parameters;
const map<string, FuncInfo*>& funcByName; const map<string, FuncInfo*>& funcByName;
const set<SAPFOR::Argument*>& shared_regs;
public: public:
bool updateJumpStatus() bool updateJumpStatus()
{ {
@@ -196,30 +234,41 @@ public:
return updated; return updated;
} }
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getIn() { map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getIn()
{
return getBlock()->getLiveOut(); return getBlock()->getLiveOut();
} }
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getOut() { map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getOut()
{
return getBlock()->getLiveIn(); return getBlock()->getLiveIn();
} }
bool addIn(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data) { bool addIn(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data)
bool inserted = getBlock()->addLiveOut(data); {
return getBlock()->addLiveOut(data);
if (!useful_block)
inserted |= updateNextNotEmpty();
inserted |= updateJumpStatus();
return inserted;
} }
bool addOut(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data) { bool addOut(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data)
{
return getBlock()->addLiveIn(data); return getBlock()->addLiveIn(data);
} }
bool forwardData(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data) { bool updateState()
{
bool updated = false;
if (!useful_block)
updated |= updateNextNotEmpty();
updated |= updateJumpStatus();
updated |= this->forwardData({ });
return updated;
}
bool forwardData(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data)
{
bool inserted = false; bool inserted = false;
SAPFOR::BasicBlock* bb= getBlock(); SAPFOR::BasicBlock* bb= getBlock();
@@ -235,30 +284,34 @@ public:
for (SAPFOR::Argument* arg : use) for (SAPFOR::Argument* arg : use)
{ {
bool this_block = usedByThisBlock.find(arg) != usedByThisBlock.end(); if(arg && (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR ||
(arg->getType() == SAPFOR::CFG_ARG_TYPE::REG && shared_regs.find(arg) != shared_regs.end())))
if (!this_block)
{ {
auto data_it = data.find(arg); bool this_block = usedByThisBlock.find(arg) != usedByThisBlock.end();
if (data_it == data.end()) if (!this_block)
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) auto data_it = data.find(arg);
skip = true; // nothing to do, inserted = false
else if (data_it == data.end())
bb->removeLiveIn(arg); 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(!skip)
inserted |= bb->addLiveIn({ { arg, { bb } } });
} }
} }
@@ -291,38 +344,39 @@ public:
DeadCodeAnalysisNode(SAPFOR::BasicBlock* block, DeadCodeAnalysisNode(SAPFOR::BasicBlock* block,
vector<SAPFOR::Argument*>& formal_parameters, vector<SAPFOR::Argument*>& formal_parameters,
const map<string, FuncInfo*>& funcByName) const map<string, FuncInfo*>& funcByName,
const set<SAPFOR::Argument*>& shared_regs)
: :
formal_parameters(formal_parameters), formal_parameters(formal_parameters),
funcByName(funcByName) funcByName(funcByName),
shared_regs(shared_regs)
{ {
setBlock(block); setBlock(block);
useful.resize(block->getInstructions().size(), false); useful.resize(block->getInstructions().size(), false);
set<SAPFOR::Argument*> use, def;
set<SAPFOR::Argument*> usedByThisBlock;
buildUseDef(getBlock(), use, def, this->formal_parameters, useful, usedByThisBlock, funcByName);
for (SAPFOR::Argument* arg : use)
getBlock()->addLiveIn({ { arg, { getBlock() } } });
} }
const vector<bool>& getResult() { return useful; } const vector<bool>& getResult() { return useful; }
}; };
class DeadCodeAnalysis : public BackwardDataFlowAnalysis<DeadCodeAnalysisNode> { class DeadCodeAnalysis : public BackwardDataFlowAnalysis<DeadCodeAnalysisNode>
{
protected: protected:
vector<SAPFOR::Argument*>& formal_parameters; vector<SAPFOR::Argument*>& formal_parameters;
const map<string, FuncInfo*>& funcByName; const map<string, FuncInfo*>& funcByName;
const set<SAPFOR::Argument*>& shared_regs;
DeadCodeAnalysisNode* createNode(SAPFOR::BasicBlock* block) override { DeadCodeAnalysisNode* createNode(SAPFOR::BasicBlock* block) override
return new DeadCodeAnalysisNode(block, formal_parameters, funcByName); {
return new DeadCodeAnalysisNode(block, formal_parameters, funcByName, shared_regs);
} }
public: public:
DeadCodeAnalysis(vector<SAPFOR::Argument*>& formal_parameters, const map<string, FuncInfo*>& funcByName) DeadCodeAnalysis(vector<SAPFOR::Argument*>& formal_parameters,
const map<string, FuncInfo*>& funcByName,
const set<SAPFOR::Argument*>& shared_regs)
: :
formal_parameters(formal_parameters), formal_parameters(formal_parameters),
funcByName(funcByName) funcByName(funcByName),
shared_regs(shared_regs)
{ } { }
}; };
@@ -394,8 +448,10 @@ void removeDeadCode(SgStatement* func,
cfg_pair.second.erase(remove_unreachable_it, cfg_pair.second.end()); cfg_pair.second.erase(remove_unreachable_it, cfg_pair.second.end());
#if DEBUG_IR
dumpCFG({ cfg_pair }, false);
#endif
// detect useless code // detect useless code
vector<SAPFOR::Argument*> func_parameters(cfg_pair.first->funcParams.countOfPars, NULL); vector<SAPFOR::Argument*> func_parameters(cfg_pair.first->funcParams.countOfPars, NULL);
map<string, FuncInfo*> funcByName; map<string, FuncInfo*> funcByName;
@@ -403,8 +459,11 @@ void removeDeadCode(SgStatement* func,
for (auto& byFile : allFuncs) for (auto& byFile : allFuncs)
for (auto byFunc : byFile.second) for (auto byFunc : byFile.second)
funcByName[byFunc->funcName] = byFunc; funcByName[byFunc->funcName] = byFunc;
set<SAPFOR::Argument*> shared_regs;
fillSharedRegs({ cfg_pair }, shared_regs);
DeadCodeAnalysis analysis_object(func_parameters, funcByName); DeadCodeAnalysis analysis_object(func_parameters, funcByName, shared_regs);
analysis_object.fit(cfg_pair.second); analysis_object.fit(cfg_pair.second);
analysis_object.analyze(); analysis_object.analyze();
@@ -462,38 +521,45 @@ void removeDeadCode(SgStatement* func,
__spf_print(PRINT_USELESS_STATEMENTS, "[Useless statement on line %d and file %s]\n", rem->lineNumber(), rem->fileName()); __spf_print(PRINT_USELESS_STATEMENTS, "[Useless statement on line %d and file %s]\n", rem->lineNumber(), rem->fileName());
rem->deleteStmt(); rem->deleteStmt();
} }
remove.clear(); //remove empty blocks
//remove empty blocks do
for (auto st = start; st != end; st = st->lexNext())
{ {
const int var = st->variant(); remove.clear();
if ((var == FOR_NODE || var == WHILE_NODE || var == IF_NODE || var == SWITCH_NODE) && for (auto st = start; st != end; st = st->lexNext())
st->lexNext()->variant() == CONTROL_END)
{ {
remove.push_back(st); const int var = st->variant();
continue; if ((var == FOR_NODE || var == WHILE_NODE || var == SWITCH_NODE) &&
} st->lexNext()->variant() == CONTROL_END)
{
remove.push_back(st);
}
else if (var == IF_NODE)
{
SgStatement* ifS = st;
while (ifS->lexNext()->variant() == ELSEIF_NODE)
ifS = ifS->lexNext();
if (var == IF_NODE) SgStatement* lastNode = ifS->lastNodeOfStmt();
{
SgStatement* ifS = st;
while (ifS->lexNext()->variant() == ELSEIF_NODE)
ifS = ifS->lexNext(); ifS = ifS->lexNext();
if (ifS->lexNext()->variant() == CONTROL_END) while (ifS->variant() == CONTROL_END && ifS != lastNode)
remove.push_back(st); ifS = ifS->lexNext();
if (ifS == lastNode)
remove.push_back(st);
}
//TODO: SWITCH and other block statements
} }
//TODO: SWITCH and other block statements
}
for (auto& rem : remove)
for (auto& rem : remove) {
{ __spf_print(PRINT_USELESS_STATEMENTS, "[Useless block statement on line %d and file %s]\n", rem->lineNumber(), rem->fileName());
__spf_print(PRINT_USELESS_STATEMENTS, "[Useless block statement on line %d and file %s]\n", rem->lineNumber(), rem->fileName()); rem->deleteStmt();
rem->deleteStmt(); }
} } while (remove.size());
deleteCFG(cfg); deleteCFG(cfg);
} }

View File

@@ -290,20 +290,17 @@ static bool isSymbolInExpression(SgSymbol* symbol, SgExpression* exp)
isSymbolInExpression(symbol, exp->rhs()); isSymbolInExpression(symbol, exp->rhs());
} }
static FuncInfo* findFunc(string fileName, string funcName, const map<string, vector<FuncInfo*>>& allFuncInfo) static FuncInfo* findFuncByName(string funcName, const map<string, vector<FuncInfo*>>& allFuncInfo)
{ {
auto fileInfo = allFuncInfo.find(fileName); for (const auto& fileFuncs : allFuncInfo)
if (fileInfo == allFuncInfo.end()) for (auto funcInfo : fileFuncs.second)
return nullptr; if (funcInfo->funcName == funcName)
return funcInfo;
for (auto funcInfo : fileInfo->second)
if (funcInfo->funcName == funcName)
return funcInfo;
return nullptr; return nullptr;
} }
static FuncInfo* getCurrectFunc(SgStatement* stmt, const map<string, vector<FuncInfo*>>& allFuncInfo) static FuncInfo* getCurrentFunc(SgStatement* stmt, const map<string, vector<FuncInfo*>>& allFuncInfo)
{ {
auto fileInfo = allFuncInfo.find(stmt->fileName()); auto fileInfo = allFuncInfo.find(stmt->fileName());
if (fileInfo == allFuncInfo.end()) if (fileInfo == allFuncInfo.end())
@@ -855,7 +852,7 @@ void removePrivates(string filename, vector<Messages>& messages,
for (auto& dcLoopRem : removeDC) for (auto& dcLoopRem : removeDC)
{ {
auto loopStmt = dcLoopRem->loop->GetOriginal(); auto loopStmt = dcLoopRem->loop->GetOriginal();
FuncInfo* currFunc = getCurrectFunc(loopStmt, allFuncInfo); FuncInfo* currFunc = getCurrentFunc(loopStmt, allFuncInfo);
if (currFunc == nullptr) if (currFunc == nullptr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__); printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
@@ -1140,7 +1137,7 @@ static SgForStmt* getLoopStmtForVar(SgStatement* stmt, string loopVar)
static vector<ArraySubscript> getFixedSubscriptsVector(SgArrayRefExp* arrayRef, int dimensionsNum = 0, static vector<ArraySubscript> getFixedSubscriptsVector(SgArrayRefExp* arrayRef, int dimensionsNum = 0,
SgStatement* stmt = nullptr) SgStatement* stmt = nullptr)
{ {
if (arrayRef->numberOfSubscripts() == 0) if (arrayRef == nullptr || arrayRef->numberOfSubscripts() == 0)
return vector<ArraySubscript>(dimensionsNum); return vector<ArraySubscript>(dimensionsNum);
vector<ArraySubscript> subscriptsVector; vector<ArraySubscript> subscriptsVector;
@@ -1202,7 +1199,7 @@ static void checkImplicitDirectUsage(Context* ctx, SgExpression* exp, int stmtLi
{ {
SgFunctionCallExp* funcCallExp = (SgFunctionCallExp*)exp; SgFunctionCallExp* funcCallExp = (SgFunctionCallExp*)exp;
string funcName = funcCallExp->funName()->identifier(); string funcName = funcCallExp->funName()->identifier();
FuncInfo* funcInfo = findFunc(ctx->loopStmt->fileName(), funcName, ctx->allFuncInfo); FuncInfo* funcInfo = findFuncByName(funcName, ctx->allFuncInfo);
if (funcInfo != nullptr) if (funcInfo != nullptr)
{ {
for (int i = 0; i < funcCallExp->numberOfArgs(); ++i) for (int i = 0; i < funcCallExp->numberOfArgs(); ++i)
@@ -1244,8 +1241,8 @@ static vector<vector<ArraySubscript>> checkImplicitDirectUsage(Context* ctx)
// st->variant() == PROC_STAT: // st->variant() == PROC_STAT:
SgCallStmt* callStmt = (SgCallStmt*)st; SgCallStmt* callStmt = (SgCallStmt*)st;
string procName = callStmt->name()->identifier(); string procName = callStmt->name()->identifier();
FuncInfo* funcInfo = findFunc(callStmt->fileName(), procName, ctx->allFuncInfo); FuncInfo* funcInfo = findFuncByName(procName, ctx->allFuncInfo);
for (int i = 0; i < callStmt->numberOfArgs(); ++i) for (int i = 0; i < callStmt->numberOfArgs(); ++i)
{ {
SgExpression* callArg = callStmt->arg(i); SgExpression* callArg = callStmt->arg(i);
@@ -1295,34 +1292,37 @@ static vector<Variable*> getCommonBlockGroupedVar(FuncInfo* curFunc, SgSymbol* v
// checkIndirectUsage returns masks of array indirect usage in function // checkIndirectUsage returns masks of array indirect usage in function
// (indirect usage is usage through common blocks) and writes messages about it // (indirect usage is usage through common blocks) and writes messages about it
static void checkIndirectUsage(Context* ctx, FuncInfo* curFunc, vector<Variable*> commonBlockGroupedVar, static void checkIndirectUsage(Context* ctx, FuncInfo* calledFunc, vector<Variable*> commonBlockGroupedVar,
set<string>& visitedFuncs, vector<vector<ArraySubscript>>& indirectUsageMasks) set<string>& visitedFuncs, vector<vector<ArraySubscript>>& indirectUsageMasks)
{ {
if (visitedFuncs.find(curFunc->funcName) != visitedFuncs.end()) if (visitedFuncs.find(calledFunc->funcName) != visitedFuncs.end())
return; return;
visitedFuncs.insert(curFunc->funcName); visitedFuncs.insert(calledFunc->funcName);
for (Variable* commonBlockVar : commonBlockGroupedVar) for (Variable* commonBlockVar : commonBlockGroupedVar)
{ {
for (const CommonVariableUse& varUse : commonBlockVar->getAllUse()) for (const CommonVariableUse& varUse : commonBlockVar->getAllUse())
{ {
if (varUse.getFileName() != curFunc->fileName || varUse.getFunctionName() != curFunc->funcName) if (varUse.getFileName() != calledFunc->fileName || varUse.getFunctionName() != calledFunc->funcName)
continue; continue;
vector<SgArrayRefExp*> directArrayRefs = getDirectArrayRefs(varUse.getFunction(), varUse.getUseS()); SgStatement* calledFuncStmt = varUse.getFunction();
calledFuncStmt->switchToFile();
vector<SgArrayRefExp*> directArrayRefs = getDirectArrayRefs(calledFuncStmt, varUse.getUseS());
for (auto arrayRef : directArrayRefs) for (auto arrayRef : directArrayRefs)
{ {
auto mask = getFixedSubscriptsVector(arrayRef, ctx->dimensionsNum); auto mask = getFixedSubscriptsVector(arrayRef, ctx->dimensionsNum);
indirectUsageMasks.push_back(mask); indirectUsageMasks.push_back(mask);
addMessageUsageInFunctionCall(ctx->messages, getDimensionVarName(ctx->arraySymbol, mask), addMessageUsageInFunctionCall(ctx->messages, getDimensionVarName(ctx->arraySymbol, mask),
curFunc->funcName, ctx->loop->lineNum, ctx->loop->lineNum); calledFunc->funcName, ctx->loop->lineNum, ctx->loop->lineNum);
} }
ctx->loopStmt->switchToFile();
} }
} }
for (FuncInfo* calledFunc : curFunc->callsFromV) for (FuncInfo* subCalledFunc : calledFunc->callsFromV)
checkIndirectUsage(ctx, calledFunc, commonBlockGroupedVar, visitedFuncs, indirectUsageMasks); checkIndirectUsage(ctx, subCalledFunc, commonBlockGroupedVar, visitedFuncs, indirectUsageMasks);
} }
// checkIndirectUsage returns masks of array indirect usage in any function call in loop // checkIndirectUsage returns masks of array indirect usage in any function call in loop
@@ -1330,7 +1330,7 @@ static void checkIndirectUsage(Context* ctx, FuncInfo* curFunc, vector<Variable*
static vector<vector<ArraySubscript>> checkIndirectUsage(Context* ctx) static vector<vector<ArraySubscript>> checkIndirectUsage(Context* ctx)
{ {
vector<vector<ArraySubscript>> indirectUsageMasks; vector<vector<ArraySubscript>> indirectUsageMasks;
FuncInfo* currentFunc = getCurrectFunc(ctx->loopStmt, ctx->allFuncInfo); FuncInfo* currentFunc = getCurrentFunc(ctx->loopStmt, ctx->allFuncInfo);
if (currentFunc == nullptr) if (currentFunc == nullptr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__); printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
@@ -1338,10 +1338,17 @@ static vector<vector<ArraySubscript>> checkIndirectUsage(Context* ctx)
if (commonBlockGroupedVar.empty()) if (commonBlockGroupedVar.empty())
return indirectUsageMasks; return indirectUsageMasks;
set<string> visitedFunctions = { currentFunc->funcName }; set<string> visitedFunctions = { currentFunc->funcName };
for (FuncInfo* calledFunc : currentFunc->callsFromV) for (SgStatement* st = ctx->loopStmt->lexNext(); st != ctx->loopStmt->lastNodeOfStmt(); st = st->lexNext())
checkIndirectUsage(ctx, calledFunc, commonBlockGroupedVar, visitedFunctions, indirectUsageMasks); {
if (st->variant() == PROC_STAT)
{
SgCallStmt* callStmt = (SgCallStmt*)st;
string procName = callStmt->name()->identifier();
FuncInfo* calledFunc = findFuncByName(procName, ctx->allFuncInfo);
checkIndirectUsage(ctx, calledFunc, commonBlockGroupedVar, visitedFunctions, indirectUsageMasks);
}
}
return indirectUsageMasks; return indirectUsageMasks;
} }
@@ -2136,8 +2143,8 @@ void removePrivatesAnalysis(string filename,
auto filterMasks = checkImplicitAndIndirectUsage(&context); auto filterMasks = checkImplicitAndIndirectUsage(&context);
filterArrayRefs(&context, arrayRefs, filterMasks); filterArrayRefs(&context, arrayRefs, filterMasks);
context.explicitArrayRefs.swap(arrayRefs);
context.explicitArrayRefs.swap(arrayRefs);
if (context.explicitArrayRefs.empty()) if (context.explicitArrayRefs.empty())
continue; continue;

View File

@@ -291,7 +291,7 @@ void InitPassesDependencies(map<passes, vector<passes>> &passDepsIn, set<passes>
list({ CALL_GRAPH2, REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= list({ DUPLICATE_FUNCTIONS, REMOVE_UNUSED_FUNCTIONS }); list({ CALL_GRAPH2, REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= list({ DUPLICATE_FUNCTIONS, REMOVE_UNUSED_FUNCTIONS });
list({ CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= list({ LOOPS_SPLITTER, LOOPS_COMBINER, PRIVATE_ARRAYS_EXPANSION, PRIVATE_ARRAYS_SHRINKING, CREATE_PARALLEL_REGIONS, PURE_SAVE_TO_PARAMS, PURE_MODULE_TO_PARAMS, PURE_COMMON_TO_PARAMS, PURE_INTENT_INSERT }); list({ CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= list({ LOOPS_SPLITTER, LOOPS_COMBINER, PRIVATE_ARRAYS_EXPANSION, PRIVATE_ARRAYS_SHRINKING, CREATE_PARALLEL_REGIONS, PURE_SAVE_TO_PARAMS, PURE_MODULE_TO_PARAMS, PURE_COMMON_TO_PARAMS, PURE_INTENT_INSERT, PRIVATE_REMOVING });
list({ GET_ALL_ARRAY_DECL, FILL_PARALLEL_REG_IR }) <= Pass(CONVERT_ASSIGN_TO_LOOP); list({ GET_ALL_ARRAY_DECL, FILL_PARALLEL_REG_IR }) <= Pass(CONVERT_ASSIGN_TO_LOOP);
@@ -307,8 +307,8 @@ void InitPassesDependencies(map<passes, vector<passes>> &passDepsIn, set<passes>
Pass(REMOVE_OMP_DIRS) <= Pass(REMOVE_OMP_DIRS_TRANSFORM); Pass(REMOVE_OMP_DIRS) <= Pass(REMOVE_OMP_DIRS_TRANSFORM);
Pass(CALL_GRAPH2) <= Pass(REMOVE_DEAD_CODE); list({ CALL_GRAPH2, REVERT_SUBST_EXPR_RD }) <= Pass(REMOVE_DEAD_CODE);
list({ REMOVE_DEAD_CODE, REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= Pass(REMOVE_DEAD_CODE_AND_UNPARSE); list({ REMOVE_DEAD_CODE, 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, 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, EXTRACT_SHADOW_DIRS, CREATE_REMOTES, UNPARSE_FILE, REMOVE_AND_CALC_SHADOW,

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define VERSION_SPF "2303" #define VERSION_SPF "2305"