1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Add expression compilation support to LLVM JIT provider.

In addition to the interpretation of expressions (which back
evaluation of WHERE clauses, target list projection, aggregates
transition values etc) support compiling expressions to native code,
using the infrastructure added in earlier commits.

To avoid duplicating a lot of code, only support emitting code for
cases that are likely to be performance critical. For expression steps
that aren't deemed that, use the existing interpreter.

The generated code isn't great - some architectural changes are
required to address that. But this already yields a significant
speedup for some analytics queries, particularly with WHERE clauses
filtering a lot, or computing multiple aggregates.

Author: Andres Freund
Tested-By: Thomas Munro
Discussion: https://postgr.es/m/20170901064131.tazjxwus3k2w3ybh@alap3.anarazel.de

Disable JITing for VALUES() nodes.

VALUES() nodes are only ever executed once. This is primarily helpful
for debugging, when forcing JITing even for cheap queries.

Author: Andres Freund
Discussion: https://postgr.es/m/20170901064131.tazjxwus3k2w3ybh@alap3.anarazel.de
This commit is contained in:
Andres Freund
2018-03-20 02:20:46 -07:00
parent 7ced1d1247
commit 2a0faed9d7
13 changed files with 2890 additions and 3 deletions

View File

@ -24,6 +24,7 @@
#include "fmgr.h"
#include "executor/execExpr.h"
#include "jit/jit.h"
#include "miscadmin.h"
#include "utils/resowner_private.h"
@ -35,6 +36,7 @@ bool jit_enabled = true;
char *jit_provider = "llvmjit";
bool jit_debugging_support = false;
bool jit_dump_bitcode = false;
bool jit_expressions = true;
bool jit_profiling_support = false;
double jit_above_cost = 100000;
double jit_optimize_above_cost = 500000;
@ -143,6 +145,41 @@ jit_release_context(JitContext *context)
pfree(context);
}
/*
* Ask provider to JIT compile an expression.
*
* Returns true if successful, false if not.
*/
bool
jit_compile_expr(struct ExprState *state)
{
/*
* We can easily create a one-off context for functions without an
* associated PlanState (and thus EState). But because there's no executor
* shutdown callback that could deallocate the created function, they'd
* live to the end of the transactions, where they'd be cleaned up by the
* resowner machinery. That can lead to a noticeable amount of memory
* usage, and worse, trigger some quadratic behaviour in gdb. Therefore,
* at least for now, don't create a JITed function in those circumstances.
*/
if (!state->parent)
return false;
/* if no jitting should be performed at all */
if (!(state->parent->state->es_jit_flags & PGJIT_PERFORM))
return false;
/* or if expressions aren't JITed */
if (!(state->parent->state->es_jit_flags & PGJIT_EXPR))
return false;
/* this also takes !jit_enabled into account */
if (provider_init())
return provider.compile_expr(state);
return false;
}
static bool
file_exists(const char *name)
{

View File

@ -39,7 +39,7 @@ OBJS=$(WIN32RES)
# Infrastructure
OBJS += llvmjit.o llvmjit_error.o llvmjit_wrap.o
# Code generation
OBJS +=
OBJS += llvmjit_expr.o
all: all-shared-lib llvmjit_types.bc

View File

@ -14,6 +14,7 @@
#include "postgres.h"
#include "jit/llvmjit.h"
#include "jit/llvmjit_emit.h"
#include "miscadmin.h"
@ -114,6 +115,7 @@ _PG_jit_provider_init(JitProviderCallbacks *cb)
{
cb->reset_after_error = llvm_reset_after_error;
cb->release_context = llvm_release_context;
cb->compile_expr = llvm_compile_expr;
}
/*
@ -339,6 +341,68 @@ llvm_copy_attributes(LLVMValueRef v_from, LLVMValueRef v_to)
}
}
/*
* Return a callable LLVMValueRef for fcinfo.
*/
LLVMValueRef
llvm_function_reference(LLVMJitContext *context,
LLVMBuilderRef builder,
LLVMModuleRef mod,
FunctionCallInfo fcinfo)
{
char *modname;
char *basename;
char *funcname;
LLVMValueRef v_fn;
fmgr_symbol(fcinfo->flinfo->fn_oid, &modname, &basename);
if (modname != NULL && basename != NULL)
{
/* external function in loadable library */
funcname = psprintf("pgextern.%s.%s", modname, basename);
}
else if (basename != NULL)
{
/* internal function */
funcname = psprintf("%s", basename);
}
else
{
/*
* Function we don't know to handle, return pointer. We do so by
* creating a global constant containing a pointer to the function.
* Makes IR more readable.
*/
LLVMValueRef v_fn_addr;
funcname = psprintf("pgoidextern.%u",
fcinfo->flinfo->fn_oid);
v_fn = LLVMGetNamedGlobal(mod, funcname);
if (v_fn != 0)
return LLVMBuildLoad(builder, v_fn, "");
v_fn_addr = l_ptr_const(fcinfo->flinfo->fn_addr, TypePGFunction);
v_fn = LLVMAddGlobal(mod, TypePGFunction, funcname);
LLVMSetInitializer(v_fn, v_fn_addr);
LLVMSetGlobalConstant(v_fn, true);
return LLVMBuildLoad(builder, v_fn, "");
return v_fn;
}
/* check if function already has been added */
v_fn = LLVMGetNamedFunction(mod, funcname);
if (v_fn != 0)
return v_fn;
v_fn = LLVMAddFunction(mod, funcname, LLVMGetElementType(TypePGFunction));
return v_fn;
}
/*
* Optimize code in module using the flags set in context.
*/

File diff suppressed because it is too large Load Diff