diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index e070d17..c7e89b7 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -20,14 +20,11 @@ using namespace std; -extern std::map, std::pair> declaredArrays; -extern map> SPF_messages; - static set collapsed; -static void RemoveEmptyPoints(ArrayAccessingIndexes& container) +static void removeEmptyPoints(arrayAccessingIndexes& container) { - ArrayAccessingIndexes resultContainer; + arrayAccessingIndexes resultContainer; set toRemove; for (auto& [arrayName, accessingSet] : container) @@ -52,7 +49,7 @@ static void RemoveEmptyPoints(ArrayAccessingIndexes& container) container[arrayName] = accessingSet; } -static void Collapse(Region* region) +static void collapse(Region* region) { if (region->getBasickBlocks().empty()) return; @@ -90,7 +87,7 @@ static void Collapse(Region* region) RegionInstruction instruction; instruction.def = move(region->array_def); - ArrayAccessingIndexes recursivePriv; + arrayAccessingIndexes recursivePriv; for (auto& byBlock : region->getBasickBlocks()) { if (!byBlock->array_priv.empty()) @@ -107,7 +104,7 @@ static void Collapse(Region* region) } } - ArrayAccessingIndexes useUnionB; + arrayAccessingIndexes useUnionB; for (auto& byBlock : region->getBasickBlocks()) for (auto& instruction : byBlock->instructions) for (auto& [arrayName, _] : instruction.use) @@ -134,9 +131,9 @@ static void Collapse(Region* region) region->array_priv = move(recursivePriv); } -static void SolveForBasickBlock(Region* block) +static void solveForBasickBlock(Region* block) { - ArrayAccessingIndexes newIn; + arrayAccessingIndexes newIn; bool flagFirst = true; for (Region* prevBlock : block->getPrevRegions()) { @@ -175,7 +172,7 @@ static void SolveForBasickBlock(Region* block) if (i > 0) instruction.in = block->instructions[i - 1].out; - ArrayAccessingIndexes newOut; + arrayAccessingIndexes newOut; if (instruction.def.empty()) newOut = instruction.in; else if (instruction.in.empty()) @@ -192,9 +189,7 @@ static void SolveForBasickBlock(Region* block) for (auto& [arrayName, accessSet] : instruction.in) { if (newOut.find(arrayName) == newOut.end()) - { newOut[arrayName] = accessSet; - } } } @@ -204,30 +199,31 @@ static void SolveForBasickBlock(Region* block) block->array_out = block->instructions.back().out; } -static void SolveDataFlowTopologically(Region* DFG) +static void solveDataFlowTopologically(Region* DFG) { for (Region* b : DFG->getBasickBlocks()) { collapsed.insert(b); - SolveForBasickBlock(b); + solveForBasickBlock(b); } } -static void SolveDataFlow(Region* DFG) +static void solveDataFlow(Region* DFG) { if (!DFG) return; + for (Region* subRegion : DFG->getSubRegions()) { - SolveDataFlow(subRegion); + solveDataFlow(subRegion); DFG->addBasickBlocks(subRegion); } vector& blocks = DFG->getBasickBlocks(); auto pos = remove_if(blocks.begin(), blocks.end(), [](Region* r) { return collapsed.find(r) != collapsed.end(); }); blocks.erase(pos, blocks.end()); - TopologySort(DFG->getBasickBlocks(), DFG->getHeader()); - SolveDataFlowTopologically(DFG); - Collapse(DFG); + topologySort(DFG->getBasickBlocks(), DFG->getHeader()); + solveDataFlowTopologically(DFG); + collapse(DFG); } static bool getArrayDeclaredDimensions(SgArrayRefExp* arrayRef, vector& declaredDims) @@ -247,9 +243,7 @@ static bool getArrayDeclaredDimensions(SgArrayRefExp* arrayRef, vector else if (constValSymb) dimLength = stol(constValSymb->constantValue()->unparse()); else if (subscriptExpr) - { dimLength = stol(subscriptExpr->rhs()->unparse()) - stol(subscriptExpr->lhs()->unparse()); - } else return false; @@ -260,7 +254,7 @@ static bool getArrayDeclaredDimensions(SgArrayRefExp* arrayRef, vector return true; } -static DIST::Array* getDistArrayBySymbol(SgSymbol* arrSym) +static DIST::Array* getDistArrayBySymbol(SgSymbol* arrSym, const map, pair>& declaredArrays) { if (!arrSym) return nullptr; @@ -268,10 +262,10 @@ static DIST::Array* getDistArrayBySymbol(SgSymbol* arrSym) { DIST::Array* distArr = val.first; if (!distArr) - continue; + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); Symbol* declSym = distArr->GetDeclSymbol(); if (!declSym) - continue; + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); SgSymbol* sgDecl = declSym->GetOriginal(); if (sgDecl && isEqSymbols(sgDecl, arrSym)) return distArr; @@ -279,7 +273,7 @@ static DIST::Array* getDistArrayBySymbol(SgSymbol* arrSym) return nullptr; } -static bool CheckDimensionLength(const AccessingSet& array) +static bool checkDimensionLength(const AccessingSet& array, const map, pair>& declaredArrays) { if (array.GetElements().empty()) return false; @@ -291,7 +285,7 @@ static bool CheckDimensionLength(const AccessingSet& array) vector declaredDims; declaredDims.reserve(dimCount); - DIST::Array* distArr = getDistArrayBySymbol(arrayRef->symbol()); + DIST::Array* distArr = getDistArrayBySymbol(arrayRef->symbol(), declaredArrays); if (distArr && distArr->GetDimSize() == (int)dimCount) { const auto& sizes = distArr->GetSizes(); @@ -317,7 +311,9 @@ static bool CheckDimensionLength(const AccessingSet& array) return false; } -static void AddPrivateArraysToLoop(LoopGraph* loop, const ArrayAccessingIndexes& privates, set& insertedPrivates) +static void addPrivateArraysToLoop(LoopGraph* loop, const arrayAccessingIndexes& privates, set& insertedPrivates, + map>& SPF_messages, + const map, pair>& declaredArrays) { SgStatement* spfStat = new SgStatement(SPF_ANALYSIS_DIR); spfStat->setlineNumber(loop->loop->lineNumber()); @@ -328,19 +324,12 @@ static void AddPrivateArraysToLoop(LoopGraph* loop, const ArrayAccessingIndexes& { int idx = arrayName.find('%'); string name = (idx != -1 ? arrayName.substr(idx+1) : arrayName); - if (!CheckDimensionLength(accessingSet)) + if (!checkDimensionLength(accessingSet, declaredArrays)) { wstring messageE, messageR; - __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)); + __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)); continue; } @@ -378,12 +367,18 @@ static void AddPrivateArraysToLoop(LoopGraph* loop, const ArrayAccessingIndexes& } } -void findPrivateArrays(map>& loopGraph, map>& FullIR, set& insertedPrivates) +void findPrivateArrays(map>& loopGraph, map>& FullIR, + set& insertedPrivates, + map>& SPF_messages, + const map, pair>& declaredArrays) { - map result; + map result; for (const auto& [fileName, loops] : loopGraph) { - SgFile::switchToFile(fileName); + if (SgFile::switchToFile(fileName) == -1) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + for (const auto& loop : loops) { if (!loop->isFor()) @@ -397,28 +392,31 @@ void findPrivateArrays(map>& loopGraph, mapfileName == fileName && funcInfo->funcPointer->GetOriginal() == search_func) { - Region* loopRegion; + Region* loopRegion = nullptr; try { loopRegion = new Region(loop, blocks); } catch (...) { - continue; + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); } + if (loopRegion->getBasickBlocks().size() <= 1) { - delete(loopRegion); + delete loopRegion; continue; } - SolveDataFlow(loopRegion); - RemoveEmptyPoints(loopRegion->array_priv); + + solveDataFlow(loopRegion); + removeEmptyPoints(loopRegion->array_priv); result[loop] = loopRegion->array_priv; - delete(loopRegion); + delete loopRegion; } } + if (result.find(loop) != result.end() && !result[loop].empty()) - AddPrivateArraysToLoop(loop, result[loop], insertedPrivates); + addPrivateArraysToLoop(loop, result[loop], insertedPrivates, SPF_messages, declaredArrays); } } } \ No newline at end of file diff --git a/src/PrivateAnalyzer/private_arrays_search.h b/src/PrivateAnalyzer/private_arrays_search.h index cb14a24..06fa7c4 100644 --- a/src/PrivateAnalyzer/private_arrays_search.h +++ b/src/PrivateAnalyzer/private_arrays_search.h @@ -3,10 +3,12 @@ #include #include #include -#include #include "range_structures.h" #include "graph_loops.h" #include "CFGraph/CFGraph.h" -void findPrivateArrays(std::map>& loopGraph, std::map>& FullIR, std::set& insertedPrivates); +void findPrivateArrays(std::map>& loopGraph, std::map>& FullIR, std::set& insertedPrivates, + std::map>& SPF_messages, + const std::map, std::pair>& declaredArrays); diff --git a/src/PrivateAnalyzer/range_structures.cpp b/src/PrivateAnalyzer/range_structures.cpp index 340a9e8..a93d5bb 100644 --- a/src/PrivateAnalyzer/range_structures.cpp +++ b/src/PrivateAnalyzer/range_structures.cpp @@ -8,7 +8,7 @@ using namespace std; -static vector FindParticularSolution(const ArrayDimension& dim1, const ArrayDimension& dim2) +static vector findParticularSolution(const ArrayDimension& dim1, const ArrayDimension& dim2) { for (uint64_t i = 0; i < dim1.tripCount; i++) { @@ -24,9 +24,9 @@ static vector FindParticularSolution(const ArrayDimension& dim1, const } /* dim1 /\ dim2 */ -static ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const ArrayDimension& dim2) +static ArrayDimension* dimensionIntersection(const ArrayDimension& dim1, const ArrayDimension& dim2) { - vector partSolution = FindParticularSolution(dim1, dim2); + vector partSolution = findParticularSolution(dim1, dim2); if (partSolution.empty()) return NULL; @@ -52,9 +52,9 @@ static ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const A } /* dim1 / dim2 */ -static vector DimensionDifference(const ArrayDimension& dim1, const ArrayDimension& dim2) +static vector dimensionDifference(const ArrayDimension& dim1, const ArrayDimension& dim2) { - ArrayDimension* intersection = DimensionIntersection(dim1, dim2); + ArrayDimension* intersection = dimensionIntersection(dim1, dim2); if (!intersection) return { dim1 }; @@ -87,10 +87,10 @@ static vector DimensionDifference(const ArrayDimension& dim1, co } -static vector DimensionUnion(const ArrayDimension& dim1, const ArrayDimension& dim2) +static vector dimensionUnion(const ArrayDimension& dim1, const ArrayDimension& dim2) { vector res; - ArrayDimension* inter = DimensionIntersection(dim1, dim2); + ArrayDimension* inter = dimensionIntersection(dim1, dim2); if (!inter) return { dim1, dim2 }; @@ -98,14 +98,14 @@ static vector DimensionUnion(const ArrayDimension& dim1, const A delete(inter); vector diff1, diff2; - diff1 = DimensionDifference(dim1, dim2); - diff2 = DimensionDifference(dim2, dim1); + diff1 = dimensionDifference(dim1, dim2); + diff2 = dimensionDifference(dim2, dim1); res.insert(res.end(), diff1.begin(), diff1.end()); res.insert(res.end(), diff2.begin(), diff2.end()); return res; } -static vector ElementsIntersection(const vector& firstElement, const vector& secondElement) +static vector elementsIntersection(const vector& firstElement, const vector& secondElement) { if (firstElement.empty() || secondElement.empty()) return {}; @@ -113,13 +113,13 @@ static vector ElementsIntersection(const vector& size_t dimAmount = firstElement.size(); /* check if there is no intersecction */ for (size_t i = 0; i < dimAmount; i++) - if (FindParticularSolution(firstElement[i], secondElement[i]).empty()) + if (findParticularSolution(firstElement[i], secondElement[i]).empty()) return {}; vector result(dimAmount); for (size_t i = 0; i < dimAmount; i++) { - ArrayDimension* resPtr = DimensionIntersection(firstElement[i], secondElement[i]); + ArrayDimension* resPtr = dimensionIntersection(firstElement[i], secondElement[i]); if (resPtr) result[i] = *resPtr; else @@ -129,7 +129,7 @@ static vector ElementsIntersection(const vector& return result; } -static vector> ElementsDifference(const vector& firstElement, +static vector> elementsDifference(const vector& firstElement, const vector& secondElement) { if (firstElement.empty()) @@ -137,14 +137,14 @@ static vector> ElementsDifference(const vector intersection = ElementsIntersection(firstElement, secondElement); + vector intersection = elementsIntersection(firstElement, secondElement); vector> result; if (intersection.empty()) return { firstElement }; for (int i = 0; i < firstElement.size(); i++) { - auto dimDiff = DimensionDifference(firstElement[i], secondElement[i]); + auto dimDiff = dimensionDifference(firstElement[i], secondElement[i]); if (!dimDiff.empty()) { vector firstCopy = firstElement; @@ -158,14 +158,14 @@ static vector> ElementsDifference(const vector& firstElement, const vector& secondElement, +static void elementsUnion(const vector& firstElement, const vector& secondElement, vector>& lc, vector>& rc, vector& intersection) { /* lc(rc) is a set of ranges, which only exist in first(second) element*/ - intersection = ElementsIntersection(firstElement, secondElement); - lc = ElementsDifference(firstElement, intersection); - rc = ElementsDifference(secondElement, intersection); + intersection = elementsIntersection(firstElement, secondElement); + lc = elementsDifference(firstElement, intersection); + rc = elementsDifference(secondElement, intersection); } void AccessingSet::FindUncovered(const vector& element, vector>& result) const { @@ -175,8 +175,8 @@ void AccessingSet::FindUncovered(const vector& element, vector& element, vector; +using arrayAccessingIndexes = std::map; bool operator!=(const ArrayDimension& lhs, const ArrayDimension& rhs); bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs); -bool operator!=(const ArrayAccessingIndexes& lhs, const ArrayAccessingIndexes& rhs); \ No newline at end of file +bool operator!=(const arrayAccessingIndexes& lhs, const arrayAccessingIndexes& rhs); \ No newline at end of file diff --git a/src/PrivateAnalyzer/region.cpp b/src/PrivateAnalyzer/region.cpp index 26fb1b7..8b226bf 100644 --- a/src/PrivateAnalyzer/region.cpp +++ b/src/PrivateAnalyzer/region.cpp @@ -23,7 +23,7 @@ static bool isParentStmt(SgStatement* stmt, SgStatement* parent) } /*returns head block and loop*/ -pair> GetBasicBlocksForLoop(const LoopGraph* loop, const vector blocks) +pair> getBasicBlocksForLoop(const LoopGraph* loop, const vector blocks) { set block_loop; SAPFOR::BasicBlock* head_block = nullptr; @@ -51,15 +51,15 @@ pair> GetBasicBlocksForLoop(const return { head_block, block_loop }; } -static void BuildLoopIndex(map& loopForIndex, LoopGraph* loop) { +static void buildLoopIndex(map& loopForIndex, LoopGraph* loop) { string index = loop->loopSymbol(); loopForIndex[loop->loop->GetOriginal()] = loop; for (const auto& childLoop : loop->children) - BuildLoopIndex(loopForIndex, childLoop); + buildLoopIndex(loopForIndex, childLoop); } -static string FindIndexName(int pos, SAPFOR::BasicBlock* block, map& loopForIndex) { +static string findIndexName(int pos, SAPFOR::BasicBlock* block, map& loopForIndex) { set args = { block->getInstructions()[pos]->getInstruction()->getArg1() }; for (int i = pos - 1; i >= 0; i--) @@ -93,10 +93,10 @@ static string FindIndexName(int pos, SAPFOR::BasicBlock* block, mapgetInstructions(); map loopForIndex; - BuildLoopIndex(loopForIndex, loop); + buildLoopIndex(loopForIndex, loop); for (int i = 0; i < instructions.size(); i++) { auto instruction = instructions[i]; @@ -233,7 +233,7 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces } -static void RemoveHeaderConnection(SAPFOR::BasicBlock* header, const set& blockSet, map& bbToRegion) +static void removeHeaderConnection(SAPFOR::BasicBlock* header, const set& blockSet, map& bbToRegion) { for (SAPFOR::BasicBlock* block : blockSet) { @@ -249,10 +249,7 @@ static void RemoveHeaderConnection(SAPFOR::BasicBlock* header, const set& result, - const set& cycleBlocks, - map& color) +static bool DFS(Region* block, vector& result, const set& cycleBlocks, map& color) { auto it = color.find(block); if (it != color.end()) @@ -275,7 +272,7 @@ static bool DFS(Region* block, return true; } -bool HasCycle(Region* block, const std::set& cycleBlocks, std::set& visitedBlocks) +bool hasCycle(Region* block, const std::set& cycleBlocks, std::set& visitedBlocks) { return false; if (visitedBlocks.find(block) != visitedBlocks.end()) @@ -283,13 +280,13 @@ bool HasCycle(Region* block, const std::set& cycleBlocks, std::setgetNextRegions()) { - if (cycleBlocks.find(nextBlock) != cycleBlocks.end() && HasCycle(nextBlock, cycleBlocks, visitedBlocks)) + if (cycleBlocks.find(nextBlock) != cycleBlocks.end() && hasCycle(nextBlock, cycleBlocks, visitedBlocks)) return true; } return false; } -bool TopologySort(std::vector& basikBlocks, Region* header) +bool topologySort(std::vector& basikBlocks, Region* header) { set cycleBlocks(basikBlocks.begin(), basikBlocks.end()); vector result; @@ -301,7 +298,7 @@ bool TopologySort(std::vector& basikBlocks, Region* header) return true; } -static void SetConnections(map& bbToRegion, const set& blockSet) +static void setConnections(map& bbToRegion, const set& blockSet) { for (SAPFOR::BasicBlock* block : blockSet) { @@ -315,11 +312,11 @@ static void SetConnections(map& bbToRegion, const } } -static Region* CreateSubRegion(LoopGraph* loop, const vector& Blocks, map& bbToRegion) +static Region* createSubRegion(LoopGraph* loop, const vector& Blocks, map& bbToRegion) { Region* region = new Region; - auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); - RemoveHeaderConnection(header, blockSet, bbToRegion); + auto [header, blockSet] = getBasicBlocksForLoop(loop, Blocks); + removeHeaderConnection(header, blockSet, bbToRegion); if (bbToRegion.find(header) != bbToRegion.end()) region->setHeader(bbToRegion.at(header)); else @@ -336,34 +333,34 @@ static Region* CreateSubRegion(LoopGraph* loop, const vectorisFor()) continue; - region->addSubRegions(CreateSubRegion(childLoop, Blocks, bbToRegion)); + region->addSubRegions(createSubRegion(childLoop, Blocks, bbToRegion)); } - if (!TopologySort(region->getBasickBlocks(), region->getHeader())) + if (!topologySort(region->getBasickBlocks(), region->getHeader())) throw std::runtime_error("Unnoticed cycle"); return region; } Region::Region(LoopGraph* loop, const vector& Blocks) { - auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks); + auto [header, blockSet] = getBasicBlocksForLoop(loop, Blocks); map bbToRegion; for (auto poiner : blockSet) { bbToRegion[poiner] = new Region(*poiner); this->basickBlocks.push_back(bbToRegion[poiner]); - GetDefUseArray(poiner, loop, bbToRegion[poiner]->array_def, bbToRegion[poiner]->array_use, bbToRegion[poiner]); + getDefUseArray(poiner, loop, bbToRegion[poiner]->array_def, bbToRegion[poiner]->array_use, bbToRegion[poiner]); } this->header = bbToRegion[header]; - SetConnections(bbToRegion, blockSet); - RemoveHeaderConnection(header, blockSet, bbToRegion); + setConnections(bbToRegion, blockSet); + removeHeaderConnection(header, blockSet, bbToRegion); //create subRegions for (LoopGraph* childLoop : loop->children) { if (!childLoop->isFor()) continue; - subRegions.insert(CreateSubRegion(childLoop, Blocks, bbToRegion)); + subRegions.insert(createSubRegion(childLoop, Blocks, bbToRegion)); } - if (!TopologySort(basickBlocks, this->header)) + if (!topologySort(basickBlocks, this->header)) throw std::runtime_error("Unnoticed cycle"); } \ No newline at end of file diff --git a/src/PrivateAnalyzer/region.h b/src/PrivateAnalyzer/region.h index adecfe6..26d4a06 100644 --- a/src/PrivateAnalyzer/region.h +++ b/src/PrivateAnalyzer/region.h @@ -10,7 +10,7 @@ struct RegionInstruction { - ArrayAccessingIndexes def, use, in, out; + arrayAccessingIndexes def, use, in, out; }; class Region : public SAPFOR::BasicBlock { @@ -67,7 +67,7 @@ public: std::vector instructions; - ArrayAccessingIndexes array_def, array_use, array_out, array_in, array_priv; + arrayAccessingIndexes array_def, array_use, array_out, array_in, array_priv; private: std::vector basickBlocks; @@ -79,6 +79,6 @@ private: Region* header; }; -bool HasCycle(Region* block, const std::set& cycleBlocks, std::set& visitedBlocks); +bool hasCycle(Region* block, const std::set& cycleBlocks, std::set& visitedBlocks); -bool TopologySort(std::vector& basikBlocks, Region* header); \ No newline at end of file +bool topologySort(std::vector& basikBlocks, Region* header); \ No newline at end of file diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index fcb4294..9a28e34 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -1905,7 +1905,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne else if (curr_regime == TRANSFORM_ASSUMED_SIZE_PARAMETERS) transformAssumedSizeParameters(allFuncInfo); else if (curr_regime == FIND_PRIVATE_ARRAYS_ANALYSIS) - findPrivateArrays(loopGraph, fullIR, insertedPrivates); + findPrivateArrays(loopGraph, fullIR, insertedPrivates, SPF_messages, declaredArrays); else if (curr_regime == MERGE_REGIONS) mergeRegions(parallelRegions, allFuncInfo); else if (curr_regime == ARRAY_PROPAGATION)