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/Pass/Pass.h" 16 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 17 #include "mlir/Transforms/Passes.h" 18 19 using namespace mlir; 20 21 namespace { 22 /// Canonicalize operations in nested regions. 23 struct Canonicalizer : public CanonicalizerBase<Canonicalizer> { runOnOperation__anona5b21df90111::Canonicalizer24 void runOnOperation() override { 25 OwningRewritePatternList patterns; 26 27 // TODO: Instead of adding all known patterns from the whole system lazily 28 // add and cache the canonicalization patterns for ops we see in practice 29 // when building the worklist. For now, we just grab everything. 30 auto *context = &getContext(); 31 for (auto *op : context->getRegisteredOperations()) 32 op->getCanonicalizationPatterns(patterns, context); 33 34 Operation *op = getOperation(); 35 applyPatternsAndFoldGreedily(op->getRegions(), std::move(patterns)); 36 } 37 }; 38 } // end anonymous namespace 39 40 /// Create a Canonicalizer pass. createCanonicalizerPass()41std::unique_ptr<Pass> mlir::createCanonicalizerPass() { 42 return std::make_unique<Canonicalizer>(); 43 } 44