15 #include "mlir/Pass/Pass.h"
16 #include "llvm/Support/JSON.h"
19 #define GEN_PASS_DEF_PRINTOPCOUNT
20 #include "circt/Transforms/Passes.h.inc"
24 using namespace circt;
29 raw_ostream &os,
bool sorted =
false) {
33 llvm::sort(opNames, [](OperationName name1, OperationName name2) {
34 return name1.getStringRef() < name2.getStringRef();
36 for (
auto opName : opNames) {
37 os <<
"- name: " << opName <<
"\n";
38 os <<
" count: " << opCount.
getOpCount(opName) <<
"\n";
40 if (operandMap.size() <= 1)
42 llvm::SmallVector<size_t> keys;
43 for (
auto pair : operandMap)
44 keys.push_back(pair.first);
48 for (
auto num : keys) {
49 os <<
" - operands: " << num <<
"\n";
50 os <<
" count: " << operandMap[num] <<
"\n";
57 auto jos = llvm::json::OStream(os);
60 jos.attributeObject(opName.getStringRef(), [&] {
61 for (auto pair : opCount.getOperandCountMap(opName))
62 jos.attribute(std::to_string(pair.first), pair.second);
67 struct PrintOpCountPass
68 :
public circt::impl::PrintOpCountBase<PrintOpCountPass> {
70 PrintOpCountPass(raw_ostream &os) : os(os) {}
72 void runOnOperation()
override {
73 auto &opCount = getAnalysis<circt::analysis::OpCountAnalysis>();
74 switch (emissionFormat) {
75 case OpCountEmissionFormat::Readable:
76 printOpAndOperandCounts(opCount, os);
78 case OpCountEmissionFormat::ReadableSorted:
79 printOpAndOperandCounts(opCount, os,
true);
81 case OpCountEmissionFormat::JSON:
82 printOpAndOperandJSON(opCount, os);
95 return std::make_unique<PrintOpCountPass>(os);
100 return std::make_unique<PrintOpCountPass>(llvm::outs());
size_t getOpCount(OperationName opName)
Get the frequency of operations of a specific name.
DenseMap< size_t, size_t > getOperandCountMap(OperationName opName)
Get a map from number of operands to corresponding frequency for the given operation.
SmallVector< OperationName > getFoundOpNames()
Get the names of all distinct operations found by the analysis.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
std::unique_ptr< mlir::Pass > createPrintOpCountPass(llvm::raw_ostream &os)