From a78606c882afbec044edd9491f2073fa8a8121ff Mon Sep 17 00:00:00 2001 From: Oleg Nikitin Date: Thu, 4 Dec 2025 08:54:09 +0300 Subject: [PATCH] add directives --- src/PrivateAnalyzer/private_arrays_search.cpp | 73 +++++++++++++++++++ src/PrivateAnalyzer/range_structures.cpp | 8 +- src/PrivateAnalyzer/range_structures.h | 3 + src/PrivateAnalyzer/region.cpp | 1 + 4 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index eec5d5b..47d28d3 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -121,6 +121,75 @@ static void SolveDataFlow(Region* DFG) Collapse(DFG); } +unsigned long long CalculateLength(const AccessingSet& array) +{ + if (array.GetElements().empty()) + return 0; + unsigned long long result = 1; + for (const auto& range : array.GetElements()) + { + for (const auto& dim : range) + { + result *= (dim.start + dim.step * dim.tripCount); + } + } + return result; +} + +void AddPrivateArraysToLoop(LoopGraph* loop, ArrayAccessingIndexes privates) +{ + SgStatement* privSpf = new SgStatement(SPF_ANALYSIS_DIR, NULL, NULL, NULL, NULL, NULL); + + SgExpression* tmp = new SgExpression(ACC_PRIVATE_OP); + SgExpression* exprList = new SgExpression(EXPR_LIST, tmp, NULL, NULL); + privSpf->setExpression(0, *exprList); + exprList = exprList->lhs(); + + SgExpression* tmp1 = new SgExpression(EXPR_LIST); + exprList->setLhs(tmp1); + exprList = exprList->lhs(); + vector arraysToInsert; + for (const auto& [_, accessingSet] : privates) + { + for (const auto& arrayElement : accessingSet.GetElements()) + { + if (arrayElement.empty()) + continue; + + auto arrayType = isSgArrayType(arrayElement[0].array->symbol()->type()); + SgExpression* dimList = NULL; + if (arrayType) + { + dimList = arrayType->getDimList(); + for (int i = 0; i < arrayType->length()->valueInteger(); i++) + { + int lb = dimList[i].lhs()->valueInteger(); + int ub = dimList[i].rhs()->valueInteger(); + int expectedLength = ub - lb + 1; + if (expectedLength == CalculateLength(accessingSet)) + { + arraysToInsert.push_back(arrayElement[0].array); + } + } + } + } + } + if (!arraysToInsert.empty()) + { + for (int i = 0; i < arraysToInsert.size(); i++) + { + exprList->setLhs(arraysToInsert[i]); + if (i < arraysToInsert.size() - 1) + { + SgExpression* tmp = new SgExpression(EXPR_LIST); + exprList->setRhs(tmp); + exprList = exprList->rhs(); + } + } + loop->loop->GetOriginal()->addAttribute(SPF_ANALYSIS_DIR, privSpf, sizeof(SgStatement)); + } +} + map FindPrivateArrays(map> &loopGraph, map>& FullIR) { map result; @@ -149,6 +218,10 @@ map FindPrivateArrays(map DimensionDifference(const ArrayDimension& dim1, co vector result; /* add the part before intersection */ if (dim1.start < intersection->start) - result.push_back({ dim1.start, dim1.step, (intersection->start - dim1.start) / dim1.step }); + result.push_back({ dim1.start, dim1.step, (intersection->start - dim1.start) / dim1.step, dim1.array}); /* add the parts between intersection steps */ uint64_t start = (intersection->start - dim1.start) / dim1.step; @@ -73,7 +73,7 @@ static vector DimensionDifference(const ArrayDimension& dim1, co { if (i - start > 1) { - result.push_back({ dim1.start + (start + 1) * dim1.step, dim1.step, i - start - 1 }); + result.push_back({ dim1.start + (start + 1) * dim1.step, dim1.step, i - start - 1, dim1.array }); start = i; } interValue += intersection->step; @@ -85,7 +85,7 @@ static vector DimensionDifference(const ArrayDimension& dim1, co /* first value after intersection */ uint64_t right_start = intersection->start + intersection->step * (intersection->tripCount - 1) + dim1.step; uint64_t tripCount = (dim1.start + dim1.step * dim1.tripCount - right_start) / dim1.step; - result.push_back({ right_start, dim1.step, tripCount }); + result.push_back({ right_start, dim1.step, tripCount, dim1.array }); } delete(intersection); return result; diff --git a/src/PrivateAnalyzer/range_structures.h b/src/PrivateAnalyzer/range_structures.h index 4b52f97..6a609fc 100644 --- a/src/PrivateAnalyzer/range_structures.h +++ b/src/PrivateAnalyzer/range_structures.h @@ -6,9 +6,12 @@ #include #include +#include "SgUtils.h" + struct ArrayDimension { uint64_t start, step, tripCount; + SgArrayRefExp* array; }; class AccessingSet { diff --git a/src/PrivateAnalyzer/region.cpp b/src/PrivateAnalyzer/region.cpp index 252c9d2..21c5c65 100644 --- a/src/PrivateAnalyzer/region.cpp +++ b/src/PrivateAnalyzer/region.cpp @@ -148,6 +148,7 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces int currentVarPos = refPos.back(); pair currentCoefs = coefsForDims.back(); ArrayDimension current_dim; + current_dim.array = ref; if (var->getType() == SAPFOR::CFG_ARG_TYPE::CONST) current_dim = { stoul(var->getValue()), 1, 1 }; else