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