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
11#include <mim/phase/eta_conv.h>
12#include <mim/phase/lam_spec.h>
14#include <mim/phase/ret_wrap.h>
15#include <mim/phase/scalarize.h>
17
19
20using namespace mim;
21using namespace mim::plug;
22
23/// Phase hook for `%compile.named`.
24/// Reads the fully-qualified annex name (e.g. `"clos.clos_conv"`) from the driving App at phase-build time,
25/// looks up the matching annex `Def` in the current `World`, and *redirects* Phase::create to that annex's own
26/// Phase. If the plugin part of the name is not loaded or the annex is missing, it elides (resolves to nothing),
27/// so the enclosing `%compile.phases` simply skips it.
28class Named : public Phase {
29public:
31 : Phase(w, a) {}
32
33 void start() final { fe::unreachable(); } // a Named always redirects and never runs itself
34
35 void apply(const App* app) final {
36 if (!app) return;
37 auto str = tuple2str(app->arg());
38 if (str.empty()) return;
39
40 auto dot = str.find('.');
41 if (dot == std::string::npos) return;
42 auto begin = str[0] == '%' ? 1uz : 0uz; // skip the leading '%' of the annex name
43 if (!driver().is_loaded(driver().sym(str.substr(begin, dot - begin)))) return;
44
45 if (auto def = world().annex(driver().sym(str))) resolved_ = Phase::create(driver().phases(), def);
46 }
47
48 bool redirects() const override { return true; }
49 std::unique_ptr<Phase> take_resolved() override { return std::move(resolved_); }
50
51private:
52 std::unique_ptr<Phase> resolved_;
53};
54
71
void reg_phases(Flags2Phases &phases)
Definition affine.cpp:12
void apply(const App *app) final
Invoked if your Phase has additional args.
Definition compile.cpp:35
std::unique_ptr< Phase > take_resolved() override
The Phase to use instead; nullptr means elide.
Definition compile.cpp:49
void start() final
Actual entry.
Definition compile.cpp:33
bool redirects() const override
If true, Phase::create uses take_resolved().
Definition compile.cpp:48
Named(World &w, flags_t a)
Definition compile.cpp:30
static void hook(Flags2Phases &phases)
Definition phase.h:70
flags_t annex() const
Definition phase.h:81
Phase(World &world, std::string name)
Definition phase.h:29
static std::unique_ptr< Phase > create(const Flags2Phases &phases, const Def *def)
Definition phase.h:50
Driver & driver()
Definition phase.h:78
World & world()
Definition phase.h:77
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
void reg_phases(Flags2Phases &phases)
Definition compile.cpp:55
#define MIM_EXPORT
Definition config.h:19
void register_normalizers(Normalizers &normalizers)
Definition ast.h:14
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
Definition util.h:117
u64 flags_t
Definition types.h:39
absl::flat_hash_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:25
std::string tuple2str(const Def *)
Definition tuple.cpp:57
mim::Plugin mim_get_plugin()
#define MIM_VERSION
Definition plugin.h:54
static consteval flags_t base()
Definition plugin.h:150
Basic info and registration function pointer to be returned from a specific plugin.
Definition plugin.h:59