//===- ElementwiseToLinalg.cpp - conversion of elementwise to linalg ------===// // // 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 // //===----------------------------------------------------------------------===// #include "mlir/Dialect/Linalg/Passes.h" #include "mlir/Dialect/Arith/Utils/Utils.h" #include "mlir/Dialect/Linalg/IR/Linalg.h" #include "mlir/Dialect/Linalg/Transforms/Transforms.h" #include "mlir/Dialect/Linalg/Utils/Utils.h" #include "mlir/Transforms/DialectConversion.h" namespace mlir { #define GEN_PASS_DEF_CONVERTELEMENTWISETOLINALGPASS #include "mlir/Dialect/Linalg/Passes.h.inc" } // namespace mlir usingnamespacemlir; static bool isElementwiseMappableOpOnRankedTensors(Operation *op) { … } /// Given `op` assumed `isElementwiseMappableOpOnRankedTensors`, iterate over /// the result types and return a list of values such that, for each result type /// `t` and value `v` at the same index `idx`: /// 1. `v.getType() == t` /// 2. If an operand of `op` has type `t`, let `operand_first` be the first /// such operand. Then`v == operand_first`. /// 3. Otherwise, v is a newly created `tensor::EmptyOp` with: /// a. Static and dynamic dims extracted from the first operand of `op`. /// b. Elemental type equal to the elemental type of `t`. /// /// This is sufficient because ElementwiseMappable guarantees that "The static /// types of all vector (resp. tensor) operands and results must have the same /// shape". static SmallVector<Value, 4> getOrCreateOperandsMatchingResultTypes(OpBuilder &b, Operation *op) { … } namespace { struct ConvertAnyElementwiseMappableOpOnRankedTensors : public RewritePattern { … }; } // namespace void mlir::linalg::populateElementwiseToLinalgConversionPatterns( RewritePatternSet &patterns) { … } namespace { class ConvertElementwiseToLinalgPass : public impl::ConvertElementwiseToLinalgPassBase< ConvertElementwiseToLinalgPass> { … }; } // namespace