17 #include "mlir/Dialect/SCF/IR/SCF.h"
18 #include "mlir/Support/LLVM.h"
19 #include "llvm/ADT/TypeSwitch.h"
22 using namespace circt;
33 for (
auto operand : op->getOperands())
34 if (!operand.template getDefiningOp<ConstantOp>())
45 class CombRuntimeCostEstimateDialectInterface
47 using RuntimeCostEstimateDialectInterface::
48 RuntimeCostEstimateDialectInterface;
51 assert(isa<CombDialect>(op->getDialect()));
53 return TypeSwitch<Operation *, uint32_t>(op)
57 .Case<
ExtractOp>([](
auto op) {
return (op.getLowBit() > 0) * 4 + 4; })
59 .Case<ReplicateOp>([](
auto op) {
return 20; })
60 .Case<MuxOp, ShlOp, ShrUOp, ShrSOp, SubOp, ICmpOp>(
61 [](
auto op) {
return 10; })
64 .Case<ParityOp>([](
auto op) {
return 20; })
66 .Case<DivUOp, DivSOp, ModUOp, ModSOp>([](
auto op) {
return 100; })
67 .Case<MulOp>([](
auto op) {
return (op->getNumOperands() - 1) * 30; })
68 .Case<AddOp, AndOp, OrOp, XorOp>(
69 [](
auto op) {
return (op->getNumOperands() - 1) * 10; })
75 class HWRuntimeCostEstimateDialectInterface
77 using RuntimeCostEstimateDialectInterface::
78 RuntimeCostEstimateDialectInterface;
81 assert(circt::isa<HWDialect>(op->getDialect()));
83 return llvm::TypeSwitch<mlir::Operation *, uint32_t>(op)
85 [](
auto op) {
return 0; })
86 .Case<ArrayGetOp, StructExtractOp, StructInjectOp, UnionExtractOp>(
87 [](
auto op) {
return 10; })
88 .Case<ArrayCreateOp, StructCreateOp, StructExplodeOp, UnionCreateOp>(
94 class SCFRuntimeCostEstimateDialectInterface
96 using RuntimeCostEstimateDialectInterface::
97 RuntimeCostEstimateDialectInterface;
100 assert(isa<scf::SCFDialect>(op->getDialect()));
102 return llvm::TypeSwitch<mlir::Operation *, uint32_t>(op)
103 .Case<scf::YieldOp>([](
auto op) {
return 0; })
105 .Case<scf::IfOp>([](
auto op) {
return 20; });
116 mlir::DialectRegistry ®istry) {
117 registry.addExtension(+[](MLIRContext *ctx, comb::CombDialect *dialect) {
118 dialect->addInterfaces<CombRuntimeCostEstimateDialectInterface>();
123 mlir::DialectRegistry ®istry) {
124 registry.addExtension(+[](MLIRContext *ctx, hw::HWDialect *dialect) {
125 dialect->addInterfaces<HWRuntimeCostEstimateDialectInterface>();
130 mlir::DialectRegistry ®istry) {
131 registry.addExtension(+[](MLIRContext *ctx, scf::SCFDialect *dialect) {
132 dialect->addInterfaces<SCFRuntimeCostEstimateDialectInterface>();
assert(baseType &&"element must be base type")
static uint32_t getCostEstimate(const SmallPtrSetImpl< Operation * > &ops)
Simple helper to invoke the runtime cost interface for every operation in a set and sum up the costs.
static uint32_t sumNonConstantOperands(Operation *op, uint32_t perOperand)
A dialect interface to get runtime cost estimates of MLIR operations.
void registerCombRuntimeCostEstimateInterface(mlir::DialectRegistry ®istry)
void registerSCFRuntimeCostEstimateInterface(mlir::DialectRegistry ®istry)
void registerHWRuntimeCostEstimateInterface(mlir::DialectRegistry ®istry)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.