7 Commits

Author SHA1 Message Date
798052e565 add Region constructor and SolveDataflow function 2025-04-08 15:26:13 +03:00
87aec52e3f add Collapse 2025-04-08 15:26:13 +03:00
ALEXks
da6685df43 fixed 2025-04-01 18:53:49 +03:00
ALEXks
9c4993feeb version 2025-04-01 16:36:31 +03:00
ALEXks
7666a6c804 declare for module in progess 2025-04-01 16:36:09 +03:00
ALEXks
79302bf7b7 moved LoopGraph to json 2025-04-01 15:31:43 +03:00
ALEXks
a7f199ad4c moved LoopGraph to json 2025-04-01 15:28:53 +03:00
11 changed files with 23081 additions and 92 deletions

View File

@@ -76,7 +76,8 @@ set(UTILS src/Utils/AstWrapper.h
src/Utils/utils.h
src/Utils/version.h
src/Utils/module_utils.h
src/Utils/module_utils.cpp)
src/Utils/module_utils.cpp
src/Utils/json.hpp)
set(OMEGA src/SageAnalysisTool/OmegaForSage/add-assert.cpp
src/SageAnalysisTool/OmegaForSage/affine.cpp

View File

@@ -17,7 +17,6 @@
using namespace std;
#define DVMH_REG_RD 0
#define DVMH_REG_WT 1
@@ -1428,21 +1427,15 @@ static set<DIST::Array*>
for (auto& realArray : realRef)
{
if (added.count(realArray) != 0 || !realArray->IsNotDistribute())
if (added.count(realArray) != 0)
continue;
if (!realArray->IsNotDistribute())
continue;
SgStatement* declStat = NULL;
if (realArray->GetLocation().first != DIST::l_COMMON)
{
if (std::count(usedAll.begin(), usedAll.end(), realArray) == 0)
{
auto declInfo = *realArray->GetDeclInfo().begin();
declStat = SgStatement::getStatementByFileAndLine(declInfo.first, declInfo.second);
checkNull(declStat, convertFileName(__FILE__).c_str(), __LINE__);
}
}
else
if (realArray->GetLocation().first == DIST::l_COMMON)
{
commonArrays.insert(realArray);
auto decls = realArray->GetDeclInfo();
@@ -1471,6 +1464,19 @@ static set<DIST::Array*>
}
}
}
else if (realArray->GetLocation().first == DIST::l_MODULE)
{
//TODO
}
else
{
if (std::count(usedAll.begin(), usedAll.end(), realArray) == 0)
{
auto declInfo = *realArray->GetDeclInfo().begin();
declStat = SgStatement::getStatementByFileAndLine(declInfo.first, declInfo.second);
checkNull(declStat, convertFileName(__FILE__).c_str(), __LINE__);
}
}
if (declStat)
{

View File

@@ -26,11 +26,11 @@
#include "../Utils/errors.h"
#include "../Utils/AstWrapper.h"
#include "../Utils/json.hpp"
#include "../DirectiveProcessing/directive_parser.h"
#include "../DynamicAnalysis/gCov_parser_func.h"
#include "../GraphCall/graph_calls_func.h"
#include "../Transformations/array_assign_to_loop.h"
using std::vector;
@@ -40,6 +40,8 @@ using std::string;
using std::wstring;
using std::pair;
using json = nlohmann::json;
#define DEBUG 0
static inline void insertLabels(SgExpression *lb, map<int, vector<int>> &gotoLabels, const int line)
{
@@ -918,7 +920,7 @@ void* LoopGraph::getRealStat(const char* file) const
}
extern int PASSES_DONE[EMPTY_PASS];
static void printToBuffer(const LoopGraph *currLoop, const int childSize, char buf[512])
static int getLoopState(const LoopGraph* currLoop)
{
int loopState = 0; // 0 - unknown, 1 - good, 2 - bad
if (PASSES_DONE[CREATE_TEMPLATE_LINKS] ||
@@ -934,8 +936,14 @@ static void printToBuffer(const LoopGraph *currLoop, const int childSize, char b
if (currLoop->hasLimitsToParallel())
loopState = 2;
}
return loopState;
}
static void printToBuffer(const LoopGraph *currLoop, const int childSize, char buf[512])
{
sprintf(buf, "#%d#%d#%d#%d#%d#%d#%d#%d",
currLoop->lineNum, currLoop->lineNumAfterLoop, currLoop->perfectLoop, currLoop->hasGoto, currLoop->hasPrints, childSize, loopState,
currLoop->lineNum, currLoop->lineNumAfterLoop, currLoop->perfectLoop, currLoop->hasGoto, currLoop->hasPrints, childSize, getLoopState(currLoop),
currLoop->hasNonRectangularBounds);
}
@@ -947,36 +955,90 @@ static int calculateNormalChildSize(const LoopGraph *currLoop)
return count;
}
void convertToString(const LoopGraph *currLoop, string &result)
static json convertToJson(const LoopGraph* currLoop)
{
json loop;
const auto& file = currLoop->fileName;
if (currLoop && currLoop->lineNum > 0)
{
char buf[512];
result += "#" + std::to_string(currLoop->calls.size());
for (int i = 0; i < currLoop->calls.size(); ++i)
result += "#" + currLoop->calls[i].first + "#" + std::to_string(currLoop->calls[i].second);
printToBuffer(currLoop, calculateNormalChildSize(currLoop), buf);
result += string(buf);
loop["file"] = file;
loop["line"] = currLoop->lineNum;
loop["lineNumAfterLoop"] = currLoop->lineNumAfterLoop;
loop["perfectLoop"] = currLoop->perfectLoop;
loop["loopState"] = getLoopState(currLoop);
loop["hasNonRectangularBounds"] = (int)currLoop->hasNonRectangularBounds;
result += "#" + std::to_string(currLoop->linesOfExternalGoTo.size());
for (int i = 0; i < currLoop->linesOfExternalGoTo.size(); ++i)
result += "#" + std::to_string(currLoop->linesOfExternalGoTo[i]);
json calls = json::array();
for (auto& [func, line] : currLoop->calls)
{
json call;
call["line"] = line;
call["funcName"] = func;
call["canBeInlined"] = 0;
call["parentLineOffset"] = 0;
result += "#" + std::to_string(currLoop->linesOfInternalGoTo.size());
for (int i = 0; i < currLoop->linesOfInternalGoTo.size(); ++i)
result += "#" + std::to_string(currLoop->linesOfInternalGoTo[i]);
calls.push_back(call);
}
loop["funcCalls"] = calls;
result += "#" + std::to_string(currLoop->linesOfIO.size());
for (auto& i : currLoop->linesOfIO)
result += "#" + std::to_string(i);
json e_gotos = json::array();
for (auto& line : currLoop->linesOfExternalGoTo)
e_gotos.push_back(line);
loop["extGotos"] = e_gotos;
result += "#" + std::to_string(currLoop->linesOfStop.size());
for (auto& i : currLoop->linesOfStop)
result += "#" + std::to_string(i);
json i_gotos = json::array();
for (auto& line : currLoop->linesOfInternalGoTo)
i_gotos.push_back(line);
loop["intGotos"] = i_gotos;
for (int i = 0; i < (int)currLoop->children.size(); ++i)
convertToString(currLoop->children[i], result);
json ios = json::array();
for (auto& line : currLoop->linesOfIO)
ios.push_back(line);
loop["ios"] = ios;
json stops = json::array();
for (auto& line : currLoop->linesOfStop)
stops.push_back(line);
loop["stops"] = stops;
json children = json::array();
for (const auto& ch : currLoop->children)
{
auto conv = convertToJson(ch);
if (!conv.empty())
children.push_back(conv);
}
loop["children"] = children;
}
return loop;
}
json convertToJson(const map<string, vector<LoopGraph*>>& loopsByFileMap)
{
json loopsByFile = json::array();
for (auto& byFile : loopsByFileMap)
{
json loop;
const string& file = byFile.first;
json loops_array = json::array();
for (auto& loop : byFile.second)
{
auto conv = convertToJson(loop);
if (!conv.empty())
loops_array.push_back(conv);
}
loop["file"] = file;
loop["loops"] = loops_array;
loopsByFile.push_back(loop);
}
json allLoops;
allLoops["allLoops"] = loopsByFile;
return allLoops;
}
void createMapLoopGraph(const vector<LoopGraph*> &loops, map<int, LoopGraph*> &mapGraph)

View File

@@ -497,7 +497,6 @@ void processLoopInformationForFunction(std::map<LoopGraph*, std::map<DIST::Array
void addToDistributionGraph(const std::map<LoopGraph*, std::map<DIST::Array*, ArrayInfo*>>& loopInfo, const std::map<DIST::Array*, std::set<DIST::Array*>>& arrayLinksByFuncCalls);
void selectFreeLoopsForParallelization(const std::vector<LoopGraph*>& loops, const std::string& funcName, bool isDistribute, const std::vector<ParallelRegion*>& regions, std::vector<Messages>& messagesForFile);
void convertToString(const LoopGraph* currLoop, std::string& result);
int printLoopGraph(const char* fileName, const std::map<std::string, std::vector<LoopGraph*>>& loopGraph, bool withRegs = false);
void checkCountOfIter(std::map<std::string, std::vector<LoopGraph*>>& loopGraph, const std::map<std::string, std::vector<FuncInfo*>>& allFuncInfo, std::map<std::string, std::vector<Messages>>& SPF_messages);

View File

@@ -2,8 +2,10 @@
#include <vector>
#include <map>
#include <string>
#include "graph_loops.h"
#include "../Distribution/DvmhDirective.h"
#include "Utils/json.hpp"
struct SpfInterval;
@@ -18,3 +20,5 @@ bool recSymbolFind(SgExpression *ex, const std::string &symb, const int var);
void completeFillOfArrayUsageBetweenProc(const std::map<std::string, std::vector<LoopGraph*>>& loopGraph, const std::map<std::string, std::vector<FuncInfo*>>& allFuncInfo);
bool detectMpiCalls(SgProject* proj, std::map<std::string, std::vector<Messages>>& SPF_messages);
int calculateLoopIters(SgExpression* start, SgExpression* end, SgExpression* step, std::tuple<int, int, int>& result);
nlohmann::json convertToJson(const std::map<std::string, std::vector<LoopGraph*>>& loopsByFile);

View File

@@ -44,7 +44,7 @@ static bool isParentStmt(SgStatement* stmt, SgStatement* parent)
}
/*returns head block and loop*/
static pair<SAPFOR::BasicBlock*, unordered_set<SAPFOR::BasicBlock*>> GetBasicBlocksForLoop(LoopGraph* loop, vector<SAPFOR::BasicBlock*> blocks)
pair<SAPFOR::BasicBlock*, unordered_set<SAPFOR::BasicBlock*>> GetBasicBlocksForLoop(LoopGraph* loop, vector<SAPFOR::BasicBlock*> blocks)
{
unordered_set<SAPFOR::BasicBlock*> block_loop;
SAPFOR::BasicBlock* head_block = nullptr;
@@ -397,7 +397,7 @@ static vector<vector<ArrayDimension>> ElementsDifference(const vector<ArrayDimen
if(!dimDiff.empty())
{
vector<ArrayDimension> firstCopy = firstElement;
for(const auto range: dimDiff)
for(const auto& range: dimDiff)
{
firstCopy[i] = range;
result.push_back(firstCopy);
@@ -508,27 +508,94 @@ AccessingSet AccessingSet::Diff(const AccessingSet& secondSet) const
void Collapse(Region* region)
{
Region* newBlock = new Region();
for (auto& [arrayName, arrayRanges] : region->GetHeader()->array_out)
//Region* newBlock = new Region();
for (auto& [arrayName, arrayRanges] : region->getHeader()->array_out)
{
for (Region* byBlock : region->GetBasickBlocks())
for (Region* byBlock : region->getBasickBlocks())
{
AccessingSet intersection = byBlock->array_def[arrayName].Intersect(arrayRanges);
newBlock->array_def[arrayName].Union(intersection);
region->array_def[arrayName].Union(intersection);
}
}
for (auto& byBlock : region->GetBasickBlocks()) {
for (auto& byBlock : region->getBasickBlocks()) {
for (auto& [arrayName, arrayRanges] : byBlock->array_use)
{
AccessingSet diff = byBlock->array_use[arrayName].Diff(byBlock->array_in[arrayName]);
newBlock->array_use[arrayName].Union(diff);
region->array_use[arrayName].Union(diff);
}
}
for (Region* prevRegion : region->getPrevRegions()) {
prevRegion->setNextRegion(newBlock);
for (Region* prevBlock : region->getHeader()->getPrevRegions())
{
prevBlock->replaceInNextRegions(region, region->getHeader());
}
region->getNextRegion()->setPrevRegion(newBlock);
for (Region* nextBlock : region->getHeader()->getNextRegions())
{
nextBlock->replaceInPrevRegions(region, region->getHeader());
}
}
static void SetConnections(unordered_map<SAPFOR::BasicBlock*, Region*>& bbToRegion, const unordered_set<SAPFOR::BasicBlock*>& blockSet)
{
for (SAPFOR::BasicBlock* block : blockSet)
{
for (SAPFOR::BasicBlock* nextBlock : block->getNext())
{
if (bbToRegion.find(nextBlock) != bbToRegion.end())
{
bbToRegion[block]->addNextRegion(bbToRegion[nextBlock]);
}
}
for (SAPFOR::BasicBlock* prevBlock : block->getPrev())
{
if (bbToRegion.find(prevBlock) != bbToRegion.end())
{
bbToRegion[block]->addPrevRegion(bbToRegion[prevBlock]);
}
}
}
}
static Region* CreateSubRegion(LoopGraph* loop, const vector<SAPFOR::BasicBlock*>& Blocks, const unordered_map<SAPFOR::BasicBlock*, Region*>& bbToRegion)
{
Region* region = new Region;
auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks);
for (SAPFOR::BasicBlock* block : Blocks)
{
region->addBasickBlocks(bbToRegion.at(block));
}
for (LoopGraph* childLoop : loop->children)
{
region->addSubRegions(CreateSubRegion(childLoop, Blocks, bbToRegion));
}
return region;
}
Region::Region(LoopGraph* loop, vector<SAPFOR::BasicBlock*>& Blocks)
{
auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks);
unordered_map<SAPFOR::BasicBlock*, Region*> bbToRegion;
for (auto poiner : blockSet)
{
bbToRegion[poiner] = new Region(*poiner);
}
SetConnections(bbToRegion, blockSet);
//create subRegions
for (LoopGraph* childLoop : loop->children)
{
subRegions.insert(CreateSubRegion(childLoop, Blocks, bbToRegion));
}
}
void SolveDataFlow(Region* DFG)
{
//SolveDataFlowIteratively(DFG)
for (Region* subRegion : DFG->getSubRegions())
{
SolveDataFlow(subRegion);
}
Collapse(DFG);
}
void FindPrivateArrays(map<string, vector<LoopGraph*>> &loopGraph, map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR)

View File

@@ -7,6 +7,8 @@ using std::vector;
using std::map;
using std::string;
using std::set;
using std::unordered_set;
using std::pair;
struct ArrayDimension
{
@@ -37,48 +39,84 @@ class Region: public SAPFOR::BasicBlock {
Region()
{
header = nullptr;
nextRegion = nullptr;
}
Region(SAPFOR::BasicBlock block) : SAPFOR::BasicBlock::BasicBlock(block)
{
header = nullptr;
nextRegion = nullptr;
};
//Region(LoopGraph* loop);
Region* GetHeader()
}
Region(LoopGraph* loop, vector<SAPFOR::BasicBlock*>& Blocks);
Region* getHeader()
{
return header;
}
set<Region*> GetBasickBlocks()
unordered_set<Region*>& getBasickBlocks()
{
return basickBlocks;
}
vector<Region*> getPrevRegions()
void addBasickBlocks(Region* region)
{
basickBlocks.insert(region);
}
unordered_set<Region*> getPrevRegions()
{
return prevRegions;
}
Region* getNextRegion()
unordered_set<Region*> getNextRegions()
{
return nextRegion;
return nextRegions;
}
void setPrevRegion(Region* region)
void addPrevRegion(Region* region)
{
prevRegions.push_back(region);
prevRegions.insert(region);
}
void setNextRegion(Region* region)
void addNextRegion(Region* region)
{
nextRegion = region;
nextRegions.insert(region);
}
void replaceInPrevRegions(Region* source, Region* destination)
{
prevRegions.erase(destination);
prevRegions.insert(source);
}
void replaceInNextRegions(Region* source, Region* destination)
{
nextRegions.erase(destination);
nextRegions.insert(source);
}
unordered_set<Region*> getSubRegions()
{
return subRegions;
}
void addSubRegions(Region* region)
{
subRegions.insert(region);
}
ArrayAccessingIndexes array_def, array_use, array_out, array_in;
private:
set<Region*> subRegions, basickBlocks;
unordered_set<Region*> subRegions, basickBlocks;
/*next Region which is BB for current BB Region*/
unordered_set<Region*> nextRegions;
/*prev Regions which is BBs for current BB Region*/
unordered_set<Region*> prevRegions;
Region* header;
Region* nextRegion;
vector<Region*> prevRegions;
};
void Collapse(Region* region);
void FindPrivateArrays(map<string, vector<LoopGraph*>>& loopGraph, map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR);
void GetDimensionInfo(LoopGraph* loop, map<DIST::Array*, vector<vector<ArrayDimension>>>& loopDimensionsInfo, int level);
set<SAPFOR::BasicBlock> GetBasicBlocksForLoop(LoopGraph* loop, vector<SAPFOR::BasicBlock>);
pair<SAPFOR::BasicBlock*, unordered_set<SAPFOR::BasicBlock*>> GetBasicBlocksForLoop(LoopGraph* loop, vector<SAPFOR::BasicBlock*> blocks);

View File

@@ -1561,6 +1561,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
{
if (keepFiles)
printLoopGraph("_loopGraph.txt", loopGraph);
//printf("%s\n", convertToJson(loopGraph).dump(2).c_str());
}
else if (curr_regime == FILL_COMMON_BLOCKS)
{

22828
src/Utils/json.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,3 @@
#pragma once
#define VERSION_SPF "2401"
#define VERSION_SPF "2404"

View File

@@ -41,6 +41,7 @@
#include "../LoopAnalyzer/loop_analyzer.h"
#include "../DirectiveProcessing/insert_directive.h"
#include "../ProjectManipulation/PerfAnalyzer.h"
#include "Utils/json.hpp"
#include "BuildGraph.h"
@@ -61,6 +62,7 @@ using std::pair;
using std::tuple;
using std::to_string;
using std::make_pair;
using json = nlohmann::json;
extern set<short*> allocated;
extern set<int*> allocatedInt;
@@ -510,26 +512,7 @@ int SPF_GetGraphLoops(void*& context, int winHandler, short *options, short *pro
{
runPassesForVisualizer(projName, { LOOP_GRAPH } );
string resVal = "";
for (auto f = loopGraph.begin(); f != loopGraph.end(); ++f)
{
if (resVal != "")
resVal += "|";
int realLoops = 0;
for (int i = 0; i < f->second.size(); ++i)
if (f->second[i]->lineNum > 0)
realLoops++;
resVal += f->first + "|" + to_string(realLoops);
for (int i = 0; i < f->second.size(); ++i)
{
string localRes = "";
convertToString(f->second[i], localRes);
resVal += localRes;
}
}
string resVal = convertToJson(loopGraph).dump();
copyStringToShort(result, resVal);
retSize = (int)resVal.size() + 1;
}