From 69a13ce5174cf8635eeae1229b8e0227208dff5b Mon Sep 17 00:00:00 2001 From: Vinoth Veeraraghavan Date: Mon, 12 Dec 2022 16:16:45 +0800 Subject: [PATCH] Minor cleanup in MOT JIT code --- .../storage/mot/jit_exec/jit_common.cpp | 6 ----- .../storage/mot/jit_exec/jit_common.h | 3 --- .../storage/mot/jit_exec/jit_exec.cpp | 6 ----- .../mot/jit_exec/jit_llvm_query_codegen.cpp | 27 +++---------------- .../mot/jit_exec/jit_llvm_query_codegen.h | 15 ----------- .../storage/mot/jit_exec/jit_llvm_sp.cpp | 25 +++++++---------- .../mot/jit_exec/jit_llvm_sp_codegen.h | 13 +++++++++ .../storage/mot/jit_exec/jit_llvm_util.cpp | 11 ++++++++ .../storage/mot/jit_exec/jit_llvm_util.h | 13 +++++++++ .../storage/mot/jit_exec/jit_source.cpp | 3 --- 10 files changed, 50 insertions(+), 72 deletions(-) diff --git a/src/gausskernel/storage/mot/jit_exec/jit_common.cpp b/src/gausskernel/storage/mot/jit_exec/jit_common.cpp index 4d3fad1ba..9e8da6340 100644 --- a/src/gausskernel/storage/mot/jit_exec/jit_common.cpp +++ b/src/gausskernel/storage/mot/jit_exec/jit_common.cpp @@ -49,7 +49,6 @@ #include "mm_global_api.h" #include "catalog_column_types.h" #include "mot_internal.h" -#include "jit_llvm_query_codegen.h" namespace JitExec { DECLARE_LOGGER(JitCommon, JitExec) @@ -68,11 +67,6 @@ const char* JitRuntimeFaultToString(int faultCode) } } -void JitResetCompileState() -{ - JitLlvmResetCompileState(); -} - const char* CommandToString(JitCommandType commandType) { switch (commandType) { diff --git a/src/gausskernel/storage/mot/jit_exec/jit_common.h b/src/gausskernel/storage/mot/jit_exec/jit_common.h index 2f74fadb7..7a5a0e245 100644 --- a/src/gausskernel/storage/mot/jit_exec/jit_common.h +++ b/src/gausskernel/storage/mot/jit_exec/jit_common.h @@ -92,9 +92,6 @@ inline bool IsJitRuntimeFaultCode(int faultCode) /** @brief Converts JIT generic fault code to string. */ extern const char* JitRuntimeFaultToString(int faultCode); -/** @brief Reset compile state for LLVM (cleanup in case of error). */ -extern void JitResetCompileState(); - // common helpers for code generation /** @brief Converts an LLVM command type to string form. */ extern const char* CommandToString(JitCommandType commandType); diff --git a/src/gausskernel/storage/mot/jit_exec/jit_exec.cpp b/src/gausskernel/storage/mot/jit_exec/jit_exec.cpp index 8b5251e83..e9f00d7c7 100644 --- a/src/gausskernel/storage/mot/jit_exec/jit_exec.cpp +++ b/src/gausskernel/storage/mot/jit_exec/jit_exec.cpp @@ -869,9 +869,6 @@ extern MotJitContext* JitCodegenQuery(Query* query, const char* queryString, Jit } PG_END_TRY(); - // reset compile state for robustness - JitResetCompileState(); - // pop currently parsed query u_sess->mot_cxt.jit_pg_query = prevQuery; @@ -1006,9 +1003,6 @@ extern MotJitContext* JitCodegenFunction(PLpgSQL_function* function, HeapTuple p } PG_END_TRY(); - // reset compile state for robustness - JitResetCompileState(); - if (nsPushed) { PopJitSourceNamespace(); } diff --git a/src/gausskernel/storage/mot/jit_exec/jit_llvm_query_codegen.cpp b/src/gausskernel/storage/mot/jit_exec/jit_llvm_query_codegen.cpp index b8f8ce419..abe95c477 100644 --- a/src/gausskernel/storage/mot/jit_exec/jit_llvm_query_codegen.cpp +++ b/src/gausskernel/storage/mot/jit_exec/jit_llvm_query_codegen.cpp @@ -2446,16 +2446,6 @@ static MotJitContext* JitInvokeCodegen(JitLlvmCodeGenContext* ctx, Query* query, return jitContext; } -#define JIT_ASSERT_QUERY_CODEGEN_UTIL_VALID() \ - do { \ - MOT_ASSERT(JIT_IF_CURRENT() == nullptr); \ - MOT_ASSERT(JIT_WHILE_CURRENT() == nullptr); \ - MOT_ASSERT(JIT_DO_WHILE_CURRENT() == nullptr); \ - MOT_ASSERT(JIT_FOR_CURRENT() == nullptr); \ - MOT_ASSERT(JIT_SWITCH_CURRENT() == nullptr); \ - MOT_ASSERT(JIT_TRY_CURRENT() == nullptr); \ - } while (0) - static bool InitCodeGenContextByPlan( JitLlvmCodeGenContext* ctx, GsCodeGen* codeGen, GsCodeGen::LlvmBuilder* builder, JitPlan* plan) { @@ -2521,7 +2511,7 @@ static bool InitCodeGenContextByPlan( MotJitContext* JitCodegenLlvmQuery(Query* query, const char* query_string, JitPlan* plan, JitCodegenStats& codegenStats) { - JIT_ASSERT_QUERY_CODEGEN_UTIL_VALID(); + JIT_ASSERT_LLVM_CODEGEN_UTIL_VALID(); GsCodeGen* codeGen = SetupCodegenEnv(); if (codeGen == nullptr) { return nullptr; @@ -2596,7 +2586,9 @@ MotJitContext* JitCodegenLlvmQuery(Query* query, const char* query_string, JitPl "Got LLVM-jitted function %p after compile, for query: %s", jitContext->m_llvmFunction, query_string); } - JIT_ASSERT_QUERY_CODEGEN_UTIL_VALID(); + // reset compile state for robustness + llvm_util::JitResetCompileState(); + JIT_ASSERT_LLVM_CODEGEN_UTIL_VALID(); return (MotJitContext*)jitContext; } @@ -2642,15 +2634,4 @@ extern int JitExecLlvmQuery(JitQueryContext* jitContext, ParamListInfo params, T MOT_LOG_TRACE("JitExecLlvmQuery returned %d/%u for query: %s", result, jitContext->m_rc, jitContext->m_queryString); return result; } - -extern void JitLlvmResetCompileState() -{ - u_sess->mot_cxt.jit_llvm_if_stack = nullptr; - u_sess->mot_cxt.jit_llvm_loop_stack = nullptr; - u_sess->mot_cxt.jit_llvm_while_stack = nullptr; - u_sess->mot_cxt.jit_llvm_do_while_stack = nullptr; - u_sess->mot_cxt.jit_llvm_for_stack = nullptr; - u_sess->mot_cxt.jit_llvm_switch_case_stack = nullptr; - u_sess->mot_cxt.jit_llvm_try_catch_stack = nullptr; -} } // namespace JitExec diff --git a/src/gausskernel/storage/mot/jit_exec/jit_llvm_query_codegen.h b/src/gausskernel/storage/mot/jit_exec/jit_llvm_query_codegen.h index f3b5211b8..8e3f1a056 100644 --- a/src/gausskernel/storage/mot/jit_exec/jit_llvm_query_codegen.h +++ b/src/gausskernel/storage/mot/jit_exec/jit_llvm_query_codegen.h @@ -59,21 +59,6 @@ MotJitContext* JitCodegenLlvmQuery(Query* query, const char* queryString, JitPla */ extern int JitExecLlvmQuery(JitQueryContext* jitContext, ParamListInfo params, TupleTableSlot* slot, uint64_t* tuplesProcessed, int* scanEnded, int newScan); - -/** - * @brief Executed a previously LLVM-jitted stored procedure. - * @param jitContext The context produced by a previous call to @ref QueryCodegen(). - * @param params The list of bound parameters passed to the query. - * @param[out] slot The slot used for reporting select result. - * @param[out] tuplesProcessed The variable used to report the number of processed tuples. - * @param[out] scanEnded The variable used to report if a range scan ended. - * @return Zero if succeeded, otherwise an error code. - * @note This function may cause transaction abort. - */ -extern int JitExecLlvmFunction(JitFunctionContext* jitContext, ParamListInfo params, TupleTableSlot* slot, - uint64_t* tuplesProcessed, int* scanEnded); - -extern void JitLlvmResetCompileState(); } // namespace JitExec #endif /* JIT_LLVM_QUERY_CODEGEN_H */ diff --git a/src/gausskernel/storage/mot/jit_exec/jit_llvm_sp.cpp b/src/gausskernel/storage/mot/jit_exec/jit_llvm_sp.cpp index 25741501d..d3fa6b024 100644 --- a/src/gausskernel/storage/mot/jit_exec/jit_llvm_sp.cpp +++ b/src/gausskernel/storage/mot/jit_exec/jit_llvm_sp.cpp @@ -5338,20 +5338,10 @@ static bool InitLocalVariables(JitLlvmFunctionCodeGenContext* ctx) return true; } -#define JIT_ASSERT_SP_CODEGEN_UTIL_VALID() \ - do { \ - MOT_ASSERT(JIT_IF_CURRENT() == nullptr); \ - MOT_ASSERT(JIT_WHILE_CURRENT() == nullptr); \ - MOT_ASSERT(JIT_DO_WHILE_CURRENT() == nullptr); \ - MOT_ASSERT(JIT_FOR_CURRENT() == nullptr); \ - MOT_ASSERT(JIT_SWITCH_CURRENT() == nullptr); \ - MOT_ASSERT(JIT_TRY_CURRENT() == nullptr); \ - } while (0) - extern MotJitContext* JitCodegenLlvmFunction(PLpgSQL_function* function, HeapTuple procTuple, Oid functionOid, ReturnSetInfo* returnSetInfo, JitPlan* plan, JitCodegenStats& codegenStats) { - JIT_ASSERT_SP_CODEGEN_UTIL_VALID(); + JIT_ASSERT_LLVM_CODEGEN_UTIL_VALID(); bool procNameIsNull = false; bool procSrcIsNull = false; bool procIsStrictIsNull = false; @@ -5361,7 +5351,7 @@ extern MotJitContext* JitCodegenLlvmFunction(PLpgSQL_function* function, HeapTup if (procNameIsNull || procSrcIsNull || procIsStrictIsNull) { MOT_LOG_TRACE("Failed to generate jitted code for stored procedure: catalog entry for stored procedure " "contains null attributes"); - JIT_ASSERT_SP_CODEGEN_UTIL_VALID(); + JIT_ASSERT_LLVM_CODEGEN_UTIL_VALID(); return nullptr; } @@ -5377,7 +5367,7 @@ extern MotJitContext* JitCodegenLlvmFunction(PLpgSQL_function* function, HeapTup if (!functionSource) { MOT_REPORT_ERROR( MOT_ERROR_OOM, "Parse Stored Procedure", "Failed to allocate memory for stored procedure source code"); - JIT_ASSERT_SP_CODEGEN_UTIL_VALID(); + JIT_ASSERT_LLVM_CODEGEN_UTIL_VALID(); return nullptr; } bool isStrict = DatumGetBool(procIsStrictDatum); @@ -5388,14 +5378,14 @@ extern MotJitContext* JitCodegenLlvmFunction(PLpgSQL_function* function, HeapTup // prepare compile context GsCodeGen* codeGen = SetupCodegenEnv(); if (codeGen == nullptr) { - JIT_ASSERT_SP_CODEGEN_UTIL_VALID(); + JIT_ASSERT_LLVM_CODEGEN_UTIL_VALID(); return nullptr; } GsCodeGen::LlvmBuilder builder(codeGen->context()); volatile JitLlvmFunctionCodeGenContext cgCtx = {}; if (!InitLlvmFunctionCodeGenContext( (JitLlvmFunctionCodeGenContext*)&cgCtx, codeGen, &builder, function, returnSetInfo)) { - JIT_ASSERT_SP_CODEGEN_UTIL_VALID(); + JIT_ASSERT_LLVM_CODEGEN_UTIL_VALID(); FreeGsCodeGen(codeGen); return nullptr; } @@ -5490,7 +5480,10 @@ extern MotJitContext* JitCodegenLlvmFunction(PLpgSQL_function* function, HeapTup // cleanup function->pre_parse_trig = preParseTrig; DestroyLlvmFunctionCodeGenContext((JitLlvmFunctionCodeGenContext*)ctx); - JIT_ASSERT_SP_CODEGEN_UTIL_VALID(); + + // reset compile state for robustness + llvm_util::JitResetCompileState(); + JIT_ASSERT_LLVM_CODEGEN_UTIL_VALID(); return (MotJitContext*)jitContext; } diff --git a/src/gausskernel/storage/mot/jit_exec/jit_llvm_sp_codegen.h b/src/gausskernel/storage/mot/jit_exec/jit_llvm_sp_codegen.h index 4accd7221..c644ceb7a 100644 --- a/src/gausskernel/storage/mot/jit_exec/jit_llvm_sp_codegen.h +++ b/src/gausskernel/storage/mot/jit_exec/jit_llvm_sp_codegen.h @@ -48,6 +48,19 @@ struct JitPlan; */ extern MotJitContext* JitCodegenLlvmFunction(PLpgSQL_function* function, HeapTuple procTuple, Oid functionOid, ReturnSetInfo* returnSetInfo, JitPlan* plan, JitCodegenStats& codegenStats); + +/** + * @brief Executed a previously LLVM-jitted stored procedure. + * @param jitContext The context produced by a previous call to @ref QueryCodegen(). + * @param params The list of bound parameters passed to the query. + * @param[out] slot The slot used for reporting select result. + * @param[out] tuplesProcessed The variable used to report the number of processed tuples. + * @param[out] scanEnded The variable used to report if a range scan ended. + * @return Zero if succeeded, otherwise an error code. + * @note This function may cause transaction abort. + */ +extern int JitExecLlvmFunction(JitFunctionContext* jitContext, ParamListInfo params, TupleTableSlot* slot, + uint64_t* tuplesProcessed, int* scanEnded); } // namespace JitExec #endif /* JIT_LLVM_SP_CODEGEN_H */ \ No newline at end of file diff --git a/src/gausskernel/storage/mot/jit_exec/jit_llvm_util.cpp b/src/gausskernel/storage/mot/jit_exec/jit_llvm_util.cpp index 7c5489a21..aa7401eb2 100644 --- a/src/gausskernel/storage/mot/jit_exec/jit_llvm_util.cpp +++ b/src/gausskernel/storage/mot/jit_exec/jit_llvm_util.cpp @@ -87,6 +87,17 @@ extern void CleanupCompileFrames(LlvmCodeGenContext* ctx) } } +extern void JitResetCompileState() +{ + u_sess->mot_cxt.jit_llvm_if_stack = nullptr; + u_sess->mot_cxt.jit_llvm_loop_stack = nullptr; + u_sess->mot_cxt.jit_llvm_while_stack = nullptr; + u_sess->mot_cxt.jit_llvm_do_while_stack = nullptr; + u_sess->mot_cxt.jit_llvm_for_stack = nullptr; + u_sess->mot_cxt.jit_llvm_switch_case_stack = nullptr; + u_sess->mot_cxt.jit_llvm_try_catch_stack = nullptr; +} + extern llvm::FunctionCallee DefineFunction(llvm::Module* module, llvm::Type* returnType, const char* name, ...) { va_list vargs; diff --git a/src/gausskernel/storage/mot/jit_exec/jit_llvm_util.h b/src/gausskernel/storage/mot/jit_exec/jit_llvm_util.h index 40f8bd40a..04faeda02 100644 --- a/src/gausskernel/storage/mot/jit_exec/jit_llvm_util.h +++ b/src/gausskernel/storage/mot/jit_exec/jit_llvm_util.h @@ -222,6 +222,9 @@ extern void CloseCompileFrame(LlvmCodeGenContext* ctx); extern void CleanupCompileFrames(LlvmCodeGenContext* ctx); +/** @brief Reset compile state for LLVM (cleanup in case of error). */ +extern void JitResetCompileState(); + /** * @brief Helper function for defining LLVM external function. * @param module The LLVM module into which the function declaration is to be added. @@ -898,6 +901,16 @@ private: }; } // namespace llvm_util +#define JIT_ASSERT_LLVM_CODEGEN_UTIL_VALID() \ + do { \ + MOT_ASSERT(JIT_IF_CURRENT() == nullptr); \ + MOT_ASSERT(JIT_WHILE_CURRENT() == nullptr); \ + MOT_ASSERT(JIT_DO_WHILE_CURRENT() == nullptr); \ + MOT_ASSERT(JIT_FOR_CURRENT() == nullptr); \ + MOT_ASSERT(JIT_SWITCH_CURRENT() == nullptr); \ + MOT_ASSERT(JIT_TRY_CURRENT() == nullptr); \ + } while (0) + /************************************** * Return Statement **************************************/ diff --git a/src/gausskernel/storage/mot/jit_exec/jit_source.cpp b/src/gausskernel/storage/mot/jit_exec/jit_source.cpp index ca725e31d..d83006253 100644 --- a/src/gausskernel/storage/mot/jit_exec/jit_source.cpp +++ b/src/gausskernel/storage/mot/jit_exec/jit_source.cpp @@ -1081,9 +1081,6 @@ static JitCodegenState RegenerateJitFunctionSource(JitSource* jitSource) } PG_END_TRY(); - // reset compile state for robustness - JitResetCompileState(); - if (plan != nullptr) { JitDestroyPlan((JitPlan*)plan); }