MimIR
0.4-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
Loading...
Searching...
No Matches
lower_get_set.cpp
Go to the documentation of this file.
1
#include "
mim/plug/tensor/phase/lower_get_set.h
"
2
3
#include <
mim/def.h
>
4
#include <
mim/lam.h
>
5
6
#include <
mim/util/types.h
>
7
8
#include "
mim/plug/tensor/tensor.h
"
9
10
namespace
mim::plug::tensor::phase
{
11
12
const
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
30
const
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
55
const
Def
*
LowerGetSet::rewrite_imm_App
(
const
App
* app) {
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
mim::App
Definition
lam.h:224
mim::Axm::isa
static auto isa(const Def *def)
Definition
axm.h:112
mim::Def
Base class for all Defs.
Definition
def.h:273
mim::Def::projs
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
mim::Lit::isa
static std::optional< T > isa(const Def *def)
Definition
def.h:937
mim::Phase::log
const fe::Log & log() const
Definition
phase.h:79
mim::Phase::args
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition
phase.cpp:23
mim::RWPhase::new_world
World & new_world()
Create new Defs into this.
Definition
phase.h:452
mim::Rewriter::rewrite
virtual const Def * rewrite(const Def *)
Definition
rewrite.cpp:55
mim::plug::tensor::phase::LowerGetSet::rewrite_imm_App
const Def * rewrite_imm_App(const App *) final
Definition
lower_get_set.cpp:55
def.h
lam.h
lower_get_set.h
mim::plug::core::conv::s
@ s
Definition
autogen.h:253
mim::plug::math::round::r
@ r
Definition
autogen.h:298
mim::plug::regex::cls::w
@ w
Definition
autogen.h:63
mim::plug::tensor::phase
Definition
constraints.h:5
mim::plug::tensor::set
set
Definition
autogen.h:36
mim::plug::tensor::get
get
Definition
autogen.h:28
mim::DefVec
fe::Vector< const Def * > DefVec
Definition
def.h:93
mim::Node::App
@ App
Definition
def.h:122
tensor.h
types.h
src
mim
plug
tensor
phase
lower_get_set.cpp
Generated by
1.18.0