MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
lower.cpp
Go to the documentation of this file.
2
3#include "mim/def.h"
4#include "mim/lam.h"
5
7
9
10const Def* Lower::lower_via_impl(const App* app, const Def* impl_annex) {
11 auto& w = new_world();
12
13 // Walk the curry chain (innermost App outermost in syntax) to collect the args
14 // in the order they were applied.
16 const Def* head = app;
17 while (auto h = head->isa<App>()) {
18 args.push_back(rewrite(h->arg()));
19 head = h->callee();
20 }
21 std::reverse(args.begin(), args.end());
22
23 auto impl = impl_annex;
24 for (auto a : args)
25 impl = w.app(impl, a);
26
27 // The `_impl` is a `lam`, so applying it triggers beta-reduction. Each `_impl`
28 // body references the `_impl` variants of its dependencies directly, so the
29 // chain bottoms out at the low-level axioms (`map_reduce`, …) in one go.
30 return impl;
31}
32
33const Def* Lower::rewrite_imm_App(const App* app) {
34 auto& w = new_world();
35
37 return lower_via_impl(app, w.annex<tensor::broadcast_in_dim_impl>());
38 else if (Axm::isa<tensor::product_2d>(app))
39 return lower_via_impl(app, w.annex<tensor::product_2d_impl>());
40 else if (Axm::isa<tensor::bmm>(app))
41 return lower_via_impl(app, w.annex<tensor::bmm_impl>());
43 return lower_via_impl(app, w.annex<tensor::dot_product_impl>());
44 else if (Axm::isa<tensor::transpose>(app))
45 return lower_via_impl(app, w.annex<tensor::transpose_impl>());
47 return lower_via_impl(app, w.annex<tensor::transpose_2d_impl>());
48 else if (Axm::isa<tensor::map>(app))
49 return lower_via_impl(app, w.annex<tensor::map_impl>());
50 else if (Axm::isa<tensor::unary>(app))
51 return lower_via_impl(app, w.annex<tensor::unary_impl>());
52 else if (Axm::isa<tensor::binary>(app))
53 return lower_via_impl(app, w.annex<tensor::binary_impl>());
54 else if (Axm::isa<tensor::select>(app))
55 return lower_via_impl(app, w.annex<tensor::select_impl>());
56 else if (Axm::isa<tensor::repeat>(app))
57 return lower_via_impl(app, w.annex<tensor::repeat_impl>());
58 else if (Axm::isa<tensor::reshape>(app))
59 return lower_via_impl(app, w.annex<tensor::reshape_impl>());
60 else if (Axm::isa<tensor::slice>(app))
61 return lower_via_impl(app, w.annex<tensor::slice_impl>());
62 else if (Axm::isa<tensor::flip>(app))
63 return lower_via_impl(app, w.annex<tensor::flip_impl>());
64 else if (Axm::isa<tensor::conv>(app))
65 return lower_via_impl(app, w.annex<tensor::conv_impl>());
66 else if (Axm::isa<tensor::pool>(app))
67 return lower_via_impl(app, w.annex<tensor::pool_impl>());
68 return RWPhase::rewrite_imm_App(app);
69}
70
71} // namespace mim::plug::tensor::phase
static auto isa(const Def *def)
Definition axm.h:107
Base class for all Defs.
Definition def.h:261
const Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition phase.cpp:23
World & new_world()
Create new Defs into this.
Definition phase.h:368
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:56
const Def * rewrite_imm_App(const App *) final
Definition lower.cpp:33
Vector< const Def * > DefVec
Definition def.h:79
@ App
Definition def.h:109