> On Mon, May 22, 2023 at 03:38:44PM +1200, Thomas Munro wrote: > Further changes are already needed for their "main" branch (LLVM > 17-to-be), so this won't quite be enough to shut seawasp up. At a > glance, we will need to change from the "old pass manager" API that > has recently been vaporised[1] > (llvm-c/Transforms/PassManagerBuilder.h) to the new one[2][3] > (llvm-c/Transforms/PassBuilder.h), which I suspect/hope will be as > simple as changing llvmjit.c to call LLVMRunPasses() with a string > describing the passes we want in "opt -passes" format, instead of our > code that calls LLVMAddFunctionInlingPass() etc. But that'll be a > topic for another day, and another thread. > > [1] > https://github.com/llvm/llvm-project/commit/0aac9a2875bad4f065367e4a6553fad78605f895 > [2] https://llvm.org/docs/NewPassManager.html > [3] https://reviews.llvm.org/D102136
Thanks for tackling the topic! I've tested it with a couple of versions, LLVM 12 that comes with my Gentoo box, LLVM 15 build from sources and the modified version of patch adopted for LLVM 17 (build form sources as well). In all three cases everything seems to be working fine. Simple benchmarking with a query stolen from some other jit thread (pgbench running single client with multiple unions of selects a-la SELECT a, count(*), sum(b) FROM test WHERE c = 2 GROUP BY a) show some slight performance differences, but nothing dramatic so far. LLVM 17 version produces the lowest latency, with faster generation, inlining and optimization, but slower emission time. LLVM 12 version produces the largest latencies with everything except emission timings being slower. LLVM 15 is somewhere in between. I'll continue reviewing and, for the records, attach adjustments I was using for LLVM 17 (purely for testing, not taking into account other versions), in case if I've missed something.
>From 33e39a376fdbcceb6d4e757f4a798b3922e7068c Mon Sep 17 00:00:00 2001 From: Dmitrii Dolgov <9erthali...@gmail.com> Date: Sun, 4 Jun 2023 11:10:41 +0200 Subject: [PATCH 2/2] LLVM 17 --- src/backend/jit/llvm/llvmjit.c | 73 ++++++++------------------- src/backend/jit/llvm/llvmjit_wrap.cpp | 4 ++ 2 files changed, 25 insertions(+), 52 deletions(-) diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c index b83bc04ae3a..b701e167abd 100644 --- a/src/backend/jit/llvm/llvmjit.c +++ b/src/backend/jit/llvm/llvmjit.c @@ -18,6 +18,9 @@ #include <llvm-c/BitWriter.h> #include <llvm-c/Core.h> #include <llvm-c/ExecutionEngine.h> +#if LLVM_VERSION_MAJOR > 16 +#include <llvm-c/Transforms/PassBuilder.h> +#endif #if LLVM_VERSION_MAJOR > 11 #include <llvm-c/Orc.h> #include <llvm-c/OrcEE.h> @@ -27,12 +30,14 @@ #endif #include <llvm-c/Support.h> #include <llvm-c/Target.h> +#if LLVM_VERSION_MAJOR < 17 #include <llvm-c/Transforms/IPO.h> #include <llvm-c/Transforms/PassManagerBuilder.h> #include <llvm-c/Transforms/Scalar.h> #if LLVM_VERSION_MAJOR > 6 #include <llvm-c/Transforms/Utils.h> #endif +#endif #include "jit/llvmjit.h" #include "jit/llvmjit_emit.h" @@ -559,69 +564,33 @@ llvm_function_reference(LLVMJitContext *context, static void llvm_optimize_module(LLVMJitContext *context, LLVMModuleRef module) { - LLVMPassManagerBuilderRef llvm_pmb; - LLVMPassManagerRef llvm_mpm; - LLVMPassManagerRef llvm_fpm; - LLVMValueRef func; + LLVMPassBuilderOptionsRef options; + LLVMErrorRef err; int compile_optlevel; + char *passes; if (context->base.flags & PGJIT_OPT3) compile_optlevel = 3; else compile_optlevel = 0; - /* - * Have to create a new pass manager builder every pass through, as the - * inliner has some per-builder state. Otherwise one ends up only inlining - * a function the first time though. - */ - llvm_pmb = LLVMPassManagerBuilderCreate(); - LLVMPassManagerBuilderSetOptLevel(llvm_pmb, compile_optlevel); - llvm_fpm = LLVMCreateFunctionPassManagerForModule(module); + passes = psprintf("default<O%d>,mem2reg,function(no-op-function),no-op-module", + compile_optlevel); - if (context->base.flags & PGJIT_OPT3) - { - /* TODO: Unscientifically determined threshold */ - LLVMPassManagerBuilderUseInlinerWithThreshold(llvm_pmb, 512); - } - else - { - /* we rely on mem2reg heavily, so emit even in the O0 case */ - LLVMAddPromoteMemoryToRegisterPass(llvm_fpm); - } + options = LLVMCreatePassBuilderOptions(); - LLVMPassManagerBuilderPopulateFunctionPassManager(llvm_pmb, llvm_fpm); +#ifdef LLVM_PASS_DEBUG + LLVMPassBuilderOptionsSetDebugLogging(options, 1); +#endif - /* - * Do function level optimization. This could be moved to the point where - * functions are emitted, to reduce memory usage a bit. - */ - LLVMInitializeFunctionPassManager(llvm_fpm); - for (func = LLVMGetFirstFunction(context->module); - func != NULL; - func = LLVMGetNextFunction(func)) - LLVMRunFunctionPassManager(llvm_fpm, func); - LLVMFinalizeFunctionPassManager(llvm_fpm); - LLVMDisposePassManager(llvm_fpm); + LLVMPassBuilderOptionsSetInlinerThreshold(options, 512); - /* - * Perform module level optimization. We do so even in the non-optimized - * case, so always-inline functions etc get inlined. It's cheap enough. - */ - llvm_mpm = LLVMCreatePassManager(); - LLVMPassManagerBuilderPopulateModulePassManager(llvm_pmb, - llvm_mpm); - /* always use always-inliner pass */ - if (!(context->base.flags & PGJIT_OPT3)) - LLVMAddAlwaysInlinerPass(llvm_mpm); - /* if doing inlining, but no expensive optimization, add inlining pass */ - if (context->base.flags & PGJIT_INLINE - && !(context->base.flags & PGJIT_OPT3)) - LLVMAddFunctionInliningPass(llvm_mpm); - LLVMRunPassManager(llvm_mpm, context->module); - LLVMDisposePassManager(llvm_mpm); - - LLVMPassManagerBuilderDispose(llvm_pmb); + err = LLVMRunPasses(module, passes, NULL, options); + + if (err) + elog(ERROR, "failed to JIT module: %s", llvm_error_message(err)); + + LLVMDisposePassBuilderOptions(options); } /* diff --git a/src/backend/jit/llvm/llvmjit_wrap.cpp b/src/backend/jit/llvm/llvmjit_wrap.cpp index 997a2c02789..7b51f3d825c 100644 --- a/src/backend/jit/llvm/llvmjit_wrap.cpp +++ b/src/backend/jit/llvm/llvmjit_wrap.cpp @@ -24,7 +24,11 @@ extern "C" #include <llvm/IR/Attributes.h> #include <llvm/IR/Function.h> #include <llvm/MC/SubtargetFeature.h> +#if LLVM_VERSION_MAJOR > 16 +#include <llvm/TargetParser/Host.h> +#else #include <llvm/Support/Host.h> +#endif #include "jit/llvmjit.h" -- 2.32.0