forked from makslevental/mmlir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimalDialect.cpp
95 lines (80 loc) · 2.96 KB
/
MinimalDialect.cpp
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
//===- MinimalDialect.cpp - Minimal dialect ---------------------*- C++ -*-===//
//
// This file is licensed 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
//
//===----------------------------------------------------------------------===//
#include "MinimalDialect.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/InitAllDialects.h"
#include "mlir/InitAllPasses.h"
#include "mlir/Rewrite/FrozenRewritePatternSet.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Tools/mlir-opt/MlirOptMain.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
using namespace mlir;
using namespace mlir::minimal;
#include "MinimalDialect.cpp.inc"
//===----------------------------------------------------------------------===//
// Minimal dialect.
//===----------------------------------------------------------------------===//
void MinimalDialect::initialize() {
addOperations<
#define GET_OP_LIST
#include "MinimalOps.cpp.inc"
>();
registerTypes();
}
//===----------------------------------------------------------------------===//
// Minimal ops
//===----------------------------------------------------------------------===//
#define GET_OP_CLASSES
#include "MinimalOps.cpp.inc"
namespace mlir::minimal {
#define GEN_PASS_DEF_MINIMALSWITCHBARFOO
#include "MinimalPasses.h.inc"
//===----------------------------------------------------------------------===//
// Minimal passes
//===----------------------------------------------------------------------===//
namespace {
class MinimalSwitchBarFooRewriter : public OpRewritePattern<func::FuncOp> {
public:
using OpRewritePattern<func::FuncOp>::OpRewritePattern;
LogicalResult matchAndRewrite(func::FuncOp op,
PatternRewriter &rewriter) const final {
if (op.getSymName() == "bar") {
rewriter.modifyOpInPlace(op, [&op]() { op.setSymName("foo"); });
return success();
}
return failure();
}
};
class MinimalSwitchBarFoo
: public impl::MinimalSwitchBarFooBase<MinimalSwitchBarFoo> {
public:
using impl::MinimalSwitchBarFooBase<
MinimalSwitchBarFoo>::MinimalSwitchBarFooBase;
void runOnOperation() final {
RewritePatternSet patterns(&getContext());
patterns.add<MinimalSwitchBarFooRewriter>(&getContext());
FrozenRewritePatternSet patternSet(std::move(patterns));
if (failed(applyPatternsAndFoldGreedily(getOperation(), patternSet)))
signalPassFailure();
}
};
} // namespace
} // namespace mlir::minimal
//===----------------------------------------------------------------------===//
// Minimal types
//===----------------------------------------------------------------------===//
#define GET_TYPEDEF_CLASSES
#include "MinimalTypes.cpp.inc"
void MinimalDialect::registerTypes() {
addTypes<
#define GET_TYPEDEF_LIST
#include "MinimalTypes.cpp.inc"
>();
}