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 // FIXME unsound: ignores any buffer.write through this handle, including one in a callee.
34 if (auto ex = buf->isa<Extract>())
35 if (auto l = Axm::isa<lit>(ex->tuple())) {
36 auto [cmem, v] = l->arg()->projs<2>();
37 return world.tuple({mem, v});
38 }
39 return {};
40}
41
42const Def* normalize_write(const Def*, const Def*, const Def*) { return {}; }
43
44/// `shape (r, s, T) (buf, i)` ↦ `s#i`; the logical shape, since the type of `buf` has its size-1 axes folded away.
45const Def* normalize_shape(const Def* type, const Def* callee, const Def* arg) {
46 auto index = arg->proj(2, 1);
47 auto [r, s, T] = callee->as<App>()->args<3>();
48 return type->world().extract(s, index);
49}
50
52
53} // namespace mim::plug::buffer
#define MIM_buffer_NORMALIZER_IMPL
Definition autogen.h:88
static auto isa(const Def *def)
Definition axm.h:112
Base class for all Defs.
Definition def.h:289
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
Definition def.cpp:647
World & world() const noexcept
Definition def.h:1105
auto projs(Projector auto f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
Definition def.h:457
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:360
static std::optional< T > isa(const Def *def)
Definition def.h:976
The buffer Plugin
Definition buffer.h:8
const Def * normalize_write(const Def *, const Def *, const Def *)
const Def * normalize_read(const Def *type, const Def *, const Def *arg)
read (constant v) idx ↦ v.
const Def * normalize_shape(const Def *type, const Def *callee, const Def *arg)
shape (r, s, T) (buf, i) ↦ s#i; the logical shape, since the type of buf has its size-1 axes folded a...
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
fe::Vector< const Def * > DefVec
Definition def.h:98
uint64_t u64
Definition types.h:27