add message and restore pass

This commit is contained in:
2026-04-19 20:47:20 +03:00
parent 9e2d0eacbd
commit a5ca9e593d
16 changed files with 743 additions and 550 deletions

View File

@@ -165,9 +165,6 @@ set(PARALLEL_REG src/ParallelizationRegions/ParRegions.cpp
src/ParallelizationRegions/uniq_name_creator.cpp src/ParallelizationRegions/uniq_name_creator.cpp
src/ParallelizationRegions/uniq_name_creator.h) src/ParallelizationRegions/uniq_name_creator.h)
set(ARRAY_PROP src/ArrayConstantPropagation/propagation.cpp
src/ArrayConstantPropagation/propagation.h
)
set(MERGE_REGIONS src/ParallelizationRegions/merge_regions.cpp set(MERGE_REGIONS src/ParallelizationRegions/merge_regions.cpp
src/ParallelizationRegions/merge_regions.h) src/ParallelizationRegions/merge_regions.h)
@@ -214,6 +211,8 @@ set(TR_RENAME_SYMBOLS src/Transformations/RenameSymbols/rename_symbols.cpp
src/Transformations/RenameSymbols/rename_symbols.h) src/Transformations/RenameSymbols/rename_symbols.h)
SET(TR_MOVE_OPERATORS src/Transformations/MoveOperators/move_operators.cpp SET(TR_MOVE_OPERATORS src/Transformations/MoveOperators/move_operators.cpp
src/Transformations/MoveOperators/move_operators.h) src/Transformations/MoveOperators/move_operators.h)
SET(TR_ARR_PROP src/Transformations/ArrayConstantPropagation/propagation.cpp
src/Transformations/ArrayConstantPropagation/propagation.h)
set(TRANSFORMS set(TRANSFORMS
${TR_DEAD_CODE} ${TR_DEAD_CODE}
@@ -237,7 +236,8 @@ set(TRANSFORMS
${TR_EXPR_TRANSFORM} ${TR_EXPR_TRANSFORM}
${TR_INLINER} ${TR_INLINER}
${TR_RENAME_SYMBOLS} ${TR_RENAME_SYMBOLS}
${TR_MOVE_OPERATORS}) ${TR_MOVE_OPERATORS}
${TR_ARR_PROP})
set(CFG src/CFGraph/IR.cpp set(CFG src/CFGraph/IR.cpp
src/CFGraph/IR.h src/CFGraph/IR.h
@@ -432,7 +432,6 @@ set(SOURCE_EXE
${PARALLEL_REG} ${PARALLEL_REG}
${MERGE_REGIONS} ${MERGE_REGIONS}
${PRIV} ${PRIV}
${ARRAY_PROP}
${FDVM} ${FDVM}
${OMEGA} ${OMEGA}
${UTILS} ${UTILS}
@@ -473,6 +472,7 @@ source_group (Transformations\\ConvertToC FILES ${TR_CONV})
source_group (Transformations\\SetImplicitNone FILES ${TR_IMPLICIT_NONE}) source_group (Transformations\\SetImplicitNone FILES ${TR_IMPLICIT_NONE})
source_group (Transformations\\ReplaceArraysInIO FILES ${TR_REPLACE_ARRAYS_IN_IO}) source_group (Transformations\\ReplaceArraysInIO FILES ${TR_REPLACE_ARRAYS_IN_IO})
source_group (Transformations\\MoveOperators FILES ${TR_MOVE_OPERATORS}) source_group (Transformations\\MoveOperators FILES ${TR_MOVE_OPERATORS})
source_group (Transformations\\ArrayConstantPropagation FILES ${TR_ARR_PROP})
source_group (CreateIntervals FILES ${CREATE_INTER_T}) source_group (CreateIntervals FILES ${CREATE_INTER_T})
@@ -486,7 +486,6 @@ source_group (LoopAnalyzer FILES ${LOOP_ANALYZER})
source_group (ParallelizationRegions FILES ${PARALLEL_REG}) source_group (ParallelizationRegions FILES ${PARALLEL_REG})
source_group (MergeRegions FILES ${MERGE_REGIONS}) source_group (MergeRegions FILES ${MERGE_REGIONS})
source_group (PrivateAnalyzer FILES ${PRIV}) source_group (PrivateAnalyzer FILES ${PRIV})
source_group (ArrayConstantPropagation FILES ${ARRAY_PROP})
source_group (FDVM_Compiler FILES ${FDVM}) source_group (FDVM_Compiler FILES ${FDVM})
source_group (SageExtension FILES ${OMEGA}) source_group (SageExtension FILES ${OMEGA})
source_group (Utils FILES ${UTILS}) source_group (Utils FILES ${UTILS})

View File

@@ -1,380 +0,0 @@
#include "propagation.h"
#include "../Utils/SgUtils.h"
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
namespace {
struct MyHash {
size_t operator()(const SgSymbol* s) const {
return std::hash<std::string_view>{}(s->identifier());
}
};
struct MyEq {
bool operator()(const SgSymbol* a, const SgSymbol* b) const {
return std::strcmp(a->identifier(), b->identifier()) == 0;
}
};
}
SgStatement* declPlace = NULL;
unordered_set<SgStatement*> changed;
unordered_set<SgSymbol*, MyHash, MyEq> variablesToAdd;
unordered_set<SgStatement*> positionsToAdd;
unordered_set<SgStatement*> statementsToRemove;
unordered_map<string, vector<pair<SgStatement*, SgStatement*>>> expToChange;
static bool CheckConstIndexes(SgExpression* exp)
{
if (!exp)
{
return true;
}
SgExpression* lhs = exp->lhs();
SgExpression* rhs = exp->rhs();
do
{
if (lhs->variant() != INT_VAL)
{
return false;
}
if (rhs)
{
lhs = rhs->lhs();
rhs = rhs->rhs();
}
} while (rhs);
return true;
}
static SgExpression* CreateVar(int& variableNumber, SgType* type)
{
string varName = "__tmp_prop_var";
string name = varName + std::to_string(variableNumber) + "__";
variableNumber++;
SgStatement* funcStart = declPlace->controlParent();
SgSymbol* varSymbol = new SgSymbol(VARIABLE_NAME, name.c_str(), *type, *funcStart);
variablesToAdd.insert(varSymbol);
positionsToAdd.insert(declPlace);
return new SgExpression(VAR_REF, NULL, NULL, varSymbol, type->copyPtr());
}
static SgStatement* FindLastDeclStatement(SgStatement* funcStart)
{
SgStatement* endSt = funcStart->lastNodeOfStmt();
SgStatement* cur = funcStart->lexNext();
SgStatement* lastDecl = funcStart;
const set<int> declVariants = { VAR_DECL, VAR_DECL_90, ALLOCATABLE_STMT, DIM_STAT,
EXTERN_STAT, COMM_STAT, HPF_TEMPLATE_STAT, DVM_VAR_DECL, STRUCT_DECL };
while (cur && cur != endSt)
{
if (cur->variant() == INTERFACE_STMT)
cur = cur->lastNodeOfStmt();
if (declVariants.find(cur->variant()) != declVariants.end())
lastDecl = cur;
else if (isSgExecutableStatement(cur))
break;
cur = cur->lexNext();
}
return lastDecl;
}
static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const unordered_set<SgSymbol*, MyHash, MyEq>& symbols)
{
if (symbols.empty())
return;
const string commonBlockName = "__propagation_common__";
SgStatement* funcEnd = funcStart->lastNodeOfStmt();
SgStatement* commonStat = NULL;
SgExpression* commonList = NULL;
for (SgStatement* cur = funcStart->lexNext();
cur && cur != funcEnd; cur = cur->lexNext())
{
if (cur->variant() != COMM_STAT)
continue;
for (SgExpression* exp = cur->expr(0); exp; exp = exp->rhs())
{
if (exp->variant() != COMM_LIST)
continue;
const char* id = exp->symbol() ? exp->symbol()->identifier() : NULL;
string existingName = id ? string(id) : string("spf_unnamed");
if (existingName == commonBlockName)
{
commonStat = cur;
commonList = exp;
break;
}
}
if (commonStat)
break;
}
vector<SgExpression*> varRefs;
for (SgSymbol* sym : symbols)
{
if (!sym || sym->variant() != VARIABLE_NAME || string(sym->identifier()) == commonBlockName)
continue;
SgSymbol* symToAdd = new SgSymbol(VARIABLE_NAME, sym->identifier(), *sym->type(), *funcStart);
varRefs.push_back(new SgVarRefExp(symToAdd));
}
SgExpression* varList = makeExprList(varRefs, false);
SgStatement* insertAfter = FindLastDeclStatement(funcStart);
for (SgSymbol* sym : symbols)
{
SgStatement* declStmt = sym->makeVarDeclStmt();
if (!declStmt)
continue;
if (SgVarDeclStmt* vds = isSgVarDeclStmt(declStmt))
vds->setVariant(VAR_DECL_90);
declStmt->setFileName(funcStart->fileName());
declStmt->setFileId(funcStart->getFileId());
declStmt->setProject(funcStart->getProject());
declStmt->setlineNumber(getNextNegativeLineNumber());
insertAfter->insertStmtAfter(*declStmt, *funcStart);
insertAfter = declStmt;
statementsToRemove.insert(declStmt);
}
if (!commonList)
{
SgSymbol* commonSymbol = new SgSymbol(COMMON_NAME, commonBlockName.c_str());
commonList = new SgExpression(COMM_LIST, varList, NULL, commonSymbol);
commonStat = new SgStatement(COMM_STAT);
commonStat->setFileName(funcStart->fileName());
commonStat->setFileId(funcStart->getFileId());
commonStat->setProject(funcStart->getProject());
commonStat->setlineNumber(getNextNegativeLineNumber());
commonStat->setExpression(0, commonList);
SgStatement* lastDecl = FindLastDeclStatement(funcStart);
lastDecl->insertStmtAfter(*commonStat, *funcStart);
statementsToRemove.insert(commonStat);
}
else
{
commonList->setLhs(varList);
}
}
static void TransformRightPart(SgStatement* st, SgExpression* exp, unordered_map<string, SgExpression*>& arrayToVariable, int& variableNumber)
{
if (!exp)
return;
vector<SgExpression*> subnodes = { exp->lhs(), exp->rhs() };
string expUnparsed;
SgExpression* toAdd = NULL;
if (exp->variant() == ARRAY_REF && CheckConstIndexes(exp->lhs()))
{
expUnparsed = exp->unparse();
if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp->symbol()->type()->baseType())
{
arrayToVariable[expUnparsed] = CreateVar(variableNumber, exp->symbol()->type()->baseType());
}
positionsToAdd.insert(declPlace);
SgSymbol* builder = arrayToVariable[expUnparsed]->symbol();
auto* sym = new SgSymbol(builder->variant(), builder->identifier(), builder->type(), st->controlParent());
auto* newVarExp = new SgVarRefExp(sym);
expToChange[st->fileName()].push_back({ st , st->copyPtr() });
st->setExpression(1, newVarExp);
return;
}
for (int i = 0; i < 2; i++)
{
if (subnodes[i] && subnodes[i]->variant() == ARRAY_REF && subnodes[i]->symbol()->type()->baseType() && CheckConstIndexes(subnodes[i]->lhs()))
{
expUnparsed = subnodes[i]->unparse();
if (arrayToVariable.find(expUnparsed) == arrayToVariable.end())
arrayToVariable[expUnparsed] = CreateVar(variableNumber, subnodes[i]->symbol()->type()->baseType());
positionsToAdd.insert(declPlace);
SgSymbol* builder = arrayToVariable[expUnparsed]->symbol();
auto* sym = new SgSymbol(builder->variant(), builder->identifier(), builder->type(), st->controlParent());
toAdd = new SgVarRefExp(sym);
if (toAdd)
{
if (i == 0)
{
expToChange[st->fileName()].push_back({ st , st->copyPtr() });;
exp->setLhs(toAdd);
}
else
{
expToChange[st->fileName()].push_back({ st , st->copyPtr() });;
exp->setRhs(toAdd);
}
}
}
else
TransformRightPart(st, subnodes[i], arrayToVariable, variableNumber);
}
}
static void TransformLeftPart(SgStatement* st, SgExpression* exp, unordered_map<string, SgExpression*>& arrayToVariable, int& variableNumber)
{
if (exp->symbol()->type()->variant() == T_STRING)
return;
if (changed.find(st) != changed.end())
return;
string expUnparsed = exp->unparse();
if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp->symbol()->type()->baseType())
{
arrayToVariable[expUnparsed] = CreateVar(variableNumber, exp->symbol()->type()->baseType());
}
positionsToAdd.insert(declPlace);
SgStatement* newStatement = new SgStatement(ASSIGN_STAT, NULL, NULL, arrayToVariable[expUnparsed]->copyPtr(), st->expr(1)->copyPtr(), NULL);
newStatement->setFileId(st->getFileId());
newStatement->setProject(st->getProject());
newStatement->setlineNumber(getNextNegativeLineNumber());
newStatement->setLocalLineNumber(st->lineNumber());
st->insertStmtBefore(*newStatement, *st->controlParent());
changed.insert(st);
statementsToRemove.insert(newStatement);
}
static void TransformBorder(SgStatement* st, SgExpression* exp, unordered_map<string, SgExpression*>& arrayToVariable, int& variableNumber)
{
SgStatement* firstStatement = declPlace->lexPrev();
st = st->lexPrev();
string array = exp->unparse();
if (arrayToVariable.find(array) == arrayToVariable.end())
arrayToVariable[array] = CreateVar(variableNumber, exp->symbol()->type()->baseType());
while (st != firstStatement)
{
if (st->variant() == ASSIGN_STAT && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end())
{
if (st->expr(1))
{
TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber);
}
if (st->expr(0) && st->expr(0)->variant() == ARRAY_REF && CheckConstIndexes(st->expr(0)->lhs()) && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end())
TransformLeftPart(st, st->expr(0), arrayToVariable, variableNumber);
}
st = st->lexPrev();
}
}
static void CheckVariable(SgStatement* st, SgExpression* exp, unordered_map<string, SgExpression*>& arrayToVariable, int& variableNumber)
{
SgStatement* firstStatement = declPlace->lexPrev();
st = st->lexPrev();
string varName = exp->unparse();
while (st != firstStatement)
{
if (st->variant() == ASSIGN_STAT && st->expr(0)->symbol() == exp->symbol())
{
TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber);
positionsToAdd.insert(declPlace);
}
if (st->variant() == ASSIGN_STAT && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end())
{
if (st->expr(1))
{
TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber);
positionsToAdd.insert(declPlace);
}
if (st->expr(0) && st->expr(0)->variant() == ARRAY_REF && CheckConstIndexes(st->expr(0)->lhs()) && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end())
{
TransformLeftPart(st, st->expr(0), arrayToVariable, variableNumber);
positionsToAdd.insert(declPlace);
}
}
st = st->lexPrev();
}
}
void arrayConstantPropagation(SgProject& project)
{
unordered_map<string, SgExpression*> arrayToVariable;
int variableNumber = 0;
for (int i = 0; i < project.numberOfFiles(); i++)
{
SgFile* file = &(project.file(i));
if (!file)
continue;
SgFile::switchToFile(file->filename());
const int funcNum = file->numberOfFunctions();
for (int i = 0; i < funcNum; ++i)
{
SgStatement* st = file->functions(i);
declPlace = st->lexNext();
SgStatement* lastNode = st->lastNodeOfStmt();
for (; st != lastNode; st = st->lexNext())
{
if (st->variant() == FOR_NODE)
{
SgExpression* lowerBound = st->expr(0)->lhs();
SgExpression* upperBound = st->expr(0)->rhs();
SgStatement* boundCopy = NULL;
string lowerBoundUnparsed = lowerBound->unparse(), upperBoundUnparsed = upperBound->unparse();
if (upperBound->variant() == ARRAY_REF && upperBound->symbol()->type()->baseType() && CheckConstIndexes(upperBound->lhs()))
{
boundCopy = st->copyPtr();
TransformBorder(st, upperBound, arrayToVariable, variableNumber);
st->expr(0)->setRhs(arrayToVariable[upperBoundUnparsed]->copyPtr());
expToChange[st->fileName()].push_back({ st ,boundCopy });;
positionsToAdd.insert(declPlace);
}
else if (upperBound->variant() == VAR_REF)
CheckVariable(st, upperBound, arrayToVariable, variableNumber);
if (lowerBound->variant() == ARRAY_REF && lowerBound->symbol()->type()->baseType() && CheckConstIndexes(lowerBound->lhs()))
{
boundCopy = st->copyPtr();
TransformBorder(st, lowerBound, arrayToVariable, variableNumber);
st->expr(0)->setLhs(arrayToVariable[lowerBoundUnparsed]->copyPtr());
expToChange[st->fileName()].push_back({ st , boundCopy });;
positionsToAdd.insert(declPlace);
}
else if (lowerBound->variant() == VAR_REF)
CheckVariable(st, lowerBound, arrayToVariable, variableNumber);
}
}
}
}
unordered_set<SgStatement*> funcStarts;
for (SgStatement* st : positionsToAdd)
{
SgFile::switchToFile(st->fileName());
SgStatement* scope = st->controlParent();
if (scope)
funcStarts.insert(scope);
}
for (const auto& st : funcStarts)
{
SgFile::switchToFile(st->fileName());
InsertCommonAndDeclsForFunction(st, variablesToAdd);
}
}

