2018-03-22 03:28:28 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* jit.c
|
|
|
|
* Provider independent JIT infrastructure.
|
|
|
|
*
|
|
|
|
* Code related to loading JIT providers, redirecting calls into JIT providers
|
|
|
|
* and error handling. No code specific to a specific JIT implementation
|
|
|
|
* should end up here.
|
|
|
|
*
|
|
|
|
*
|
2023-01-02 21:00:37 +01:00
|
|
|
* Copyright (c) 2016-2023, PostgreSQL Global Development Group
|
2018-03-22 03:28:28 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/backend/jit/jit.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-03-20 10:20:46 +01:00
|
|
|
#include "executor/execExpr.h"
|
2019-11-12 04:00:16 +01:00
|
|
|
#include "fmgr.h"
|
2018-03-22 03:28:28 +01:00
|
|
|
#include "jit/jit.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "utils/fmgrprotos.h"
|
|
|
|
|
|
|
|
/* GUCs */
|
|
|
|
bool jit_enabled = true;
|
2018-09-15 23:24:35 +02:00
|
|
|
char *jit_provider = NULL;
|
2018-03-22 19:07:55 +01:00
|
|
|
bool jit_debugging_support = false;
|
Support for optimizing and emitting code in LLVM JIT provider.
This commit introduces the ability to actually generate code using
LLVM. In particular, this adds:
- Ability to emit code both in heavily optimized and largely
unoptimized fashion
- Batching facility to allow functions to be defined in small
increments, but optimized and emitted in executable form in larger
batches (for performance and memory efficiency)
- Type and function declaration synchronization between runtime
generated code and normal postgres code. This is critical to be able
to access struct fields etc.
- Developer oriented jit_dump_bitcode GUC, for inspecting / debugging
the generated code.
- per JitContext statistics of number of functions, time spent
generating code, optimizing, and emitting it. This will later be
employed for EXPLAIN support.
This commit doesn't yet contain any code actually generating
functions. That'll follow in later commits.
Documentation for GUCs added, and for JIT in general, will be added in
later commits.
Author: Andres Freund, with contributions by Pierre Ducroquet
Testing-By: Thomas Munro, Peter Eisentraut
Discussion: https://postgr.es/m/20170901064131.tazjxwus3k2w3ybh@alap3.anarazel.de
2018-03-22 19:05:22 +01:00
|
|
|
bool jit_dump_bitcode = false;
|
2018-03-20 10:20:46 +01:00
|
|
|
bool jit_expressions = true;
|
2018-03-22 19:07:55 +01:00
|
|
|
bool jit_profiling_support = false;
|
2018-03-26 21:57:19 +02:00
|
|
|
bool jit_tuple_deforming = true;
|
2018-03-22 19:45:07 +01:00
|
|
|
double jit_above_cost = 100000;
|
2018-03-28 22:19:08 +02:00
|
|
|
double jit_inline_above_cost = 500000;
|
2018-03-22 19:45:07 +01:00
|
|
|
double jit_optimize_above_cost = 500000;
|
2018-03-22 03:28:28 +01:00
|
|
|
|
|
|
|
static JitProviderCallbacks provider;
|
|
|
|
static bool provider_successfully_loaded = false;
|
|
|
|
static bool provider_failed_loading = false;
|
|
|
|
|
|
|
|
|
|
|
|
static bool provider_init(void);
|
|
|
|
static bool file_exists(const char *name);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SQL level function returning whether JIT is available in the current
|
|
|
|
* backend. Will attempt to load JIT provider if necessary.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_jit_available(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_BOOL(provider_init());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return whether a JIT provider has successfully been loaded, caching the
|
|
|
|
* result.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
provider_init(void)
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
JitProviderInit init;
|
|
|
|
|
|
|
|
/* don't even try to load if not enabled */
|
|
|
|
if (!jit_enabled)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't retry loading after failing - attempting to load JIT provider
|
|
|
|
* isn't cheap.
|
|
|
|
*/
|
|
|
|
if (provider_failed_loading)
|
|
|
|
return false;
|
|
|
|
if (provider_successfully_loaded)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether shared library exists. We do that check before actually
|
|
|
|
* attempting to load the shared library (via load_external_function()),
|
|
|
|
* because that'd error out in case the shlib isn't available.
|
|
|
|
*/
|
|
|
|
snprintf(path, MAXPGPATH, "%s/%s%s", pkglib_path, jit_provider, DLSUFFIX);
|
|
|
|
elog(DEBUG1, "probing availability of JIT provider at %s", path);
|
|
|
|
if (!file_exists(path))
|
|
|
|
{
|
|
|
|
elog(DEBUG1,
|
|
|
|
"provider not available, disabling JIT for current session");
|
|
|
|
provider_failed_loading = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If loading functions fails, signal failure. We do so because
|
|
|
|
* load_external_function() might error out despite the above check if
|
|
|
|
* e.g. the library's dependencies aren't installed. We want to signal
|
|
|
|
* ERROR in that case, so the user is notified, but we don't want to
|
|
|
|
* continually retry.
|
|
|
|
*/
|
|
|
|
provider_failed_loading = true;
|
|
|
|
|
|
|
|
/* and initialize */
|
|
|
|
init = (JitProviderInit)
|
|
|
|
load_external_function(path, "_PG_jit_provider_init", true, NULL);
|
|
|
|
init(&provider);
|
|
|
|
|
|
|
|
provider_successfully_loaded = true;
|
|
|
|
provider_failed_loading = false;
|
|
|
|
|
|
|
|
elog(DEBUG1, "successfully loaded JIT provider in current session");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset JIT provider's error handling. This'll be called after an error has
|
|
|
|
* been thrown and the main-loop has re-established control.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
jit_reset_after_error(void)
|
|
|
|
{
|
|
|
|
if (provider_successfully_loaded)
|
|
|
|
provider.reset_after_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release resources required by one JIT context.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
jit_release_context(JitContext *context)
|
|
|
|
{
|
|
|
|
if (provider_successfully_loaded)
|
|
|
|
provider.release_context(context);
|
|
|
|
|
|
|
|
pfree(context);
|
|
|
|
}
|
|
|
|
|
2018-03-20 10:20:46 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:54:29 +02:00
|
|
|
/* Aggregate JIT instrumentation information */
|
|
|
|
void
|
|
|
|
InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
|
|
|
|
{
|
|
|
|
dst->created_functions += add->created_functions;
|
|
|
|
INSTR_TIME_ADD(dst->generation_counter, add->generation_counter);
|
2023-09-08 15:05:12 +02:00
|
|
|
INSTR_TIME_ADD(dst->deform_counter, add->deform_counter);
|
2018-09-25 21:54:29 +02:00
|
|
|
INSTR_TIME_ADD(dst->inlining_counter, add->inlining_counter);
|
|
|
|
INSTR_TIME_ADD(dst->optimization_counter, add->optimization_counter);
|
|
|
|
INSTR_TIME_ADD(dst->emission_counter, add->emission_counter);
|
|
|
|
}
|
|
|
|
|
2018-03-22 03:28:28 +01:00
|
|
|
static bool
|
|
|
|
file_exists(const char *name)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2022-10-28 09:19:06 +02:00
|
|
|
Assert(name != NULL);
|
2018-03-22 03:28:28 +01:00
|
|
|
|
|
|
|
if (stat(name, &st) == 0)
|
2021-09-08 02:44:04 +02:00
|
|
|
return !S_ISDIR(st.st_mode);
|
2018-03-22 03:28:28 +01:00
|
|
|
else if (!(errno == ENOENT || errno == ENOTDIR))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not access file \"%s\": %m", name)));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|