@@ -135,13 +135,17 @@ static const string constructNewBoundName(const char *oldName, int loopLineNumbe
return newName ;
}
static SgSymbol * createNewArrayNameSymbol ( SgExpression * declaration , bool isExpansion )
static SgSymbol * createNewArrayNameSymbol ( SgExpression * declaration , bool isExpansion , bool canBeStatic )
{
SgType * type = new SgType ( T_ARRAY ) ;
char * newNameStr = constructNewArrayName ( declaration - > symbol ( ) - > identifier ( ) , isExpansion ) ;
SgSymbol * newName = new SgSymbol ( VARIABLE_NAME , newNameStr , type , NULL ) ;
newName - > setAttribute ( declaration - > symbol ( ) - > attributes ( ) ) ;
auto attrs = declaration - > symbol ( ) - > attributes ( ) ;
if ( ! canBeStatic )
attrs | = ALLOCATABLE_BIT ;
newName - > setAttribute ( attrs ) ;
return newName ;
}
@@ -155,7 +159,17 @@ static void fillLoopBoundExprs(const LoopGraph* forLoop, int depthOfResize, cons
{
if ( indexes [ j ] & & isEqSymbols ( loopStmt - > doName ( ) , indexes [ j ] ) )
{
bounds [ j ] = new SgExpression ( DDOT , loopStmt - > start ( ) - > copyPtr ( ) , loopStmt - > end ( ) - > copyPtr ( ) , NULL ) ;
//TODO: add MIN and MAX call for bounds
if ( curLoop - > stepVal = = 0 )
{
__spf_print ( 1 , " unknown step sign of loop on line %d \n " , curLoop - > lineNum ) ;
printInternalError ( convertFileName ( __FILE__ ) . c_str ( ) , __LINE__ ) ;
}
if ( curLoop - > stepVal > 0 )
bounds [ j ] = new SgExpression ( DDOT , loopStmt - > start ( ) - > copyPtr ( ) , loopStmt - > end ( ) - > copyPtr ( ) , NULL ) ;
else
bounds [ j ] = new SgExpression ( DDOT , loopStmt - > end ( ) - > copyPtr ( ) , loopStmt - > start ( ) - > copyPtr ( ) , NULL ) ;
break ;
}
}
@@ -335,7 +349,7 @@ static SgSymbol* alterExtendArrayDeclaration(const LoopGraph* forLoop, SgStateme
SgExpression * array = findSymbolInExprList ( arraySymbol , declarationStmt - > expr ( 0 ) ) ;
SgExpression * newArray = array - > copyPtr ( ) ;
newArraySymbol = createNewArrayNameSymbol ( newArray , true ) ;
newArraySymbol = createNewArrayNameSymbol ( newArray , true , canBeStatic );
if ( newArraySymbol = = NULL )
printInternalError ( convertFileName ( __FILE__ ) . c_str ( ) , __LINE__ ) ;
@@ -350,13 +364,14 @@ static SgSymbol* alterExtendArrayDeclaration(const LoopGraph* forLoop, SgStateme
return newArraySymbol ;
}
static SgSymbol * alterShrinkArrayDeclaration ( SgStatement * declarationStatement , SgSymbol * arraySymbol , vector < int > & dimensions )
static SgSymbol * alterShrinkArrayDeclaration ( SgStatement * declarationStatement , SgSymbol * arraySymbol ,
vector < int > & dimensions , bool canBeStatic )
{
SgSymbol * newArraySymbol = NULL ;
SgExpression * array = findSymbolInExprList ( arraySymbol , declarationStatement - > expr ( 0 ) ) ;
SgExpression * newArray = array - > copyPtr ( ) ;
newArraySymbol = createNewArrayNameSymbol ( newArray , false ) ;
newArraySymbol = createNewArrayNameSymbol ( newArray , false , canBeStatic );
reduceArray ( dimensions , newArray , newArraySymbol ) ;
@@ -369,35 +384,25 @@ static SgSymbol* alterShrinkArrayDeclaration(SgStatement *declarationStatement,
return newArraySymbol ;
}
static void extendArrayRefsInExpr ( const vector < SgSymbol * > & indexes , SgExpression * expr , SgSymbol * arraySymbol ,
SgSymbol * newA rraySymbol, const vector < SgExpression * > & lowBounds )
static void extendArrayRefsInExpr ( const vector < SgSymbol * > & indexes , SgExpression * expr ,
SgSymbol * a rraySymbol, SgSymbol * newArraySymbol ,
const vector < SgExpression * > & lowBounds )
{
SgExpression * lhs = expr - > lhs ( ) , * rhs = expr - > rhs ( ) ;
if ( lhs )
if ( expr )
{
if ( isArrayRef ( lh s) & & isEqSymbols ( arraySymbol , lhs - > symbol ( ) ) )
extendArrayRef ( indexes , lhs , newArraySymbol , lowBounds ) ;
else if ( lhs - > variant ( ) = = VAR_REF & & isEqSymbols ( arraySymbol , lhs - > symbol ( ) ) )
{
SgExpression * extended = extendArrayRef ( indexes , lhs , newArraySymbol , lowBounds ) ;
expr - > setLhs ( extended ) ;
}
else
extendArrayRefsInExpr ( indexes , lhs , arraySymbol , newArraySymbol , lowBounds ) ;
}
extendArrayRefsInExpr ( indexe s, expr - > lhs ( ) , arraySymbol , newArraySymbol , lowBounds ) ;
extendArrayRefsInExpr ( indexes , expr - > rhs ( ) , arraySymbol , newArraySymbol , lowBounds ) ;
if ( rhs )
{
if ( isArrayRef ( rhs ) & & isEqSymbols ( arraySymbol , rhs - > symbol ( ) ) )
extendArrayRef ( indexes , rhs , newArraySymbol , lowBounds ) ;
else if ( rhs - > variant ( ) = = VAR_REF & & isEqSymbols ( arraySymbol , rhs - > symbol ( ) ) )
if ( isArrayRef ( expr ) & & isEqSymbols ( arraySymbol , expr - > symbol ( ) ) )
extendArrayRef ( indexes , expr , newArraySymbol , lowBounds ) ;
else if ( expr - > variant ( ) = = VAR_REF & & isEqSymbols ( arraySymbol , expr - > symbol ( ) ) )
{
SgExpression * extended = extendArrayRef ( indexes , rhs , newArraySymbol , lowBounds ) ;
expr - > setRhs ( extended ) ;
SgExpression * extended = extendArrayRef ( indexes , expr , newArraySymbol , lowBounds ) ;
expr - > setSymbol ( extended - > symbol ( ) ) ;
expr - > setLhs ( extended - > lhs ( ) ) ;
expr - > setRhs ( extended - > rhs ( ) ) ;
}
else
extendArrayRefsInExpr ( indexes , rhs , arraySymbol , newArraySymbol , lowBounds ) ;
}
}
@@ -406,18 +411,22 @@ static void extendArrayRefs(const vector<SgSymbol*> &indexes, SgStatement *st, S
{
for ( int i = 0 ; i < 3 ; + + i )
{
if ( st - > expr ( i ) )
if ( st - > variant ( ) = = PROC_STAT )
{
if ( isArrayRef ( st - > expr ( i ) ) & & isEqSymbols ( arraySymbol , st - > expr ( i ) - > symbol ( ) ) )
extendArrayRef ( indexes , st - > expr ( i ) , newArraySymbol , lowBounds ) ;
else if ( st - > expr ( i ) - > variant ( ) = = VAR_REF & & isEqSymbols ( arraySymbol , st - > expr ( i ) - > symbol ( ) ) )
SgCallStmt * call = isSgCallStmt ( st ) ;
for ( int arg = 0 ; arg < call - > numberOfArgs ( ) ; + + arg )
{
SgExpression * extended = extendArrayRef ( indexes , st - > expr ( i ) , newArraySymbol , lowBounds ) ;
st - > setExpression ( i , * extended ) ;
auto argRef = call - > arg ( arg ) ;
if ( ( isArrayRef ( argRef ) | | argRef - > variant ( ) = = VAR_REF ) & & isEqSymbols ( argRef - > symbol ( ) , arraySymbol ) )
{
__spf_print ( 1 , " unsupported private array extension under call on line %d \n " , st - > lineNumber ( ) ) ;
printInternalError ( convertFileName ( __FILE__ ) . c_str ( ) , __LINE__ ) ;
}
}
else
extendArrayRefsInExpr ( indexes , st - > expr ( i ) , arraySymbol , newArraySymbol , lowBounds ) ;
}
if ( st - > expr ( i ) )
extendArrayRefsInExpr ( indexes , st - > expr ( i ) , arraySymbol , newArraySymbol , lowBounds ) ;
}
}
@@ -428,6 +437,8 @@ static SgStatement* createNewDeclarationStatemnet(SgStatement *loop, SgStatement
{
if ( isSgExecutableStatement ( lastDecl ) )
break ;
if ( lastDecl - > variant ( ) = = CONTAINS_STMT )
break ;
lastDecl = lastDecl - > lexNext ( ) ;
}
@@ -559,7 +570,7 @@ static SgExpression* constructArrayAllocationExp(const LoopGraph* forLoop, SgExp
return new SgExpression ( EXPR_LIST , arrayRef , ( SgExpression * ) NULL , ( SgSymbol * ) NULL ) ;
}
static void insertAllocDealloc ( const LoopGraph * forLoop , SgSymbol * origArraySymbol , SgSymbol * arraySymbol ,
static void insertAllocDealloc ( const LoopGraph * forLoop , SgSymbol * origArraySymbol , SgSymbol * arraySymbol , SgSymbol * allocDone ,
bool isExpansion , const vector < SgSymbol * > * indexes = NULL , const vector < int > * shrinkIndexes = NULL )
{
SgForStmt * loopStmt = ( SgForStmt * ) ( forLoop - > loop - > GetOriginal ( ) ) ;
@@ -589,11 +600,17 @@ static void insertAllocDealloc(const LoopGraph* forLoop, SgSymbol* origArraySymb
new SgExpression ( ARRAY_REF , ( SgExpression * ) NULL , ( SgExpression * ) NULL , arraySymbol ) ,
( SgExpression * ) NULL , ( SgSymbol * ) NULL ) ;
SgStatement * allocate = new SgStatement ( ALLOCATE_STMT , ( SgLabel * ) NULL , ( SgSymbol * ) NULL , arrayAllocation , ( SgExpression * ) NULL , ( SgExpression * ) NULL ) ;
SgStatement * deallocate = new SgStatement ( DEALLOCATE_STMT , ( SgLabel * ) NULL , ( SgSymbol * ) NULL , arrayDeallocation , ( SgExpression * ) NULL , ( SgExpression * ) NULL ) ;
loopStmt - > insertStmtBefore ( * allocate , * loopStmt - > controlParent ( ) ) ;
loopStmt - > lastNodeOfStmt ( ) - > insertStmtAfter ( * dea llocate , * loopStmt - > controlParent ( ) ) ;
SgStatement * allocate = new SgStatement ( ALLOCATE_STMT , ( SgLabel * ) NULL , ( SgSymbol * ) NULL , arrayA llocation , ( SgExpression * ) NULL , ( SgExpression * ) NULL ) ;
SgIfStmt * ifSt = new SgIfStmt ( SgEqOp ( * new SgVarRefExp ( allocDone ) , * new SgValueExp ( 0 ) ) , * allocate ) ;
loopStmt - > insertStmtBefore ( * ifSt , * loopStmt - > controlParent ( ) ) ;
// insert allocates as save
//SgStatement* deallocate = new SgStatement(DEALLOCATE_STMT, (SgLabel*)NULL, (SgSymbol*)NULL, arrayDeallocation, (SgExpression*)NULL, (SgExpression*)NULL);
//loopStmt->lastNodeOfStmt()->insertStmtAfter(*deallocate, *loopStmt->controlParent());
}
static bool containsFunctionCall ( SgExpression * exp )
@@ -781,6 +798,61 @@ static void renamePrivateVarsInAttributes(const vector<SgStatement*>& attrs, con
}
}
static void removePrivateVarsInAttributes ( const vector < SgStatement * > & attrs , const map < SgSymbol * , SgSymbol * > & symbols )
{
for ( SgStatement * st : attrs )
{
if ( st - > variant ( ) ! = SPF_ANALYSIS_DIR )
return ;
SgExpression * exprList = st - > expr ( 0 ) ;
vector < SgExpression * > newL ;
bool remList = false ;
while ( exprList )
{
if ( exprList - > lhs ( ) - > variant ( ) = = ACC_PRIVATE_OP )
{
vector < SgExpression * > newL ;
bool removed = false ;
SgExpression * list = exprList - > lhs ( ) - > lhs ( ) ;
while ( list )
{
bool found = false ;
for ( auto & pair : symbols )
{
if ( isEqSymbols ( pair . first , list - > lhs ( ) - > symbol ( ) ) )
found = true ;
if ( found )
break ;
}
if ( ! found )
newL . push_back ( list - > lhs ( ) ) ;
else
removed = true ;
list = list - > rhs ( ) ;
}
if ( removed )
{
if ( newL . size ( ) = = 0 )
remList = true ;
else
{
exprList - > lhs ( ) - > setLhs ( makeExprList ( newL ) ) ;
newL . push_back ( exprList - > lhs ( ) ) ;
}
}
}
else
newL . push_back ( exprList - > lhs ( ) ) ;
exprList = exprList - > rhs ( ) ;
}
if ( remList )
st - > setExpression ( 0 , makeExprList ( newL ) ) ;
}
}
static int getArrayDimensionality ( SgSymbol * array )
{
if ( array - > type ( ) - > variant ( ) ! = T_ARRAY )
@@ -975,7 +1047,7 @@ static int fillIndexesToExtend(const LoopGraph* loop, int origDim, int depthOfRe
return 0 ;
}
static SgSymbol * shrinkArray ( const LoopGraph * forLoop , SgSymbol * arraySymbol , vector < int > & indexes )
static SgSymbol * shrinkArray ( const LoopGraph * forLoop , SgSymbol * arraySymbol , vector < int > & indexes , SgSymbol * allocDone )
{
int maxDepth = forLoop - > perfectLoop ;
const LoopGraph * curLoop = forLoop ;
@@ -1005,16 +1077,17 @@ static SgSymbol* shrinkArray(const LoopGraph *forLoop, SgSymbol *arraySymbol, ve
SgStatement * originalDeclaration = declaratedInStmt ( arraySymbol ) ;
SgStatement * copiedDeclaration = createNewDeclarationStatemnet ( forLoop - > loop - > GetOriginal ( ) , originalDeclaration , arraySymbol ) ;
bool canBeStatic = ! ( ! reduceToVariable & & isAllocatable ( arraySymbol ) ) ;
SgSymbol * newSymbol = reduceToVariable
? createReducedToVariableArray ( copiedDeclaration , forLoop - > lineNum , arraySymbol )
: alterShrinkArrayDeclaration ( copiedDeclaration , arraySymbol , indexes ) ;
: alterShrinkArrayDeclaration ( copiedDeclaration , arraySymbol , indexes , canBeStatic );
if ( newSymbol )
{
SgForStmt * loopStmt = ( SgForStmt * ) ( forLoop - > loop - > GetOriginal ( ) ) ;
if ( ! reduceToVariable & & isAllocatable ( arraySymbol ) )
insertAllocDealloc ( forLoop , arraySymbol , newSymbol , false , NULL , & indexes ) ;
insertAllocDealloc ( forLoop , arraySymbol , newSymbol , false , allocDone , NULL , & indexes ) ;
for ( SgStatement * st = loopStmt - > lexNext ( ) ; st ! = loopStmt - > lastNodeOfStmt ( ) - > lexNext ( ) ; st = st - > lexNext ( ) )
if ( st - > variant ( ) ! = ALLOCATE_STMT & & st - > variant ( ) ! = DEALLOCATE_STMT )
@@ -1026,7 +1099,7 @@ static SgSymbol* shrinkArray(const LoopGraph *forLoop, SgSymbol *arraySymbol, ve
return NULL ;
}
static SgSymbol * resizeArray ( const LoopGraph * forLoop , SgSymbol * arraySymbol , int depthOfResize )
static SgSymbol * resizeArray ( const LoopGraph * forLoop , SgSymbol * arraySymbol , int depthOfResize , SgSymbol * allocDone )
{
if ( depthOfResize < 0 | | depthOfResize > forLoop - > perfectLoop )
depthOfResize = forLoop - > perfectLoop ;
@@ -1074,7 +1147,8 @@ static SgSymbol* resizeArray(const LoopGraph *forLoop, SgSymbol *arraySymbol, in
{
SgForStmt * loopStmt = ( SgForStmt * ) ( forLoop - > loop - > GetOriginal ( ) ) ;
if ( ! canBeStatic | | isAllocatable ( newArraySymbol ) )
insertAllocDealloc ( forLoop , arraySymbol , newArraySymbol , true , & indexes , NULL ) ;
insertAllocDealloc ( forLoop , arraySymbol , newArraySymbol , allocDone , true , & indexes , NULL ) ;
for ( SgStatement * st = loopStmt - > lexNext ( ) ; st ! = loopStmt - > lastNodeOfStmt ( ) - > lexNext ( ) ; st = st - > lexNext ( ) )
if ( st - > variant ( ) ! = ALLOCATE_STMT & & st - > variant ( ) ! = DEALLOCATE_STMT )
extendArrayRefs ( indexes , st , arraySymbol , newArraySymbol , lowBounds ) ;
@@ -1257,13 +1331,27 @@ void analyzeShrinking(SgFile* file, const vector<LoopGraph*>& loopGraphs, vector
}
}
static void createAllocDoneSymbol ( map < SgStatement * , SgSymbol * > & allocDoneByFunc , SgStatement * func )
{
if ( allocDoneByFunc . find ( func ) = = allocDoneByFunc . end ( ) )
{
auto newName = ( string ( " spf_ " ) + string ( func - > symbol ( ) - > identifier ( ) ) + " _alloc_ " ) ;
allocDoneByFunc [ func ] = new SgSymbol ( VARIABLE_NAME , newName . c_str ( ) , SgTypeInt ( ) , func ) ;
}
}
//Вычислять размер массива с учётом шага цикла - //TODO
int privateArraysResizing ( SgFile * file , const vector < LoopGraph * > & loopGraphs , vector < Messages > & messages ,
const map < pair < string , int > , set < SgStatement * > > & usersDirectives , bool isExpand )
void privateArraysResizing ( SgFile * file , const vector < LoopGraph * > & loopGraphs , vector < Messages > & messages , int & countOfTransform ,
const map < pair < string , int > , set < SgStatement * > > & usersDirectives , bool isExpand )
{
map < int , LoopGraph * > mapLoopGraph ;
createMapLoopGraph ( loopGraphs , mapLoopGraph ) ;
map < SgStatement * , SgSymbol * > allocDoneByFunc ;
map < SgStatement * , SgSymbol * > usedAllocDoneByFunc ;
map < SgStatement * , vector < SgExpression * > > saveDecls ;
for ( auto & loopPair : mapLoopGraph )
{
LoopGraph * loop = loopPair . second ;
@@ -1275,6 +1363,9 @@ int privateArraysResizing(SgFile *file, const vector<LoopGraph*> &loopGraphs, ve
auto attrPrivInCode = getAttributes < SgStatement * , SgStatement * > ( loopSt , set < int > { SPF_ANALYSIS_DIR } ) ;
auto arrayPrivates = fillPrivates ( usersDirectives , loop ) ;
auto func = getFuncStat ( loopSt ) ;
set < SgSymbol * > symbolsToDecl ;
if ( arrayPrivates . size ( ) = = 0 )
{
@@ -1284,9 +1375,7 @@ int privateArraysResizing(SgFile *file, const vector<LoopGraph*> &loopGraphs, ve
else
__spf_printToBuf ( str , " Can not do PRIVATE SHRINK for this loop - privates not found " ) ;
//TODO:
//messages.push_back(Messages(NOTE, loop->lineNum, str, 2008));
__spf_print ( 1 , " %s on line %d \n " , str . c_str ( ) , loop - > lineNum ) ;
__spf_print ( 0 , " %s on line %d \n " , str . c_str ( ) , loop - > lineNum ) ;
}
else
{
@@ -1302,6 +1391,8 @@ int privateArraysResizing(SgFile *file, const vector<LoopGraph*> &loopGraphs, ve
if ( list - > lhs ( ) - > variant ( ) = = SPF_EXPAND_OP & & isExpand )
{
createAllocDoneSymbol ( allocDoneByFunc , func ) ;
int deep = - 1 ;
if ( list - > lhs ( ) - > lhs ( ) ! = NULL )
{
@@ -1312,14 +1403,24 @@ int privateArraysResizing(SgFile *file, const vector<LoopGraph*> &loopGraphs, ve
for ( SgSymbol * privArr : arrayPrivates )
{
SgSymbol * newSymbol = resizeArray ( loop , privArr , deep ) ;
SgSymbol * newSymbol = resizeArray ( loop , privArr , deep , allocDoneByFunc [ func ] ) ;
if ( newSymbol )
{
symbols . insert ( std : : make_pair ( privArr , newSymbol ) ) ;
usedAllocDoneByFunc [ func ] = allocDoneByFunc [ func ] ;
countOfTransform + + ;
if ( isAllocatable ( newSymbol ) )
symbolsToDecl . insert ( newSymbol ) ;
}
}
renam ePrivateVarsInAttributes ( attrPrivInCode , symbols ) ;
remov ePrivateVarsInAttributes ( attrPrivInCode , symbols ) ;
}
else if ( list - > lhs ( ) - > variant ( ) = = SPF_SHRINK_OP & & ! isExpand )
{
createAllocDoneSymbol ( allocDoneByFunc , func ) ;
SgExprListExp * listExp = isSgExprListExp ( list - > lhs ( ) - > lhs ( ) ) ;
checkNull ( listExp , convertFileName ( __FILE__ ) . c_str ( ) , __LINE__ ) ;
@@ -1335,9 +1436,17 @@ int privateArraysResizing(SgFile *file, const vector<LoopGraph*> &loopGraphs, ve
if ( ! indexes . empty ( ) & & ! skip )
{
SgSymbol * newSymbol = shrinkArray ( loop , privArr , indexes ) ;
SgSymbol * newSymbol = shrinkArray ( loop , privArr , indexes , allocDoneByFunc [ func ] ) ;
if ( newSymbol )
{
symbols . insert ( std : : make_pair ( privArr , newSymbol ) ) ;
usedAllocDoneByFunc [ func ] = allocDoneByFunc [ func ] ;
countOfTransform + + ;
if ( isAllocatable ( newSymbol ) )
symbolsToDecl . insert ( newSymbol ) ;
}
}
}
renamePrivateVarsInAttributes ( attrPrivInCode , symbols ) ;
@@ -1348,23 +1457,39 @@ int privateArraysResizing(SgFile *file, const vector<LoopGraph*> &loopGraphs, ve
list = list - > rhs ( ) ;
}
SgExpression * ex = NULL ;
SgExpression * p = NULL ;
for ( int z = 0 ; z < newL . size ( ) ; + + z )
{
if ( z = = 0 )
p = ex = newL [ z ] ;
else
{
ex - > setRhs ( newL [ z ] ) ;
ex = ex - > rhs ( ) ;
}
}
attr - > setExpression ( 0 , p ) ;
attr - > setExpression ( 0 , makeExprList ( newL , false ) ) ;
//__spf_print(1, "set new %d attributes to line %d\n", newL.size(), loop->lineNum);
}
}
for ( auto & elem : symbolsToDecl )
saveDecls [ func ] . push_back ( new SgVarRefExp ( elem ) ) ;
}
return 0 ;
for ( auto & funcPair : saveDecls )
{
auto func = funcPair . first ;
if ( usedAllocDoneByFunc . find ( func ) = = usedAllocDoneByFunc . end ( ) )
printInternalError ( convertFileName ( __FILE__ ) . c_str ( ) , __LINE__ ) ;
SgSymbol * allocVar = usedAllocDoneByFunc [ func ] ;
auto list = makeExprList ( funcPair . second ) ;
SgStatement * save = new SgStatement ( SAVE_DECL , NULL , NULL , list ) ;
SgStatement * st = func - > lexNext ( ) ;
while ( ! isSgExecutableStatement ( st ) )
{
if ( st - > variant ( ) = = CONTAINS_STMT )
break ;
st = st - > lexNext ( ) ;
}
vector < SgExpression * > init = { new SgValueExp ( - 1 ) } ;
makeDeclaration ( func , { allocVar } , & init ) ;
st - > insertStmtBefore ( * save , * func ) ;
SgAssignStmt * assign = new SgAssignStmt ( * new SgVarRefExp ( allocVar ) , * new SgVarRefExp ( allocVar ) + * new SgValueExp ( 1 ) ) ;
st - > insertStmtBefore ( * assign , * func ) ;
}
}