Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
clos_conv_prep.h
Go to the documentation of this file.
1#pragma once
2
4#include <thorin/pass/pass.h>
5
7
8namespace thorin {
9
10class EtaExp;
11
12namespace plug::clos {
13
14class ClosConvPrep : public RWPass<ClosConvPrep, Lam> {
15public:
17 : RWPass(man, "clos_conv_prep")
18 , eta_exp_(eta_exp)
19 , old2wrapper_()
20 , lam2fscope_() {}
21
22 void enter() override;
23 Ref rewrite(Ref) override;
24 const App* rewrite_arg(const App* app);
25 const App* rewrite_callee(const App* app);
26
27 Lam* scope(Lam* lam);
28
29 bool from_outer_scope(Lam* lam) {
30 // return scope_.free_defs().contains(lam);
31 return scope(lam) && scope(lam) != scope(curr_mut());
32 }
33
34 bool from_outer_scope(Ref lam) { return scope_->free_defs().contains(lam); }
35
36 Ref eta_wrap(Ref def, attr a) {
37 auto& w = world();
38 auto [entry, inserted] = old2wrapper_.emplace(def, nullptr);
39 auto& wrapper = entry->second;
40 if (inserted) {
41 wrapper = w.mut_lam(def->type()->as<Pi>());
42 wrapper->app(false, def, wrapper->var());
43 lam2fscope_[wrapper] = scope(curr_mut());
44 wrapper_.emplace(wrapper);
45 }
46 return world().call(a, wrapper);
47 }
48
49private:
50 EtaExp* eta_exp_;
51 DefMap<Lam*> old2wrapper_;
52 DefSet wrapper_;
53 Lam2Lam lam2fscope_;
54 std::unique_ptr<Scope> scope_;
55 bool ignore_ = false;
56};
57
58} // namespace plug::clos
59} // namespace thorin
Ref var(nat_t a, nat_t i)
Definition def.h:403
const Def * type() const
Yields the raw type of this Def, i.e. maybe nullptr.
Definition def.h:248
Performs η-expansion: f -> λx.f x, if f is a Lam with more than one user and does not appear in calle...
Definition eta_exp.h:13
A function.
Definition lam.h:97
An optimizer that combines several optimizations in an optimal way.
Definition pass.h:107
PassMan & man()
Definition pass.h:30
World & world()
Definition pass.h:296
A dependent function type.
Definition lam.h:11
Inherit from this class using CRTP, if your Pass does not need state and a fixed-point iteration.
Definition pass.h:220
Helper class to retrieve Infer::arg if present.
Definition def.h:87
const Def * call(Id id, Args &&... args)
Definition world.h:497
const App * rewrite_callee(const App *app)
void enter() override
Invoked just before Pass::rewriteing PassMan::curr_mut's body.
const App * rewrite_arg(const App *app)
ClosConvPrep(PassMan &man, EtaExp *eta_exp)
Definition cfg.h:11
GIDSet< const Def * > DefSet
Definition def.h:60
LamMap< Lam * > Lam2Lam
Definition lam.h:191
GIDMap< const Def *, To > DefMap
Definition def.h:59