1//===- StandalonePasses.cpp - Standalone passes -----------------*- C++ -*-===//
2//
3// This file is licensed 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#include "mlir/Dialect/Func/IR/FuncOps.h"
9#include "mlir/IR/PatternMatch.h"
10#include "mlir/Rewrite/FrozenRewritePatternSet.h"
11#include "mlir/Support/LogicalResult.h"
12#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
13
14#include "Standalone/StandalonePasses.h"
15
16namespace mlir::standalone {
17#define GEN_PASS_DEF_STANDALONESWITCHBARFOO
18#include "Standalone/StandalonePasses.h.inc"
19
20namespace {
21class StandaloneSwitchBarFooRewriter : public OpRewritePattern<func::FuncOp> {
22public:
23 using OpRewritePattern<func::FuncOp>::OpRewritePattern;
24 LogicalResult matchAndRewrite(func::FuncOp op,
25 PatternRewriter &rewriter) const final {
26 if (op.getSymName() == "bar") {
27 rewriter.modifyOpInPlace(op, [&op]() { op.setSymName("foo"); });
28 return success();
29 }
30 return failure();
31 }
32};
33
34class StandaloneSwitchBarFoo
35 : public impl::StandaloneSwitchBarFooBase<StandaloneSwitchBarFoo> {
36public:
37 using impl::StandaloneSwitchBarFooBase<
38 StandaloneSwitchBarFoo>::StandaloneSwitchBarFooBase;
39 void runOnOperation() final {
40 RewritePatternSet patterns(&getContext());
41 patterns.add<StandaloneSwitchBarFooRewriter>(&getContext());
42 FrozenRewritePatternSet patternSet(std::move(patterns));
43 if (failed(applyPatternsAndFoldGreedily(getOperation(), patternSet)))
44 signalPassFailure();
45 }
46};
47} // namespace
48} // namespace mlir::standalone
49

source code of mlir/examples/standalone/lib/Standalone/StandalonePasses.cpp