MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
split_off_kernels.cpp
Go to the documentation of this file.
2
3#include <mim/driver.h>
4
5namespace mim::plug::gpu::phase {
6
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
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
26void 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
39 auto new_def = RWPhase::rewrite_mut_Lam(old_lam);
40
41 if (kernels_.contains(old_lam)) {
42 old_lam->set<true>(old_lam->unique_name());
43 auto new_lam = new_def->as_mut<Lam>();
44 if (new_lam->sym().empty()) {
45 assert(!old_lam->sym().empty());
46 new_lam->set(old_lam->sym());
47 }
48 new_lam->externalize();
49 old_lam->unset();
50 }
51
52 return new_def;
53}
54
55} // namespace mim::plug::gpu::phase
static auto isa(const Def *def)
Definition axm.h:107
Base class for all Defs.
Definition def.h:261
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
Definition def.h:536
Defs deps() const noexcept
Definition def.cpp:514
void externalize()
Definition def.cpp:617
Sym sym() const
Definition def.h:558
std::string unique_name() const
name + "_" + Def::gid
Definition def.cpp:626
A function.
Definition lam.h:110
Lam * unset()
Definition lam.h:179
Lam * set(Filter filter, const Def *body)
Definition lam.cpp:29
virtual void rewrite_annex(flags_t, Sym, const Def *)
Definition phase.cpp:158
World & old_world()
Get old Defs from here.
Definition phase.h:367
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:56
bool analyze() final
Runs the optional pre-analysis on RWPhase::old_world(), typically to a fixed point,...