Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
DispatcherHIP.hpp
Go to the documentation of this file.
1#ifndef PROTEUS_FRONTEND_DISPATCHER_HIP_HPP
2#define PROTEUS_FRONTEND_DISPATCHER_HIP_HPP
3
4#if PROTEUS_ENABLE_HIP
5
6#include "proteus/Error.h"
9
10namespace proteus {
11
12class DispatcherHIP : public Dispatcher {
13public:
14 static DispatcherHIP &instance() {
15 static DispatcherHIP D;
16 return D;
17 }
18
19 std::unique_ptr<MemoryBuffer> compile(std::unique_ptr<LLVMContext> Ctx,
20 std::unique_ptr<Module> Mod,
21 HashT ModuleHash,
22 bool DisableIROpt = false) override {
23 // This is necessary to ensure Ctx outlives M. Setting [[maybe_unused]] can
24 // trigger a lifetime bug.
25 auto CtxOwner = std::move(Ctx);
26 auto ModOwner = std::move(Mod);
27
28 std::unique_ptr<MemoryBuffer> ObjectModule =
29 Jit.compileOnly(*ModOwner, DisableIROpt);
30 if (!ObjectModule)
31 PROTEUS_FATAL_ERROR("Expected non-null object library");
32
33 StorageCache.store(ModuleHash, ObjectModule->getMemBufferRef());
34
35 return ObjectModule;
36 }
37
38 std::unique_ptr<CompiledLibrary>
39 lookupCompiledLibrary(HashT ModuleHash) override {
40 return StorageCache.lookup(ModuleHash);
41 }
42
43 DispatchResult launch(void *KernelFunc, LaunchDims GridDim,
45 uint64_t ShmemSize, void *Stream) override {
46 dim3 HipGridDim = {GridDim.X, GridDim.Y, GridDim.Z};
47 dim3 HipBlockDim = {BlockDim.X, BlockDim.Y, BlockDim.Z};
48 hipStream_t HipStream = reinterpret_cast<hipStream_t>(Stream);
49
50 void **KernelArgsPtrs = const_cast<void **>(KernelArgs.data());
54 }
55
56 StringRef getDeviceArch() const override { return Jit.getDeviceArch(); }
57
59 CodeCache.printStats();
60 StorageCache.printStats();
61 }
62
63 void *getFunctionAddress(StringRef KernelName, HashT ModuleHash,
64 CompiledLibrary &Library) override {
65 auto GetKernelFunc = [&]() {
66 // Hash the kernel name to get a unique id.
67 HashT HashValue = hash(KernelName, ModuleHash);
68
69 if (auto KernelFunc = CodeCache.lookup(HashValue))
70 return KernelFunc;
71
73 KernelName, Library.ObjectModule->getBufferStart(),
74 /*RelinkGlobalsByCopy*/ false,
75 /* VarNameToDevPtr */ {});
76
77 CodeCache.insert(HashValue, KernelFunc, KernelName);
78
79 return KernelFunc;
80 };
81
83 return KernelFunc;
84 }
85
86 void registerDynamicLibrary(HashT, const SmallString<128> &) override {
87 PROTEUS_FATAL_ERROR("Dispatch HIP does not support registerDynamicLibrary");
88 }
89
90private:
91 JitEngineDeviceHIP &Jit;
92 DispatcherHIP() : Jit(JitEngineDeviceHIP::instance()) {
93 TargetModel = TargetModelType::HIP;
94 }
96 JitStorageCache StorageCache;
97};
98
99} // namespace proteus
100
101#endif
102
103#endif // PROTEUS_FRONTEND_DISPATCHER_HIP_HPP
void char * KernelName
Definition CompilerInterfaceDevice.cpp:50
auto & Jit
Definition CompilerInterfaceDevice.cpp:54
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
Definition BuiltinsCUDA.cpp:4
HashT hash(FirstT &&First, RestTs &&...Rest)
Definition Hashing.hpp:126
T getRuntimeConstantValue(void *Arg)
Definition CompilerInterfaceRuntimeConstantInfo.h:114
cudaError_t launchKernelFunction(CUfunction KernelFunc, dim3 GridDim, dim3 BlockDim, void **KernelArgs, uint64_t ShmemSize, CUstream Stream)
Definition CoreDeviceCUDA.hpp:51
CUfunction getKernelFunctionFromImage(StringRef KernelName, const void *Image, bool RelinkGlobalsByCopy, const std::unordered_map< std::string, const void * > &VarNameToDevPtr)
Definition CoreDeviceCUDA.hpp:27
Definition Dispatcher.hpp:16
unsigned Z
Definition Dispatcher.hpp:17
unsigned Y
Definition Dispatcher.hpp:17
unsigned X
Definition Dispatcher.hpp:17