2018-03-21 22:28:28 -04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
* jit.h
|
|
|
|
|
* Provider independent JIT infrastructure.
|
|
|
|
|
*
|
2021-01-02 13:06:25 -05:00
|
|
|
* Copyright (c) 2016-2021, PostgreSQL Global Development Group
|
2018-03-21 22:28:28 -04:00
|
|
|
*
|
|
|
|
|
* src/include/jit/jit.h
|
|
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
#ifndef JIT_H
|
|
|
|
|
#define JIT_H
|
|
|
|
|
|
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 14:05:22 -04:00
|
|
|
#include "executor/instrument.h"
|
2018-03-21 22:28:28 -04:00
|
|
|
#include "utils/resowner.h"
|
|
|
|
|
|
|
|
|
|
|
2018-04-01 15:01:28 -04:00
|
|
|
/* Flags determining what kind of JIT operations to perform */
|
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 14:05:22 -04:00
|
|
|
#define PGJIT_NONE 0
|
2018-04-23 04:48:08 -04:00
|
|
|
#define PGJIT_PERFORM (1 << 0)
|
|
|
|
|
#define PGJIT_OPT3 (1 << 1)
|
|
|
|
|
#define PGJIT_INLINE (1 << 2)
|
|
|
|
|
#define PGJIT_EXPR (1 << 3)
|
|
|
|
|
#define PGJIT_DEFORM (1 << 4)
|
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 14:05:22 -04:00
|
|
|
|
|
|
|
|
|
2018-09-25 15:54:29 -04:00
|
|
|
typedef struct JitInstrumentation
|
2018-03-21 22:28:28 -04:00
|
|
|
{
|
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 14:05:22 -04:00
|
|
|
/* number of emitted functions */
|
|
|
|
|
size_t created_functions;
|
|
|
|
|
|
|
|
|
|
/* accumulated time to generate code */
|
|
|
|
|
instr_time generation_counter;
|
|
|
|
|
|
2018-03-28 16:19:08 -04:00
|
|
|
/* accumulated time for inlining */
|
|
|
|
|
instr_time inlining_counter;
|
|
|
|
|
|
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 14:05:22 -04:00
|
|
|
/* accumulated time for optimization */
|
|
|
|
|
instr_time optimization_counter;
|
|
|
|
|
|
|
|
|
|
/* accumulated time for code emission */
|
|
|
|
|
instr_time emission_counter;
|
2018-09-25 15:54:29 -04:00
|
|
|
} JitInstrumentation;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* DSM structure for accumulating jit instrumentation of all workers.
|
|
|
|
|
*/
|
|
|
|
|
typedef struct SharedJitInstrumentation
|
|
|
|
|
{
|
|
|
|
|
int num_workers;
|
|
|
|
|
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER];
|
|
|
|
|
} SharedJitInstrumentation;
|
|
|
|
|
|
|
|
|
|
typedef struct JitContext
|
|
|
|
|
{
|
|
|
|
|
/* see PGJIT_* above */
|
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
|
|
ResourceOwner resowner;
|
|
|
|
|
|
|
|
|
|
JitInstrumentation instr;
|
2018-03-21 22:28:28 -04:00
|
|
|
} JitContext;
|
|
|
|
|
|
|
|
|
|
typedef struct JitProviderCallbacks JitProviderCallbacks;
|
|
|
|
|
|
|
|
|
|
extern void _PG_jit_provider_init(JitProviderCallbacks *cb);
|
|
|
|
|
typedef void (*JitProviderInit) (JitProviderCallbacks *cb);
|
|
|
|
|
typedef void (*JitProviderResetAfterErrorCB) (void);
|
|
|
|
|
typedef void (*JitProviderReleaseContextCB) (JitContext *context);
|
2018-03-20 05:20:46 -04:00
|
|
|
struct ExprState;
|
|
|
|
|
typedef bool (*JitProviderCompileExprCB) (struct ExprState *state);
|
2018-03-21 22:28:28 -04:00
|
|
|
|
|
|
|
|
struct JitProviderCallbacks
|
|
|
|
|
{
|
|
|
|
|
JitProviderResetAfterErrorCB reset_after_error;
|
|
|
|
|
JitProviderReleaseContextCB release_context;
|
2018-03-20 05:20:46 -04:00
|
|
|
JitProviderCompileExprCB compile_expr;
|
2018-03-21 22:28:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* GUCs */
|
|
|
|
|
extern bool jit_enabled;
|
|
|
|
|
extern char *jit_provider;
|
2018-03-22 14:07:55 -04:00
|
|
|
extern bool jit_debugging_support;
|
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 14:05:22 -04:00
|
|
|
extern bool jit_dump_bitcode;
|
2018-03-20 05:20:46 -04:00
|
|
|
extern bool jit_expressions;
|
2018-03-22 14:07:55 -04:00
|
|
|
extern bool jit_profiling_support;
|
2018-03-26 15:57:19 -04:00
|
|
|
extern bool jit_tuple_deforming;
|
2018-03-22 14:45:07 -04:00
|
|
|
extern double jit_above_cost;
|
2018-03-28 16:19:08 -04:00
|
|
|
extern double jit_inline_above_cost;
|
2018-03-22 14:45:07 -04:00
|
|
|
extern double jit_optimize_above_cost;
|
2018-03-21 22:28:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
extern void jit_reset_after_error(void);
|
|
|
|
|
extern void jit_release_context(JitContext *context);
|
|
|
|
|
|
2018-03-20 05:20:46 -04:00
|
|
|
/*
|
|
|
|
|
* Functions for attempting to JIT code. Callers must accept that these might
|
|
|
|
|
* not be able to perform JIT (i.e. return false).
|
|
|
|
|
*/
|
|
|
|
|
extern bool jit_compile_expr(struct ExprState *state);
|
2018-09-25 15:54:29 -04:00
|
|
|
extern void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add);
|
2018-03-20 05:20:46 -04:00
|
|
|
|
|
|
|
|
|
2018-03-21 22:28:28 -04:00
|
|
|
#endif /* JIT_H */
|