MimIR
0.4-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
Loading...
Searching...
No Matches
split_off_kernels.cpp
Go to the documentation of this file.
1
#include "
mim/plug/gpu/phase/split_off_kernels.h
"
2
3
#include <
mim/driver.h
>
4
5
namespace
mim::plug::gpu::phase
{
6
7
void
SplitOffKernels::start
() {
8
analyze
();
9
10
for
(
const
auto
& [f, entry] :
old_world
().annexes())
11
rewrite_annex
(f, entry.sym, entry.def);
12
13
for
(
auto
kernel : kernels_)
14
rewrite
(kernel);
15
}
16
17
bool
SplitOffKernels::analyze
() {
18
for
(
auto
def :
old_world
().annexes().defs())
19
analyze
(def);
20
for
(
auto
def :
old_world
().externals().muts())
21
analyze
(def);
22
23
return
false
;
// no fixed-point necessary
24
}
25
26
void
SplitOffKernels::analyze
(
const
Def
* def) {
27
if
(
auto
[_, ins] = analyzed_.emplace(def); !ins)
return
;
28
29
if
(
auto
launch
=
Axm::isa<gpu::launch>
(def)) {
30
auto
kernel =
launch
->decurry()->decurry()->arg();
31
if
(
auto
lam = kernel->isa_mut<
Lam
>()) kernels_.emplace(lam);
32
}
33
34
for
(
auto
d : def->
deps
())
35
analyze
(d);
36
}
37
38
const
Def
*
SplitOffKernels::rewrite_mut_Lam
(
Lam
* old_lam) {
39
auto
new_def = RWPhase::rewrite_mut_Lam(old_lam);
40
41
if
(kernels_.contains(old_lam)) {
42
// A kernel's name becomes its external symbol, so gid-suffix it: two kernels may share a name.
43
old_lam->
set
<
true
>(old_lam->
unique_name
());
44
new_def->
as_mut
<
Lam
>()->set<true>(old_lam->
sym
())->
externalize
();
45
old_lam->
unset
();
46
}
47
48
return
new_def;
49
}
50
51
}
// namespace mim::plug::gpu::phase
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::Def::as_mut
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
Definition
def.h:589
mim::Def::deps
Defs deps() const noexcept
Definition
def.cpp:468
mim::Def::externalize
void externalize()
Definition
def.cpp:607
mim::Def::sym
Sym sym() const
Definition
def.h:612
mim::Def::unique_name
std::string unique_name() const
name + "_" + Def::gid
Definition
def.cpp:616
mim::Lam
A function.
Definition
lam.h:113
mim::Lam::unset
Lam * unset()
Definition
lam.h:189
mim::Lam::set
Lam * set(Filter filter, const Def *body)
Definition
lam.cpp:27
mim::RWPhase::rewrite_annex
void rewrite_annex(flags_t, Sym, const Def *) override
Definition
phase.cpp:198
mim::RWPhase::old_world
World & old_world()
Get old Defs from here.
Definition
phase.h:451
mim::Rewriter::rewrite
virtual const Def * rewrite(const Def *)
Definition
rewrite.cpp:55
mim::plug::gpu::phase::SplitOffKernels::start
void start() final
RWBase::start() and then swaps the two worlds.
Definition
split_off_kernels.cpp:7
mim::plug::gpu::phase::SplitOffKernels::analyze
bool analyze() final
Runs the optional pre-analysis on Phase::world, typically to a fixed point, before rewriting begins.
Definition
split_off_kernels.cpp:17
mim::plug::gpu::phase::SplitOffKernels::rewrite_mut_Lam
const Def * rewrite_mut_Lam(Lam *) final
Definition
split_off_kernels.cpp:38
driver.h
mim::plug::gpu::phase
Definition
lower_map_reduce.h:7
mim::plug::gpu::launch
launch
Definition
autogen.h:227
split_off_kernels.h
src
mim
plug
gpu
phase
split_off_kernels.cpp
Generated by
1.18.0