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
10#include "proteus/Hashing.hpp"
11
12#if PROTEUS_ENABLE_HIP && __HIP__
13#include <hip/hip_runtime.h>
14#endif
15
16struct LaunchDims {
17 unsigned X = 1, Y = 1, Z = 1;
18};
19
20namespace proteus {
21
22template <typename T> struct sig_traits;
23
24template <typename R, typename... Args> struct sig_traits<R(Args...)> {
25 using return_type = R;
26 using argument_types = std::tuple<Args...>;
27};
28
29using namespace llvm;
30
31// in Dispatcher.hpp (or a new Errors.hpp)
33 int Ret;
34
35 // construct from an integer error‐code
36 constexpr DispatchResult(int Ret = 0) noexcept : Ret(Ret) {}
37
38 // implicit conversion back to int
39 operator int() const noexcept { return Ret; }
40
41#if PROTEUS_ENABLE_HIP && __HIP__
42 operator hipError_t() const noexcept { return static_cast<hipError_t>(Ret); }
43#endif
44
45#if PROTEUS_ENABLE_CUDA && defined(__CUDACC__)
46 operator cudaError_t() const noexcept {
47 return static_cast<cudaError_t>(Ret);
48 }
49#endif
50};
51
52struct DispatchResult;
53
55protected:
57
58public:
60
61 virtual std::unique_ptr<MemoryBuffer>
62 compile(std::unique_ptr<LLVMContext> Ctx, std::unique_ptr<Module> M,
63 HashT ModuleHash, bool DisableIROpt = false) = 0;
64
65 virtual std::unique_ptr<CompiledLibrary>
67
69 LaunchDims BlockDim,
71 void *Stream) = 0;
72
73 virtual StringRef getDeviceArch() const = 0;
74
75 template <typename Sig, typename... ArgT>
76 typename sig_traits<Sig>::return_type run(void *FuncPtr, ArgT &&...Args) {
79 "Dispatcher run interface is only supported for host derived models");
80
81 auto Fn = reinterpret_cast<Sig *>(FuncPtr);
82 using Ret = typename sig_traits<Sig>::return_type;
83
84 if constexpr (std::is_void_v<Ret>) {
85 Fn(std::forward<ArgT>(Args)...);
86 return;
87 } else
88 return Fn(std::forward<ArgT>(Args)...);
89 }
90
92 CompiledLibrary &Library) = 0;
93
94 virtual void registerDynamicLibrary(HashT HashValue,
95 const SmallString<128> &Path) = 0;
96};
97
98} // namespace proteus
99
100#endif
char int void ** Args
Definition CompilerInterfaceHost.cpp:21
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
Definition Dispatcher.hpp:54
virtual StringRef getDeviceArch() const =0
TargetModelType TargetModel
Definition Dispatcher.hpp:56
virtual std::unique_ptr< CompiledLibrary > lookupCompiledLibrary(HashT ModuleHash)=0
static Dispatcher & getDispatcher(TargetModelType TargetModel)
Definition Dispatcher.cpp:55
virtual std::unique_ptr< MemoryBuffer > compile(std::unique_ptr< LLVMContext > Ctx, std::unique_ptr< Module > M, HashT ModuleHash, bool DisableIROpt=false)=0
virtual void registerDynamicLibrary(HashT HashValue, const SmallString< 128 > &Path)=0
virtual void * getFunctionAddress(StringRef FunctionName, HashT ModuleHash, CompiledLibrary &Library)=0
virtual DispatchResult launch(void *KernelFunc, LaunchDims GridDim, LaunchDims BlockDim, ArrayRef< void * > KernelArgs, uint64_t ShmemSize, void *Stream)=0
sig_traits< Sig >::return_type run(void *FuncPtr, ArgT &&...Args)
Definition Dispatcher.hpp:76
Definition Hashing.hpp:20
Definition Helpers.h:138
Definition BuiltinsCUDA.cpp:4
TargetModelType
Definition TargetModel.hpp:14
bool isHostTargetModel(TargetModelType TargetModel)
Definition TargetModel.hpp:55
T getRuntimeConstantValue(void *Arg)
Definition CompilerInterfaceRuntimeConstantInfo.h:114
Definition Dispatcher.hpp:16
unsigned Z
Definition Dispatcher.hpp:17
unsigned Y
Definition Dispatcher.hpp:17
unsigned X
Definition Dispatcher.hpp:17
Definition CompiledLibrary.hpp:12
Definition Dispatcher.hpp:32
constexpr DispatchResult(int Ret=0) noexcept
Definition Dispatcher.hpp:36
int Ret
Definition Dispatcher.hpp:33
R return_type
Definition Dispatcher.hpp:25
std::tuple< Args... > argument_types
Definition Dispatcher.hpp:26
Definition Dispatcher.hpp:22