//===- ConstantFold.cpp - Implementation of constant folding on Linalg ops ===// // // 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 // //===----------------------------------------------------------------------===// // // This file implements constant folding on Linalg operations. // //===----------------------------------------------------------------------===// #include "mlir/Dialect/Affine/IR/AffineOps.h" #include "mlir/Dialect/Linalg/IR/Linalg.h" #include "mlir/Dialect/Linalg/Transforms/Transforms.h" #include "mlir/IR/Matchers.h" #include "mlir/IR/PatternMatch.h" #include "mlir/Support/LLVM.h" #include "mlir/Transforms/GreedyPatternRewriteDriver.h" #include <optional> usingnamespacemlir; usingnamespacemlir::linalg; namespace { /// Base class for constant folding linalg structured ops with N inputs, 1 /// output, and permutation indexing maps. /// /// `ConcreteType` should provide methods with signatures /// /// ```c++ /// bool matchIndexingMaps(LinalgOp linalgOp) const; /// RegionComputationFn getRegionComputeFn(LinalgOp) const; /// ``` /// /// The latter inspects the region and returns the computation inside as a /// functor. The functor will be invoked with constant elements for all inputs /// and should return the corresponding computed constant element for output. template <typename ConcreteType> class FoldConstantBase : public OpInterfaceRewritePattern<LinalgOp> { … }; // Folds linalg.transpose (and linalg.generic ops that are actually transposes) // on constant values. struct FoldConstantTranspose : public FoldConstantBase<FoldConstantTranspose> { … }; } // namespace void mlir::linalg::populateConstantFoldLinalgOperations( RewritePatternSet &patterns, const ControlFusionFn &controlFn) { … }