1/*-------------------------------------------------------------------------
2 * llvmjit.h
3 * LLVM JIT provider.
4 *
5 * Copyright (c) 2016-2019, PostgreSQL Global Development Group
6 *
7 * src/include/jit/llvmjit.h
8 *
9 *-------------------------------------------------------------------------
10 */
11#ifndef LLVMJIT_H
12#define LLVMJIT_H
13
14/*
15 * To avoid breaking cpluspluscheck, allow including the file even when LLVM
16 * is not available.
17 */
18#ifdef USE_LLVM
19
20#include <llvm-c/Types.h>
21
22
23/*
24 * File needs to be includable by both C and C++ code, and include other
25 * headers doing the same. Therefore wrap C portion in our own extern "C" if
26 * in C++ mode.
27 */
28#ifdef __cplusplus
29extern "C"
30{
31#endif
32
33
34#include "fmgr.h"
35#include "jit/jit.h"
36#include "nodes/pg_list.h"
37#include "access/tupdesc.h"
38
39
40typedef struct LLVMJitContext
41{
42 JitContext base;
43
44 /* number of modules created */
45 size_t module_generation;
46
47 /* current, "open for write", module */
48 LLVMModuleRef module;
49
50 /* is there any pending code that needs to be emitted */
51 bool compiled;
52
53 /* # of objects emitted, used to generate non-conflicting names */
54 int counter;
55
56 /* list of handles for code emitted via Orc */
57 List *handles;
58} LLVMJitContext;
59
60
61/* type and struct definitions */
62extern LLVMTypeRef TypeParamBool;
63extern LLVMTypeRef TypePGFunction;
64extern LLVMTypeRef TypeSizeT;
65extern LLVMTypeRef TypeStorageBool;
66
67extern LLVMTypeRef StructNullableDatum;
68extern LLVMTypeRef StructTupleDescData;
69extern LLVMTypeRef StructHeapTupleData;
70extern LLVMTypeRef StructTupleTableSlot;
71extern LLVMTypeRef StructHeapTupleTableSlot;
72extern LLVMTypeRef StructMinimalTupleTableSlot;
73extern LLVMTypeRef StructMemoryContextData;
74extern LLVMTypeRef StructFunctionCallInfoData;
75extern LLVMTypeRef StructExprContext;
76extern LLVMTypeRef StructExprEvalStep;
77extern LLVMTypeRef StructExprState;
78extern LLVMTypeRef StructAggState;
79extern LLVMTypeRef StructAggStatePerTransData;
80extern LLVMTypeRef StructAggStatePerGroupData;
81
82extern LLVMValueRef AttributeTemplate;
83extern LLVMValueRef FuncStrlen;
84extern LLVMValueRef FuncVarsizeAny;
85extern LLVMValueRef FuncSlotGetmissingattrs;
86extern LLVMValueRef FuncSlotGetsomeattrsInt;
87extern LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal;
88extern LLVMValueRef FuncExecEvalSubscriptingRef;
89extern LLVMValueRef FuncExecEvalSysVar;
90extern LLVMValueRef FuncExecAggTransReparent;
91extern LLVMValueRef FuncExecAggInitGroup;
92
93
94extern void llvm_enter_fatal_on_oom(void);
95extern void llvm_leave_fatal_on_oom(void);
96extern void llvm_reset_after_error(void);
97extern void llvm_assert_in_fatal_section(void);
98
99extern LLVMJitContext *llvm_create_context(int jitFlags);
100extern LLVMModuleRef llvm_mutable_module(LLVMJitContext *context);
101extern char *llvm_expand_funcname(LLVMJitContext *context, const char *basename);
102extern void *llvm_get_function(LLVMJitContext *context, const char *funcname);
103extern void llvm_split_symbol_name(const char *name, char **modname, char **funcname);
104extern LLVMValueRef llvm_get_decl(LLVMModuleRef mod, LLVMValueRef f);
105extern void llvm_copy_attributes(LLVMValueRef from, LLVMValueRef to);
106extern LLVMValueRef llvm_function_reference(LLVMJitContext *context,
107 LLVMBuilderRef builder,
108 LLVMModuleRef mod,
109 FunctionCallInfo fcinfo);
110
111extern void llvm_inline(LLVMModuleRef mod);
112
113/*
114 ****************************************************************************
115 * Code generation functions.
116 ****************************************************************************
117 */
118extern bool llvm_compile_expr(struct ExprState *state);
119struct TupleTableSlotOps;
120extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDesc desc,
121 const struct TupleTableSlotOps *ops, int natts);
122
123/*
124 ****************************************************************************
125 * Extensions / Backward compatibility section of the LLVM C API
126 * Error handling related functions.
127 ****************************************************************************
128 */
129#if defined(HAVE_DECL_LLVMGETHOSTCPUNAME) && !HAVE_DECL_LLVMGETHOSTCPUNAME
130/** Get the host CPU as a string. The result needs to be disposed with
131 LLVMDisposeMessage. */
132extern char *LLVMGetHostCPUName(void);
133#endif
134
135#if defined(HAVE_DECL_LLVMGETHOSTCPUFEATURES) && !HAVE_DECL_LLVMGETHOSTCPUFEATURES
136/** Get the host CPU features as a string. The result needs to be disposed
137 with LLVMDisposeMessage. */
138extern char *LLVMGetHostCPUFeatures(void);
139#endif
140
141#ifdef __cplusplus
142} /* extern "C" */
143#endif
144
145#endif /* USE_LLVM */
146#endif /* LLVMJIT_H */
147