MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
clos2sjlj.cpp
Go to the documentation of this file.
2
4
5namespace mim::plug::clos {
6
7namespace {
8
9std::array<Ref, 3> split(Ref def) {
10 auto new_ops = DefVec(def->num_projs() - 2, nullptr);
11 auto& w = def->world();
12 const Def *mem, *env;
13 auto j = 0;
14 for (size_t i = 0; i < def->num_projs(); i++) {
15 auto op = def->proj(i);
16 if (op == w.annex<mem::M>() || op->type() == w.annex<mem::M>())
17 mem = op;
18 else if (i == Clos_Env_Param)
19 env = op;
20 else
21 new_ops[j++] = op;
22 }
23 assert(mem && env);
24 auto remaining = def->is_term() ? w.tuple(new_ops) : w.sigma(new_ops);
25 if (new_ops.size() == 1 && remaining != new_ops[0]) {
26 // FIXME: For some reason this is not constant folded away??
27 remaining = new_ops[0];
28 }
29 return {mem, env, remaining};
30}
31
32Ref rebuild(Ref mem, Ref env, Defs remaining) {
33 auto& w = mem->world();
34 auto new_ops = DefVec(remaining.size() + 2, [&](auto i) -> const Def* {
35 static_assert(Clos_Env_Param == 1);
36 if (i == 0) return mem;
37 if (i == 1) return env;
38 return remaining[i - 2];
39 });
40 return w.tuple(new_ops);
41}
42
43} // namespace
44
45void Clos2SJLJ::get_exn_closures(Ref def, DefSet& visited) {
46 if (!def->is_term() || def->isa_mut<Lam>() || visited.contains(def)) return;
47 visited.emplace(def);
48 if (auto c = isa_clos_lit(def)) {
49 auto lam = c.fnc_as_lam();
50 if (c.is_basicblock() && !ignore_.contains(lam)) {
51 def->world().DLOG("FOUND exn closure: {}", c.fnc_as_lam());
52 lam2tag_[c.fnc_as_lam()] = {lam2tag_.size() + 1, c.env()};
53 }
54 get_exn_closures(c.env(), visited);
55 } else {
56 for (auto op : def->ops()) get_exn_closures(op, visited);
57 }
58}
59
60void Clos2SJLJ::get_exn_closures() {
61 lam2tag_.clear();
62 if (!curr_mut()->is_set() || !Lam::isa_cn(curr_mut())) return;
63 auto app = curr_mut()->body()->isa<App>();
64 if (!app) return;
65 if (auto p = app->callee()->isa<Extract>(); p && isa_clos_type(p->tuple()->type())) {
66 auto p2 = p->tuple()->isa<Extract>();
67 if (p2 && p2->tuple()->isa<Tuple>()) {
68 // branch: Check the closure environments, but be careful not to traverse
69 // the closures themselves
70 auto branches = p2->tuple()->ops();
71 for (auto b : branches) {
72 auto c = isa_clos_lit(b);
73 if (c) {
74 ignore_.emplace(c.fnc_as_lam());
75 world().DLOG("IGNORE {}", c.fnc_as_lam());
76 }
77 }
78 }
79 }
80 auto visited = DefSet();
81 get_exn_closures(app->arg(), visited);
82}
83
84Lam* Clos2SJLJ::get_throw(Ref dom) {
85 auto& w = world();
86 auto [p, inserted] = dom2throw_.emplace(dom, nullptr);
87 auto& tlam = p->second;
88 if (inserted || !tlam) {
89 tlam = w.mut_con(clos_sub_env(dom, w.sigma({jb_type(), rb_type(), tag_type()})))->set("throw");
90 auto [m0, env, var] = split(tlam->var());
91 auto [jbuf, rbuf, tag] = env->projs<3>();
92 auto [m1, r] = mem::op_alloc(var->type(), m0)->projs<2>();
93 auto m2 = w.call<mem::store>(Defs{m1, r, var});
94 rbuf = w.call<core::bitcast>(world().call<mem::Ptr0>(world().call<mem::Ptr0>(var->type())), rbuf);
95 auto m3 = w.call<mem::store>(Defs{m2, rbuf, r});
96 tlam->set(false, w.call<longjmp>(Defs{m3, jbuf, tag}));
97 ignore_.emplace(tlam);
98 }
99 return tlam;
100}
101
102Lam* Clos2SJLJ::get_lpad(Lam* lam, Ref rb) {
103 auto& w = world();
104 auto [p, inserted] = lam2lpad_.emplace(w.tuple({lam, rb}), nullptr);
105 auto& lpad = p->second;
106 if (inserted || !lpad) {
107 auto [_, env_type, dom] = split(lam->dom());
108 lpad = mem::mut_con(env_type)->set("lpad");
109 auto [m, env, __] = split(lpad->var());
110 auto [m1, arg_ptr] = w.call<mem::load>(Defs{m, rb})->projs<2>();
111 arg_ptr = w.call<core::bitcast>(world().call<mem::Ptr0>(dom), arg_ptr);
112 auto [m2, args] = w.call<mem::load>(Defs{m1, arg_ptr})->projs<2>();
113 auto full_args = (lam->num_doms() == 3) ? rebuild(m2, env, {args}) : rebuild(m2, env, args->ops());
114 lpad->app(false, lam, full_args);
115 ignore_.emplace(lpad);
116 }
117 return lpad;
118}
119
121 auto& w = world();
122 get_exn_closures();
123 if (lam2tag_.empty()) return;
124
125 {
126 auto m0 = mem::mem_var(curr_mut());
127 auto [m1, jb] = w.call<clos::alloc_jmpbuf>(m0)->projs<2>();
128 auto [m2, rb] = mem::op_slot(void_ptr(), m1)->projs<2>();
129 auto new_args = curr_mut()->vars();
130 new_args[0] = m2;
131 curr_mut()->reset(curr_mut()->reduce(w.tuple(new_args)));
132
133 cur_jbuf_ = jb;
134 cur_rbuf_ = rb;
135
136 // apparently apply() can change the id of the closures, so we have to do it again :(
137 get_exn_closures();
138 }
139
140 auto body = curr_mut()->body()->as<App>();
141
142 auto branch_type = clos_type(w.cn(w.annex<mem::M>()));
143 auto branches = DefVec(lam2tag_.size() + 1);
144 {
145 auto env = w.tuple(body->args().view().subspan(1));
146 auto new_callee = mem::mut_con(env->type())->set("sjlj_wrap");
147 auto [m, env_var, _] = split(new_callee->var());
148 auto new_args = DefVec(env->num_projs() + 1, [&](size_t i) { return (i == 0) ? *m : env_var->proj(i - 1); });
149 new_callee->app(false, body->callee(), new_args);
150 branches[0] = clos_pack(env, new_callee, branch_type);
151 }
152
153 for (auto [exn_lam, p] : lam2tag_) {
154 auto [i, env] = p;
155 branches[i] = clos_pack(env, get_lpad(exn_lam, cur_rbuf_), branch_type);
156 }
157
158 auto m0 = body->arg(0);
159 assert(m0->type() == w.annex<mem::M>());
160 auto [m1, tag] = w.call<setjmp>(Defs{m0, cur_jbuf_})->projs<2>();
161 tag = w.call(core::conv::s, branches.size(), tag);
162 auto filter = curr_mut()->filter();
163 auto branch = w.extract(w.tuple(branches), tag);
164 curr_mut()->reset({filter, clos_apply(branch, m1)});
165}
166
168 if (auto c = isa_clos_lit(def); c && lam2tag_.contains(c.fnc_as_lam())) {
169 auto& w = world();
170 auto [i, _] = lam2tag_[c.fnc_as_lam()];
171 auto tlam = get_throw(c.fnc_as_lam()->dom());
172 return clos_pack(w.tuple({cur_jbuf_, cur_rbuf_, w.lit_idx(i)}), tlam, c.type());
173 }
174 return def;
175}
176
177} // namespace mim::plug::clos
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
Definition def.cpp:518
auto vars(F f)
Definition def.h:401
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == -1_n) or std::array (otherwise).
Definition def.h:367
const Def * type() const
Definition def.h:248
Def * reset(size_t i, const Def *def)
Successively reset from left to right.
Definition def.h:291
Ref filter() const
Definition lam.h:113
Lam * set(Filter filter, const Def *body)
Definition lam.h:166
Ref body() const
Definition lam.h:114
static const Lam * isa_cn(Ref d)
Definition lam.h:138
World & world()
Definition pass.h:296
Lam * curr_mut() const
Definition pass.h:232
Helper class to retrieve Infer::arg if present.
Definition def.h:86
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
const Def * call(Id id, Args &&... args)
Definition world.h:518
Ref rewrite(Ref) override
void enter() override
Invoked just before Pass::rewriteing PassMan::curr_mut's body.
@ App
Definition def.h:40
@ Extract
Definition def.h:40
@ Lam
Definition def.h:40
The clos Plugin
Definition clos.h:7
ClosLit isa_clos_lit(Ref def, bool fn_isa_lam=true)
Tries to match a closure literal.
Definition clos.cpp:64
Ref clos_sub_env(Ref tup_or_sig, Ref new_env)
Definition clos.h:139
Sigma * clos_type(const Pi *pi)
Creates a typed closure type from pi.
Definition clos.cpp:121
Ref clos_apply(Ref closure, Ref args)
Apply a closure to arguments.
Definition clos.cpp:100
static constexpr size_t Clos_Env_Param
Describes where the environment is placed in the argument list.
Definition clos.h:107
const Sigma * isa_clos_type(Ref def)
Definition clos.cpp:111
Ref clos_pack(Ref env, Ref fn, Ref ct=nullptr)
Pack a typed closure. This assumes that fn expects the environment as its Clos_Env_Paramth argument.
Definition clos.cpp:78
Ref op(trait o, Ref type)
Definition core.h:33
The mem Plugin
Definition mem.h:11
Lam * mut_con(World &w)
Yields con[mem.M].
Definition mem.h:16
Ref op_alloc(Ref type, Ref mem)
Definition mem.h:136
Ref mem_var(Lam *lam)
Returns the memory argument of a function if it has one.
Definition mem.h:38
Ref op_slot(Ref type, Ref mem)
Definition mem.h:144
View< const Def * > Defs
Definition def.h:61
Vector< const Def * > DefVec
Definition def.h:62
GIDSet< const Def * > DefSet
Definition def.h:59