Refactor MOT JIT TVM

This commit is contained in:
Vinoth Veeraraghavan
2020-11-16 20:57:50 +08:00
parent d887a17ce2
commit 1e6e3d764c
11 changed files with 7323 additions and 6997 deletions

View File

@ -35,9 +35,9 @@ ENGINE_INC = ../../core/src
include $(top_builddir)/src/Makefile.global
OBJ_DIR = ../obj
OBJS = $(OBJ_DIR)/jit_exec.o $(OBJ_DIR)/jit_common.o $(OBJ_DIR)/jit_llvm_query_codegen.o $(OBJ_DIR)/jit_llvm_blocks.o $(OBJ_DIR)/jit_tvm_exec.o $(OBJ_DIR)/jit_helpers.o $(OBJ_DIR)/jit_context.o $(OBJ_DIR)/jit_source.o $(OBJ_DIR)/jit_source_pool.o $(OBJ_DIR)/jit_source_map.o $(OBJ_DIR)/jit_context_pool.o $(OBJ_DIR)/jit_plan.o $(OBJ_DIR)/jit_plan_expr.o $(OBJ_DIR)/jit_explain.o $(OBJ_DIR)/jit_llvm_util.o $(OBJ_DIR)/jit_tvm_util.o $(OBJ_DIR)/jit_llvm.o $(OBJ_DIR)/jit_tvm.o $(OBJ_DIR)/jit_statistics.o
OBJS = $(OBJ_DIR)/jit_exec.o $(OBJ_DIR)/jit_common.o $(OBJ_DIR)/jit_llvm_query_codegen.o $(OBJ_DIR)/jit_llvm_blocks.o $(OBJ_DIR)/jit_tvm_query_codegen.o $(OBJ_DIR)/jit_tvm_blocks.o $(OBJ_DIR)/jit_helpers.o $(OBJ_DIR)/jit_context.o $(OBJ_DIR)/jit_source.o $(OBJ_DIR)/jit_source_pool.o $(OBJ_DIR)/jit_source_map.o $(OBJ_DIR)/jit_context_pool.o $(OBJ_DIR)/jit_plan.o $(OBJ_DIR)/jit_plan_expr.o $(OBJ_DIR)/jit_explain.o $(OBJ_DIR)/jit_llvm_util.o $(OBJ_DIR)/jit_tvm_util.o $(OBJ_DIR)/jit_llvm.o $(OBJ_DIR)/jit_tvm.o $(OBJ_DIR)/jit_statistics.o
DEPS := $(OBJ_DIR)/jit_exec.d $(OBJ_DIR)/jit_common.d $(OBJ_DIR)/jit_llvm_query_codegen.d $(OBJ_DIR)/jit_llvm_blocks.d $(OBJ_DIR)/jit_tvm_exec.d $(OBJ_DIR)/jit_helpers.d $(OBJ_DIR)/jit_context.d $(OBJ_DIR)/jit_source.d $(OBJ_DIR)/jit_source_pool.d $(OBJ_DIR)/jit_source_map.d $(OBJ_DIR)/jit_context_pool.d $(OBJ_DIR)/jit_plan.d $(OBJ_DIR)/jit_plan_expr.d $(OBJ_DIR)/jit_explain.d $(OBJ_DIR)/jit_llvm_util.d $(OBJ_DIR)/jit_tvm_util.d $(OBJ_DIR)/jit_llvm.d $(OBJ_DIR)/jit_tvm.d $(OBJ_DIR)/jit_statistics.d
DEPS := $(OBJ_DIR)/jit_exec.d $(OBJ_DIR)/jit_common.d $(OBJ_DIR)/jit_llvm_query_codegen.d $(OBJ_DIR)/jit_llvm_blocks.d $(OBJ_DIR)/jit_tvm_query_codegen.d $(OBJ_DIR)/jit_tvm_blocks.d $(OBJ_DIR)/jit_helpers.d $(OBJ_DIR)/jit_context.d $(OBJ_DIR)/jit_source.d $(OBJ_DIR)/jit_source_pool.d $(OBJ_DIR)/jit_source_map.d $(OBJ_DIR)/jit_context_pool.d $(OBJ_DIR)/jit_plan.d $(OBJ_DIR)/jit_plan_expr.d $(OBJ_DIR)/jit_explain.d $(OBJ_DIR)/jit_llvm_util.d $(OBJ_DIR)/jit_tvm_util.d $(OBJ_DIR)/jit_llvm.d $(OBJ_DIR)/jit_tvm.d $(OBJ_DIR)/jit_statistics.d
# Shared library stuff
include $(top_srcdir)/src/gausskernel/common.mk

