MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
mem_checks.cpp
Go to the documentation of this file.
2
3#include <mim/axm.h>
4
5#include <mim/plug/mem/mem.h>
6
7namespace mim::plug::gpu::phase {
8
9namespace {
10
11class MemFinder {
12public:
13 MemFinder(const Def* to_search)
14 : to_search({to_search}) {}
15 MemFinder(DefVec&& to_search)
16 : to_search(std::move(to_search)) {}
17
18 using IsaMem = Axm::IsA<mem::M, mim::App>;
19
20 IsaMem next_mem();
21
22private:
23 DefVec to_search;
24};
25
26MemFinder::IsaMem MemFinder::next_mem() {
27 while (!to_search.empty()) {
28 auto cur = to_search.back();
29 to_search.pop_back();
30 if (cur->isa<Sigma>())
31 for (auto op : cur->ops())
32 to_search.push_back(op);
33 else if (auto mem = Axm::isa<mem::M>(cur))
34 return mem;
35 }
36 return IsaMem();
37}
38
39} // namespace
40
42 if (auto launch = Axm::isa<gpu::launch>(app)) {
43 auto kernel = launch->decurry()->decurry()->arg();
44 auto kernel_args = launch->decurry()->arg();
45 auto kernel_args_t = kernel_args->type();
46
47 MemFinder mem_finder(kernel_args_t);
48 if (mem_finder.next_mem()) {
49 fe::throwf("`mem.M` must not be passed across device boundaries: `{}` of type `{}` is passed from host to "
50 "kernel `{}`",
51 kernel_args, kernel_args_t, kernel);
52 }
53 }
54 return Super::rewrite_imm_App(app);
55}
56
58 auto lam = def->isa<Lam>();
59 if (lam && lam->sym().str() == "main") {
60 MemFinder intype_mem_finder(lam->type()->dom());
61 while (auto mem = intype_mem_finder.next_mem()) {
62 auto addr_space = mem->arg();
63 if (Lit::as(addr_space) != 0)
64 fe::throwf("the `main` function must not take a `mem.M n` with a non-zero `n` as an argument");
65 }
66
67 MemFinder outtype_mem_finder(lam->type()->ret_dom());
68 while (auto mem = outtype_mem_finder.next_mem()) {
69 auto addr_space = mem->arg();
70 if (Lit::as(addr_space) != 0)
71 fe::throwf("the `main` function must not return a `mem.M n` with a non-zero `n`");
72 }
73 }
75}
76
77} // namespace mim::plug::gpu::phase
virtual void rewrite_external(Def *)
Definition phase.cpp:103
static auto isa(const Def *def)
Definition axm.h:112
Base class for all Defs.
Definition def.h:273
A function.
Definition lam.h:113
static T as(const Def *def)
Definition def.h:943
const Def * rewrite_imm_App(const App *) final
void rewrite_external(Def *) final
The mem Plugin
Definition mem.h:11
fe::Vector< const Def * > DefVec
Definition def.h:93