MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
normalizers.cpp
Go to the documentation of this file.
1#include <mim/axm.h>
2#include <mim/tuple.h>
3#include <mim/world.h>
4
6
7namespace mim::plug::buffer {
8
9/// `Buf (r, s, T)` with literal size-1 axes in `s` ↦ `Buf (r', s', T)` with those axes dropped.
10/// This mirrors the folding of the corresponding array types (`«3, 1; T»` ≡ `«3; T»`), so buffer types
11/// derived from logical shapes agree with the (folded) boundary types by construction.
12const Def* normalize_Buf(const Def*, const Def* callee, const Def* arg) {
13 auto& world = arg->world();
14 auto [r, s, T] = arg->projs<3>();
15 auto r_l = Lit::isa<u64>(r);
16 if (!r_l) return {};
17
18 DefVec dims;
19 dims.reserve(*r_l);
20 for (u64 i = 0; i < *r_l; ++i) {
21 auto d = s->proj(*r_l, i);
22 if (auto l = Lit::isa<u64>(d); l && *l == 1) continue;
23 dims.push_back(d);
24 }
25 if (dims.size() == *r_l) return {};
26 return world.app(callee, world.tuple({world.lit_nat(dims.size()), world.tuple(dims), T}));
27}
28
29/// `read (constant v) idx` ↦ `v`.
30const Def* normalize_read(const Def* type, const Def*, const Def* arg) {
31 auto& world = type->world();
32 auto [mem, buf, index] = arg->projs<3>();
33 if (auto ex = buf->isa<Extract>())
34 if (auto cst = Axm::isa<constant>(ex->tuple())) {
35 auto [cmem, v] = cst->arg()->projs<2>();
36 return world.tuple({mem, v});
37 }
38 return {};
39}
40
41const Def* normalize_write(const Def*, const Def*, const Def*) { return {}; }
42
43/// `shape buf i` ↦ the `i`-th size, read off the buffer's type.
44const Def* normalize_shape(const Def* type, const Def*, const Def* arg) {
45 auto& world = type->world();
46 auto [buf, index] = arg->projs<2>();
47 auto [r, s, T] = Axm::isa<Buf, false>(buf->type())->args<3>();
48 return world.extract(s, index);
49}
50
52
53} // namespace mim::plug::buffer
#define MIM_buffer_NORMALIZER_IMPL
Definition autogen.h:81
static auto isa(const Def *def)
Definition axm.h:107
Base class for all Defs.
Definition def.h:261
World & world() const noexcept
Definition def.cpp:483
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:402
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:210
static std::optional< T > isa(const Def *def)
Definition def.h:878
The buffer Plugin
Definition buffer.h:7
const Def * normalize_write(const Def *, const Def *, const Def *)
const Def * normalize_shape(const Def *type, const Def *, const Def *arg)
shape buf i ↦ the i-th size, read off the buffer's type.
const Def * normalize_read(const Def *type, const Def *, const Def *arg)
read (constant v) idx ↦ v.
const Def * normalize_Buf(const Def *, const Def *callee, const Def *arg)
Buf (r, s, T) with literal size-1 axes in s ↦ Buf (r', s', T) with those axes dropped.
The mem Plugin
Definition mem.h:11
Vector< const Def * > DefVec
Definition def.h:79
uint64_t u64
Definition types.h:27