MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
optimize.cpp
Go to the documentation of this file.
2
3#include "mim/driver.h"
4#include "mim/phase.h"
5
6namespace mim {
7
8void optimize(World& world) {
9 world.driver().move_to_library(world);
10
11 // clang-format off
12 auto compilation_functions = {
13 world.sym("_compile"),
14 world.sym("_default_compile"),
15 };
16 // clang-format on
17
18 const Def* compilation = nullptr;
19 for (auto compilation_function : compilation_functions) {
20 if (auto compilation_ = world.externals()[compilation_function]) {
21 if (!compilation) compilation = compilation_;
22 compilation_->internalize();
23 }
24 }
25
26 // make all functions `[] -> compile.Phase` internal
27 for (auto def : world.externals().mutate()) {
28 if (auto lam = def->isa<Lam>(); lam && lam->num_doms() == 0) {
29 if (lam->codom()->sym().view() == "compile.Phase") {
30 if (!compilation) compilation = lam;
31 def->internalize();
32 }
33 }
34 }
35
36 if (!compilation) {
37 world.log().i("no compilation function found; skipping optimization");
38 } else {
39 world.log().d("compile with {}: {}", compilation, compilation->type());
40
41 world.log().d("build pipeline");
42 if (auto phase = Phase::create(world.driver().phases(), compilation->as<Lam>()->body())) phase->run();
43 }
44}
45
46} // namespace mim
Base class for all Defs.
Definition def.h:289
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1119
void internalize()
Definition def.cpp:626
void move_to_library(World &world)
Moves everything of world into library() and imports only its externals back.
Definition driver.cpp:141
const auto & phases() const
Definition driver.h:212
A function.
Definition lam.h:91
const Def * body() const
Definition lam.h:104
nat_t num_doms() const noexcept
Definition lam.h:117
static std::unique_ptr< Phase > create(const Flags2Phases &phases, const Def *def)
Definition phase.cpp:25
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:194
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
Driver & driver()
Definition world.h:107
const fe::Log & log() const
Log via log().e("...", args) etc.; owned by the Driver.
Definition world.cpp:224
Sym sym(std::string_view)
Definition world.cpp:228
const Externals & externals() const
Definition world.h:286
Definition ast.h:16
void optimize(World &)
Runs _compile or _default_compile, if available (in this order).
Definition optimize.cpp:8