WIP add analizing IR loop

This commit is contained in:
2024-05-26 12:32:53 +03:00
parent 62e4c5cdfe
commit 172eedfef1
5 changed files with 126 additions and 3 deletions

View File

@@ -1613,3 +1613,118 @@ vector<IR_Block*> buildIR(SgStatement* function, const FuncInfo* func, const vec
return blocks;
}
void dfs(SAPFOR::BasicBlock* block, vector<int>& visit, vector<pair<SAPFOR::BasicBlock*, SAPFOR::BasicBlock*>>& startAndEnd, SAPFOR::BasicBlock* prev) {
if (visit[block->getNumber()] == 2) {
cout << "error";
return;
}
if (visit[block->getNumber()] == 1) {
visit[block->getNumber()] = 2;
startAndEnd.push_back(make_pair(prev, block));
return;
}
visit[block->getNumber()] = 1;
for (auto i : block->getNext()) {
dfs(i, visit, startAndEnd, block);
}
}
void printBlock(SAPFOR::BasicBlock* block) {
cout << "block - " << block->getNumber() << endl;
cout << "next -";
for (auto i : block->getNext())
{
cout << " " << i->getNumber();
}
cout << endl << "prev -";
for (auto i : block->getPrev())
{
cout << " " << i->getNumber();
}
cout << endl;
for (auto i : block->getInstructions())
{
cout << i->getNumber() << " " << i->getInstruction()->dump() << endl;
}
cout << endl;
}
void testIR(map<FuncInfo*, vector<SAPFOR::BasicBlock*>> fullIR) {
for (auto& i : fullIR)
{
for (auto j : i.second)
printBlock(j);
vector<int> visited(i.second.size(), 0);
vector<pair<SAPFOR::BasicBlock*, SAPFOR::BasicBlock*>> startAndEnd;
dfs(i.second[0], visited, startAndEnd, NULL);
vector<LoopGraph*> loops;
for (auto j : startAndEnd)
{
auto instruction = j.second->getInstructions()[0]->getInstruction();
if (instruction->getOperator()->variant() == FOR_NODE) {
SgForStmt* stmt = isSgForStmt(instruction->getOperator());
auto tmpLoop = new LoopGraph();
tmpLoop->isFor = true;
tmpLoop->lineNum = instruction->getOperator()->lineNumber();
cout << "for" << endl << stmt->sunparse() << endl;
cout << "loop start line " << tmpLoop->lineNum << endl << endl;
loops.push_back(tmpLoop);
} else if (instruction->getOperator()->variant() == WHILE_NODE) {
SgWhileStmt* stmt = isSgWhileStmt(instruction->getOperator());
auto tmpLoop = new LoopGraph();
tmpLoop->lineNum = instruction->getOperator()->lineNumber();
if (stmt->conditional() == NULL)
{
//infinit loop
cout << "infinit loop " << endl << stmt->sunparse() << endl;
}
else
{
//while
cout << "while " << endl << stmt->sunparse();
}
cout << "loop start line " << tmpLoop->lineNum << endl << endl;
loops.push_back(tmpLoop);
} else if (instruction->getOperator()->variant() == LOOP_NODE) {
cout << "not known loop" << endl << instruction->getOperator()->sunparse() << endl;
}
else {
cout << "goto loop - " << instruction->getOperator()->sunparse() << endl;
}
}
/*for (auto j : i.second) {
cout << j->getNumber() << endl << "in" << endl;
for (auto k : j->getRD_In()) {
cout << k.first->getMemTypeStr() << " - ";
for (auto h : k.second) {
cout << h << " ";
}
cout << endl;
}
cout << "out" << endl;
for (auto k : j->getRD_Out()) {
cout << k.first->getMemTypeStr() << " - ";
for (auto h : k.second) {
cout << h << " ";
}
cout << endl;
}*/
}
}

View File

@@ -130,7 +130,7 @@ namespace SAPFOR
{
if (arg == NULL)
return "";
return arg->getValue();
return arg->getValue();
}
public:
@@ -169,7 +169,7 @@ namespace SAPFOR
{
std::string res = "";
std::string resultVal = getArgValue(result);
std::string resultVal = getArgValue(result);
std::string arg1Val = getArgValue(arg1);
std::string arg2Val = getArgValue(arg2);
@@ -312,4 +312,6 @@ std::vector<SAPFOR::IR_Block*> buildIR(SgStatement* function, const FuncInfo* fu
SAPFOR::Instruction* getInstructionByNumber(const std::vector<SAPFOR::IR_Block*>& blocks, int num);
std::pair<SAPFOR::Instruction*, SAPFOR::BasicBlock*> getInstructionAndBlockByNumber(const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& CFGraph, int num);
std::pair<SAPFOR::Instruction*, SAPFOR::BasicBlock*> getInstructionAndBlockByStatement(const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& CFGraph, SgStatement* stmt);
int getParamIndex(SAPFOR::Argument* func_param, int max_index);
int getParamIndex(SAPFOR::Argument* func_param, int max_index);
void testIR(std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>> fullIR);

View File

@@ -1036,6 +1036,8 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
if(func->funcPointer->variant() != ENTRY_STAT)
countOfTransform += removeDeadCode(func->funcPointer, allFuncInfo, commonBlocks);
}
else if (curr_regime == EXPLORE_IR_LOOPS)
testIR(fullIR);
else if (curr_regime == TEST_PASS)
{
//test pass

View File

@@ -178,6 +178,7 @@ enum passes {
INSERT_NO_DISTR_FLAGS_FROM_GUI,
SET_IMPLICIT_NONE,
EXPLORE_IR_LOOPS,
TEST_PASS,
EMPTY_PASS
@@ -360,6 +361,7 @@ static void setPassValues()
passNames[CONVERT_TO_C] = "CONVERT_TO_C";
passNames[SET_IMPLICIT_NONE] = "SET_IMPLICIT_NONE";
passNames[INSERT_NO_DISTR_FLAGS_FROM_GUI] = "INSERT_NO_DISTR_FLAGS_FROM_GUI";
passNames[EXPLORE_IR_LOOPS] = "EXPLORE_IR_LOOPS";
passNames[TEST_PASS] = "TEST_PASS";
}

View File

@@ -312,6 +312,8 @@ void InitPassesDependencies(map<passes, vector<passes>> &passDepsIn, set<passes>
Pass(CORRECT_VAR_DECL) <= Pass(SET_IMPLICIT_NONE);
list({ CALL_GRAPH, LOOP_GRAPH, CALL_GRAPH2 }) <= Pass(EXPLORE_IR_LOOPS);
passesIgnoreStateDone.insert({ CREATE_PARALLEL_DIRS, INSERT_PARALLEL_DIRS, INSERT_SHADOW_DIRS, EXTRACT_PARALLEL_DIRS,
EXTRACT_SHADOW_DIRS, CREATE_REMOTES, UNPARSE_FILE, REMOVE_AND_CALC_SHADOW,
REVERSE_CREATED_NESTED_LOOPS, PREDICT_SCHEME, CALCULATE_STATS_SCHEME, REVERT_SPF_DIRS, CLEAR_SPF_DIRS, TRANSFORM_SHADOW_IF_FULL,