MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
gpu.cpp
Go to the documentation of this file.
1#include "mim/plug/gpu/gpu.h"
2
3#include <mim/config.h>
4#include <mim/driver.h>
5#include <mim/phase.h>
6#include <mim/plugin.h>
7
8#include <mim/plug/mem/mem.h>
9
14
15using namespace mim;
16using namespace mim::plug;
17
18void reg_phases(Flags2Phases& phases) {
20 auto global_as = Lit::as(world().annex<gpu::addr_space_global>());
21 auto shared_as = Lit::as(world().annex<gpu::addr_space_shared>());
22 auto const_as = Lit::as(world().annex<gpu::addr_space_const>());
23 auto local_as = Lit::as(world().annex<gpu::addr_space_local>());
24 if (auto malloc = Axm::isa<mem::malloc>(def)) {
25 auto addr_space = Lit::as(malloc->decurry()->arg(1));
26 if (addr_space == shared_as || addr_space == const_as || addr_space == local_as)
27 fe::throwf("`mem.malloc` cannot be used in address space {}: `{}`", addr_space, malloc);
28 } else if (auto free = Axm::isa<mem::free>(def)) {
29 auto addr_space = Lit::as(free->decurry()->arg(1));
30 if (addr_space == shared_as || addr_space == const_as || addr_space == local_as)
31 fe::throwf("`mem.free` cannot be used in address space {}: `{}`", addr_space, free);
32 } else if (auto mslot = Axm::isa<mem::mslot>(def)) {
33 auto addr_space = Lit::as(mslot->decurry()->arg(1));
34 if (addr_space == global_as || addr_space == const_as)
35 fe::throwf("`mem.mslot` cannot be used in address space {}: `{}`", addr_space, mslot);
36 } else if (auto store = Axm::isa<mem::store>(def)) {
37 auto addr_space = Lit::as(store->decurry()->arg(1));
38 if (addr_space == const_as)
39 fe::throwf("`mem.store` cannot be used in address space {}: `{}`", addr_space, store);
40 }
41 return {};
42 });
43
45 auto global_as = Lit::as(world().annex<gpu::addr_space_global>());
46 if (auto malloc = Axm::isa<mem::malloc>(def)) {
47 auto [type, addr_space] = malloc->decurry()->args<2>();
48 if (Lit::as(addr_space) == global_as) {
49 auto [mem, _] = malloc->args<2>();
50 World& w = type->world();
51 return w.app(w.app(w.annex<gpu::alloc>(gpu::alloc::block), type), mem);
52 }
53 } else if (auto free = Axm::isa<mem::free>(def)) {
54 auto [type, addr_space] = free->decurry()->args<2>();
55 if (Lit::as(addr_space) == global_as) {
56 auto [mem, ptr] = free->args<2>();
57 World& w = type->world();
58 return w.app(w.app(w.annex<gpu::free>(gpu::free::block), type), {mem, ptr});
59 }
60 }
61 return {};
62 });
63
64 // clang-format off
69 // clang-format on
70}
71
72extern "C" MIM_EXPORT Plugin mim_get_plugin() { return {"gpu", MIM_VERSION, {}, reg_phases, {}, {}, {}, {}}; }
void reg_phases(Flags2Phases &phases)
Definition affine.cpp:12
static auto isa(const Def *def)
Definition axm.h:112
static T as(const Def *def)
Definition def.h:943
static void hook(Flags2Phases &phases)
Definition phase.h:70
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:40
#define MIM_EXPORT
Definition config.h:21
host_malloc2gpualloc_repl
Definition autogen.h:255
The mem Plugin
Definition mem.h:11
Definition ast.h:16
absl::flat_hash_map< flags_t, std::function< std::unique_ptr< Phase >(World &)> > Flags2Phases
Maps an axiom of a Phase to a function that creates one.
Definition plugin.h:30
mim::Plugin mim_get_plugin()
#define MIM_REPL(__phases, __annex,...)
Definition phase.h:547
#define MIM_VERSION
Definition plugin.h:149
Basic info and registration function pointer to be returned from a specific plugin.
Definition plugin.h:154