MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
sink_args.cpp
Go to the documentation of this file.
2
3#include <mim/lam.h>
4
5#include "mim/plug/mem/mem.h"
6
7namespace mim::plug::mem::phase {
8
9std::array<const Def*, 2> SinkArgs::rewrite_filter_body(Lam* old_lam, Lam* new_lam) {
10 if (auto dispatch = Dispatch(old_lam->body())) {
11 auto n = dispatch.num_args();
12 auto sieve = Sieve(n, [&](size_t i) { return isa_mem(dispatch.arg(n, i)); });
13 if (!sieve.all()) {
14 auto filter = rewrite(old_lam->filter());
15 auto body = sink(dispatch, sieve);
16 return {filter, body};
17 }
18 }
19 return RWPhase::rewrite_filter_body(old_lam, new_lam);
20}
21
22const Def* SinkArgs::sink(const Dispatch& dispatch, const Sieve& sieve) {
23 auto& w = new_world();
24 auto n = sieve.num_old();
25 auto m = sieve.num_new();
26 auto args = DefVec(n, [&](size_t i) { return rewrite(dispatch.arg(n, i)); });
27 auto mems = sieve.gather(args);
28 auto dom = DefVec(m, [&](size_t j) { return mems[j]->type(); });
29 auto ops = [&](Lam* lam) {
30 return DefVec(n, [&](size_t i) {
31 auto j = sieve[i];
32 return j == Sieve::Gone ? args[i] : lam->var(m, j);
33 });
34 };
35
36 auto targets = DefVec(dispatch.num_targets(), [&](size_t t) -> const Def* {
37 auto target = dispatch.target(t);
38 if (auto lam = target->isa_mut<Lam>(); lam && lam->is_set()) {
39 if (auto stub = lookup(lam)) return stub; // the same target again
40 auto stub = w.mut_con(dom)->set(lam->dbg());
41 map(lam, stub);
42 auto vs = ops(stub);
43 for (size_t i = 0; i != n; ++i)
44 map(lam->var(n, i), vs[i]);
45 if (n > 1) map(lam->var(), w.tuple(vs));
46 return stub->set(rewrite_filter_body(lam, stub));
47 }
48 auto wrapper = w.mut_con(dom)->set(target->dbg());
49 return wrapper->app(false, rewrite(target), w.tuple(ops(wrapper)));
50 });
51
52 profile_count("sink-args");
53 invalidate();
54 return w.app(w.extract(w.tuple(targets), rewrite(dispatch.index())), w.tuple(mems));
55}
56
57} // namespace mim::plug::mem::phase
Base class for all Defs.
Definition def.h:285
Matches (target_0, target_1, ...)#index arg.
Definition tuple.h:467
const Def * arg() const
Definition tuple.cpp:111
Dispatch(const Def *)
Definition tuple.cpp:105
A function.
Definition lam.h:91
const Def * filter() const
Definition lam.h:103
const Def * body() const
Definition lam.h:104
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition phase.cpp:23
World & new_world()
Create new Defs into this.
Definition phase.h:418
virtual std::array< const Def *, 2 > rewrite_filter_body(Lam *old_lam, Lam *)
The rewritten filter and body of old_lam for its stub new_lam, before the body is coerce()d.
Definition phase.h:434
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:62
Filters the components of an aggregate and keeps track of where the survivors end up.
Definition tuple.h:116
Sieve(size_t num)
Definition tuple.h:123
DefVec gather(Defs ops) const
The surviving components of ops - one per candidate, concat'ed ones included.
Definition tuple.h:177
size_t num_new() const
Number of survivors.
Definition tuple.h:163
size_t num_old() const
Number of old components.
Definition tuple.h:162
static constexpr size_t Gone
Definition tuple.h:118
size_t num_targets() const
Definition tuple.h:418
const Def * index() const
Definition tuple.h:416
std::array< const Def *, 2 > rewrite_filter_body(Lam *, Lam *) final
The rewritten filter and body of old_lam for its stub new_lam, before the body is coerce()d.
Definition sink_args.cpp:9
const App * isa_mem(const Def *def)
If def is a core.M-typed value, yields its memory type core.M a; otherwise nullptr.
Definition core.h:148
fe::Vector< const Def * > DefVec
Definition def.h:96