-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathLowerVariadic.cpp
More file actions
265 lines (233 loc) · 9.53 KB
/
LowerVariadic.cpp
File metadata and controls
265 lines (233 loc) · 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//===- LowerVariadic.cpp - Lowering Variadic to Binary Ops ------*- 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 pass lowers variadic operations to binary operations using a
// delay-aware algorithm for commutative operations.
//
//===----------------------------------------------------------------------===//
#include "circt/Dialect/Comb/CombDialect.h"
#include "circt/Dialect/Comb/CombOps.h"
#include "circt/Dialect/HW/HWDialect.h"
#include "circt/Dialect/HW/HWOps.h"
#include "circt/Dialect/Synth/Analysis/LongestPathAnalysis.h"
#include "circt/Dialect/Synth/SynthOps.h"
#include "circt/Dialect/Synth/Transforms/SynthPasses.h"
#include "mlir/Analysis/TopologicalSortUtils.h"
#include "mlir/IR/Block.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/IR/PatternMatch.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#define DEBUG_TYPE "synth-lower-variadic"
namespace circt {
namespace synth {
#define GEN_PASS_DEF_LOWERVARIADIC
#include "circt/Dialect/Synth/Transforms/SynthPasses.h.inc"
} // namespace synth
} // namespace circt
using namespace circt;
using namespace synth;
//===----------------------------------------------------------------------===//
// Lower Variadic pass
//===----------------------------------------------------------------------===//
namespace {
struct LowerVariadicPass : public impl::LowerVariadicBase<LowerVariadicPass> {
using LowerVariadicBase::LowerVariadicBase;
void runOnOperation() override;
};
} // namespace
/// Construct a balanced binary tree from a variadic operation using a
/// delay-aware algorithm. This function builds the tree by repeatedly combining
/// the two values with the earliest arrival times, which minimizes the critical
/// path delay.
static LogicalResult replaceWithBalancedTree(
IncrementalLongestPathAnalysis *analysis, mlir::IRRewriter &rewriter,
Operation *op, llvm::function_ref<bool(OpOperand &)> isInverted,
llvm::function_ref<Value(ValueWithArrivalTime, ValueWithArrivalTime)>
createBinaryOp) {
// Collect all operands with their arrival times and inversion flags
SmallVector<ValueWithArrivalTime> operands;
size_t valueNumber = 0;
for (size_t i = 0, e = op->getNumOperands(); i < e; ++i) {
int64_t delay = 0;
// If analysis is available, use it to compute the delay.
// If not available, use zero delay and `valueNumber` will be used instead.
if (analysis) {
auto result = analysis->getMaxDelay(op->getOperand(i));
if (failed(result))
return failure();
delay = *result;
}
operands.push_back(ValueWithArrivalTime(op->getOperand(i), delay,
isInverted(op->getOpOperand(i)),
valueNumber++));
}
// Use shared tree building utility
auto result = buildBalancedTreeWithArrivalTimes<ValueWithArrivalTime>(
operands,
// Combine: create binary operation and compute new arrival time
[&](const ValueWithArrivalTime &lhs, const ValueWithArrivalTime &rhs) {
Value combined = createBinaryOp(lhs, rhs);
int64_t newDelay = 0;
if (analysis) {
auto delayResult = analysis->getMaxDelay(combined);
if (succeeded(delayResult))
newDelay = *delayResult;
}
return ValueWithArrivalTime(combined, newDelay, false, valueNumber++);
});
rewriter.replaceOp(op, result.getValue());
return success();
}
// Returns if an operation is a subset of the other.
static bool isOpSubsetOf(aig::AndInverterOp sub, aig::AndInverterOp super) {
if (sub->getNumOperands() > super->getNumOperands())
return false;
SmallVector<bool> matched(super->getNumOperands(), false);
// All sub operands need to be in super.
for (size_t i = 0, e = sub.getNumOperands(); i < e; ++i) {
bool found = false;
for (size_t j = 0, e2 = super.getNumOperands(); j < e2; ++j) {
if (matched[j])
continue;
if (sub->getOperand(i) == super.getOperand(j) &&
sub.isInverted(i) == super.isInverted(j)) {
matched[j] = true;
found = true;
break;
}
}
if (!found)
return false;
}
return true;
}
static void simplifyWithExistingOperations(aig::AndInverterOp op,
mlir::IRRewriter &rewriter) {
mlir::Block *block = op->getBlock();
for (Operation &otherOp : *block) {
if (&otherOp == op.getOperation())
continue;
auto otherAIG = llvm::dyn_cast<aig::AndInverterOp>(otherOp);
if (!otherAIG)
continue;
if (isOpSubsetOf(otherAIG, op)) {
SmallVector<Value> newOperands;
SmallVector<bool> newInversions;
SmallVector<bool> isPartAsSubset(op.getNumOperands(), false);
// Find the operands that match.
for (size_t i = 0; i < otherAIG.getNumOperands(); ++i) {
for (size_t j = 0; j < op.getNumOperands(); ++j) {
if (!isPartAsSubset[j] &&
otherAIG.getOperand(i) == op.getOperand(j) &&
otherAIG.isInverted(i) == op.isInverted(j)) {
isPartAsSubset[j] = true;
break;
}
}
}
// Build the list of operands that are not shared.
for (size_t i = 0; i < op->getNumOperands(); ++i) {
if (!isPartAsSubset[i]) {
newOperands.push_back(op->getOperand(i));
newInversions.push_back(op.isInverted(i));
}
}
newOperands.push_back(otherAIG.getResult());
newInversions.push_back(false);
rewriter.modifyOpInPlace(op, [&]() {
op.getOperation()->setOperands(newOperands);
op->setAttr("inversions", rewriter.getBoolArrayAttr(newInversions));
});
// We found a first match, not necessarily the best one.
break;
}
}
}
void LowerVariadicPass::runOnOperation() {
// Topologically sort operations in graph regions to ensure operands are
// defined before uses.
if (!mlir::sortTopologically(
getOperation().getBodyBlock(), [](Value val, Operation *op) -> bool {
if (isa_and_nonnull<hw::HWDialect>(op->getDialect()))
return isa<hw::InstanceOp>(op);
return !isa_and_nonnull<comb::CombDialect, synth::SynthDialect>(
op->getDialect());
})) {
mlir::emitError(getOperation().getLoc())
<< "Failed to topologically sort graph region blocks";
return signalPassFailure();
}
// Get longest path analysis if timing-aware lowering is enabled.
synth::IncrementalLongestPathAnalysis *analysis = nullptr;
if (timingAware.getValue())
analysis = &getAnalysis<synth::IncrementalLongestPathAnalysis>();
auto moduleOp = getOperation();
// Build set of operation names to lower if specified.
SmallVector<OperationName> names;
for (const auto &name : opNames)
names.push_back(OperationName(name, &getContext()));
// Return true if the operation should be lowered.
auto shouldLower = [&](Operation *op) {
// If no names specified, lower all variadic ops.
if (names.empty())
return true;
return llvm::find(names, op->getName()) != names.end();
};
mlir::IRRewriter rewriter(&getContext());
rewriter.setListener(analysis);
// FIXME: Currently only top-level operations are lowered due to the lack of
// topological sorting in across nested regions.
for (auto &opRef :
llvm::make_early_inc_range(moduleOp.getBodyBlock()->getOperations())) {
auto *op = &opRef;
// Skip operations that don't need lowering or are already binary.
if (!shouldLower(op) || op->getNumOperands() <= 2)
continue;
rewriter.setInsertionPoint(op);
// Handle AndInverterOp specially to preserve inversion flags.
if (auto andInverterOp = dyn_cast<aig::AndInverterOp>(op)) {
simplifyWithExistingOperations(andInverterOp, rewriter);
auto result = replaceWithBalancedTree(
analysis, rewriter, op,
// Check if each operand is inverted.
[&](OpOperand &operand) {
return andInverterOp.isInverted(operand.getOperandNumber());
},
// Create binary AndInverterOp with inversion flags.
[&](ValueWithArrivalTime lhs, ValueWithArrivalTime rhs) {
return aig::AndInverterOp::create(
rewriter, op->getLoc(), lhs.getValue(), rhs.getValue(),
lhs.isInverted(), rhs.isInverted());
});
if (failed(result))
return signalPassFailure();
continue;
}
// Handle commutative operations (and, or, xor, mul, add, etc.) using
// delay-aware lowering to minimize critical path.
if (isa_and_nonnull<comb::CombDialect>(op->getDialect()) &&
op->hasTrait<OpTrait::IsCommutative>()) {
auto result = replaceWithBalancedTree(
analysis, rewriter, op,
// No inversion flags for standard commutative operations.
[](OpOperand &) { return false; },
// Create binary operation with the same operation type.
[&](ValueWithArrivalTime lhs, ValueWithArrivalTime rhs) {
OperationState state(op->getLoc(), op->getName());
state.addOperands(ValueRange{lhs.getValue(), rhs.getValue()});
state.addTypes(op->getResult(0).getType());
auto *newOp = Operation::create(state);
rewriter.insert(newOp);
return newOp->getResult(0);
});
if (failed(result))
return signalPassFailure();
}
}
}