1 /* Copyright 2020 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 #include "tensorflow/compiler/mlir/tosa/tf_passes.h"
17
18 #include "mlir/Dialect/Tosa/Transforms/Passes.h" // from @llvm-project
19 #include "mlir/Pass/PassRegistry.h" // from @llvm-project
20 #include "mlir/Transforms/Passes.h" // from @llvm-project
21 #include "tensorflow/compiler/mlir/tosa/transforms/passes.h"
22
23 namespace mlir {
24 namespace tosa {
25
createTFtoTOSALegalizationPipeline(OpPassManager & pm,const TOSATFLegalizationPipelineOptions & opts)26 void createTFtoTOSALegalizationPipeline(
27 OpPassManager& pm, const TOSATFLegalizationPipelineOptions& opts) {
28 //----------------------------------------------------------------------------
29 // Prepare TFL module for conversion
30 //----------------------------------------------------------------------------
31 // Inline all functions into main and then delete the functions themselves.
32 pm.addPass(mlir::createInlinerPass());
33
34 // Now that there is only one function, run some MLIR passes on it.
35 pm.addPass(mlir::createCanonicalizerPass());
36 pm.addPass(mlir::createCSEPass());
37
38 pm.addPass(mlir::createLoopFusionPass());
39 pm.addPass(mlir::createMemRefDataFlowOptPass());
40
41 //----------------------------------------------------------------------------
42 // Perform main conversion.
43 // Now that there is only one function, run some MLIR passes on it.
44 //----------------------------------------------------------------------------
45 pm.addPass(mlir::tosa::createFuseBiasTFPass());
46 pm.addPass(mlir::tosa::createLegalizeTFPass());
47
48 //----------------------------------------------------------------------------
49 // Post conversion cleanup.
50 //----------------------------------------------------------------------------
51 pm.addPass(mlir::tosa::createTosaMakeBroadcastablePass());
52 // Inline the call/return basic blocks within TOSA control flow ops.
53 pm.addPass(mlir::createInlinerPass());
54 // Clean up with DCE.
55 pm.addPass(mlir::createSymbolDCEPass());
56 }
57
58 static mlir::PassPipelineRegistration<TOSATFLegalizationPipelineOptions>
59 tf_tosa_pipeline("tf-to-tosa-pipeline",
60 "TensorFlow to TOSA legalization pipeline",
61 createTFtoTOSALegalizationPipeline);
62
63 } // namespace tosa
64 } // namespace mlir
65