MimIR
0.4-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
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
10
#include "
mim/plug/gpu/phase/lower_map_reduce.h
"
11
#include "
mim/plug/gpu/phase/mem_checks.h
"
12
#include "
mim/plug/gpu/phase/remove_double_syncs.h
"
13
#include "
mim/plug/gpu/phase/split_apply.h
"
14
15
using namespace
mim
;
16
using namespace
mim::plug
;
17
18
void
reg_phases
(
Flags2Phases
& phases) {
19
MIM_REPL
(phases,
gpu::check_addr_spaces_repl
, {
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
44
MIM_REPL
(phases,
gpu::host_malloc2gpualloc_repl
, {
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
65
Phase::hook<gpu::mem_checks, gpu::phase::MemChecks >
(phases);
66
Phase::hook<gpu::remove_double_syncs, gpu::phase::RemoveDoubleSyncs>
(phases);
67
Phase::hook<gpu::split_apply, gpu::phase::SplitApply >
(phases);
68
Phase::hook<gpu::lower_btensor_map_reduce, gpu::phase::LowerMapReduce >
(phases);
69
// clang-format on
70
}
71
72
extern
"C"
MIM_EXPORT
Plugin
mim_get_plugin
() {
return
{
"gpu"
,
MIM_VERSION
, {},
reg_phases
, {}, {}, {}, {}}; }
reg_phases
void reg_phases(Flags2Phases &phases)
Definition
affine.cpp:12
mim::Axm::isa
static auto isa(const Def *def)
Definition
axm.h:112
mim::Lit::as
static T as(const Def *def)
Definition
def.h:943
mim::Phase::hook
static void hook(Flags2Phases &phases)
Definition
phase.h:70
mim::World
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition
world.h:40
config.h
MIM_EXPORT
#define MIM_EXPORT
Definition
config.h:21
driver.h
lower_map_reduce.h
gpu.h
mem.h
mem_checks.h
mim::plug::gpu::check_addr_spaces_repl
check_addr_spaces_repl
Definition
autogen.h:248
mim::plug::gpu::alloc
alloc
Definition
autogen.h:168
mim::plug::gpu::alloc::block
@ block
Definition
autogen.h:169
mim::plug::gpu::free
free
Definition
autogen.h:177
mim::plug::gpu::free::block
@ block
Definition
autogen.h:178
mim::plug::gpu::host_malloc2gpualloc_repl
host_malloc2gpualloc_repl
Definition
autogen.h:255
mim::plug::mem
The mem Plugin
Definition
mem.h:11
mim::plug
Definition
lower_for.h:5
mim
Definition
ast.h:16
mim::Flags2Phases
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::mim_get_plugin
mim::Plugin mim_get_plugin()
phase.h
MIM_REPL
#define MIM_REPL(__phases, __annex,...)
Definition
phase.h:547
plugin.h
MIM_VERSION
#define MIM_VERSION
Definition
plugin.h:149
remove_double_syncs.h
split_apply.h
mim::Plugin
Basic info and registration function pointer to be returned from a specific plugin.
Definition
plugin.h:154
src
mim
plug
gpu
gpu.cpp
Generated by
1.18.0