MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
lower_ptr.cpp
Go to the documentation of this file.
2
3#include <mim/axm.h>
4#include <mim/def.h>
5
6#include <mim/plug/gpu/gpu.h>
7#include <mim/plug/mem/mem.h>
8
10
11namespace mim::plug::buffer {
12
13const Def* LowerPtr::rewrite_imm_App(const App* app) {
14 auto& w = new_world();
15
16 if (auto buf_ax = Axm::isa<buffer::Buf>(app)) {
17 auto [r, s, T] = buf_ax->args<3>();
18 s = rewrite(s);
19 T = rewrite(T);
20 return w.call<mem::Ptr>(Defs{w.arr(s, T), w.lit_nat_0()});
21 } else if (auto alloc_ax = Axm::isa<buffer::alloc>(app)) {
22 auto mem = rewrite(alloc_ax->arg());
23 auto [r, s, T] = alloc_ax->callee()->as<App>()->args<3>();
24 s = rewrite(s);
25 T = rewrite(T);
26 auto [mem2, ptr] = mem::op_alloc(w.arr(s, T), mem)->projs<2>();
27 return w.tuple({mem2, ptr});
28 } else if (auto read_ax = Axm::isa<buffer::read>(app)) {
29 auto [mem, buf, idx] = read_ax->args<3>();
30 mem = rewrite(mem);
31 buf = rewrite(buf);
32 idx = rewrite(idx);
33 auto element_ptr = mem::op_lea_tuple(buf, idx);
34 auto [mem2, val] = w.call<mem::load>(Defs{mem, element_ptr})->projs<2>();
35 return w.tuple({mem2, val});
36 } else if (auto write_ax = Axm::isa<buffer::write>(app)) {
37 auto [mem, buf, idx, val] = write_ax->args<4>();
38 mem = rewrite(mem);
39 buf = rewrite(buf);
40 idx = rewrite(idx);
41 val = rewrite(val);
42 auto element_ptr = mem::op_lea_tuple(buf, idx);
43 auto mem2 = w.call<mem::store>(Defs{mem, element_ptr, val});
44 return w.tuple({mem2, buf});
45 } else if (auto copy_ax = Axm::isa<buffer::copy>(app)) {
46 auto [mem, dst, src] = copy_ax->args<3>();
47 mem = rewrite(mem);
48 dst = rewrite(dst);
49 src = rewrite(src);
50 // Whole-buffer copy: load the entire array out of `src` and store it into `dst`.
51 auto [mem2, val] = w.call<mem::load>(Defs{mem, src})->projs<2>();
52 return w.call<mem::store>(Defs{mem2, dst, val});
53 } else if (auto free_ax = Axm::isa<buffer::free>(app)) {
54 auto [mem, buf] = free_ax->args<2>([this](const Def* d) { return rewrite(d); });
55 return w.call<mem::free>(Defs{mem, buf});
56 } else if (auto init_ax = Axm::isa<buffer::init>(app)) {
57 auto [mem, val] = init_ax->args<2>();
58 auto [r, s, T] = init_ax->callee()->as<App>()->args<3>();
59 mem = rewrite(mem);
60 val = rewrite(val);
61 s = rewrite(s);
62 T = rewrite(T);
63 auto [mem2, ptr] = mem::op_alloc(w.arr(s, T), mem)->projs<2>();
64 auto mem3 = w.call<mem::store>(Defs{mem2, ptr, val});
65 return w.tuple({mem3, ptr});
66 } else if (auto const_ax = Axm::isa<buffer::lit>(app)) {
67 auto [mem, val] = const_ax->args<2>();
68 auto [r, s, T] = const_ax->callee()->as<App>()->args<3>();
69 mem = rewrite(mem);
70 val = rewrite(val);
71 s = rewrite(s);
72 T = rewrite(T);
73 auto [mem2, ptr] = mem::op_alloc(w.arr(s, T), mem)->projs<2>();
74 auto mem3 = w.call<mem::store>(Defs{mem2, ptr, w.pack(s, val)});
75 return w.tuple({mem3, ptr});
76 } else if (auto buf_alloc_copy = Axm::isa<gpu::buf_alloc_copy>(app)) {
77 auto [m0, m1, ptr] = buf_alloc_copy->args<3>([this](const Def* d) { return rewrite(d); });
78 return w.call(gpu::alloc_copy::block, Defs{m0, m1, ptr});
79 } else if (auto buf_copy_to_host = Axm::isa<gpu::buf_copy_to_host>(app)) {
80 auto [m0, m1, d_ptr, h_ptr] = buf_copy_to_host->args<4>([this](const Def* d) { return rewrite(d); });
81 return w.call(gpu::copy_to_host::block, Defs{m0, m1, d_ptr, h_ptr});
82 }
83
84 return RWPhase::rewrite_imm_App(app);
85}
86
87} // namespace mim::plug::buffer
static auto isa(const Def *def)
Definition axm.h:112
Base class for all Defs.
Definition def.h:289
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
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:379
const Def * rewrite_imm_App(const App *) override
Definition phase.cpp:223
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:62
const Def * rewrite_imm_App(const App *) override
Definition lower_ptr.cpp:13
The buffer Plugin
Definition buffer.h:8
The mem Plugin
plug::core::Ptr Ptr
Definition autogen.h:15
const Def * op_alloc(const Def *type, const Def *as, const Def *mem)
Definition mem.h:108
const Def * op_lea_tuple(const Def *ptr, const Def *tuple)
Successively offsets ptr by each component of the index tuple, peeling one array dimension per mem....
Definition mem.h:97
fe::View< const Def * > Defs
Definition def.h:96