MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
lower_typed_clos_prep.cpp
Go to the documentation of this file.
2
3#include <mim/plug/mem/mem.h>
4
5namespace mim::plug::clos {
6
7namespace {
8
9bool interesting_type(const Def* type, DefSet& visited) {
10 if (type->isa_mut()) visited.insert(type);
11 if (isa_clos_type(type)) return true;
12 if (auto sigma = type->isa<Sigma>())
13 return std::any_of(sigma->ops().begin(), sigma->ops().end(),
14 [&](auto d) { return !visited.contains(d) && interesting_type(d, visited); });
15 if (auto arr = type->isa<Arr>()) return interesting_type(arr->body(), visited);
16 return false;
17}
18
19bool interesting_type(const Def* def) {
20 auto visited = DefSet();
21 return interesting_type(def->type(), visited);
22}
23
24void split(DefSet& out, const Def* def, bool as_callee) {
25 if (auto lam = def->isa<Lam>()) {
26 out.insert(lam);
27 } else if (auto [var, lam] = ca_isa_var<Lam>(def); var && lam) {
28 if (var->type()->isa<Pi>() || interesting_type(var)) out.insert(var);
29 } else if (auto c = isa_clos_lit(def, false)) {
30 split(out, c.fnc(), as_callee);
31 } else if (auto a = Axm::isa<attr>(def)) {
32 split(out, a->arg(), as_callee);
33 } else if (auto proj = def->isa<Extract>()) {
34 split(out, proj->tuple(), as_callee);
35 } else if (auto pack = def->isa<Pack>()) {
36 split(out, pack->body(), as_callee);
37 } else if (auto tuple = def->isa<Tuple>()) {
38 for (auto op : tuple->ops())
39 split(out, op, as_callee);
40 } else if (as_callee) {
41 out.insert(def);
42 }
43}
44
45DefSet split(const Def* def, bool keep_others) {
46 DefSet out;
47 split(out, def, keep_others);
48 return out;
49}
50
51} // namespace
52
53undo_t LowerTypedClosPrep::set_esc(const Def* def) {
54 auto undo = No_Undo;
55 for (auto d : split(def, false)) {
56 if (is_esc(d)) continue;
57 if (auto lam = d->isa_mut<Lam>())
58 undo = std::min(undo, undo_visit(lam));
59 else if (auto [var, lam] = ca_isa_var<Lam>(d); var && lam)
60 undo = std::min(undo, undo_visit(lam));
61 DLOG("set esc: {}", d);
62 esc_.emplace(d);
63 }
64 return undo;
65}
66
68 if (auto closure = isa_clos_lit(def, false)) {
69 auto fnc = closure.fnc();
70 if (!Axm::isa<attr>(fnc)) {
71 auto new_fnc = world().call(esc_.contains(fnc) ? attr::esc : attr::bottom, fnc);
72 return clos_pack(closure.env(), new_fnc, closure->type());
73 }
74 }
75 return def;
76}
77
79 if (auto c = isa_clos_lit(def, false)) {
80 DLOG("closure ({}, {})", c.env(), c.fnc());
81 if (!c.fnc_as_lam() || is_esc(c.fnc_as_lam()) || is_esc(c.env_var())) return set_esc(c.env());
82 } else if (auto store = Axm::isa<mem::store>(def)) {
83 DLOG("store {}", store->arg(2));
84 return set_esc(store->arg(2));
85 } else if (auto app = def->isa<App>(); app && Pi::isa_cn(app->callee_type())) {
86 DLOG("app {}", def);
87 auto undo = No_Undo;
88 auto callees = split(app->callee(), true);
89 for (auto i = 0_u64; i < app->num_args(); i++) {
90 if (!interesting_type(app->arg(i))) continue;
91 if (std::any_of(callees.begin(), callees.end(), [&](const Def* callee) {
92 if (auto lam = callee->isa_mut<Lam>()) return is_esc(lam->var(i));
93 return true;
94 }))
95 undo = std::min(undo, set_esc(app->arg(i)));
96 }
97 return undo;
98 }
99 return No_Undo;
100}
101
102} // namespace mim::plug::clos
static auto isa(const Def *def)
Definition axm.h:107
Base class for all Defs.
Definition def.h:251
undo_t undo_visit(Def *mut) const
Definition pass.h:360
static const Pi * isa_cn(const Def *d)
Is this a continuation - i.e. is the Pi::codom mim::Bottom?
Definition lam.h:48
World & world()
Definition pass.h:64
const Def * call(Id id, Args &&... args)
Complete curried call of annexes obeying implicits.
Definition world.h:546
const Def * rewrite(const Def *) override
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Definition log.h:95
The clos Plugin
Definition clos.h:7
ClosLit isa_clos_lit(const Def *def, bool fn_isa_lam=true)
Tries to match a closure literal.
Definition clos.cpp:59
std::tuple< const Extract *, N * > ca_isa_var(const Def *def)
Checks is def is the variable of a mut of type N.
Definition clos.h:79
const Def * clos_pack(const Def *env, const Def *fn, const Def *ct=nullptr)
Pack a typed closure. This assumes that fn expects the environment as its Clos_Env_Paramth argument.
Definition clos.cpp:73
const Sigma * isa_clos_type(const Def *def)
Definition clos.cpp:106
GIDSet< const Def * > DefSet
Definition def.h:74
size_t undo_t
Definition pass.h:21
static constexpr undo_t No_Undo
Definition pass.h:22
@ Pi
Definition def.h:114
@ Lam
Definition def.h:114
@ Arr
Definition def.h:114
@ Pack
Definition def.h:114
@ Sigma
Definition def.h:114
@ Extract
Definition def.h:114
@ Tuple
Definition def.h:114