xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/transforms/tac_pass.h (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 #ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_TAC_PASS_H_
16 #define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_TAC_PASS_H_
17 
18 #include <string>
19 
20 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
21 #include "mlir/Pass/Pass.h"  // from @llvm-project
22 #include "tensorflow/compiler/mlir/lite/experimental/tac/hardwares/target_hardware.h"
23 #include "tensorflow/compiler/mlir/lite/experimental/tac/tac_module.h"
24 
25 namespace mlir {
26 namespace TFL {
27 namespace tac {
28 // An OperationPass<> with access to the TAC module instance that the
29 // pass is running part of.
30 // See OperationPass<> comments for all details/restrictions of OperationPass.
31 //
32 // When adding new Pass to TAC, users should use this class as the base class
33 // as it provides access to the TAC module.
34 template <typename T>
35 class TacPass : public OperationPass<T> {
36  public:
37   using OperationPass<T>::OperationPass;
TacPass(const TacModule * module)38   explicit TacPass(const TacModule* module)
39       : OperationPass<T>::OperationPass(mlir::TypeID::get<T>()),
40         module_(module) {}
41 
~TacPass()42   ~TacPass() override {}
43 
GetTargetHardware(const std::string & hardware_name)44   const TargetHardware* GetTargetHardware(const std::string& hardware_name) {
45     return module_ != nullptr
46                ? module_->GetTargetHardware(hardware_name)
47                : mlir::TFL::tac::GetTargetHardware(hardware_name);
48   }
49 
50  protected:
51   const TacModule* module_ = nullptr;  // Not owned.
52 };
53 
54 // A FunctionPass but with access to TAC module.
55 // See FunctionPass comments for all details/restrictions of FunctionPass.
56 //
57 // When adding new Pass to TAC, users should use this class as the base class
58 // as it provides access to the TAC module.
59 template <typename T>
60 class TacFunctionPass : public TacPass<func::FuncOp> {
61  public:
62   using TacPass<func::FuncOp>::TacPass;
63 
~TacFunctionPass()64   ~TacFunctionPass() override {}
65 
getFunction()66   mlir::func::FuncOp getFunction() { return getOperation(); }
67 
68   virtual void runOnFunction() = 0;
69 
runOnOperation()70   void runOnOperation() final {
71     if (!getFunction().isExternal()) runOnFunction();
72   }
73 
74  protected:
75   // Returns the derived pass name.
getName()76   StringRef getName() const override { return llvm::getTypeName<T>(); }
77 
78   // A clone method to create a copy of this pass.
clonePass()79   std::unique_ptr<Pass> clonePass() const override {
80     return std::make_unique<T>(*static_cast<const T*>(this));
81   }
82 };
83 
84 }  // namespace tac
85 }  // namespace TFL
86 }  // namespace mlir
87 
88 #endif  // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_TRANSFORMS_TAC_PASS_H_
89