added json for call graph

This commit is contained in:
ALEXks
2025-04-13 17:24:49 +03:00
committed by Egor Mayorov
parent 8e90d2517d
commit 50c80f7ea1
6 changed files with 77 additions and 66 deletions

View File

@@ -18,6 +18,7 @@
#include "../GraphLoop/graph_loops_func.h" #include "../GraphLoop/graph_loops_func.h"
#include "../DirectiveProcessing/directive_parser.h" #include "../DirectiveProcessing/directive_parser.h"
#include "../Utils/SgUtils.h" #include "../Utils/SgUtils.h"
#include "../Utils/json.hpp"
#include "../ParallelizationRegions/ParRegions_func.h" #include "../ParallelizationRegions/ParRegions_func.h"
#include "../DynamicAnalysis/gCov_parser_func.h" #include "../DynamicAnalysis/gCov_parser_func.h"
#include "../ExpressionTransform/expr_transform.h" #include "../ExpressionTransform/expr_transform.h"
@@ -37,6 +38,8 @@ using std::cout;
using std::endl; using std::endl;
using std::stack; using std::stack;
using json = nlohmann::json;
#define DEBUG 0 #define DEBUG 0
//TODO: improve parameter checking //TODO: improve parameter checking
@@ -2592,4 +2595,71 @@ void setInlineAttributeToCalls(const map<string, FuncInfo*>& allFunctions,
} }
} }
static json convertToJson(const FuncInfo* currFunc) {
json func;
if (currFunc)
{
func["funcName"] = currFunc->funcName;
func["line"] = currFunc->linesNum.first;
func["lineEnd"] = currFunc->linesNum.second;
func["isMain"] = (int)currFunc->isMain;
func["needToInline"] = (int)currFunc->needToInline;
func["doNotInline"] = (int)currFunc->doNotInline;
func["doNotAnalyze"] = (int)currFunc->doNotAnalyze;
json func_pars = json::array();
for (int z = 0; z < currFunc->funcParams.countOfPars; ++z)
{
json par;
par["inoutType"] = currFunc->funcParams.inout_types[z];
par["identificator"] = currFunc->funcParams.identificators[z];
par["parameterT"] = string(paramNames[currFunc->funcParams.parametersT[z]]);
func_pars.push_back(par);
}
func["params"] = func_pars;
json calls_from = json::array();
for (const auto& call_from : currFunc->callsFromDetailed)
{
json call;
call["line"] = call_from.detailCallsFrom.second;
call["funcName"] = call_from.detailCallsFrom.first;
calls_from.push_back(call);
}
func["callsFrom"] = calls_from;
}
return func;
}
json convertToJson(const map<string, vector<FuncInfo*>>& funcsByFileMap) {
json loopsByFile = json::array();
for (auto& byFile : funcsByFileMap)
{
json func;
const string& file = byFile.first;
json func_array = json::array();
for (auto& func : byFile.second)
{
auto conv = convertToJson(func);
if (!conv.empty())
func_array.push_back(conv);
}
func["file"] = file;
func["functions"] = func_array;
loopsByFile.push_back(func);
}
json allFuncs;
allFuncs["allFunctions"] = loopsByFile;
return allFuncs;
}
#undef DEBUG #undef DEBUG

View File

