Skip to content

Commit cc8bc79

Browse files
committed
added the possibility to export general statistics objects as json. added commandline argument to store the exported json in a file
1 parent 93bd166 commit cc8bc79

5 files changed

Lines changed: 32 additions & 0 deletions

File tree

include/phasar/DB/ProjectIRDB.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Passes/PassBuilder.h"
2323

2424
#include "phasar/Utils/EnumFlags.h"
25+
#include "nlohmann/json.hpp"
2526

2627
namespace llvm {
2728
class Value;
@@ -61,6 +62,7 @@ class ProjectIRDB {
6162
// Maps an id to its corresponding instruction
6263
std::map<std::size_t, llvm::Instruction *> IDInstructionMapping;
6364
size_t NumberCallsites;
65+
nlohmann::json StatsJson;
6466

6567
void buildIDModuleMapping(llvm::Module *M);
6668

@@ -182,6 +184,8 @@ class ProjectIRDB {
182184

183185
[[nodiscard]] static std::size_t getInstructionID(const llvm::Instruction *I);
184186

187+
void printAsJson(llvm::raw_ostream &OS = llvm::outs()) const;
188+
185189
void print() const;
186190

187191
void emitPreprocessedIR(llvm::raw_ostream &OS = llvm::outs(),

include/phasar/PhasarLLVM/Passes/GeneralStatisticsAnalysis.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <set>
2121

2222
#include "llvm/IR/PassManager.h"
23+
#include "nlohmann/json.hpp"
2324

2425
namespace llvm {
2526
class Type;
@@ -115,6 +116,8 @@ class GeneralStatistics {
115116
*/
116117
[[nodiscard]] std::set<const llvm::Instruction *>
117118
getRetResInstructions() const;
119+
[[nodiscard]] nlohmann::json getAsJson() const;
120+
118121
};
119122

120123
/**

lib/Controller/AnalysisController.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ void AnalysisController::emitRequestedHelperAnalysisResults() {
272272
ICF.printAsJson();
273273
}
274274
}
275+
276+
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitStatisticAsJson) {
277+
if (!ResultDirectory.empty()) {
278+
if (auto OFS = openFileStream("/psr-IrStatistic.json")) {
279+
IRDB.printAsJson(*OFS);
280+
}
281+
} else {
282+
IRDB.printAsJson();
283+
}
284+
}
275285
}
276286

277287
std::unique_ptr<llvm::raw_fd_ostream>

lib/DB/ProjectIRDB.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ void ProjectIRDB::preprocessModule(llvm::Module *M) {
126126
MPM.run(*M, MAM);
127127
// retrieve data from the GeneralStatisticsAnalysis registered earlier
128128
auto GSPResult = MAM.getResult<GeneralStatisticsAnalysis>(*M);
129+
StatsJson = GSPResult.getAsJson();
129130
NumberCallsites = GSPResult.getFunctioncalls();
130131
auto Allocas = GSPResult.getAllocaInstructions();
131132
AllocaInstructions.insert(Allocas.begin(), Allocas.end());
@@ -267,6 +268,10 @@ void ProjectIRDB::print() const {
267268
}
268269
}
269270

271+
void ProjectIRDB::printAsJson(llvm::raw_ostream &OS) const {
272+
OS << StatsJson.dump();
273+
}
274+
270275
void ProjectIRDB::emitPreprocessedIR(llvm::raw_ostream &OS,
271276
bool ShortenIR) const {
272277
for (const auto &[File, Module] : Modules) {

lib/PhasarLLVM/Passes/GeneralStatisticsAnalysis.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,14 @@ GeneralStatistics::getRetResInstructions() const {
216216
return RetResInstructions;
217217
}
218218

219+
nlohmann::json GeneralStatistics::getAsJson() const {
220+
nlohmann::json J;
221+
J["Instructions"] = getInstructions();
222+
J["Functions"] = Functions;
223+
J["Alloca Instructions"] = AllocaInstructions.size();
224+
J["Call Sites"] = CallSites;
225+
J["Global Variables"] = Globals;
226+
return J;
227+
}
228+
219229
} // namespace psr

0 commit comments

Comments
 (0)