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 launch
50 ->blame("`mem.M` must not be passed across device boundaries: `{}` of type `{}` is passed from host "
51 "to kernel `{}`",
52 kernel_args, kernel_args_t, kernel)
53 .bail();
54 }
55 }
56 return Super::rewrite_imm_App(app);
57}
58
60 auto lam = def->isa<Lam>();
61 if (lam && lam->sym().str() == "main") {
62 MemFinder intype_mem_finder(lam->type()->dom());
63 while (auto mem = intype_mem_finder.next_mem()) {
64 auto addr_space = mem->arg();
65 if (Lit::as(addr_space) != 0)
66 lam->blame("the `main` function must not take a `mem.M n` with a non-zero `n` as an argument").bail();
67 }
68
69 MemFinder outtype_mem_finder(lam->type()->ret_dom());
70 while (auto mem = outtype_mem_finder.next_mem()) {
71 auto addr_space = mem->arg();
72 if (Lit::as(addr_space) != 0)
73 lam->blame("the `main` function must not return a `mem.M n` with a non-zero `n`").bail();
74 }
75 }
77}
78
79} // namespace mim::plug::gpu::phase
virtual void rewrite_external(Def *)
Definition phase.cpp:116
static auto isa(const Def *def)
Definition axm.h:112
Base class for all Defs.
Definition def.h:289
A function.
Definition lam.h:91
static T as(const Def *def)
Definition def.h:982
virtual const Def * rewrite_imm_App(const App *)
const Def * rewrite_imm_App(const App *) final
void rewrite_external(Def *) final
The mem Plugin
fe::Vector< const Def * > DefVec
Definition def.h:98