MimIR
MimIR is my Intermediate Representation
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
7namespace 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.
25class DS2CPS : public RWPhase {
26public:
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
32protected:
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) {}
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
64private:
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;
69 Lam* cont;
70 };
71
72 fe::Vector<Pending> pending_;
73 bool liftable_ = false;
74};
75
76} // namespace mim
size_t base() const
Definition ds2cps.h:45
Scope(DS2CPS &phase, bool liftable)
Definition ds2cps.h:37
An RWPhase that lifts direct-style calls of CPS functions out of the bodies of continuations.
Definition ds2cps.h:25
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
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
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
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition phase.h:360
~DS2CPS() override
Definition ds2cps.h:28
bool liftable() const
Is the body of a continuation being rewritten, so that lift() is legal?
Definition ds2cps.h:55
Base class for all Defs.
Definition def.h:285
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition phase.cpp:23
std::string_view name() const
Definition phase.h:63
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition phase.h:360
pop()s at the end of its C++ scope.
Definition rewrite.h:44
Pop scoped(Vars old_vars={}, Vars new_vars={})
push()es a scope that lives as long as the returned Pop.
Definition rewrite.h:62
Definition ast.h:16
fe::View< const Def * > Defs
Definition def.h:94
fe::Vector< const Def * > DefVec
Definition def.h:96