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 // 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
static auto isa(const Def *def)
Definition axm.h:112
Base class for all Defs.
Definition def.h:273
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
Definition def.h:589
Defs deps() const noexcept
Definition def.cpp:468
void externalize()
Definition def.cpp:607
Sym sym() const
Definition def.h:612
std::string unique_name() const
name + "_" + Def::gid
Definition def.cpp:616
A function.
Definition lam.h:113
Lam * unset()
Definition lam.h:189
Lam * set(Filter filter, const Def *body)
Definition lam.cpp:27
void rewrite_annex(flags_t, Sym, const Def *) override
Definition phase.cpp:198
World & old_world()
Get old Defs from here.
Definition phase.h:451
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:55
void start() final
RWBase::start() and then swaps the two worlds.
bool analyze() final
Runs the optional pre-analysis on Phase::world, typically to a fixed point, before rewriting begins.