MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
schedule.cpp
Go to the documentation of this file.
2
3namespace mim::plug::mach {
4
5void Schedule::place(Lam* curr, const Def* def) {
6 if (def->isa_mut()) return;
7
8 auto insn = is_insn(def);
9 auto remat = is_remat(def);
10 if (!remat && !(insn ? placed_ : visited_[curr]).emplace(def).second) return;
11 if (target_.is_mem(def)) mems_[scheduler_->early(def)->mut()->as<Lam>()].emplace_back(def);
12 if (auto node = insn && !remat ? scheduler_->smart(curr, def) : nullptr) curr = node->mut()->as<Lam>();
13
14 auto ops = DefVec(def->ops().begin(), def->ops().end());
15 // A terminator branches on its condition last.
16 if (auto app = def->isa<App>(); app && def == curr->body()) ops = {app->arg(), app->callee()};
17 for (auto op : ops)
18 if (!is_remat(op)) place(curr, op);
19 // Last, so that nothing separates a rematerialized operand from its user.
20 for (auto op : ops)
21 if (is_remat(op)) place(curr, op);
22 if (insn) insns_[curr].emplace_back(def);
23}
24
26 auto scheduler = Scheduler(nest);
27 scheduler_ = &scheduler;
28 reset();
29 placed_.clear(), threaded_.clear(), visited_.clear(), insns_.clear(), mems_.clear();
30
31 for (auto mut : nest.muts())
32 if (auto lam = mut->isa<Lam>(); lam && lam->is_set()) place(lam, lam->body());
33 rewrite(root());
34}
35
36const Def* Schedule::rewrite(const Def* old_def) {
37 if (auto mut = old_def->isa_mut()) {
38 if (auto lam = mut->isa<Lam>(); lam && lam->is_set() && nest()[lam] && threaded_.emplace(lam).second)
39 thread(lam);
40 return mut;
41 }
42 return Rewriter::rewrite(old_def);
43}
44
45const Def* Schedule::rewrite_imm_App(const App* app) {
46 // thread() maps every instruction it places, so this one escaped all schedules.
47 if (is_insn(app))
48 app->blame("{} backend: instruction `{}` is not on the `mem.M` chain of a continuation", target_.name(), app)
49 .bail();
50 return Rewriter::rewrite_imm_App(app);
51}
52
53void Schedule::thread(Lam* lam) {
54 auto n = lam->num_vars();
55 auto i = n;
56 for (size_t j = 0; j != n && i == n; ++j)
57 if (target_.is_mem(lam->var(n, j))) i = j;
58
59 // Direct style has no chain, so an instruction in there runs into rewrite_imm_App.
60 if (i == n && Lam::isa_cn(lam))
61 lam->blame("{} backend: continuation `{}` has no `mem.M` to schedule on", target_.name(), lam).bail();
62 if (i != n && !Lam::isa_cn(lam))
63 lam->blame("{} backend: direct-style function `{}` takes a `mem.M`, which only a continuation can schedule on",
64 target_.name(), lam)
65 .bail();
66 if (i != n && !lam->body()->isa<App>())
67 lam->blame("{} backend: the body of continuation `{}` is no jump: `{}`", target_.name(), lam, lam->body())
68 .bail();
69
70 auto _ = scoped(Vars(lam->has_var()), Vars(lam->has_var()));
71 if (i != n) chain(lam, i);
72 auto filter = rewrite(lam->filter());
73 auto body = rewrite(lam->body());
74 lam->unset()->set(filter, body);
75}
76
77void Schedule::chain(Lam* lam, size_t i) {
78 auto& w = world();
79 auto n = lam->num_vars();
80 auto m = lam->var(n, i);
81 auto order = std::move(insns_[lam]);
82 auto mems = std::move(mems_[lam]);
83
84 // Instructions hoisted into lam come after the condition of its branch, so the branch gets a fresh one at the end.
85 if (auto ex = lam->body()->as<App>()->callee()->isa<Extract>(); ex && is_remat(ex->index())) {
86 auto cc = ex->index()->as<App>();
87 if (is_remat(cc->arg())) order.emplace_back(cc->arg());
88 order.emplace_back(cc);
89 }
90
91 for (auto old_def : order) {
92 auto app = old_def->as<App>();
93 if (Axm::isa<mach::sched>(app)) {
94 auto res = w.call<mach::sched>(rewrite(Target::sched_insn(app)), m, rewrite(app->arg()));
95 m = res->proj(2, 0);
96 map(old_def, res);
97 } else if (target_.prev_mem(app)) {
98 auto old_arg = app->arg();
99 auto k = old_arg->num_projs();
100 auto args = DefVec(k, [&](size_t j) {
101 auto a = old_arg->proj(k, j);
102 return target_.is_mem(a) ? m : rewrite(a);
103 });
104 auto new_app = w.app(rewrite(app->callee()), w.tuple(args));
105 if (target_.is_mem(new_app)) {
106 m = new_app;
107 } else {
108 m = new_app->proj(2, 0);
109 map(old_def, new_app);
110 }
111 } else {
112 auto res = w.call<mach::sched>(rewrite(app->callee()), m, rewrite(app->arg()));
113 m = res->proj(2, 0);
114 map(old_def, res->proj(2, 1));
115 }
116 }
117
118 for (auto old_mem : mems)
119 map(old_mem, m);
120 // The whole Var may be passed on, so it must carry the end of the chain as well.
121 if (n > 1) map(lam->var(), w.tuple(DefVec(n, [&](size_t j) { return j == i ? m : lam->var(n, j); })));
122}
123
124} // namespace mim::plug::mach
const Def * callee() const
Definition lam.h:237
static auto isa(const Def *def)
Definition axm.h:112
M * root() const
Definition phase.h:581
Base class for all Defs.
Definition def.h:285
bool is_set() const
Definition def.h:392
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:584
const Def * var(nat_t a, nat_t i) noexcept
Definition def.h:488
nat_t num_vars() noexcept
Definition def.h:488
fe::Error & blame(fe::cite_string< Args... > s, Args &&... args) const
Reports an error that blames this; chain Error::n for Notes and Error::bail to throw.
Definition def.h:329
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
Definition def.h:492
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:360
A function.
Definition lam.h:91
Lam * unset()
Definition lam.h:166
const Def * filter() const
Definition lam.h:103
static const Lam * isa_cn(const Def *d)
Definition lam.h:125
Lam * set(Filter filter, const Def *body)
Definition lam.cpp:34
const Def * body() const
Definition lam.h:104
Builds a nesting tree for all mutables/binders.
Definition nest.h:26
auto muts() const
Definition nest.h:206
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition phase.cpp:23
World & world()
Definition rewrite.h:33
const Def * map(const Def *old_def, const Def *new_def)
Definition rewrite.cpp:29
void reset()
Definition rewrite.cpp:23
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:62
virtual const Def * rewrite_imm_App(const App *)
Pop scoped(Vars old_vars={}, Vars new_vars={})
push()es a scope that lives as long as the returned Pop.
Definition rewrite.h:62
Scheduler()=default
std::string_view name() const
The plugin name, used as the prefix of diagnostics.
Definition target.h:18
void visit(const Nest &) final
Definition schedule.cpp:25
const Def * rewrite(const Def *) final
Definition schedule.cpp:36
const Def * rewrite_imm_App(const App *) final
Definition schedule.cpp:45
static bool is_mem(const Def *def)
Definition target.h:205
const Def * prev_mem(const App *app) const
The memory app continues, if app is an instruction on a mem.M 0 chain.
Definition target.h:210
static const Def * sched_insn(const App *app)
Definition target.h:204
The mach Plugin
Definition asm.h:15
fe::Vector< const Def * > DefVec
Definition def.h:96
fe::Patricia< const Var, DefKey >::Set Vars
Definition def.h:113
@ Lam
Definition def.h:123