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