1 /* Copyright 2019 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 #ifndef TENSORFLOW_COMPILER_MLIR_TENSORFLOW_ANALYSIS_PER_FUNCTION_AGGREGATE_ANALYSIS_H_
17 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_ANALYSIS_PER_FUNCTION_AGGREGATE_ANALYSIS_H_
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <memory>
22 
23 #include "llvm/ADT/DenseMap.h"
24 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
25 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
26 #include "mlir/Pass/Pass.h"  // from @llvm-project
27 
28 namespace mlir {
29 namespace TF {
30 namespace detail {
31 
32 // This template defines an aggregate analysis base class, which analyzes a
33 // module but the analysis info is stored per function.
34 template <typename InfoT>
35 class PerFunctionAggregateAnalysis {
36  public:
37   using Info = InfoT;
38 
39   // Returns the analysis info for the given function.
GetAnalysisForFunc(func::FuncOp func)40   const Info& GetAnalysisForFunc(func::FuncOp func) const {
41     auto it = info_map_.find(func);
42     assert(it != info_map_.end());
43     return it->second;
44   }
45 
46  protected:
47   // Since `InfoT` might be large, DenseMap is used instead of SmallDenseMap to
48   // avoid stack overflow.
49   llvm::DenseMap<func::FuncOp, InfoT> info_map_;
50 };
51 
52 }  // namespace detail
53 
54 // Base CRTP class to help write passes that are consumes a per-function
55 // aggregate analysis and operate on all non-extern functions (similar to a
56 // OperationPass<func::FuncOp>, but with no concurrency between functions). The
57 // derived classes need to provide a runOnFunction() method that accepts the
58 // function and the analysis information for that function.
59 template <typename DerivedT, typename AnalysisT>
60 class PerFunctionAggregateAnalysisConsumerPass
61     : public PassWrapper<
62           PerFunctionAggregateAnalysisConsumerPass<DerivedT, AnalysisT>,
63           OperationPass<ModuleOp>> {
64  public:
resolveTypeID()65   static ::mlir::TypeID resolveTypeID() {
66     static ::mlir::SelfOwningTypeID id;
67     return id;
68   }
69 
70  private:
runOnOperation()71   void runOnOperation() override {
72     ModuleOp op = this->getOperation();
73     DerivedT& derived = *static_cast<DerivedT*>(this);
74     auto& analysis = this->template getAnalysis<AnalysisT>();
75 
76     for (auto func : op.getOps<func::FuncOp>())
77       if (!func.isExternal())
78         derived.runOnFunction(func, analysis.GetAnalysisForFunc(func));
79   }
80 };
81 
82 }  // namespace TF
83 }  // namespace mlir
84 
85 #endif  // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_ANALYSIS_PER_FUNCTION_AGGREGATE_ANALYSIS_H_
86