2018-03-22 03:28:28 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* llvmjit.h
|
|
|
|
* LLVM JIT provider.
|
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Copyright (c) 2016-2019, PostgreSQL Global Development Group
|
2018-03-22 03:28:28 +01:00
|
|
|
*
|
|
|
|
* src/include/jit/llvmjit.h
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef LLVMJIT_H
|
|
|
|
#define LLVMJIT_H
|
|
|
|
|
2019-01-29 03:05:52 +01:00
|
|
|
/*
|
|
|
|
* To avoid breaking cpluspluscheck, allow including the file even when LLVM
|
|
|
|
* is not available.
|
|
|
|
*/
|
|
|
|
#ifdef USE_LLVM
|
2018-03-22 03:28:28 +01:00
|
|
|
|
|
|
|
#include <llvm-c/Types.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File needs to be includable by both C and C++ code, and include other
|
|
|
|
* headers doing the same. Therefore wrap C portion in our own extern "C" if
|
|
|
|
* in C++ mode.
|
|
|
|
*/
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-03-20 10:20:46 +01:00
|
|
|
#include "fmgr.h"
|
2018-03-22 03:28:28 +01:00
|
|
|
#include "jit/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 19:05:22 +01:00
|
|
|
#include "nodes/pg_list.h"
|
2018-03-26 21:57:19 +02:00
|
|
|
#include "access/tupdesc.h"
|
2018-03-22 03:28:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct LLVMJitContext
|
|
|
|
{
|
|
|
|
JitContext base;
|
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
|
|
|
|
|
|
|
/* number of modules created */
|
|
|
|
size_t module_generation;
|
|
|
|
|
|
|
|
/* current, "open for write", module */
|
|
|
|
LLVMModuleRef module;
|
|
|
|
|
|
|
|
/* is there any pending code that needs to be emitted */
|
|
|
|
bool compiled;
|
|
|
|
|
|
|
|
/* # of objects emitted, used to generate non-conflicting names */
|
|
|
|
int counter;
|
|
|
|
|
|
|
|
/* list of handles for code emitted via Orc */
|
|
|
|
List *handles;
|
2018-03-22 03:28:28 +01:00
|
|
|
} LLVMJitContext;
|
|
|
|
|
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
|
|
|
|
|
|
|
/* type and struct definitions */
|
2018-03-23 06:15:51 +01:00
|
|
|
extern LLVMTypeRef TypeParamBool;
|
2018-02-05 18:09:28 +01:00
|
|
|
extern LLVMTypeRef TypePGFunction;
|
2018-03-23 06:15:51 +01:00
|
|
|
extern LLVMTypeRef TypeSizeT;
|
|
|
|
extern LLVMTypeRef TypeStorageBool;
|
|
|
|
|
Change function call information to be variable length.
Before this change FunctionCallInfoData, the struct arguments etc for
V1 function calls are stored in, always had space for
FUNC_MAX_ARGS/100 arguments, storing datums and their nullness in two
arrays. For nearly every function call 100 arguments is far more than
needed, therefore wasting memory. Arg and argnull being two separate
arrays also guarantees that to access a single argument, two
cachelines have to be touched.
Change the layout so there's a single variable-length array with pairs
of value / isnull. That drastically reduces memory consumption for
most function calls (on x86-64 a two argument function now uses
64bytes, previously 936 bytes), and makes it very likely that argument
value and its nullness are on the same cacheline.
Arguments are stored in a new NullableDatum struct, which, due to
padding, needs more memory per argument than before. But as usually
far fewer arguments are stored, and individual arguments are cheaper
to access, that's still a clear win. It's likely that there's other
places where conversion to NullableDatum arrays would make sense,
e.g. TupleTableSlots, but that's for another commit.
Because the function call information is now variable-length
allocations have to take the number of arguments into account. For
heap allocations that can be done with SizeForFunctionCallInfoData(),
for on-stack allocations there's a new LOCAL_FCINFO(name, nargs) macro
that helps to allocate an appropriately sized and aligned variable.
Some places with stack allocation function call information don't know
the number of arguments at compile time, and currently variably sized
stack allocations aren't allowed in postgres. Therefore allow for
FUNC_MAX_ARGS space in these cases. They're not that common, so for
now that seems acceptable.
Because of the need to allocate FunctionCallInfo of the appropriate
size, older extensions may need to update their code. To avoid subtle
breakages, the FunctionCallInfoData struct has been renamed to
FunctionCallInfoBaseData. Most code only references FunctionCallInfo,
so that shouldn't cause much collateral damage.
This change is also a prerequisite for more efficient expression JIT
compilation (by allocating the function call information on the stack,
allowing LLVM to optimize it away); previously the size of the call
information caused problems inside LLVM's optimizer.
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20180605172952.x34m5uz6ju6enaem@alap3.anarazel.de
2019-01-26 23:17:52 +01:00
|
|
|
extern LLVMTypeRef StructNullableDatum;
|
2019-01-15 01:15:20 +01:00
|
|
|
extern LLVMTypeRef StructTupleDescData;
|
2018-02-05 18:09:28 +01:00
|
|
|
extern LLVMTypeRef StructHeapTupleData;
|
|
|
|
extern LLVMTypeRef StructTupleTableSlot;
|
Make TupleTableSlots extensible, finish split of existing slot type.
This commit completes the work prepared in 1a0586de36, splitting the
old TupleTableSlot implementation (which could store buffer, heap,
minimal and virtual slots) into four different slot types. As
described in the aforementioned commit, this is done with the goal of
making tuple table slots extensible, to allow for pluggable table
access methods.
To achieve runtime extensibility for TupleTableSlots, operations on
slots that can differ between types of slots are performed using the
TupleTableSlotOps struct provided at slot creation time. That
includes information from the size of TupleTableSlot struct to be
allocated, initialization, deforming etc. See the struct's definition
for more detailed information about callbacks TupleTableSlotOps.
I decided to rename TTSOpsBufferTuple to TTSOpsBufferHeapTuple and
ExecCopySlotTuple to ExecCopySlotHeapTuple, as that seems more
consistent with other naming introduced in recent patches.
There's plenty optimization potential in the slot implementation, but
according to benchmarking the state after this commit has similar
performance characteristics to before this set of changes, which seems
sufficient.
There's a few changes in execReplication.c that currently need to poke
through the slot abstraction, that'll be repaired once the pluggable
storage patchset provides the necessary infrastructure.
Author: Andres Freund and Ashutosh Bapat, with changes by Amit Khandekar
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-17 01:35:11 +01:00
|
|
|
extern LLVMTypeRef StructHeapTupleTableSlot;
|
|
|
|
extern LLVMTypeRef StructMinimalTupleTableSlot;
|
2018-02-05 18:09:28 +01:00
|
|
|
extern LLVMTypeRef StructMemoryContextData;
|
|
|
|
extern LLVMTypeRef StructFunctionCallInfoData;
|
|
|
|
extern LLVMTypeRef StructExprContext;
|
|
|
|
extern LLVMTypeRef StructExprEvalStep;
|
|
|
|
extern LLVMTypeRef StructExprState;
|
|
|
|
extern LLVMTypeRef StructAggState;
|
|
|
|
extern LLVMTypeRef StructAggStatePerTransData;
|
|
|
|
extern LLVMTypeRef StructAggStatePerGroupData;
|
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
|
|
|
|
|
|
|
extern LLVMValueRef AttributeTemplate;
|
|
|
|
extern LLVMValueRef FuncStrlen;
|
2018-03-26 21:57:19 +02:00
|
|
|
extern LLVMValueRef FuncVarsizeAny;
|
2018-03-28 06:03:10 +02:00
|
|
|
extern LLVMValueRef FuncSlotGetmissingattrs;
|
2018-11-16 19:25:34 +01:00
|
|
|
extern LLVMValueRef FuncSlotGetsomeattrsInt;
|
2018-02-05 18:09:28 +01:00
|
|
|
extern LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal;
|
2019-02-01 16:50:32 +01:00
|
|
|
extern LLVMValueRef FuncExecEvalSubscriptingRef;
|
2018-11-07 20:08:45 +01:00
|
|
|
extern LLVMValueRef FuncExecEvalSysVar;
|
2018-02-05 18:09:28 +01:00
|
|
|
extern LLVMValueRef FuncExecAggTransReparent;
|
|
|
|
extern LLVMValueRef FuncExecAggInitGroup;
|
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
|
|
|
|
|
|
|
|
2018-03-22 03:28:28 +01:00
|
|
|
extern void llvm_enter_fatal_on_oom(void);
|
|
|
|
extern void llvm_leave_fatal_on_oom(void);
|
|
|
|
extern void llvm_reset_after_error(void);
|
|
|
|
extern void llvm_assert_in_fatal_section(void);
|
|
|
|
|
|
|
|
extern LLVMJitContext *llvm_create_context(int jitFlags);
|
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
|
|
|
extern LLVMModuleRef llvm_mutable_module(LLVMJitContext *context);
|
|
|
|
extern char *llvm_expand_funcname(LLVMJitContext *context, const char *basename);
|
|
|
|
extern void *llvm_get_function(LLVMJitContext *context, const char *funcname);
|
|
|
|
extern void llvm_split_symbol_name(const char *name, char **modname, char **funcname);
|
|
|
|
extern LLVMValueRef llvm_get_decl(LLVMModuleRef mod, LLVMValueRef f);
|
|
|
|
extern void llvm_copy_attributes(LLVMValueRef from, LLVMValueRef to);
|
2018-03-20 10:20:46 +01:00
|
|
|
extern LLVMValueRef llvm_function_reference(LLVMJitContext *context,
|
|
|
|
LLVMBuilderRef builder,
|
|
|
|
LLVMModuleRef mod,
|
|
|
|
FunctionCallInfo fcinfo);
|
2018-03-22 03:28:28 +01:00
|
|
|
|
2018-03-28 22:19:08 +02:00
|
|
|
extern void llvm_inline(LLVMModuleRef mod);
|
2018-03-22 03:44:17 +01:00
|
|
|
|
2018-03-20 10:20:46 +01:00
|
|
|
/*
|
|
|
|
****************************************************************************
|
2018-04-01 21:01:28 +02:00
|
|
|
* Code generation functions.
|
2018-03-20 10:20:46 +01:00
|
|
|
****************************************************************************
|
|
|
|
*/
|
|
|
|
extern bool llvm_compile_expr(struct ExprState *state);
|
2018-11-16 07:00:30 +01:00
|
|
|
struct TupleTableSlotOps;
|
|
|
|
extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDesc desc,
|
|
|
|
const struct TupleTableSlotOps *ops, int natts);
|
2018-03-20 10:20:46 +01:00
|
|
|
|
2018-03-22 03:44:17 +01:00
|
|
|
/*
|
|
|
|
****************************************************************************
|
|
|
|
* Extensions / Backward compatibility section of the LLVM C API
|
|
|
|
* Error handling related functions.
|
|
|
|
****************************************************************************
|
|
|
|
*/
|
|
|
|
#if defined(HAVE_DECL_LLVMGETHOSTCPUNAME) && !HAVE_DECL_LLVMGETHOSTCPUNAME
|
|
|
|
/** Get the host CPU as a string. The result needs to be disposed with
|
|
|
|
LLVMDisposeMessage. */
|
|
|
|
extern char *LLVMGetHostCPUName(void);
|
|
|
|
#endif
|
|
|
|
|
2018-08-24 19:20:55 +02:00
|
|
|
#if defined(HAVE_DECL_LLVMGETHOSTCPUFEATURES) && !HAVE_DECL_LLVMGETHOSTCPUFEATURES
|
2018-03-22 03:44:17 +01:00
|
|
|
/** Get the host CPU features as a string. The result needs to be disposed
|
|
|
|
with LLVMDisposeMessage. */
|
|
|
|
extern char *LLVMGetHostCPUFeatures(void);
|
2018-08-24 19:20:55 +02:00
|
|
|
#endif
|
2018-03-22 03:44:17 +01:00
|
|
|
|
2018-03-22 03:28:28 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|
|
|
|
|
2019-01-29 03:05:52 +01:00
|
|
|
#endif /* USE_LLVM */
|
2018-03-22 03:28:28 +01:00
|
|
|
#endif /* LLVMJIT_H */
|