|
| 1 | +//===- EndomorphismSimplification.h -----------------------------*- 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 | +#ifndef MLIR_TRANSFORMS_SIMPLIFY_ENDOMORPHISM_H_ |
| 10 | +#define MLIR_TRANSFORMS_SIMPLIFY_ENDOMORPHISM_H_ |
| 11 | + |
| 12 | +#include "mlir/IR/IRMapping.h" |
| 13 | +#include "mlir/IR/PatternMatch.h" |
| 14 | +#include "mlir/IR/Value.h" |
| 15 | +#include "mlir/Support/LLVM.h" |
| 16 | +#include "llvm/ADT/SmallVector.h" |
| 17 | +#include "llvm/Support/Casting.h" |
| 18 | +#include <iterator> |
| 19 | +#include <optional> |
| 20 | +#include <type_traits> |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +#include "mlir/Support/LogicalResult.h" |
| 24 | + |
| 25 | +namespace mlir { |
| 26 | + |
| 27 | +// If `f` is an endomorphism with respect to the algebraic structure induced by |
| 28 | +// function `g`, transforms `g(f(x1), f(x2) ..., f(xn))` into |
| 29 | +// `f(g(x1, x2, ..., xn))`. |
| 30 | +// `g` is the algebraic operation and `f` is the endomorphism. |
| 31 | +// |
| 32 | +// Functors: |
| 33 | +// --------- |
| 34 | +// `GetEndomorphismOpOperandFn`: `(Operation*) -> OpOperand*` |
| 35 | +// Returns the operand relevant to the endomorphism. |
| 36 | +// There may be other operands that are not relevant. |
| 37 | +// |
| 38 | +// `GetEndomorphismOpResultFn`: `(Operation*) -> OpResult` |
| 39 | +// Returns the result relevant to the endomorphism. |
| 40 | +// |
| 41 | +// `GetAlgebraicOpOperandsFn`: `(Operation*, SmallVector<OpOperand*>&) -> void` |
| 42 | +// Populates into the vector the operands relevant to the endomorphism. |
| 43 | +// |
| 44 | +// `GetAlgebraicOpResultFn`: `(Operation*) -> OpResult` |
| 45 | +// Return the result relevant to the endomorphism. |
| 46 | +// |
| 47 | +// `IsEndomorphismOpFn`: `(Operation*, std::optional<Operation*>) -> bool` |
| 48 | +// Check if the operation is an endomorphism of the required type. |
| 49 | +// Additionally if the optional is present checks if the operations are |
| 50 | +// compatible endomorphisms. |
| 51 | +// |
| 52 | +// `IsAlgebraicOpFn`: `(Operation*) -> bool` |
| 53 | +// Check if the operation is an operation of the algebraic structure. |
| 54 | +template <typename GetEndomorphismOpOperandFn, |
| 55 | + typename GetEndomorphismOpResultFn, typename GetAlgebraicOpOperandsFn, |
| 56 | + typename GetAlgebraicOpResultFn, typename IsEndomorphismOpFn, |
| 57 | + typename IsAlgebraicOpFn> |
| 58 | +struct EndomorphismSimplification : RewritePattern { |
| 59 | + template <typename GetEndomorphismOpOperandFnArg, |
| 60 | + typename GetEndomorphismOpResultFnArg, |
| 61 | + typename GetAlgebraicOpOperandsFnArg, |
| 62 | + typename GetAlgebraicOpResultFnArg, typename IsEndomorphismOpFnArg, |
| 63 | + typename IsAlgebraicOpFnArg, typename... RewritePatternArgs> |
| 64 | + EndomorphismSimplification( |
| 65 | + GetEndomorphismOpOperandFnArg &&getEndomorphismOpOperand, |
| 66 | + GetEndomorphismOpResultFnArg &&getEndomorphismOpResult, |
| 67 | + GetAlgebraicOpOperandsFnArg &&getAlgebraicOpOperands, |
| 68 | + GetAlgebraicOpResultFnArg &&getAlgebraicOpResult, |
| 69 | + IsEndomorphismOpFnArg &&isEndomorphismOp, |
| 70 | + IsAlgebraicOpFnArg &&isAlgebraicOp, RewritePatternArgs &&...args) |
| 71 | + : RewritePattern(std::forward<RewritePatternArgs>(args)...), |
| 72 | + getEndomorphismOpOperand(std::forward<GetEndomorphismOpOperandFnArg>( |
| 73 | + getEndomorphismOpOperand)), |
| 74 | + getEndomorphismOpResult(std::forward<GetEndomorphismOpResultFnArg>( |
| 75 | + getEndomorphismOpResult)), |
| 76 | + getAlgebraicOpOperands( |
| 77 | + std::forward<GetAlgebraicOpOperandsFnArg>(getAlgebraicOpOperands)), |
| 78 | + getAlgebraicOpResult( |
| 79 | + std::forward<GetAlgebraicOpResultFnArg>(getAlgebraicOpResult)), |
| 80 | + isEndomorphismOp(std::forward<IsEndomorphismOpFnArg>(isEndomorphismOp)), |
| 81 | + isAlgebraicOp(std::forward<IsAlgebraicOpFnArg>(isAlgebraicOp)) {} |
| 82 | + |
| 83 | + LogicalResult matchAndRewrite(Operation *op, |
| 84 | + PatternRewriter &rewriter) const override { |
| 85 | + if (failed(matchOp(op, algebraicOpOperands))) { |
| 86 | + return failure(); |
| 87 | + } |
| 88 | + return rewriteOp(op, algebraicOpOperands, rewriter); |
| 89 | + } |
| 90 | + |
| 91 | +private: |
| 92 | + LogicalResult matchOp(Operation *algebraicOp, |
| 93 | + SmallVector<OpOperand *> &algebraicOpOperands) const { |
| 94 | + if (!isAlgebraicOp(algebraicOp)) { |
| 95 | + return failure(); |
| 96 | + } |
| 97 | + algebraicOpOperands.clear(); |
| 98 | + getAlgebraicOpOperands(algebraicOp, algebraicOpOperands); |
| 99 | + if (algebraicOpOperands.empty()) { |
| 100 | + return failure(); |
| 101 | + } |
| 102 | + |
| 103 | + Operation *firstEndomorphismOp = |
| 104 | + algebraicOpOperands.front()->get().getDefiningOp(); |
| 105 | + if (!firstEndomorphismOp || |
| 106 | + !isEndomorphismOp(firstEndomorphismOp, std::nullopt)) { |
| 107 | + return failure(); |
| 108 | + } |
| 109 | + OpResult firstEndomorphismOpResult = |
| 110 | + getEndomorphismOpResult(firstEndomorphismOp); |
| 111 | + if (firstEndomorphismOpResult != algebraicOpOperands.front()->get()) { |
| 112 | + return failure(); |
| 113 | + } |
| 114 | + |
| 115 | + for (auto operand : algebraicOpOperands) { |
| 116 | + Operation *endomorphismOp = operand->get().getDefiningOp(); |
| 117 | + if (!endomorphismOp || |
| 118 | + !isEndomorphismOp(endomorphismOp, firstEndomorphismOp)) { |
| 119 | + return failure(); |
| 120 | + } |
| 121 | + } |
| 122 | + return success(); |
| 123 | + } |
| 124 | + |
| 125 | + LogicalResult rewriteOp(Operation *algebraicOp, |
| 126 | + const SmallVector<OpOperand *> &algebraicOpOperands, |
| 127 | + PatternRewriter &rewriter) const { |
| 128 | + irMapping.clear(); |
| 129 | + for (auto operand : algebraicOpOperands) { |
| 130 | + Operation *endomorphismOp = operand->get().getDefiningOp(); |
| 131 | + irMapping.map(operand->get(), |
| 132 | + getEndomorphismOpOperand(endomorphismOp)->get()); |
| 133 | + } |
| 134 | + Operation *newAlgebraicOp = rewriter.clone(*algebraicOp, irMapping); |
| 135 | + |
| 136 | + irMapping.clear(); |
| 137 | + assert(!algebraicOpOperands.empty()); |
| 138 | + Operation *firstEndomorphismOp = |
| 139 | + algebraicOpOperands[0]->get().getDefiningOp(); |
| 140 | + irMapping.map(getEndomorphismOpOperand(firstEndomorphismOp)->get(), |
| 141 | + getAlgebraicOpResult(newAlgebraicOp)); |
| 142 | + Operation *newEndomorphismOp = |
| 143 | + rewriter.clone(*firstEndomorphismOp, irMapping); |
| 144 | + rewriter.replaceAllUsesWith(getAlgebraicOpResult(algebraicOp), |
| 145 | + getEndomorphismOpResult(newEndomorphismOp)); |
| 146 | + return success(); |
| 147 | + } |
| 148 | + |
| 149 | + GetEndomorphismOpOperandFn getEndomorphismOpOperand; |
| 150 | + GetEndomorphismOpResultFn getEndomorphismOpResult; |
| 151 | + GetAlgebraicOpOperandsFn getAlgebraicOpOperands; |
| 152 | + GetAlgebraicOpResultFn getAlgebraicOpResult; |
| 153 | + IsEndomorphismOpFn isEndomorphismOp; |
| 154 | + IsAlgebraicOpFn isAlgebraicOp; |
| 155 | + mutable SmallVector<OpOperand *> algebraicOpOperands; |
| 156 | + mutable IRMapping irMapping; |
| 157 | +}; |
| 158 | + |
| 159 | +} // namespace mlir |
| 160 | + |
| 161 | +#endif // MLIR_TRANSFORMS_SIMPLIFY_ENDOMORPHISM_H_ |
0 commit comments