Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
optimize.cpp
Go to the documentation of this file.
2
3#include <vector>
4
5#include "thorin/driver.h"
6
15#include "thorin/phase/phase.h"
16
17namespace thorin {
18
19void optimize(World& world) {
20 auto compilation_functions = {world.sym("_compile"), world.sym("_default_compile"), world.sym("_core_compile"),
21 world.sym("_fallback_compile")};
22 const Def* compilation = nullptr;
23 for (auto compilation_function : compilation_functions) {
24 if (auto compilation_ = world.external(compilation_function)) {
25 if (!compilation) compilation = compilation_;
26 compilation_->make_internal();
27 }
28 }
29 // make all functions `[] -> Pipeline` internal
30 auto externals = world.externals(); // copy
31 for (auto [_, def] : externals) {
32 if (auto lam = def->isa<Lam>(); lam && lam->num_doms() == 0) {
33 if (lam->codom()->sym().view() == "%compile.Pipeline") {
34 if (!compilation) compilation = lam;
35 def->make_internal();
36 }
37 }
38 }
39 assert(compilation && "no compilation function found");
40
41 // We found a compilation directive in the file and use it to build the compilation pipeline.
42 // The general idea is that passes and phases are exposed as axioms.
43 // Each pass/phase axiom is associated with a handler function operating on the PipelineBuilder in the
44 // passes map. This registering is analogous to the normalizers (`code -> code`) but only operated using
45 // side effects that change the pipeline.
46 world.DLOG("compilation using {} : {}", compilation, compilation->type());
47
48 // We can not directly access compile axioms here.
49 // But the compile plugin has not the necessary communication pipeline.
50 // Therefore, we register the handlers and let the compile plugin call them.
51
52 PipelineBuilder pipe_builder(world);
53 auto pipeline = compilation->as<Lam>()->body();
54 auto [ax, phases] = collect_args(pipeline);
55
56 // handle pipeline like all other pass axioms
57 auto pipeline_axiom = ax->as<Axiom>();
58 auto pipeline_flags = pipeline_axiom->flags();
59 world.DLOG("Building pipeline");
60 if (auto pass = world.driver().pass(pipeline_flags))
61 (*pass)(world, pipe_builder, pipeline);
62 else
63 error("pipeline_axiom not found in passes");
64
65 world.DLOG("Executing pipeline");
66 pipe_builder.run_pipeline();
67
68 return;
69}
70
71} // namespace thorin
Base class for all Defs.
Definition def.h:222
const Def * type() const
Yields the raw type of this Def, i.e. maybe nullptr.
Definition def.h:248
void make_internal()
Definition def.cpp:522
flags_t flags() const
Definition def.h:237
auto pass(flags_t flags)
Definition driver.h:73
A function.
Definition lam.h:97
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
const auto & externals() const
Definition world.h:150
Sym sym(std::string_view)
Definition world.cpp:77
const Driver & driver() const
Definition world.h:83
Def * external(Sym name)
Lookup by name.
Definition world.h:164
Definition cfg.h:11
void optimize(World &)
Definition optimize.cpp:19
void error(const Def *def, const char *fmt, Args &&... args)
Definition def.h:622
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