MimIR
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#include "mim/phase.h"
5
6namespace mim {
7
8void optimize(World& world) {
9 // clang-format off
10 auto compilation_functions = {
11 world.sym("_compile"),
12 world.sym("_default_compile"),
13 };
14 // clang-format on
15
16 const Def* compilation = nullptr;
17 for (auto compilation_function : compilation_functions) {
18 if (auto compilation_ = world.externals()[compilation_function]) {
19 if (!compilation) compilation = compilation_;
20 compilation_->internalize();
21 }
22 }
23
24 // make all functions `[] -> %compile.Phase` internal
25 for (auto def : world.externals().mutate()) {
26 if (auto lam = def->isa<Lam>(); lam && lam->num_doms() == 0) {
27 if (lam->codom()->sym().view() == "%compile.Phase") {
28 if (!compilation) compilation = lam;
29 def->internalize();
30 }
31 }
32 }
33
34 if (!compilation) {
35 world.ILOG("no compilation function found - skipping optimization");
36 } else {
37 world.DLOG("compilation using {} : {}", compilation, compilation->type());
38
39 auto body = compilation->as<Lam>()->body();
40 auto callee = App::uncurry_callee(body);
41
42 world.DLOG("Building pipeline");
43 if (auto f = world.driver().stage(callee->flags())) {
44 auto stage = (*f)(world);
45 auto phase = stage.get()->as<Phase>();
46 if (auto app = body->isa<App>()) phase->apply(app);
47 phase->run();
48 } else
49 world.ELOG("axm not found in passes");
50 }
51}
52
53} // namespace mim
const Def * uncurry_callee() const
Definition lam.h:327
Base class for all Defs.
Definition def.h:246
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:452
void internalize()
Definition def.cpp:576
auto stage(flags_t flags)
Definition driver.h:120
A function.
Definition lam.h:110
Unlike a Pass, a Phase performs one self-contained task and does not interleave with other phases.
Definition phase.h:27
virtual void apply(const App *)
Invoked if your Stage has additional args.
Definition pass.h:37
Vector< Def * > mutate() const
Returns a copy of muts() in a Vector; this allows you to modify the Externals while iterating.
Definition world.h:202
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
const Driver & driver() const
Definition world.h:93
Sym sym(std::string_view)
Definition world.cpp:105
const Externals & externals() const
Definition world.h:282
Definition ast.h:14
void optimize(World &)
Runs _compile or _default_compile, if available (in this order).
Definition optimize.cpp:8