|
| 1 | +//===- AnalysisManager.h - Analysis Management Infrastructure ---*- C++ -*-===// |
| 2 | +// |
| 3 | +// Copyright 2019 The MLIR Authors. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +// ============================================================================= |
| 17 | + |
| 18 | +#ifndef MLIR_PASS_ANALYSISMANAGER_H |
| 19 | +#define MLIR_PASS_ANALYSISMANAGER_H |
| 20 | + |
| 21 | +#include "mlir/IR/Module.h" |
| 22 | +#include "mlir/Support/LLVM.h" |
| 23 | +#include "llvm/ADT/DenseMap.h" |
| 24 | +#include "llvm/ADT/SmallPtrSet.h" |
| 25 | + |
| 26 | +namespace mlir { |
| 27 | +/// A special type used by analyses to provide an address that identifies a |
| 28 | +/// particular analysis set or a concrete analysis type. |
| 29 | +struct AnalysisID { |
| 30 | + template <typename AnalysisT> static AnalysisID *getID() { |
| 31 | + static AnalysisID id; |
| 32 | + return &id; |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +//===----------------------------------------------------------------------===// |
| 37 | +// Analysis Preservation and Result Modeling |
| 38 | +//===----------------------------------------------------------------------===// |
| 39 | + |
| 40 | +namespace detail { |
| 41 | +/// A utility class to represent the analyses that are known to be preserved. |
| 42 | +class PreservedAnalyses { |
| 43 | +public: |
| 44 | + /// Mark all analyses as preserved. |
| 45 | + void preserveAll() { preservedIDs.insert(&allAnalysesID); } |
| 46 | + |
| 47 | + /// Returns if all analyses were marked preserved. |
| 48 | + bool isAll() const { return preservedIDs.count(&allAnalysesID); } |
| 49 | + |
| 50 | +private: |
| 51 | + /// An identifier used to represent all potential analyses. |
| 52 | + constexpr static AnalysisID allAnalysesID = {}; |
| 53 | + |
| 54 | + /// The set of analyses that are known to be preserved. |
| 55 | + SmallPtrSet<const void *, 2> preservedIDs; |
| 56 | +}; |
| 57 | + |
| 58 | +/// The abstract polymorphic base class representing an analysis. |
| 59 | +struct AnalysisConcept { |
| 60 | + virtual ~AnalysisConcept() = default; |
| 61 | +}; |
| 62 | + |
| 63 | +/// A derived analysis model used to hold a specific analysis object. |
| 64 | +template <typename AnalysisT> struct AnalysisModel : public AnalysisConcept { |
| 65 | + template <typename... Args> |
| 66 | + explicit AnalysisModel(Args &&... args) |
| 67 | + : analysis(std::forward<Args>(args)...) {} |
| 68 | + |
| 69 | + AnalysisT analysis; |
| 70 | +}; |
| 71 | + |
| 72 | +/// This class represents a cache of analysis results for a single IR unit. All |
| 73 | +/// computation, caching, and invalidation of analyses takes place here. |
| 74 | +template <typename IRUnitT> class AnalysisResultMap { |
| 75 | + /// A mapping between an analysis id and an existing analysis instance. |
| 76 | + using ResultMap = |
| 77 | + DenseMap<const AnalysisID *, std::unique_ptr<AnalysisConcept>>; |
| 78 | + |
| 79 | +public: |
| 80 | + explicit AnalysisResultMap(IRUnitT *ir) : ir(ir) {} |
| 81 | + |
| 82 | + /// Get an analysis for the current IR unit, computing it if necessary. |
| 83 | + template <typename AnalysisT> AnalysisT &getResult() { |
| 84 | + typename ResultMap::iterator it; |
| 85 | + bool wasInserted; |
| 86 | + std::tie(it, wasInserted) = |
| 87 | + results.try_emplace(AnalysisID::getID<AnalysisT>()); |
| 88 | + |
| 89 | + // If we don't have a cached result for this function, compute it directly |
| 90 | + // and add it to the cache. |
| 91 | + if (wasInserted) |
| 92 | + it->second = llvm::make_unique<AnalysisModel<AnalysisT>>(ir); |
| 93 | + return static_cast<AnalysisModel<AnalysisT> &>(*it->second).analysis; |
| 94 | + } |
| 95 | + |
| 96 | + /// Get a cached analysis instance if one exists, otherwise return null. |
| 97 | + template <typename AnalysisT> |
| 98 | + llvm::Optional<std::reference_wrapper<AnalysisT>> getCachedResult() const { |
| 99 | + auto res = results.find(AnalysisID::getID<AnalysisT>()); |
| 100 | + if (res == results.end()) |
| 101 | + return llvm::None; |
| 102 | + return {static_cast<AnalysisModel<AnalysisT> &>(*res->second).analysis}; |
| 103 | + } |
| 104 | + |
| 105 | + /// Returns the IR unit that this result map represents. |
| 106 | + IRUnitT *getIRUnit() { return ir; } |
| 107 | + const IRUnitT *getIRUnit() const { return ir; } |
| 108 | + |
| 109 | + /// Clear any held analysis results. |
| 110 | + void clear() { results.clear(); } |
| 111 | + |
| 112 | + /// Invalidate any cached analyses based upon the given set of preserved |
| 113 | + /// analyses. |
| 114 | + void invalidate(const detail::PreservedAnalyses &pa) { |
| 115 | + // If all analyses were preserved, then there is nothing to do here. |
| 116 | + if (pa.isAll()) |
| 117 | + return; |
| 118 | + // TODO: Fine grain invalidation of analyses. |
| 119 | + clear(); |
| 120 | + } |
| 121 | + |
| 122 | +private: |
| 123 | + IRUnitT *ir; |
| 124 | + ResultMap results; |
| 125 | +}; |
| 126 | + |
| 127 | +} // namespace detail |
| 128 | + |
| 129 | +//===----------------------------------------------------------------------===// |
| 130 | +// Analysis Management |
| 131 | +//===----------------------------------------------------------------------===// |
| 132 | + |
| 133 | +/// An analysis manager for a specific function instance. This class can only be |
| 134 | +/// constructed from a ModuleAnalysisManager instance. |
| 135 | +class FunctionAnalysisManager { |
| 136 | +public: |
| 137 | + // Query for a cached analysis on the parent Module. The analysis may not |
| 138 | + // exist and if it does it may be stale. |
| 139 | + template <typename AnalysisT> |
| 140 | + llvm::Optional<std::reference_wrapper<AnalysisT>> |
| 141 | + getCachedModuleResult() const { |
| 142 | + return parentImpl->getCachedResult<AnalysisT>(); |
| 143 | + } |
| 144 | + |
| 145 | + // Query for the given analysis for the current function. |
| 146 | + template <typename AnalysisT> AnalysisT &getResult() { |
| 147 | + return impl->getResult<AnalysisT>(); |
| 148 | + } |
| 149 | + |
| 150 | + // Query for a cached entry of the given analysis on the current function. |
| 151 | + template <typename AnalysisT> |
| 152 | + llvm::Optional<std::reference_wrapper<AnalysisT>> getCachedResult() const { |
| 153 | + return impl->getCachedResult<AnalysisT>(); |
| 154 | + } |
| 155 | + |
| 156 | + /// Invalidate any non preserved analyses, |
| 157 | + void invalidate(const detail::PreservedAnalyses &pa) { impl->invalidate(pa); } |
| 158 | + |
| 159 | + /// Clear any held analyses. |
| 160 | + void clear() { impl->clear(); } |
| 161 | + |
| 162 | +private: |
| 163 | + FunctionAnalysisManager(const detail::AnalysisResultMap<Module> *parentImpl, |
| 164 | + detail::AnalysisResultMap<Function> *impl) |
| 165 | + : parentImpl(parentImpl), impl(impl) {} |
| 166 | + |
| 167 | + /// A reference to the results map of the parent module within the owning |
| 168 | + /// analysis manager. |
| 169 | + const detail::AnalysisResultMap<Module> *parentImpl; |
| 170 | + |
| 171 | + /// A reference to the results map within the owning analysis manager. |
| 172 | + detail::AnalysisResultMap<Function> *impl; |
| 173 | + |
| 174 | + /// Allow access to the constructor. |
| 175 | + friend class ModuleAnalysisManager; |
| 176 | +}; |
| 177 | + |
| 178 | +/// An analysis manager for a specific module instance. |
| 179 | +class ModuleAnalysisManager { |
| 180 | +public: |
| 181 | + ModuleAnalysisManager(Module *module) : moduleAnalyses(module) {} |
| 182 | + ModuleAnalysisManager(const ModuleAnalysisManager &) = delete; |
| 183 | + ModuleAnalysisManager &operator=(const ModuleAnalysisManager &) = delete; |
| 184 | + |
| 185 | + /// Query for the analysis of a function. The analysis is computed if it does |
| 186 | + /// not exist. |
| 187 | + template <typename AnalysisT> |
| 188 | + AnalysisT &getFunctionResult(Function *function) { |
| 189 | + return slice(function).getResult<AnalysisT>(); |
| 190 | + } |
| 191 | + |
| 192 | + /// Query for the analysis of a module. The analysis is computed if it does |
| 193 | + /// not exist. |
| 194 | + template <typename AnalysisT> AnalysisT &getResult() { |
| 195 | + return moduleAnalyses.getResult<AnalysisT>(); |
| 196 | + } |
| 197 | + |
| 198 | + /// Query for a cached analysis for the module, or return nullptr. |
| 199 | + template <typename AnalysisT> |
| 200 | + llvm::Optional<std::reference_wrapper<AnalysisT>> getCachedResult() const { |
| 201 | + return moduleAnalyses.getCachedResult<AnalysisT>(); |
| 202 | + } |
| 203 | + |
| 204 | + /// Create an analysis slice for the given child function. |
| 205 | + FunctionAnalysisManager slice(Function *function); |
| 206 | + |
| 207 | + /// Invalidate any non preserved analyses. |
| 208 | + void invalidate(const detail::PreservedAnalyses &pa); |
| 209 | + |
| 210 | +private: |
| 211 | + /// The cached analyses for functions within the current module. |
| 212 | + DenseMap<Function *, detail::AnalysisResultMap<Function>> functionAnalyses; |
| 213 | + |
| 214 | + /// The analyses for the owning module. |
| 215 | + detail::AnalysisResultMap<Module> moduleAnalyses; |
| 216 | +}; |
| 217 | + |
| 218 | +} // end namespace mlir |
| 219 | + |
| 220 | +#endif // MLIR_PASS_ANALYSISMANAGER_H |
0 commit comments