MLIR  21.0.0git
TosaLayerwiseConstantFoldPass.cpp
Go to the documentation of this file.
1 //===- TosaLayerwiseConstantFoldPass.cpp ----------------------------------===//
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 // This file implements constant folding transformations on TOSA operations
10 //
11 //===----------------------------------------------------------------------===//
12 
14 
17 #include "mlir/Pass/Pass.h"
19 
20 namespace mlir {
21 namespace tosa {
22 #define GEN_PASS_DEF_TOSALAYERWISECONSTANTFOLDPASS
23 #include "mlir/Dialect/Tosa/Transforms/Passes.h.inc"
24 } // namespace tosa
25 } // namespace mlir
26 
27 using namespace mlir;
28 using namespace mlir::tosa;
29 
30 namespace {
31 
32 template <typename... Args>
33 void addOpsCanonicalizations(MLIRContext *ctx, RewritePatternSet &patterns) {
34  (Args::getCanonicalizationPatterns(patterns, ctx), ...);
35 }
36 
37 void populateTosaOpsCanonicalizationPatterns(MLIRContext *ctx,
39  addOpsCanonicalizations<
40 #define GET_OP_LIST
41 #include "mlir/Dialect/Tosa/IR/TosaOps.cpp.inc"
42  >(ctx, patterns);
43 }
44 
45 struct TosaLayerwiseConstantFoldPass
46  : public tosa::impl::TosaLayerwiseConstantFoldPassBase<
47  TosaLayerwiseConstantFoldPass> {
48  using Base::Base;
49 
50  void runOnOperation() override {
51  auto *ctx = &getContext();
53  auto func = getOperation();
54 
58  aggressiveReduceConstant);
59  populateTosaOpsCanonicalizationPatterns(ctx, patterns);
60 
61  if (applyPatternsGreedily(func, std::move(patterns)).failed())
62  signalPassFailure();
63  }
64 };
65 
66 } // namespace
static MLIRContext * getContext(OpFoldResult val)
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
void populateTosaConstantReduction(MLIRContext *ctx, RewritePatternSet &patterns, bool aggressiveReduceConstant)
void populateTosaFoldConstantReciprocalPatterns(MLIRContext *ctx, RewritePatternSet &patterns)
void populateTosaFoldConstantTransposePatterns(MLIRContext *ctx, RewritePatternSet &patterns)
Include the generated interface declarations.
LogicalResult applyPatternsGreedily(Region &region, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
const FrozenRewritePatternSet & patterns