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
5#include "proteus/Debug.h"
6#include "proteus/Error.h"
7#include "proteus/Logger.hpp"
9
10#include <optional>
11
12namespace proteus {
13
14using namespace llvm;
15
16// This struct holds the information passed from the compiler pass for a runtime
17// constant argument to a function.
25
26// This struct holds the information from the compiler pass for a runtime
27// constant array, that is the number of elements, if a known compile time
28// constant, or a runtime constant argument as the number of elements, and the
29// element type.
31 int32_t NumElts = 0;
33
34 std::optional<RuntimeConstantArgInfo> OptNumEltsRCInfo = std::nullopt;
35
40 RuntimeConstantType NumEltsType,
41 int32_t NumEltsPos)
43 OptNumEltsRCInfo{RuntimeConstantArgInfo{NumEltsType, NumEltsPos}} {}
44};
45
46// This struct holds the information from the compiler pass for a runtime
47// constant object assumed trivially copyable, that is the size of the object
48// and whether it is passed by value.
56
57// This struct holds the information from the compiler pass for a runtime
58// constant, be it a scalar or an array. If the runtime constant is an array,
59// there is an optional variable to store the runtime constant array info.
62 std::optional<RuntimeConstantArrayInfo> OptArrInfo = std::nullopt;
63 std::optional<RuntimeConstantObjectInfo> OptObjInfo = std::nullopt;
64
66 : ArgInfo{Type, Pos} {
68 reportFatalError("Missing array info");
69 }
70
72 int32_t NumElts, RuntimeConstantType EltType)
73 : ArgInfo{Type, Pos},
75 if ((Type != RuntimeConstantType::ARRAY) &&
78 reportFatalError("Expected array runtime constant but type is " +
79 toString(Type));
80 }
81
83 RuntimeConstantType EltType,
84 RuntimeConstantType NumEltsType,
85 int32_t NumEltsPos)
86 : ArgInfo{Type, Pos},
87 OptArrInfo{RuntimeConstantArrayInfo{EltType, NumEltsType, NumEltsPos}} {
89 reportFatalError("Expected array runtime constant but type is " +
90 std::to_string(Type));
91 }
92
94 int32_t Size, bool PassByValue)
95 : ArgInfo{Type, Pos},
96 OptObjInfo{RuntimeConstantObjectInfo{Size, PassByValue}} {
98 reportFatalError("Expected object runtime constant but type is " +
99 std::to_string(Type));
100 }
101
102 bool operator==(const RuntimeConstantInfo &O) const {
103 return ((ArgInfo.Type == O.ArgInfo.Type) && (ArgInfo.Pos == O.ArgInfo.Pos));
104 }
105 bool operator!=(const RuntimeConstantInfo &O) const { return !(*this == O); }
106
107 // Compare by Pos.
108 bool operator<(const RuntimeConstantInfo &O) const {
109 return ArgInfo.Pos < O.ArgInfo.Pos;
110 }
111};
112
113template <typename T> inline T getRuntimeConstantValue(void *Arg) {
114 if constexpr (std::is_same_v<T, bool>) {
115 return *static_cast<bool *>(Arg);
116 } else if constexpr (std::is_same_v<T, int8_t>) {
117 return *static_cast<int8_t *>(Arg);
118 } else if constexpr (std::is_same_v<T, int32_t>) {
119 return *static_cast<int32_t *>(Arg);
120 } else if constexpr (std::is_same_v<T, int64_t>) {
121 return *static_cast<int64_t *>(Arg);
122 } else if constexpr (std::is_same_v<T, float>) {
123 return *static_cast<float *>(Arg);
124 } else if constexpr (std::is_same_v<T, double>) {
125 return *static_cast<double *>(Arg);
126 } else if constexpr (std::is_same_v<T, long double>) {
127 return *static_cast<long double *>(Arg);
128 } else if constexpr (std::is_pointer_v<T>) {
129 return static_cast<T>(*(intptr_t *)Arg);
130 } else {
131 reportFatalError("Unsupported type for runtime constant value");
132 }
133}
134
135inline RuntimeConstant
137 const RuntimeConstantInfo &RCInfo) {
138 RuntimeConstant RC{RCInfo.ArgInfo.Type, RCInfo.ArgInfo.Pos};
139
140 void *Arg = Args[RC.Pos];
141 switch (RC.Type) {
143 RC.Value.BoolVal = getRuntimeConstantValue<bool>(Arg);
144 PROTEUS_DBG(Logger::logs("proteus")
145 << "Value " << RC.Value.BoolVal << "\n");
146 break;
148 RC.Value.Int8Val = getRuntimeConstantValue<int8_t>(Arg);
149 PROTEUS_DBG(Logger::logs("proteus")
150 << "Value " << RC.Value.Int8Val << "\n");
151 break;
153 RC.Value.Int32Val = getRuntimeConstantValue<int32_t>(Arg);
154 PROTEUS_DBG(Logger::logs("proteus")
155 << "Value " << RC.Value.Int32Val << "\n");
156 break;
158 RC.Value.Int64Val = getRuntimeConstantValue<int64_t>(Arg);
159 PROTEUS_DBG(Logger::logs("proteus")
160 << "Value " << RC.Value.Int64Val << "\n");
161 break;
163 RC.Value.FloatVal = getRuntimeConstantValue<float>(Arg);
164 PROTEUS_DBG(Logger::logs("proteus")
165 << "Value " << RC.Value.FloatVal << "\n");
166 break;
168 RC.Value.DoubleVal = getRuntimeConstantValue<double>(Arg);
169 PROTEUS_DBG(Logger::logs("proteus")
170 << "Value " << RC.Value.DoubleVal << "\n");
171 break;
173 // NOTE: long double on device should correspond to plain double.
174 // XXX: CUDA with a long double SILENTLY fails to create a working
175 // kernel in AOT compilation, with or without JIT.
176 RC.Value.LongDoubleVal = getRuntimeConstantValue<long double>(Arg);
177 PROTEUS_DBG(Logger::logs("proteus")
178 << "Value " << std::to_string(RC.Value.LongDoubleVal) << "\n");
179 break;
181 RC.Value.PtrVal = (void *)getRuntimeConstantValue<intptr_t>(Arg);
182 PROTEUS_DBG(Logger::logs("proteus") << "Value " << RC.Value.PtrVal << "\n");
183 break;
186 if (RCInfo.OptArrInfo->OptNumEltsRCInfo) {
187 int32_t NumEltsPos = RCInfo.OptArrInfo->OptNumEltsRCInfo->Pos;
189 RCInfo.OptArrInfo->OptNumEltsRCInfo->Type;
190
194
196 } else {
197 NumElts = RCInfo.OptArrInfo->NumElts;
198 }
199
200 size_t SizeInBytes = NumElts * getSizeInBytes(RCInfo.OptArrInfo->EltType);
201 std::shared_ptr<unsigned char[]> Blob{new unsigned char[SizeInBytes]};
202 // The interface is a pointer-to-pointer so we need to deref it to copy the
203 // data.
205 std::memcpy(Blob.get(), Src, SizeInBytes);
206
207 RC.ArrInfo = ArrayInfo{NumElts, RCInfo.OptArrInfo->EltType, Blob};
208
209 PROTEUS_DBG(Logger::logs("proteus")
210 << "Value Blob ptr " << Blob.get() << "\n");
211 break;
212 }
214 size_t SizeInBytes =
215 RCInfo.OptArrInfo->NumElts * getSizeInBytes(RCInfo.OptArrInfo->EltType);
216 std::shared_ptr<unsigned char[]> Blob{new unsigned char[SizeInBytes]};
217 // Static arrays are passed by value, so it is a pointer directly to the
218 // stack.
219 std::memcpy(Blob.get(), Arg, SizeInBytes);
220
221 RC.ArrInfo =
222 ArrayInfo{RCInfo.OptArrInfo->NumElts, RCInfo.OptArrInfo->EltType, Blob};
223
224 PROTEUS_DBG(Logger::logs("proteus")
225 << "Value Blob ptr " << Blob.get() << "\n");
226 break;
227 }
229 size_t SizeInBytes =
230 RCInfo.OptArrInfo->NumElts * getSizeInBytes(RCInfo.OptArrInfo->EltType);
231 std::shared_ptr<unsigned char[]> Blob{new unsigned char[SizeInBytes]};
232 // Vectors are passed by value, so it is a pointer directly to the stack.
233 std::memcpy(Blob.get(), Arg, SizeInBytes);
234
235 RC.ArrInfo =
236 ArrayInfo{RCInfo.OptArrInfo->NumElts, RCInfo.OptArrInfo->EltType, Blob};
237
238 PROTEUS_DBG(Logger::logs("proteus")
239 << "Value Blob ptr " << Blob.get() << "\n");
240 break;
241 }
243 std::shared_ptr<unsigned char[]> Blob{
244 new unsigned char[RCInfo.OptObjInfo->Size]};
245
246 void *Src = (RCInfo.OptObjInfo->PassByValue
247 ? Args[RCInfo.ArgInfo.Pos]
249 Args[RCInfo.ArgInfo.Pos]));
250 std::memcpy(Blob.get(), Src, RCInfo.OptObjInfo->Size);
251
252 RC.ObjInfo = ObjectInfo{RCInfo.OptObjInfo->Size,
253 RCInfo.OptObjInfo->PassByValue, Blob};
254
255 PROTEUS_DBG(Logger::logs("proteus") << "Value " << RC.Value.PtrVal << "\n");
256 break;
257 }
258 default:
259 reportFatalError("Unsupported runtime constant type: " + toString(RC.Type));
260 }
261
262 return RC;
263}
264
265}; // namespace proteus
266
267#endif
char int void ** Args
Definition CompilerInterfaceHost.cpp:20
#define PROTEUS_DBG(x)
Definition Debug.h:9
static llvm::raw_ostream & logs(const std::string &Name)
Definition Logger.hpp:19
Definition Helpers.h:141
Definition ObjectCacheChain.cpp:26
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
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14
T getRuntimeConstantValue(void *Arg)
Definition CompilerInterfaceRuntimeConstantInfo.h:113
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:136
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:18
int32_t Pos
Definition CompilerInterfaceRuntimeConstantInfo.h:20
RuntimeConstantType Type
Definition CompilerInterfaceRuntimeConstantInfo.h:19
RuntimeConstantArgInfo(RuntimeConstantType Type, int32_t Pos)
Definition CompilerInterfaceRuntimeConstantInfo.h:22
Definition CompilerInterfaceRuntimeConstantInfo.h:30
int32_t NumElts
Definition CompilerInterfaceRuntimeConstantInfo.h:31
RuntimeConstantType EltType
Definition CompilerInterfaceRuntimeConstantInfo.h:32
RuntimeConstantArrayInfo(RuntimeConstantType EltType, RuntimeConstantType NumEltsType, int32_t NumEltsPos)
Definition CompilerInterfaceRuntimeConstantInfo.h:39
std::optional< RuntimeConstantArgInfo > OptNumEltsRCInfo
Definition CompilerInterfaceRuntimeConstantInfo.h:34
RuntimeConstantArrayInfo(int32_t NumElts, RuntimeConstantType EltType)
Definition CompilerInterfaceRuntimeConstantInfo.h:36
Definition CompilerInterfaceRuntimeConstantInfo.h:60
bool operator!=(const RuntimeConstantInfo &O) const
Definition CompilerInterfaceRuntimeConstantInfo.h:105
RuntimeConstantInfo(RuntimeConstantType Type, int32_t Pos)
Definition CompilerInterfaceRuntimeConstantInfo.h:65
std::optional< RuntimeConstantArrayInfo > OptArrInfo
Definition CompilerInterfaceRuntimeConstantInfo.h:62
RuntimeConstantInfo(RuntimeConstantType Type, int32_t Pos, int32_t NumElts, RuntimeConstantType EltType)
Definition CompilerInterfaceRuntimeConstantInfo.h:71
RuntimeConstantInfo(RuntimeConstantType Type, int32_t Pos, int32_t Size, bool PassByValue)
Definition CompilerInterfaceRuntimeConstantInfo.h:93
bool operator<(const RuntimeConstantInfo &O) const
Definition CompilerInterfaceRuntimeConstantInfo.h:108
bool operator==(const RuntimeConstantInfo &O) const
Definition CompilerInterfaceRuntimeConstantInfo.h:102
RuntimeConstantArgInfo ArgInfo
Definition CompilerInterfaceRuntimeConstantInfo.h:61
RuntimeConstantInfo(RuntimeConstantType Type, int32_t Pos, RuntimeConstantType EltType, RuntimeConstantType NumEltsType, int32_t NumEltsPos)
Definition CompilerInterfaceRuntimeConstantInfo.h:82
std::optional< RuntimeConstantObjectInfo > OptObjInfo
Definition CompilerInterfaceRuntimeConstantInfo.h:63
Definition CompilerInterfaceRuntimeConstantInfo.h:49
bool PassByValue
Definition CompilerInterfaceRuntimeConstantInfo.h:51
int32_t Size
Definition CompilerInterfaceRuntimeConstantInfo.h:50
RuntimeConstantObjectInfo(int32_t Size, bool PassByValue)
Definition CompilerInterfaceRuntimeConstantInfo.h:53
Definition CompilerInterfaceTypes.h:72