MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
compile.cpp
Go to the documentation of this file.
2
3#include <memory>
4
5#include <mim/config.h>
6#include <mim/driver.h>
7#include <mim/phase.h>
8
9#include <mim/phase/adce.h>
10#include <mim/phase/beta_red.h>
12#include <mim/phase/eta_conv.h>
13#include <mim/phase/lam_spec.h>
15#include <mim/phase/ret_wrap.h>
16#include <mim/phase/scalarize.h>
17#include <mim/phase/sccp.h>
20
22
23using namespace mim;
24using namespace mim::plug;
25
26/// Phase hook for `compile.named`.
27/// Reads the fully-qualified annex name (e.g. `"clos.clos_conv"`) from the driving App at phase-build time,
28/// looks up the matching annex `Def` in the current `World`, and *redirects* Phase::create to that annex's own
29/// Phase. If the plugin part of the name is not loaded or the annex is missing, it elides (resolves to nothing),
30/// so the enclosing `compile.phases` simply skips it.
31class Named : public Phase {
32public:
34 : Phase(w, a) {}
35
36 void start() final { std::unreachable(); } // a Named always redirects and never runs itself
37
38 void apply(const App* app) final {
39 if (!app) return;
40 auto str = tuple2str(app->arg());
41 if (str.empty()) return;
42
43 auto dot = str.find('.');
44 if (dot == std::string::npos) return;
45 if (!driver().is_loaded(std::string_view(str).substr(0, dot))) return;
46
47 if (auto def = world().annex(driver().sym(str))) resolved_ = Phase::create(driver().phases(), def);
48 }
49
50 bool redirects() const override { return true; }
51 std::unique_ptr<Phase> take_resolved() override { return std::move(resolved_); }
52
53private:
54 std::unique_ptr<Phase> resolved_;
55};
56
76
77// clang-format off
78static constexpr PluginArg known_args[] = {
79 {"aggr", "Forces `compile.aggr` to `tt`, switching fixed-point iteration of the `opt` pipeline's `optimize` stage on (off by default)."},
80 {"no-aggr", "Forces `compile.aggr` to `ff`, switching that fixed-point iteration off."},
81};
82// clang-format on
83
85 plugin.register_normalizers = compile::register_normalizers;
86 plugin.register_phases = reg_phases;
87 plugin.args = known_args;
88 plugin.num_args = std::size(known_args);
89}
void apply(const App *app) final
Invoked if your Phase has additional args.
Definition compile.cpp:38
std::unique_ptr< Phase > take_resolved() override
The Phase to use instead; nullptr means elide.
Definition compile.cpp:51
void start() final
Actual entry.
Definition compile.cpp:36
bool redirects() const override
If true, Phase::create uses take_resolved().
Definition compile.cpp:50
Named(World &w, flags_t a)
Definition compile.cpp:33
static void hook(Flags2Phases &phases)
Definition phase.h:51
flags_t annex() const
Definition phase.h:62
Phase(World &world, std::string name)
Definition phase.h:27
Driver & driver()
Definition phase.h:59
static std::unique_ptr< Phase > create(const Flags2Phases &phases, const Def *def)
Definition phase.cpp:25
World & world()
Definition phase.h:58
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
static void reg_phases(Flags2Phases &phases)
Definition compile.cpp:57
static constexpr PluginArg known_args[]
Definition compile.cpp:78
The compile Plugin
void register_normalizers(Normalizers &normalizers)
Definition ast.h:16
u64 flags_t
Definition types.h:39
std::string tuple2str(const Def *)
Definition tuple.cpp:115
ankerl::unordered_dense::map< flags_t, std::function< std::unique_ptr< Phase >(World &)> > Flags2Phases
Maps an axiom of a Phase to a function that creates one.
Definition plugin.h:30
One -X <plugin>:<arg> a Plugin understands; see Arguments.
Definition plugin.h:34
#define MIM_PLUGIN_ENTRY(p)
Defines a Plugin's entry point; the body fills in the plugin handed to it, as in MIM_PLUGIN_ENTRY(dem...
Definition plugin.h:196
static consteval flags_t base()
Definition plugin.h:276