21#include "mlir/Analysis/TopologicalSortUtils.h"
22#include "mlir/IR/Block.h"
23#include "mlir/IR/OpDefinition.h"
24#include "mlir/IR/PatternMatch.h"
25#include "mlir/IR/Value.h"
26#include "mlir/Support/LLVM.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/Support/Casting.h"
29#include "llvm/Support/Error.h"
30#include "llvm/Support/raw_ostream.h"
34#define DEBUG_TYPE "synth-lower-variadic"
38#define GEN_PASS_DEF_LOWERVARIADIC
39#include "circt/Dialect/Synth/Transforms/SynthPasses.h.inc"
52struct LowerVariadicPass :
public impl::LowerVariadicBase<LowerVariadicPass> {
53 using LowerVariadicBase::LowerVariadicBase;
54 void runOnOperation()
override;
64 IncrementalLongestPathAnalysis *analysis, mlir::IRRewriter &rewriter,
65 Operation *op, llvm::function_ref<
bool(OpOperand &)> isInverted,
66 llvm::function_ref<Value(ValueWithArrivalTime, ValueWithArrivalTime)>
69 SmallVector<ValueWithArrivalTime> operands;
70 size_t valueNumber = 0;
72 for (
size_t i = 0, e = op->getNumOperands(); i < e; ++i) {
77 auto result = analysis->getMaxDelay(op->getOperand(i));
82 operands.push_back(ValueWithArrivalTime(op->getOperand(i), delay,
83 isInverted(op->getOpOperand(i)),
88 auto result = buildBalancedTreeWithArrivalTimes<ValueWithArrivalTime>(
91 [&](
const ValueWithArrivalTime &lhs,
const ValueWithArrivalTime &rhs) {
92 Value combined = createBinaryOp(lhs, rhs);
95 auto delayResult = analysis->getMaxDelay(combined);
96 if (succeeded(delayResult))
97 newDelay = *delayResult;
99 return ValueWithArrivalTime(combined, newDelay,
false, valueNumber++);
102 rewriter.replaceOp(op, result.getValue());
106using OperandKey = llvm::SmallVector<std::pair<mlir::Value, bool>>;
122 llvm::hash_code hash = 0;
124 for (
const auto &pair : val) {
125 hash = llvm::hash_combine(
129 return static_cast<unsigned>(hash);
143 const std::pair<mlir::Value, bool> &rhs)
const {
144 if (lhs.first != rhs.first) {
145 auto lhsArg = llvm::dyn_cast<mlir::BlockArgument>(lhs.first);
146 auto rhsArg = llvm::dyn_cast<mlir::BlockArgument>(rhs.first);
147 if (lhsArg && rhsArg)
148 return lhsArg.getArgNumber() < rhsArg.getArgNumber();
154 auto *lhsOp = lhs.first.getDefiningOp();
155 auto *rhsOp = rhs.first.getDefiningOp();
156 return lhsOp->isBeforeInBlock(rhsOp);
158 return lhs.second < rhs.second;
164 for (
size_t i = 0, e = op.getNumOperands(); i < e; ++i)
165 key.emplace_back(op.getOperand(i), op.isInverted(i));
172 aig::AndInverterOp op, mlir::IRRewriter &rewriter,
173 llvm::DenseMap<OperandKey, mlir::Value> &seenExpressions) {
175 if (op.getNumOperands() <= 2)
179 mlir::SmallVector<Value> newValues;
180 mlir::SmallVector<bool> newInversions;
182 for (
auto it = allOperands.begin(); it != allOperands.end(); ++it) {
186 auto match = seenExpressions.find(remaining);
187 if (match != seenExpressions.end() && match->second != op.getResult()) {
188 newValues.push_back(match->second);
189 newInversions.push_back(
false);
197 newValues.push_back(it->first);
198 newInversions.push_back(it->second);
201 if (newValues.size() < allOperands.size()) {
202 rewriter.modifyOpInPlace(op, [&]() {
203 op.getOperation()->setOperands(newValues);
204 op.setInverted(newInversions);
209void LowerVariadicPass::runOnOperation() {
212 if (!mlir::sortTopologically(
213 getOperation().
getBodyBlock(), [](Value val, Operation *op) ->
bool {
214 if (isa_and_nonnull<hw::HWDialect>(op->getDialect()))
215 return isa<hw::InstanceOp>(op);
216 return !isa_and_nonnull<comb::CombDialect, synth::SynthDialect>(
219 mlir::emitError(getOperation().
getLoc())
220 <<
"Failed to topologically sort graph region blocks";
221 return signalPassFailure();
226 if (timingAware.getValue())
227 analysis = &getAnalysis<synth::IncrementalLongestPathAnalysis>();
229 auto moduleOp = getOperation();
232 SmallVector<OperationName> names;
233 for (
const auto &name : opNames)
234 names.push_back(OperationName(name, &getContext()));
237 auto shouldLower = [&](Operation *op) {
241 return llvm::find(names, op->getName()) != names.end();
244 mlir::IRRewriter rewriter(&getContext());
245 rewriter.setListener(analysis);
249 llvm::DenseMap<OperandKey, mlir::Value> seenExpressions;
251 for (
auto &op : moduleOp.
getBodyBlock()->getOperations()) {
252 if (
auto andInverterOp = llvm::dyn_cast<aig::AndInverterOp>(op)) {
254 seenExpressions[key] = andInverterOp.getResult();
258 for (
auto &op : moduleOp.
getBodyBlock()->getOperations()) {
259 if (
auto andInverterOp = llvm::dyn_cast<aig::AndInverterOp>(op)) {
272 if (!shouldLower(op) || op->getNumOperands() <= 2)
275 rewriter.setInsertionPoint(op);
278 if (
auto andInverterOp = dyn_cast<aig::AndInverterOp>(op)) {
280 analysis, rewriter, op,
282 [&](OpOperand &operand) {
283 return andInverterOp.isInverted(operand.getOperandNumber());
286 [&](ValueWithArrivalTime lhs, ValueWithArrivalTime rhs) {
287 return aig::AndInverterOp::create(
288 rewriter, op->getLoc(), lhs.getValue(), rhs.getValue(),
289 lhs.isInverted(), rhs.isInverted());
292 return signalPassFailure();
298 if (isa_and_nonnull<comb::CombDialect>(op->getDialect()) &&
299 op->hasTrait<OpTrait::IsCommutative>()) {
301 analysis, rewriter, op,
303 [](OpOperand &) {
return false; },
305 [&](ValueWithArrivalTime lhs, ValueWithArrivalTime rhs) {
306 OperationState state(op->getLoc(), op->getName());
307 state.addOperands(ValueRange{lhs.getValue(), rhs.getValue()});
308 state.addTypes(op->getResult(0).getType());
309 auto *newOp = Operation::create(state);
310 rewriter.insert(newOp);
311 return newOp->getResult(0);
314 return signalPassFailure();
static LogicalResult replaceWithBalancedTree(IncrementalLongestPathAnalysis *analysis, mlir::IRRewriter &rewriter, Operation *op, llvm::function_ref< bool(OpOperand &)> isInverted, llvm::function_ref< Value(ValueWithArrivalTime, ValueWithArrivalTime)> createBinaryOp)
Construct a balanced binary tree from a variadic operation using a delay-aware algorithm.
static void simplifyWithExistingOperations(aig::AndInverterOp op, mlir::IRRewriter &rewriter, llvm::DenseMap< OperandKey, mlir::Value > &seenExpressions)
llvm::SmallVector< std::pair< mlir::Value, bool > > OperandKey
static OperandKey getSortedOperandKey(aig::AndInverterOp op)
static Location getLoc(DefSlot slot)
static Block * getBodyBlock(FModuleLike mod)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
bool operator()(const std::pair< mlir::Value, bool > &lhs, const std::pair< mlir::Value, bool > &rhs) const
static OperandKey getTombstoneKey()
static OperandKey getEmptyKey()
static unsigned getHashValue(const OperandKey &val)
static bool isEqual(const OperandKey &lhs, const OperandKey &rhs)