14#include "mlir/IR/SymbolTable.h"
15#include "mlir/Pass/Pass.h"
17#define DEBUG_TYPE "arc-infer-state-properties"
21#define GEN_PASS_DEF_INFERSTATEPROPERTIES
22#include "circt/Dialect/Arc/ArcPasses.h.inc"
35 return constOp.getValue().isZero();
42 return constOp.getValue().getBitWidth() == 1 &&
43 constOp.getValue().isAllOnes();
56 ResetInfo() =
default;
57 ResetInfo(std::function<Value(OpBuilder &)> &&constructInput,
58 BlockArgument condition,
bool isZeroReset)
59 : constructInput(constructInput), condition(condition),
60 isZeroReset(isZeroReset) {}
62 ResetInfo(Value input, BlockArgument condition,
bool isZeroReset)
63 : ResetInfo([=](OpBuilder &) {
return input; }, condition, isZeroReset) {}
65 std::function<Value(OpBuilder &)> constructInput;
66 BlockArgument condition;
69 operator bool() {
return constructInput && condition; }
75 EnableInfo() =
default;
76 EnableInfo(std::function<Value(OpBuilder &)> &&constructInput,
77 BlockArgument condition, BlockArgument selfArg,
bool isDisable)
78 : constructInput(constructInput), condition(condition), selfArg(selfArg),
79 isDisable(isDisable) {}
81 EnableInfo(Value input, BlockArgument condition, BlockArgument selfArg,
83 : EnableInfo([=](OpBuilder &) {
return input; }, condition, selfArg,
86 std::function<Value(OpBuilder &)> constructInput;
87 BlockArgument condition;
88 BlockArgument selfArg;
91 operator bool() {
return constructInput && condition && selfArg; }
102 ArrayRef<ResetInfo> resetInfos) {
103 auto outputOp = cast<arc::OutputOp>(arcOp.getBodyBlock().getTerminator());
105 assert(outputOp.getOutputs().size() == resetInfos.size() &&
106 "required to pass the same amount of resets as outputs of the arc");
108 for (
auto info : resetInfos) {
116 if (!resetInfos.empty() &&
117 (info.condition != resetInfos.back().condition ||
118 info.isZeroReset != resetInfos.back().isZeroReset))
122 if (!info.isZeroReset)
126 if (resetInfos.empty())
129 OpBuilder builder(outputOp);
131 for (
size_t i = 0, e = outputOp.getOutputs().size(); i < e; ++i) {
132 auto *defOp = outputOp.getOperands()[i].getDefiningOp();
133 outputOp.getOperands()[i].replaceUsesWithIf(
134 resetInfos[i].constructInput(builder),
135 [](OpOperand &op) {
return isa<arc::OutputOp>(op.getOwner()); });
137 if (defOp && defOp->getResult(0).use_empty())
147 unsigned resetConditionIndex) {
148 Value resetCond = stateOp.getInputs()[resetConditionIndex];
149 ImplicitLocOpBuilder builder(stateOp.getLoc(), stateOp);
151 if (stateOp.getEnable())
152 resetCond = comb::AndOp::create(builder, stateOp.getEnable(), resetCond);
154 if (stateOp.getReset())
155 resetCond = comb::OrOp::create(builder, stateOp.getReset(), resetCond);
157 stateOp.getResetMutable().assign(resetCond);
165 ArrayRef<EnableInfo> enableInfos) {
166 auto outputOp = cast<arc::OutputOp>(arcOp.getBodyBlock().getTerminator());
168 assert(outputOp.getOutputs().size() == enableInfos.size() &&
169 "required to pass the same amount of enables as outputs of the arc");
171 for (
auto info : enableInfos) {
176 if (info.selfArg.hasOneUse() &&
hw::getBitWidth(info.selfArg.getType()) < 0)
183 if (!enableInfos.empty() &&
184 (info.condition != enableInfos.back().condition ||
185 info.isDisable != enableInfos.back().isDisable))
189 if (enableInfos.empty())
192 if (!enableInfos[0].condition.hasOneUse())
195 ImplicitLocOpBuilder builder(stateOp.getLoc(), stateOp);
196 SmallVector<Value> inputs(stateOp.getInputs());
199 stateOp.getInputs()[enableInfos[0].condition.getArgNumber()];
201 if (enableInfos[0].isDisable) {
202 inputs[enableInfos[0].condition.getArgNumber()] =
204 enableCond = comb::XorOp::create(builder, enableCond, one);
206 inputs[enableInfos[0].condition.getArgNumber()] = one;
209 if (stateOp.getEnable())
210 enableCond = comb::AndOp::create(builder, stateOp.getEnable(), enableCond);
212 stateOp.getEnableMutable().assign(enableCond);
214 for (
size_t i = 0, e = outputOp.getOutputs().size(); i < e; ++i) {
215 if (enableInfos[i].selfArg.hasOneUse()) {
218 auto type = enableInfos[i].selfArg.getType();
220 builder, stateOp.getLoc(),
222 if (zero.getType() != type)
224 inputs[enableInfos[i].selfArg.getArgNumber()] = zero;
228 stateOp.getInputsMutable().assign(inputs);
244 assert(isa<arc::OutputOp>(output.getOwner()) &&
245 "value has to be returned by the arc");
247 if (
auto mux = output.get().getDefiningOp<
comb::MuxOp>()) {
251 if (!mux.getResult().hasOneUse())
254 if (
auto condArg = dyn_cast<BlockArgument>(mux.getCond()))
255 return ResetInfo(mux.getFalseValue(), condArg,
true);
267 assert(isa<arc::OutputOp>(output.getOwner()) &&
268 "value has to be returned by the arc");
270 if (
auto andOp = output.get().getDefiningOp<
comb::AndOp>()) {
271 if (!andOp.getResult().getType().isInteger(1))
274 if (!andOp.getResult().hasOneUse())
277 for (
auto &operand : andOp->getOpOperands()) {
278 if (
auto xorOp = operand.get().getDefiningOp<
comb::XorOp>();
279 xorOp && xorOp->getNumOperands() == 2 &&
280 xorOp.getResult().hasOneUse()) {
281 if (
auto condArg = dyn_cast<BlockArgument>(xorOp.getInputs()[0])) {
282 if (xorOp.getInputs().size() != 2 ||
286 const unsigned condOutputNumber = operand.getOperandNumber();
287 auto inputConstructor = [=](OpBuilder &builder) -> Value {
288 if (andOp->getNumOperands() > 2) {
289 builder.setInsertionPoint(andOp);
290 auto copy = cast<comb::AndOp>(builder.clone(*andOp));
291 copy.getInputsMutable().erase(condOutputNumber);
292 return copy->getResult(0);
295 return andOp->getOperand(!condOutputNumber);
298 return ResetInfo(inputConstructor, condArg,
true);
312 Value cond,
unsigned outputNr,
314 if (
auto trueArg = dyn_cast<BlockArgument>(selfArg)) {
315 if (stateOp.getInputs()[trueArg.getArgNumber()] !=
316 stateOp.getResult(outputNr))
319 if (
auto condArg = dyn_cast<BlockArgument>(cond))
320 return EnableInfo(selfArg, condArg, trueArg, isDisable);
331 assert(isa<arc::OutputOp>(output.getOwner()) &&
332 "value has to be returned by the arc");
334 if (
auto mux = output.get().getDefiningOp<
comb::MuxOp>()) {
335 if (!mux.getResult().hasOneUse())
339 output.getOperandNumber(),
false);
351 assert(isa<arc::OutputOp>(output.getOwner()) &&
352 "value has to be returned by the arc");
354 if (
auto mux = output.get().getDefiningOp<
comb::MuxOp>()) {
355 if (!mux.getResult().hasOneUse())
359 output.getOperandNumber(),
true);
394struct InferStatePropertiesPass
395 :
public impl::InferStatePropertiesBase<InferStatePropertiesPass> {
396 using InferStatePropertiesBase::InferStatePropertiesBase;
398 void runOnOperation()
override;
399 void runOnStateOp(arc::StateOp stateOp, arc::DefineOp arc,
400 DenseMap<arc::DefineOp, unsigned> &resetConditionMap,
401 const mlir::SymbolUserMap &symbolUsers);
405void InferStatePropertiesPass::runOnOperation() {
406 SymbolTableCollection symbolTable;
407 mlir::SymbolUserMap symbolUsers(symbolTable, getOperation());
409 DenseMap<arc::DefineOp, unsigned> resetConditionMap;
410 getOperation()->walk([&](arc::StateOp stateOp) {
412 cast<arc::DefineOp>(cast<mlir::CallOpInterface>(stateOp.getOperation())
413 .resolveCallableInTable(&symbolTable));
414 runOnStateOp(stateOp, arc, resetConditionMap, symbolUsers);
418void InferStatePropertiesPass::runOnStateOp(
419 arc::StateOp stateOp, arc::DefineOp arc,
420 DenseMap<arc::DefineOp, unsigned> &resetConditionMap,
421 const mlir::SymbolUserMap &symbolUsers) {
423 auto outputOp = cast<arc::OutputOp>(arc.getBodyBlock().getTerminator());
424 static constexpr unsigned visitedNoChange = -1;
429 bool seen = resetConditionMap.count(arc);
435 if (!seen && !llvm::all_of(symbolUsers.getUsers(arc), [](Operation *user) {
436 return isa<arc::StateOp>(user);
438 resetConditionMap[arc] = visitedNoChange;
443 SmallVector<ResetInfo> resetInfos;
445 for (
auto &output : outputOp->getOpOperands()) {
447 resetInfos.push_back(resetInfo);
454 if ((succeeded(result) && resetInfos[0]))
455 resetConditionMap[arc] = resetInfos[0].condition.getArgNumber();
457 resetConditionMap[arc] = visitedNoChange;
460 missedResets += numResets;
464 if (resetConditionMap[arc] != visitedNoChange) {
472 SmallVector<EnableInfo> enableInfos;
474 for (OpOperand &output : outputOp->getOpOperands()) {
476 enableInfos.push_back(enableInfo);
485 missedEnables += numEnables;
assert(baseType &&"element must be base type")
static bool isConstTrue(Value value)
static EnableInfo checkOperandsForEnable(arc::StateOp stateOp, Value selfArg, Value cond, unsigned outputNr, bool isDisable)
Just a helper function for the following two patterns.
static EnableInfo getIfMuxBasedDisable(OpOperand &output, StateOp stateOp)
A negated enable represented by a single mux operation.
static ResetInfo getIfAndBasedReset(OpOperand &output)
A reset represented by an AND and XOR operation for i1 values only.
static LogicalResult applyEnableTransformation(arc::DefineOp arcOp, arc::StateOp stateOp, ArrayRef< EnableInfo > enableInfos)
Take an arc and a detected enable per output value and apply it to the given state if applicable (no ...
static bool isConstZero(Value value)
static LogicalResult applyResetTransformation(arc::DefineOp arcOp, ArrayRef< ResetInfo > resetInfos)
Take an arc and a detected reset per output value and apply it to the arc if applicable (but does not...
EnableInfo computeEnableInfoFromPattern(OpOperand &output, StateOp stateOp)
Combine all the enable patterns to one.
static void setResetOperandOfStateOp(arc::StateOp stateOp, unsigned resetConditionIndex)
Transform the given state operation to match the changes done to the arc in 'applyResetTransformation...
ResetInfo computeResetInfoFromPattern(OpOperand &output)
Combine all the reset patterns to one.
static EnableInfo getIfMuxBasedEnable(OpOperand &output, StateOp stateOp)
An enable represented by a single mux operation.
static ResetInfo getIfMuxBasedReset(OpOperand &output)
A reset represented with a single mux operation.
false return failure()
Parse an optional keyword or string and set instance into 'result'.
int64_t getBitWidth(mlir::Type type)
Return the hardware bit width of a type.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.