1 /* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 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 COMPILER_OPTIMIZER_PASS_MANAGER_H 17 #define COMPILER_OPTIMIZER_PASS_MANAGER_H 18 19 #include <tuple> 20 #include "compiler_options.h" 21 #include "pass.h" 22 #include "utils/bit_field.h" 23 #include "utils/arena_containers.h" 24 #include "pass_manager_statistics.h" 25 26 namespace ark::compiler { 27 class Graph; 28 class Pass; 29 class Analysis; 30 class CatchInputs; 31 class LivenessAnalyzer; 32 class LiveRegisters; 33 class LoopAnalyzer; 34 class AliasAnalysis; 35 class DominatorsTree; 36 class Rpo; 37 class LinearOrder; 38 class BoundsAnalysis; 39 class MonitorAnalysis; 40 // NOLINTNEXTLINE(fuchsia-multiple-inheritance) 41 class ObjectTypePropagation; 42 class RegAllocVerifier; 43 class TypesAnalysis; 44 45 namespace details { 46 template <typename... Types> 47 class PassTypeList { 48 public: 49 using IdentifierType = size_t; 50 using TupleType = std::tuple<std::decay_t<Types>...>; 51 52 template <typename T> HasType()53 static constexpr bool HasType() 54 { 55 return std::disjunction_v<std::is_same<T, Types>...>; 56 } 57 58 template <typename T, typename... Args> Instantiate(ArenaAllocator * allocator,Args &&...args)59 static ArenaVector<T> Instantiate(ArenaAllocator *allocator, Args &&...args) 60 { 61 ArenaVector<T> vec(allocator->Adapter()); 62 vec.reserve(sizeof...(Types)); 63 ((vec.push_back(allocator->New<Types>((std::forward<Args>(args))...))), ...); 64 return vec; 65 } 66 67 template <typename Type, std::size_t... INDEXES> GetIndex(std::index_sequence<INDEXES...>)68 static constexpr size_t GetIndex(std::index_sequence<INDEXES...> /* unused */) 69 { 70 static_assert(HasType<Type>()); 71 return (0 + ... + 72 (std::is_same_v<Type, std::tuple_element_t<INDEXES, TupleType>> ? size_t(INDEXES) : size_t {})); 73 } 74 75 template <typename Type> 76 static constexpr IdentifierType ID = GetIndex<std::decay_t<Type>>(std::index_sequence_for<Types...> {}); 77 static constexpr size_t SIZE = sizeof...(Types); 78 }; 79 80 using PredefinedAnalyses = 81 PassTypeList<LivenessAnalyzer, LoopAnalyzer, AliasAnalysis, DominatorsTree, Rpo, LinearOrder, BoundsAnalysis, 82 MonitorAnalysis, LiveRegisters, ObjectTypePropagation, RegAllocVerifier, TypesAnalysis, CatchInputs>; 83 } // namespace details 84 85 class PassManager { 86 public: 87 PassManager(Graph *graph, PassManager *parentPm); 88 89 ArenaAllocator *GetAllocator(); 90 ArenaAllocator *GetLocalAllocator(); 91 92 bool RunPass(Pass *pass, size_t localMemSizeBeforePass); 93 94 template <typename T, typename... Args> RunPass(Args...args)95 bool RunPass(Args... args) 96 { 97 auto localMemSizeBefore = GetLocalAllocator()->GetAllocatedSize(); 98 bool res = false; 99 // NOLINTNEXTLINE(readability-braces-around-statements) 100 if constexpr (details::PredefinedAnalyses::HasType<T>()) { 101 static_assert(sizeof...(Args) == 0); 102 res = RunPass(analyses_[details::PredefinedAnalyses::ID<T>], localMemSizeBefore); 103 // NOLINTNEXTLINE(readability-misleading-indentation) 104 } else { 105 T pass(graph_, std::forward<Args>(args)...); 106 res = RunPass(&pass, localMemSizeBefore); 107 } 108 if (!IsCheckMode()) { 109 if (g_options.IsCompilerResetLocalAllocator()) { 110 ASSERT(GetLocalAllocator() != GetAllocator()); 111 GetLocalAllocator()->Resize(localMemSizeBefore); 112 } 113 } 114 return res; 115 } 116 117 template <typename T> GetAnalysis()118 T &GetAnalysis() 119 { 120 static_assert(std::is_base_of_v<Analysis, std::decay_t<T>>); 121 return *static_cast<T *>(analyses_[details::PredefinedAnalyses::ID<T>]); 122 } 123 std::string GetFileName(const char *passName = nullptr, const std::string &suffix = ".cfg"); 124 void DumpGraph(const char *passName); 125 void DumpLifeIntervals(const char *passName); 126 void InitialDumpVisualizerGraph(); 127 void DumpVisualizerGraph(const char *passName); 128 void RunPassChecker(Pass *pass, bool result, bool isCodegen); 129 GetGraph()130 Graph *GetGraph() 131 { 132 return graph_; 133 } 134 135 void Finalize() const; 136 GetStatistics()137 PassManagerStatistics *GetStatistics() 138 { 139 return stats_; 140 } 141 SetCheckMode(bool v)142 void SetCheckMode(bool v) 143 { 144 checkMode_ = v; 145 } 146 IsCheckMode()147 bool IsCheckMode() const 148 { 149 return checkMode_; 150 } 151 152 // NOLINTNEXTLINE(readability-convert-member-functions-to-static) GetExecutionCounter()153 size_t GetExecutionCounter() const 154 { 155 return executionCounter_; 156 } 157 158 // NOLINTNEXTLINE(readability-convert-member-functions-to-static) StartExecution()159 void StartExecution() 160 { 161 executionCounter_++; 162 } 163 164 private: 165 Graph *graph_ {nullptr}; 166 ArenaVector<Optimization *> optimizations_; 167 const ArenaVector<Analysis *> analyses_; 168 169 PassManagerStatistics *stats_ {nullptr}; 170 inline static size_t executionCounter_ {0}; 171 172 // Whether passes are run by checker. 173 bool checkMode_ {false}; 174 175 bool firstExecution_ {true}; 176 }; 177 } // namespace ark::compiler 178 179 #endif // COMPILER_OPTIMIZER_PASS_MANAGER_H 180