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