@@ -378,33 +378,6 @@ FuncInfo* isUserFunctionInProject(const string &func)
return ret; return ret;
} }
string convertToString(const FuncInfo *currFunc)
{
string result = "";
if (currFunc)
{
result += "|" + currFunc->funcName + "|" + to_string(currFunc->linesNum.first) +
"#" + to_string(currFunc->linesNum.second) +
"#" + to_string(currFunc->callsFromDetailed.size()) +
"#" + to_string(currFunc->needToInline) + "#" + to_string(currFunc->doNotInline) +
"#" + to_string(currFunc->doNotAnalyze) + "#" + to_string((int)currFunc->isMain);
result += "#" + to_string(currFunc->funcParams.countOfPars);
if (currFunc->funcParams.countOfPars)
{
for (int z = 0; z < currFunc->funcParams.countOfPars; ++z)
{
result += "#" + currFunc->funcParams.identificators[z] + "#" + to_string(currFunc->funcParams.inout_types[z]);
result += "#" + string(paramNames[currFunc->funcParams.parametersT[z]]);
}
}
for (int i = 0; i < currFunc->callsFromDetailed.size(); ++i)
result += "|" + currFunc->callsFromDetailed[i].detailCallsFrom.first + "|" + to_string(currFunc->callsFromDetailed[i].detailCallsFrom.second);
}
return result;
}
// Find dead functions and fill callTo / callFrom information // Find dead functions and fill callTo / callFrom information
void findDeadFunctionsAndFillCalls(map<string, vector<FuncInfo*>> &allFuncInfo, map<string, vector<Messages>> &allMessages, bool noPrint) void findDeadFunctionsAndFillCalls(map<string, vector<FuncInfo*>> &allFuncInfo, map<string, vector<Messages>> &allMessages, bool noPrint)
{ {

View File

@@ -7,6 +7,7 @@
#include "graph_calls.h" #include "graph_calls.h"
#include "../GraphLoop/graph_loops.h" #include "../GraphLoop/graph_loops.h"
#include "../ParallelizationRegions/ParRegions.h" #include "../ParallelizationRegions/ParRegions.h"
#include "Utils/json.hpp"
namespace Distribution namespace Distribution
{ {
@@ -23,7 +24,6 @@ int CreateCallGraphViz(const char *fileName, const std::map<std::string, std::ve
int CreateFuncInfo(const char *fileName, const std::map<std::string, std::vector<FuncInfo*>> &funcByFile); int CreateFuncInfo(const char *fileName, const std::map<std::string, std::vector<FuncInfo*>> &funcByFile);
std::string removeString(const std::string &toRemove, const std::string &inStr); std::string removeString(const std::string &toRemove, const std::string &inStr);
FuncInfo* isUserFunctionInProject(const std::string &func); FuncInfo* isUserFunctionInProject(const std::string &func);
std::string convertToString(const FuncInfo *currFunc);
void findDeadFunctionsAndFillCalls(std::map<std::string, std::vector<FuncInfo*>> &allFuncInfo, std::map<std::string, std::vector<Messages>> &allMessages, bool noPrint = false); void findDeadFunctionsAndFillCalls(std::map<std::string, std::vector<FuncInfo*>> &allFuncInfo, std::map<std::string, std::vector<Messages>> &allMessages, bool noPrint = false);
void createLinksBetweenFormalAndActualParams(std::map<std::string, std::vector<FuncInfo*>> &allFuncInfo, std::map<DIST::Array*, std::set<DIST::Array*>> &arrayLinksByFuncCalls, void createLinksBetweenFormalAndActualParams(std::map<std::string, std::vector<FuncInfo*>> &allFuncInfo, std::map<DIST::Array*, std::set<DIST::Array*>> &arrayLinksByFuncCalls,
const std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>> &declaredArrays, const std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>> &declaredArrays,
@@ -53,5 +53,6 @@ void findContainsFunctions(SgStatement *st, std::vector<SgStatement*> &found, co
void correctNameIfContains(SgStatement* call, SgExpression* exCall, std::string& name, const std::vector<SgStatement*>& containsFunctions, const std::string& prefix); void correctNameIfContains(SgStatement* call, SgExpression* exCall, std::string& name, const std::vector<SgStatement*>& containsFunctions, const std::string& prefix);
int countPerfectLoopNest(SgStatement* st); int countPerfectLoopNest(SgStatement* st);
void setInlineAttributeToCalls(const std::map<std::string, FuncInfo*>& allFunctions, const std::map<std::string, std::set<std::pair<std::string, int>>>& inDataChains, const std::map<std::string, std::vector<SgStatement*>>& hiddenData); void setInlineAttributeToCalls(const std::map<std::string, FuncInfo*>& allFunctions, const std::map<std::string, std::set<std::pair<std::string, int>>>& inDataChains, const std::map<std::string, std::vector<SgStatement*>>& hiddenData);
nlohmann::json convertToJson(const std::map<std::string, std::vector<FuncInfo*>>& funcsByFile);
#endif #endif

View File

@@ -940,28 +940,13 @@ static int getLoopState(const LoopGraph* currLoop)
return loopState; return loopState;
} }
static void printToBuffer(const LoopGraph *currLoop, const int childSize, char buf[512])
{
sprintf(buf, "#%d#%d#%d#%d#%d#%d#%d#%d",
currLoop->lineNum, currLoop->lineNumAfterLoop, currLoop->perfectLoop, currLoop->hasGoto, currLoop->hasPrints, childSize, getLoopState(currLoop),
currLoop->hasNonRectangularBounds);
}
static int calculateNormalChildSize(const LoopGraph *currLoop)
{
int count = 0;
for (auto &elem : currLoop->children)
count += (elem->lineNum > 0) ? 1 : 0;
return count;
}
static json convertToJson(const LoopGraph* currLoop) static json convertToJson(const LoopGraph* currLoop)
{ {
json loop; json loop;
const auto& file = currLoop->fileName;
if (currLoop && currLoop->lineNum > 0) if (currLoop && currLoop->lineNum > 0)
{ {
loop["file"] = file; loop["file"] = currLoop->fileName;
loop["line"] = currLoop->lineNum; loop["line"] = currLoop->lineNum;
loop["lineNumAfterLoop"] = currLoop->lineNumAfterLoop; loop["lineNumAfterLoop"] = currLoop->lineNumAfterLoop;
loop["perfectLoop"] = currLoop->perfectLoop; loop["perfectLoop"] = currLoop->perfectLoop;
@@ -974,8 +959,6 @@ static json convertToJson(const LoopGraph* currLoop)
json call; json call;
call["line"] = line; call["line"] = line;
call["funcName"] = func; call["funcName"] = func;
call["canBeInlined"] = 0;
call["parentLineOffset"] = 0;
calls.push_back(call); calls.push_back(call);
} }

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define VERSION_SPF "2404" #define VERSION_SPF "2405"

View File

@@ -552,15 +552,7 @@ int SPF_GetGraphFunctions(void*& context, int winHandler, short *options, short
{ {
runPassesForVisualizer(projName, { FILL_PAR_REGIONS_LINES } ); runPassesForVisualizer(projName, { FILL_PAR_REGIONS_LINES } );
string resVal = ""; string resVal = convertToJson(allFuncInfo).dump();
resVal = to_string(allFuncInfo.size());
for (auto f = allFuncInfo.begin(); f != allFuncInfo.end(); ++f)
{
resVal += "|" + f->first + "|" + to_string(f->second.size());
for (int i = 0; i < f->second.size(); ++i)
resVal += convertToString(f->second[i]);
}
copyStringToShort(result, resVal); copyStringToShort(result, resVal);
retSize = (int)resVal.size() + 1; retSize = (int)resVal.size() + 1;
} }
@@ -1429,15 +1421,7 @@ int SPF_SetFunctionsToInclude(void*& context, int winHandler, short *options, sh
{ {
runPassesForVisualizer(projName, { FIND_FUNC_TO_INCLUDE }); runPassesForVisualizer(projName, { FIND_FUNC_TO_INCLUDE });
string resVal = ""; string resVal = convertToJson(allFuncInfo).dump();
resVal = to_string(allFuncInfo.size());
for (auto f = allFuncInfo.begin(); f != allFuncInfo.end(); ++f)
{
resVal += "|" + f->first + "|" + to_string(f->second.size());
for (int i = 0; i < f->second.size(); ++i)
resVal += convertToString(f->second[i]);
}
copyStringToShort(result, resVal); copyStringToShort(result, resVal);
retSize = (int)resVal.size() + 1; retSize = (int)resVal.size() + 1;
} }