-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Analysis] Add OpCount Analysis (#7644)
- Loading branch information
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//===- OpCountAnalysis.h - operation count analyses -----------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This header file defines prototypes for methods that perform analysis | ||
// involving the frequency of different kinds of operations found in a | ||
// builtin.module. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CIRCT_ANALYSIS_OPCOUNT_ANALYSIS_H | ||
#define CIRCT_ANALYSIS_OPCOUNT_ANALYSIS_H | ||
|
||
#include "circt/Support/LLVM.h" | ||
#include "mlir/IR/Operation.h" | ||
#include "llvm/ADT/DenseMap.h" | ||
|
||
namespace mlir { | ||
class AnalysisManager; | ||
} // namespace mlir | ||
namespace circt { | ||
namespace analysis { | ||
|
||
class OpCountAnalysis { | ||
public: | ||
OpCountAnalysis(Operation *moduleOp, mlir::AnalysisManager &am); | ||
|
||
/// Get the frequency of operations of a specific name | ||
size_t getOpCount(OperationName opName); | ||
|
||
/// Get the names of all distinct operations found by the analysis | ||
SmallVector<OperationName> getFoundOpNames(); | ||
|
||
/// Get a map from number of operands to corresponding frequency for the given | ||
/// operation | ||
DenseMap<size_t, size_t> getOperandCountMap(OperationName opName); | ||
|
||
private: | ||
DenseMap<OperationName, size_t> opCounts; | ||
DenseMap<OperationName, DenseMap<size_t, size_t>> operandCounts; | ||
}; | ||
|
||
} // namespace analysis | ||
} // namespace circt | ||
|
||
#endif // CIRCT_ANALYSIS_OPCOUNT_ANALYSIS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//===- OpCountAnalysis.cpp - operation count analyses -----------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines the op count analysis. This is an analysis that | ||
// provides information about the frequency of different kinds of operations | ||
// found in a builtin.module. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "circt/Analysis/OpCountAnalysis.h" | ||
#include "mlir/IR/Operation.h" | ||
|
||
using namespace circt; | ||
using namespace analysis; | ||
|
||
OpCountAnalysis::OpCountAnalysis(Operation *moduleOp, | ||
mlir::AnalysisManager &am) { | ||
moduleOp->walk([&](Operation *op) { | ||
auto opName = op->getName(); | ||
// Update opCounts | ||
opCounts[opName]++; | ||
|
||
// Update operandCounts | ||
operandCounts[opName][op->getNumOperands()]++; | ||
}); | ||
} | ||
|
||
SmallVector<OperationName> OpCountAnalysis::getFoundOpNames() { | ||
SmallVector<OperationName> opNames; | ||
for (auto pair : opCounts) | ||
opNames.push_back(pair.first); | ||
return opNames; | ||
} | ||
|
||
size_t OpCountAnalysis::getOpCount(OperationName opName) { | ||
return opCounts[opName]; | ||
} | ||
|
||
DenseMap<size_t, size_t> | ||
OpCountAnalysis::getOperandCountMap(OperationName opName) { | ||
return operandCounts[opName]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// RUN: circt-opt -test-op-count-analysis %s 2>&1 | FileCheck %s | ||
|
||
// CHECK: builtin.module: 1 | ||
// CHECK: with 0 operands: 1 | ||
// CHECK: comb.add: 4 | ||
// CHECK: with 1 operands: 1 | ||
// CHECK: with 2 operands: 1 | ||
// CHECK: with 3 operands: 2 | ||
// CHECK: comb.icmp: 1 | ||
// CHECK: with 2 operands: 1 | ||
// CHECK: comb.xor: 1 | ||
// CHECK: with 1 operands: 1 | ||
// CHECK: hw.module: 1 | ||
// CHECK: with 0 operands: 1 | ||
// CHECK: hw.output: 1 | ||
// CHECK: with 0 operands: 1 | ||
// CHECK: scf.if: 1 | ||
// CHECK: with 1 operands: 1 | ||
// CHECK: scf.yield: 2 | ||
// CHECK: with 1 operands: 2 | ||
|
||
module { | ||
hw.module @bar(in %in1: i8, in %in2: i8, in %in3: i8) { | ||
%add2 = comb.add %in1, %in2 : i8 | ||
%add3 = comb.add %in1, %in2, %in3 : i8 | ||
%add3again = comb.add %in1, %in3, %in3 : i8 | ||
%gt = comb.icmp ult %in1, %in2 : i8 | ||
%x = scf.if %gt -> (i8) { | ||
%add1 = comb.add %in1 : i8 | ||
scf.yield %add1 : i8 | ||
} else { | ||
%xor = comb.xor %add2 : i8 | ||
scf.yield %xor : i8 | ||
} | ||
} | ||
} |