From e46720271bea2116f265cbdac69df38eefddc6d0 Mon Sep 17 00:00:00 2001 From: "O. Nikitin" Date: Mon, 24 Feb 2025 23:21:31 +0300 Subject: [PATCH] Add new operations --- .../PrivateAnalyzer/private_arrays_search.cpp | 162 +++++++++++++++++- .../PrivateAnalyzer/private_arrays_search.h | 27 ++- 2 files changed, 177 insertions(+), 12 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/PrivateAnalyzer/private_arrays_search.cpp b/sapfor/experts/Sapfor_2017/_src/PrivateAnalyzer/private_arrays_search.cpp index e6cab9e..46726cc 100644 --- a/sapfor/experts/Sapfor_2017/_src/PrivateAnalyzer/private_arrays_search.cpp +++ b/sapfor/experts/Sapfor_2017/_src/PrivateAnalyzer/private_arrays_search.cpp @@ -42,7 +42,7 @@ static bool isParentStmt(SgStatement* stmt, SgStatement* parent) } /*returns head block and loop*/ -pair> GetBasicBlocksForLoop(LoopGraph* loop, vector blocks) +static pair> GetBasicBlocksForLoop(LoopGraph* loop, vector blocks) { unordered_set block_loop; SAPFOR::BasicBlock* head_block = nullptr; @@ -72,7 +72,7 @@ pair> GetBasicBlocksForL } -void BuildLoopIndex(map& loopForIndex, LoopGraph* loop) { +static void BuildLoopIndex(map& loopForIndex, LoopGraph* loop) { string index = loop->loopSymbol; loopForIndex[index] = loop; for (const auto& childLoop : loop->children) { @@ -80,7 +80,7 @@ void BuildLoopIndex(map& loopForIndex, LoopGraph* loop) { } } -string FindIndexName(int pos, SAPFOR::BasicBlock* block, map& loopForIndex) { +static string FindIndexName(int pos, SAPFOR::BasicBlock* block, map& loopForIndex) { unordered_set args = {block->getInstructions()[pos]->getInstruction()->getArg1()}; for (int i = pos-1; i >= 0; i--) { @@ -111,7 +111,7 @@ string FindIndexName(int pos, SAPFOR::BasicBlock* block, map return ""; } -int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAccessingIndexes& def, ArrayAccessingIndexes& use) { +static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAccessingIndexes& def, ArrayAccessingIndexes& use) { auto instructions = block->getInstructions(); map loopForIndex; BuildLoopIndex(loopForIndex, loop); @@ -232,7 +232,7 @@ int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAccessingInd } -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++) { @@ -248,8 +248,9 @@ vector FindParticularSolution(const ArrayDimension& dim1, const ArrayD } return {}; } + /* dim1 /\ dim2 */ -ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const ArrayDimension& dim2) +static ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const ArrayDimension& dim2) { vector partSolution = FindParticularSolution(dim1, dim2); if (partSolution.empty()) @@ -279,7 +280,7 @@ ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const ArrayDim } /* dim1 / dim2 */ -vector DimensionDifference(const ArrayDimension& dim1, const ArrayDimension& dim2) +static vector DimensionDifference(const ArrayDimension& dim1, const ArrayDimension& dim2) { ArrayDimension* intersection = DimensionIntersection(dim1, dim2); if (!intersection) @@ -321,7 +322,7 @@ vector DimensionDifference(const ArrayDimension& dim1, const Arr } -vector DimensionUnion(const ArrayDimension& dim1, const ArrayDimension& dim2) +static vector DimensionUnion(const ArrayDimension& dim1, const ArrayDimension& dim2) { vector res; ArrayDimension* inter = DimensionIntersection(dim1, dim2); @@ -339,6 +340,151 @@ vector DimensionUnion(const ArrayDimension& dim1, const ArrayDim return res; } +static vector ElementsIntersection(const vector& firstElement, const vector& secondElement) +{ + if(firstElement.empty() || secondElement.empty()) { + return {}; + } + 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()){ + return {}; + } + } + vector result(dimAmount); + for(size_t i = 0; i < dimAmount; i++) + { + ArrayDimension* resPtr = DimensionIntersection(firstElement[i], secondElement[i]); + if(resPtr) + { + result[i] = *resPtr; + } + else + { + return {}; + } + } + return result; +} + +static vector> ElementsDifference(const vector& firstElement, + const vector& secondElement) +{ + if(firstElement.empty() || secondElement.empty()) { + return {}; + } + 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]); + if(!dimDiff.empty()) + { + vector firstCopy = firstElement; + for(const auto range: dimDiff) + { + firstCopy[i] = range; + result.push_back(firstCopy); + } + } + } + return result; +} + +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); +} + +void AccessingSet::FindUncovered(const vector& element, vector>& result) { + vector> result, newTails; + result.push_back(element); + for(const auto& currentElement: allElements) + { + for(const auto& tailLoc: result) + { + auto intersection = ElementsIntersection(tailLoc, currentElement); + auto diff = ElementsDifference(tailLoc, intersection); + if(!diff.empty()) { + newTails.insert(newTails.end(), diff.begin(), diff.end()); + } + } + result = move(newTails); + } +} + +bool AccessingSet::ContainsElement(const vector& element) +{ + vector> tails; + FindUncovered(element, tails); + return !tails.empty(); +} + +void AccessingSet::FindCoveredBy(const vector& element, vector>& result) +{ + for(const auto& currentElement: allElements) + { + for(const auto& tailLoc: tails) + { + auto intersection = ElementsIntersection(tailLoc, currentElement); + if(!intersection.empty()) { + result.push_back(intersection); + } + } + } +} + +vector>> AccessingSet::GetElements() +{ + return AllElements; +} + +void AccessingSet::Insert(const vector& element) +{ + vector> tails; + FindUncovered(element, tails); + AllElements.insert(AllElements.end(), tails.begin(), tails.end()); +} + +void AccessingSet::Union(const AccessingSet& source) { + for(const auto element: source.GetElements()) { + Insert(element); + } +} + +vector> AccessingSet::Intersect(const AccessingSet& secondSet) +{ + vector> result; + for(const auto& element: AllElements) + { + if(ContainsElement(secondSet), element) + { + result.push_back(element) + } + else + { + vector coveredBy; + FindCoveredBy(secondSet, element, coveredBy); + if(!coveredBy.empty()) + { + result.insert(result.end(), coveredBy.begin(), coveredBy.end()); + } + } + } + return result; +} + void FindPrivateArrays(map> &loopGraph, map>& FullIR) { for (const auto& curr_graph_pair: loopGraph) diff --git a/sapfor/experts/Sapfor_2017/_src/PrivateAnalyzer/private_arrays_search.h b/sapfor/experts/Sapfor_2017/_src/PrivateAnalyzer/private_arrays_search.h index 97ecd47..3375bf1 100644 --- a/sapfor/experts/Sapfor_2017/_src/PrivateAnalyzer/private_arrays_search.h +++ b/sapfor/experts/Sapfor_2017/_src/PrivateAnalyzer/private_arrays_search.h @@ -3,13 +3,32 @@ #include "../GraphLoop/graph_loops.h" #include "../CFGraph/CFGraph.h" +using std::vector; +using std::map; +using std::string; +using std::set; + struct ArrayDimension { uint64_t start, step, tripCount; }; -typedef std::map>> ArrayAccessingIndexes; +typedef map ArrayAccessingIndexes; -void FindPrivateArrays(std::map>& loopGraph, std::map>& FullIR); -void GetDimensionInfo(LoopGraph* loop, std::map>>& loopDimensionsInfo, int level); -std::set GetBasicBlocksForLoop(LoopGraph* loop, std::vector); +class AccessingSet { + prinvate: + vector>> AllElements; + bool ContainsElement(const vector& element); + void FindCoveredBy(const vector& element, vector>& result); + void FindUncovered(const vector& element, vector>& result); + public: + AccessingSet(vector>> input): AllElements(input) {}; + vector>> GetElements(); + void Insert(const vector& element); + void Union(const AccessingSet& source); + vector> Intersect(const AccessingSet& secondSet); +}; + +void FindPrivateArrays(map>& loopGraph, map>& FullIR); +void GetDimensionInfo(LoopGraph* loop, map>>& loopDimensionsInfo, int level); +set GetBasicBlocksForLoop(LoopGraph* loop, vector);