MimIR
0.4-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
Loading...
Searching...
No Matches
mem_checks.cpp
Go to the documentation of this file.
1
#include "
mim/plug/gpu/phase/mem_checks.h
"
2
3
#include <
mim/axm.h
>
4
5
#include <
mim/plug/mem/mem.h
>
6
7
namespace
mim::plug::gpu::phase
{
8
9
namespace
{
10
11
class
MemFinder {
12
public
:
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
22
private
:
23
DefVec
to_search;
24
};
25
26
MemFinder::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
41
const
Def
*
MemChecks::rewrite_imm_App
(
const
App
* app) {
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
57
void
MemChecks::rewrite_external
(
Def
* def) {
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
}
74
Super::rewrite_external
(def);
75
}
76
77
}
// namespace mim::plug::gpu::phase
axm.h
mim::Analysis::rewrite_external
virtual void rewrite_external(Def *)
Definition
phase.cpp:103
mim::App
Definition
lam.h:224
mim::Axm::isa
static auto isa(const Def *def)
Definition
axm.h:112
mim::Def
Base class for all Defs.
Definition
def.h:273
mim::Lam
A function.
Definition
lam.h:113
mim::Lit::as
static T as(const Def *def)
Definition
def.h:943
mim::plug::gpu::phase::MemChecks::rewrite_imm_App
const Def * rewrite_imm_App(const App *) final
Definition
mem_checks.cpp:41
mim::plug::gpu::phase::MemChecks::rewrite_external
void rewrite_external(Def *) final
Definition
mem_checks.cpp:57
mem.h
mem_checks.h
mim::plug::gpu::phase
Definition
lower_map_reduce.h:7
mim::plug::gpu::launch
launch
Definition
autogen.h:227
mim::plug::mem
The mem Plugin
Definition
mem.h:11
mim::DefVec
fe::Vector< const Def * > DefVec
Definition
def.h:93
src
mim
plug
gpu
phase
mem_checks.cpp
Generated by
1.18.0