| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* llvmjit_types.c |
| 4 |
* List of types needed by JIT emitting code. |
| 5 |
* |
| 6 |
* JIT emitting code often needs to access struct elements, create functions |
| 7 |
* with the correct signature etc. To allow synchronizing these types with a |
| 8 |
* low chance of definitions getting out of sync, this file lists types and |
| 9 |
* functions that directly need to be accessed from LLVM. |
| 10 |
* |
| 11 |
* When LLVM is first used in a backend, a bitcode version of this file will |
| 12 |
* be loaded. The needed types and signatures will be stored into Struct*, |
| 13 |
* Type*, Func* variables. |
| 14 |
* |
| 15 |
* NB: This file will not be linked into the server, it's just converted to |
| 16 |
* bitcode. |
| 17 |
* |
| 18 |
* |
| 19 |
* Copyright (c) 2016-2025, PostgreSQL Global Development Group |
| 20 |
* |
| 21 |
* IDENTIFICATION |
| 22 |
* src/backend/jit/llvm/llvmjit_types.c |
| 23 |
* |
| 24 |
*------------------------------------------------------------------------- |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "postgres.h" |
| 28 |
|
| 29 |
#include "access/htup.h" |
| 30 |
#include "access/htup_details.h" |
| 31 |
#include "access/tupdesc.h" |
| 32 |
#include "catalog/pg_attribute.h" |
| 33 |
#include "executor/execExpr.h" |
| 34 |
#include "executor/nodeAgg.h" |
| 35 |
#include "executor/tuptable.h" |
| 36 |
#include "fmgr.h" |
| 37 |
#include "nodes/execnodes.h" |
| 38 |
#include "nodes/memnodes.h" |
| 39 |
#include "utils/expandeddatum.h" |
| 40 |
#include "utils/palloc.h" |
| 41 |
|
| 42 |
|
| 43 |
/* |
| 44 |
* List of types needed for JITing. These have to be non-static, otherwise |
| 45 |
* clang/LLVM will omit them. As this file will never be linked into |
| 46 |
* anything, that's harmless. |
| 47 |
*/ |
| 48 |
PGFunction TypePGFunction; |
| 49 |
size_t TypeSizeT; |
| 50 |
bool TypeStorageBool; |
| 51 |
|
| 52 |
ExecEvalSubroutine TypeExecEvalSubroutine; |
| 53 |
ExecEvalBoolSubroutine TypeExecEvalBoolSubroutine; |
| 54 |
|
| 55 |
NullableDatum StructNullableDatum; |
| 56 |
AggState StructAggState; |
| 57 |
AggStatePerGroupData StructAggStatePerGroupData; |
| 58 |
AggStatePerTransData StructAggStatePerTransData; |
| 59 |
ExprContext StructExprContext; |
| 60 |
ExprEvalStep StructExprEvalStep; |
| 61 |
ExprState StructExprState; |
| 62 |
FunctionCallInfoBaseData StructFunctionCallInfoData; |
| 63 |
HeapTupleData StructHeapTupleData; |
| 64 |
HeapTupleHeaderData StructHeapTupleHeaderData; |
| 65 |
MemoryContextData StructMemoryContextData; |
| 66 |
TupleTableSlot StructTupleTableSlot; |
| 67 |
HeapTupleTableSlot StructHeapTupleTableSlot; |
| 68 |
MinimalTupleTableSlot StructMinimalTupleTableSlot; |
| 69 |
TupleDescData StructTupleDescData; |
| 70 |
PlanState StructPlanState; |
| 71 |
MinimalTupleData StructMinimalTupleData; |
| 72 |
|
| 73 |
|
| 74 |
/* |
| 75 |
* To determine which attributes functions need to have (depends e.g. on |
| 76 |
* compiler version and settings) to be compatible for inlining, we simply |
| 77 |
* copy the attributes of this function. |
| 78 |
*/ |
| 79 |
extern Datum AttributeTemplate(PG_FUNCTION_ARGS); |
| 80 |
Datum |
| 81 |
AttributeTemplate(PG_FUNCTION_ARGS) |
| 82 |
{ |
| 83 |
AssertVariableIsOfType(&AttributeTemplate, PGFunction); |
| 84 |
|
| 85 |
PG_RETURN_NULL(); |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* And some more "templates" to give us examples of function types |
| 90 |
* corresponding to function pointer types. |
| 91 |
*/ |
| 92 |
|
| 93 |
extern void ExecEvalSubroutineTemplate(ExprState *state, |
| 94 |
struct ExprEvalStep *op, |
| 95 |
ExprContext *econtext); |
| 96 |
void |
| 97 |
ExecEvalSubroutineTemplate(ExprState *state, |
| 98 |
struct ExprEvalStep *op, |
| 99 |
ExprContext *econtext) |
| 100 |
{ |
| 101 |
AssertVariableIsOfType(&ExecEvalSubroutineTemplate, |
| 102 |
ExecEvalSubroutine); |
| 103 |
} |
| 104 |
|
| 105 |
extern bool ExecEvalBoolSubroutineTemplate(ExprState *state, |
| 106 |
struct ExprEvalStep *op, |
| 107 |
ExprContext *econtext); |
| 108 |
bool |
| 109 |
ExecEvalBoolSubroutineTemplate(ExprState *state, |
| 110 |
struct ExprEvalStep *op, |
| 111 |
ExprContext *econtext) |
| 112 |
{ |
| 113 |
AssertVariableIsOfType(&ExecEvalBoolSubroutineTemplate, |
| 114 |
ExecEvalBoolSubroutine); |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
/* |
| 120 |
* Clang represents bool returned by functions differently (as i1) than stored |
| 121 |
* ones (as i8). Therefore we do not just need TypeStorageBool (above), but |
| 122 |
* also a way to determine the width of a returned integer. |
| 123 |
*/ |
| 124 |
extern bool FunctionReturningBool(void); |
| 125 |
bool |
| 126 |
FunctionReturningBool(void) |
| 127 |
{ |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* To force signatures of functions used during JITing to be present, |
| 133 |
* reference the functions required. This again has to be non-static, to avoid |
| 134 |
* being removed as unnecessary. |
| 135 |
*/ |
| 136 |
void *referenced_functions[] = |
| 137 |
{ |
| 138 |
ExecAggInitGroup, |
| 139 |
ExecAggCopyTransValue, |
| 140 |
ExecEvalPreOrderedDistinctSingle, |
| 141 |
ExecEvalPreOrderedDistinctMulti, |
| 142 |
ExecEvalAggOrderedTransDatum, |
| 143 |
ExecEvalAggOrderedTransTuple, |
| 144 |
ExecEvalArrayCoerce, |
| 145 |
ExecEvalArrayExpr, |
| 146 |
ExecEvalConstraintCheck, |
| 147 |
ExecEvalConstraintNotNull, |
| 148 |
ExecEvalConvertRowtype, |
| 149 |
ExecEvalCurrentOfExpr, |
| 150 |
ExecEvalFieldSelect, |
| 151 |
ExecEvalFieldStoreDeForm, |
| 152 |
ExecEvalFieldStoreForm, |
| 153 |
ExecEvalFuncExprFusage, |
| 154 |
ExecEvalFuncExprStrictFusage, |
| 155 |
ExecEvalGroupingFunc, |
| 156 |
ExecEvalMergeSupportFunc, |
| 157 |
ExecEvalMinMax, |
| 158 |
ExecEvalNextValueExpr, |
| 159 |
ExecEvalParamExec, |
| 160 |
ExecEvalParamExtern, |
| 161 |
ExecEvalParamSet, |
| 162 |
ExecEvalRow, |
| 163 |
ExecEvalRowNotNull, |
| 164 |
ExecEvalRowNull, |
| 165 |
ExecEvalCoerceViaIOSafe, |
| 166 |
ExecEvalSQLValueFunction, |
| 167 |
ExecEvalScalarArrayOp, |
| 168 |
ExecEvalHashedScalarArrayOp, |
| 169 |
ExecEvalSubPlan, |
| 170 |
ExecEvalSysVar, |
| 171 |
ExecEvalWholeRowVar, |
| 172 |
ExecEvalXmlExpr, |
| 173 |
ExecEvalJsonConstructor, |
| 174 |
ExecEvalJsonIsPredicate, |
| 175 |
ExecEvalJsonCoercion, |
| 176 |
ExecEvalJsonCoercionFinish, |
| 177 |
ExecEvalJsonExprPath, |
| 178 |
MakeExpandedObjectReadOnlyInternal, |
| 179 |
slot_getmissingattrs, |
| 180 |
slot_getsomeattrs_int, |
| 181 |
strlen, |
| 182 |
varsize_any, |
| 183 |
ExecInterpExprStillValid, |
| 184 |
}; |