MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
add_mem.h
Go to the documentation of this file.
1#pragma once
2
3#include <mim/phase.h>
4
6
7/// Threads the `mem.M` memory monad through the world:
8/// mem-extends continuations and rewires every memory operand to the *current* memory at that program point.
9/// It's primarily to be used as preparation for other phases that rely on all continuations having a mem.
10/// It also splices the `⊥ : mem.M 0` memory placeholders of freshly emitted memory operations into the
11/// global memory chain and resolves `mem.fresh (a, k)` requests by jumping to `k` with the current memory
12/// (see mim::plug::tensor::phase::LowerToMem, which emits both and schedules this phase right behind itself).
13///
14/// The rewrite is a plain RWPhase.
15/// Memory is a linear resource, so at any program point exactly one memory token is live - the *current* memory.
16/// We track it in AddMem::curr_mem_ while rewriting a continuation's body:
17/// - it starts out as the continuation's (leading or grouped) mem parameter,
18/// - a rewritten memory operation advances it to the operation's result mem, and
19/// - every memory-typed *operand* is rewritten to the current memory.
20///
21/// Because the Rewriter visits operands before their users, the current memory naturally threads through the
22/// data-dependency order - no separate schedule is required.
23/// Independent `⊥`/`⊤` placeholder chains are thereby linearized into one chain in encounter order.
24///
25/// Three rules keep the rewrite type-correct in the presence of axiom-pinned ABIs:
26/// - Only continuations (Pi::isa_cn) are mem-extended; direct-style functions (e.g. the affine index
27/// mappings passed to `btensor.map_reduce_post`) keep their signature.
28/// - A pi whose leading parameter carries the memory *grouped* (the `Fn [mem.M 0, To, ins] → …` shape of
29/// a mem-threaded combiner) counts as already mem-threaded.
30/// - Lams reachable from axm-app arguments are preserved untouched: axioms pin their arguments' ABI
31/// (e.g. the combiner slot of `btensor.map_reduce_post`), so mem-extending them would be ill-typed.
32class AddMem : public RWPhase {
33public:
36
37private:
38 bool analyze() final;
39 const Def* rewrite(const Def*) override;
40 const Def* rewrite_mut_Lam(Lam*) override;
41 const Def* rewrite_imm_App(const App*) override;
42 const Def* rewrite_imm_Tuple(const Tuple*) override;
43 const Def* rewrite_imm_Pi(const Pi*) override;
44
45 /// Advances AddMem::curr_mem_ if @p def produces a memory (a bare `mem.M` or a `[mem.M, …]` tuple).
46 void advance_mem(const Def* def);
47
48 /// The current memory token in the continuation being rewritten (new world), or `nullptr` outside any mem context.
49 const Def* curr_mem_ = nullptr;
50 /// `true` while rewriting a pinned-ABI subtree (an axm-app argument): no memory is threaded or added there.
51 bool preserving_ = false;
52 /// Lams (transitively) reachable from axm-app arguments: their ABI is pinned by the axiom.
53 LamSet preserved_;
54};
55
56} // namespace mim::plug::mem::phase
Base class for all Defs.
Definition def.h:273
A function.
Definition lam.h:113
flags_t annex() const
Definition phase.h:81
A dependent function type.
Definition lam.h:14
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition phase.h:431
World & world()=delete
Hides both and forbids direct access.
Data constructor for a Sigma.
Definition tuple.h:61
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:40
bool analyze() final
Runs the optional pre-analysis on Phase::world, typically to a fixed point, before rewriting begins.
Definition add_mem.cpp:11
const Def * rewrite_imm_Tuple(const Tuple *) override
Definition add_mem.cpp:171
const Def * rewrite_imm_Pi(const Pi *) override
Definition add_mem.cpp:68
const Def * rewrite(const Def *) override
Definition add_mem.cpp:42
const Def * rewrite_mut_Lam(Lam *) override
Definition add_mem.cpp:105
const Def * rewrite_imm_App(const App *) override
Definition add_mem.cpp:135
AddMem(World &world, flags_t annex)
Definition add_mem.h:34
GIDSet< Lam * > LamSet
Definition lam.h:220
u64 flags_t
Definition types.h:39