1 //===- Canonicalizer.cpp - Canonicalize MLIR operations -------------------===// 2 // 3 // Copyright 2019 The MLIR Authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // ============================================================================= 17 // 18 // This transformation pass converts operations into their canonical forms by 19 // folding constants, applying operation identity transformations etc. 20 // 21 //===----------------------------------------------------------------------===// 22 23 #include "mlir/IR/MLIRContext.h" 24 #include "mlir/IR/PatternMatch.h" 25 #include "mlir/Pass/Pass.h" 26 #include "mlir/Transforms/Passes.h" 27 using namespace mlir; 28 29 namespace { 30 /// Canonicalize operations in nested regions. 31 struct Canonicalizer : public OperationPass<Canonicalizer> { 32 void runOnOperation() override { 33 OwningRewritePatternList patterns; 34 35 // TODO: Instead of adding all known patterns from the whole system lazily 36 // add and cache the canonicalization patterns for ops we see in practice 37 // when building the worklist. For now, we just grab everything. 38 auto *context = &getContext(); 39 for (auto *op : context->getRegisteredOperations()) 40 op->getCanonicalizationPatterns(patterns, context); 41 42 Operation *op = getOperation(); 43 applyPatternsGreedily(op->getRegions(), patterns); 44 } 45 }; 46 } // end anonymous namespace 47 48 /// Create a Canonicalizer pass. 49 std::unique_ptr<Pass> mlir::createCanonicalizerPass() { 50 return std::make_unique<Canonicalizer>(); 51 } 52 53 static PassRegistration<Canonicalizer> pass("canonicalize", 54 "Canonicalize operations"); 55