Commit 32af96b2 authored by Andres Freund's avatar Andres Freund

JIT tuple deforming in LLVM JIT provider.

Performing JIT compilation for deforming gains performance benefits
over unJITed deforming from compile-time knowledge of the tuple
descriptor. Fixed column widths, NOT NULLness, etc can be taken
advantage of.

Right now the JITed deforming is only used when deforming tuples as
part of expression evaluation (and obviously only if the descriptor is
known). It's likely to be beneficial in other cases, too.

By default tuple deforming is JITed whenever an expression is JIT
compiled. There's a separate boolean GUC controlling it, but that's
expected to be primarily useful for development and benchmarking.

Docs will follow in a later commit containing docs for the whole JIT
feature.

Author: Andres Freund
Discussion: https://postgr.es/m/20170901064131.tazjxwus3k2w3ybh@alap3.anarazel.de
parent 64f85894
...@@ -1556,3 +1556,13 @@ minimal_tuple_from_heap_tuple(HeapTuple htup) ...@@ -1556,3 +1556,13 @@ minimal_tuple_from_heap_tuple(HeapTuple htup)
result->t_len = len; result->t_len = len;
return result; return result;
} }
/*
* This mainly exists so JIT can inline the definition, but it's also
* sometimes useful in debugging sessions.
*/
size_t
varsize_any(void *p)
{
return VARSIZE_ANY(p);
}
...@@ -2287,18 +2287,21 @@ ExecPushExprSlots(ExprState *state, LastAttnumInfo *info) ...@@ -2287,18 +2287,21 @@ ExecPushExprSlots(ExprState *state, LastAttnumInfo *info)
{ {
scratch.opcode = EEOP_INNER_FETCHSOME; scratch.opcode = EEOP_INNER_FETCHSOME;
scratch.d.fetch.last_var = info->last_inner; scratch.d.fetch.last_var = info->last_inner;
scratch.d.fetch.known_desc = NULL;
ExprEvalPushStep(state, &scratch); ExprEvalPushStep(state, &scratch);
} }
if (info->last_outer > 0) if (info->last_outer > 0)
{ {
scratch.opcode = EEOP_OUTER_FETCHSOME; scratch.opcode = EEOP_OUTER_FETCHSOME;
scratch.d.fetch.last_var = info->last_outer; scratch.d.fetch.last_var = info->last_outer;
scratch.d.fetch.known_desc = NULL;
ExprEvalPushStep(state, &scratch); ExprEvalPushStep(state, &scratch);
} }
if (info->last_scan > 0) if (info->last_scan > 0)
{ {
scratch.opcode = EEOP_SCAN_FETCHSOME; scratch.opcode = EEOP_SCAN_FETCHSOME;
scratch.d.fetch.last_var = info->last_scan; scratch.d.fetch.last_var = info->last_scan;
scratch.d.fetch.known_desc = NULL;
ExprEvalPushStep(state, &scratch); ExprEvalPushStep(state, &scratch);
} }
} }
...@@ -3250,10 +3253,12 @@ ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc, ...@@ -3250,10 +3253,12 @@ ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
/* push deform steps */ /* push deform steps */
scratch.opcode = EEOP_INNER_FETCHSOME; scratch.opcode = EEOP_INNER_FETCHSOME;
scratch.d.fetch.last_var = maxatt; scratch.d.fetch.last_var = maxatt;
scratch.d.fetch.known_desc = ldesc;
ExprEvalPushStep(state, &scratch); ExprEvalPushStep(state, &scratch);
scratch.opcode = EEOP_OUTER_FETCHSOME; scratch.opcode = EEOP_OUTER_FETCHSOME;
scratch.d.fetch.last_var = maxatt; scratch.d.fetch.last_var = maxatt;
scratch.d.fetch.known_desc = rdesc;
ExprEvalPushStep(state, &scratch); ExprEvalPushStep(state, &scratch);
/* /*
......
...@@ -896,6 +896,7 @@ ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc) ...@@ -896,6 +896,7 @@ ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc)
{ {
scanstate->ss_ScanTupleSlot = ExecAllocTableSlot(&estate->es_tupleTable, scanstate->ss_ScanTupleSlot = ExecAllocTableSlot(&estate->es_tupleTable,
tupledesc); tupledesc);
scanstate->ps.scandesc = tupledesc;
} }
/* ---------------- /* ----------------
......
...@@ -186,7 +186,11 @@ ExecInitForeignScan(ForeignScan *node, EState *estate, int eflags) ...@@ -186,7 +186,11 @@ ExecInitForeignScan(ForeignScan *node, EState *estate, int eflags)
} }
else else
{ {
ExecInitScanTupleSlot(estate, &scanstate->ss, RelationGetDescr(currentRelation)); TupleDesc scan_tupdesc;
/* don't trust FDWs to return tuples fulfilling NOT NULL constraints */
scan_tupdesc = CreateTupleDescCopy(RelationGetDescr(currentRelation));
ExecInitScanTupleSlot(estate, &scanstate->ss, scan_tupdesc);
/* Node's targetlist will contain Vars with varno = scanrelid */ /* Node's targetlist will contain Vars with varno = scanrelid */
tlistvarno = scanrelid; tlistvarno = scanrelid;
} }
......
...@@ -38,6 +38,7 @@ bool jit_debugging_support = false; ...@@ -38,6 +38,7 @@ bool jit_debugging_support = false;
bool jit_dump_bitcode = false; bool jit_dump_bitcode = false;
bool jit_expressions = true; bool jit_expressions = true;
bool jit_profiling_support = false; bool jit_profiling_support = false;
bool jit_tuple_deforming = true;
double jit_above_cost = 100000; double jit_above_cost = 100000;
double jit_optimize_above_cost = 500000; double jit_optimize_above_cost = 500000;
......
...@@ -39,7 +39,7 @@ OBJS=$(WIN32RES) ...@@ -39,7 +39,7 @@ OBJS=$(WIN32RES)
# Infrastructure # Infrastructure
OBJS += llvmjit.o llvmjit_error.o llvmjit_wrap.o OBJS += llvmjit.o llvmjit_error.o llvmjit_wrap.o
# Code generation # Code generation
OBJS += llvmjit_expr.o OBJS += llvmjit_expr.o llvmjit_deform.o
all: all-shared-lib llvmjit_types.bc all: all-shared-lib llvmjit_types.bc
......
...@@ -74,6 +74,7 @@ LLVMTypeRef StructAggStatePerTransData; ...@@ -74,6 +74,7 @@ LLVMTypeRef StructAggStatePerTransData;
LLVMValueRef AttributeTemplate; LLVMValueRef AttributeTemplate;
LLVMValueRef FuncStrlen; LLVMValueRef FuncStrlen;
LLVMValueRef FuncVarsizeAny;
LLVMValueRef FuncSlotGetsomeattrs; LLVMValueRef FuncSlotGetsomeattrs;
LLVMValueRef FuncHeapGetsysattr; LLVMValueRef FuncHeapGetsysattr;
LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal; LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal;
...@@ -784,6 +785,7 @@ llvm_create_types(void) ...@@ -784,6 +785,7 @@ llvm_create_types(void)
AttributeTemplate = LLVMGetNamedFunction(mod, "AttributeTemplate"); AttributeTemplate = LLVMGetNamedFunction(mod, "AttributeTemplate");
FuncStrlen = LLVMGetNamedFunction(mod, "strlen"); FuncStrlen = LLVMGetNamedFunction(mod, "strlen");
FuncVarsizeAny = LLVMGetNamedFunction(mod, "varsize_any");
FuncSlotGetsomeattrs = LLVMGetNamedFunction(mod, "slot_getsomeattrs"); FuncSlotGetsomeattrs = LLVMGetNamedFunction(mod, "slot_getsomeattrs");
FuncHeapGetsysattr = LLVMGetNamedFunction(mod, "heap_getsysattr"); FuncHeapGetsysattr = LLVMGetNamedFunction(mod, "heap_getsysattr");
FuncMakeExpandedObjectReadOnlyInternal = LLVMGetNamedFunction(mod, "MakeExpandedObjectReadOnlyInternal"); FuncMakeExpandedObjectReadOnlyInternal = LLVMGetNamedFunction(mod, "MakeExpandedObjectReadOnlyInternal");
......
This diff is collapsed.
...@@ -152,7 +152,7 @@ llvm_compile_expr(ExprState *state) ...@@ -152,7 +152,7 @@ llvm_compile_expr(ExprState *state)
param_types[0] = l_ptr(StructExprState); /* state */ param_types[0] = l_ptr(StructExprState); /* state */
param_types[1] = l_ptr(StructExprContext); /* econtext */ param_types[1] = l_ptr(StructExprContext); /* econtext */
param_types[2] = l_ptr(TypeParamBool); /* isnull */ param_types[2] = l_ptr(TypeParamBool); /* isnull */
eval_sig = LLVMFunctionType(TypeSizeT, eval_sig = LLVMFunctionType(TypeSizeT,
param_types, lengthof(param_types), param_types, lengthof(param_types),
...@@ -272,6 +272,7 @@ llvm_compile_expr(ExprState *state) ...@@ -272,6 +272,7 @@ llvm_compile_expr(ExprState *state)
case EEOP_OUTER_FETCHSOME: case EEOP_OUTER_FETCHSOME:
case EEOP_SCAN_FETCHSOME: case EEOP_SCAN_FETCHSOME:
{ {
TupleDesc desc = NULL;
LLVMValueRef v_slot; LLVMValueRef v_slot;
LLVMBasicBlockRef b_fetch; LLVMBasicBlockRef b_fetch;
LLVMValueRef v_nvalid; LLVMValueRef v_nvalid;
...@@ -279,17 +280,38 @@ llvm_compile_expr(ExprState *state) ...@@ -279,17 +280,38 @@ llvm_compile_expr(ExprState *state)
b_fetch = l_bb_before_v(opblocks[i + 1], b_fetch = l_bb_before_v(opblocks[i + 1],
"op.%d.fetch", i); "op.%d.fetch", i);
if (op->d.fetch.known_desc)
desc = op->d.fetch.known_desc;
if (opcode == EEOP_INNER_FETCHSOME) if (opcode == EEOP_INNER_FETCHSOME)
{ {
PlanState *is = innerPlanState(parent);
v_slot = v_innerslot; v_slot = v_innerslot;
if (!desc &&
is &&
is->ps_ResultTupleSlot &&
is->ps_ResultTupleSlot->tts_fixedTupleDescriptor)
desc = is->ps_ResultTupleSlot->tts_tupleDescriptor;
} }
else if (opcode == EEOP_OUTER_FETCHSOME) else if (opcode == EEOP_OUTER_FETCHSOME)
{ {
PlanState *os = outerPlanState(parent);
v_slot = v_outerslot; v_slot = v_outerslot;
if (!desc &&
os &&
os->ps_ResultTupleSlot &&
os->ps_ResultTupleSlot->tts_fixedTupleDescriptor)
desc = os->ps_ResultTupleSlot->tts_tupleDescriptor;
} }
else else
{ {
v_slot = v_scanslot; v_slot = v_scanslot;
if (!desc && parent)
desc = parent->scandesc;
} }
/* /*
...@@ -308,6 +330,27 @@ llvm_compile_expr(ExprState *state) ...@@ -308,6 +330,27 @@ llvm_compile_expr(ExprState *state)
LLVMPositionBuilderAtEnd(b, b_fetch); LLVMPositionBuilderAtEnd(b, b_fetch);
/*
* If the tupledesc of the to-be-deformed tuple is known,
* and JITing of deforming is enabled, build deform
* function specific to tupledesc and the exact number of
* to-be-extracted attributes.
*/
if (desc && (context->base.flags & PGJIT_DEFORM))
{
LLVMValueRef params[1];
LLVMValueRef l_jit_deform;
l_jit_deform =
slot_compile_deform(context, desc,
op->d.fetch.last_var);
params[0] = v_slot;
LLVMBuildCall(b, l_jit_deform,
params, lengthof(params), "");
}
else
{ {
LLVMValueRef params[2]; LLVMValueRef params[2];
......
...@@ -96,6 +96,7 @@ FunctionReturningBool(void) ...@@ -96,6 +96,7 @@ FunctionReturningBool(void)
void *referenced_functions[] = void *referenced_functions[] =
{ {
strlen, strlen,
varsize_any,
slot_getsomeattrs, slot_getsomeattrs,
heap_getsysattr, heap_getsysattr,
MakeExpandedObjectReadOnlyInternal, MakeExpandedObjectReadOnlyInternal,
......
...@@ -550,6 +550,8 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams) ...@@ -550,6 +550,8 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
*/ */
if (jit_expressions) if (jit_expressions)
result->jitFlags |= PGJIT_EXPR; result->jitFlags |= PGJIT_EXPR;
if (jit_tuple_deforming)
result->jitFlags |= PGJIT_DEFORM;
} }
return result; return result;
......
...@@ -1788,6 +1788,17 @@ static struct config_bool ConfigureNamesBool[] = ...@@ -1788,6 +1788,17 @@ static struct config_bool ConfigureNamesBool[] =
NULL, NULL, NULL NULL, NULL, NULL
}, },
{
{"jit_tuple_deforming", PGC_USERSET, DEVELOPER_OPTIONS,
gettext_noop("Allow JIT compilation of tuple deforming."),
NULL,
GUC_NOT_IN_SAMPLE
},
&jit_tuple_deforming,
true,
NULL, NULL, NULL
},
/* End-of-list marker */ /* End-of-list marker */
{ {
{NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL, NULL {NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL, NULL
......
...@@ -829,5 +829,6 @@ extern void heap_free_minimal_tuple(MinimalTuple mtup); ...@@ -829,5 +829,6 @@ extern void heap_free_minimal_tuple(MinimalTuple mtup);
extern MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup); extern MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup);
extern HeapTuple heap_tuple_from_minimal_tuple(MinimalTuple mtup); extern HeapTuple heap_tuple_from_minimal_tuple(MinimalTuple mtup);
extern MinimalTuple minimal_tuple_from_heap_tuple(HeapTuple htup); extern MinimalTuple minimal_tuple_from_heap_tuple(HeapTuple htup);
extern size_t varsize_any(void *p);
#endif /* HTUP_DETAILS_H */ #endif /* HTUP_DETAILS_H */
...@@ -262,6 +262,7 @@ typedef struct ExprEvalStep ...@@ -262,6 +262,7 @@ typedef struct ExprEvalStep
{ {
/* attribute number up to which to fetch (inclusive) */ /* attribute number up to which to fetch (inclusive) */
int last_var; int last_var;
TupleDesc known_desc;
} fetch; } fetch;
/* for EEOP_INNER/OUTER/SCAN_[SYS]VAR[_FIRST] */ /* for EEOP_INNER/OUTER/SCAN_[SYS]VAR[_FIRST] */
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#define PGJIT_OPT3 1 << 1 #define PGJIT_OPT3 1 << 1
/* reserved for PGJIT_INLINE */ /* reserved for PGJIT_INLINE */
#define PGJIT_EXPR 1 << 3 #define PGJIT_EXPR 1 << 3
#define PGJIT_DEFORM 1 << 4
typedef struct JitContext typedef struct JitContext
...@@ -67,6 +68,7 @@ extern bool jit_debugging_support; ...@@ -67,6 +68,7 @@ extern bool jit_debugging_support;
extern bool jit_dump_bitcode; extern bool jit_dump_bitcode;
extern bool jit_expressions; extern bool jit_expressions;
extern bool jit_profiling_support; extern bool jit_profiling_support;
extern bool jit_tuple_deforming;
extern double jit_above_cost; extern double jit_above_cost;
extern double jit_optimize_above_cost; extern double jit_optimize_above_cost;
......
...@@ -32,6 +32,7 @@ extern "C" ...@@ -32,6 +32,7 @@ extern "C"
#include "fmgr.h" #include "fmgr.h"
#include "jit/jit.h" #include "jit/jit.h"
#include "nodes/pg_list.h" #include "nodes/pg_list.h"
#include "access/tupdesc.h"
typedef struct LLVMJitContext typedef struct LLVMJitContext
...@@ -75,6 +76,7 @@ extern LLVMTypeRef StructAggStatePerGroupData; ...@@ -75,6 +76,7 @@ extern LLVMTypeRef StructAggStatePerGroupData;
extern LLVMValueRef AttributeTemplate; extern LLVMValueRef AttributeTemplate;
extern LLVMValueRef FuncStrlen; extern LLVMValueRef FuncStrlen;
extern LLVMValueRef FuncVarsizeAny;
extern LLVMValueRef FuncSlotGetsomeattrs; extern LLVMValueRef FuncSlotGetsomeattrs;
extern LLVMValueRef FuncHeapGetsysattr; extern LLVMValueRef FuncHeapGetsysattr;
extern LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal; extern LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal;
...@@ -107,6 +109,7 @@ extern LLVMValueRef llvm_function_reference(LLVMJitContext *context, ...@@ -107,6 +109,7 @@ extern LLVMValueRef llvm_function_reference(LLVMJitContext *context,
**************************************************************************** ****************************************************************************
*/ */
extern bool llvm_compile_expr(struct ExprState *state); extern bool llvm_compile_expr(struct ExprState *state);
extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDesc desc, int natts);
/* /*
**************************************************************************** ****************************************************************************
......
...@@ -920,6 +920,7 @@ typedef struct PlanState ...@@ -920,6 +920,7 @@ typedef struct PlanState
ExprState *qual; /* boolean qual condition */ ExprState *qual; /* boolean qual condition */
struct PlanState *lefttree; /* input plan tree(s) */ struct PlanState *lefttree; /* input plan tree(s) */
struct PlanState *righttree; struct PlanState *righttree;
List *initPlan; /* Init SubPlanState nodes (un-correlated expr List *initPlan; /* Init SubPlanState nodes (un-correlated expr
* subselects) */ * subselects) */
List *subPlan; /* SubPlanState nodes in my expressions */ List *subPlan; /* SubPlanState nodes in my expressions */
...@@ -935,6 +936,13 @@ typedef struct PlanState ...@@ -935,6 +936,13 @@ typedef struct PlanState
TupleTableSlot *ps_ResultTupleSlot; /* slot for my result tuples */ TupleTableSlot *ps_ResultTupleSlot; /* slot for my result tuples */
ExprContext *ps_ExprContext; /* node's expression-evaluation context */ ExprContext *ps_ExprContext; /* node's expression-evaluation context */
ProjectionInfo *ps_ProjInfo; /* info for doing tuple projection */ ProjectionInfo *ps_ProjInfo; /* info for doing tuple projection */
/*
* Scanslot's descriptor if known. This is a bit of a hack, but otherwise
* it's hard for expression compilation to optimize based on the
* descriptor, without encoding knowledge about all executor nodes.
*/
TupleDesc scandesc;
} PlanState; } PlanState;
/* ---------------- /* ----------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment