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 // 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
const Def * uncurry_callee() const
Definition lam.h:326
Cleanup(World &world)
Definition phase.h:564
Base class for all Defs.
Definition def.h:273
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1111
void internalize()
Definition def.cpp:608
auto phase(flags_t flags)
Definition driver.h:191
A function.
Definition lam.h:113
A Phase performs one self-contained task over the whole World.
Definition phase.h:25
virtual void apply(const App *)
Invoked if your Phase has additional args.
Definition phase.h:37
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
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:40
Driver & driver()
Definition world.h:103
const fe::Log & log() const
Log via log().e("...", args) etc.; owned by the Driver.
Definition world.cpp:129
Sym sym(std::string_view)
Definition world.cpp:133
const Externals & externals() const
Definition world.h:278
Definition ast.h:16
void optimize(World &)
Runs _compile or _default_compile, if available (in this order).
Definition optimize.cpp:8