MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
reshape.h
Go to the documentation of this file.
1#pragma once
2
3#include <queue>
4
5#include "mim/phase/phase.h"
6
7namespace mim::plug::mem {
8
9using DefQueue = std::deque<const Def*>;
10
11/// The general idea of this pass/phase is to change the shape of signatures of functions.
12/// * Example: `Cn[ [mem, A, B], C , ret]`
13/// * Arg : `Cn[ [mem, [A, B , C]], ret]` (general `Cn[ [mem, args], ret]`)
14/// * Flat : `Cn[ mem, A, B , C , ret]` (general `Cn[mem, ...args, ret]`)
15/// For convenience, we want Arg-style for optimizations.
16/// The invariant is that every closed function has at most one "real" argument and a return-continuation.
17/// If memory is present, the argument is a pair of memory and the remaining arguments.
18/// However, flat style is required for code generation. Especially in the closure conversion.
19///
20/// The concept is to rewrite all signatures of functions with consistent reassociation of arguments.
21/// This change is propagated to (nested) applications.
22// TODO: use RWPhase instead
23class Reshape : public RWPass<Reshape, Lam> {
24public:
25 enum Mode { Flat, Arg };
26
28 : RWPass(man, "reshape")
29 , mode_(mode) {}
30
31 /// Fall-through to `rewrite_def` which falls through to `rewrite_lam`.
32 void enter() override;
33
34private:
35 /// Memoized version of `rewrite_def_`
36 const Def* rewrite_def(const Def* def);
37 /// Replace lambas with reshaped versions, shape application arguments, and replace vars and already rewritten
38 /// lambdas.
39 const Def* rewrite_def_(const Def* def);
40 /// Create a new lambda with the reshaped signature and rewrite its body.
41 /// The old var is associated with a reshaped version of the new var in `old2new_`.
42 Lam* reshape_lam(Lam* def);
43
44 /// Reshapes a type into its flat or arg representation.
45 const Def* reshape_type(const Def* T);
46 /// Reshapes a def into its flat or arg representation.
47 const Def* reshape(const Def* def);
48 // This generalized version of reshape transforms def to match the shape of target.
49 const Def* reshape(const Def* def, const Def* target);
50 /// Reconstructs the target type by taking defs out of the queue.
51 const Def* reshape(DefVec&, const Def* target, const Def* mem);
52
53 /// Keeps track of the replacements.
54 Def2Def old2new_;
55 /// The mode to rewrite all lambas to. Either flat or arg.
56 Mode mode_;
57};
58
59} // namespace mim::plug::mem
Base class for all Defs.
Definition def.h:223
A function.
Definition lam.h:103
An optimizer that combines several optimizations in an optimal way.
Definition pass.h:107
PassMan & man()
Definition pass.h:30
Inherit from this class using CRTP, if your Pass does not need state and a fixed-point iteration.
Definition pass.h:220
The general idea of this pass/phase is to change the shape of signatures of functions.
Definition reshape.h:23
void enter() override
Fall-through to rewrite_def which falls through to rewrite_lam.
Definition reshape.cpp:69
Reshape(PassMan &man, Mode mode)
Definition reshape.h:27
The mem Plugin
Definition mem.h:11
std::deque< const Def * > DefQueue
Definition reshape.h:9
DefMap< const Def * > Def2Def
Definition def.h:60