MimIR
0.6-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
Loading...
Searching...
No Matches
ds2cps.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <exception>
4
5
#include "
mim/phase.h
"
6
7
namespace
mim
{
8
9
/// An RWPhase that lifts direct-style calls of CPS functions out of the bodies of continuations.
10
///
11
/// A subclass calls lift() for a call `k arg` of a CPS `k: Cn [T, Cn U]` while rewriting a continuation's body.
12
/// It allocates a fresh continuation `cont` receiving the result and records the pending call `k (arg, cont)`;
13
/// `cont`'s variable is the value of the call.
14
/// Once the enclosing Lam is done, the pending calls are wired up in encounter order.
15
/// This respects data dependencies, because operands are always rewritten before their users:
16
/// ```
17
/// lam = k₀ (arg₀, cont₀)
18
/// cont₀ = k₁ (arg₁, cont₁)
19
/// ...
20
/// contₙ = rewritten body of lam
21
/// ```
22
/// A lifted call simply lives in the Lam whose body rewriting first reaches it.
23
/// Rewrites whose result mentions such a continuation variable are scoped to that Lam.
24
/// Sibling scopes re-derive - and thus re-lift - them.
25
class
DS2CPS
:
public
RWPhase
{
26
public
:
27
using
RWPhase::RWPhase
;
28
~DS2CPS
()
override
{
29
assert((std::uncaught_exceptions() || pending_.empty()) &&
"a call lifted outside of any Lam is never wired"
);
30
}
31
32
protected
:
33
/// RAII guard for rewriting the body of one mutable Lam:
34
/// fences pending_, pushes a map scope for contaminated rewrites, and tracks whether lifting is legal here.
35
class
Scope
{
36
public
:
37
Scope
(
DS2CPS
& phase,
bool
liftable
)
38
: phase_(phase)
39
, base_(phase.pending_.size())
40
, liftable_(phase.liftable_,
liftable
) {}
41
~Scope
() {
42
assert((std::uncaught_exceptions() || phase_.pending_.size() == base_) &&
"pending calls must be wire()d"
);
43
}
44
45
size_t
base
()
const
{
return
base_; }
46
47
private
:
48
DS2CPS
& phase_;
49
size_t
base_;
50
Pop
pop_ = phase_.
scoped
();
51
fe::Restore<bool> liftable_;
52
};
53
54
/// Is the body of a continuation being rewritten, so that lift() is legal?
55
bool
liftable
()
const
{
return
liftable_; }
56
/// Records the call `k (new_args..., cont)` as pending and returns the Var of `cont: Cn new_type` as its result.
57
/// `cont` has @p filter and is named @p name, or after `k` by default.
58
const
Def
*
lift
(
const
Def
* k,
Defs
new_args,
const
Def
* new_type,
bool
filter =
false
, Sym
name
= {});
59
/// Wires all pending calls above @p base around @p body and returns the new body.
60
const
Def*
wire
(
size_t
base,
const
Def* body);
61
62
std::array<const Def*, 2>
rewrite_filter_body
(Lam*, Lam*)
override
;
63
64
private
:
65
/// A lifted call `callee (args..., cont)` waiting to be wired into the body of the enclosing Lam.
66
struct
Pending {
67
const
Def* callee;
68
DefVec
args
;
69
Lam* cont;
70
};
71
72
fe::Vector<Pending> pending_;
73
bool
liftable_ =
false
;
74
};
75
76
}
// namespace mim
mim::DS2CPS::Scope::~Scope
~Scope()
Definition
ds2cps.h:41
mim::DS2CPS::Scope::base
size_t base() const
Definition
ds2cps.h:45
mim::DS2CPS::Scope::Scope
Scope(DS2CPS &phase, bool liftable)
Definition
ds2cps.h:37
mim::DS2CPS
An RWPhase that lifts direct-style calls of CPS functions out of the bodies of continuations.
Definition
ds2cps.h:25
mim::DS2CPS::lift
const Def * lift(const Def *k, Defs new_args, const Def *new_type, bool filter=false, Sym name={})
Records the call k (new_args..., cont) as pending and returns the Var of cont: Cn new_type as its res...
Definition
ds2cps.cpp:11
mim::DS2CPS::wire
const Def * wire(size_t base, const Def *body)
Wires all pending calls above base around body and returns the new body.
Definition
ds2cps.cpp:22
mim::DS2CPS::rewrite_filter_body
std::array< const Def *, 2 > rewrite_filter_body(Lam *, Lam *) override
The rewritten filter and body of old_lam for its stub new_lam, before the body is coerce()d.
Definition
ds2cps.cpp:5
mim::DS2CPS::RWPhase
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition
phase.h:360
mim::DS2CPS::~DS2CPS
~DS2CPS() override
Definition
ds2cps.h:28
mim::DS2CPS::liftable
bool liftable() const
Is the body of a continuation being rewritten, so that lift() is legal?
Definition
ds2cps.h:55
mim::Def
Base class for all Defs.
Definition
def.h:285
mim::Phase::args
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition
phase.cpp:23
mim::Phase::name
std::string_view name() const
Definition
phase.h:63
mim::RWPhase::RWPhase
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition
phase.h:360
mim::Rewriter::Pop
pop()s at the end of its C++ scope.
Definition
rewrite.h:44
mim::Rewriter::scoped
Pop scoped(Vars old_vars={}, Vars new_vars={})
push()es a scope that lives as long as the returned Pop.
Definition
rewrite.h:62
mim
Definition
ast.h:16
mim::Defs
fe::View< const Def * > Defs
Definition
def.h:94
mim::DefVec
fe::Vector< const Def * > DefVec
Definition
def.h:96
phase.h
include
mim
phase
ds2cps.h
Generated by
1.18.0