Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
Dispatcher.hpp
Go to the documentation of this file.
1#ifndef PROTEUS_FRONTEND_DISPATCHER_HPP
2#define PROTEUS_FRONTEND_DISPATCHER_HPP
3
4#include <llvm/IR/Module.h>
5#include <llvm/Support/MemoryBuffer.h>
6#include <memory>
7
8#if PROTEUS_ENABLE_HIP && __HIP__
9#include <hip/hip_runtime.h>
10#endif
11
12enum class TargetModelType { HOST, CUDA, HIP };
13
14struct LaunchDims {
15 unsigned X = 1, Y = 1, Z = 1;
16};
17
18namespace proteus {
19
20using namespace llvm;
21
22// in Dispatcher.hpp (or a new Errors.hpp)
24 int Ret;
25
26 // construct from an integer error‐code
27 constexpr DispatchResult(int Ret = 0) noexcept : Ret(Ret) {}
28
29 // implicit conversion back to int
30 operator int() const noexcept { return Ret; }
31
32#if PROTEUS_ENABLE_HIP && __HIP__
33 operator hipError_t() const noexcept { return static_cast<hipError_t>(Ret); }
34#endif
35
36#if PROTEUS_ENABLE_CUDA && defined(__CUDACC__)
37 operator cudaError_t() const noexcept {
38 return static_cast<cudaError_t>(Ret);
39 }
40#endif
41};
42
43struct DispatchResult;
44
46public:
48
49 virtual void compile(std::unique_ptr<Module> M) = 0;
50
51 virtual DispatchResult launch(StringRef KernelName, LaunchDims GridDim,
52 LaunchDims BlockDim,
53 ArrayRef<void *> KernelArgs, uint64_t ShmemSize,
54 void *Stream) = 0;
55
56 template <typename Ret, typename... ArgT>
57 Ret run(StringRef FuncName, ArgT... Args) {
58 void *Addr = getFunctionAddress(FuncName);
59 using FnPtr = Ret (*)(ArgT...);
60 auto Fn = reinterpret_cast<FnPtr>(Addr);
61 return Fn(Args...);
62 }
63
64protected:
65 std::unique_ptr<MemoryBuffer> Library = nullptr;
66 virtual void *getFunctionAddress(StringRef FunctionName) = 0;
67};
68
69} // namespace proteus
70
71#endif
void char * KernelName
Definition CompilerInterfaceDevice.cpp:50
char int void ** Args
Definition CompilerInterfaceHost.cpp:20
TargetModelType
Definition Dispatcher.hpp:12
Definition Dispatcher.hpp:45
Ret run(StringRef FuncName, ArgT... Args)
Definition Dispatcher.hpp:57
virtual void compile(std::unique_ptr< Module > M)=0
static Dispatcher & getDispatcher(TargetModelType Model)
Definition Dispatcher.cpp:15
virtual void * getFunctionAddress(StringRef FunctionName)=0
std::unique_ptr< MemoryBuffer > Library
Definition Dispatcher.hpp:65
virtual DispatchResult launch(StringRef KernelName, LaunchDims GridDim, LaunchDims BlockDim, ArrayRef< void * > KernelArgs, uint64_t ShmemSize, void *Stream)=0
Definition Dispatcher.cpp:14
Definition Dispatcher.hpp:14
unsigned Z
Definition Dispatcher.hpp:15
unsigned Y
Definition Dispatcher.hpp:15
unsigned X
Definition Dispatcher.hpp:15
Definition Dispatcher.hpp:23
constexpr DispatchResult(int Ret=0) noexcept
Definition Dispatcher.hpp:27
int Ret
Definition Dispatcher.hpp:24