//===- Pattern.h - Pattern wrapper class ------------------------*- 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 // //===----------------------------------------------------------------------===// // // Pattern wrapper class to simplify using TableGen Record defining a MLIR // Pattern. // //===----------------------------------------------------------------------===// #ifndef MLIR_TABLEGEN_PATTERN_H_ #define MLIR_TABLEGEN_PATTERN_H_ #include "mlir/Support/LLVM.h" #include "mlir/TableGen/Argument.h" #include "mlir/TableGen/Operator.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringSet.h" #include <optional> #include <unordered_map> namespace llvm { class DagInit; class Init; class Record; } // namespace llvm namespace mlir { namespace tblgen { // Mapping from TableGen Record to Operator wrapper object. // // We allocate each wrapper object in heap to make sure the pointer to it is // valid throughout the lifetime of this map. This is important because this map // is shared among multiple patterns to avoid creating the wrapper object for // the same op again and again. But this map will continuously grow. RecordOperatorMap; class Pattern; // Wrapper class providing helper methods for accessing TableGen DAG leaves // used inside Patterns. This class is lightweight and designed to be used like // values. // // A TableGen DAG construct is of the syntax // `(operator, arg0, arg1, ...)`. // // This class provides getters to retrieve `arg*` as tblgen:: wrapper objects // for handy helper methods. It only works on `arg*`s that are not nested DAG // constructs. class DagLeaf { … }; // Wrapper class providing helper methods for accessing TableGen DAG constructs // used inside Patterns. This class is lightweight and designed to be used like // values. // // A TableGen DAG construct is of the syntax // `(operator, arg0, arg1, ...)`. // // When used inside Patterns, `operator` corresponds to some dialect op, or // a known list of verbs that defines special transformation actions. This // `arg*` can be a nested DAG construct. This class provides getters to // retrieve `operator` and `arg*` as tblgen:: wrapper objects for handy helper // methods. // // A null DagNode contains a nullptr and converts to false implicitly. class DagNode { … }; // A class for maintaining information for symbols bound in patterns and // provides methods for resolving them according to specific use cases. // // Symbols can be bound to // // * Op arguments and op results in the source pattern and // * Op results in result patterns. // // Symbols can be referenced in result patterns and additional constraints to // the pattern. // // For example, in // // ``` // def : Pattern< // (SrcOp:$results1 $arg0, %arg1), // [(ResOp1:$results2), (ResOp2 $results2 (ResOp3 $arg0, $arg1))]>; // ``` // // `$argN` is bound to the `SrcOp`'s N-th argument. `$results1` is bound to // `SrcOp`. `$results2` is bound to `ResOp1`. $result2 is referenced to build // `ResOp2`. `$arg0` and `$arg1` are referenced to build `ResOp3`. // // If a symbol binds to a multi-result op and it does not have the `__N` // suffix, the symbol is expanded to represent all results generated by the // multi-result op. If the symbol has a `__N` suffix, then it will expand to // only the N-th *static* result as declared in ODS, and that can still // corresponds to multiple *dynamic* values if the N-th *static* result is // variadic. // // This class keeps track of such symbols and resolves them into their bound // values in a suitable way. class SymbolInfoMap { … }; // Wrapper class providing helper methods for accessing MLIR Pattern defined // in TableGen. This class should closely reflect what is defined as class // `Pattern` in TableGen. This class contains maps so it is not intended to be // used as values. class Pattern { … }; } // namespace tblgen } // namespace mlir namespace llvm { template <> struct DenseMapInfo<mlir::tblgen::DagNode> { … }; template <> struct DenseMapInfo<mlir::tblgen::DagLeaf> { … }; } // namespace llvm #endif // MLIR_TABLEGEN_PATTERN_H_