15void flatten(
const Def* x,
DefVec& res) {
16 if (
auto n = x->num_projs(); n > 1)
17 for (
size_t i = 0; i != n; ++i)
18 flatten(x->proj(n, i), res);
24const Def* part(
const Def* var,
const Def* arg,
const Def* e) {
25 if (e == var)
return arg;
26 if (
auto ex =
e->isa<Extract>())
27 if (
auto a = part(var, arg, ex->tuple()))
return e->world().extract(a, ex->index());
32class Closer :
public Rewriter {
34 Closer(
const Liveness&
live, Lam* fun,
const LamMap<DefVec>& sigs,
const LamMap<Lam*>& lams)
35 : Rewriter(fun->world())
36 , target_(
live.target())
45 const Def* rewrite(
const Def*)
final;
47 void tree(Lam* old_lam, Lam* new_lam);
49 const Def* join(
const Def* x);
53 const Def* jump(
const App* term, fe::Vector<Lam*>& nested);
55 DefVec args(Lam* bb,
const App* term);
56 Lam* closed(Lam* lam)
const {
57 auto new_lam = fe::lookup(lams_, lam);
58 return new_lam ? new_lam : lam;
61 const Target& target_;
62 const Liveness& live_;
64 const LamMap<DefVec>& sigs_;
65 const LamMap<Lam*>& lams_;
69void Closer::run(Lam* lam) {
70 auto new_lam = closed(lam);
72 const auto& sig = sigs_.find(lam)->second;
74 for (
size_t i = 0; i != m; ++i)
75 map(sig[i], new_lam->var(m, i));
81const Def* Closer::join(
const Def* x) {
82 if (
auto y = lookup(x))
return y;
83 auto n = x->num_projs();
84 return map(x, world().tuple(
DefVec(n, [&](
size_t i) {
return join(x->proj(n, i)); })));
87void Closer::tree(Lam* old_lam, Lam* new_lam) {
88 auto b = live_.
block(old_lam);
89 owned_.insert(
b->insns.begin(),
b->insns.end());
90 auto nested = fe::Vector<Lam*>();
91 auto body = jump(old_lam->body()->as<App>(), nested);
92 if (new_lam != old_lam) {
93 new_lam->set(world().lit_ff(), body);
95 auto filter = rewrite(old_lam->filter());
96 old_lam->unset()->set(filter, body);
98 if (
auto k =
call_cont(live_, fun_, *b)) nested.emplace_back(k->lam);
99 for (
auto bb : nested)
100 if (lams_.contains(bb))
106void Closer::forward(Lam* k) {
108 for (
auto e : sigs_.find(k)->second)
109 args.emplace_back(part(k->var(), k->var(), e) ? e : rewrite(e));
110 k->unset()->set(world().lit_ff(), world().app(closed(k), world().tuple(args)));
113const Def* Closer::rewrite(
const Def* def) {
114 if (
auto new_def = lookup(def))
return new_def;
115 if (def->isa_mut() || def->is_closed() || def->isa<Var>())
return def;
116 if (
auto app = def->isa<App>(); app && target_.
prev_mem(app) && !owned_.contains(app))
117 def->blame(
"{} backend: instruction `{}` is not available here", target_.
name(), def).bail();
118 return Rewriter::rewrite(def);
121const Def* Closer::jump(
const App* term, fe::Vector<Lam*>& nested) {
122 if (
auto bb = Lam::isa_mut_basicblock(term->callee()); bb && sigs_.contains(bb))
123 return world().app(closed(bb), world().tuple(args(bb, term)));
124 if (
auto dispatch = Dispatch(term))
125 for (
size_t i = 0, n = dispatch.num_targets(); i != n; ++i)
126 nested.emplace_back(dispatch.target(i)->as_mut<Lam>());
127 return rewrite(term);
130DefVec Closer::args(Lam* bb,
const App* term) {
133 for (
auto e : sigs_.find(bb)->second)
134 if (
auto a = part(bb->var(), term->arg(), e))
135 res.emplace_back(rewrite(a));
136 else if (used.contains(e))
137 res.emplace_back(rewrite(e));
139 res.emplace_back(world().
lit(
e->type(), 0));
149 const auto& blocks =
live.blocks();
152 for (
const auto& b : blocks)
153 for (
auto s : b.targets)
154 ++jumps[blocks[s].lam];
157 for (
const auto& b : blocks) {
158 if (
auto k =
call_cont(
live, fun, b); k && !nested.emplace(k->lam).second)
159 k->lam->blame(
"{} backend: return continuation `{}` of several calls", t.name(), k->lam).bail();
160 if (
auto dispatch =
Dispatch(b.lam->body())) {
162 if (dispatch.num_args() != 1 || !t.is_mem(dispatch.arg()))
163 b.lam->blame(
"{} backend: `{}` passes more than its memory to a branch", t.name(), b.lam).bail();
164 for (
size_t i = 0, e = dispatch.num_targets(); i != e; ++i) {
166 if (!bb || !
live.block(bb))
167 b.lam->blame(
"{} backend: `{}` branches to something other than blocks", t.name(), b.lam).bail();
168 if (jumps[bb] != 1 || !nested.emplace(bb).second)
169 bb->blame(
"{} backend: branch target `{}` has several predecessors", t.name(), bb).bail();
175 for (
const auto& b : blocks | std::views::drop(1))
176 if (!nested.contains(b.lam)) lifted[b.lam] = &b.live_in;
178 auto forwards = fe::Vector<Lam*>();
179 for (
const auto& b : blocks)
181 lifted[k->lam] = &k->live_in;
182 forwards.emplace_back(k->lam);
185 auto rank = [&](
const Def* x) ->
size_t {
186 if (t.is_mem(x))
return 0;
193 for (
const auto& b : blocks) {
194 auto vs = fe::lookup(lifted, b.lam);
197 auto n = lam->num_vars();
198 auto params =
DefVec(n, [&](
size_t i) {
return lam->var(n, i); });
200 for (
auto x : params)
202 auto extra =
DefVec(vs->begin(), vs->end());
203 std::ranges::sort(extra, {}, &
Def::gid);
206 lam->blame(
"{} backend: `{}` of class {} would become a parameter of `{}`", t.name(), v,
size_t(c), lam)
210 sig.insert(sig.end(), extra.begin(), extra.end());
211 std::ranges::stable_sort(sig, {}, rank);
212 auto same = extra.empty() && std::ranges::equal(sig, params);
217 auto new_lam = w.mut_con(
DefVec(m, [&](
size_t i) {
return sig[i]->type(); }))->set(lam->dbg());
218 for (
size_t i = 0; i != m; ++i)
219 new_lam->var(m, i)->set(sig[i]->sym());
224 for (
auto k : forwards | std::views::reverse)
225 Closer(
live, fun, sigs, lams).run(k);
226 for (
const auto& b : blocks)
227 if (!nested.contains(b.lam)) Closer(
live, fun, sigs, lams).run(b.lam);
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
static Lam * isa_mut_basicblock(const Def *d)
A dependent function type.
std::string_view name() const
The plugin name, used as the prefix of diagnostics.
const Target & target() const
const Block * block(Lam *lam) const
Liveness(const FunPhase &phase, Lam *fun, bool solve=false)
Bails unless the blocks of the function fun are closed; with solve, it iterates to the fixed point in...
const Def * prev_mem(const App *app) const
The memory app continues, if app is an instruction on a mem.M 0 chain.
const Liveness::Block * call_cont(const Liveness &live, Lam *fun, const Liveness::Block &block)
The block that a call at the end of block returns to, if any.
GIDMap< Lam *, To > LamMap
fe::Vector< const Def * > DefVec
GIDSet< const Def * > DefSet