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 for (auto def : world.copy_externals()) {
23 if (auto lam = def->isa<Lam>(); lam && lam->num_doms() == 0) {
24 if (lam->codom()->sym().view() == "%compile.Pipeline") {
25 if (!compilation) compilation = lam;
26 def->make_internal();
27 }
28 }
29 }
30 assert(compilation && "no compilation function found");
31
32 // We found a compilation directive in the file and use it to build the compilation pipeline.
33 // The general idea is that passes and phases are exposed as axioms.
34 // Each pass/phase axiom is associated with a handler function operating on the PipelineBuilder in the
35 // passes map. This registering is analogous to the normalizers (`code -> code`) but only operated using
36 // side effects that change the pipeline.
37 world.DLOG("compilation using {} : {}", compilation, compilation->type());
38
39 // We can not directly access compile axioms here.
40 // But the compile plugin has not the necessary communication pipeline.
41 // Therefore, we register the handlers and let the compile plugin call them.
42
43 PipelineBuilder pipe_builder(world);
44 auto pipeline = compilation->as<Lam>()->body();
45 auto [ax, phases] = collect_args(pipeline);
46
47 // handle pipeline like all other pass axioms
48 auto pipeline_axiom = ax->as<Axiom>();
49 auto pipeline_flags = pipeline_axiom->flags();
50 world.DLOG("Building pipeline");
51 if (auto pass = world.driver().pass(pipeline_flags))
52 (*pass)(world, pipe_builder, pipeline);
53 else
54 error("pipeline_axiom not found in passes");
55
56 world.DLOG("Executing pipeline");
57 pipe_builder.run_pipeline();
58
59 return;
60}
61
62} // namespace mim
Base class for all Defs.
Definition def.h:212
void make_internal()
Definition def.cpp:478
constexpr flags_t flags() const noexcept
Definition def.h:230
Ref type() const noexcept
Yields the raw type of this Def, i.e. maybe nullptr.
Definition def.h:241
auto pass(flags_t flags)
Definition driver.h:75
A function.
Definition lam.h:105
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
const Driver & driver() const
Definition world.h:80
Sym sym(std::string_view)
Definition world.cpp:76
Def * external(Sym name)
Lookup by name.
Definition world.h:191
Vector< Def * > copy_externals() const
Definition world.h:193
Definition ast.h:14
std::pair< Ref, DefVec > collect_args(Ref def)
Helper function to cope with the fact that normalizers take all arguments and not only its axiom argu...
Definition lam.cpp:86
void optimize(World &)
Definition optimize.cpp:10
void error(Loc loc, const char *f, Args &&... args)
Definition dbg.h:122