docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
analysis.h
Go to the documentation of this file.
1#pragma once
2
3#include "sdfg/options.h"
6
7namespace sdfg {
8namespace analysis {
9
10class AnalysisManager;
11
12class Analysis {
13 friend class AnalysisManager;
14
15protected:
18 // Options this analysis was constructed for (non-owning).
20
21 virtual void run(analysis::AnalysisManager& analysis_manager) = 0;
22
23 template<class T>
24 T option(const OptionKey<T>& key, T fallback = T{}) const {
25 return options_->get(key, fallback);
26 }
27
28public:
30 // Opt-in: an analysis declaring this constructor is built for a set of options.
32
33 virtual ~Analysis() = default;
34
35 virtual std::string name() const = 0;
36
46 return false;
47 }
48
49 Analysis(const Analysis& a) = delete;
50 Analysis& operator=(const Analysis&) = delete;
51};
52
54private:
55 StructuredSDFG& sdfg_;
56 symbolic::Assumptions additional_assumptions_;
57 const Options* options_ = &Options::empty();
58
59 std::unordered_map<std::type_index, std::unique_ptr<Analysis>> cache_;
60
61 static constexpr const char* STATS_SCOPE = "AnalysisMgr";
62
63public:
66 AnalysisManager(StructuredSDFG& sdfg, const symbolic::Assumptions& additional_assumptions);
67 AnalysisManager(StructuredSDFG& sdfg, const symbolic::Assumptions& additional_assumptions, const Options& options);
68
69 AnalysisManager(const AnalysisManager& am) = delete;
71
72 // Options visible to every analysis (analyses read via option()/options()).
73 const Options& options() const {
74 return *options_;
75 }
76
77 template<class T>
78 T option(const OptionKey<T>& key) const {
79 return options_->get(key);
80 }
81
82 template<class T>
83 T& get() {
84 std::type_index type = std::type_index(typeid(T));
85
86 // Check cache
87 auto it = cache_.find(type);
88 if (it != cache_.end()) {
89 return *static_cast<T*>(it->second.get());
90 }
91
92 // Run a new analysis; forward options to analyses that opt in via constructor.
93 if constexpr (std::is_constructible_v<T, StructuredSDFG&, const Options&>) {
94 cache_[type] = std::make_unique<T>(this->sdfg_, *this->options_);
95 } else {
96 cache_[type] = std::make_unique<T>(this->sdfg_);
97 }
98 cache_[type]->additional_assumptions_ = this->additional_assumptions_;
99
100 passes::CompileStatistics* stats = nullptr;
103 stats->enter_scope(cache_[type]->name(), STATS_SCOPE);
104 }
105
106 cache_[type]->run(*this);
107
108 if (stats) {
109 stats->exit_scope();
110 }
111
112 return *static_cast<T*>(cache_[type].get());
113 }
114
115 template<class T>
116 void invalidate() {
117 std::type_index type = std::type_index(typeid(T));
118 if (cache_.find(type) != cache_.end()) {
119 cache_.erase(type);
120 }
121 }
122
123 // Preserve only the listed analyses and invalidate all others.
124 // Analyses not present in the cache are unaffected.
125 template<class... Ts>
126 void preserve() {
127 std::unordered_map<std::type_index, std::unique_ptr<Analysis>> kept;
128 auto try_keep = [&](std::type_index type) {
129 auto it = cache_.find(type);
130 if (it != cache_.end()) {
131 kept.emplace(type, std::move(it->second));
132 }
133 };
134 (try_keep(std::type_index(typeid(Ts))), ...);
135 cache_.clear();
136 for (auto& [type, analysis] : kept) {
137 cache_.emplace(type, std::move(analysis));
138 }
139 }
140
142
149};
150
151} // namespace analysis
152} // namespace sdfg
Definition options.h:56
T get(const OptionKey< T > &key, T fallback=T{}) const
Definition options.h:72
static const Options & empty()
Definition options.h:84
Definition structured_sdfg.h:30
Definition analysis.h:53
AnalysisManager & operator=(const AnalysisManager &)=delete
void invalidate_all(structured_control_flow::ControlFlowNode &changed)
Invalidates the analyses after a change confined to the subtree of changed; see Analysis::invalidate.
AnalysisManager(StructuredSDFG &sdfg, const Options &options)
AnalysisManager(StructuredSDFG &sdfg, const symbolic::Assumptions &additional_assumptions)
AnalysisManager(StructuredSDFG &sdfg, const symbolic::Assumptions &additional_assumptions, const Options &options)
void preserve()
Definition analysis.h:126
T option(const OptionKey< T > &key) const
Definition analysis.h:78
void invalidate()
Definition analysis.h:116
T & get()
Definition analysis.h:83
AnalysisManager(StructuredSDFG &sdfg)
AnalysisManager(const AnalysisManager &am)=delete
const Options & options() const
Definition analysis.h:73
Analysis & operator=(const Analysis &)=delete
symbolic::Assumptions additional_assumptions_
Definition analysis.h:17
Analysis(const Analysis &a)=delete
const Options * options_
Definition analysis.h:19
virtual std::string name() const =0
StructuredSDFG & sdfg_
Definition analysis.h:16
virtual ~Analysis()=default
Analysis(StructuredSDFG &sdfg)
virtual void run(analysis::AnalysisManager &analysis_manager)=0
Analysis(StructuredSDFG &sdfg, const Options &options)
T option(const OptionKey< T > &key, T fallback=T{}) const
Definition analysis.h:24
virtual bool invalidate(structured_control_flow::ControlFlowNode &changed)
Updates the analysis after a change confined to the subtree of changed.
Definition analysis.h:45
friend class AnalysisManager
Definition analysis.h:13
Definition statistics.h:73
static bool enabled()
Definition statistics.h:200
static CompileStatistics & instance()
Definition statistics.h:92
Node * enter_scope(const std::string &name, const std::string &scope_type)
Base class for all structured control flow nodes in a StructuredSDFG.
Definition control_flow_node.h:44
Map from symbols to their assumptions.
Definition assumptions.h:313
Definition analysis.h:8
Definition analysis.h:7
Definition options.h:40