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
9
10namespace proteus {
11
12class DispatcherHost : public Dispatcher {
13public:
15 static DispatcherHost D;
16 return D;
17 }
18
19 std::unique_ptr<MemoryBuffer> compile(std::unique_ptr<LLVMContext> Ctx,
20 std::unique_ptr<Module> Mod,
21 const HashT &ModuleHash,
22 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 std::unique_ptr<MemoryBuffer> ObjectModule =
29 Jit.compileOnly(*ModOwner, DisableIROpt);
30 if (!ObjectModule)
31 reportFatalError("Expected non-null object library");
32
34 ModuleHash, CacheEntry::staticObject(ObjectModule->getMemBufferRef()));
35
36 return ObjectModule;
37 }
38
39 std::unique_ptr<CompiledLibrary>
40 lookupCompiledLibrary(const HashT &ModuleHash) override {
41 return ObjectCache->lookup(ModuleHash);
42 }
43
44 DispatchResult launch(void *, LaunchDims, LaunchDims, void *[], uint64_t,
45 void *) override {
46 reportFatalError("Host does not support launch");
47 }
48
49 StringRef getDeviceArch() const override {
50 reportFatalError("Host dispatcher does not implement getDeviceArch");
51 }
52
53 void *getFunctionAddress(const std::string &FnName, const HashT &ModuleHash,
54 CompiledLibrary &Library) override {
56 HashT FuncHash = hash(FnName, ModuleHash);
57
58 if (void *FuncPtr = CodeCache.lookup(FuncHash))
59 return FuncPtr;
60
61 if (!Library.IsLoaded) {
62 Jit.loadCompiledLibrary(Library);
63 Library.IsLoaded = true;
64 }
65
66 void *FuncAddr = Jit.getFunctionAddress(FnName, Library);
67 if (!FuncAddr)
68 reportFatalError("Failed to find address for function " + FnName);
69
70 CodeCache.insert(FuncHash, FuncAddr, FnName);
71
72 return FuncAddr;
73 }
74
75 void registerDynamicLibrary(const HashT &HashValue,
76 const std::string &Path) override {
77 auto Buf = MemoryBuffer::getFileAsStream(Path);
78 if (!Buf)
79 reportFatalError("Failed to read dynamic library: " + Path);
80 ObjectCache->store(HashValue,
81 CacheEntry::sharedObject((*Buf)->getMemBufferRef()));
82 }
83
84 void registerObject(const HashT &HashValue,
85 const llvm::MemoryBufferRef &Obj) override {
87 }
88
89protected:
90 explicit DispatcherHost(const std::string &Label = "DispatcherHost")
92 Jit(JitEngineHost::instance()), CodeCache(Label) {}
93
95 CodeCache.printStats();
96 CodeCache.printKernelTrace();
98 }
99
100private:
101 JitEngineHost &Jit;
102 MemoryCache<void *> CodeCache;
103};
104
105} // namespace proteus
106
107#endif // PROTEUS_FRONTEND_DISPATCHER_HOST_H
#define TIMESCOPE(...)
Definition TimeTracing.h:66
Definition DispatcherHost.h:12
std::unique_ptr< CompiledLibrary > lookupCompiledLibrary(const HashT &ModuleHash) override
Definition DispatcherHost.h:40
static DispatcherHost & instance()
Definition DispatcherHost.h:14
StringRef getDeviceArch() const override
Definition DispatcherHost.h:49
DispatchResult launch(void *, LaunchDims, LaunchDims, void *[], uint64_t, void *) override
Definition DispatcherHost.h:44
void registerObject(const HashT &HashValue, const llvm::MemoryBufferRef &Obj) override
Definition DispatcherHost.h:84
void * getFunctionAddress(const std::string &FnName, const HashT &ModuleHash, CompiledLibrary &Library) override
Definition DispatcherHost.h:53
~DispatcherHost()
Definition DispatcherHost.h:94
DispatcherHost(const std::string &Label="DispatcherHost")
Definition DispatcherHost.h:90
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:19
void registerDynamicLibrary(const HashT &HashValue, const std::string &Path) override
Definition DispatcherHost.h:75
Definition Dispatcher.h:75
Definition Hashing.h:22
Definition JitEngineHost.h:34
std::unique_ptr< MemoryBuffer > compileOnly(Module &M, bool DisableIROpt=false)
Definition JitEngineHost.cpp:283
void loadCompiledLibrary(CompiledLibrary &Library)
Definition JitEngineHost.cpp:322
void * getFunctionAddress(StringRef FnName, CompiledLibrary &Library)
Definition JitEngineHost.cpp:358
Definition MemoryCache.h:31
void printStats()
Definition MemoryCache.h:62
void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName)
Definition MemoryCache.h:49
void printKernelTrace()
Definition MemoryCache.h:76
Function_t lookup(HashT &HashValue)
Definition MemoryCache.h:35
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:27
TargetModelType
Definition TargetModel.h:8
HashT hash(FirstT &&First, RestTs &&...Rest)
Definition Hashing.h:168
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14
Definition Dispatcher.h:22
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:53