10#include "mlir/IR/PatternMatch.h"
11#include "mlir/Support/LogicalResult.h"
21static bool isAlways(Attribute attr,
bool expected) {
22 if (
auto enable = dyn_cast_or_null<IntegerAttr>(attr))
23 return enable.getValue().getBoolValue() == expected;
31LogicalResult StateOp::fold(FoldAdaptor adaptor,
32 SmallVectorImpl<OpFoldResult> &results) {
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) {
45 if (hasExplicitInitials)
46 results.append(adaptor.getInitials().begin(),
47 adaptor.getInitials().end());
49 for (
auto resTy : getResultTypes())
50 results.push_back(IntegerAttr::get(resTy, 0));
53 if (!hasExplicitInitials &&
isAlways(adaptor.getReset(),
true)) {
56 for (
auto resTy : getResultTypes())
57 results.push_back(IntegerAttr::get(resTy, 0));
63 if (
isAlways(adaptor.getReset(),
false))
64 return getResetMutable().clear(),
success();
67 if (
isAlways(adaptor.getEnable(),
true))
68 return getEnableMutable().clear(),
success();
73LogicalResult StateOp::canonicalize(StateOp op, PatternRewriter &rewriter) {
76 if (op->use_empty() && !op->hasAttr(
"name") && !op->hasAttr(
"names")) {
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());
static bool isAlways(Attribute attr, bool expected)
false return failure()
Parse an optional keyword or string and set instance into 'result'.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.