MimIR
0.4-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
Loading...
Searching...
No Matches
optimize.cpp
Go to the documentation of this file.
1
#include "
mim/phase/optimize.h
"
2
3
#include "
mim/driver.h
"
4
#include "
mim/phase.h
"
5
6
namespace
mim
{
7
8
void
optimize
(
World
& world) {
9
// The freshly compiled world may still contain solved-but-unresolved Hole%s, let's try to resolve them before
10
// running the optimization pipeline.
11
Cleanup
(world).run();
12
13
// clang-format off
14
auto
compilation_functions = {
15
world.
sym
(
"_compile"
),
16
world.
sym
(
"_default_compile"
),
17
};
18
// clang-format on
19
20
const
Def
* compilation =
nullptr
;
21
for
(
auto
compilation_function : compilation_functions) {
22
if
(
auto
compilation_ = world.
externals
()[compilation_function]) {
23
if
(!compilation) compilation = compilation_;
24
compilation_->
internalize
();
25
}
26
}
27
28
// make all functions `[] -> compile.Phase` internal
29
for
(
auto
def : world.
externals
().
mutate
()) {
30
if
(
auto
lam = def->isa<
Lam
>(); lam && lam->num_doms() == 0) {
31
if
(lam->codom()->sym().view() ==
"compile.Phase"
) {
32
if
(!compilation) compilation = lam;
33
def->
internalize
();
34
}
35
}
36
}
37
38
if
(!compilation) {
39
world.
log
().i(
"no compilation function found; skipping optimization"
);
40
}
else
{
41
world.
log
().d(
"compile with {}: {}"
, compilation, compilation->
type
());
42
43
auto
body = compilation->as<
Lam
>()->body();
44
auto
callee =
App::uncurry_callee
(body);
45
46
world.
log
().d(
"build pipeline"
);
47
if
(
auto
f = world.
driver
().
phase
(callee->flags())) {
48
auto
stage = (*f)(world);
49
auto
phase = stage.get()->as<
Phase
>();
50
if
(
auto
app = body->isa<
App
>()) phase->
apply
(app);
51
phase->run();
52
}
else
53
fe::throwf(
"no phase registered for stage `{}`; is its plugin loaded?"
, callee);
54
}
55
}
56
57
}
// namespace mim
mim::App
Definition
lam.h:224
mim::App::uncurry_callee
const Def * uncurry_callee() const
Definition
lam.h:326
mim::Cleanup::Cleanup
Cleanup(World &world)
Definition
phase.h:564
mim::Def
Base class for all Defs.
Definition
def.h:273
mim::Def::type
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition
def.h:1111
mim::Def::internalize
void internalize()
Definition
def.cpp:608
mim::Driver::phase
auto phase(flags_t flags)
Definition
driver.h:191
mim::Lam
A function.
Definition
lam.h:113
mim::Phase
A Phase performs one self-contained task over the whole World.
Definition
phase.h:25
mim::Phase::apply
virtual void apply(const App *)
Invoked if your Phase has additional args.
Definition
phase.h:37
mim::World::Externals::mutate
fe::Vector< Def * > mutate() const
Returns a copy of muts() in a fe::Vector; this allows you to modify the Externals while iterating.
Definition
world.h:182
mim::World
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition
world.h:40
mim::World::driver
Driver & driver()
Definition
world.h:103
mim::World::log
const fe::Log & log() const
Log via log().e("...", args) etc.; owned by the Driver.
Definition
world.cpp:129
mim::World::sym
Sym sym(std::string_view)
Definition
world.cpp:133
mim::World::externals
const Externals & externals() const
Definition
world.h:278
driver.h
mim
Definition
ast.h:16
mim::optimize
void optimize(World &)
Runs _compile or _default_compile, if available (in this order).
Definition
optimize.cpp:8
optimize.h
phase.h
src
mim
phase
optimize.cpp
Generated by
1.18.0