Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
JitInterface.hpp
Go to the documentation of this file.
1//===-- jit.hpp -- user interface to Proteus JIT library --===//
2//
3// Part of the Proteus Project, under the Apache License v2.0 with LLVM
4// Exceptions. See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//===----------------------------------------------------------------------===//
10
11// NOLINTBEGIN(readability-identifier-naming)
12
13#ifndef PROTEUS_JIT_INTERFACE_HPP
14#define PROTEUS_JIT_INTERFACE_HPP
15
17
18#include <cassert>
19#include <cstring>
20#include <utility>
21
23extern "C" void __jit_register_lambda(const char *Symbol);
24extern "C" void __jit_init_host();
25extern "C" void __jit_init_device();
26extern "C" void __jit_finalize_host();
27extern "C" void __jit_finalize_device();
28extern "C" void __jit_enable_host();
29extern "C" void __jit_enable_device();
30extern "C" void __jit_disable_host();
31extern "C" void __jit_disable_device();
32
33namespace proteus {
34
35template <typename T> __attribute__((noinline)) void jit_arg(T V) noexcept;
36#if defined(__CUDACC__) || defined(__HIP__)
37template <typename T>
38__attribute__((noinline)) __device__ void jit_arg(T V) noexcept;
39#endif
40
41template <typename T>
42__attribute__((noinline)) void
43jit_array(T V, [[maybe_unused]] size_t NumElts,
44 [[maybe_unused]]
45 typename std::remove_pointer<T>::type Velem = 0) noexcept;
46#if defined(__CUDACC__) || defined(__HIP__)
47template <typename T>
48__attribute__((noinline)) __device__ void
49jit_array(T V, [[maybe_unused]] size_t NumElts,
50 [[maybe_unused]]
51 typename std::remove_pointer<T>::type Velem = 0) noexcept;
52#endif
53
54template <typename T>
55__attribute__((noinline))
56std::enable_if_t<std::is_trivially_copyable_v<std::remove_pointer_t<T>>, void>
57jit_object(T *V, size_t Size = sizeof(std::remove_pointer_t<T>)) noexcept;
58
59#if defined(__CUDACC__) || defined(__HIP__)
60template <typename T>
61__attribute__((noinline)) __device__ std::enable_if_t<
62 std::is_trivially_copyable_v<std::remove_pointer_t<T>>, void>
63jit_object(T *V, size_t Size = sizeof(T)) noexcept;
64#endif
65
66template <typename T>
67__attribute__((noinline))
68std::enable_if_t<!std::is_pointer_v<T> &&
69 std::is_trivially_copyable_v<std::remove_reference_t<T>>,
70 void>
71jit_object(T &V, size_t Size = sizeof(std::remove_reference_t<T>)) noexcept;
72
73#if defined(__CUDACC__) || defined(__HIP__)
74template <typename T>
75__attribute__((noinline)) __device__ std::enable_if_t<
76 !std::is_pointer_v<T> &&
77 std::is_trivially_copyable_v<std::remove_reference_t<T>>,
78 void>
79jit_object(T &V, size_t Size = sizeof(T)) noexcept;
80#endif
81
82template <typename T> inline static RuntimeConstantType convertCTypeToRCType() {
83 if constexpr (std::is_same_v<T, bool>) {
85 } else if constexpr (std::is_integral_v<T> && sizeof(T) == sizeof(int8_t)) {
87 } else if constexpr (std::is_integral_v<T> && sizeof(T) == sizeof(int32_t)) {
89 } else if constexpr (std::is_integral_v<T> && sizeof(T) == sizeof(int64_t)) {
91 } else if constexpr (std::is_same_v<T, float>) {
93 } else if constexpr (std::is_same_v<T, double>) {
95 } else if constexpr (std::is_same_v<T, long double>) {
97 } else if constexpr (std::is_pointer_v<T>) {
99 } else {
101 }
102}
103
104template <typename T>
105static __attribute__((noinline)) T jit_variable(T V, int Pos = -1,
106 int Offset = -1) noexcept {
107 RuntimeConstant RC{convertCTypeToRCType<T>(), Pos, Offset};
108 std::memcpy(&RC, &V, sizeof(T));
110
111 return V;
112}
113
114template <typename T>
115static __attribute__((noinline)) T &&
116register_lambda(T &&t, const char *Symbol = "") noexcept {
117 assert(Symbol && "Expected non-null Symbol");
118 __jit_register_lambda(Symbol);
119 return std::forward<T>(t);
120}
121
122#if defined(__CUDACC__) || defined(__HIP__)
123template <typename T>
124__device__ __attribute__((noinline)) T *
125shared_array([[maybe_unused]] size_t N,
126 [[maybe_unused]] size_t ElemSize = sizeof(T)) {
127 __builtin_trap();
128}
129#endif
130
131inline void enable() {
134}
135
136inline void disable() {
139}
140
141inline void init() {
143#if PROTEUS_ENABLE_HIP || PROTEUS_ENABLE_CUDA
145#endif
146}
147
148inline void finalize() {
150#if PROTEUS_ENABLE_HIP || PROTEUS_ENABLE_CUDA
152#endif
153}
154
155} // namespace proteus
156
157#endif
158
159// NOLINTEND(readability-identifier-naming)
void __jit_init_device()
Definition CompilerInterfaceDevice.cpp:65
void __jit_enable_device()
Definition CompilerInterfaceDevice.cpp:72
void __jit_disable_device()
Definition CompilerInterfaceDevice.cpp:77
void __jit_finalize_device()
Definition CompilerInterfaceDevice.cpp:67
void __jit_init_host()
Definition CompilerInterfaceHost.cpp:41
void __jit_disable_host()
Definition CompilerInterfaceHost.cpp:50
void __jit_enable_host()
Definition CompilerInterfaceHost.cpp:45
void __jit_finalize_host()
Definition CompilerInterfaceHost.cpp:43
void __jit_init_host()
Definition CompilerInterfaceHost.cpp:41
void __jit_init_device()
Definition CompilerInterfaceDevice.cpp:65
void __jit_enable_device()
Definition CompilerInterfaceDevice.cpp:72
void __jit_disable_host()
Definition CompilerInterfaceHost.cpp:50
void __jit_enable_host()
Definition CompilerInterfaceHost.cpp:45
void __jit_disable_device()
Definition CompilerInterfaceDevice.cpp:77
void __jit_finalize_host()
Definition CompilerInterfaceHost.cpp:43
void __jit_push_variable(proteus::RuntimeConstant RC)
void __jit_register_lambda(const char *Symbol)
void __jit_finalize_device()
Definition CompilerInterfaceDevice.cpp:67
Definition CppJitModule.cpp:21
size_t std::remove_pointer< T >::type Velem
Definition JitInterface.hpp:45
static int Pos
Definition JitInterface.hpp:105
RuntimeConstantType
Definition CompilerInterfaceTypes.h:20
@ NONE
Definition CompilerInterfaceTypes.h:22
@ INT32
Definition CompilerInterfaceTypes.h:25
@ INT64
Definition CompilerInterfaceTypes.h:26
@ FLOAT
Definition CompilerInterfaceTypes.h:27
@ LONG_DOUBLE
Definition CompilerInterfaceTypes.h:29
@ INT8
Definition CompilerInterfaceTypes.h:24
@ BOOL
Definition CompilerInterfaceTypes.h:23
@ PTR
Definition CompilerInterfaceTypes.h:30
@ DOUBLE
Definition CompilerInterfaceTypes.h:28
size_t NumElts
Definition JitInterface.hpp:43
__attribute__((noinline)) void jit_arg(T V) noexcept
Definition CompilerInterfaceTypes.h:72