2018-03-22 03:28:28 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* llvmjit.h
|
|
|
|
* LLVM JIT provider.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2018, PostgreSQL Global Development Group
|
|
|
|
*
|
|
|
|
* src/include/jit/llvmjit.h
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef LLVMJIT_H
|
|
|
|
#define LLVMJIT_H
|
|
|
|
|
|
|
|
#ifndef USE_LLVM
|
|
|
|
#error "llvmjit.h should only be included by code dealing with llvm"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2018-02-05 18:09:28 +01:00
|
|
|
extern LLVMTypeRef StructtupleDesc;
|
|
|
|
extern LLVMTypeRef StructHeapTupleData;
|
|
|
|
extern LLVMTypeRef StructTupleTableSlot;
|
|
|
|
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-02-05 18:09:28 +01:00
|
|
|
extern LLVMValueRef FuncSlotGetsomeattrs;
|
2018-03-28 06:03:10 +02:00
|
|
|
extern LLVMValueRef FuncSlotGetmissingattrs;
|
2018-02-05 18:09:28 +01:00
|
|
|
extern LLVMValueRef FuncHeapGetsysattr;
|
|
|
|
extern LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal;
|
|
|
|
extern LLVMValueRef FuncExecEvalArrayRefSubscript;
|
|
|
|
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-03-26 21:57:19 +02:00
|
|
|
extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDesc desc, 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
|
|
|
|
|
|
|
|
#endif /* LLVMJIT_H */
|