| 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 "mlir/Transforms/Passes.h" |
| 15 | |
| 16 | #include "mlir/IR/DialectRegistry.h" |
| 17 | #include "mlir/Pass/Pass.h" |
| 18 | #include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 19 | #include "llvm/ADT/DenseSet.h" |
| 20 | |
| 21 | namespace mlir { |
| 22 | #define GEN_PASS_DEF_CANONICALIZERPASS |
| 23 | #include "mlir/Transforms/Passes.h.inc" |
| 24 | } // namespace mlir |
| 25 | |
| 26 | using namespace mlir; |
| 27 | |
| 28 | namespace { |
| 29 | /// Canonicalize operations in nested regions. |
| 30 | struct Canonicalizer : public impl::CanonicalizerPassBase<Canonicalizer> { |
| 31 | using impl::CanonicalizerPassBase<Canonicalizer>::CanonicalizerPassBase; |
| 32 | Canonicalizer(const GreedyRewriteConfig &config, |
| 33 | ArrayRef<std::string> disabledPatterns, |
| 34 | ArrayRef<std::string> enabledPatterns) |
| 35 | : config(config) { |
| 36 | this->topDownProcessingEnabled = config.getUseTopDownTraversal(); |
| 37 | this->regionSimplifyLevel = config.getRegionSimplificationLevel(); |
| 38 | this->maxIterations = config.getMaxIterations(); |
| 39 | this->maxNumRewrites = config.getMaxNumRewrites(); |
| 40 | this->cseBetweenIterations = config.isCSEBetweenIterationsEnabled(); |
| 41 | this->disabledPatterns = disabledPatterns; |
| 42 | this->enabledPatterns = enabledPatterns; |
| 43 | } |
| 44 | |
| 45 | void getDependentDialects(DialectRegistry ®istry) const override { |
| 46 | // Force-load any dialects named via the `filter-dialects` option. The |
| 47 | // allocator is resolved later from the MLIRContext's own registry. |
| 48 | for (const std::string &name : filterDialects) |
| 49 | registry.addDialectToPreload(name: StringRef(name)); |
| 50 | } |
| 51 | |
| 52 | /// Initialize the canonicalizer by building the set of patterns used during |
| 53 | /// execution. |
| 54 | LogicalResult initialize(MLIRContext *context) override { |
| 55 | // Set the config from possible pass options set in the meantime. |
| 56 | config.setUseTopDownTraversal(topDownProcessingEnabled); |
| 57 | config.setRegionSimplificationLevel(regionSimplifyLevel); |
| 58 | config.setMaxIterations(maxIterations); |
| 59 | config.setMaxNumRewrites(maxNumRewrites); |
| 60 | config.enableCSEBetweenIterations(enable: cseBetweenIterations); |
| 61 | |
| 62 | llvm::DenseSet<TypeID> allowedDialects; |
| 63 | for (const std::string &name : filterDialects) { |
| 64 | Dialect *dialect = context->getLoadedDialect(name); |
| 65 | assert(dialect && "filter-dialect should have been preloaded by the " |
| 66 | "PassManager via getDependentDialects" ); |
| 67 | allowedDialects.insert(V: dialect->getTypeID()); |
| 68 | } |
| 69 | auto isAllowed = [&](Dialect *dialect) { |
| 70 | return allowedDialects.empty() || |
| 71 | allowedDialects.contains(V: dialect->getTypeID()); |
| 72 | }; |
| 73 | |
| 74 | RewritePatternSet owningPatterns(context); |
| 75 | for (auto *dialect : context->getLoadedDialects()) |
| 76 | if (isAllowed(dialect)) |
| 77 | dialect->getCanonicalizationPatterns(results&: owningPatterns); |
| 78 | for (RegisteredOperationName op : context->getRegisteredOperations()) |
| 79 | if (isAllowed(&op.getDialect())) |
| 80 | op.getCanonicalizationPatterns(results&: owningPatterns, context); |
| 81 | |
| 82 | patterns = std::make_shared<FrozenRewritePatternSet>( |
| 83 | args: std::move(owningPatterns), args&: disabledPatterns, args&: enabledPatterns); |
| 84 | return success(); |
| 85 | } |
| 86 | void runOnOperation() override { |
| 87 | bool changed = false; |
| 88 | LogicalResult converged = |
| 89 | applyPatternsGreedily(op: getOperation(), patterns: *patterns, config, changed: &changed); |
| 90 | // Canonicalization is best-effort. Non-convergence is not a pass failure. |
| 91 | if (testConvergence && failed(Result: converged)) |
| 92 | signalPassFailure(); |
| 93 | if (!changed) |
| 94 | markAllAnalysesPreserved(); |
| 95 | } |
| 96 | GreedyRewriteConfig config; |
| 97 | std::shared_ptr<const FrozenRewritePatternSet> patterns; |
| 98 | }; |
| 99 | } // namespace |
| 100 | |
| 101 | /// Creates an instance of the Canonicalizer pass with the specified config. |
| 102 | std::unique_ptr<Pass> |
| 103 | mlir::createCanonicalizerPass(const GreedyRewriteConfig &config, |
| 104 | ArrayRef<std::string> disabledPatterns, |
| 105 | ArrayRef<std::string> enabledPatterns) { |
| 106 | return std::make_unique<Canonicalizer>(args: config, args&: disabledPatterns, |
| 107 | args&: enabledPatterns); |
| 108 | } |
| 109 | |