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
11#include <mim/plug/mem/mem.h>
12
13using namespace mim;
14using namespace mim::plug;
15
16void reg_phases(Flags2Phases& phases) {
18 auto global_as = Lit::as(world().annex<gpu::addr_space_global>());
19 auto shared_as = Lit::as(world().annex<gpu::addr_space_shared>());
20 auto const_as = Lit::as(world().annex<gpu::addr_space_const>());
21 auto local_as = Lit::as(world().annex<gpu::addr_space_local>());
22 if (auto malloc = Axm::isa<mem::malloc>(def)) {
23 auto addr_space = Lit::as(malloc->decurry()->arg(1));
24 if (addr_space == shared_as || addr_space == const_as || addr_space == local_as)
25 fe::throwf("Invalid use of %mem.malloc: cannot be used in address space {}: {}", addr_space, malloc);
26 } else if (auto free = Axm::isa<mem::free>(def)) {
27 auto addr_space = Lit::as(free->decurry()->arg(1));
28 if (addr_space == shared_as || addr_space == const_as || addr_space == local_as)
29 fe::throwf("Invalid use of %mem.free: cannot be used in address space {}: {}", addr_space, free);
30 } else if (auto mslot = Axm::isa<mem::mslot>(def)) {
31 auto addr_space = Lit::as(mslot->decurry()->arg(1));
32 if (addr_space == global_as || addr_space == const_as)
33 fe::throwf("Invalid use of %mem.mslot: cannot be used in address space {}: {}", addr_space, mslot);
34 } else if (auto store = Axm::isa<mem::store>(def)) {
35 auto addr_space = Lit::as(store->decurry()->arg(1));
36 if (addr_space == const_as)
37 fe::throwf("Invalid use of %mem.store: cannot be used in address space {}: {}", addr_space, store);
38 }
39 return {};
40 });
41
43 auto global_as = Lit::as(world().annex<gpu::addr_space_global>());
44 if (auto malloc = Axm::isa<mem::malloc>(def)) {
45 auto [type, addr_space] = malloc->decurry()->args<2>();
46 if (Lit::as(addr_space) == global_as) {
47 auto [mem, _] = malloc->args<2>();
48 World& w = type->world();
49 return w.app(w.app(w.annex<gpu::alloc>(gpu::alloc::block), type), mem);
50 }
51 } else if (auto free = Axm::isa<mem::free>(def)) {
52 auto [type, addr_space] = free->decurry()->args<2>();
53 if (Lit::as(addr_space) == global_as) {
54 auto [mem, ptr] = free->args<2>();
55 World& w = type->world();
56 return w.app(w.app(w.annex<gpu::free>(gpu::free::block), type), {mem, ptr});
57 }
58 }
59 return {};
60 });
61
62 // clang-format off
66 // clang-format on
67}
68
69extern "C" MIM_EXPORT Plugin mim_get_plugin() { return {"gpu", MIM_VERSION, nullptr, reg_phases}; }
void reg_phases(Flags2Phases &phases)
Definition affine.cpp:12
static auto isa(const Def *def)
Definition axm.h:107
static T as(const Def *def)
Definition def.h:884
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:36
#define MIM_EXPORT
Definition config.h:19
void reg_phases(Flags2Phases &phases)
Definition gpu.cpp:16
host_malloc2gpualloc_repl
Definition autogen.h:227
The mem Plugin
Definition mem.h:11
Definition ast.h:14
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:25
mim::Plugin mim_get_plugin()
#define MIM_REPL(__phases, __annex,...)
Definition phase.h:406
#define MIM_VERSION
Definition plugin.h:54
Basic info and registration function pointer to be returned from a specific plugin.
Definition plugin.h:59