CIRCT 23.0.0git
Loading...
Searching...
No Matches
ArcFolds.cpp
Go to the documentation of this file.
1//===- ArcFolds.cpp -------------------------------------------------------===//
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
10#include "mlir/IR/PatternMatch.h"
11#include "mlir/Support/LogicalResult.h"
12
13using namespace circt;
14using namespace arc;
15using namespace mlir;
16
17//===----------------------------------------------------------------------===//
18// Helpers
19//===----------------------------------------------------------------------===//
20
21static bool isAlways(Attribute attr, bool expected) {
22 if (auto enable = dyn_cast_or_null<IntegerAttr>(attr))
23 return enable.getValue().getBoolValue() == expected;
24 return false;
25}
26
27//===----------------------------------------------------------------------===//
28// StateOp
29//===----------------------------------------------------------------------===//
30
31LogicalResult StateOp::fold(FoldAdaptor adaptor,
32 SmallVectorImpl<OpFoldResult> &results) {
33
34 if (getNumResults() > 0 && !getOperation()->hasAttr("name") &&
35 !getOperation()->hasAttr("names")) {
36 bool hasExplicitInitials = !getInitials().empty();
37 bool allInitialsConstant =
38 !hasExplicitInitials ||
39 llvm::all_of(adaptor.getInitials(),
40 [&](Attribute attr) { return !!attr; });
41 if (isAlways(adaptor.getEnable(), false) && allInitialsConstant) {
42 // Fold to the explicit or implicit initial value if
43 // the state is never enabled and the initial values
44 // are compile-time constants.
45 if (hasExplicitInitials)
46 results.append(adaptor.getInitials().begin(),
47 adaptor.getInitials().end());
48 else
49 for (auto resTy : getResultTypes())
50 results.push_back(IntegerAttr::get(resTy, 0));
51 return success();
52 }
53 if (!hasExplicitInitials && isAlways(adaptor.getReset(), true)) {
54 // We assume both the implicit initial value and the
55 // implicit (synchronous) reset value to be zero.
56 for (auto resTy : getResultTypes())
57 results.push_back(IntegerAttr::get(resTy, 0));
58 return success();
59 }
60 }
61
62 // Remove operand when input is default value.
63 if (isAlways(adaptor.getReset(), false))
64 return getResetMutable().clear(), success();
65
66 // Remove operand when input is default value.
67 if (isAlways(adaptor.getEnable(), true))
68 return getEnableMutable().clear(), success();
69
70 return failure();
71}
72
73LogicalResult StateOp::canonicalize(StateOp op, PatternRewriter &rewriter) {
74 // When there are no names attached, the state is not externaly observable.
75 // When there are also no internal users, we can remove it.
76 if (op->use_empty() && !op->hasAttr("name") && !op->hasAttr("names")) {
77 rewriter.eraseOp(op);
78 return success();
79 }
80
81 return failure();
82}
83
84//===----------------------------------------------------------------------===//
85// StorageGetOp
86//===----------------------------------------------------------------------===//
87
88LogicalResult StorageGetOp::canonicalize(StorageGetOp op,
89 PatternRewriter &rewriter) {
90 if (auto pred = op.getStorage().getDefiningOp<StorageGetOp>()) {
91 rewriter.modifyOpInPlace(op, [&] {
92 op.getStorageMutable().assign(pred.getStorage());
93 op.setOffset(op.getOffset() + pred.getOffset());
94 });
95 return success();
96 }
97 return failure();
98}
static bool isAlways(Attribute attr, bool expected)
Definition ArcFolds.cpp:21
Definition arc.py:1
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition CalyxOps.cpp:56
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.