This commit is contained in:
2026-03-26 16:01:14 +03:00
committed by ALEXks
parent 97e60e16be
commit 8632dfbf31
5 changed files with 94 additions and 23 deletions

View File

@@ -287,7 +287,6 @@ void ArrayConstantPropagation(SgProject& project)
if (!file)
continue;
const int funcNum = file->numberOfFunctions();
for (int i = 0; i < funcNum; ++i)
{
@@ -327,7 +326,10 @@ void ArrayConstantPropagation(SgProject& project)
if (scope)
funcStarts.insert(scope);
}
for (const auto& st: funcStarts)
for (const auto& st : funcStarts)
{
SgFile::switchToFile(st->fileName());
InsertCommonAndDeclsForFunction(st, variablesToAdd);
}
}
}

View File

@@ -7,16 +7,20 @@
#include <numeric>
#include <iostream>
#include "CFGraph/CFGraph.h"
#include "Distribution/Array.h"
#include "graph_loops.h"
#include "private_arrays_search.h"
#include "range_structures.h"
#include "region.h"
#include "SgUtils.h"
#include "graph_loops.h"
#include "CFGraph/CFGraph.h"
#include "utils.h"
#include "Utils/AstWrapper.h"
using namespace std;
extern std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>> declaredArrays;
static unordered_set<Region*> collapsed;
static void RemoveEmptyPoints(ArrayAccessingIndexes& container)
@@ -308,29 +312,63 @@ static bool getArrayDeclaredDimensions(SgArrayRefExp* arrayRef, vector<uint64_t>
return true;
}
static DIST::Array* getDistArrayBySymbol(SgSymbol* arrSym)
{
if (!arrSym)
return nullptr;
for (auto& [key, val] : declaredArrays)
{
DIST::Array* distArr = val.first;
if (!distArr)
continue;
Symbol* declSym = distArr->GetDeclSymbol();
if (!declSym)
continue;
SgSymbol* sgDecl = declSym->GetOriginal();
if (sgDecl && isEqSymbols(sgDecl, arrSym))
return distArr;
}
return nullptr;
}
static bool CheckDimensionLength(const AccessingSet& array)
{
if (array.GetElements().empty())
return false;
size_t dimCount = array.GetElements()[0].size();
SgArrayRefExp* arrayRef = array.GetElements()[0][0].array;
if (!arrayRef)
if (!arrayRef || !arrayRef->symbol())
return false;
vector<uint64_t> declaredDims;
declaredDims.reserve(dimCount);
if (!getArrayDeclaredDimensions(arrayRef, declaredDims))
return false;
vector<ArrayDimension> testArray(dimCount);
for (size_t i = 0; i < dimCount; i++)
DIST::Array* distArr = getDistArrayBySymbol(arrayRef->symbol());
if (distArr && distArr->GetDimSize() == (int)dimCount)
{
testArray[i] = { 1, 1, declaredDims[i], nullptr };
const auto& sizes = distArr->GetSizes();
bool valid = true;
for (size_t i = 0; i < dimCount && valid; ++i)
{
int lo = sizes[i].first;
int hi = sizes[i].second;
if (lo > hi)
valid = false;
else
declaredDims.push_back((uint64_t)(hi - lo + 1));
}
if (valid && declaredDims.size() == dimCount)
{
vector<ArrayDimension> testArray(dimCount);
for (size_t i = 0; i < dimCount; i++)
testArray[i] = { 1, 1, declaredDims[i], nullptr };
return AccessingSet({ testArray }).Diff(array).GetElements().empty();
}
}
AccessingSet diff = AccessingSet({ testArray }).Diff(array);
return diff.GetElements().empty();
return false;
}
static void AddPrivateArraysToLoop(LoopGraph* loop, const ArrayAccessingIndexes& privates, set<SgStatement*>& insertedPrivates)
{
SgStatement* spfStat = new SgStatement(SPF_ANALYSIS_DIR);
@@ -338,6 +376,7 @@ static void AddPrivateArraysToLoop(LoopGraph* loop, const ArrayAccessingIndexes&
spfStat->setFileName(loop->loop->fileName());
SgExpression* toAdd = new SgExpression(EXPR_LIST, new SgExpression(ACC_PRIVATE_OP), NULL, NULL);
set<SgSymbol*> arraysToInsert;
std::cout << "First bp\n";
for (const auto& [_, accessingSet] : privates)
{
if (!CheckDimensionLength(accessingSet))
@@ -395,7 +434,15 @@ void FindPrivateArrays(map<string, vector<LoopGraph*>>& loopGraph, map<FuncInfo*
{
if (funcInfo->fileName == fileName && funcInfo->funcPointer->GetOriginal() == search_func)
{
Region* loopRegion = new Region(loop, blocks);
Region* loopRegion;
try
{
loopRegion = new Region(loop, blocks);
}
catch (...)
{
continue;
}
if (loopRegion->getBasickBlocks().size() <= 1)
{
delete(loopRegion);
@@ -407,7 +454,6 @@ void FindPrivateArrays(map<string, vector<LoopGraph*>>& loopGraph, map<FuncInfo*
delete(loopRegion);
}
}
if (result.find(loop) != result.end() && !result[loop].empty())
AddPrivateArraysToLoop(loop, result[loop], insertedPrivates);
}

View File

@@ -150,6 +150,8 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces
vector<ArrayDimension> accessPoint(n);
auto* ref = isSgArrayRefExp(instruction->getInstruction()->getExpression());
if (!ref)
continue;
int fillCount = 0;
vector<pair<int, int>> coeffsForDims;
@@ -267,13 +269,30 @@ static void DFS(Region* block, vector<Region*>& result, unordered_set<Region*> c
result.push_back(block);
}
void TopologySort(std::vector<Region*>& basikBlocks, Region* header)
bool HasCycle(Region* block, const std::unordered_set<Region*>& cycleBlocks, std::unordered_set<Region*>& visitedBlocks)
{
if (visitedBlocks.find(block) != visitedBlocks.end())
return true;
visitedBlocks.insert(block);
for (Region* nextBlock : block->getNextRegions())
{
if (cycleBlocks.find(nextBlock) != cycleBlocks.end() && HasCycle(nextBlock, cycleBlocks, visitedBlocks))
return true;
}
return false;
}
bool TopologySort(std::vector<Region*>& basikBlocks, Region* header)
{
vector<Region*> result;
unordered_set<Region*> cycleBlocks(basikBlocks.begin(), basikBlocks.end());
unordered_set<Region*> visitedBlocks;
if (HasCycle(header, cycleBlocks, visitedBlocks))
return false;
vector<Region*> result;
DFS(header, result, cycleBlocks);
reverse(result.begin(), result.end());
basikBlocks = result;
basikBlocks = move(result);
return true;
}
static void SetConnections(unordered_map<SAPFOR::BasicBlock*, Region*>& bbToRegion, const unordered_set<SAPFOR::BasicBlock*>& blockSet)
@@ -313,7 +332,8 @@ static Region* CreateSubRegion(LoopGraph* loop, const vector<SAPFOR::BasicBlock*
continue;
region->addSubRegions(CreateSubRegion(childLoop, Blocks, bbToRegion));
}
TopologySort(region->getBasickBlocks(), region->getHeader());
if (!TopologySort(region->getBasickBlocks(), region->getHeader()))
throw std::runtime_error("Unnoticed cycle");
return region;
}
@@ -337,6 +357,7 @@ Region::Region(LoopGraph* loop, const vector<SAPFOR::BasicBlock*>& Blocks)
if (!childLoop->isFor())
continue;
subRegions.insert(CreateSubRegion(childLoop, Blocks, bbToRegion));
}
TopologySort(basickBlocks, this->header);
}
if (!TopologySort(basickBlocks, this->header))
throw std::runtime_error("Unnoticed cycle");
}

View File

@@ -79,4 +79,6 @@ private:
Region* header;
};
void TopologySort(std::vector<Region*>& basikBlocks, Region* header);
bool HasCycle(Region* block, const std::unordered_set<Region*>& cycleBlocks, std::unordered_set<Region*>& visitedBlocks);
bool TopologySort(std::vector<Region*>& basikBlocks, Region* header);

View File

@@ -658,7 +658,7 @@ string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const ch
if (wasDeleted)
{
if (str.size() || str.back() != '\n')
if (str.size() && str.back() != '\n')
str += '\n';
st->setComments(str.c_str());
}