MimIR
0.4-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
Loading...
Searching...
No Matches
compile.cpp
Go to the documentation of this file.
1
#include "
mim/plug/compile/compile.h
"
2
3
#include <memory>
4
5
#include <
mim/config.h
>
6
#include <
mim/driver.h
>
7
#include <
mim/phase.h
>
8
9
#include <
mim/phase/beta_red.h
>
10
#include <
mim/phase/branch_normalize.h
>
11
#include <
mim/phase/eta_conv.h
>
12
#include <
mim/phase/lam_spec.h
>
13
#include <
mim/phase/ret_wrap.h
>
14
#include <
mim/phase/scalarize.h
>
15
#include <
mim/phase/static_arg_opt.h
>
16
#include <
mim/phase/unload.h
>
17
18
#include "
mim/plug/compile/autogen.h
"
19
20
using namespace
mim
;
21
using namespace
mim::plug
;
22
23
/// Phase hook for `compile.named`.
24
/// Reads the fully-qualified annex name (e.g. `"clos.clos_conv"`) from the driving App at phase-build time,
25
/// looks up the matching annex `Def` in the current `World`, and *redirects* Phase::create to that annex's own
26
/// Phase. If the plugin part of the name is not loaded or the annex is missing, it elides (resolves to nothing),
27
/// so the enclosing `compile.phases` simply skips it.
28
class
Named
:
public
Phase
{
29
public
:
30
Named
(
World
& w,
flags_t
a)
31
:
Phase
(w, a) {}
32
33
void
start
() final { fe::unreachable(); }
// a Named always redirects and never runs itself
34
35
void
apply
(
const
App
* app)
final
{
36
if
(!app)
return
;
37
auto
str =
tuple2str
(app->arg());
38
if
(str.empty())
return
;
39
40
auto
dot = str.find(
'.'
);
41
if
(dot == std::string::npos)
return
;
42
if
(!
driver
().is_loaded(std::string_view(str).substr(0, dot)))
return
;
43
44
if
(
auto
def =
world
().
annex
(
driver
().sym(str))) resolved_ =
Phase::create
(
driver
().phases(), def);
45
}
46
47
bool
redirects
()
const override
{
return
true
; }
48
std::unique_ptr<Phase>
take_resolved
()
override
{
return
std::move(resolved_); }
49
50
private
:
51
std::unique_ptr<Phase> resolved_;
52
};
53
54
void
reg_phases
(
Flags2Phases
& phases) {
55
// clang-format off
56
fe::assert_emplace(phases,
Annex::base<compile::null>
(), [](
World
&) {
return
std::unique_ptr<Phase>{}; });
57
Phase::hook<compile::beta_red, BetaRed >
(phases);
58
Phase::hook<compile::branch_normalize, BranchNormalize>
(phases);
59
Phase::hook<compile::cleanup, Cleanup >
(phases);
60
Phase::hook<compile::eta_conv, EtaConv >
(phases);
61
Phase::hook<compile::lam_spec, LamSpec >
(phases);
62
Phase::hook<compile::named, Named >
(phases);
63
Phase::hook<compile::phases, PhaseMan >
(phases);
64
Phase::hook<compile::unload, Unload >
(phases);
65
Phase::hook<compile::ret_wrap, RetWrap >
(phases);
66
Phase::hook<compile::scalarize, Scalarize >
(phases);
67
Phase::hook<compile::static_arg_opt, StaticArgOpt >
(phases);
68
// clang-format on
69
}
70
71
// clang-format off
72
static
constexpr
PluginArg
known_args
[] = {
73
{
"aggr"
,
"Forces `compile.aggr` to `tt`, switching fixed-point iteration of the `opt` pipeline's `optimize` stage on (off by default)."
},
74
{
"no-aggr"
,
"Forces `compile.aggr` to `ff`, switching that fixed-point iteration off."
},
75
};
76
// clang-format on
77
78
extern
"C"
MIM_EXPORT
Plugin
mim_get_plugin
() {
79
return
{
"compile"
,
MIM_VERSION
,
compile::register_normalizers
,
reg_phases
,
known_args
, std::size(
known_args
),
80
{}, {}};
81
}
reg_phases
void reg_phases(Flags2Phases &phases)
Definition
affine.cpp:12
beta_red.h
branch_normalize.h
Named::apply
void apply(const App *app) final
Invoked if your Phase has additional args.
Definition
compile.cpp:35
Named::take_resolved
std::unique_ptr< Phase > take_resolved() override
The Phase to use instead; nullptr means elide.
Definition
compile.cpp:48
Named::start
void start() final
Actual entry.
Definition
compile.cpp:33
Named::redirects
bool redirects() const override
If true, Phase::create uses take_resolved().
Definition
compile.cpp:47
Named::Named
Named(World &w, flags_t a)
Definition
compile.cpp:30
mim::App
Definition
lam.h:224
mim::Phase::hook
static void hook(Flags2Phases &phases)
Definition
phase.h:70
mim::Phase::annex
flags_t annex() const
Definition
phase.h:81
mim::Phase::Phase
Phase(World &world, std::string name)
Definition
phase.h:29
mim::Phase::create
static std::unique_ptr< Phase > create(const Flags2Phases &phases, const Def *def)
Definition
phase.h:50
mim::Phase::driver
Driver & driver()
Definition
phase.h:78
mim::Phase::world
World & world()
Definition
phase.h:77
mim::World
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition
world.h:40
autogen.h
known_args
static constexpr PluginArg known_args[]
Definition
compile.cpp:72
compile.h
config.h
MIM_EXPORT
#define MIM_EXPORT
Definition
config.h:21
driver.h
eta_conv.h
lam_spec.h
mim::plug::compile::register_normalizers
void register_normalizers(Normalizers &normalizers)
mim::plug
Definition
lower_for.h:5
mim
Definition
ast.h:16
mim::flags_t
u64 flags_t
Definition
types.h:39
mim::Flags2Phases
absl::flat_hash_map< flags_t, std::function< std::unique_ptr< Phase >(World &)> > Flags2Phases
Maps an axiom of a Phase to a function that creates one.
Definition
plugin.h:30
mim::tuple2str
std::string tuple2str(const Def *)
Definition
tuple.cpp:48
mim::mim_get_plugin
mim::Plugin mim_get_plugin()
mim::PluginArg
One -X <plugin>:<arg> a Plugin understands; see Arguments.
Definition
plugin.h:34
phase.h
MIM_VERSION
#define MIM_VERSION
Definition
plugin.h:149
ret_wrap.h
scalarize.h
static_arg_opt.h
mim::Annex::base
static consteval flags_t base()
Definition
plugin.h:250
mim::Plugin
Basic info and registration function pointer to be returned from a specific plugin.
Definition
plugin.h:154
unload.h
src
mim
plug
compile
compile.cpp
Generated by
1.18.0