//===- EraseUnusedOperandsAndResults.cpp ----------------------------------===// // // 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/Transforms/Transforms.h" #include "mlir/Dialect/Linalg/IR/Linalg.h" usingnamespacemlir; usingnamespacemlir::linalg; /// Return `true` if the `result` of an operation `genericOp` is dead. static bool isResultValueDead(linalg::GenericOp genericOp, OpResult result) { … } namespace { struct DeduplicateAndRemoveDeadOperandsAndResults : public OpRewritePattern<GenericOp> { … }; /// Remove unused cycles. /// We can remove unused cycle within a payload of generic region /// if these conditions are met: /// - Result from out operand is dead. /// - Block arg from out operand has a single use in the %cycle /// instruction. /// - Cycle has a single use and it is in yield. struct RemoveUnusedCycleInGenericOp : public OpRewritePattern<GenericOp> { … }; /// Fold uses of duplicate inputs in the body of a linalg.generic. E.g.: /// ``` /// linalg.generic ins(%a, %b, %a, %b) outs(%a) /// ^bb0(%in0, %in1, %in2, %in3, %out1) /// ``` /// Assuming that all %a and %b have the same index map: /// * All uses of %in0 and %in2 are replaced with %out1 /// * All uses of %in1 are replaced with %in3 /// This pattern can enable additional canonicalizations: In the above example, /// %in0, %in1 and %in3 have no uses anymore and their corresponding operands /// can be folded away. This pattern does not modify uses of output block args. struct FoldDuplicateInputBbArgs : public OpRewritePattern<GenericOp> { … }; } // namespace void mlir::linalg::populateEraseUnusedOperandsAndResultsPatterns( RewritePatternSet &patterns) { … } void mlir::linalg::populateEraseUnnecessaryInputsPatterns( RewritePatternSet &patterns) { … }