MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
pass.cpp
Go to the documentation of this file.
1#include "mim/pass/pass.h"
2
3#include "mim/phase/phase.h"
4#include "mim/util/util.h"
5
6namespace mim {
7
8Pass::Pass(PassMan& man, std::string_view name)
9 : man_(man)
10 , name_(name)
11 , index_(man.passes().size()) {}
12
13void PassMan::push_state() {
14 if (fixed_point()) {
15 states_.emplace_back(passes().size());
16
17 // copy over from prev_state to curr_state
18 auto&& prev_state = states_[states_.size() - 2];
19 curr_state().curr_mut = prev_state.stack.top();
20 curr_state().stack = prev_state.stack;
21 curr_state().mut2visit = prev_state.mut2visit;
22 curr_state().old_ops.assign(curr_state().curr_mut->ops().begin(), curr_state().curr_mut->ops().end());
23
24 for (size_t i = 0; i != passes().size(); ++i) curr_state().data[i] = passes_[i]->copy(prev_state.data[i]);
25 }
26}
27
28void PassMan::pop_states(size_t undo) {
29 while (states_.size() != undo) {
30 for (size_t i = 0, e = curr_state().data.size(); i != e; ++i) passes_[i]->dealloc(curr_state().data[i]);
31
32 if (undo != 0) // only reset if not final cleanup
33 curr_state().curr_mut->reset(curr_state().old_ops);
34
35 states_.pop_back();
36 }
37}
38
40 world().verify().ILOG("run");
41
42 auto num = passes().size();
43 states_.emplace_back(num);
44 for (size_t i = 0; i != num; ++i) curr_state().data[i] = passes_[i]->alloc();
45
46 for (auto&& pass : passes_) world().ILOG(" + {}", pass->name());
47 world().debug_dump();
48
49 for (auto&& pass : passes_) pass->prepare();
50
51 for (auto mut : world().copy_externals()) {
52 analyzed(mut);
53 if (mut->is_set()) curr_state().stack.push(mut);
54 }
55
56 while (!curr_state().stack.empty()) {
57 push_state();
58 curr_mut_ = pop(curr_state().stack);
59 world().VLOG("=== state {}: {} ===", states_.size() - 1, curr_mut_);
60
61 if (!curr_mut_->is_set()) continue;
62
63 for (auto&& pass : passes_)
64 if (pass->inspect()) pass->enter();
65
66 curr_mut_->world().DLOG("curr_mut: {} : {}", curr_mut_, curr_mut_->type());
67 for (size_t i = 0, e = curr_mut_->num_ops(); i != e; ++i) curr_mut_->reset(i, rewrite(curr_mut_->op(i)));
68
69 world().VLOG("=== analyze ===");
70 proxy_ = false;
71 auto undo = No_Undo;
72 for (auto op : curr_mut_->deps()) undo = std::min(undo, analyze(op));
73
74 if (undo == No_Undo) {
75 assert(!proxy_ && "proxies must not occur anymore after leaving a mut with No_Undo");
76 world().DLOG("=== done ===");
77 } else {
78 pop_states(undo);
79 world().DLOG("=== undo: {} -> {} ===", undo, curr_state().stack.top());
80 }
81 }
82
83 world().verify().ILOG("finished");
84 pop_states(0);
85
86 world().debug_dump();
88}
89
90Ref PassMan::rewrite(Ref old_def) {
91 if (!old_def->has_dep()) return old_def;
92
93 if (auto mut = old_def->isa_mut()) {
94 curr_state().mut2visit.emplace(mut, curr_undo());
95 return map(mut, mut);
96 }
97
98 if (auto new_def = lookup(old_def)) {
99 if (old_def == *new_def)
100 return old_def;
101 else
102 return map(old_def, rewrite(*new_def));
103 }
104
105 auto new_type = old_def->type() ? rewrite(old_def->type()) : nullptr;
106 auto new_ops = absl::FixedArray<const Def*>(old_def->num_ops());
107 for (size_t i = 0, e = old_def->num_ops(); i != e; ++i) new_ops[i] = rewrite(old_def->op(i));
108 auto new_def = old_def->rebuild(new_type, new_ops);
109
110 if (auto proxy = new_def->isa<Proxy>()) {
111 if (auto&& pass = passes_[proxy->pass()]; pass->inspect()) {
112 if (auto rw = pass->rewrite(proxy); rw != proxy) return map(old_def, rewrite(rw));
113 }
114 } else {
115 for (auto&& pass : passes_) {
116 if (!pass->inspect()) continue;
117
118 if (auto var = new_def->isa<Var>()) {
119 if (auto rw = pass->rewrite(var); rw != var) return map(old_def, rewrite(rw));
120 } else {
121 if (auto rw = pass->rewrite(new_def); rw != new_def) return map(old_def, rewrite(rw));
122 }
123 }
124 }
125
126 return map(old_def, new_def);
127}
128
129undo_t PassMan::analyze(Ref def) {
130 undo_t undo = No_Undo;
131
132 if (!def->has_dep() || analyzed(def)) {
133 // do nothing
134 } else if (auto mut = def->isa_mut()) {
135 if (mut->is_set()) curr_state().stack.push(mut);
136 } else if (auto proxy = def->isa<Proxy>()) {
137 proxy_ = true;
138 undo = passes_[proxy->pass()]->analyze(proxy);
139 } else {
140 auto var = def->isa<Var>();
141 if (!var)
142 for (auto op : def->deps()) undo = std::min(undo, analyze(op));
143
144 for (auto&& pass : passes_)
145 if (pass->inspect()) undo = std::min(undo, var ? pass->analyze(var) : pass->analyze(def));
146 }
147
148 return undo;
149}
150
151} // namespace mim
bool is_set() const
Yields true if empty or the last op is set.
Definition def.cpp:281
Defs deps() const
Definition def.cpp:410
Ref op(size_t i) const noexcept
Definition def.h:264
World & world() const noexcept
Definition def.cpp:384
constexpr auto ops() const noexcept
Definition def.h:261
T * isa_mut() const
If this is *mut*able, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:432
Def * reset(size_t i, Ref def)
Successively reset from left to right.
Definition def.h:286
Ref type() const noexcept
Yields the raw type of this Def, i.e. maybe nullptr.
Definition def.h:241
bool has_dep() const
Definition def.h:313
Ref rebuild(World &w, Ref type, Defs ops) const
Def::rebuilds this Def while using new_op as substitute for its i'th Def::op.
Definition def.h:494
constexpr size_t num_ops() const noexcept
Definition def.h:265
An optimizer that combines several optimizations in an optimal way.
Definition pass.h:107
Def * curr_mut() const
Definition pass.h:117
void run()
Run all registered passes on the whole World.
Definition pass.cpp:39
World & world() const
Definition pass.h:114
const auto & passes() const
Definition pass.h:115
bool fixed_point() const
Definition pass.h:116
Pass(PassMan &, std::string_view name)
Definition pass.cpp:8
virtual void run()
Entry point and generates some debug output; invokes Phase::start.
Definition phase.cpp:5
Helper class to retrieve Infer::arg if present.
Definition def.h:86
World & verify()
Verifies that all externals() and annexes() are Def::is_closed(), if MIM_ENABLE_CHECKS.
Definition world.cpp:563
void debug_dump()
Dump in Debug build if World::log::level is Log::Level::Debug.
Definition dump.cpp:488
Definition ast.h:14
auto pop(S &s) -> decltype(s.top(), typename S::value_type())
Definition util.h:80
size_t undo_t
Definition pass.h:14
static constexpr undo_t No_Undo
Definition pass.h:15