MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
lower_get_set.cpp
Go to the documentation of this file.
2
3#include <mim/def.h>
4#include <mim/lam.h>
5
6#include <mim/util/types.h>
7
9
11
12const Def* LowerGetSet::lower_get(const App* app) {
13 auto& w = new_world();
14 auto [T, r, s] = rewrite(app->callee())->as<App>()->args<3>();
15 auto [index, arr] = rewrite(app->arg())->projs<2>();
16
17 log().d("lower get: arr = {}: {}, index = {}, T = {}, r = {}, s = {}", arr, arr->type(), index, T, r, s);
18
19 auto r_nat = Lit::isa<u64>(r);
20 if (!r_nat) {
21 log().w("rank {} of {} is not known at lowering time", r, app);
22 return nullptr;
23 }
24
25 for (auto ri = 0_u64; ri != *r_nat; ++ri)
26 arr = w.extract(arr, index->proj(*r_nat, ri));
27 return arr;
28}
29
30const Def* LowerGetSet::lower_set(const App* app) {
31 auto& w = new_world();
32 auto [T, r, s] = rewrite(app->callee())->as<App>()->args<3>();
33 auto [index, arr, x] = rewrite(app->arg())->projs<3>();
34
35 log().d("lower set: arr = {}: {}, index = {}, x = {}: {}, T = {}, r = {}, s = {}", arr, arr->type(), index, x,
36 x->type(), T, r, s);
37
38 auto r_nat = Lit::isa<u64>(r);
39 if (!r_nat) {
40 log().w("rank {} of {} is not known at lowering time", r, app);
41 return nullptr;
42 }
43
44 // The sub-array each level inserts into; `nested[0]` is `arr` itself (rank 0 is normalized away).
45 DefVec nested(*r_nat);
46 nested[0] = arr;
47 for (auto ri = 1_u64; ri != *r_nat; ++ri)
48 nested[ri] = w.extract(nested[ri - 1], index->proj(*r_nat, ri - 1));
49
50 for (auto ri = *r_nat; ri-- != 0;)
51 x = w.insert(nested[ri], index->proj(*r_nat, ri), x);
52 return x;
53}
54
56 if (auto get = Axm::isa<tensor::get>(app)) {
57 if (auto res = lower_get(get)) return res;
58 } else if (auto set = Axm::isa<tensor::set>(app)) {
59 if (auto res = lower_set(set)) return res;
60 }
61 return RWPhase::rewrite_imm_App(app);
62}
63
64} // namespace mim::plug::tensor::phase
static auto isa(const Def *def)
Definition axm.h:112
Base class for all Defs.
Definition def.h:273
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
Definition def.h:440
static std::optional< T > isa(const Def *def)
Definition def.h:937
const fe::Log & log() const
Definition phase.h:79
const fe::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:452
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:55
const Def * rewrite_imm_App(const App *) final
fe::Vector< const Def * > DefVec
Definition def.h:93
@ App
Definition def.h:122