//===- ToyCombine.cpp - Toy High Level Optimizer --------------------------===// // // 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 a set of simple combiners for optimizing operations in // the Toy dialect. // //===----------------------------------------------------------------------===// #include "mlir/IR/BuiltinAttributes.h" #include "mlir/IR/MLIRContext.h" #include "mlir/IR/OpDefinition.h" #include "mlir/IR/PatternMatch.h" #include "mlir/IR/Value.h" #include "toy/Dialect.h" #include "llvm/Support/Casting.h" #include <cstddef> usingnamespacemlir; usingnamespacetoy; namespace { /// Include the patterns defined in the Declarative Rewrite framework. #include "ToyCombine.inc" } // namespace /// Fold constants. OpFoldResult ConstantOp::fold(FoldAdaptor adaptor) { … } /// Fold struct constants. OpFoldResult StructConstantOp::fold(FoldAdaptor adaptor) { … } /// Fold simple struct access operations that access into a constant. OpFoldResult StructAccessOp::fold(FoldAdaptor adaptor) { … } /// This is an example of a c++ rewrite pattern for the TransposeOp. It /// optimizes the following scenario: transpose(transpose(x)) -> x struct SimplifyRedundantTranspose : public mlir::OpRewritePattern<TransposeOp> { … }; /// Register our patterns as "canonicalization" patterns on the TransposeOp so /// that they can be picked up by the Canonicalization framework. void TransposeOp::getCanonicalizationPatterns(RewritePatternSet &results, MLIRContext *context) { … } /// Register our patterns as "canonicalization" patterns on the ReshapeOp so /// that they can be picked up by the Canonicalization framework. void ReshapeOp::getCanonicalizationPatterns(RewritePatternSet &results, MLIRContext *context) { … }