1 //===- Canonicalizer.cpp - Canonicalize MLIR operations -------------------===// 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 // 9 // This transformation pass converts operations into their canonical forms by 10 // folding constants, applying operation identity transformations etc. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "PassDetail.h" 15 #include "mlir/Dialect/MemRef/IR/MemRef.h" 16 #include "mlir/Pass/Pass.h" 17 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 18 #include "mlir/Transforms/Passes.h" 19 20 using namespace mlir; 21 22 namespace { 23 /// Canonicalize operations in nested regions. 24 struct Canonicalizer : public CanonicalizerBase<Canonicalizer> { 25 /// Initialize the canonicalizer by building the set of patterns used during 26 /// execution. 27 LogicalResult initialize(MLIRContext *context) override { 28 RewritePatternSet owningPatterns(context); 29 for (auto *op : context->getRegisteredOperations()) 30 op->getCanonicalizationPatterns(owningPatterns, context); 31 patterns = std::move(owningPatterns); 32 return success(); 33 } 34 void runOnOperation() override { 35 (void)applyPatternsAndFoldGreedily(getOperation()->getRegions(), patterns); 36 } 37 38 FrozenRewritePatternSet patterns; 39 }; 40 } // end anonymous namespace 41 42 /// Create a Canonicalizer pass. 43 std::unique_ptr<Pass> mlir::createCanonicalizerPass() { 44 return std::make_unique<Canonicalizer>(); 45 } 46