Files
SAPFOR/src/PrivateAnalyzer/private_arrays_search.cpp

444 lines
15 KiB
C++
Raw Normal View History

2026-02-21 02:34:57 +03:00
#include <algorithm>
2025-03-31 02:50:30 +03:00
#include <map>
2026-04-27 16:18:43 +03:00
#include <set>
2025-03-31 02:50:30 +03:00
#include <vector>
#include <queue>
#include <numeric>
#include <iostream>
2026-03-26 16:01:14 +03:00
#include "CFGraph/CFGraph.h"
#include "Distribution/Array.h"
2026-04-27 16:18:43 +03:00
#include "errors.h"
2026-03-26 16:01:14 +03:00
#include "graph_loops.h"
2025-03-31 02:50:30 +03:00
#include "private_arrays_search.h"
2025-05-27 02:25:39 +03:00
#include "range_structures.h"
#include "region.h"
2025-06-04 13:08:38 +03:00
#include "SgUtils.h"
2026-04-27 16:18:43 +03:00
#include "Transformations/ArrayConstantPropagation/propagation.h"
#include "utils.h"
2026-03-26 16:01:14 +03:00
#include "Utils/AstWrapper.h"
2025-03-31 02:50:30 +03:00
using namespace std;
2026-04-27 16:18:43 +03:00
static set<Region*> collapsed;
2026-02-21 02:34:57 +03:00
2026-05-01 12:23:51 +03:00
static void removeEmptyPoints(arrayAccessingIndexes& container)
2025-12-19 01:55:23 +03:00
{
2026-05-01 12:23:51 +03:00
arrayAccessingIndexes resultContainer;
2026-04-27 16:18:43 +03:00
set<string> toRemove;
2025-12-19 21:06:55 +03:00
2025-12-19 01:55:23 +03:00
for (auto& [arrayName, accessingSet] : container)
{
vector<vector<ArrayDimension>> points;
for (auto& arrayPoint : accessingSet.GetElements())
{
if (!arrayPoint.empty())
points.push_back(arrayPoint);
}
2025-12-19 21:06:55 +03:00
2026-03-12 04:25:45 +03:00
if (!points.empty())
2025-12-19 01:55:23 +03:00
resultContainer[arrayName] = points;
2026-03-12 04:25:45 +03:00
else
2025-12-19 01:55:23 +03:00
toRemove.insert(arrayName);
}
for (const string& name : toRemove)
container.erase(name);
2025-12-19 21:06:55 +03:00
2025-12-19 01:55:23 +03:00
for (auto& [arrayName, accessingSet] : resultContainer)
container[arrayName] = accessingSet;
}
2026-05-01 12:23:51 +03:00
static void collapse(Region* region)
2025-03-31 02:50:30 +03:00
{
2025-05-19 20:50:35 +03:00
if (region->getBasickBlocks().empty())
return;
2025-05-27 02:25:39 +03:00
2026-02-21 02:34:57 +03:00
bool firstRegion = true;
2026-04-27 16:18:43 +03:00
int blockCount = 0;
2026-02-21 02:34:57 +03:00
for (Region* basickBlock : region->getBasickBlocks())
2025-03-31 02:50:30 +03:00
{
2026-02-21 02:34:57 +03:00
if (basickBlock->getNextRegions().empty())
2025-03-31 02:50:30 +03:00
{
2026-02-21 02:34:57 +03:00
if (firstRegion)
{
region->array_def = basickBlock->array_out;
firstRegion = false;
}
else
{
2026-04-27 16:18:43 +03:00
set<string> toErease;
2026-02-21 02:34:57 +03:00
for (auto& [arrayName, arrayRanges] : region->array_def)
{
if (basickBlock->array_out.find(arrayName) != basickBlock->array_out.end())
arrayRanges = arrayRanges.Intersect(basickBlock->array_out[arrayName]);
else
{
arrayRanges = AccessingSet();
toErease.insert(arrayName);
}
}
for (string arrayName : toErease)
region->array_def.erase(arrayName);
}
2025-03-31 02:50:30 +03:00
}
}
2026-02-21 02:34:57 +03:00
RegionInstruction instruction;
instruction.def = move(region->array_def);
2026-05-01 12:23:51 +03:00
arrayAccessingIndexes recursivePriv;
2026-02-21 02:34:57 +03:00
for (auto& byBlock : region->getBasickBlocks())
2025-05-19 20:50:35 +03:00
{
2026-04-27 16:18:43 +03:00
if (!byBlock->array_priv.empty())
recursivePriv = byBlock->array_priv;
2026-02-21 02:34:57 +03:00
for (auto& instruction : byBlock->instructions)
2025-03-31 02:50:30 +03:00
{
2026-04-27 16:18:43 +03:00
if (!instruction.def.empty() || !instruction.use.empty())
blockCount++;
2026-02-21 02:34:57 +03:00
for (auto& [arrayName, _] : instruction.use)
{
AccessingSet diff = instruction.use[arrayName].Diff(instruction.in[arrayName]);
region->array_use[arrayName] = region->array_use[arrayName].Union(diff);
}
2025-03-31 02:50:30 +03:00
}
}
2025-12-19 21:06:55 +03:00
2026-05-01 12:23:51 +03:00
arrayAccessingIndexes useUnionB;
2025-05-19 20:50:35 +03:00
for (auto& byBlock : region->getBasickBlocks())
2026-02-21 02:34:57 +03:00
for (auto& instruction : byBlock->instructions)
for (auto& [arrayName, _] : instruction.use)
useUnionB[arrayName] = useUnionB[arrayName].Union(instruction.use[arrayName]);
for (auto& [arrayName, _] : useUnionB)
region->array_priv[arrayName] = useUnionB[arrayName].Diff(region->array_use[arrayName]);
2025-05-30 12:45:05 +03:00
2026-02-21 02:34:57 +03:00
instruction.use = move(region->array_use);
for (Region* prevBlock : region->getHeader()->getPrevRegions())
{
prevBlock->replaceInNextRegions(region, region->getHeader());
2026-02-21 02:34:57 +03:00
region->addPrevRegion(prevBlock);
}
for (Region* nextBlock : region->getHeader()->getNextRegions())
2026-02-21 02:34:57 +03:00
{
nextBlock->replaceInPrevRegions(region, region->getHeader());
2026-02-21 02:34:57 +03:00
region->addNextRegion(nextBlock);
}
region->instructions.push_back(instruction);
2026-04-27 16:18:43 +03:00
if (blockCount == 1 && !recursivePriv.empty())
region->array_priv = move(recursivePriv);
2026-02-21 02:34:57 +03:00
}
2026-05-01 12:23:51 +03:00
static void solveForBasickBlock(Region* block)
2026-02-21 02:34:57 +03:00
{
2026-05-01 12:23:51 +03:00
arrayAccessingIndexes newIn;
2026-02-21 02:34:57 +03:00
bool flagFirst = true;
for (Region* prevBlock : block->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();
}
}
}
if (block->instructions.empty())
block->instructions.push_back(RegionInstruction());
block->instructions[0].in = move(newIn);
for (int i = 0; i < block->instructions.size(); i++)
{
auto& instruction = block->instructions[i];
if (i > 0)
instruction.in = block->instructions[i - 1].out;
2026-05-01 12:23:51 +03:00
arrayAccessingIndexes newOut;
2026-02-21 02:34:57 +03:00
if (instruction.def.empty())
newOut = instruction.in;
else if (instruction.in.empty())
newOut = instruction.def;
else
{
for (auto& [arrayName, accessSet] : instruction.def)
{
if (instruction.in.find(arrayName) != instruction.in.end())
newOut[arrayName] = instruction.def[arrayName].Union(instruction.in[arrayName]);
else
newOut[arrayName] = accessSet;
}
for (auto& [arrayName, accessSet] : instruction.in)
{
if (newOut.find(arrayName) == newOut.end())
newOut[arrayName] = accessSet;
}
}
instruction.out = move(newOut);
}
if (!block->instructions.empty())
block->array_out = block->instructions.back().out;
}
2026-05-01 12:23:51 +03:00
static void solveDataFlowTopologically(Region* DFG)
2026-02-21 02:34:57 +03:00
{
for (Region* b : DFG->getBasickBlocks())
{
collapsed.insert(b);
2026-05-01 12:23:51 +03:00
solveForBasickBlock(b);
2026-02-21 02:34:57 +03:00
}
2025-04-29 17:55:51 +03:00
}
2026-05-01 12:23:51 +03:00
static void solveDataFlow(Region* DFG)
{
2025-05-27 02:25:39 +03:00
if (!DFG)
return;
2026-05-01 12:23:51 +03:00
for (Region* subRegion : DFG->getSubRegions())
2026-02-21 02:34:57 +03:00
{
2026-05-01 12:23:51 +03:00
solveDataFlow(subRegion);
2026-02-21 02:34:57 +03:00
DFG->addBasickBlocks(subRegion);
}
vector<Region*>& blocks = DFG->getBasickBlocks();
auto pos = remove_if(blocks.begin(), blocks.end(), [](Region* r) { return collapsed.find(r) != collapsed.end(); });
blocks.erase(pos, blocks.end());
2026-05-01 12:23:51 +03:00
topologySort(DFG->getBasickBlocks(), DFG->getHeader());
solveDataFlowTopologically(DFG);
collapse(DFG);
2025-03-31 02:50:30 +03:00
}
2026-02-21 02:34:57 +03:00
static bool getArrayDeclaredDimensions(SgArrayRefExp* arrayRef, vector<uint64_t>& declaredDims)
{
if (!arrayRef || !arrayRef->symbol() || !isSgArrayType(arrayRef->symbol()->type()))
return false;
SgArrayType* arrayType = (SgArrayType*)arrayRef->symbol()->type();
int dimCount = arrayType->dimension();
for (int i = 0; i < dimCount; i++)
{
SgExpression* sizeExpr = arrayType->sizeInDim(i);
SgConstantSymb* constValSymb = isSgConstantSymb(sizeExpr->symbol());
2026-03-12 04:25:45 +03:00
SgSubscriptExp* subscriptExpr = isSgSubscriptExp(sizeExpr);
uint64_t dimLength;
2026-02-21 02:34:57 +03:00
if (sizeExpr && sizeExpr->variant() == INT_VAL)
2026-03-12 04:25:45 +03:00
dimLength = stol(sizeExpr->unparse());
2026-02-21 02:34:57 +03:00
else if (constValSymb)
2026-03-12 04:25:45 +03:00
dimLength = stol(constValSymb->constantValue()->unparse());
else if (subscriptExpr)
dimLength = stol(subscriptExpr->rhs()->unparse()) - stol(subscriptExpr->lhs()->unparse());
2026-02-21 02:34:57 +03:00
else
return false;
2026-03-12 04:25:45 +03:00
if (dimLength == 0)
2026-02-21 02:34:57 +03:00
return false;
2026-03-12 04:25:45 +03:00
declaredDims.push_back(dimLength);
2026-02-21 02:34:57 +03:00
}
return true;
}
2026-05-01 12:23:51 +03:00
static DIST::Array* getDistArrayBySymbol(SgSymbol* arrSym, const map<tuple<int, string, string>, pair<DIST::Array*, DIST::ArrayAccessInfo*>>& declaredArrays)
2026-03-26 16:01:14 +03:00
{
if (!arrSym)
return nullptr;
for (auto& [key, val] : declaredArrays)
{
DIST::Array* distArr = val.first;
if (!distArr)
2026-05-01 12:23:51 +03:00
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
2026-03-26 16:01:14 +03:00
Symbol* declSym = distArr->GetDeclSymbol();
if (!declSym)
2026-05-01 12:23:51 +03:00
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
2026-03-26 16:01:14 +03:00
SgSymbol* sgDecl = declSym->GetOriginal();
if (sgDecl && isEqSymbols(sgDecl, arrSym))
return distArr;
}
return nullptr;
}
2026-05-01 12:23:51 +03:00
static bool checkDimensionLength(const AccessingSet& array, const map<tuple<int, string, string>, pair<DIST::Array*, DIST::ArrayAccessInfo*>>& declaredArrays)
2026-02-21 02:34:57 +03:00
{
if (array.GetElements().empty())
return false;
size_t dimCount = array.GetElements()[0].size();
SgArrayRefExp* arrayRef = array.GetElements()[0][0].array;
2026-03-26 16:01:14 +03:00
if (!arrayRef || !arrayRef->symbol())
2026-02-21 02:34:57 +03:00
return false;
2026-03-26 16:01:14 +03:00
2026-03-12 04:25:45 +03:00
vector<uint64_t> declaredDims;
declaredDims.reserve(dimCount);
2026-03-26 16:01:14 +03:00
2026-05-01 12:23:51 +03:00
DIST::Array* distArr = getDistArrayBySymbol(arrayRef->symbol(), declaredArrays);
2026-03-26 16:01:14 +03:00
if (distArr && distArr->GetDimSize() == (int)dimCount)
2026-02-21 02:34:57 +03:00
{
2026-03-26 16:01:14 +03:00
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();
}
2026-02-21 02:34:57 +03:00
}
2026-03-26 16:01:14 +03:00
return false;
2026-02-21 02:34:57 +03:00
}
2026-05-01 12:56:32 +03:00
static int addPrivateArraysToLoop(LoopGraph* loop, const arrayAccessingIndexes& privates, set<SgStatement*>& insertedPrivates,
2026-05-01 12:23:51 +03:00
map<string, vector<Messages>>& SPF_messages,
const map<tuple<int, string, string>, pair<DIST::Array*, DIST::ArrayAccessInfo*>>& declaredArrays)
2025-12-04 08:54:09 +03:00
{
SgStatement* spfStat = new SgStatement(SPF_ANALYSIS_DIR);
spfStat->setlineNumber(loop->loop->lineNumber());
spfStat->setFileName(loop->loop->fileName());
2025-12-04 08:54:09 +03:00
SgExpression* toAdd = new SgExpression(EXPR_LIST, new SgExpression(ACC_PRIVATE_OP), NULL, NULL);
set<SgSymbol*> arraysToInsert;
2026-04-27 16:18:43 +03:00
for (const auto& [arrayName, accessingSet] : privates)
2025-12-04 08:54:09 +03:00
{
2026-04-27 16:18:43 +03:00
int idx = arrayName.find('%');
string name = (idx != -1 ? arrayName.substr(idx+1) : arrayName);
2026-05-01 12:23:51 +03:00
if (!checkDimensionLength(accessingSet, declaredArrays))
2026-04-27 16:18:43 +03:00
{
wstring messageE, messageR;
2026-05-01 12:23:51 +03:00
__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));
2026-02-21 02:34:57 +03:00
continue;
2026-04-27 16:18:43 +03:00
}
2025-12-04 08:54:09 +03:00
for (const auto& arrayElement : accessingSet.GetElements())
{
if (arrayElement.empty())
continue;
arraysToInsert.insert(arrayElement[0].array->symbol());
}
}
spfStat->setExpression(0, *toAdd);
toAdd = toAdd->lhs();
bool first = true;
for (auto& elem : arraysToInsert)
{
if (first)
{
toAdd->setLhs(new SgExpression(EXPR_LIST));
toAdd = toAdd->lhs();
first = false;
}
else
{
toAdd->setRhs(new SgExpression(EXPR_LIST));
toAdd = toAdd->rhs();
}
toAdd->setLhs(new SgVarRefExp(elem));
}
2026-02-21 02:34:57 +03:00
if (arraysToInsert.size() != 0)
{
loop->loop->insertStmtBefore(*spfStat, *loop->loop->controlParent());
insertedPrivates.insert(spfStat);
}
2025-12-04 08:54:09 +03:00
}
2026-05-01 12:56:32 +03:00
int findPrivateArrays(map<string, vector<LoopGraph*>>& loopGraph, map<FuncInfo*,
2026-05-01 12:23:51 +03:00
vector<SAPFOR::BasicBlock*>>& FullIR,
set<SgStatement*>& insertedPrivates,
map<string, vector<Messages>>& SPF_messages,
const map<tuple<int, string, string>, pair<DIST::Array*, DIST::ArrayAccessInfo*>>& declaredArrays)
2026-02-21 02:34:57 +03:00
{
2026-05-01 12:23:51 +03:00
map<LoopGraph*, arrayAccessingIndexes> result;
2025-10-30 06:04:02 +03:00
for (const auto& [fileName, loops] : loopGraph)
2025-03-31 02:50:30 +03:00
{
2026-05-01 12:23:51 +03:00
if (SgFile::switchToFile(fileName) == -1)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
2025-05-19 20:50:35 +03:00
for (const auto& loop : loops)
2025-03-31 02:50:30 +03:00
{
2025-12-19 01:55:23 +03:00
if (!loop->isFor())
continue;
2025-10-30 06:04:02 +03:00
SgStatement* search_func = loop->loop->GetOriginal();
while (search_func && (!isSgProgHedrStmt(search_func)))
search_func = search_func->controlParent();
2026-02-21 02:34:57 +03:00
for (const auto& [funcInfo, blocks] : FullIR)
{
2025-10-30 06:04:02 +03:00
if (funcInfo->fileName == fileName && funcInfo->funcPointer->GetOriginal() == search_func)
{
2026-05-01 12:23:51 +03:00
Region* loopRegion = nullptr;
2026-03-26 16:01:14 +03:00
try
{
loopRegion = new Region(loop, blocks);
}
catch (...)
{
2026-05-01 12:23:51 +03:00
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
2026-03-26 16:01:14 +03:00
}
2026-05-01 12:23:51 +03:00
2025-10-30 06:04:02 +03:00
if (loopRegion->getBasickBlocks().size() <= 1)
{
2026-05-01 12:23:51 +03:00
delete loopRegion;
2025-10-30 06:04:02 +03:00
continue;
}
2026-05-01 12:23:51 +03:00
solveDataFlow(loopRegion);
removeEmptyPoints(loopRegion->array_priv);
2025-10-30 06:04:02 +03:00
result[loop] = loopRegion->array_priv;
2026-05-01 12:23:51 +03:00
delete loopRegion;
2025-10-30 06:04:02 +03:00
}
2025-03-31 02:50:30 +03:00
}
2026-05-01 12:23:51 +03:00
2025-12-04 08:54:09 +03:00
if (result.find(loop) != result.end() && !result[loop].empty())
2026-05-01 12:23:51 +03:00
addPrivateArraysToLoop(loop, result[loop], insertedPrivates, SPF_messages, declaredArrays);
2025-03-31 02:50:30 +03:00
}
}
2026-05-01 12:56:32 +03:00
for (const auto& [loop, accesing] : result)
{
if (accesing.size())
{
__spf_print(1, "found for loop on line %d in file %s\n", loop->lineNum, loop->fileName.c_str());
for (const auto& [name, accesingSet] : accesing)
{
const auto& byDimention = accesingSet.GetElements();
__spf_print(1, " for array %s with dimention %d\n", name.c_str(), byDimention.size());
for (int z = 0; z < byDimention.size(); ++z)
{
__spf_print(1, " dim %d (start, step, tripCount):\n", z);
for (auto& elem : byDimention[z])
__spf_print(1, " [%ld %ld %ld]\n", elem.start, elem.step, elem.tripCount);
}
}
}
}
return insertedPrivates.size();
2026-02-21 02:34:57 +03:00
}