3#include <unordered_set>
37 virtual bool update(
const T& incoming) = 0;
61template<
typename T,
typename I>
89 bool any_changes =
false;
90 for (
auto it = incoming.begin(); it != incoming.end(); ++it) {
91 auto [placed, changed] =
incoming_.insert(*it);
92 any_changes |= changed;
94 return any_changes || !
ran_;
98 bool any_changes =
false;
101 any_changes |= changed;
121 for (
auto it = merge_into.begin(); it != merge_into.end();) {
122 auto key = it->first;
123 if (!other.contains(key)) {
124 it = merge_into.erase(it);
130 merge_into.insert(other.begin(), other.end());
164template<
typename State>
204 return *states_.at(&node);
208 return states_.contains(&node);
213 auto& ptr = states_[&node];
232 return State::empty_in();
236 std::unordered_map<const structured_control_flow::ControlFlowNode*, std::unique_ptr<State>> states_;
252 return solve(*seq, in);
255 return solve(*if_else, in);
258 return solve_loop(*sloop, in);
261 return solve(*while_loop, in);
266 bool changed =
s.update(in);
267 return {changed,
s.forward_exposed()};
272 std::pair<bool, const ExposedType&> solve(structured_control_flow::Sequence& seq,
const ExposedType& in) {
274 seq_state.update_incoming(in);
277 bool updating =
true;
278 for (
size_t i = 0; i < seq.size() && updating; ++i) {
279 auto& child = seq.at(i);
280 auto [changed, output] = solve(child, *current);
286 auto changed = seq_state.update_forward_exposed(*current);
287 return {changed, *current};
289 return {
false, seq_state.forward_exposed()};
295 std::pair<bool, const ExposedType&> solve(structured_control_flow::IfElse& if_else,
const ExposedType& in) {
297 state.update_incoming(in);
302 for (
size_t i = 0; i < if_else.size(); ++i) {
303 auto [branch_seq, cond] = if_else.at(i);
304 auto [branch_changed, branch_out] = solve(branch_seq, in);
305 changed |= branch_changed;
312 if (!if_else.is_complete()) {
317 changed =
state.update_forward_exposed(merged);
319 return {changed,
state.forward_exposed()};
324 std::pair<bool, const ExposedType&> solve_loop(structured_control_flow::StructuredLoop& loop,
const ExposedType& in) {
332 std::pair<bool, const ExposedType&> solve_loop_body(
334 structured_control_flow::Sequence& body,
338 state.update_incoming(in);
341 bool changing =
false;
342 bool changed =
false;
344 auto [body_changed, body_out] = solve(body, current);
345 changing = body_changed;
346 changed |= body_changed;
352 if (apply_after_body) {
353 apply_after_body(current);
358 changed =
state.update_forward_exposed(current);
361 return {changed,
state.forward_exposed()};
366 std::pair<bool, const ExposedType&> solve(structured_control_flow::While& loop,
const ExposedType& in) {
BLAS (Basic Linear Algebra Subprograms) library node definitions.
Base class for forward data-flow analysis over structured control flow.
Definition structured_data_flow_analysis.h:165
virtual std::unique_ptr< State > create_initial_state(const structured_control_flow::ControlFlowNode &node)=0
State & get_or_create_state(const structured_control_flow::ControlFlowNode &node)
Definition structured_data_flow_analysis.h:212
bool has_state(const structured_control_flow::ControlFlowNode &node) const
Definition structured_data_flow_analysis.h:207
virtual ExposedType boundary()
Provide the initial set flowing into the root.
Definition structured_data_flow_analysis.h:231
virtual ~ForwardStructuredDataFlowAnalysis()=default
void run_forward(structured_control_flow::Sequence &root)
Run the analysis starting from the given root sequence.
Definition structured_data_flow_analysis.h:190
typename State::ExposedType ExposedType
Definition structured_data_flow_analysis.h:169
const State & state(const structured_control_flow::ControlFlowNode &node) const
Retrieve the computed data-flow state for a node.
Definition structured_data_flow_analysis.h:203
A state in a Stateful DataFlow Graph (SDFG).
Definition state.h:38
Base class for all structured control flow nodes in a StructuredSDFG.
Definition control_flow_node.h:44
Represents a conditional if-else control flow node with multiple cases.
Definition if_else.h:47
A sequential container of control flow nodes.
Definition sequence.h:52
Base class for structured loop constructs.
Definition structured_loop.h:244
Represents a while-loop with condition-controlled iteration.
Definition while.h:37
sdfg::ElementId ElementId
Definition structured_data_flow_analysis.h:59
MergeMode
Definition structured_data_flow_analysis.h:19
@ CUMULATIVE
Definition structured_data_flow_analysis.h:20
@ BRANCHES
Definition structured_data_flow_analysis.h:21
@ s
Definition blas_node.h:55
@ State
Definition element.h:73
element_cast_target_t< T > * dyn_cast(Element *element)
Efficient down-cast to T, or nullptr if element is not a T.
Definition element.h:246
size_t ElementId
Definition element.h:215
Definition structured_data_flow_analysis.h:25
virtual bool ran_at_least_once() const =0
virtual bool update(const T &incoming)=0
All-in-one for leaf-nodes.
virtual const T & forward_exposed() const =0
Get the current state of live elements leaving the node in forward direction.
virtual ~DataFlowState()=default
virtual bool update_forward_exposed(const T &forward_exposed)=0
For non-leaf nodes: update outputs.
virtual bool update_incoming(const T &incoming)=0
For non-leaf nodes: update inputs.
T ExposedType
Definition structured_data_flow_analysis.h:26
Definition structured_data_flow_analysis.h:62
ExposedType forward_exposed_
Definition structured_data_flow_analysis.h:67
ExposedType incoming_
Definition structured_data_flow_analysis.h:66
static void merge(ExposedType &merge_into, const ExposedType &other, MergeMode mode=MergeMode::CUMULATIVE)
Definition structured_data_flow_analysis.h:119
bool ran_at_least_once() const override
Definition structured_data_flow_analysis.h:71
bool update_forward_exposed(const ExposedType &forward_exposed) override
For non-leaf nodes: update outputs.
Definition structured_data_flow_analysis.h:97
bool update_incoming(const ExposedType &incoming) override
For non-leaf nodes: update inputs.
Definition structured_data_flow_analysis.h:88
static ExposedType empty_in()
Definition structured_data_flow_analysis.h:115
virtual void apply_kills_and_changes(ExposedType &exposed) const =0
bool update(const ExposedType &incoming) override
All-in-one for leaf-nodes.
Definition structured_data_flow_analysis.h:75
DataFlowState< std::unordered_map< ElementId, T > >::ExposedType ExposedType
Definition structured_data_flow_analysis.h:63
bool ran_
Definition structured_data_flow_analysis.h:68
const ExposedType & forward_exposed() const override
Get the current state of live elements leaving the node in forward direction.
Definition structured_data_flow_analysis.h:111
Definition structured_data_flow_analysis.h:175
std::variant< structured_control_flow::ControlFlowNode *, structured_control_flow::StructuredLoop *, structured_control_flow::While *, structured_control_flow::IfElse *, structured_control_flow::Sequence * > node
Definition structured_data_flow_analysis.h:182
int index
Definition structured_data_flow_analysis.h:183