View File

@ -42,7 +42,7 @@
#include "storage/mot/jit_exec.h"
#include "jit_common.h"
#include "jit_llvm_query_codegen.h"
#include "jit_tvm_exec.h"
#include "jit_tvm_query_codegen.h"
#include "jit_source_pool.h"
#include "jit_source_map.h"
#include "jit_context_pool.h"

View File

@ -32,6 +32,8 @@
DECLARE_LOGGER(TVM, JitExec)
namespace tvm {
IMPLEMENT_CLASS_LOGGER(Instruction, JitExec)
IMPLEMENT_CLASS_LOGGER(Expression, JitExec)
extern ExecContext* allocExecContext(uint64_t register_count)
{

View File

@ -31,6 +31,7 @@
#include "executor/tuptable.h"
#include "storage/mot/jit_def.h"
#include "debug_utils.h"
#include "logger.h"
#include <vector>
#include <list>
@ -231,6 +232,8 @@ protected:
_sub_instructions.push_back(sub_instruction);
}
DECLARE_CLASS_LOGGER()
private:
/** @var The instruction type. */
Type _type;
@ -289,6 +292,8 @@ protected:
explicit Expression(EvalResult eval_result) : _eval_result(eval_result)
{}
DECLARE_CLASS_LOGGER()
private:
/** @var Specifies whether the evaluation of the expression can fail. */
EvalResult _eval_result;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,203 @@
/*
* Copyright (c) 2020 Huawei Technologies Co.,Ltd.
*
* openGauss is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*
* jit_tvm_blocks.h
* Helpers to generate compound TVM-jitted code.
*
* IDENTIFICATION
* src/gausskernel/storage/mot/jit_exec/src/jit_tvm_blocks.h
*
* -------------------------------------------------------------------------
*/
#ifndef JIT_TVM_BLOCKS_H
#define JIT_TVM_BLOCKS_H
// Be sure to include jit_tvm_query.h before anything else because of global.h.
// See jit_tvm_query.h for more details.
#include "jit_tvm_query.h"
namespace JitExec {
void CreateJittedFunction(JitTvmCodeGenContext* ctx, const char* function_name, const char* query_string);
bool ProcessJoinExpr(JitTvmCodeGenContext* ctx, Expr* expr, int* column_count, int* column_array, int* max_arg);
bool ProcessJoinOpExpr(
JitTvmCodeGenContext* ctx, const OpExpr* op_expr, int* column_count, int* column_array, int* max_arg);
bool ProcessJoinBoolExpr(
JitTvmCodeGenContext* ctx, const BoolExpr* boolexpr, int* column_count, int* column_array, int* max_arg);
void buildIsSoftMemoryLimitReached(JitTvmCodeGenContext* ctx);
tvm::Instruction* buildExpression(JitTvmCodeGenContext* ctx, tvm::Expression* expr);
void buildWriteDatumColumn(JitTvmCodeGenContext* ctx, tvm::Instruction* row, int colid, tvm::Instruction* datum_value);
void buildWriteRow(JitTvmCodeGenContext* ctx, tvm::Instruction* row, bool isPKey, JitTvmRuntimeCursor* cursor);
void buildResetRowsProcessed(JitTvmCodeGenContext* ctx);
void buildIncrementRowsProcessed(JitTvmCodeGenContext* ctx);
tvm::Instruction* buildCreateNewRow(JitTvmCodeGenContext* ctx);
tvm::Instruction* buildSearchRow(
JitTvmCodeGenContext* ctx, MOT::AccessType access_type, JitRangeScanType range_scan_type, int subQueryIndex = -1);
tvm::Expression* buildFilter(JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitFilter* filter, int* max_arg);
bool buildFilterRow(JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitFilterArray* filters, int* max_arg,
tvm::BasicBlock* next_block);
void buildInsertRow(JitTvmCodeGenContext* ctx, tvm::Instruction* row);
void buildDeleteRow(JitTvmCodeGenContext* ctx);
tvm::Instruction* buildSearchIterator(JitTvmCodeGenContext* ctx, JitIndexScanDirection index_scan_direction,
JitRangeBoundMode range_bound_mode, JitRangeScanType range_scan_type, int subQueryIndex = -1);
/** @brief Adds code to search for an iterator. */
tvm::Instruction* buildBeginIterator(JitTvmCodeGenContext* ctx, JitRangeScanType rangeScanType, int subQueryIndex = -1);
tvm::Instruction* buildGetRowFromIterator(JitTvmCodeGenContext* ctx, tvm::BasicBlock* endLoopBlock,
MOT::AccessType access_mode, JitIndexScanDirection index_scan_direction, JitTvmRuntimeCursor* cursor,
JitRangeScanType range_scan_type, int subQueryIndex = -1);
tvm::Expression* ProcessConstExpr(
JitTvmCodeGenContext* ctx, const Const* const_value, int& result_type, int arg_pos, int depth, int* max_arg);
tvm::Expression* ProcessParamExpr(
JitTvmCodeGenContext* ctx, const Param* param, int& result_type, int arg_pos, int depth, int* max_arg);
tvm::Expression* ProcessRelabelExpr(
JitTvmCodeGenContext* ctx, RelabelType* relabel_type, int& result_type, int arg_pos, int depth, int* max_arg);
tvm::Expression* ProcessVarExpr(
JitTvmCodeGenContext* ctx, const Var* var, int& result_type, int arg_pos, int depth, int* max_arg);
tvm::Expression* ProcessOpExpr(
JitTvmCodeGenContext* ctx, const OpExpr* op_expr, int& result_type, int arg_pos, int depth, int* max_arg);
tvm::Expression* ProcessFuncExpr(
JitTvmCodeGenContext* ctx, const FuncExpr* func_expr, int& result_type, int arg_pos, int depth, int* max_arg);
tvm::Expression* ProcessExpr(
JitTvmCodeGenContext* ctx, Expr* expr, int& result_type, int arg_pos, int depth, int* max_arg);
tvm::Expression* ProcessConstExpr(JitTvmCodeGenContext* ctx, const JitConstExpr* expr, int* max_arg);
tvm::Expression* ProcessParamExpr(JitTvmCodeGenContext* ctx, const JitParamExpr* expr, int* max_arg);
tvm::Expression* ProcessVarExpr(JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitVarExpr* expr, int* max_arg);
tvm::Expression* ProcessOpExpr(JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitOpExpr* expr, int* max_arg);
tvm::Expression* ProcessFuncExpr(JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitFuncExpr* expr, int* max_arg);
tvm::Expression* ProcessFilterExpr(JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitFilter* filter, int* max_arg);
tvm::Expression* ProcessSubLinkExpr(
JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitSubLinkExpr* expr, int* max_arg);
tvm::Expression* ProcessBoolExpr(JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitBoolExpr* expr, int* maxArg);
tvm::Expression* ProcessExpr(JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitExpr* expr, int* max_arg);
bool buildScanExpression(JitTvmCodeGenContext* ctx, JitColumnExpr* expr, int* max_arg,
JitRangeIteratorType range_itr_type, JitRangeScanType range_scan_type, tvm::Instruction* outer_row,
int subQueryIndex);
bool buildPointScan(JitTvmCodeGenContext* ctx, JitColumnExprArray* expr_array, int* max_arg,
JitRangeScanType range_scan_type, tvm::Instruction* outer_row, int expr_count = -1, int subQueryIndex = -1);
bool writeRowColumns(
JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitColumnExprArray* expr_array, int* max_arg, bool is_update);
bool selectRowColumns(JitTvmCodeGenContext* ctx, tvm::Instruction* row, JitSelectExprArray* expr_array, int* max_arg,
JitRangeScanType range_scan_type, int subQueryIndex = -1);
bool buildClosedRangeScan(JitTvmCodeGenContext* ctx, JitIndexScan* indexScan, int* maxArg,
JitRangeScanType rangeScanType, tvm::Instruction* outerRow, int subQueryIndex);
bool buildSemiOpenRangeScan(JitTvmCodeGenContext* ctx, JitIndexScan* index_scan, int* max_arg,
JitRangeScanType range_scan_type, JitRangeBoundMode* begin_range_bound, JitRangeBoundMode* end_range_bound,
tvm::Instruction* outer_row, int subQueryIndex);
bool buildOpenRangeScan(JitTvmCodeGenContext* ctx, JitIndexScan* index_scan, int* max_arg,
JitRangeScanType range_scan_type, JitRangeBoundMode* begin_range_bound, JitRangeBoundMode* end_range_bound,
tvm::Instruction* outer_row, int subQueryIndex);
bool buildRangeScan(JitTvmCodeGenContext* ctx, JitIndexScan* indexScan, int* maxArg,
JitRangeScanType rangeScanType, JitRangeBoundMode* beginRangeBound, JitRangeBoundMode* endRangeBound,
tvm::Instruction* outerRow, int subQueryIndex = -1);
bool buildPrepareStateScan(JitTvmCodeGenContext* ctx, JitIndexScan* index_scan, int* max_arg,
JitRangeScanType range_scan_type, tvm::Instruction* outer_row);
bool buildPrepareStateRow(JitTvmCodeGenContext* ctx, MOT::AccessType access_mode, JitIndexScan* index_scan,
int* max_arg, JitRangeScanType range_scan_type, tvm::BasicBlock* next_block);
tvm::Instruction* buildPrepareStateScanRow(JitTvmCodeGenContext* ctx, JitIndexScan* index_scan,
JitRangeScanType range_scan_type, MOT::AccessType access_mode, int* max_arg, tvm::Instruction* outer_row,
tvm::BasicBlock* next_block, tvm::BasicBlock** loop_block);
JitTvmRuntimeCursor buildRangeCursor(JitTvmCodeGenContext* ctx, JitIndexScan* indexScan, int* maxArg,
JitRangeScanType rangeScanType, JitIndexScanDirection indexScanDirection, tvm::Instruction* outerRow,
int subQueryIndex = -1);
bool prepareAggregateAvg(JitTvmCodeGenContext* ctx, const JitAggregate* aggregate);
bool prepareAggregateSum(JitTvmCodeGenContext* ctx, const JitAggregate* aggregate);
bool prepareAggregateMaxMin(JitTvmCodeGenContext* ctx, JitAggregate* aggregate);
bool prepareAggregateCount(JitTvmCodeGenContext* ctx, const JitAggregate* aggregate);
bool prepareDistinctSet(JitTvmCodeGenContext* ctx, const JitAggregate* aggregate);
bool prepareAggregate(JitTvmCodeGenContext* ctx, JitAggregate* aggregate);
tvm::Expression* buildAggregateAvg(JitTvmCodeGenContext* ctx, const JitAggregate* aggregate,
tvm::Expression* current_aggregate, tvm::Expression* var_expr);
tvm::Expression* buildAggregateSum(JitTvmCodeGenContext* ctx, const JitAggregate* aggregate,
tvm::Expression* current_aggregate, tvm::Expression* var_expr);
tvm::Expression* buildAggregateMax(JitTvmCodeGenContext* ctx, const JitAggregate* aggregate,
tvm::Expression* current_aggregate, tvm::Expression* var_expr);
tvm::Expression* buildAggregateMin(JitTvmCodeGenContext* ctx, const JitAggregate* aggregate,
tvm::Expression* current_aggregate, tvm::Expression* var_expr);
tvm::Expression* buildAggregateCount(JitTvmCodeGenContext* ctx, const JitAggregate* aggregate,
tvm::Expression* count_aggregate);
bool buildAggregateMaxMin(JitTvmCodeGenContext* ctx, JitAggregate* aggregate, tvm::Expression* var_expr);
bool buildAggregateTuple(JitTvmCodeGenContext* ctx, JitAggregate* aggregate, tvm::Expression* var_expr);
bool buildAggregateRow(
JitTvmCodeGenContext* ctx, JitAggregate* aggregate, tvm::Instruction* row, tvm::BasicBlock* next_block);
void buildAggregateResult(JitTvmCodeGenContext* ctx, const JitAggregate* aggregate);
void buildCheckLimit(JitTvmCodeGenContext* ctx, int limit_count);
bool selectJoinRows(JitTvmCodeGenContext* ctx, tvm::Instruction* outer_row_copy, tvm::Instruction* inner_row,
JitJoinPlan* plan, int* max_arg);
} // namespace JitExec
#endif /* JIT_TVM_BLOCKS_H */

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,97 @@
/*
* Copyright (c) 2020 Huawei Technologies Co.,Ltd.
*
* openGauss is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*
* jit_tvm_query.h
* TVM-jitted query codegen common header.
*
* IDENTIFICATION
* src/gausskernel/storage/mot/jit_exec/src/jit_tvm_query.h
*
* -------------------------------------------------------------------------
*/
#ifndef JIT_TVM_QUERY_H
#define JIT_TVM_QUERY_H
// Be sure to include global.h before postgres.h to avoid conflict between libintl.h (included in global.h)
// and c.h (included in postgres.h).
#include "global.h"
// be careful to include gscodegen.h before anything else to avoid clash with PM definition in datetime.h
#include "codegen/gscodegen.h"
#include "postgres.h"
#include "catalog/pg_operator.h"
#include "utils/fmgroids.h"
#include "nodes/parsenodes.h"
#include "storage/ipc.h"
#include "nodes/pg_list.h"
#include "utils/elog.h"
#include "utils/numeric.h"
#include "utils/numeric_gs.h"
#include "catalog/pg_aggregate.h"
#include "mot_internal.h"
#include "storage/mot/jit_exec.h"
#include "jit_common.h"
#include "jit_tvm.h"
#include "jit_tvm_util.h"
#include "jit_util.h"
#include "jit_plan.h"
#include "mot_engine.h"
#include "utilities.h"
#include "mot_internal.h"
#include "catalog_column_types.h"
#include "mot_error.h"
#include "utilities.h"
#include "mm_session_api.h"
#include <list>
#include <string>
#include <assert.h>
namespace JitExec {
/** @struct Holds instructions that evaluate in runtime to begin and end iterators of a cursor. */
struct JitTvmRuntimeCursor {
/** @var The iterator pointing to the beginning of the range. */
tvm::Instruction* begin_itr;
/** @var The iterator pointing to the end of the range. */
tvm::Instruction* end_itr;
};
/** @struct Context used for compiling tvm-jitted functions. */
struct JitTvmCodeGenContext {
/** @var Main table info. */
TableInfo _table_info;
/** @var Inner table info (in JOIN queries). */
TableInfo m_innerTable_info;
/** @var Sub-query table info (in COMPOUND queries). */
TableInfo* m_subQueryTableInfo;
/** @var Sub-query count (in COMPOUND queries). */
uint64_t m_subQueryCount;
/** @var The builder used for emitting code. */
tvm::Builder* _builder;
/** @var The resulting jitted function. */
tvm::Function* m_jittedQuery;
};
} // namespace JitExec
#endif /* JIT_TVM_QUERY_H */

File diff suppressed because it is too large Load Diff

View File

@ -13,17 +13,17 @@
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*
* jit_tvm_exec.h
* TVM-jitted query execution.
* jit_tvm_query_codegen.h
* TVM-jitted code generation.
*
* IDENTIFICATION
* src/gausskernel/storage/mot/jit_exec/src/jit_tvm_exec.h
* src/gausskernel/storage/mot/jit_exec/src/jit_tvm_query_codegen.h
*
* -------------------------------------------------------------------------
*/
#ifndef JIT_TVM_EXEC_H
#define JIT_TVM_EXEC_H
#ifndef JIT_TVM_QUERY_CODEGEN_H
#define JIT_TVM_QUERY_CODEGEN_H
#include "nodes/parsenodes.h"
#include "nodes/params.h"