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/tfl_passes.h"
17
18 #include "mlir/Dialect/Affine/Passes.h" // from @llvm-project
19 #include "mlir/Dialect/Tosa/Transforms/Passes.h" // from @llvm-project
20 #include "mlir/Transforms/Passes.h" // from @llvm-project
21 #include "tensorflow/compiler/mlir/lite/transforms/passes.h"
22 #include "tensorflow/compiler/mlir/tosa/transforms/passes.h"
23
24 namespace mlir {
25 namespace tosa {
26
createTFLtoTOSALegalizationPipeline(OpPassManager & pm,const TOSATFLLegalizationPipelineOptions & opts)27 void createTFLtoTOSALegalizationPipeline(
28 OpPassManager& pm, const TOSATFLLegalizationPipelineOptions& opts) {
29 //----------------------------------------------------------------------------
30 // Prepare TFL module for conversion
31 //----------------------------------------------------------------------------
32 // Inline all functions into main and then delete the functions themselves.
33 pm.addPass(mlir::createInlinerPass());
34
35 // Add pass to decompose TFLite mixed quantization to non-quantized variants.
36 pm.addPass(TFL::CreateDecomposeHybridQuantizationPass());
37
38 // Now that there is only one function, run some MLIR passes on it.
39 pm.addPass(mlir::createCanonicalizerPass());
40 pm.addPass(mlir::createCSEPass());
41
42 pm.addPass(mlir::createLoopFusionPass());
43 pm.addPass(mlir::createAffineScalarReplacementPass());
44
45 //----------------------------------------------------------------------------
46 // Perform main conversion.
47 //----------------------------------------------------------------------------
48 pm.addPass(mlir::tosa::createConvertTFLUint8Pass());
49 if (opts.dequantize_tfl_softmax) {
50 pm.addPass(mlir::tosa::createDequantizeTFLSoftmaxPass());
51 }
52 pm.addPass(mlir::tosa::createLegalizeTFLPass(opts.disabled_patterns,
53 opts.enabled_patterns));
54
55 //----------------------------------------------------------------------------
56 // Post conversion cleanup.
57 //----------------------------------------------------------------------------
58 pm.addPass(mlir::tosa::createTosaInferShapesPass());
59 pm.addPass(mlir::tosa::createTosaMakeBroadcastablePass());
60 // Inline the call/return basic blocks within TOSA control flow ops.
61 pm.addPass(mlir::createInlinerPass());
62 // Clean up with DCE.
63 pm.addPass(mlir::createSymbolDCEPass());
64 }
65
registerTFLtoTOSALegalizationPipeline()66 void registerTFLtoTOSALegalizationPipeline() {
67 mlir::PassPipelineRegistration<TOSATFLLegalizationPipelineOptions>(
68 "tfl-to-tosa-pipeline", "TensorFlow Lite to TOSA legalization pipeline",
69 createTFLtoTOSALegalizationPipeline);
70 }
71
72 } // namespace tosa
73 } // namespace mlir
74