Dump DFG patterns with --stats (#4889)

With --stats, we will print DFG pattern combinations, one per line, as
S-expressions to new stat files, together with their frequency, to aid
discovery of new peephole patterns.
This commit is contained in:
Geza Lore
2024-02-11 15:41:10 +00:00
committed by GitHub
parent d667b73e8d
commit cbc76a7816
10 changed files with 412 additions and 2 deletions
+28
View File
@@ -25,6 +25,8 @@
#include "V3AstUserAllocator.h"
#include "V3Dfg.h"
#include "V3DfgPasses.h"
#include "V3DfgPatternStats.h"
#include "V3File.h"
#include "V3Graph.h"
#include "V3UniqueNames.h"
@@ -252,6 +254,8 @@ void V3DfgOptimizer::optimize(AstNetlist* netlistp, const string& label) {
V3DfgOptimizationContext ctx{label};
V3DfgPatternStats patternStats;
// Run the optimization phase
for (AstNode* nodep = netlistp->modulesp(); nodep; nodep = nodep->nextp()) {
// Only optimize proper modules
@@ -295,10 +299,34 @@ void V3DfgOptimizer::optimize(AstNetlist* netlistp, const string& label) {
dfg->addGraph(*component);
}
// Accumulate patterns from the optimized graph for reporting
if (v3Global.opt.stats()) patternStats.accumulate(*dfg);
// Convert back to Ast
if (dumpDfgLevel() >= 8) dfg->dumpDotFilePrefixed(ctx.prefix() + "whole-optimized");
AstModule* const resultModp = V3DfgPasses::dfgToAst(*dfg, ctx);
UASSERT_OBJ(resultModp == modp, modp, "Should be the same module");
}
// Print the collected patterns
if (v3Global.opt.stats()) {
// Label to lowercase, without spaces
std::string ident = label;
std::transform(ident.begin(), ident.end(), ident.begin(), [](unsigned char c) { //
return c == ' ' ? '_' : std::tolower(c);
});
// File to dump to
const std::string filename = v3Global.opt.hierTopDataDir() + "/" + v3Global.opt.prefix()
+ "__stats_dfg_patterns__" + ident + ".txt";
// Open, write, close
std::ofstream* const ofp = V3File::new_ofstream(filename);
if (ofp->fail()) v3fatal("Can't write " << filename);
patternStats.dump(label, *ofp);
ofp->close();
VL_DO_DANGLING(delete ofp, ofp);
}
V3Global::dumpCheckGlobalTree("dfg-optimize", 0, dumpTreeEitherLevel() >= 3);
}