1 //===- GreedyPatternRewriteDriver.h - Greedy Pattern Driver -----*- C++ -*-===// 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 file declares methods for applying a set of patterns greedily, choosing 10 // the patterns with the highest local benefit, until a fixed point is reached. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef MLIR_TRANSFORMS_GREEDYPATTERNREWRITEDRIVER_H_ 15 #define MLIR_TRANSFORMS_GREEDYPATTERNREWRITEDRIVER_H_ 16 17 #include "mlir/Rewrite/FrozenRewritePatternList.h" 18 19 namespace mlir { 20 21 //===----------------------------------------------------------------------===// 22 // applyPatternsGreedily 23 //===----------------------------------------------------------------------===// 24 25 /// Rewrite the regions of the specified operation, which must be isolated from 26 /// above, by repeatedly applying the highest benefit patterns in a greedy 27 /// work-list driven manner. 28 /// This variant may stop after a predefined number of iterations, see the 29 /// alternative below to provide a specific number of iterations before stopping 30 /// in absence of convergence. 31 /// Return success if the iterative process converged and no more patterns can 32 /// be matched in the result operation regions. 33 /// Note: This does not apply patterns to the top-level operation itself. 34 /// These methods also perform folding and simple dead-code elimination 35 /// before attempting to match any of the provided patterns. 36 LogicalResult 37 applyPatternsAndFoldGreedily(Operation *op, 38 const FrozenRewritePatternList &patterns); 39 40 /// Rewrite the regions of the specified operation, with a user-provided limit 41 /// on iterations to attempt before reaching convergence. 42 LogicalResult 43 applyPatternsAndFoldGreedily(Operation *op, 44 const FrozenRewritePatternList &patterns, 45 unsigned maxIterations); 46 47 /// Rewrite the given regions, which must be isolated from above. 48 LogicalResult 49 applyPatternsAndFoldGreedily(MutableArrayRef<Region> regions, 50 const FrozenRewritePatternList &patterns); 51 52 /// Rewrite the given regions, with a user-provided limit on iterations to 53 /// attempt before reaching convergence. 54 LogicalResult 55 applyPatternsAndFoldGreedily(MutableArrayRef<Region> regions, 56 const FrozenRewritePatternList &patterns, 57 unsigned maxIterations); 58 59 /// Applies the specified patterns on `op` alone while also trying to fold it, 60 /// by selecting the highest benefits patterns in a greedy manner. Returns 61 /// success if no more patterns can be matched. `erased` is set to true if `op` 62 /// was folded away or erased as a result of becoming dead. Note: This does not 63 /// apply any patterns recursively to the regions of `op`. 64 LogicalResult applyOpPatternsAndFold(Operation *op, 65 const FrozenRewritePatternList &patterns, 66 bool *erased = nullptr); 67 68 } // end namespace mlir 69 70 #endif // MLIR_TRANSFORMS_GREEDYPATTERNREWRITEDRIVER_H_ 71