MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
affine.h
Go to the documentation of this file.
1#pragma once
2
3#include <mim/lam.h>
4#include <mim/world.h>
5
7
9
11
12/// `affine.For (0, bound, 1, acc)` over a fresh `body: Cn [I64, acc_t, Cn acc_t]` named @p name.
13/// @returns `{body, for_call}`.
14inline std::pair<Lam*, const Def*> counting_for(const Def* bound, const Def* acc, const Def* exit, Sym name) {
15 auto& w = bound->world();
16 auto acc_ty = acc->type();
17 auto body = w.mut_con({/* iter */ w.type_i64(), /* acc */ acc_ty, /* return */ w.cn(acc_ty)})->set(name);
18 auto for_loop = w.call<For>(body, exit, Defs{w.lit_i64(0), bound, w.lit_i64(1), acc});
19 return {body, for_loop};
20}
21
22/// Nests one counting loop per bound of @p dims inside @p cur, threading @p cur, @p exit and @p acc down to
23/// the innermost body; @returns the raw i64 loop counters.
24inline DefVec counting_nest(Lam*& cur, const Def*& exit, const Def*& acc, Defs dims, std::string_view name) {
25 auto& w = cur->world();
26 auto iters = DefVec();
27 iters.reserve(dims.size());
28 for (size_t i = 0, e = dims.size(); i != e; ++i) {
29 auto bound = w.call<core::bitcast>(w.type_i64(), dims[i]);
30 auto [body, for_call] = counting_for(bound, acc, exit, w.sym(std::format("{}_{}", name, i)));
31 auto [iter, new_acc, yield] = body->vars<3>();
32 exit = yield;
33 acc = new_acc;
34 iters.emplace_back(iter);
35 cur->set(true, for_call);
36 cur = body;
37 }
38 return iters;
39}
40
41} // namespace mim::plug::affine
Base class for all Defs.
Definition def.h:289
World & world() const noexcept
Definition def.h:1105
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1119
A function.
Definition lam.h:91
Lam * set(Filter filter, const Def *body)
Definition lam.cpp:34
The affine Plugin
Definition affine.h:10
std::pair< Lam *, const Def * > counting_for(const Def *bound, const Def *acc, const Def *exit, Sym name)
affine.For (0, bound, 1, acc) over a fresh body: Cn [I64, acc_t, Cn acc_t] named name.
Definition affine.h:14
DefVec counting_nest(Lam *&cur, const Def *&exit, const Def *&acc, Defs dims, std::string_view name)
Nests one counting loop per bound of dims inside cur, threading cur, exit and acc down to the innermo...
Definition affine.h:24
fe::View< const Def * > Defs
Definition def.h:96
fe::Vector< const Def * > DefVec
Definition def.h:98