View File

@@ -1,20 +0,0 @@
#pragma once
#include "../Utils/SgUtils.h"
#include <string>
#include <vector>
using namespace std;
struct ExprRestoreEntry
{
enum Kind { kStatementExpr, kExprChild };
Kind kind;
SgStatement* stmt;
int stmtExprIndex;
SgExpression* parent;
bool childIsRhs;
SgExpression* savedCopy;
};
void arrayConstantPropagation(SgProject& project);

View File

@@ -1,36 +1,34 @@
#include <algorithm> #include <algorithm>
#include <map> #include <map>
#include <unordered_set> #include <set>
#include <unordered_map>
#include <vector> #include <vector>
#include <queue> #include <queue>
#include <numeric> #include <numeric>
#include <iostream> #include <iostream>
#include "ArrayConstantPropagation/propagation.h"
#include "CFGraph/CFGraph.h" #include "CFGraph/CFGraph.h"
#include "Distribution/Array.h" #include "Distribution/Array.h"
#include "errors.h"
#include "graph_loops.h" #include "graph_loops.h"
#include "private_arrays_search.h" #include "private_arrays_search.h"
#include "range_structures.h" #include "range_structures.h"
#include "region.h" #include "region.h"
#include "SgUtils.h" #include "SgUtils.h"
#include "Transformations/ArrayConstantPropagation/propagation.h"
#include "utils.h" #include "utils.h"
#include "Utils/AstWrapper.h" #include "Utils/AstWrapper.h"
using namespace std; using namespace std;
extern std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>> declaredArrays; extern std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>> declaredArrays;
extern map<string, vector<Messages>> SPF_messages;
extern unordered_set<SgStatement*> statementsToRemove; static set<Region*> collapsed;
extern unordered_map<string, vector<pair<SgStatement*, SgStatement*>>> expToChange;
static unordered_set<Region*> collapsed;
static void RemoveEmptyPoints(ArrayAccessingIndexes& container) static void RemoveEmptyPoints(ArrayAccessingIndexes& container)
{ {
ArrayAccessingIndexes resultContainer; ArrayAccessingIndexes resultContainer;
unordered_set<string> toRemove; set<string> toRemove;
for (auto& [arrayName, accessingSet] : container) for (auto& [arrayName, accessingSet] : container)
{ {
@@ -60,6 +58,7 @@ static void Collapse(Region* region)
return; return;
bool firstRegion = true; bool firstRegion = true;
int blockCount = 0;
for (Region* basickBlock : region->getBasickBlocks()) for (Region* basickBlock : region->getBasickBlocks())
{ {
if (basickBlock->getNextRegions().empty()) if (basickBlock->getNextRegions().empty())
@@ -71,7 +70,7 @@ static void Collapse(Region* region)
} }
else else
{ {
unordered_set<string> toErease; set<string> toErease;
for (auto& [arrayName, arrayRanges] : region->array_def) for (auto& [arrayName, arrayRanges] : region->array_def)
{ {
if (basickBlock->array_out.find(arrayName) != basickBlock->array_out.end()) if (basickBlock->array_out.find(arrayName) != basickBlock->array_out.end())
@@ -91,11 +90,15 @@ static void Collapse(Region* region)
RegionInstruction instruction; RegionInstruction instruction;
instruction.def = move(region->array_def); instruction.def = move(region->array_def);
ArrayAccessingIndexes recursivePriv;
for (auto& byBlock : region->getBasickBlocks()) for (auto& byBlock : region->getBasickBlocks())
{ {
if (!byBlock->array_priv.empty())
recursivePriv = byBlock->array_priv;
for (auto& instruction : byBlock->instructions) for (auto& instruction : byBlock->instructions)
{ {
if (!instruction.def.empty() || !instruction.use.empty())
blockCount++;
for (auto& [arrayName, _] : instruction.use) for (auto& [arrayName, _] : instruction.use)
{ {
AccessingSet diff = instruction.use[arrayName].Diff(instruction.in[arrayName]); AccessingSet diff = instruction.use[arrayName].Diff(instruction.in[arrayName]);
@@ -127,67 +130,8 @@ static void Collapse(Region* region)
region->addNextRegion(nextBlock); region->addNextRegion(nextBlock);
} }
region->instructions.push_back(instruction); region->instructions.push_back(instruction);
if (blockCount == 1 && !recursivePriv.empty())
} region->array_priv = move(recursivePriv);
static void SolveDataFlowIteratively(Region* DFG)
{
auto blocks = DFG->getBasickBlocks();
std::unordered_set<Region*> worklist(blocks.begin(), blocks.end());
do
{
Region* b = *worklist.begin();
ArrayAccessingIndexes newIn;
bool flagFirst = true;
for (Region* prevBlock : b->getPrevRegions())
{
if (flagFirst)
{
newIn = prevBlock->array_out;
flagFirst = false;
}
else
{
if (prevBlock->array_out.empty())
{
newIn.clear();
break;
}
for (const auto& [arrayName, accessSet] : prevBlock->array_out)
{
if (newIn.find(arrayName) != newIn.end())
newIn[arrayName] = newIn[arrayName].Intersect(accessSet);
else
newIn[arrayName] = AccessingSet();
}
}
}
b->array_in = move(newIn);
ArrayAccessingIndexes newOut;
if (b->array_def.empty())
newOut = b->array_in;
else if (b->array_in.empty())
newOut = b->array_def;
else
{
for (auto& [arrayName, accessSet] : b->array_def)
{
if (newOut.find(arrayName) != newOut.end())
newOut[arrayName] = b->array_def[arrayName].Union(b->array_in[arrayName]);
else
newOut[arrayName] = accessSet;
}
}
/* can not differ */
if (newOut != b->array_out)
b->array_out = newOut;
else
worklist.erase(b);
} while (!worklist.empty());
} }
static void SolveForBasickBlock(Region* block) static void SolveForBasickBlock(Region* block)
@@ -380,11 +324,26 @@ static void AddPrivateArraysToLoop(LoopGraph* loop, const ArrayAccessingIndexes&
spfStat->setFileName(loop->loop->fileName()); spfStat->setFileName(loop->loop->fileName());
SgExpression* toAdd = new SgExpression(EXPR_LIST, new SgExpression(ACC_PRIVATE_OP), NULL, NULL); SgExpression* toAdd = new SgExpression(EXPR_LIST, new SgExpression(ACC_PRIVATE_OP), NULL, NULL);
set<SgSymbol*> arraysToInsert; set<SgSymbol*> arraysToInsert;
std::cout << "First bp\n"; for (const auto& [arrayName, accessingSet] : privates)
for (const auto& [_, accessingSet] : privates)
{ {
int idx = arrayName.find('%');
string name = (idx != -1 ? arrayName.substr(idx+1) : arrayName);
if (!CheckDimensionLength(accessingSet)) if (!CheckDimensionLength(accessingSet))
{
wstring messageE, messageR;
__spf_printToLongBuf(
messageE,
L"Private array '%s' was skipped because dimension lengths are inconsistent",
to_wstring(name).c_str());
__spf_printToLongBuf(
messageR,
R159,
to_wstring("array " + name + " has inconsistent dimension lengths").c_str());
SPF_messages[loop->loop->fileName()].push_back(
Messages(WARR, loop->loop->lineNumber(), messageR, messageE, 1029));
continue; continue;
}
for (const auto& arrayElement : accessingSet.GetElements()) for (const auto& arrayElement : accessingSet.GetElements())
{ {
if (arrayElement.empty()) if (arrayElement.empty())
@@ -462,20 +421,4 @@ void findPrivateArrays(map<string, vector<LoopGraph*>>& loopGraph, map<FuncInfo*
AddPrivateArraysToLoop(loop, result[loop], insertedPrivates); AddPrivateArraysToLoop(loop, result[loop], insertedPrivates);
} }
} }
for (SgStatement* st : statementsToRemove)
{
SgFile::switchToFile(st->fileName());
st->deleteStmt();
}
for (auto& [filename, statements] : expToChange)
{
SgFile::switchToFile(filename);
for (auto& [statement, statementCopy] : statements)
{
statement->insertStmtBefore(*statementCopy, *statement->controlParent());
statement->deleteStmt();
}
}
} }

View File

@@ -3,7 +3,7 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include <set> #include <set>
#include <unordered_set> #include <set>
#include "range_structures.h" #include "range_structures.h"
#include "graph_loops.h" #include "graph_loops.h"

View File

@@ -1,6 +1,6 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include <unordered_set> #include <set>
#include <string> #include <string>
#include "utils.h" #include "utils.h"

View File

@@ -2,7 +2,7 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include <unordered_set> #include <set>
#include <string> #include <string>
#include <cstdint> #include <cstdint>

View File

@@ -1,8 +1,8 @@
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include <map> #include <map>
#include <unordered_set> #include <set>
#include <unordered_map> #include <map>
#include <string> #include <string>
#include <numeric> #include <numeric>
#include <iostream> #include <iostream>
@@ -23,9 +23,9 @@ static bool isParentStmt(SgStatement* stmt, SgStatement* parent)
} }
/*returns head block and loop*/ /*returns head block and loop*/
static pair<SAPFOR::BasicBlock*, unordered_set<SAPFOR::BasicBlock*>> GetBasicBlocksForLoop(const LoopGraph* loop, const vector<SAPFOR::BasicBlock*> blocks) pair<SAPFOR::BasicBlock*, set<SAPFOR::BasicBlock*>> GetBasicBlocksForLoop(const LoopGraph* loop, const vector<SAPFOR::BasicBlock*> blocks)
{ {
unordered_set<SAPFOR::BasicBlock*> block_loop; set<SAPFOR::BasicBlock*> block_loop;
SAPFOR::BasicBlock* head_block = nullptr; SAPFOR::BasicBlock* head_block = nullptr;
auto loop_operator = loop->loop->GetOriginal(); auto loop_operator = loop->loop->GetOriginal();
for (const auto& block : blocks) for (const auto& block : blocks)
@@ -51,16 +51,16 @@ static pair<SAPFOR::BasicBlock*, unordered_set<SAPFOR::BasicBlock*>> GetBasicBlo
return { head_block, block_loop }; return { head_block, block_loop };
} }
static void BuildLoopIndex(map<string, LoopGraph*>& loopForIndex, LoopGraph* loop) { static void BuildLoopIndex(map<SgStatement*, LoopGraph*>& loopForIndex, LoopGraph* loop) {
string index = loop->loopSymbol(); string index = loop->loopSymbol();
loopForIndex[index] = loop; loopForIndex[loop->loop->GetOriginal()] = loop;
for (const auto& childLoop : loop->children) for (const auto& childLoop : loop->children)
BuildLoopIndex(loopForIndex, childLoop); BuildLoopIndex(loopForIndex, childLoop);
} }
static string FindIndexName(int pos, SAPFOR::BasicBlock* block, map<string, LoopGraph*>& loopForIndex) { static string FindIndexName(int pos, SAPFOR::BasicBlock* block, map<string, LoopGraph*>& loopForIndex) {
unordered_set<SAPFOR::Argument*> args = { block->getInstructions()[pos]->getInstruction()->getArg1() }; set<SAPFOR::Argument*> args = { block->getInstructions()[pos]->getInstruction()->getArg1() };
for (int i = pos - 1; i >= 0; i--) for (int i = pos - 1; i >= 0; i--)
{ {
@@ -95,7 +95,7 @@ static string FindIndexName(int pos, SAPFOR::BasicBlock* block, map<string, Loop
static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAccessingIndexes& def, ArrayAccessingIndexes& use, Region* region) { static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAccessingIndexes& def, ArrayAccessingIndexes& use, Region* region) {
auto instructions = block->getInstructions(); auto instructions = block->getInstructions();
map<string, LoopGraph*> loopForIndex; map<SgStatement*, LoopGraph*> loopForIndex;
BuildLoopIndex(loopForIndex, loop); BuildLoopIndex(loopForIndex, loop);
for (int i = 0; i < instructions.size(); i++) for (int i = 0; i < instructions.size(); i++)
{ {
@@ -136,7 +136,6 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces
vector<SAPFOR::Argument*> index_vars; vector<SAPFOR::Argument*> index_vars;
vector<int> refPos; vector<int> refPos;
string array_name = instruction->getInstruction()->getArg1()->getValue(); string array_name = instruction->getInstruction()->getArg1()->getValue();
int j = i - 1; int j = i - 1;
while (j >= 0 && instructions[j]->getInstruction()->getOperation() == SAPFOR::CFG_OP::REF) while (j >= 0 && instructions[j]->getInstruction()->getOperation() == SAPFOR::CFG_OP::REF)
{ {
@@ -180,25 +179,16 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces
string name, full_name = var->getValue(); string name, full_name = var->getValue();
int pos = full_name.find('%'); int pos = full_name.find('%');
LoopGraph* currentLoop; LoopGraph* currentLoop;
if (pos != -1)
{
name = full_name.substr(pos + 1);
if (loopForIndex.find(name) != loopForIndex.end())
currentLoop = loopForIndex[name];
else
return -1;
}
else
{
name = FindIndexName(currentVarPos, block, loopForIndex);
if (name == "")
return -1;
if (loopForIndex.find(name) != loopForIndex.end()) auto serachInstr = instruction->getInstruction()->getOperator();
currentLoop = loopForIndex[name]; while (serachInstr && serachInstr->variant() != FOR_NODE)
else serachInstr = serachInstr->controlParent();
return -1;
} name = full_name.substr(pos + 1);
if (loopForIndex.find(serachInstr) != loopForIndex.end())
currentLoop = loopForIndex[serachInstr];
else
return -1;
uint64_t start = coeffsForDims.back().second * currentLoop->startVal + coeffsForDims.back().first; uint64_t start = coeffsForDims.back().second * currentLoop->startVal + coeffsForDims.back().first;
uint64_t step = currentLoop->stepVal; uint64_t step = currentLoop->stepVal;
@@ -243,7 +233,7 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces
} }
static void RemoveHeaderConnection(SAPFOR::BasicBlock* header, const unordered_set<SAPFOR::BasicBlock*>& blockSet, unordered_map<SAPFOR::BasicBlock*, Region*>& bbToRegion) static void RemoveHeaderConnection(SAPFOR::BasicBlock* header, const set<SAPFOR::BasicBlock*>& blockSet, map<SAPFOR::BasicBlock*, Region*>& bbToRegion)
{ {
for (SAPFOR::BasicBlock* block : blockSet) for (SAPFOR::BasicBlock* block : blockSet)
{ {
@@ -259,18 +249,35 @@ static void RemoveHeaderConnection(SAPFOR::BasicBlock* header, const unordered_s
} }
} }
static void DFS(Region* block, vector<Region*>& result, unordered_set<Region*> cycleBlocks) static bool DFS(Region* block,
vector<Region*>& result,
const set<Region*>& cycleBlocks,
map<Region*, int>& color)
{ {
auto it = color.find(block);
if (it != color.end())
{
if (it->second == 0)
return false;
if (it->second == 1)
return true;
}
color[block] = 0;
for (Region* nextBlock : block->getNextRegions()) for (Region* nextBlock : block->getNextRegions())
{ {
if (cycleBlocks.find(nextBlock) != cycleBlocks.end()) if (cycleBlocks.find(nextBlock) == cycleBlocks.end())
DFS(nextBlock, result, cycleBlocks); continue;
if (!DFS(nextBlock, result, cycleBlocks, color))
return false;
} }
color[block] = 1;
result.push_back(block); result.push_back(block);
return true;
} }
bool HasCycle(Region* block, const std::unordered_set<Region*>& cycleBlocks, std::unordered_set<Region*>& visitedBlocks) bool HasCycle(Region* block, const std::set<Region*>& cycleBlocks, std::set<Region*>& visitedBlocks)
{ {
return false;
if (visitedBlocks.find(block) != visitedBlocks.end()) if (visitedBlocks.find(block) != visitedBlocks.end())
return true; return true;
visitedBlocks.insert(block); visitedBlocks.insert(block);
@@ -284,18 +291,17 @@ bool HasCycle(Region* block, const std::unordered_set<Region*>& cycleBlocks, std
bool TopologySort(std::vector<Region*>& basikBlocks, Region* header) bool TopologySort(std::vector<Region*>& basikBlocks, Region* header)
{ {
unordered_set<Region*> cycleBlocks(basikBlocks.begin(), basikBlocks.end()); set<Region*> cycleBlocks(basikBlocks.begin(), basikBlocks.end());
unordered_set<Region*> visitedBlocks;
if (HasCycle(header, cycleBlocks, visitedBlocks))
return false;
vector<Region*> result; vector<Region*> result;
DFS(header, result, cycleBlocks); map<Region*, int> color;
if (!DFS(header, result, cycleBlocks, color))
return false;
reverse(result.begin(), result.end()); reverse(result.begin(), result.end());
basikBlocks = move(result); basikBlocks = move(result);
return true; return true;
} }
static void SetConnections(unordered_map<SAPFOR::BasicBlock*, Region*>& bbToRegion, const unordered_set<SAPFOR::BasicBlock*>& blockSet) static void SetConnections(map<SAPFOR::BasicBlock*, Region*>& bbToRegion, const set<SAPFOR::BasicBlock*>& blockSet)
{ {
for (SAPFOR::BasicBlock* block : blockSet) for (SAPFOR::BasicBlock* block : blockSet)
{ {
@@ -309,7 +315,7 @@ static void SetConnections(unordered_map<SAPFOR::BasicBlock*, Region*>& bbToRegi
} }
} }
static Region* CreateSubRegion(LoopGraph* loop, const vector<SAPFOR::BasicBlock*>& Blocks, unordered_map<SAPFOR::BasicBlock*, Region*>& bbToRegion) static Region* CreateSubRegion(LoopGraph* loop, const vector<SAPFOR::BasicBlock*>& Blocks, map<SAPFOR::BasicBlock*, Region*>& bbToRegion)
{ {
Region* region = new Region; Region* region = new Region;
auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks);
@@ -340,7 +346,7 @@ static Region* CreateSubRegion(LoopGraph* loop, const vector<SAPFOR::BasicBlock*
Region::Region(LoopGraph* loop, const vector<SAPFOR::BasicBlock*>& Blocks) Region::Region(LoopGraph* loop, const vector<SAPFOR::BasicBlock*>& Blocks)
{ {
auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks);
unordered_map<SAPFOR::BasicBlock*, Region*> bbToRegion; map<SAPFOR::BasicBlock*, Region*> bbToRegion;
for (auto poiner : blockSet) for (auto poiner : blockSet)
{ {
bbToRegion[poiner] = new Region(*poiner); bbToRegion[poiner] = new Region(*poiner);

View File

@@ -2,7 +2,7 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include <unordered_set> #include <set>
#include <string> #include <string>
#include "graph_loops.h" #include "graph_loops.h"
@@ -29,9 +29,9 @@ public:
void addBasickBlocks(Region* region) { basickBlocks.push_back(region); } void addBasickBlocks(Region* region) { basickBlocks.push_back(region); }
const std::unordered_set<Region*>& getPrevRegions() { return prevRegions; } const std::set<Region*>& getPrevRegions() { return prevRegions; }
std::unordered_set<Region*>& getNextRegions() { return nextRegions; } std::set<Region*>& getNextRegions() { return nextRegions; }
void removeNextRegion(Region* region) void removeNextRegion(Region* region)
{ {
@@ -61,7 +61,7 @@ public:
nextRegions.insert(source); nextRegions.insert(source);
} }
std::unordered_set<Region*> getSubRegions() { return subRegions; } std::set<Region*> getSubRegions() { return subRegions; }
void addSubRegions(Region* region) { subRegions.insert(region); } void addSubRegions(Region* region) { subRegions.insert(region); }
@@ -71,14 +71,14 @@ public:
private: private:
std::vector<Region*> basickBlocks; std::vector<Region*> basickBlocks;
std::unordered_set<Region*> subRegions; std::set<Region*> subRegions;
/*next Region which is BB for current BB Region*/ /*next Region which is BB for current BB Region*/
std::unordered_set<Region*> nextRegions; std::set<Region*> nextRegions;
/*prev Regions which is BBs for current BB Region*/ /*prev Regions which is BBs for current BB Region*/
std::unordered_set<Region*> prevRegions; std::set<Region*> prevRegions;
Region* header; Region* header;
}; };
bool HasCycle(Region* block, const std::unordered_set<Region*>& cycleBlocks, std::unordered_set<Region*>& visitedBlocks); bool HasCycle(Region* block, const std::set<Region*>& cycleBlocks, std::set<Region*>& visitedBlocks);
bool TopologySort(std::vector<Region*>& basikBlocks, Region* header); bool TopologySort(std::vector<Region*>& basikBlocks, Region* header);

View File

@@ -1910,6 +1910,8 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
mergeRegions(parallelRegions, allFuncInfo); mergeRegions(parallelRegions, allFuncInfo);
else if (curr_regime == ARRAY_PROPAGATION) else if (curr_regime == ARRAY_PROPAGATION)
arrayConstantPropagation(project); arrayConstantPropagation(project);
else if (curr_regime == ARRAY_PROPAGATION_RESTORE)
restoreArrays();
const float elapsed = duration_cast<milliseconds>(high_resolution_clock::now() - timeForPass).count() / 1000.; const float elapsed = duration_cast<milliseconds>(high_resolution_clock::now() - timeForPass).count() / 1000.;
const float elapsedGlobal = duration_cast<milliseconds>(high_resolution_clock::now() - globalTime).count() / 1000.; const float elapsedGlobal = duration_cast<milliseconds>(high_resolution_clock::now() - globalTime).count() / 1000.;

View File

@@ -192,6 +192,7 @@ enum passes {
TRANSFORM_ASSUMED_SIZE_PARAMETERS, TRANSFORM_ASSUMED_SIZE_PARAMETERS,
ARRAY_PROPAGATION, ARRAY_PROPAGATION,
ARRAY_PROPAGATION_RESTORE,
TEST_PASS, TEST_PASS,
EMPTY_PASS EMPTY_PASS
@@ -384,7 +385,7 @@ static void setPassValues()
passNames[TRANSFORM_ASSUMED_SIZE_PARAMETERS] = "TRANSFORM_ASSUMED_SIZE_PARAMETERS"; passNames[TRANSFORM_ASSUMED_SIZE_PARAMETERS] = "TRANSFORM_ASSUMED_SIZE_PARAMETERS";
passNames[ARRAY_PROPAGATION] = "ARRAY_PROPAGATION"; passNames[ARRAY_PROPAGATION] = "ARRAY_PROPAGATION";
passNames[ARRAY_PROPAGATION_RESTORE] = "ARRAY_PROPAGATION_RESTORE";
passNames[TEST_PASS] = "TEST_PASS"; passNames[TEST_PASS] = "TEST_PASS";
} }

View File

@@ -0,0 +1,631 @@
#include "propagation.h"
#include "../Utils/SgUtils.h"
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
static SgStatement* declPlace = NULL;
static set<SgStatement*> changed;
static map<string, SgSymbol*> variablesToAdd;
static map<string, set<SgStatement*>> positionsToAdd;
static map<string, string> arrayToName;
static set<SgStatement*> statementsToRemove;
static map<string, map<SgStatement*, SgStatement*>> expToChange;
static bool CheckConstIndexes(SgExpression* exp)
{
if (!exp)
{
return false;
}
SgExpression* lhs = exp->lhs();
SgExpression* rhs = exp->rhs();
do
{
if (lhs && lhs->variant() != INT_VAL)
{
return false;
}
if (rhs)
{
lhs = rhs->lhs();
rhs = rhs->rhs();
}
} while (rhs);
return true;
}
static SgExpression* CreateVar(int& variableNumber, SgType* type)
{
string varName = "tmp_prop_var";
string name = varName + std::to_string(variableNumber) + "__";
variableNumber++;
SgStatement* funcStart = declPlace->controlParent();
SgSymbol* varSymbol = new SgSymbol(VARIABLE_NAME, name.c_str(), SgTypeInt(), funcStart);
variablesToAdd[name] = varSymbol;
positionsToAdd[string(declPlace->fileName())].insert(declPlace);
return new SgExpression(VAR_REF, NULL, NULL, varSymbol, type->copyPtr());
}
static SgStatement* FindLastDeclStatement(SgStatement* funcStart)
{
if (!funcStart)
return NULL;
SgStatement* endSt = funcStart->lastNodeOfStmt();
SgStatement* cur = funcStart->lexNext();
SgStatement* lastDecl = funcStart;
const set<int> declVariants = { VAR_DECL, VAR_DECL_90, ALLOCATABLE_STMT, DIM_STAT,
EXTERN_STAT, COMM_STAT, HPF_TEMPLATE_STAT, DVM_VAR_DECL, STRUCT_DECL };
while (cur && cur != endSt)
{
if (cur->variant() == INTERFACE_STMT)
cur = cur->lastNodeOfStmt();
if (declVariants.find(cur->variant()) != declVariants.end())
lastDecl = cur;
else if (isSgExecutableStatement(cur))
break;
cur = cur->lexNext();
}
return lastDecl;
}
static void InsertCommonAndDeclsForFunction(SgStatement* funcStart, const map<string, SgSymbol*>& symbols)
{
if (symbols.empty())
return;
if (!funcStart)
return;
const string commonBlockName = "propagation_common__";
SgStatement* funcEnd = funcStart->lastNodeOfStmt();
SgStatement* commonStat = NULL;
SgExpression* commonList = NULL;
for (SgStatement* cur = funcStart->lexNext();
cur && cur != funcEnd; cur = cur->lexNext())
{
if (cur->variant() != COMM_STAT)
continue;
for (SgExpression* exp = cur->expr(0); exp; exp = exp->rhs())
{
if (exp->variant() != COMM_LIST)
continue;
const char* id = exp->symbol() ? exp->symbol()->identifier() : NULL;
string existingName = id ? string(id) : string("spf_unnamed");
if (existingName == commonBlockName)
{
commonStat = cur;
commonList = exp;
break;
}
}
if (commonStat)
break;
}
vector<SgExpression*> varRefs;
for (const auto& [name, sym] : symbols)
{
if (!sym || sym->variant() != VARIABLE_NAME || string(sym->identifier()) == commonBlockName)
continue;
SgSymbol* symToAdd = new SgSymbol(VARIABLE_NAME, name.c_str(), SgTypeInt(), funcStart);
varRefs.push_back(new SgVarRefExp(symToAdd));
}
SgExpression* varList = makeExprList(varRefs, false);
SgStatement* insertAfter = FindLastDeclStatement(funcStart);
for (const auto& [name, sym] : symbols)
{
if (!sym)
continue;
SgStatement* declStmt = sym->makeVarDeclStmt();
if (!declStmt)
continue;
if (SgVarDeclStmt* vds = isSgVarDeclStmt(declStmt))
vds->setVariant(VAR_DECL_90);
declStmt->setFileName(funcStart->fileName());
declStmt->setFileId(funcStart->getFileId());
declStmt->setProject(funcStart->getProject());
declStmt->setlineNumber(getNextNegativeLineNumber());
insertAfter->insertStmtAfter(*declStmt, *funcStart);
insertAfter = declStmt;
statementsToRemove.insert(declStmt);
}
if (!commonList)
{
SgSymbol* commonSymbol = new SgSymbol(COMMON_NAME, commonBlockName.c_str());
commonList = new SgExpression(COMM_LIST, varList, NULL, commonSymbol);
commonStat = new SgStatement(COMM_STAT);
commonStat->setFileName(funcStart->fileName());
commonStat->setFileId(funcStart->getFileId());
commonStat->setProject(funcStart->getProject());
commonStat->setlineNumber(getNextNegativeLineNumber());
commonStat->setExpression(0, commonList);
SgStatement* lastDecl = FindLastDeclStatement(funcStart);
lastDecl->insertStmtAfter(*commonStat, *funcStart);
statementsToRemove.insert(commonStat);
}
else
{
commonList->setLhs(varList);
}
}
static void copyStatement(SgStatement* st)
{
if (!st)
return;
if (expToChange[st->fileName()].find(st) == expToChange[st->fileName()].end())
{
SgStatement* boundCopy = st->copyPtr();
for (int i = 0; i < 3; i++)
{
SgExpression* expCopy = st->expr(i);
if (expCopy)
boundCopy->setExpression(i, expCopy->copyPtr());
else
boundCopy->setExpression(i, NULL);
}
expToChange[st->fileName()][st] = boundCopy;
}
}
static bool TransformRightPart(SgStatement* st, SgExpression* exp, map<string, SgExpression*>& arrayToVariable, int& variableNumber)
{
if (!exp)
return false;
bool isChanged = false;
vector<SgExpression*> subnodes = { exp->lhs(), exp->rhs() };
string expUnparsed;
SgExpression* toAdd = NULL;
if (isArrayRef(exp) && CheckConstIndexes(exp->lhs()))
{
expUnparsed = exp->unparse();
if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp && exp->symbol() &&
exp->symbol()->type() && exp->symbol()->type()->baseType())
{
arrayToVariable[expUnparsed] = CreateVar(variableNumber, exp->symbol()->type()->baseType());
arrayToName[expUnparsed] = arrayToVariable[expUnparsed]->unparse();
}
positionsToAdd[string(declPlace->fileName())].insert(declPlace);
auto* sym = new SgSymbol(VARIABLE_NAME, arrayToName[expUnparsed].c_str(), SgTypeInt(), declPlace->controlParent());
auto* newVarExp = new SgVarRefExp(sym);
copyStatement(st);
st->setExpression(1, newVarExp);
return true;
}
for (int i = 0; i < 2; i++)
{
if (subnodes[i] && isArrayRef(subnodes[i]) && subnodes[i]->symbol() && subnodes[i]->symbol()->type() &&
subnodes[i]->symbol()->type()->baseType() && CheckConstIndexes(subnodes[i]->lhs()))
{
isChanged = true;
expUnparsed = subnodes[i]->unparse();
if (arrayToVariable.find(expUnparsed) == arrayToVariable.end())
{
arrayToVariable[expUnparsed] = CreateVar(variableNumber, subnodes[i]->symbol()->type()->baseType());
arrayToName[expUnparsed] = arrayToVariable[expUnparsed]->unparse();
}
positionsToAdd[string(declPlace->fileName())].insert(declPlace);
SgSymbol* builder = arrayToVariable[expUnparsed]->symbol();
auto* sym = new SgSymbol(VARIABLE_NAME, arrayToName[expUnparsed].c_str(), SgTypeInt(), declPlace->controlParent());
toAdd = new SgVarRefExp(sym);
if (toAdd)
{
copyStatement(st);
if (i == 0)
exp->setLhs(toAdd);
else
exp->setRhs(toAdd);
}
}
else
isChanged = isChanged || TransformRightPart(st, subnodes[i], arrayToVariable, variableNumber);
}
return isChanged;
}
static void TransformLeftPart(SgStatement* st, SgExpression* exp, map<string, SgExpression*>& arrayToVariable, int& variableNumber)
{
if (!st || !st->expr(1))
return;
if (!exp || !exp->symbol() || !exp->symbol()->type() || !exp->symbol()->type()->baseType())
return;
if (exp->symbol()->type()->variant() == T_STRING)
return;
if (changed.find(st) != changed.end())
return;
string expUnparsed = exp->unparse();
if (arrayToVariable.find(expUnparsed) == arrayToVariable.end() && exp->symbol()->type()->baseType())
{
arrayToVariable[expUnparsed] = CreateVar(variableNumber, exp->symbol()->type()->baseType());
arrayToName[expUnparsed] = arrayToVariable[expUnparsed]->unparse();
}
positionsToAdd[string(declPlace->fileName())].insert(declPlace);
auto* sym = new SgSymbol(VARIABLE_NAME, arrayToName[expUnparsed].c_str(), SgTypeInt(), declPlace->controlParent());
auto* newVarExp = new SgVarRefExp(sym);
SgStatement* newStatement = new SgStatement(ASSIGN_STAT, NULL, NULL, newVarExp, st->expr(1)->copyPtr(), NULL);
newStatement->setFileId(st->getFileId());
newStatement->setProject(st->getProject());
st->insertStmtBefore(*newStatement, *st->controlParent());
newStatement->setlineNumber(getNextNegativeLineNumber());
newStatement->setLocalLineNumber(st->lineNumber());
changed.insert(st);
statementsToRemove.insert(newStatement);
}
static void TransformBorder(SgStatement* st, SgExpression* exp, map<string, SgExpression*>& arrayToVariable, int& variableNumber)
{
if (!st || !exp)
return;
SgStatement* firstStatement = declPlace->lexPrev();
positionsToAdd[string(declPlace->fileName())].insert(declPlace);
TransformRightPart(st, exp, arrayToVariable, variableNumber);
st = st->lexPrev();
while (st &&st != firstStatement)
{
if (st->variant() == ASSIGN_STAT)
{
if (st->expr(1))
{
TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber);
}
if (st->expr(0) && isArrayRef(st->expr(0)) && CheckConstIndexes(st->expr(0)->lhs()) && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end())
TransformLeftPart(st, st->expr(0), arrayToVariable, variableNumber);
}
st = st->lexPrev();
}
}
static void CheckVariable(SgStatement* st, SgExpression* exp, map<string, SgExpression*>& arrayToVariable, int& variableNumber)
{
SgStatement* firstStatement = declPlace->lexPrev();
st = st->lexPrev();
while (st != firstStatement)
{
if (st->variant() == ASSIGN_STAT && st->expr(0)->symbol() == exp->symbol())
{
if (TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber))
{
positionsToAdd[string(declPlace->fileName())].insert(declPlace);
}
}
if (st->variant() == ASSIGN_STAT && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end())
{
if (st->expr(1))
{
if(TransformRightPart(st, st->expr(1), arrayToVariable, variableNumber))
{
positionsToAdd[string(declPlace->fileName())].insert(declPlace);
}
}
if (st->expr(0) && isArrayRef(st->expr(0)) && CheckConstIndexes(st->expr(0)->lhs()) && arrayToVariable.find(st->expr(0)->unparse()) != arrayToVariable.end())
{
TransformLeftPart(st, st->expr(0), arrayToVariable, variableNumber);
positionsToAdd[string(declPlace->fileName())].insert(declPlace);
}
}
st = st->lexPrev();
}
}
static void findConstValues(
SgProject& project,
const map<string, map<string, SgStatement*>>& borderVars,
const map<string, SgExpression*>& arrayToVariable,
map<string, int>& hitCount,
map<string, map<SgStatement*, vector<pair<string, string>>>>& result)
{
for (int i = 0; i < project.numberOfFiles(); i++)
{
SgFile* file = &(project.file(i));
if (!file)
continue;
SgFile::switchToFile(file->filename());
const int funcNum = file->numberOfFunctions();
for (int i = 0; i < funcNum; ++i)
{
SgStatement* st = file->functions(i);
SgStatement* lastNode = st->lastNodeOfStmt();
if (!st)
continue;
for (; st != lastNode; st = st->lexNext())
{
if (st && st->variant() == ASSIGN_STAT)
{
if (!st->expr(0) || !st->expr(1))
continue;
SgExpression* lhs = st->expr(0);
SgExpression* rhs = st->expr(1);
auto varIt = arrayToVariable.find(lhs->unparse());
string varName = (varIt != arrayToVariable.end()) ? varIt->second->unparse() : lhs->unparse();
if (rhs->variant() == INT_VAL)
hitCount[string(lhs->unparse())]++;
for (const auto& [filename, names] : borderVars)
{
if(names.find(string(lhs->unparse())) != names.end() && rhs->variant() == INT_VAL)
result[filename][names.at(lhs->unparse())].push_back({ varName, rhs->unparse()});
}
}
}
}
}
}
static void insertDefinition(map<string, map<SgStatement*, vector<pair<string, string>>>>& definitions, map<string, int>& hitCount)
{
for (const auto& [filename, variables] : definitions)
{
if (SgFile::switchToFile(filename) == -1)
continue;
for (const auto& [statement, values] : variables)
{
if (!statement)
continue;
SgStatement* insertBefore = statement, *st = statement;
while (st && !isSgExecutableStatement(st))
{
st = st->lexNext();
insertBefore = st;
}
for (const auto& [varName, value] : values)
{
if (hitCount.find(varName) == hitCount.end() || hitCount[varName] > 1)
continue;
SgSymbol* sym = new SgSymbol(VARIABLE_NAME, varName.c_str(), SgTypeInt(), statement);
SgExpression* lhs = new SgVarRefExp(sym);
SgExpression* rhs = new SgValueExp(stoi(value));
SgStatement* asg = new SgStatement(ASSIGN_STAT, NULL, NULL, lhs, rhs, NULL);
asg->setFileName(statement->fileName());
asg->setFileId(statement->getFileId());
asg->setProject(statement->getProject());
asg->setlineNumber(getNextNegativeLineNumber());
if (insertBefore && insertBefore->controlParent())
{
insertBefore->insertStmtBefore(*asg, *insertBefore->controlParent());
statementsToRemove.insert(asg);
}
}
}
}
}
static void applyLeftPartForUnchangedAssignments(SgProject& project, map<string, SgExpression*>& arrayToVariable, int& variableNumber)
{
for (int fi = 0; fi < project.numberOfFiles(); ++fi)
{
SgFile* file = &(project.file(fi));
if (!file)
continue;
const string fileName = file->filename();
if (SgFile::switchToFile(fileName) == -1)
continue;
const int funcNum = file->numberOfFunctions();
for (int fni = 0; fni < funcNum; ++fni)
{
SgStatement* funcStart = file->functions(fni);
if (!funcStart)
continue;
declPlace = funcStart;
positionsToAdd[string(declPlace->fileName())].insert(declPlace);
SgStatement* endSt = funcStart->lastNodeOfStmt();
for (SgStatement* st = funcStart; st && st != endSt; st = st->lexNext())
{
if (st->variant() != ASSIGN_STAT)
continue;
if (!st->expr(0) || !st->expr(1))
continue;
if (changed.find(st) != changed.end())
continue;
SgExpression* lhs = st->expr(0);
if (!isArrayRef(lhs))
continue;
if (!lhs->symbol() || !lhs->symbol()->type())
continue;
if (!lhs->symbol()->type()->baseType())
continue;
if (!CheckConstIndexes(lhs->lhs()))
continue;
const string lhsUnparsed = lhs->unparse();
if (arrayToVariable.find(lhsUnparsed) == arrayToVariable.end())
continue;
TransformLeftPart(st, lhs, arrayToVariable, variableNumber);
}
}
}
}
static bool ContainsArrayRefRecursive(SgExpression* exp)
{
if (!exp)
return false;
if (isArrayRef(exp) && CheckConstIndexes(exp->lhs()))
return true;
return ContainsArrayRefRecursive(exp->lhs()) || ContainsArrayRefRecursive(exp->rhs());
}
static void getBorderVars(SgExpression* exp, const string& filename, map<string, map<string, SgStatement*>>& borderVars)
{
if (!exp)
return;
if ((isArrayRef(exp) && CheckConstIndexes(exp->lhs())) || exp->variant() == VAR_REF)
borderVars[filename][string(exp->unparse())] = declPlace;
getBorderVars(exp->lhs(), filename, borderVars);
getBorderVars(exp->rhs(), filename, borderVars);
}
static void processLoopBound(
SgStatement* st,
SgExpression* bound,
const string& boundUnparsed,
bool isUpperBound,
map<string, SgExpression*>& arrayToVariable,
map<string, map<string, SgStatement*>>& borderVars,
int& variableNumber)
{
if (!bound || !st)
return;
SgExpression* exp = isUpperBound ? bound->rhs() : bound->lhs();
getBorderVars(exp, st->fileName(), borderVars);
if (ContainsArrayRefRecursive(exp), borderVars, st->fileName())
{
copyStatement(st);
TransformBorder(st, bound, arrayToVariable, variableNumber);
positionsToAdd[string(declPlace->fileName())].insert(declPlace);
}
else if (bound->variant() == VAR_REF)
CheckVariable(st, bound, arrayToVariable, variableNumber);
}
void arrayConstantPropagation(SgProject& project)
{
map<string, SgExpression*> arrayToVariable;
map<string, map<string, SgStatement*>> borderVars;
int variableNumber = 0;
for (int i = 0; i < project.numberOfFiles(); i++)
{
SgFile* file = &(project.file(i));
if (!file)
continue;
SgFile::switchToFile(file->filename());
const int funcNum = file->numberOfFunctions();
for (int i = 0; i < funcNum; ++i)
{
SgStatement* st = file->functions(i);
if (!st)
continue;
declPlace = st;
SgStatement* lastNode = st->lastNodeOfStmt();
for (; st != lastNode; st = st->lexNext())
{
if (st && st->variant() == FOR_NODE)
{
if (!st->expr(0))
continue;
if (!st->expr(0)->lhs() || !st->expr(0)->rhs())
continue;
SgExpression* lowerBound = st->expr(0)->lhs();
SgExpression* upperBound = st->expr(0)->rhs();
string lowerBoundUnparsed = lowerBound->unparse();
string upperBoundUnparsed = upperBound->unparse();
processLoopBound(st, st->expr(0), upperBoundUnparsed, true, arrayToVariable, borderVars, variableNumber);
processLoopBound(st, st->expr(0), lowerBoundUnparsed, false, arrayToVariable, borderVars, variableNumber);
}
}
}
}
applyLeftPartForUnchangedAssignments(project, arrayToVariable, variableNumber);
map<string, set<SgStatement*>> funcStarts;
for (const auto& [fileName, statements] : positionsToAdd)
{
int res = SgFile::switchToFile(fileName);
if (res == -1)
continue;
for (SgStatement* st : statements)
{
SgStatement* scope = isSgProgHedrStmt(st) ? st : st->controlParent();
if (scope)
funcStarts[fileName].insert(scope);
}
}
for (const auto& [fileName, statements] : funcStarts)
{
SgFile::switchToFile(fileName);
for (SgStatement* st : statements)
{
InsertCommonAndDeclsForFunction(st, variablesToAdd);
}
}
map<string, map<SgStatement*, vector<pair<string, string>>>> result;
map<string, int> hitCount;
findConstValues(project, borderVars, arrayToVariable, hitCount, result);
insertDefinition(result, hitCount);
}
void restoreArrays()
{
cout << "ARRAY_PROPAGATION_RESTORE" << endl;
for (auto& [filename, statements] : expToChange)
{
if (SgFile::switchToFile(filename) == -1)
continue;
for (auto& [statement, statementCopy] : statements)
{
if (statement && statementCopy)
{
for (int i = 0; i < 3; i++)
{
statement->setExpression(i, statementCopy->expr(i));
}
}
}
}
for (SgStatement* st : statementsToRemove)
{
SgFile::switchToFile(st->fileName());
st->deleteStmt();
}
}

View File

@@ -0,0 +1,11 @@
#pragma once
#include "../Utils/SgUtils.h"
#include <string>
#include <vector>
using namespace std;
void arrayConstantPropagation(SgProject& project);
void restoreArrays();

View File

@@ -319,7 +319,7 @@ void InitPassesDependencies(map<passes, vector<passes>> &passDepsIn, set<passes>
list({ VERIFY_INCLUDES, CORRECT_VAR_DECL }) <= Pass(SET_IMPLICIT_NONE); list({ VERIFY_INCLUDES, CORRECT_VAR_DECL }) <= Pass(SET_IMPLICIT_NONE);
list({ ARRAY_PROPAGATION, CALL_GRAPH2, CALL_GRAPH, BUILD_IR, LOOP_GRAPH, LOOP_ANALYZER_DATA_DIST_S2 }) <= Pass(FIND_PRIVATE_ARRAYS_ANALYSIS); list({ ARRAY_PROPAGATION, CALL_GRAPH2, CALL_GRAPH, BUILD_IR, LOOP_GRAPH, LOOP_ANALYZER_DATA_DIST_S2 }) <= Pass(FIND_PRIVATE_ARRAYS_ANALYSIS);
list({ FIND_PRIVATE_ARRAYS_ANALYSIS, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN, REVERT_SUBST_EXPR_RD }) <= Pass(FIND_PRIVATE_ARRAYS); list({ FIND_PRIVATE_ARRAYS_ANALYSIS, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN, REVERT_SUBST_EXPR_RD, ARRAY_PROPAGATION_RESTORE }) <= Pass(FIND_PRIVATE_ARRAYS);
list({ BUILD_IR, CALL_GRAPH2, RESTORE_LOOP_FROM_ASSIGN, REVERT_SUBST_EXPR_RD }) <= Pass(MOVE_OPERATORS); list({ BUILD_IR, CALL_GRAPH2, RESTORE_LOOP_FROM_ASSIGN, REVERT_SUBST_EXPR_RD }) <= Pass(MOVE_OPERATORS);
Pass(CREATE_TEMPLATE_LINKS) <= Pass(MERGE_REGIONS); Pass(CREATE_TEMPLATE_LINKS) <= Pass(MERGE_REGIONS);