MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
lower_matrix_highlevel.cpp
Go to the documentation of this file.
2
3#include <mim/lam.h>
4
5#include <mim/plug/cps/cps.h>
6
8
10
11namespace {
12
13/// Maps a high-level matrix axm to the `map_reduce_*` unfolding function that implements it.
14std::optional<const Def*>
15internal_function_of_axm(World& world, const Axm* axm, const Def* meta_args, const Def* args) {
16 // clang-format off
17 static const absl::flat_hash_map<flags_t, flags_t> axm_to_impl = {
21 };
22 // clang-format on
23 if (auto it = axm_to_impl.find(axm->flags()); it != axm_to_impl.end()) {
24 auto spec_fun = world.implicit_app(world.annexes().flags2entry().at(it->second).def, meta_args);
25 return world.app(cps::op_cps2ds_dep(spec_fun), args);
26 }
27 return std::nullopt;
28}
29
30} // namespace
31
33 if (is_bootstrapping()) return RWPhase::rewrite_imm_App(app);
34 auto& w = new_world();
35
36 if (auto mat_ax = Axm::isa<matrix::prod>(app)) {
37 auto [m, k, l, width] = mat_ax->decurry()->args<4>();
38 auto w_lit = Lit::isa(width);
39
40 auto ext_fun = old_world().externals()[old_world().sym("extern_matrix_prod")];
41 if (ext_fun && (w_lit && *w_lit == 64)) {
42 auto [mem, M, N] = mat_ax->args<3>([this](const Def* def) { return rewrite(def); });
43 auto ds_fun = cps::op_cps2ds_dep(rewrite(ext_fun));
44 return w.app(ds_fun, {mem, rewrite(m), rewrite(k), rewrite(l), M, N});
45 }
46 }
47
48 if (auto inner_app = app->callee()->isa<App>()) {
49 if (auto axm = inner_app->callee()->isa<Axm>()) {
50 auto new_meta_args = rewrite(inner_app->arg());
51 auto new_args = rewrite(app->arg());
52 if (auto internal_function = internal_function_of_axm(w, axm, new_meta_args, new_args))
53 return *internal_function;
54 }
55 }
56
57 return RWPhase::rewrite_imm_App(app);
58}
59
60} // namespace mim::plug::matrix::phase
const Def * callee() const
Definition lam.h:276
const Def * arg() const
Definition lam.h:285
Definition axm.h:9
static auto isa(const Def *def)
Definition axm.h:107
Base class for all Defs.
Definition def.h:261
static std::optional< T > isa(const Def *def)
Definition def.h:878
World & new_world()
Create new Defs into this.
Definition phase.h:368
bool is_bootstrapping() const
Returns whether we are currently bootstrapping (rewriting annexes).
Definition phase.h:356
World & old_world()
Get old Defs from here.
Definition phase.h:367
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:56
Sym sym(std::string_view)
Definition world.cpp:105
const Externals & externals() const
Definition world.h:264
const Def * op_cps2ds_dep(const Def *k)
Definition cps.h:16
The mem Plugin
Definition mem.h:11
u64 flags_t
Definition types.h:39
@ Axm
Definition def.h:109
static constexpr flags_t Base
Definition plugin.h:148