Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
Dispatcher.h
Go to the documentation of this file.
1#ifndef PROTEUS_FRONTEND_DISPATCHER_H
2#define PROTEUS_FRONTEND_DISPATCHER_H
3
4#include "proteus/Error.h"
6
7#if PROTEUS_ENABLE_HIP && __HIP__
8#include <hip/hip_runtime.h>
9#endif
10
11#include <cstdint>
12#include <memory>
13#include <type_traits>
14
15namespace llvm {
16class LLVMContext;
17class Module;
18class MemoryBuffer;
19class MemoryBufferRef;
20} // namespace llvm
21
22struct LaunchDims {
23 unsigned X = 1, Y = 1, Z = 1;
24
25 constexpr LaunchDims() = default;
26
27 constexpr LaunchDims(unsigned X, unsigned Y = 1, unsigned Z = 1)
28 : X(X), Y(Y), Z(Z) {}
29
30 // Templated converting constructor for dim3-like types.
31 template <
32 typename T,
33 typename = std::enable_if_t<
34 std::is_convertible_v<decltype(std::declval<T>().x), unsigned> &&
35 std::is_convertible_v<decltype(std::declval<T>().y), unsigned> &&
36 std::is_convertible_v<decltype(std::declval<T>().z), unsigned>>>
37 constexpr LaunchDims(const T &Dims) : X(Dims.x), Y(Dims.y), Z(Dims.z) {}
38};
39
40namespace proteus {
41
42class ObjectCacheChain;
43struct CompiledLibrary;
44class HashT;
45
46template <typename T> struct sig_traits;
47
48template <typename R, typename... Args> struct sig_traits<R(Args...)> {
49 using return_type = R;
50 using argument_types = std::tuple<Args...>;
51};
52
54 int Ret;
55
56 // construct from an integer error‐code
57 constexpr DispatchResult(int Ret = 0) noexcept : Ret(Ret) {}
58
59 // implicit conversion back to int
60 operator int() const noexcept { return Ret; }
61
62#if PROTEUS_ENABLE_HIP && __HIP__
63 operator hipError_t() const noexcept { return static_cast<hipError_t>(Ret); }
64#endif
65
66#if PROTEUS_ENABLE_CUDA && defined(__CUDACC__)
67 operator cudaError_t() const noexcept {
68 return static_cast<cudaError_t>(Ret);
69 }
70#endif
71};
72
73struct DispatchResult;
74
76protected:
78 std::unique_ptr<ObjectCacheChain> ObjectCache;
79
80 Dispatcher(const std::string &Name, TargetModelType TM);
81
82public:
83 static Dispatcher &getDispatcher(TargetModelType TargetModel);
84 virtual ~Dispatcher() = default;
85
86 virtual std::unique_ptr<llvm::MemoryBuffer>
87 compile(std::unique_ptr<llvm::LLVMContext> Ctx,
88 std::unique_ptr<llvm::Module> M, const HashT &ModuleHash,
89 bool DisableIROpt = false) = 0;
90
91 virtual std::unique_ptr<CompiledLibrary>
92 lookupCompiledLibrary(const HashT &ModuleHash) = 0;
93
94 virtual DispatchResult launch(void *KernelFunc, LaunchDims GridDim,
95 LaunchDims BlockDim, void *KernelArgs[],
96 uint64_t ShmemSize, void *Stream) = 0;
97
98 virtual llvm::StringRef getDeviceArch() const = 0;
99
100 template <typename Sig, typename... ArgT>
101 typename sig_traits<Sig>::return_type run(void *FuncPtr, ArgT &&...Args) {
102 if (!isHostTargetModel(TargetModel))
104 "Dispatcher run interface is only supported for host derived models");
105
106 auto Fn = reinterpret_cast<Sig *>(FuncPtr);
107 using Ret = typename sig_traits<Sig>::return_type;
108
109 if constexpr (std::is_void_v<Ret>) {
110 Fn(std::forward<ArgT>(Args)...);
111 return;
112 } else
113 return Fn(std::forward<ArgT>(Args)...);
114 }
115
116 virtual void *getFunctionAddress(const std::string &FunctionName,
117 const HashT &ModuleHash,
118 CompiledLibrary &Library) = 0;
119
120 virtual void registerDynamicLibrary(const HashT &HashValue,
121 const std::string &Path) = 0;
122
123 virtual void registerObject(const HashT &HashValue,
124 const llvm::MemoryBufferRef &Obj) = 0;
125};
126
127} // namespace proteus
128
129#endif
char int void ** Args
Definition CompilerInterfaceHost.cpp:23
Definition Dispatcher.h:75
TargetModelType TargetModel
Definition Dispatcher.h:77
virtual void registerDynamicLibrary(const HashT &HashValue, const std::string &Path)=0
virtual std::unique_ptr< llvm::MemoryBuffer > compile(std::unique_ptr< llvm::LLVMContext > Ctx, std::unique_ptr< llvm::Module > M, const HashT &ModuleHash, bool DisableIROpt=false)=0
std::unique_ptr< ObjectCacheChain > ObjectCache
Definition Dispatcher.h:78
virtual ~Dispatcher()=default
virtual std::unique_ptr< CompiledLibrary > lookupCompiledLibrary(const HashT &ModuleHash)=0
virtual DispatchResult launch(void *KernelFunc, LaunchDims GridDim, LaunchDims BlockDim, void *KernelArgs[], uint64_t ShmemSize, void *Stream)=0
sig_traits< Sig >::return_type run(void *FuncPtr, ArgT &&...Args)
Definition Dispatcher.h:101
virtual void registerObject(const HashT &HashValue, const llvm::MemoryBufferRef &Obj)=0
virtual llvm::StringRef getDeviceArch() const =0
virtual void * getFunctionAddress(const std::string &FunctionName, const HashT &ModuleHash, CompiledLibrary &Library)=0
Definition Hashing.h:22
Definition CompiledLibrary.h:7
Definition MemoryCache.h:27
TargetModelType
Definition TargetModel.h:8
bool isHostTargetModel(TargetModelType TargetModel)
Definition TargetModel.cpp:49
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14
Definition Dispatcher.h:22
constexpr LaunchDims(unsigned X, unsigned Y=1, unsigned Z=1)
Definition Dispatcher.h:27
unsigned Z
Definition Dispatcher.h:23
unsigned Y
Definition Dispatcher.h:23
constexpr LaunchDims()=default
constexpr LaunchDims(const T &Dims)
Definition Dispatcher.h:37
unsigned X
Definition Dispatcher.h:23
Definition CompiledLibrary.h:18
Definition Dispatcher.h:53
constexpr DispatchResult(int Ret=0) noexcept
Definition Dispatcher.h:57
int Ret
Definition Dispatcher.h:54
R return_type
Definition Dispatcher.h:49
std::tuple< Args... > argument_types
Definition Dispatcher.h:50
Definition Dispatcher.h:46