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 
13 #include "mlir/Dialect/Tosa/IR/TosaOps.h"
14 #include "mlir/Dialect/Tosa/Transforms/PassDetail.h"
15 #include "mlir/Dialect/Tosa/Transforms/Passes.h"
16 #include "mlir/Pass/Pass.h"
17 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
18 
19 using namespace mlir;
20 using namespace mlir::tosa;
21 
22 namespace {
23 
24 struct TosaLayerwiseConstantFoldPass
25     : public TosaLayerwiseConstantFoldPassBase<TosaLayerwiseConstantFoldPass> {
26   void runOnOperation() override {
27     auto *ctx = &getContext();
28     RewritePatternSet patterns(ctx);
29     auto func = getOperation();
30 
31     mlir::tosa::populateTosaFoldConstantTransposePatterns(ctx, patterns);
32     mlir::tosa::populateTosaOpsCanonicalizationPatterns(ctx, patterns);
33 
34     if (applyPatternsAndFoldGreedily(func, std::move(patterns)).failed())
35       signalPassFailure();
36   }
37 };
38 
39 } // namespace
40 
41 std::unique_ptr<Pass> mlir::tosa::createTosaLayerwiseConstantFoldPass() {
42   return std::make_unique<TosaLayerwiseConstantFoldPass>();
43 }
44