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#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 world.sym("_core_compile"),
14 world.sym("_fallback_compile")
15 };
16 // clang-format on
17
18 const Def* compilation = nullptr;
19 for (auto compilation_function : compilation_functions) {
20 if (auto compilation_ = world.external(compilation_function)) {
21 if (!compilation) compilation = compilation_;
22 compilation_->make_internal();
23 }
24 }
25
26 // make all functions `[] -> %compile.Phase` internal
27 for (auto def : world.copy_externals()) {
28 if (auto lam = def->isa<Lam>(); lam && lam->num_doms() == 0) {
29 // TODO use Axm::isa - but rn there is a problem with the rec Pi and plugin deps
30 if (lam->codom()->sym().view() == "%compile.Phase") {
31 if (!compilation) compilation = lam;
32 def->make_internal();
33 }
34 }
35 }
36
37 if (!compilation) world.ELOG("no compilation function found");
38 world.DLOG("compilation using {} : {}", compilation, compilation->type());
39
40 auto body = compilation->as<Lam>()->body();
41 auto callee = App::uncurry_callee(body);
42
43 world.DLOG("Building pipeline");
44 if (auto f = world.driver().stage(callee->flags())) {
45 auto stage = (*f)(world);
46 auto phase = stage.get()->as<Phase>();
47 if (auto app = body->isa<App>()) phase->apply(app);
48 phase->run();
49 } else
50 world.ELOG("axm not found in passes");
51}
52
53} // namespace mim
const Def * uncurry_callee() const
Definition lam.h:328
Base class for all Defs.
Definition def.h:251
void make_internal()
Definition def.cpp:571
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:447
auto stage(flags_t flags)
Definition driver.h:80
A function.
Definition lam.h:111
As opposed to a Pass, a Phase does one thing at a time and does not mix with other Phases.
Definition phase.h:26
virtual void apply(const App *)
Invoked if your Stage has additional args.
Definition pass.h:37
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:32
const Driver & driver() const
Definition world.h:87
Sym sym(std::string_view)
Definition world.cpp:76
Def * external(Sym name)
Lookup by name.
Definition world.h:205
Vector< Def * > copy_externals() const
Definition world.h:207
Definition ast.h:14
void optimize(World &)
Definition optimize.cpp:8