MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
optimize.cpp
Go to the documentation of this file.
1#include "mim/pass/optimize.h"
2
3#include "mim/driver.h"
4
5#include "mim/phase/phase.h"
6
7namespace mim {
8
9void optimize(World& world) {
10 // clang-format off
11 auto compilation_functions = {
12 world.sym("_compile"),
13 world.sym("_default_compile"),
14 world.sym("_core_compile"),
15 world.sym("_fallback_compile")
16 };
17 // clang-format on
18
19 const Def* compilation = nullptr;
20 for (auto compilation_function : compilation_functions) {
21 if (auto compilation_ = world.external(compilation_function)) {
22 if (!compilation) compilation = compilation_;
23 compilation_->make_internal();
24 }
25 }
26
27 // make all functions `[] -> %compile.Phase` internal
28 for (auto def : world.copy_externals()) {
29 if (auto lam = def->isa<Lam>(); lam && lam->num_doms() == 0) {
30 // TODO use Axm::isa - but rn there is a problem with the rec Pi and plugin deps
31 if (lam->codom()->sym().view() == "%compile.Phase") {
32 if (!compilation) compilation = lam;
33 def->make_internal();
34 }
35 }
36 }
37
38 if (!compilation) world.ELOG("no compilation function found");
39 world.DLOG("compilation using {} : {}", compilation, compilation->type());
40
41 auto pipe = PhaseMan(world);
42 auto pipe_prog = compilation->as<Lam>()->body();
43 auto [callee, phases] = App::uncurry(pipe_prog);
44 auto axm = callee->as<Axm>();
45
46 world.DLOG("Building pipeline");
47 if (auto phase = world.driver().phase(axm->flags()))
48 (*phase)(pipe, pipe_prog);
49 else
50 world.ELOG("axm not found in passes");
51
52 pipe.run();
53}
54
55} // namespace mim
auto uncurry() const
Definition lam.h:321
Definition axm.h:9
Base class for all Defs.
Definition def.h:251
void make_internal()
Definition def.cpp:564
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:295
auto phase(flags_t flags)
Definition driver.h:80
A function.
Definition lam.h:109
Organizes several Phases in a a pipeline.
Definition phase.h:160
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
const Driver & driver() const
Definition world.h:87
Sym sym(std::string_view)
Definition world.cpp:75
Def * external(Sym name)
Lookup by name.
Definition world.h:204
Vector< Def * > copy_externals() const
Definition world.h:206
Definition ast.h:14
void optimize(World &)
Definition optimize.cpp:9