Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
compile.cpp
Go to the documentation of this file.
2
3#include <thorin/config.h>
4
9#include <thorin/pass/pass.h>
14
17
18using namespace thorin;
19using namespace thorin::plug;
20
21void add_phases(Defs phases, World& world, Passes& passes, PipelineBuilder& builder) {
22 for (auto phase : phases) compile::handle_optimization_part(phase, world, passes, builder);
23}
24
25void add_passes(World& world, PipelineBuilder& builder, Passes& passes, DefVec& pass_list) {
26 // Concept: We create a convention that passes register in the pipeline using append_**in**_last.
27 // This pass then calls the registered passes in the order they were registered in the last phase.
28
29 // We create a new dummy phase in which the passes should be inserted.
30 // builder.append_phase_end([](Pipeline&) {});
31 builder.begin_pass_phase();
32 for (auto pass : pass_list) compile::handle_optimization_part(pass, world, passes, builder);
33 builder.end_pass_phase();
34}
35
37 return {"compile", [](Normalizers& normalizers) { compile::register_normalizers(normalizers); },
38 [](Passes& passes) {
40 assert_emplace(passes, debug_phase_flag, [](World& world, PipelineBuilder& builder, const Def* app) {
41 world.DLOG("Generate debug_phase: {}", app);
42 int level = (int)(app->as<App>()->arg(0)->as<Lit>()->get<u64>());
43 world.DLOG(" Level: {}", level);
44 builder.add_phase<compile::DebugPrint>(level);
45 });
46
48 [&](World& world, PipelineBuilder& builder, const Def* app) {
49 auto pass_array = app->as<App>()->arg()->projs();
50 DefVec pass_list;
51 for (auto pass : pass_array) pass_list.push_back(pass);
52 add_passes(world, builder, passes, pass_list);
53 });
54
56 [&](World& world, PipelineBuilder& builder, const Def* app) {
57 auto phase_array = app->as<App>()->arg()->projs();
58 DefVec phase_list;
59 for (auto phase : phase_array) phase_list.push_back(phase);
60 add_phases(phase_list, world, passes, builder);
61 });
62
64 [&](World& world, PipelineBuilder& builder, const Def* app) {
65 auto [ax, phases] = collect_args(app);
66 add_phases(phases, world, passes, builder);
67 });
70 [](World&, PipelineBuilder& builder, const Def* def) { builder.def2pass(def, nullptr); });
71
72 register_pass<compile::beta_red_pass, BetaRed>(passes);
73 register_pass<compile::eta_red_pass, EtaRed>(passes);
74
75 register_pass<compile::lam_spec_pass, LamSpec>(passes);
76 register_pass<compile::ret_wrap_pass, RetWrap>(passes);
77 register_pass<compile::internal_cleanup_pass, compile::InternalCleanup>(passes);
78
79 register_pass_with_arg<compile::eta_exp_pass, EtaExp, EtaRed>(passes);
80 register_pass_with_arg<compile::scalerize_pass, Scalerize, EtaExp>(passes);
81 register_pass_with_arg<compile::tail_rec_elim_pass, TailRecElim, EtaRed>(passes);
82 },
83 nullptr};
84}
const Def * arg() const
Definition lam.h:215
Base class for all Defs.
Definition def.h:222
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == -1_n) or std::array (otherwise).
Definition def.h:369
T get() const
Definition def.h:714
void add_phase(Args &&... args)
void def2pass(const Def *, Pass *p)
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
void add_phases(Defs phases, World &world, Passes &passes, PipelineBuilder &builder)
Definition compile.cpp:21
void add_passes(World &world, PipelineBuilder &builder, Passes &passes, DefVec &pass_list)
Definition compile.cpp:25
#define THORIN_EXPORT
Definition config.h:16
void register_normalizers(Normalizers &normalizers)
void handle_optimization_part(const Def *part, World &world, Passes &passes, PipelineBuilder &builder)
Definition compile.h:10
Definition cfg.h:11
uint64_t u64
Definition types.h:35
absl::flat_hash_map< flags_t, std::function< void(World &, PipelineBuilder &, const Def *)> > Passes
axiom ↦ (pipeline part) × (axiom application) → () The function should inspect Application to const...
Definition plugin.h:22
THORIN_EXPORT thorin::Plugin thorin_get_plugin()
To be implemented and exported by a plugin.
absl::flat_hash_map< flags_t, NormalizeFn > Normalizers
Definition plugin.h:19
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
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
Definition util.h:102
u64 flags_t
Definition types.h:46
static constexpr flags_t Base
Definition plugin.h:131
Basic info and registration function pointer to be returned from a specific plugin.
Definition plugin.h:29