xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tosa/transforms/legalize_tf_tfl.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 // Legalize TensorFlow and TensorFlow Lite to TOSA
17 
18 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
19 #include "mlir/Dialect/Tosa/IR/TosaOps.h"  // from @llvm-project
20 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
21 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
22 #include "mlir/IR/PatternMatch.h"  // from @llvm-project
23 #include "mlir/Support/LLVM.h"  // from @llvm-project
24 #include "mlir/Transforms/DialectConversion.h"  // from @llvm-project
25 #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h"
26 #include "tensorflow/compiler/mlir/tosa/transforms/legalize_common.h"
27 #include "tensorflow/compiler/mlir/tosa/transforms/legalize_utils.h"
28 #include "tensorflow/compiler/mlir/tosa/transforms/passes.h"
29 
30 namespace mlir {
31 namespace tosa {
32 
33 namespace {
34 
35 // Performs lowering to TOSA dialect
36 class LegalizeTFTFL : public TosaLegalizeTFTFLPassBase<LegalizeTFTFL> {
37  public:
LegalizeTFTFL()38   explicit LegalizeTFTFL() {}
39   void runOnOperation() override;
40 };
41 
runOnOperation()42 void LegalizeTFTFL::runOnOperation() {
43   MLIRContext* ctx = &getContext();
44   RewritePatternSet patterns(ctx);
45   populateLegalizeTFPatterns(ctx, patterns);
46   populateLegalizeTFLPatterns(ctx, patterns);
47 
48   func::FuncOp func = getOperation();
49   if (ApplyPatternsWithShapeResolution(func, std::move(patterns)).failed()) {
50     signalPassFailure();
51   }
52 }
53 
54 }  // anonymous namespace
55 
56 // Creates an instance of the TensorFlow Lite dialect LegalizeTFL pass.
createLegalizeTFTFLPass()57 std::unique_ptr<OperationPass<func::FuncOp>> createLegalizeTFTFLPass() {
58   return std::make_unique<LegalizeTFTFL>();
59 }
60 
61 }  // namespace tosa
62 }  // namespace mlir
63