13 Commits

Author SHA1 Message Date
Egor Mayorov
7390d96b1f change reordering logic 2026-04-01 17:12:55 +03:00
a1e12f5c1c Merge pull request 'egormayorov' (#78) from egormayorov into master 2026-03-27 08:29:34 +03:00
ALEXks
cad8c0913d Merge branch 'master' into egormayorov 2026-03-27 08:28:44 +03:00
ALEXks
bde804cff6 updated 2026-03-27 08:27:25 +03:00
Egor Mayorov
589680a78b fix files usage 2026-03-26 14:18:45 +03:00
ALEXks
88bac54901 fixed function prototype 2026-03-26 14:18:45 +03:00
ALEXks
0d4d2b78d8 updated 2026-03-26 14:18:45 +03:00
Egor Mayorov
bbac07202d Add swith to file usage 2026-03-26 14:18:39 +03:00
ALEXks
9325723e69 updated projects 2026-03-20 15:32:24 +03:00
ALEXks
18ac53f342 fixed inliner 2026-03-19 13:04:26 +03:00
ALEXks
0bec2c6527 assign line number to intervals for loops 2026-03-10 20:33:02 +03:00
aa56778be1 Merge pull request 'Move operators pass fixes' (#77) from egormayorov into master 2026-03-10 10:03:36 +03:00
ALEXks
0a484e77de version updated 2026-03-10 10:03:27 +03:00
8 changed files with 147 additions and 90 deletions

View File

@@ -17,7 +17,7 @@ using std::fstream;
static long int getNextTag() static long int getNextTag()
{ {
static long int INTERVAL_TAG = 0; static long int INTERVAL_TAG = 0;
return INTERVAL_TAG++; return -(INTERVAL_TAG++);
} }
//Debug funcs //Debug funcs
@@ -413,7 +413,7 @@ static void findIntervals(SpfInterval *interval, map<int, int> &labelsRef, map<i
inter->lineFile = std::make_pair(currentSt->lineNumber(), currentSt->fileName()); inter->lineFile = std::make_pair(currentSt->lineNumber(), currentSt->fileName());
inter->parent = interval; inter->parent = interval;
inter->exit_levels.push_back(0); inter->exit_levels.push_back(0);
inter->tag = getNextTag(); inter->tag = currentSt->lineNumber();//getNextTag();
interval->nested.push_back(inter); interval->nested.push_back(inter);
findIntervals(inter, labelsRef, gotoStmts, currentSt); findIntervals(inter, labelsRef, gotoStmts, currentSt);

View File

@@ -1,4 +1,4 @@
#include "Utils/leak_detector.h" #include "Utils/leak_detector.h"
#pragma comment(linker, "/STACK:536870912") // 512 МБ #pragma comment(linker, "/STACK:536870912") // 512 МБ
@@ -943,7 +943,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
} }
} }
else if (curr_regime == MOVE_OPERATORS) else if (curr_regime == MOVE_OPERATORS)
moveOperators(file, loopGraph, fullIR, countOfTransform); moveOperators(file, fullIR, countOfTransform);
else if (curr_regime == PRIVATE_REMOVING_ANALYSIS) else if (curr_regime == PRIVATE_REMOVING_ANALYSIS)
{ {
auto itFound = loopGraph.find(file->filename()); auto itFound = loopGraph.find(file->filename());

View File

@@ -1089,6 +1089,8 @@ static int clean(const string& funcName, SgStatement* funcSt, const map<string,
} }
SgGotoStmt* gotoSt = new SgGotoStmt(*contLab); SgGotoStmt* gotoSt = new SgGotoStmt(*contLab);
if (st->label())
gotoSt->setLabel(*st->label());
st->insertStmtBefore(*gotoSt, *st->controlParent()); st->insertStmtBefore(*gotoSt, *st->controlParent());
toDelete.push_back(st); toDelete.push_back(st);

View File

@@ -17,7 +17,6 @@
using namespace std; using namespace std;
set<int> loop_tags = {FOR_NODE}; set<int> loop_tags = {FOR_NODE};
set<int> control_tags = {IF_NODE, ELSEIF_NODE, DO_WHILE_NODE, WHILE_NODE, LOGIF_NODE}; set<int> control_tags = {IF_NODE, ELSEIF_NODE, DO_WHILE_NODE, WHILE_NODE, LOGIF_NODE};
set<int> control_end_tags = {CONTROL_END}; set<int> control_end_tags = {CONTROL_END};
@@ -53,13 +52,25 @@ static vector<SAPFOR::IR_Block*> findInstructionsFromStatement(SgStatement* st,
vector<SAPFOR::BasicBlock*> findFuncBlocksByFuncStatement(SgStatement *st, const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR) { vector<SAPFOR::BasicBlock*> findFuncBlocksByFuncStatement(SgStatement *st, const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR) {
vector<SAPFOR::BasicBlock*> result; vector<SAPFOR::BasicBlock*> result;
if (!st)
return result;
Statement* forSt = (Statement*)st; Statement* forSt = (Statement*)st;
const string stmtFile = st->fileName();
const int stmtLine = st->lineNumber();
for (auto& func: FullIR) { for (auto& func: FullIR) {
if (func.first->funcPointer->getCurrProcessFile() == forSt->getCurrProcessFile() if (!func.first || !func.first->funcPointer)
&& func.first->funcPointer->lineNumber() == forSt->lineNumber()) continue;
const string funcFile = func.first->fileName;
const int funcLine = func.first->funcPointer->lineNumber();
// Important: select CFG blocks only for the same file and function header.
if (funcFile == stmtFile && funcLine == stmtLine)
{ {
result = func.second; result = func.second;
break;
} }
} }
return result; return result;
@@ -160,40 +171,60 @@ static map<SgStatement*, vector<SgStatement*>> analyzeBasicBlockIntraDependencie
if (!instr) if (!instr)
return string(); return string();
SgExpression* ex = instr->getExpression(); SgExpression* ex = instr->getExpression();
if (!ex || !ex->unparse()) if (!ex)
return string(); return string();
auto normalizeExprText = [](const string& raw) -> string
{
string t;
t.reserve(raw.size());
for (unsigned char c : raw)
if (!std::isspace(c))
t.push_back((char)c);
auto stripOneLayer = [](const string& x) -> string auto exprKey = [&](auto&& self, SgExpression* e) -> string
{
if (!e)
return string("_");
if (auto* ar = isSgArrayRefExp(e))
{ {
if (x.size() < 2 || x.front() != '(' || x.back() != ')') SgSymbol* sym = ar->symbol() ? OriginalSymbol(ar->symbol()) : nullptr;
return x; string key = string("A(") + (sym ? sym->identifier() : "?");
int bal = 0; const int n = ar->numberOfSubscripts();
for (size_t i = 0; i + 1 < x.size(); ++i) for (int i = 0; i < n; ++i)
{ {
if (x[i] == '(') bal++; key += ",";
else if (x[i] == ')') bal--; key += self(self, ar->subscript(i));
if (bal == 0 && i + 1 < x.size() - 1)
return x;
} }
return x.substr(1, x.size() - 2); key += ")";
}; return key;
while (true)
{
const string stripped = stripOneLayer(t);
if (stripped == t)
break;
t = stripped;
} }
return t;
if (e->variant() == VAR_REF || e->variant() == CONST_REF)
{
SgSymbol* sym = e->symbol() ? OriginalSymbol(e->symbol()) : nullptr;
return string((e->variant() == VAR_REF) ? "V(" : "C(") + (sym ? sym->identifier() : "?") + ")";
}
if (auto* v = isSgValueExp(e))
{
if (e->variant() == INT_VAL)
return string("I(") + to_string(v->intValue()) + ")";
if (e->variant() == BOOL_VAL)
return string("B(") + (v->boolValue() ? "1" : "0") + ")";
if (e->variant() == CHAR_VAL)
return string("CH(") + string(1, v->charValue()) + ")";
if (e->variant() == FLOAT_VAL)
return string("F(") + (v->floatValue() ? v->floatValue() : "") + ")";
if (e->variant() == DOUBLE_VAL)
return string("D(") + (v->doubleValue() ? v->doubleValue() : "") + ")";
if (e->variant() == STRING_VAL)
return string("S(") + (v->stringValue() ? v->stringValue() : "") + ")";
}
string key = string("N(") + to_string(e->variant());
if (e->lhs())
key += ",L=" + self(self, e->lhs());
if (e->rhs())
key += ",R=" + self(self, e->rhs());
key += ")";
return key;
}; };
return "MEMEX#" + normalizeExprText(ex->unparse());
return "MEMEX#" + exprKey(exprKey, ex);
}; };
auto isBarrier = [&](const SAPFOR::Instruction* instr) -> bool auto isBarrier = [&](const SAPFOR::Instruction* instr) -> bool
@@ -296,6 +327,9 @@ static map<SgStatement*, vector<SgStatement*>> analyzeBasicBlockIntraDependencie
addDep(instr->getArg1()); addDep(instr->getArg1());
addDep(instr->getArg2()); addDep(instr->getArg2());
if (instr->getOperation() == SAPFOR::CFG_OP::RANGE)
addDep(instr->getResult());
if (instr->getOperation() == SAPFOR::CFG_OP::STORE || instr->getOperation() == SAPFOR::CFG_OP::REC_REF_STORE) if (instr->getOperation() == SAPFOR::CFG_OP::STORE || instr->getOperation() == SAPFOR::CFG_OP::REC_REF_STORE)
addDep(instr->getResult()); addDep(instr->getResult());
@@ -343,7 +377,7 @@ static map<SgStatement*, vector<SgStatement*>> analyzeBasicBlockIntraDependencie
return result; return result;
} }
static bool reorderOperatorsInBasicBlockUsingDeps(SAPFOR::BasicBlock* bb) static bool reorderOperatorsInBasicBlockUsingDeps(SAPFOR::BasicBlock* bb, const char* expectedFile)
{ {
if (!bb) if (!bb)
return false; return false;
@@ -373,6 +407,18 @@ static bool reorderOperatorsInBasicBlockUsingDeps(SAPFOR::BasicBlock* bb)
if (ops.size() < 2) if (ops.size() < 2)
return false; return false;
// Check that analyzed BB is in the same file as the expected file
const char* bbFile = ops.front()->fileName();
if (!bbFile)
bbFile = "(unknown)";
if (expectedFile && strcmp(expectedFile, bbFile) != 0)
return false;
for (auto* st : ops)
{
if (!st || !st->fileName() || strcmp(st->fileName(), bbFile) != 0)
return false;
}
SgStatement* parent = ops.front()->controlParent(); SgStatement* parent = ops.front()->controlParent();
if (!parent) if (!parent)
return false; return false;
@@ -424,60 +470,70 @@ static bool reorderOperatorsInBasicBlockUsingDeps(SAPFOR::BasicBlock* bb)
// as close as possible after its last dependency (if any). // as close as possible after its last dependency (if any).
const auto depsMap = analyzeBasicBlockIntraDependencies(bb); const auto depsMap = analyzeBasicBlockIntraDependencies(bb);
vector<SgStatement*> order = ops; vector<SgStatement*> order = ops;
const vector<SgStatement*> originalOrder = ops;
const int nOrig = (int)originalOrder.size();
auto buildPos = [&](const vector<SgStatement*>& v) auto indexIn = [](const vector<SgStatement*>& v, SgStatement* s) -> int
{ {
map<SgStatement*, int> pos;
for (int i = 0; i < (int)v.size(); ++i) for (int i = 0; i < (int)v.size(); ++i)
pos[v[i]] = i; if (v[i] == s)
return pos; return i;
return -1;
}; };
const int maxIterations = (int)order.size() * (int)order.size() + 10; auto indexInOriginal = [&](SgStatement* s) -> int
bool anyMove = false;
for (int iter = 0; iter < maxIterations; ++iter)
{ {
bool movedThisIter = false; return indexIn(originalOrder, s);
const auto pos = buildPos(order); };
for (int i = 0; i < (int)order.size(); ++i) for (SgStatement* s : ops)
{
auto itDeps = depsMap.find(s);
if (itDeps == depsMap.end() || itDeps->second.empty())
continue;
int lastDepOrigIdx = -1;
for (SgStatement* dep : itDeps->second)
{ {
SgStatement* curSt = order[i]; const int j = indexInOriginal(dep);
auto it = depsMap.find(curSt); if (j >= 0)
if (it == depsMap.end() || it->second.empty()) lastDepOrigIdx = max(lastDepOrigIdx, j);
}
if (lastDepOrigIdx < 0)
continue;
SgStatement* successor = nullptr;
if (lastDepOrigIdx + 1 < nOrig)
successor = originalOrder[lastDepOrigIdx + 1];
int posS = indexIn(order, s);
if (posS < 0)
continue;
if (successor == nullptr)
{
if (posS == (int)order.size() - 1)
continue; continue;
order.erase(order.begin() + posS);
int lastDepIdx = -1; order.push_back(s);
for (SgStatement* dep : it->second) continue;
{
auto itP = pos.find(dep);
if (itP != pos.end())
lastDepIdx = max(lastDepIdx, itP->second);
}
if (lastDepIdx < 0)
continue;
int target = lastDepIdx + 1;
if (target == i)
continue;
SgStatement* moved = order[i];
order.erase(order.begin() + i);
if (target > i)
target -= 1;
if (target < 0)
target = 0;
if (target > (int)order.size())
target = (int)order.size();
order.insert(order.begin() + target, moved);
movedThisIter = true;
anyMove = true;
break;
} }
if (!movedThisIter) if (successor == s)
break; continue;
const int posSucc = indexIn(order, successor);
if (posSucc < 0)
continue;
if (posS + 1 == posSucc)
continue;
order.erase(order.begin() + posS);
const int posSucc2 = indexIn(order, successor);
if (posSucc2 < 0)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
order.insert(order.begin() + posSucc2, s);
} }
bool changed = false; bool changed = false;
@@ -548,15 +604,14 @@ static bool reorderOperatorsInBasicBlockUsingDeps(SAPFOR::BasicBlock* bb)
return true; return true;
} }
void moveOperators(SgFile *file, map<string, vector<LoopGraph*>>& loopGraph, void moveOperators(SgFile* file, const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR, int& countOfTransform) {
const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR,
int& countOfTransform) {
if (!file) if (!file)
return; return;
if (SgFile::switchToFile(file->filename()) == -1)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
const int funcNum = file->numberOfFunctions(); const int funcNum = file->numberOfFunctions();
for (int i = 0; i < funcNum; ++i)
for (int i = 0; i < funcNum; ++i) { {
SgStatement* st = file->functions(i); SgStatement* st = file->functions(i);
const auto loopBlocks = findBlocksInLoopsByFullIR(st, FullIR); const auto loopBlocks = findBlocksInLoopsByFullIR(st, FullIR);
@@ -564,7 +619,7 @@ void moveOperators(SgFile *file, map<string, vector<LoopGraph*>>& loopGraph,
{ {
if (!bb) if (!bb)
continue; continue;
if (reorderOperatorsInBasicBlockUsingDeps(bb)) if (reorderOperatorsInBasicBlockUsingDeps(bb, file->filename()))
countOfTransform += 1; countOfTransform += 1;
} }
} }

View File

@@ -3,4 +3,4 @@
#include "../../GraphLoop/graph_loops.h" #include "../../GraphLoop/graph_loops.h"
#include "../../CFGraph/CFGraph.h" #include "../../CFGraph/CFGraph.h"
void moveOperators(SgFile *file, std::map<std::string, std::vector<LoopGraph*>>& loopGraph, const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& FullIR, int& countOfTransform); void moveOperators(SgFile* file, const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& FullIR, int& countOfTransform);

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define VERSION_SPF "2468" #define VERSION_SPF "2472"