Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
CompilerInterfaceRuntimeConstantInfo.h
Go to the documentation of this file.
1#ifndef PROTEUS_COMPILER_INTERFACE_RUNTIME_CONSTANT_INFO_H
2#define PROTEUS_COMPILER_INTERFACE_RUNTIME_CONSTANT_INFO_H
3
4#include <optional>
5
7#include "proteus/Error.h"
9
10#include "proteus/Debug.h"
11#include "proteus/Logger.hpp"
12
13namespace proteus {
14
15using namespace llvm;
16
17// This struct holds the information passed from the compiler pass for a runtime
18// constant argument to a function.
26
27// This struct holds the information from the compiler pass for a runtime
28// constant array, that is the number of elements, if a known compile time
29// constant, or a runtime constant argument as the number of elements, and the
30// element type.
32 int32_t NumElts = 0;
34
35 std::optional<RuntimeConstantArgInfo> OptNumEltsRCInfo = std::nullopt;
36
41 RuntimeConstantType NumEltsType,
42 int32_t NumEltsPos)
44 OptNumEltsRCInfo{RuntimeConstantArgInfo{NumEltsType, NumEltsPos}} {}
45};
46
47// This struct holds the information from the compiler pass for a runtime
48// constant object assumed trivially copyable, that is the size of the object
49// and whether it is passed by value.
57
58// This struct holds the information from the compiler pass for a runtime
59// constant, be it a scalar or an array. If the runtime constant is an array,
60// there is an optional variable to store the runtime constant array info.
63 std::optional<RuntimeConstantArrayInfo> OptArrInfo = std::nullopt;
64 std::optional<RuntimeConstantObjectInfo> OptObjInfo = std::nullopt;
65
67 : ArgInfo{Type, Pos} {
69 PROTEUS_FATAL_ERROR("Missing array info");
70 }
71
73 int32_t NumElts, RuntimeConstantType EltType)
74 : ArgInfo{Type, Pos},
76 if ((Type != RuntimeConstantType::ARRAY) &&
79 PROTEUS_FATAL_ERROR("Expected array runtime constant but type is " +
80 toString(Type));
81 }
82
84 RuntimeConstantType EltType,
85 RuntimeConstantType NumEltsType,
86 int32_t NumEltsPos)
87 : ArgInfo{Type, Pos},
88 OptArrInfo{RuntimeConstantArrayInfo{EltType, NumEltsType, NumEltsPos}} {
90 PROTEUS_FATAL_ERROR("Expected array runtime constant but type is " +
91 std::to_string(Type));
92 }
93
95 int32_t Size, bool PassByValue)
96 : ArgInfo{Type, Pos},
97 OptObjInfo{RuntimeConstantObjectInfo{Size, PassByValue}} {
99 PROTEUS_FATAL_ERROR("Expected object runtime constant but type is " +
100 std::to_string(Type));
101 }
102
103 bool operator==(const RuntimeConstantInfo &O) const {
104 return ((ArgInfo.Type == O.ArgInfo.Type) && (ArgInfo.Pos == O.ArgInfo.Pos));
105 }
106 bool operator!=(const RuntimeConstantInfo &O) const { return !(*this == O); }
107
108 // Compare by Pos.
109 bool operator<(const RuntimeConstantInfo &O) const {
110 return ArgInfo.Pos < O.ArgInfo.Pos;
111 }
112};
113
114template <typename T> inline T getRuntimeConstantValue(void *Arg) {
115 if constexpr (std::is_same_v<T, bool>) {
116 return *static_cast<bool *>(Arg);
117 } else if constexpr (std::is_same_v<T, int8_t>) {
118 return *static_cast<int8_t *>(Arg);
119 } else if constexpr (std::is_same_v<T, int32_t>) {
120 return *static_cast<int32_t *>(Arg);
121 } else if constexpr (std::is_same_v<T, int64_t>) {
122 return *static_cast<int64_t *>(Arg);
123 } else if constexpr (std::is_same_v<T, float>) {
124 return *static_cast<float *>(Arg);
125 } else if constexpr (std::is_same_v<T, double>) {
126 return *static_cast<double *>(Arg);
127 } else if constexpr (std::is_same_v<T, long double>) {
128 return *static_cast<long double *>(Arg);
129 } else if constexpr (std::is_pointer_v<T>) {
130 return static_cast<T>(*(intptr_t *)Arg);
131 } else {
132 PROTEUS_FATAL_ERROR("Unsupported type for runtime constant value");
133 }
134}
135
136inline RuntimeConstant
138 const RuntimeConstantInfo &RCInfo) {
139 RuntimeConstant RC{RCInfo.ArgInfo.Type, RCInfo.ArgInfo.Pos};
140
141 void *Arg = Args[RC.Pos];
142 switch (RC.Type) {
144 RC.Value.BoolVal = getRuntimeConstantValue<bool>(Arg);
145 PROTEUS_DBG(Logger::logs("proteus")
146 << "Value " << RC.Value.BoolVal << "\n");
147 break;
149 RC.Value.Int8Val = getRuntimeConstantValue<int8_t>(Arg);
150 PROTEUS_DBG(Logger::logs("proteus")
151 << "Value " << RC.Value.Int8Val << "\n");
152 break;
154 RC.Value.Int32Val = getRuntimeConstantValue<int32_t>(Arg);
155 PROTEUS_DBG(Logger::logs("proteus")
156 << "Value " << RC.Value.Int32Val << "\n");
157 break;
159 RC.Value.Int64Val = getRuntimeConstantValue<int64_t>(Arg);
160 PROTEUS_DBG(Logger::logs("proteus")
161 << "Value " << RC.Value.Int64Val << "\n");
162 break;
164 RC.Value.FloatVal = getRuntimeConstantValue<float>(Arg);
165 PROTEUS_DBG(Logger::logs("proteus")
166 << "Value " << RC.Value.FloatVal << "\n");
167 break;
169 RC.Value.DoubleVal = getRuntimeConstantValue<double>(Arg);
170 PROTEUS_DBG(Logger::logs("proteus")
171 << "Value " << RC.Value.DoubleVal << "\n");
172 break;
174 // NOTE: long double on device should correspond to plain double.
175 // XXX: CUDA with a long double SILENTLY fails to create a working
176 // kernel in AOT compilation, with or without JIT.
177 RC.Value.LongDoubleVal = getRuntimeConstantValue<long double>(Arg);
178 PROTEUS_DBG(Logger::logs("proteus")
179 << "Value " << std::to_string(RC.Value.LongDoubleVal) << "\n");
180 break;
182 RC.Value.PtrVal = (void *)getRuntimeConstantValue<intptr_t>(Arg);
183 PROTEUS_DBG(Logger::logs("proteus") << "Value " << RC.Value.PtrVal << "\n");
184 break;
187 if (RCInfo.OptArrInfo->OptNumEltsRCInfo) {
188 int32_t NumEltsPos = RCInfo.OptArrInfo->OptNumEltsRCInfo->Pos;
190 RCInfo.OptArrInfo->OptNumEltsRCInfo->Type;
191
195
197 } else {
198 NumElts = RCInfo.OptArrInfo->NumElts;
199 }
200
201 size_t SizeInBytes = NumElts * getSizeInBytes(RCInfo.OptArrInfo->EltType);
202 std::shared_ptr<unsigned char[]> Blob{new unsigned char[SizeInBytes]};
203 // The interface is a pointer-to-pointer so we need to deref it to copy the
204 // data.
206 std::memcpy(Blob.get(), Src, SizeInBytes);
207
208 RC.ArrInfo = ArrayInfo{NumElts, RCInfo.OptArrInfo->EltType, Blob};
209
210 PROTEUS_DBG(Logger::logs("proteus")
211 << "Value Blob ptr " << Blob.get() << "\n");
212 break;
213 }
215 size_t SizeInBytes =
216 RCInfo.OptArrInfo->NumElts * getSizeInBytes(RCInfo.OptArrInfo->EltType);
217 std::shared_ptr<unsigned char[]> Blob{new unsigned char[SizeInBytes]};
218 // Static arrays are passed by value, so it is a pointer directly to the
219 // stack.
220 std::memcpy(Blob.get(), Arg, SizeInBytes);
221
222 RC.ArrInfo =
223 ArrayInfo{RCInfo.OptArrInfo->NumElts, RCInfo.OptArrInfo->EltType, Blob};
224
225 PROTEUS_DBG(Logger::logs("proteus")
226 << "Value Blob ptr " << Blob.get() << "\n");
227 break;
228 }
230 size_t SizeInBytes =
231 RCInfo.OptArrInfo->NumElts * getSizeInBytes(RCInfo.OptArrInfo->EltType);
232 std::shared_ptr<unsigned char[]> Blob{new unsigned char[SizeInBytes]};
233 // Vectors are passed by value, so it is a pointer directly to the stack.
234 std::memcpy(Blob.get(), Arg, SizeInBytes);
235
236 RC.ArrInfo =
237 ArrayInfo{RCInfo.OptArrInfo->NumElts, RCInfo.OptArrInfo->EltType, Blob};
238
239 PROTEUS_DBG(Logger::logs("proteus")
240 << "Value Blob ptr " << Blob.get() << "\n");
241 break;
242 }
244 std::shared_ptr<unsigned char[]> Blob{
245 new unsigned char[RCInfo.OptObjInfo->Size]};
246
247 void *Src = (RCInfo.OptObjInfo->PassByValue
248 ? Args[RCInfo.ArgInfo.Pos]
250 Args[RCInfo.ArgInfo.Pos]));
251 std::memcpy(Blob.get(), Src, RCInfo.OptObjInfo->Size);
252
253 RC.ObjInfo = ObjectInfo{RCInfo.OptObjInfo->Size,
254 RCInfo.OptObjInfo->PassByValue, Blob};
255
256 PROTEUS_DBG(Logger::logs("proteus") << "Value " << RC.Value.PtrVal << "\n");
257 break;
258 }
259 default:
260 PROTEUS_FATAL_ERROR("Unsupported runtime constant type: " +
261 toString(RC.Type));
262 }
263
264 return RC;
265}
266
267}; // namespace proteus
268
269#endif
char int void ** Args
Definition CompilerInterfaceHost.cpp:21
#define PROTEUS_DBG(x)
Definition Debug.h:9
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
static llvm::raw_ostream & logs(const std::string &Name)
Definition Logger.hpp:19
Definition Helpers.h:138
Definition BuiltinsCUDA.cpp:4
static int Pos
Definition JitInterface.hpp:105
RuntimeConstantType
Definition CompilerInterfaceTypes.h:20
@ ARRAY
Definition CompilerInterfaceTypes.h:33
@ VECTOR
Definition CompilerInterfaceTypes.h:32
@ INT32
Definition CompilerInterfaceTypes.h:25
@ INT64
Definition CompilerInterfaceTypes.h:26
@ FLOAT
Definition CompilerInterfaceTypes.h:27
@ STATIC_ARRAY
Definition CompilerInterfaceTypes.h:31
@ LONG_DOUBLE
Definition CompilerInterfaceTypes.h:29
@ INT8
Definition CompilerInterfaceTypes.h:24
@ BOOL
Definition CompilerInterfaceTypes.h:23
@ OBJECT
Definition CompilerInterfaceTypes.h:34
@ PTR
Definition CompilerInterfaceTypes.h:30
@ DOUBLE
Definition CompilerInterfaceTypes.h:28
T getRuntimeConstantValue(void *Arg)
Definition CompilerInterfaceRuntimeConstantInfo.h:114
size_t NumElts
Definition JitInterface.hpp:43
std::string toString(CodegenOption Option)
Definition Config.hpp:26
size_t getSizeInBytes(RuntimeConstantType RCType)
Definition RuntimeConstantTypeHelpers.h:73
RuntimeConstant dispatchGetRuntimeConstantValue(void **Args, const RuntimeConstantInfo &RCInfo)
Definition CompilerInterfaceRuntimeConstantInfo.h:137
Definition CompilerInterfaceTypes.h:41
int32_t NumElts
Definition CompilerInterfaceTypes.h:42
RuntimeConstantType EltType
Definition CompilerInterfaceTypes.h:43
Definition CompilerInterfaceTypes.h:50
int32_t Size
Definition CompilerInterfaceTypes.h:51
Definition CompilerInterfaceRuntimeConstantInfo.h:19
int32_t Pos
Definition CompilerInterfaceRuntimeConstantInfo.h:21
RuntimeConstantType Type
Definition CompilerInterfaceRuntimeConstantInfo.h:20
RuntimeConstantArgInfo(RuntimeConstantType Type, int32_t Pos)
Definition CompilerInterfaceRuntimeConstantInfo.h:23
Definition CompilerInterfaceRuntimeConstantInfo.h:31
int32_t NumElts
Definition CompilerInterfaceRuntimeConstantInfo.h:32
RuntimeConstantType EltType
Definition CompilerInterfaceRuntimeConstantInfo.h:33
RuntimeConstantArrayInfo(RuntimeConstantType EltType, RuntimeConstantType NumEltsType, int32_t NumEltsPos)
Definition CompilerInterfaceRuntimeConstantInfo.h:40
std::optional< RuntimeConstantArgInfo > OptNumEltsRCInfo
Definition CompilerInterfaceRuntimeConstantInfo.h:35
RuntimeConstantArrayInfo(int32_t NumElts, RuntimeConstantType EltType)
Definition CompilerInterfaceRuntimeConstantInfo.h:37
Definition CompilerInterfaceRuntimeConstantInfo.h:61
bool operator!=(const RuntimeConstantInfo &O) const
Definition CompilerInterfaceRuntimeConstantInfo.h:106
RuntimeConstantInfo(RuntimeConstantType Type, int32_t Pos)
Definition CompilerInterfaceRuntimeConstantInfo.h:66
std::optional< RuntimeConstantArrayInfo > OptArrInfo
Definition CompilerInterfaceRuntimeConstantInfo.h:63
RuntimeConstantInfo(RuntimeConstantType Type, int32_t Pos, int32_t NumElts, RuntimeConstantType EltType)
Definition CompilerInterfaceRuntimeConstantInfo.h:72
RuntimeConstantInfo(RuntimeConstantType Type, int32_t Pos, int32_t Size, bool PassByValue)
Definition CompilerInterfaceRuntimeConstantInfo.h:94
bool operator<(const RuntimeConstantInfo &O) const
Definition CompilerInterfaceRuntimeConstantInfo.h:109
bool operator==(const RuntimeConstantInfo &O) const
Definition CompilerInterfaceRuntimeConstantInfo.h:103
RuntimeConstantArgInfo ArgInfo
Definition CompilerInterfaceRuntimeConstantInfo.h:62
RuntimeConstantInfo(RuntimeConstantType Type, int32_t Pos, RuntimeConstantType EltType, RuntimeConstantType NumEltsType, int32_t NumEltsPos)
Definition CompilerInterfaceRuntimeConstantInfo.h:83
std::optional< RuntimeConstantObjectInfo > OptObjInfo
Definition CompilerInterfaceRuntimeConstantInfo.h:64
Definition CompilerInterfaceRuntimeConstantInfo.h:50
bool PassByValue
Definition CompilerInterfaceRuntimeConstantInfo.h:52
int32_t Size
Definition CompilerInterfaceRuntimeConstantInfo.h:51
RuntimeConstantObjectInfo(int32_t Size, bool PassByValue)
Definition CompilerInterfaceRuntimeConstantInfo.h:54
Definition CompilerInterfaceTypes.h:72