MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
nest.cpp
Go to the documentation of this file.
1#include "mim/nest.h"
2
3#include "mim/world.h"
4
5namespace mim {
6
8 : world_(r->world())
9 , root_(make_node(r)) {
10 populate();
11}
12
14 : world_(muts.front()->world())
15 , root_(make_node(nullptr)) {
16 for (auto mut : muts)
17 make_node(mut, root_);
18 populate();
19}
20
22 : world_(world)
23 , root_(make_node(nullptr)) {
24 world.for_each(false, [this](Def* mut) { make_node(mut, root_); });
25 populate();
26}
27
28void Nest::populate() {
29 std::queue<Node*> queue;
30
31 if (root()->mut())
32 queue.push(root_);
33 else
34 for (auto [_, child] : root_->child_mut2node_)
35 queue.push(child);
36
37 while (!queue.empty()) {
38 auto curr_node = pop(queue);
39 for (auto op : curr_node->mut()->deps()) {
40 for (auto local_mut : op->local_muts()) {
41 if (mut2node(local_mut) || !contains(local_mut)) continue;
42
43 if (curr_node->level() < local_mut->free_vars().size()) {
44 for (auto node = curr_node;; node = node->parent_) {
45 if (auto var = node->mut()->has_var()) {
46 if (local_mut->free_vars().contains(var)) {
47 queue.push(make_node(local_mut, node));
48 break;
49 }
50 }
51 }
52 } else {
53 uint32_t max = 0;
54 auto parent = root_;
55 for (auto var : local_mut->free_vars()) {
56 if (auto node = mut2node_nonconst(var->mut()); node && node->level() > max) {
57 max = node->level();
58 parent = node;
59 }
60 }
61 queue.push(make_node(local_mut, parent));
62 }
63 }
64 }
65 }
66}
67
68Nest::Node* Nest::make_node(Def* mut, Node* parent) {
69 auto node = std::unique_ptr<Node>(new Node(*this, mut, parent)); // can't use make_unique - c'tor is private
70 auto res = node.get();
71 mut2node_.emplace(mut, std::move(node));
72 if (mut) {
73 if (auto var = mut->has_var()) vars_ = world().vars().insert(vars_, var);
74 }
75 return res;
76}
77
78const Nest::Node* Nest::lca(const Node* n, const Node* m) {
79 while (n->level() < m->level())
80 m = m->parent();
81 while (m->level() < n->level())
82 n = n->parent();
83 while (n != m) {
84 // TODO support longer dep chains and and the possibility to opt out from this
85 if (n->deps().depends_.contains(m)) return n;
86 if (m->deps().depends_.contains(n)) return m;
87 n = n->parent();
88 m = m->parent();
89 }
90 return n;
91}
92
93void Nest::deps(Node* curr) const {
94 if (curr->mut()) {
95 for (auto op : curr->mut()->deps()) {
96 for (auto local_mut : op->local_muts()) {
97 if (auto local_node = mut2node_nonconst(local_mut)) {
98 if (local_node == curr)
99 local_node->link(local_node);
100 else if (auto parent = local_node->parent()) {
101 if (auto curr_child = parent->curr_child) {
102 assert(parent->child_mut2node_.contains(curr_child->mut()));
103 curr_child->link(local_node);
104 }
105 }
106 }
107 }
108 }
109 }
110
111 for (auto [_, child] : curr->child_mut2node_) {
112 curr->curr_child = child;
113 deps(child);
114 curr->curr_child = nullptr;
115 }
116}
117
118void Nest::find_SCCs(Node* curr) const {
119 curr->find_SCCs();
120 for (auto [_, child] : curr->child_mut2node_) {
121 child->loop_depth_ = child->is_recursive() ? curr->loop_depth() + 1 : curr->loop_depth();
122 find_SCCs(child);
123 }
124}
125
126void Nest::Node::find_SCCs() {
127 Stack stack;
128 for (int i = 0; auto& [_, node] : child_mut2node_)
129 if (node->idx_ == Unvisited) i = node->tarjan(i, this, stack);
130}
131
132uint32_t Nest::Node::tarjan(uint32_t i, Node* parent, Stack& stack) {
133 this->idx_ = this->low_ = i++;
134 this->on_stack_ = true;
135 stack.emplace(this);
136
137 for (auto dep : this->depends_) {
138 if (dep->idx_ == Unvisited) i = dep->tarjan(i, parent, stack);
139 if (dep->on_stack_) this->low_ = std::min(this->low_, dep->low_);
140 }
141
142 if (this->idx_ == this->low_) {
143 parent_->topo_.emplace_front(std::make_unique<SCC>());
144 SCC* scc = parent_->topo_.front().get();
145 Node* node;
146 int num = 0;
147 do {
148 node = pop(stack);
149 node->on_stack_ = false;
150 node->recursive_ = true;
151 node->low_ = this->idx_;
152 ++num;
153
154 scc->emplace(node);
155 auto [_, ins] = parent_->SCCs_.emplace(node, scc);
156 assert_unused(ins);
157 } while (node != this);
158
159 if (num == 1 && !this->depends_.contains(this)) this->recursive_ = false;
160 }
161
162 return i;
163}
164
165} // namespace mim
Base class for all Defs.
Definition def.h:251
Muts local_muts() const
Mutables reachable by following immutable deps(); mut->local_muts() is by definition the set { mut }...
Definition def.cpp:329
uint32_t level() const
Definition nest.h:26
const Node * parent() const
Definition nest.h:19
World & world() const
Definition nest.h:128
static const Node * lca(const Node *n, const Node *m)
Least common ancestor of n and m.
Definition nest.cpp:78
auto muts() const
Definition nest.h:137
const Node * root() const
Definition nest.h:129
Nest(Def *root)
Definition nest.cpp:7
const auto & mut2node() const
Definition nest.h:141
bool contains(const Def *def) const
Definition nest.h:131
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
auto & vars()
Definition world.h:554
Definition ast.h:14
auto pop(S &s) -> decltype(s.top(), typename S::value_type())
Definition util.h:83
Span< const T, N > View
Definition span.h:98
Node
Definition def.h:112