//===- PatternMatch.h - PatternMatcher classes -------==---------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef MLIR_IR_PATTERNMATCH_H #define MLIR_IR_PATTERNMATCH_H #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinOps.h" #include "llvm/ADT/FunctionExtras.h" #include "llvm/Support/TypeName.h" #include <optional> SmallPtrSetImpl; namespace mlir { class PatternRewriter; //===----------------------------------------------------------------------===// // PatternBenefit class //===----------------------------------------------------------------------===// /// This class represents the benefit of a pattern match in a unitless scheme /// that ranges from 0 (very little benefit) to 65K. The most common unit to /// use here is the "number of operations matched" by the pattern. /// /// This also has a sentinel representation that can be used for patterns that /// fail to match. /// class PatternBenefit { … }; //===----------------------------------------------------------------------===// // Pattern //===----------------------------------------------------------------------===// /// This class contains all of the data related to a pattern, but does not /// contain any methods or logic for the actual matching. This class is solely /// used to interface with the metadata of a pattern, such as the benefit or /// root operation. class Pattern { … }; //===----------------------------------------------------------------------===// // RewritePattern //===----------------------------------------------------------------------===// /// RewritePattern is the common base class for all DAG to DAG replacements. /// There are two possible usages of this class: /// * Multi-step RewritePattern with "match" and "rewrite" /// - By overloading the "match" and "rewrite" functions, the user can /// separate the concerns of matching and rewriting. /// * Single-step RewritePattern with "matchAndRewrite" /// - By overloading the "matchAndRewrite" function, the user can perform /// the rewrite in the same call as the match. /// class RewritePattern : public Pattern { … }; namespace detail { /// OpOrInterfaceRewritePatternBase is a wrapper around RewritePattern that /// allows for matching and rewriting against an instance of a derived operation /// class or Interface. template <typename SourceOp> struct OpOrInterfaceRewritePatternBase : public RewritePattern { … }; } // namespace detail /// OpRewritePattern is a wrapper around RewritePattern that allows for /// matching and rewriting against an instance of a derived operation class as /// opposed to a raw Operation. template <typename SourceOp> struct OpRewritePattern : public detail::OpOrInterfaceRewritePatternBase<SourceOp> { … }; /// OpInterfaceRewritePattern is a wrapper around RewritePattern that allows for /// matching and rewriting against an instance of an operation interface instead /// of a raw Operation. template <typename SourceOp> struct OpInterfaceRewritePattern : public detail::OpOrInterfaceRewritePatternBase<SourceOp> { … }; /// OpTraitRewritePattern is a wrapper around RewritePattern that allows for /// matching and rewriting against instances of an operation that possess a /// given trait. template <template <typename> class TraitType> class OpTraitRewritePattern : public RewritePattern { … }; //===----------------------------------------------------------------------===// // RewriterBase //===----------------------------------------------------------------------===// /// This class coordinates the application of a rewrite on a set of IR, /// providing a way for clients to track mutations and create new operations. /// This class serves as a common API for IR mutation between pattern rewrites /// and non-pattern rewrites, and facilitates the development of shared /// IR transformation utilities. class RewriterBase : public OpBuilder { … }; //===----------------------------------------------------------------------===// // IRRewriter //===----------------------------------------------------------------------===// /// This class coordinates rewriting a piece of IR outside of a pattern rewrite, /// providing a way to keep track of the mutations made to the IR. This class /// should only be used in situations where another `RewriterBase` instance, /// such as a `PatternRewriter`, is not available. class IRRewriter : public RewriterBase { … }; //===----------------------------------------------------------------------===// // PatternRewriter //===----------------------------------------------------------------------===// /// A special type of `RewriterBase` that coordinates the application of a /// rewrite pattern on the current IR being matched, providing a way to keep /// track of any mutations made. This class should be used to perform all /// necessary IR mutations within a rewrite pattern, as the pattern driver may /// be tracking various state that would be invalidated when a mutation takes /// place. class PatternRewriter : public RewriterBase { … }; } // namespace mlir // Optionally expose PDL pattern matching methods. #include "PDLPatternMatch.h.inc" namespace mlir { //===----------------------------------------------------------------------===// // RewritePatternSet //===----------------------------------------------------------------------===// class RewritePatternSet { … }; } // namespace mlir #endif // MLIR_IR_PATTERNMATCH_H