MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
split_off_kernels.cpp
Go to the documentation of this file.
2
3namespace mim::plug::gpu::phase {
4
6 analyze();
7 if (kernels_.empty()) return;
8
9 auto device = RWPhase(old_world(), "split_off_kernels_device");
10 // A kernel's name becomes its external symbol, so gid-suffix it: two kernels may share a name.
11 for (auto kernel : kernels_)
12 device.rewrite(kernel)->as_mut<Lam>()->set<true>(kernel->unique_name())->externalize();
13
15 swap(new_world(), device.new_world());
16}
17
19 for (auto def : old_world().externals().muts())
20 analyze(def);
21
22 return false; // no fixed-point necessary
23}
24
25void SplitOffKernels::analyze(const Def* def) {
26 if (auto [_, ins] = analyzed_.emplace(def); !ins) return;
27
28 if (auto launch = Axm::isa<gpu::launch>(def)) {
29 auto kernel = launch->decurry()->decurry()->arg();
30 if (auto lam = kernel->isa_mut<Lam>()) kernels_.emplace(lam);
31 }
32
33 for (auto d : def->deps())
34 analyze(d);
35}
36
38 if (kernels_.contains(old_lam))
39 return map(old_lam, new_world().mut_lam(rewrite(old_lam->type())->as<Pi>())->set<true>(old_lam->unique_name()));
40 return RWPhase::rewrite_mut_Lam(old_lam);
41}
42
43} // namespace mim::plug::gpu::phase
static auto isa(const Def *def)
Definition axm.h:112
Base class for all Defs.
Definition def.h:289
Defs deps() const noexcept
Definition def.cpp:486
void externalize()
Definition def.cpp:625
std::string unique_name() const
name + "_" + Def::gid
Definition def.cpp:634
A function.
Definition lam.h:91
const Pi * type() const
Definition lam.h:112
A dependent function type.
Definition lam.h:14
World & new_world()
Create new Defs into this.
Definition phase.h:379
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition phase.h:334
void start() override
Actual entry.
Definition phase.cpp:184
World & old_world()
Get old Defs from here.
Definition phase.h:378
friend void swap(Rewriter &rw1, Rewriter &rw2) noexcept
Definition rewrite.h:133
const Def * map(const Def *old_def, const Def *new_def)
Definition rewrite.cpp:29
virtual const Def * rewrite_mut_Lam(Lam *)
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:62
void set(Sym name)
Definition world.h:114
bool analyze() final
Runs the optional pre-analysis on old_world(), typically to a fixed point, before rewriting begins.