refactored

This commit is contained in:
ALEXks
2026-04-19 20:25:20 +03:00
parent 7d45f5babc
commit d77b6d5288
7 changed files with 170 additions and 137 deletions

View File

@@ -161,7 +161,9 @@ set(PARALLEL_REG src/ParallelizationRegions/ParRegions.cpp
src/ParallelizationRegions/expand_extract_reg.cpp
src/ParallelizationRegions/expand_extract_reg.h
src/ParallelizationRegions/resolve_par_reg_conflicts.cpp
src/ParallelizationRegions/resolve_par_reg_conflicts.h)
src/ParallelizationRegions/resolve_par_reg_conflicts.h
src/ParallelizationRegions/uniq_name_creator.cpp
src/ParallelizationRegions/uniq_name_creator.h)
set(ARRAY_PROP src/ArrayConstantPropagation/propagation.cpp
src/ArrayConstantPropagation/propagation.h

View File

@@ -1,14 +1,16 @@
#include "leak_detector.h"
#include <set>
#include <map>
#include "merge_regions.h"
#include <unordered_set>
using std::map;
using std::set;
using std::pair;
using std::string;
using std::unordered_map;
using std::unordered_set;
using std::vector;
//TODO: need to create new clause!!
static void parseMergeDirective(const char *comment,
vector<pair<string, string>> &parsed_mapping)
{
@@ -16,21 +18,27 @@ static void parseMergeDirective(const char *comment,
{
auto *line_end = strchr(comment, '\n');
static const char prefix[] = "!!SPF TRANSFORM(MERGE_ARRAYS(";
static const char prefix[] = "!!spf transform(merge_arrays(";
static const auto compare_chars = sizeof(prefix) - 1;
if (strncasecmp(comment, prefix, compare_chars) == 0)
if (strlen(comment) >= compare_chars)
{
auto *pair_start = comment + compare_chars;
auto *comma = strchr(pair_start, ',');
if (comma)
std::string comment_cmp(comment, compare_chars);
convertToLower(comment_cmp);
if (comment_cmp == prefix)
{
auto *close_br = strchr(comma + 1, ')');
if (close_br)
auto* pair_start = comment + compare_chars;
auto* comma = strchr(pair_start, ',');
if (comma)
{
parsed_mapping.emplace_back(
string(pair_start, comma - pair_start),
string(comma + 1, close_br - comma - 1));
auto* close_br = strchr(comma + 1, ')');
if (close_br)
{
parsed_mapping.emplace_back(
string(pair_start, comma - pair_start),
string(comma + 1, close_br - comma - 1));
}
}
}
}
@@ -78,10 +86,10 @@ static bool hasSameSizes(DIST::Array *a, DIST::Array *b)
}
static bool checkSimilarTemplates(vector<ParallelRegion *> &regions,
const unordered_map<string, string> &new_region_mapping)
const map<string, string> &new_region_mapping)
{
// new region -> old regions
unordered_map<string, unordered_set<string>> new_region_inverse_mapping;
map<string, set<string>> new_region_inverse_mapping;
for (const auto &p : new_region_mapping)
new_region_inverse_mapping[p.second].insert(p.first);
@@ -126,8 +134,8 @@ static bool checkSimilarTemplates(vector<ParallelRegion *> &regions,
return true;
}
static bool hasSameAlignment(const std::unordered_set<const AlignRule *> &align_a,
const std::unordered_set<const AlignRule *> &align_b)
static bool hasSameAlignment(const std::set<const AlignRule *> &align_a,
const std::set<const AlignRule *> &align_b)
{
if (align_a.size() != 1 || align_b.size() != 1)
return false;
@@ -238,7 +246,7 @@ SgSymbol *insertDeclIfNeeded(const string &array_name,
const string &common_block_name,
DIST::Array *example_array,
FuncInfo *dest,
unordered_map<FuncInfo *, unordered_map<string, SgSymbol *>> &inserted_arrays)
map<FuncInfo *, map<string, SgSymbol *>> &inserted_arrays)
{
auto *type = GetArrayType(example_array);
@@ -278,7 +286,7 @@ SgSymbol *insertDeclIfNeeded(const string &array_name,
static pair<string, string> createNewArray(DIST::Array *example_array, const string &base_name,
const map<string, vector<FuncInfo *>> &allFuncInfo,
unordered_map<FuncInfo *, unordered_map<string, SgSymbol *>> &inserted_arrays)
map<FuncInfo *, map<string, SgSymbol *>> &inserted_arrays)
{
auto common_block_name = base_name + "_merge_r";
auto array_name = base_name;
@@ -303,12 +311,12 @@ static pair<string, string> createNewArray(DIST::Array *example_array, const str
}
static void replaceArrayRec(SgExpression *e,
const unordered_set<string> &arrays_to_replace,
const set<string> &arrays_to_replace,
SgSymbol **func_symbol_hint,
const pair<string, string> &replace_by,
DIST::Array *example_array,
FuncInfo *func,
unordered_map<FuncInfo *, unordered_map<string, SgSymbol *>> &inserted_arrays)
map<FuncInfo *, map<string, SgSymbol *>> &inserted_arrays)
{
if (!e)
return;
@@ -346,7 +354,7 @@ static void replaceArrayRec(SgExpression *e,
inserted_arrays);
}
static void replaceRegion(SgStatement* st, const unordered_map<string, string> &new_region_mapping)
static void replaceRegion(SgStatement* st, const map<string, string> &new_region_mapping)
{
if (!st)
return;
@@ -430,8 +438,8 @@ void mergeRegions(vector<ParallelRegion *> &regions, const map<string, vector<Fu
// parse directives
// new array name -> current arrays
unordered_map<string, unordered_set<DIST::Array *>> arrays_to_merge;
unordered_map<DIST::Array *, unordered_set<const AlignRule *>> array_alignment;
map<string, set<DIST::Array *>> arrays_to_merge;
map<DIST::Array *, set<const AlignRule *>> array_alignment;
for (const auto &by_file : allFuncInfo)
{
@@ -489,10 +497,10 @@ void mergeRegions(vector<ParallelRegion *> &regions, const map<string, vector<Fu
}
// old region -> new region
unordered_map<string, string> new_region_mapping;
map<string, string> new_region_mapping;
// new array -> new region
unordered_map<string, string> arrays_new_region_mapping;
map<string, string> arrays_new_region_mapping;
vector<string> created_region_names;
for (const auto &by_new_array : arrays_to_merge)
@@ -529,7 +537,7 @@ void mergeRegions(vector<ParallelRegion *> &regions, const map<string, vector<Fu
if (!checkSimilarTemplates(regions, new_region_mapping))
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
unordered_map<FuncInfo *, unordered_map<string, SgSymbol *>> inserted_arrays;
map<FuncInfo *, map<string, SgSymbol *>> inserted_arrays;
for (const auto &by_dest_array : arrays_to_merge)
{
@@ -571,7 +579,7 @@ void mergeRegions(vector<ParallelRegion *> &regions, const map<string, vector<Fu
auto created_array_info = createNewArray(first_element, by_dest_array.first, allFuncInfo, inserted_arrays);
unordered_set<string> arrays_to_replace;
set<string> arrays_to_replace;
for (auto *array_to_merge : copy_arrays)
arrays_to_replace.insert(array_to_merge->GetShortName());

View File

@@ -4,9 +4,7 @@
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <string.h>
@@ -21,12 +19,11 @@
#include "SgUtils.h"
#include "expr_transform.h"
#include "FunctionPurifying/function_purifying.h"
#include "uniq_name_creator.h"
using std::map;
using std::pair;
using std::set;
using std::unordered_set;
using std::unordered_map;
using std::vector;
using std::stack;
using std::string;
@@ -34,9 +31,6 @@ using std::wstring;
using std::to_string;
using std::make_pair;
static const string COMMON_ARRAY_SUFFIX = "_c";
static const string COMMON_BLOCK_SUFFIX = "_r";
static inline int getRegionExplicitLine(SgStatement *startR)
{
checkNull(startR, convertFileName(__FILE__).c_str(), __LINE__);
@@ -78,7 +72,7 @@ static bool isSPF_reg(SgStatement *st)
return st->variant() == SPF_PARALLEL_REG_DIR || st->variant() == SPF_END_PARALLEL_REG_DIR;
}
static const vector<const Variable*> getArraySynonyms(DIST::Array *array)
const vector<const Variable*> getArraySynonyms(DIST::Array *array)
{
auto arrayBlock = allUsedCommonArrays.find(array);
if (arrayBlock == allUsedCommonArrays.end())
@@ -113,103 +107,6 @@ static const vector<const Variable*> getArraySynonyms(DIST::Array *array)
return arrayBlock->second->getVariables(pos);
}
class UniqueNameCreator
{
map<string, vector<FuncInfo*>> func_info;
unordered_set<string> all_declarations;
bool declarations_analyzed = false;
unordered_map<const DIST::Array*, pair<string, string>> generated_names;
static void GetSymbolsRec(SgExpression* exp, unordered_set<string>& add_to)
{
if (!exp)
return;
if (isArrayRef(exp) && exp->symbol() && exp->symbol()->identifier())
add_to.emplace(exp->symbol()->identifier());
GetSymbolsRec(exp->lhs(), add_to);
GetSymbolsRec(exp->rhs(), add_to);
}
void FillDeclarations()
{
all_declarations.clear();
auto* file_before = current_file;
for (const auto& by_file : func_info)
{
if (SgFile::switchToFile(by_file.first) != -1)
{
for (const auto* by_func : by_file.second)
{
SgStatement* st = by_func->funcPointer;
if (st)
st = st->lexNext();
while (st)
{
for (int i = 0; i < 3; i++)
GetSymbolsRec(st->expr(i), all_declarations);
st = st->lexNext();
}
}
}
}
SgFile::switchToFile(file_before->filename());
declarations_analyzed = true;
}
public:
UniqueNameCreator(const map<string, vector<FuncInfo*>> &allFuncInfo)
{
declarations_analyzed = false;
func_info = allFuncInfo;
}
void GetUniqueName(DIST::Array* array, string& array_name, string& common_block_name)
{
auto varsOnPos = getArraySynonyms(array);
if (!varsOnPos.size())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
auto array_plain_name = varsOnPos[0]->getName();
auto it = generated_names.find(array);
if (it != generated_names.end())
{
array_name.assign(it->second.first);
common_block_name.assign(it->second.second);
return;
}
if (!declarations_analyzed)
FillDeclarations();
int v = 1;
auto created_array_name = array_plain_name + COMMON_ARRAY_SUFFIX;
auto created_common_name = array_plain_name + COMMON_BLOCK_SUFFIX;
while (all_declarations.find(created_array_name) != all_declarations.end() ||
all_declarations.find(created_common_name) != all_declarations.end())
{
created_array_name = array_plain_name + "_v" + std::to_string(v) + COMMON_ARRAY_SUFFIX;
created_common_name = array_plain_name + "_v" + std::to_string(v) + COMMON_BLOCK_SUFFIX;
v++;
}
all_declarations.insert(created_array_name);
all_declarations.insert(created_common_name);
generated_names.emplace(array, std::make_pair(created_array_name, created_common_name));
array_name.assign(created_array_name);
common_block_name.assign(created_common_name);
}
};
static string getStringDeclaration(SgSymbol *symb)
{
string decl;

View File

@@ -21,4 +21,6 @@ std::pair<SgSymbol*, SgSymbol*> copyArray(const std::pair<std::string, int>& pla
const std::string& suffix,
std::string& filename,
std::map<std::string, std::map<int, std::set<std::string>>>& newDeclsToInclude,
std::map<std::string, std::map<int, std::set<std::string>>>& copied);
std::map<std::string, std::map<int, std::set<std::string>>>& copied);
const std::vector<const Variable*> getArraySynonyms(DIST::Array* array);

View File

@@ -0,0 +1,98 @@
#include "leak_detector.h"
#include <set>
#include <map>
#include <string>
#include "SgUtils.h"
#include "uniq_name_creator.h"
#include "resolve_par_reg_conflicts.h"
using namespace::std;
static const string COMMON_ARRAY_SUFFIX = "_c";
static const string COMMON_BLOCK_SUFFIX = "_r";
void UniqueNameCreator::GetSymbolsRec(SgExpression* exp, set<string>& add_to)
{
if (!exp)
return;
if (isArrayRef(exp) && exp->symbol() && exp->symbol()->identifier())
add_to.emplace(exp->symbol()->identifier());
GetSymbolsRec(exp->lhs(), add_to);
GetSymbolsRec(exp->rhs(), add_to);
}
void UniqueNameCreator::FillDeclarations()
{
allDeclarations.clear();
auto* file_before = current_file;
for (const auto& by_file : funcInfo)
{
if (SgFile::switchToFile(by_file.first) != -1)
{
for (const auto* by_func : by_file.second)
{
SgStatement* st = by_func->funcPointer;
if (st)
st = st->lexNext();
while (st)
{
for (int i = 0; i < 3; i++)
GetSymbolsRec(st->expr(i), allDeclarations);
st = st->lexNext();
}
}
}
else
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
SgFile::switchToFile(file_before->filename());
declarationsAnalyzed = true;
}
void UniqueNameCreator::GetUniqueName(DIST::Array* array, string& array_name, string& common_block_name)
{
auto varsOnPos = getArraySynonyms(array);
if (!varsOnPos.size())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
auto array_plain_name = varsOnPos[0]->getName();
auto it = generatedNames.find(array);
if (it != generatedNames.end())
{
array_name.assign(it->second.first);
common_block_name.assign(it->second.second);
return;
}
if (!declarationsAnalyzed)
FillDeclarations();
int v = 1;
auto created_array_name = array_plain_name + COMMON_ARRAY_SUFFIX;
auto created_common_name = array_plain_name + COMMON_BLOCK_SUFFIX;
while (allDeclarations.find(created_array_name) != allDeclarations.end() ||
allDeclarations.find(created_common_name) != allDeclarations.end())
{
created_array_name = array_plain_name + "_v" + std::to_string(v) + COMMON_ARRAY_SUFFIX;
created_common_name = array_plain_name + "_v" + std::to_string(v) + COMMON_BLOCK_SUFFIX;
v++;
}
allDeclarations.insert(created_array_name);
allDeclarations.insert(created_common_name);
generatedNames.emplace(array, std::make_pair(created_array_name, created_common_name));
array_name.assign(created_array_name);
common_block_name.assign(created_common_name);
}

View File

@@ -0,0 +1,26 @@
#pragma once
#include <map>
#include <set>
#include <string>
#include "GraphCall/graph_calls.h"
class UniqueNameCreator
{
std::map<std::string, std::vector<FuncInfo*>> funcInfo;
std::set<std::string> allDeclarations;
bool declarationsAnalyzed = false;
std::map<const DIST::Array*, std::pair<std::string, std::string>> generatedNames;
static void GetSymbolsRec(SgExpression* exp, std::set<std::string>& add_to);
void FillDeclarations();
public:
UniqueNameCreator(const std::map<std::string, std::vector<FuncInfo*>>& allFuncInfo)
{
declarationsAnalyzed = false;
funcInfo = allFuncInfo;
}
void GetUniqueName(DIST::Array* array, std::string& array_name, std::string& common_block_name);
};

View File

@@ -1,3 +1,3 @@
#pragma once
#define VERSION_SPF "2479"
#define VERSION_SPF "2480"