Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
DispatcherHost.h
Go to the documentation of this file.
1#ifndef PROTEUS_FRONTEND_DISPATCHER_HOST_H
2#define PROTEUS_FRONTEND_DISPATCHER_HOST_H
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 const 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 =
27 Jit.compileOnly(*ModOwner, DisableIROpt);
28 if (!ObjectModule)
29 reportFatalError("Expected non-null object library");
30
32 ModuleHash, CacheEntry::staticObject(ObjectModule->getMemBufferRef()));
33
34 return ObjectModule;
35 }
36
37 std::unique_ptr<CompiledLibrary>
38 lookupCompiledLibrary(const HashT &ModuleHash) override {
39 return ObjectCache->lookup(ModuleHash);
40 }
41
42 DispatchResult launch(void *, LaunchDims, LaunchDims, void *[], uint64_t,
43 void *) override {
44 reportFatalError("Host does not support launch");
45 }
46
47 StringRef getDeviceArch() const override {
48 reportFatalError("Host dispatcher does not implement getDeviceArch");
49 }
50
51 void *getFunctionAddress(const std::string &FnName, const HashT &ModuleHash,
52 CompiledLibrary &Library) override {
53 HashT FuncHash = hash(FnName, ModuleHash);
54
55 if (void *FuncPtr = CodeCache.lookup(FuncHash))
56 return FuncPtr;
57
58 if (!Library.IsLoaded) {
59 Jit.loadCompiledLibrary(Library);
60 Library.IsLoaded = true;
61 }
62
63 void *FuncAddr = Jit.getFunctionAddress(FnName, Library);
64 if (!FuncAddr)
65 reportFatalError("Failed to find address for function " + FnName);
66
67 CodeCache.insert(FuncHash, FuncAddr, FnName);
68
69 return FuncAddr;
70 }
71
72 void registerDynamicLibrary(const HashT &HashValue,
73 const std::string &Path) override {
74 auto Buf = MemoryBuffer::getFileAsStream(Path);
75 if (!Buf)
76 reportFatalError("Failed to read dynamic library: " + Path);
77 ObjectCache->store(HashValue,
78 CacheEntry::sharedObject((*Buf)->getMemBufferRef()));
79 }
80
81protected:
83 : Dispatcher("DispatcherHost", TargetModelType::HOST),
84 Jit(JitEngineHost::instance()) {}
85
87 CodeCache.printStats();
88 CodeCache.printKernelTrace();
90 }
91
92private:
93 JitEngineHost &Jit;
94 MemoryCache<void *> CodeCache{"DispatcherHost"};
95};
96
97} // namespace proteus
98
99#endif // PROTEUS_FRONTEND_DISPATCHER_HOST_H
Definition DispatcherHost.h:11
DispatcherHost()
Definition DispatcherHost.h:82
std::unique_ptr< CompiledLibrary > lookupCompiledLibrary(const HashT &ModuleHash) override
Definition DispatcherHost.h:38
static DispatcherHost & instance()
Definition DispatcherHost.h:13
StringRef getDeviceArch() const override
Definition DispatcherHost.h:47
DispatchResult launch(void *, LaunchDims, LaunchDims, void *[], uint64_t, void *) override
Definition DispatcherHost.h:42
void * getFunctionAddress(const std::string &FnName, const HashT &ModuleHash, CompiledLibrary &Library) override
Definition DispatcherHost.h:51
~DispatcherHost()
Definition DispatcherHost.h:86
std::unique_ptr< MemoryBuffer > compile(std::unique_ptr< LLVMContext > Ctx, std::unique_ptr< Module > Mod, const HashT &ModuleHash, bool DisableIROpt=false) override
Definition DispatcherHost.h:18
void registerDynamicLibrary(const HashT &HashValue, const std::string &Path) override
Definition DispatcherHost.h:72
Definition Dispatcher.h:74
Definition Hashing.h:21
Definition JitEngineHost.h:34
std::unique_ptr< MemoryBuffer > compileOnly(Module &M, bool DisableIROpt=false)
Definition JitEngineHost.cpp:279
void loadCompiledLibrary(CompiledLibrary &Library)
Definition JitEngineHost.cpp:322
void * getFunctionAddress(StringRef FnName, CompiledLibrary &Library)
Definition JitEngineHost.cpp:357
Definition MemoryCache.h:30
void printStats()
Definition MemoryCache.h:61
void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName)
Definition MemoryCache.h:48
void printKernelTrace()
Definition MemoryCache.h:74
Function_t lookup(HashT &HashValue)
Definition MemoryCache.h:34
Definition ObjectCache.h:39
virtual void printStats()=0
virtual void store(const HashT &HashValue, const CacheEntry &Entry)=0
virtual std::unique_ptr< CompiledLibrary > lookup(const HashT &HashValue)=0
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
Definition Dispatcher.h:21
static CacheEntry staticObject(MemoryBufferRef Buf)
Definition ObjectCache.h:31
static CacheEntry sharedObject(MemoryBufferRef Buf)
Definition ObjectCache.h:33
Definition CompiledLibrary.h:18
bool IsLoaded
Definition CompiledLibrary.h:24
Definition Dispatcher.h:52