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/LogicalResult.h"
31#include "llvm/Support/raw_ostream.h"
35#define DEBUG_TYPE "synth-lower-variadic"
39#define GEN_PASS_DEF_LOWERVARIADIC
40#include "circt/Dialect/Synth/Transforms/SynthPasses.h.inc"
53struct LowerVariadicPass :
public impl::LowerVariadicBase<LowerVariadicPass> {
54 using LowerVariadicBase::LowerVariadicBase;
55 void runOnOperation()
override;
65 IncrementalLongestPathAnalysis *analysis, mlir::IRRewriter &rewriter,
66 Operation *op, llvm::function_ref<
bool(OpOperand &)> isInverted,
67 llvm::function_ref<Value(ValueWithArrivalTime, ValueWithArrivalTime)>
70 SmallVector<ValueWithArrivalTime> operands;
71 size_t valueNumber = 0;
73 for (
size_t i = 0, e = op->getNumOperands(); i < e; ++i) {
78 auto result = analysis->getMaxDelay(op->getOperand(i));
83 operands.push_back(ValueWithArrivalTime(op->getOperand(i), delay,
84 isInverted(op->getOpOperand(i)),
89 auto result = buildBalancedTreeWithArrivalTimes<ValueWithArrivalTime>(
92 [&](
const ValueWithArrivalTime &lhs,
const ValueWithArrivalTime &rhs) {
93 Value combined = createBinaryOp(lhs, rhs);
96 auto delayResult = analysis->getMaxDelay(combined);
97 if (succeeded(delayResult))
98 newDelay = *delayResult;
100 return ValueWithArrivalTime(combined, newDelay,
false, valueNumber++);
103 rewriter.replaceOp(op, result.getValue());
107using OperandKey = llvm::SmallVector<std::pair<mlir::Value, bool>>;
123 llvm::hash_code hash = 0;
125 for (
const auto &pair : val) {
126 hash = llvm::hash_combine(
130 return static_cast<unsigned>(hash);
144 const std::pair<mlir::Value, bool> &rhs)
const {
145 if (lhs.first != rhs.first) {
146 auto lhsArg = llvm::dyn_cast<mlir::BlockArgument>(lhs.first);
147 auto rhsArg = llvm::dyn_cast<mlir::BlockArgument>(rhs.first);
148 if (lhsArg && rhsArg)
149 return lhsArg.getArgNumber() < rhsArg.getArgNumber();
155 auto *lhsOp = lhs.first.getDefiningOp();
156 auto *rhsOp = rhs.first.getDefiningOp();
157 return lhsOp->isBeforeInBlock(rhsOp);
159 return lhs.second < rhs.second;
165 for (
size_t i = 0, e = op.getNumOperands(); i < e; ++i)
166 key.emplace_back(op.getOperand(i), op.isInverted(i));
173 aig::AndInverterOp op, mlir::IRRewriter &rewriter,
174 llvm::DenseMap<OperandKey, mlir::Value> &seenExpressions) {
176 if (op.getNumOperands() <= 2)
180 mlir::SmallVector<Value> newValues;
181 mlir::SmallVector<bool> newInversions;
183 for (
auto it = allOperands.begin(); it != allOperands.end(); ++it) {
187 auto match = seenExpressions.find(remaining);
188 if (match != seenExpressions.end() && match->second != op.getResult()) {
189 newValues.push_back(match->second);
190 newInversions.push_back(
false);
198 newValues.push_back(it->first);
199 newInversions.push_back(it->second);
202 if (newValues.size() < allOperands.size()) {
203 rewriter.modifyOpInPlace(op, [&]() {
204 op.getOperation()->setOperands(newValues);
205 op.setInverted(newInversions);
210void LowerVariadicPass::runOnOperation() {
211 if (getOperation()->getNumRegions() != 1 ||
212 getOperation()->getRegion(0).getBlocks().size() != 1)
216 mlir::Block &bodyBlock = getOperation()->getRegion(0).getBlocks().front();
217 auto *moduleOp = getOperation();
219 if (!mlir::sortTopologically(
220 &bodyBlock, [](Value val, Operation *op) ->
bool {
221 if (isa_and_nonnull<hw::HWDialect>(op->getDialect()))
222 return isa<hw::InstanceOp>(op);
223 return !isa_and_nonnull<comb::CombDialect, synth::SynthDialect>(
226 mlir::emitError(moduleOp->getLoc())
227 <<
"Failed to topologically sort graph region blocks";
228 return signalPassFailure();
233 if (timingAware.getValue()) {
234 if (!dyn_cast<hw::HWModuleOp>(moduleOp)) {
235 moduleOp->emitWarning(
236 "Longest Path Analysis failed: expected 'hw.module', but found '")
237 << moduleOp->getName().getStringRef()
238 <<
"'. Only HWModuleOps are currently supported.";
240 analysis = &getAnalysis<synth::IncrementalLongestPathAnalysis>();
245 SmallVector<OperationName> names;
246 for (
const auto &name : opNames)
247 names.push_back(OperationName(name, &getContext()));
250 auto shouldLower = [&](Operation *op) {
254 return llvm::find(names, op->getName()) != names.end();
257 mlir::IRRewriter rewriter(&getContext());
258 rewriter.setListener(analysis);
262 llvm::DenseMap<OperandKey, mlir::Value> seenExpressions;
264 for (
auto &op : bodyBlock.getOperations()) {
265 if (
auto andInverterOp = llvm::dyn_cast<aig::AndInverterOp>(op)) {
267 seenExpressions[key] = andInverterOp.getResult();
271 for (
auto &op : bodyBlock.getOperations()) {
272 if (
auto andInverterOp = llvm::dyn_cast<aig::AndInverterOp>(op)) {
281 for (
auto &opRef :
llvm::make_early_inc_range(bodyBlock.getOperations())) {
284 if (!shouldLower(op) || op->getNumOperands() <= 2)
287 rewriter.setInsertionPoint(op);
291 mlir::TypeSwitch<Operation *, LogicalResult>(op)
292 .Case<aig::AndInverterOp, XorInverterOp>([&](
auto op) {
294 analysis, rewriter, op,
296 [&](OpOperand &operand) {
297 return op.isInverted(operand.getOperandNumber());
300 [&](ValueWithArrivalTime lhs, ValueWithArrivalTime rhs) {
301 return decltype(op)::create(
302 rewriter, op->getLoc(), lhs.getValue(), rhs.getValue(),
303 lhs.isInverted(), rhs.isInverted());
306 .Default([&](Operation *op) {
309 if (isa_and_nonnull<comb::CombDialect>(op->getDialect()) &&
310 op->hasTrait<OpTrait::IsCommutative>())
312 analysis, rewriter, op,
314 [](OpOperand &) {
return false; },
316 [&](ValueWithArrivalTime lhs, ValueWithArrivalTime rhs) {
317 OperationState state(op->getLoc(), op->getName());
319 ValueRange{lhs.getValue(), rhs.getValue()});
320 state.addTypes(op->getResult(0).getType());
321 auto *newOp = Operation::create(state);
322 rewriter.insert(newOp);
323 return newOp->getResult(0);
328 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)
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)