CIRCT 23.0.0git
Loading...
Searching...
No Matches
LowerVariadic.cpp
Go to the documentation of this file.
1//===- LowerVariadic.cpp - Lowering Variadic to Binary Ops ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass lowers variadic operations to binary operations using a
10// delay-aware algorithm for commutative operations.
11//
12//===----------------------------------------------------------------------===//
13
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"
32#include <iterator>
33#include <vector>
34
35#define DEBUG_TYPE "synth-lower-variadic"
36
37namespace circt {
38namespace synth {
39#define GEN_PASS_DEF_LOWERVARIADIC
40#include "circt/Dialect/Synth/Transforms/SynthPasses.h.inc"
41} // namespace synth
42} // namespace circt
43
44using namespace circt;
45using namespace synth;
46
47//===----------------------------------------------------------------------===//
48// Lower Variadic pass
49//===----------------------------------------------------------------------===//
50
51namespace {
52
53struct LowerVariadicPass : public impl::LowerVariadicBase<LowerVariadicPass> {
54 using LowerVariadicBase::LowerVariadicBase;
55 void runOnOperation() override;
56};
57
58} // namespace
59
60/// Construct a balanced binary tree from a variadic operation using a
61/// delay-aware algorithm. This function builds the tree by repeatedly combining
62/// the two values with the earliest arrival times, which minimizes the critical
63/// path delay.
64static LogicalResult replaceWithBalancedTree(
65 IncrementalLongestPathAnalysis *analysis, mlir::IRRewriter &rewriter,
66 Operation *op, llvm::function_ref<bool(OpOperand &)> isInverted,
67 llvm::function_ref<Value(ValueWithArrivalTime, ValueWithArrivalTime)>
68 createBinaryOp) {
69 // Collect all operands with their arrival times and inversion flags
70 SmallVector<ValueWithArrivalTime> operands;
71 size_t valueNumber = 0;
72
73 for (size_t i = 0, e = op->getNumOperands(); i < e; ++i) {
74 int64_t delay = 0;
75 // If analysis is available, use it to compute the delay.
76 // If not available, use zero delay and `valueNumber` will be used instead.
77 if (analysis) {
78 auto result = analysis->getMaxDelay(op->getOperand(i));
79 if (failed(result))
80 return failure();
81 delay = *result;
82 }
83 operands.push_back(ValueWithArrivalTime(op->getOperand(i), delay,
84 isInverted(op->getOpOperand(i)),
85 valueNumber++));
86 }
87
88 // Use shared tree building utility
89 auto result = buildBalancedTreeWithArrivalTimes<ValueWithArrivalTime>(
90 operands,
91 // Combine: create binary operation and compute new arrival time
92 [&](const ValueWithArrivalTime &lhs, const ValueWithArrivalTime &rhs) {
93 Value combined = createBinaryOp(lhs, rhs);
94 int64_t newDelay = 0;
95 if (analysis) {
96 auto delayResult = analysis->getMaxDelay(combined);
97 if (succeeded(delayResult))
98 newDelay = *delayResult;
99 }
100 return ValueWithArrivalTime(combined, newDelay, false, valueNumber++);
101 });
102
103 rewriter.replaceOp(op, result.getValue());
104 return success();
105}
106
107using OperandKey = llvm::SmallVector<std::pair<mlir::Value, bool>>;
108
109namespace llvm {
110template <>
112 static unsigned getHashValue(const OperandKey &val) {
113 llvm::hash_code hash = 0;
114 // Iteratively combine the hash of each pair in the vector
115 for (const auto &pair : val) {
116 hash = llvm::hash_combine(
118 pair.second);
119 }
120 return static_cast<unsigned>(hash);
121 }
122
123 static bool isEqual(const OperandKey &lhs, const OperandKey &rhs) {
124 // std::vector and std::pair already implement operator==,
125 // which does a deep equality check of the elements.
126 return lhs == rhs;
127 }
128};
129} // namespace llvm
130
131// Struct for ordering the andInverterOp operations we have already seen
133 bool operator()(const std::pair<mlir::Value, bool> &lhs,
134 const std::pair<mlir::Value, bool> &rhs) const {
135 if (lhs.first != rhs.first) {
136 auto lhsArg = llvm::dyn_cast<mlir::BlockArgument>(lhs.first);
137 auto rhsArg = llvm::dyn_cast<mlir::BlockArgument>(rhs.first);
138 if (lhsArg && rhsArg)
139 return lhsArg.getArgNumber() < rhsArg.getArgNumber();
140 if (lhsArg)
141 return true;
142 if (rhsArg)
143 return false;
144
145 auto *lhsOp = lhs.first.getDefiningOp();
146 auto *rhsOp = rhs.first.getDefiningOp();
147 return lhsOp->isBeforeInBlock(rhsOp);
148 }
149 return lhs.second < rhs.second;
150 }
151};
152
153static OperandKey getSortedOperandKey(aig::AndInverterOp op) {
154 OperandKey key;
155 for (size_t i = 0, e = op.getNumOperands(); i < e; ++i)
156 key.emplace_back(op.getOperand(i), op.isInverted(i));
157
158 std::sort(key.begin(), key.end(), OperandPairLess());
159 return key;
160}
161
163 aig::AndInverterOp op, mlir::IRRewriter &rewriter,
164 llvm::DenseMap<OperandKey, mlir::Value> &seenExpressions) {
165
166 if (op.getNumOperands() <= 2)
167 return;
168
169 OperandKey allOperands = getSortedOperandKey(op);
170 mlir::SmallVector<Value> newValues;
171 mlir::SmallVector<bool> newInversions;
172
173 for (auto it = allOperands.begin(); it != allOperands.end(); ++it) {
174 // Look at the remaining operands from 'it' to the end
175 OperandKey remaining(it, allOperands.end());
176
177 auto match = seenExpressions.find(remaining);
178 if (match != seenExpressions.end() && match->second != op.getResult()) {
179 newValues.push_back(match->second);
180 newInversions.push_back(false);
181
182 // We found a match that covers everything from 'it' to the end,
183 // so we can stop searching.
184 break;
185 }
186
187 // No match, add it to the new list of values and inversions.
188 newValues.push_back(it->first);
189 newInversions.push_back(it->second);
190 }
191
192 if (newValues.size() < allOperands.size()) {
193 rewriter.modifyOpInPlace(op, [&]() {
194 op.getOperation()->setOperands(newValues);
195 op.setInverted(newInversions);
196 });
197 }
198}
199
200void LowerVariadicPass::runOnOperation() {
201 if (getOperation()->getNumRegions() != 1 ||
202 getOperation()->getRegion(0).getBlocks().size() != 1)
203 return;
204 // Topologically sort operations in graph regions to ensure operands are
205 // defined before uses.
206 mlir::Block &bodyBlock = getOperation()->getRegion(0).getBlocks().front();
207 auto *moduleOp = getOperation();
208
209 if (!mlir::sortTopologically(
210 &bodyBlock, [](Value val, Operation *op) -> bool {
211 if (isa_and_nonnull<hw::HWDialect>(op->getDialect()))
212 return isa<hw::InstanceOp>(op);
213 return !isa_and_nonnull<comb::CombDialect, synth::SynthDialect>(
214 op->getDialect());
215 })) {
216 mlir::emitError(moduleOp->getLoc())
217 << "Failed to topologically sort graph region blocks";
218 return signalPassFailure();
219 }
220
221 // Get longest path analysis if timing-aware lowering is enabled.
222 synth::IncrementalLongestPathAnalysis *analysis = nullptr;
223 if (timingAware.getValue()) {
224 if (!dyn_cast<hw::HWModuleOp>(moduleOp)) {
225 moduleOp->emitWarning(
226 "Longest Path Analysis failed: expected 'hw.module', but found '")
227 << moduleOp->getName().getStringRef()
228 << "'. Only HWModuleOps are currently supported.";
229 } else {
230 analysis = &getAnalysis<synth::IncrementalLongestPathAnalysis>();
231 }
232 }
233
234 // Build set of operation names to lower if specified.
235 SmallVector<OperationName> names;
236 for (const auto &name : opNames)
237 names.push_back(OperationName(name, &getContext()));
238
239 // Return true if the operation should be lowered.
240 auto shouldLower = [&](Operation *op) {
241 // If no names specified, lower all variadic ops.
242 if (names.empty())
243 return true;
244 return llvm::find(names, op->getName()) != names.end();
245 };
246
247 mlir::IRRewriter rewriter(&getContext());
248 rewriter.setListener(analysis);
249
250 // Simplify exising andInverterOps by reusing operations.
251 if (reuseSubsets) {
252 llvm::DenseMap<OperandKey, mlir::Value> seenExpressions;
253 // First collect all the andInverterOp operations in the block.
254 for (auto &op : bodyBlock.getOperations()) {
255 if (auto andInverterOp = llvm::dyn_cast<aig::AndInverterOp>(op)) {
256 OperandKey key = getSortedOperandKey(andInverterOp);
257 seenExpressions[key] = andInverterOp.getResult();
258 }
259 }
260 // Now try to replace operations with subsets.
261 for (auto &op : bodyBlock.getOperations()) {
262 if (auto andInverterOp = llvm::dyn_cast<aig::AndInverterOp>(op)) {
263 simplifyWithExistingOperations(andInverterOp, rewriter,
264 seenExpressions);
265 }
266 }
267 }
268
269 // FIXME: Currently only top-level operations are lowered due to the lack of
270 // topological sorting in across nested regions.
271 for (auto &opRef : llvm::make_early_inc_range(bodyBlock.getOperations())) {
272 auto *op = &opRef;
273 // Skip operations that don't need lowering or are already binary.
274 if (!shouldLower(op) || op->getNumOperands() <= 2)
275 continue;
276
277 rewriter.setInsertionPoint(op);
278
279 // Handle invertible Synth ops specially to preserve inversion flags.
280 auto result =
281 mlir::TypeSwitch<Operation *, LogicalResult>(op)
282 .Case<aig::AndInverterOp, XorInverterOp>([&](auto op) {
284 analysis, rewriter, op,
285 // Check if each operand is inverted.
286 [&](OpOperand &operand) {
287 return op.isInverted(operand.getOperandNumber());
288 },
289 // Create binary AndInverterOp with inversion flags.
290 [&](ValueWithArrivalTime lhs, ValueWithArrivalTime rhs) {
291 return decltype(op)::create(
292 rewriter, op->getLoc(), lhs.getValue(), rhs.getValue(),
293 lhs.isInverted(), rhs.isInverted());
294 });
295 })
296 .Default([&](Operation *op) {
297 // Handle commutative operations (and, or, xor, mul, add, etc.)
298 // using delay-aware lowering to minimize critical path.
299 if (isa_and_nonnull<comb::CombDialect>(op->getDialect()) &&
300 op->hasTrait<OpTrait::IsCommutative>())
302 analysis, rewriter, op,
303 // No inversion flags for standard commutative operations.
304 [](OpOperand &) { return false; },
305 // Create binary operation with the same operation type.
306 [&](ValueWithArrivalTime lhs, ValueWithArrivalTime rhs) {
307 OperationState state(op->getLoc(), op->getName());
308 state.addOperands(
309 ValueRange{lhs.getValue(), rhs.getValue()});
310 state.addTypes(op->getResult(0).getType());
311 auto *newOp = Operation::create(state);
312 rewriter.insert(newOp);
313 return newOp->getResult(0);
314 });
315 return success();
316 });
317 if (failed(result))
318 return signalPassFailure();
319 }
320}
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.
Definition synth.py:1
bool operator()(const std::pair< mlir::Value, bool > &lhs, const std::pair< mlir::Value, bool > &rhs) const
static unsigned getHashValue(const OperandKey &val)
static bool isEqual(const OperandKey &lhs, const OperandKey &rhs)