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
6#include "mim/phase/phase.h"
7
8namespace mim {
9
10void optimize(World& world) {
11 auto compilation_functions = {world.sym("_compile"), world.sym("_default_compile"), world.sym("_core_compile"),
12 world.sym("_fallback_compile")};
13 const Def* compilation = nullptr;
14 for (auto compilation_function : compilation_functions) {
15 if (auto compilation_ = world.external(compilation_function)) {
16 if (!compilation) compilation = compilation_;
17 compilation_->make_internal();
18 }
19 }
20
21 // make all functions `[] -> Pipeline` internal
22 auto externals = world.externals(); // copy
23 for (auto [_, def] : externals) {
24 if (auto lam = def->isa<Lam>(); lam && lam->num_doms() == 0) {
25 if (lam->codom()->sym().view() == "%compile.Pipeline") {
26 if (!compilation) compilation = lam;
27 def->make_internal();
28 }
29 }
30 }
31 assert(compilation && "no compilation function found");
32
33 // We found a compilation directive in the file and use it to build the compilation pipeline.
34 // The general idea is that passes and phases are exposed as axioms.
35 // Each pass/phase axiom is associated with a handler function operating on the PipelineBuilder in the
36 // passes map. This registering is analogous to the normalizers (`code -> code`) but only operated using
37 // side effects that change the pipeline.
38 world.DLOG("compilation using {} : {}", compilation, compilation->type());
39
40 // We can not directly access compile axioms here.
41 // But the compile plugin has not the necessary communication pipeline.
42 // Therefore, we register the handlers and let the compile plugin call them.
43
44 PipelineBuilder pipe_builder(world);
45 auto pipeline = compilation->as<Lam>()->body();
46 auto [ax, phases] = collect_args(pipeline);
47
48 // handle pipeline like all other pass axioms
49 auto pipeline_axiom = ax->as<Axiom>();
50 auto pipeline_flags = pipeline_axiom->flags();
51 world.DLOG("Building pipeline");
52 if (auto pass = world.driver().pass(pipeline_flags))
53 (*pass)(world, pipe_builder, pipeline);
54 else
55 error("pipeline_axiom not found in passes");
56
57 world.DLOG("Executing pipeline");
58 pipe_builder.run_pipeline();
59
60 return;
61}
62
63} // namespace mim
Base class for all Defs.
Definition def.h:220
void make_internal()
Definition def.cpp:511
const Def * type() const
Definition def.h:245
flags_t flags() const
Definition def.h:235
auto pass(flags_t flags)
Definition driver.h:73
A function.
Definition lam.h:96
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
const Driver & driver() const
Definition world.h:81
const auto & externals() const
Definition world.h:168
Sym sym(std::string_view)
Definition world.cpp:77
Def * external(Sym name)
Lookup by name.
Definition world.h:182
Definition cfg.h:11
void optimize(World &)
Definition optimize.cpp:10
void error(Loc loc, const char *f, Args &&... args)
Definition dbg.h:122
std::pair< const Def *, std::vector< const Def * > > collect_args(const Def *def)
Helper function to cope with the fact that normalizers take all arguments and not only its axiom argu...
Definition lam.cpp:86