Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
DispatcherHost.hpp
Go to the documentation of this file.
1#ifndef PROTEUS_FRONTEND_DISPATCHER_HOST_HPP
2#define PROTEUS_FRONTEND_DISPATCHER_HOST_HPP
3
8
9namespace proteus {
10
11class DispatcherHost : public Dispatcher {
12public:
14 static DispatcherHost D;
15 return D;
16 }
17
18 std::unique_ptr<MemoryBuffer> compile(std::unique_ptr<LLVMContext> Ctx,
19 std::unique_ptr<Module> Mod,
20 HashT ModuleHash,
21 bool DisableIROpt = false) override {
22 // This is necessary to ensure Ctx outlives M. Setting [[maybe_unused]] can
23 // trigger a lifetime bug.
24 auto CtxOwner = std::move(Ctx);
25 auto ModOwner = std::move(Mod);
26 std::unique_ptr<MemoryBuffer> ObjectModule =
28 if (!ObjectModule)
29 PROTEUS_FATAL_ERROR("Expected non-null object library");
30
31 StorageCache.store(ModuleHash, ObjectModule->getMemBufferRef());
32
33 return ObjectModule;
34 }
35
36 std::unique_ptr<CompiledLibrary>
37 lookupCompiledLibrary(HashT ModuleHash) override {
38 return StorageCache.lookup(ModuleHash);
39 }
40
42 uint64_t, void *) override {
43 PROTEUS_FATAL_ERROR("Host does not support launch");
44 }
45
46 StringRef getDeviceArch() const override {
47 PROTEUS_FATAL_ERROR("Host dispatcher does not implement getDeviceArch");
48 }
49
50 void *getFunctionAddress(StringRef FnName, HashT ModuleHash,
51 CompiledLibrary &Library) override {
52 HashT FuncHash = hash(FnName, ModuleHash);
53
54 if (void *FuncPtr = CodeCache.lookup(FuncHash))
55 return FuncPtr;
56
57 if (!Library.IsLoaded) {
58 Jit.loadCompiledLibrary(Library);
59 Library.IsLoaded = true;
60 }
61
62 void *FuncAddr = Jit.getFunctionAddress(FnName, Library);
63 if (!FuncAddr)
64 PROTEUS_FATAL_ERROR("Failed to find address for function " + FnName);
65
66 CodeCache.insert(FuncHash, FuncAddr, FnName);
67
68 return FuncAddr;
69 }
70
72 const SmallString<128> &Path) override {
73 StorageCache.storeDynamicLibrary(HashValue, Path);
74 }
75
76protected:
80
82 CodeCache.printStats();
83 StorageCache.printStats();
84 }
85
86private:
87 JitEngineHost &Jit;
88 JitCache<void *> CodeCache;
89 JitStorageCache StorageCache;
90};
91
92} // namespace proteus
93
94#endif // PROTEUS_FRONTEND_DISPATCHER_HOST_HPP
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
Definition DispatcherHost.hpp:11
DispatcherHost()
Definition DispatcherHost.hpp:77
DispatchResult launch(void *, LaunchDims, LaunchDims, ArrayRef< void * >, uint64_t, void *) override
Definition DispatcherHost.hpp:41
std::unique_ptr< MemoryBuffer > compile(std::unique_ptr< LLVMContext > Ctx, std::unique_ptr< Module > Mod, HashT ModuleHash, bool DisableIROpt=false) override
Definition DispatcherHost.hpp:18
void registerDynamicLibrary(HashT HashValue, const SmallString< 128 > &Path) override
Definition DispatcherHost.hpp:71
static DispatcherHost & instance()
Definition DispatcherHost.hpp:13
StringRef getDeviceArch() const override
Definition DispatcherHost.hpp:46
~DispatcherHost()
Definition DispatcherHost.hpp:81
std::unique_ptr< CompiledLibrary > lookupCompiledLibrary(HashT ModuleHash) override
Definition DispatcherHost.hpp:37
void * getFunctionAddress(StringRef FnName, HashT ModuleHash, CompiledLibrary &Library) override
Definition DispatcherHost.hpp:50
Definition Dispatcher.hpp:54
TargetModelType TargetModel
Definition Dispatcher.hpp:56
Definition Hashing.hpp:20
Definition JitCache.hpp:32
void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName)
Definition JitCache.hpp:48
Function_t lookup(HashT &HashValue)
Definition JitCache.hpp:34
void printStats()
Definition JitCache.hpp:65
Definition JitEngineHost.hpp:30
std::unique_ptr< MemoryBuffer > compileOnly(Module &M, bool DisableIROpt=false)
Definition JitEngineHost.cpp:276
void loadCompiledLibrary(CompiledLibrary &Library)
Definition JitEngineHost.cpp:319
void * getFunctionAddress(StringRef FnName, CompiledLibrary &Library)
Definition JitEngineHost.cpp:355
Definition JitStorageCache.hpp:38
void store(HashT &HashValue, MemoryBufferRef ObjBufRef)
Definition JitStorageCache.hpp:72
void storeDynamicLibrary(HashT &HashValue, const SmallString< 128 > &Path)
Definition JitStorageCache.hpp:82
void printStats()
Definition JitStorageCache.hpp:91
std::unique_ptr< CompiledLibrary > lookup(HashT &HashValue)
Definition JitStorageCache.hpp:47
Definition BuiltinsCUDA.cpp:4
HashT hash(FirstT &&First, RestTs &&...Rest)
Definition Hashing.hpp:126
T getRuntimeConstantValue(void *Arg)
Definition CompilerInterfaceRuntimeConstantInfo.h:114
Definition Dispatcher.hpp:16
Definition CompiledLibrary.hpp:12
bool IsLoaded
Definition CompiledLibrary.hpp:18
Definition Dispatcher.hpp:32