PostgreSQL Source Code git master
Loading...
Searching...
No Matches
llvmjit_emit.h
Go to the documentation of this file.
1/*
2 * llvmjit_emit.h
3 * Helpers to make emitting LLVM IR a bit more concise and pgindent proof.
4 *
5 * Copyright (c) 2018-2026, PostgreSQL Global Development Group
6 *
7 * src/include/jit/llvmjit_emit.h
8 */
9#ifndef LLVMJIT_EMIT_H
10#define LLVMJIT_EMIT_H
11
12/*
13 * To avoid breaking cpluspluscheck, allow including the file even when LLVM
14 * is not available.
15 */
16#ifdef USE_LLVM
17
18#include <llvm-c/Core.h>
19#include <llvm-c/Target.h>
20
21#include "jit/llvmjit.h"
22
23
24/*
25 * Emit a non-LLVM pointer as an LLVM constant.
26 */
27static inline LLVMValueRef
29{
31
32 return LLVMConstIntToPtr(c, type);
33}
34
35/*
36 * Emit pointer.
37 */
38static inline LLVMTypeRef
40{
41 return LLVMPointerType(t, 0);
42}
43
44/*
45 * Emit constant integer.
46 */
47static inline LLVMValueRef
49{
50 return LLVMConstInt(LLVMInt8TypeInContext(lc), i, false);
51}
52
53/*
54 * Emit constant integer.
55 */
56static inline LLVMValueRef
58{
59 return LLVMConstInt(LLVMInt16TypeInContext(lc), i, false);
60}
61
62/*
63 * Emit constant integer.
64 */
65static inline LLVMValueRef
67{
68 return LLVMConstInt(LLVMInt32TypeInContext(lc), i, false);
69}
70
71/*
72 * Emit constant integer.
73 */
74static inline LLVMValueRef
76{
77 return LLVMConstInt(LLVMInt64TypeInContext(lc), i, false);
78}
79
80/*
81 * Emit constant integer.
82 */
83static inline LLVMValueRef
84l_sizet_const(size_t i)
85{
86 return LLVMConstInt(TypeSizeT, i, false);
87}
88
89/*
90 * Emit constant integer.
91 */
92static inline LLVMValueRef
94{
95 return LLVMConstInt(TypeDatum, i, false);
96}
97
98/*
99 * Emit constant boolean, as used for storage (e.g. global vars, structs).
100 */
101static inline LLVMValueRef
102l_sbool_const(bool i)
103{
104 return LLVMConstInt(TypeStorageBool, (int) i, false);
105}
106
107/*
108 * Emit constant boolean, as used for parameters (e.g. function parameters).
109 */
110static inline LLVMValueRef
111l_pbool_const(bool i)
112{
113 return LLVMConstInt(TypeParamBool, (int) i, false);
114}
115
116static inline LLVMValueRef
118{
119 return LLVMBuildStructGEP2(b, t, v, idx, "");
120}
121
122static inline LLVMValueRef
124{
125 return LLVMBuildGEP2(b, t, v, indices, nindices, name);
126}
127
128static inline LLVMValueRef
130{
131 return LLVMBuildLoad2(b, t, v, name);
132}
133
134static inline LLVMValueRef
136{
137 return LLVMBuildCall2(b, t, fn, args, nargs, name);
138}
139
140/*
141 * Load a pointer member idx from a struct.
142 */
143static inline LLVMValueRef
145{
146 return l_load(b,
148 l_struct_gep(b, t, v, idx, ""),
149 name);
150}
151
152/*
153 * Load value of a pointer, after applying one index operation.
154 */
155static inline LLVMValueRef
157{
158 return l_load(b, t, l_gep(b, t, v, &idx, 1, ""), name);
159}
160
161/* separate, because pg_attribute_printf(2, 3) can't appear in definition */
162static inline LLVMBasicBlockRef l_bb_before_v(LLVMBasicBlockRef r, const char *fmt,...) pg_attribute_printf(2, 3);
163
164/*
165 * Insert a new basic block, just before r, the name being determined by fmt
166 * and arguments.
167 */
170{
171 char buf[512];
174
175 va_start(args, fmt);
176 vsnprintf(buf, sizeof(buf), fmt, args);
177 va_end(args);
178
180
182}
183
184/* separate, because pg_attribute_printf(2, 3) can't appear in definition */
185static inline LLVMBasicBlockRef l_bb_append_v(LLVMValueRef f, const char *fmt,...) pg_attribute_printf(2, 3);
186
187/*
188 * Insert a new basic block after previous basic blocks, the name being
189 * determined by fmt and arguments.
190 */
193{
194 char buf[512];
197
198 va_start(args, fmt);
199 vsnprintf(buf, sizeof(buf), fmt, args);
200 va_end(args);
201
203
205}
206
207/*
208 * Mark a callsite as readonly.
209 */
210static inline void
212{
213 const char argname[] = "readonly";
215
217 argname,
218 sizeof(argname) - 1,
219 NULL, 0);
220
222}
223
224/*
225 * Mark a callsite as alwaysinline.
226 */
227static inline void
229{
230 const char argname[] = "alwaysinline";
231 int id;
232 LLVMAttributeRef attr;
233
235 sizeof(argname) - 1);
238}
239
240/*
241 * Emit code to switch memory context.
242 */
243static inline LLVMValueRef
245{
246 const char *cmc = "CurrentMemoryContext";
248 LLVMValueRef ret;
249
250 if (!(cur = LLVMGetNamedGlobal(mod, cmc)))
254
255 return ret;
256}
257
258/*
259 * Return pointer to the argno'th argument nullness.
260 */
261static inline LLVMValueRef
263{
266
269 v_fcinfo,
271 "");
274 v_args,
275 argno,
276 "");
277 return l_struct_gep(b,
279 v_argn,
281 "");
282}
283
284/*
285 * Return pointer to the argno'th argument datum.
286 */
287static inline LLVMValueRef
289{
292
295 v_fcinfo,
297 "");
300 v_args,
301 argno,
302 "");
303 return l_struct_gep(b,
305 v_argn,
307 "");
308}
309
310/*
311 * Return argno'th argument nullness.
312 */
313static inline LLVMValueRef
315{
317}
318
319/*
320 * Return argno'th argument datum.
321 */
322static inline LLVMValueRef
324{
325 return l_load(b, TypeDatum, l_funcvaluep(b, v_fcinfo, argno), "");
326}
327
328#endif /* USE_LLVM */
329#endif
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:262
int64_t int64
Definition c.h:543
#define pg_attribute_printf(f, a)
Definition c.h:242
int16_t int16
Definition c.h:541
int8_t int8
Definition c.h:540
int32_t int32
Definition c.h:542
struct cursor * cur
Definition ecpg.c:29
#define FIELDNO_FUNCTIONCALLINFODATA_ARGS
Definition fmgr.h:94
int b
Definition isn.c:74
int i
Definition isn.c:77
LLVMTypeRef StructFunctionCallInfoData
Definition llvmjit.c:70
LLVMTypeRef TypeParamBool
Definition llvmjit.c:58
LLVMTypeRef StructMemoryContextData
Definition llvmjit.c:69
LLVMTypeRef TypeSizeT
Definition llvmjit.c:56
LLVMTypeRef TypeStorageBool
Definition llvmjit.c:59
LLVMTypeRef TypeDatum
Definition llvmjit.c:57
LLVMTypeRef StructNullableDatum
Definition llvmjit.c:61
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define vsnprintf
Definition port.h:259
#define FIELDNO_NULLABLE_DATUM_ISNULL
Definition postgres.h:88
#define FIELDNO_NULLABLE_DATUM_DATUM
Definition postgres.h:86
uint64_t Datum
Definition postgres.h:70
char * c
static int fb(int x)
static void * fn(void *arg)
const char * type
const char * name