From 68e3d53d0e40a7bd505e9acced7621e89f027f77 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Thu, 19 Oct 2023 04:45:46 +1300
Subject: [PATCH] jit: Require at least LLVM 14, if enabled.

Remove support for a lot of older LLVM versions dating back to 3.9.  The
default on common software distritbutions will be at least LLVM 14 when
PostgreSQL 17 ships.

Discussion: https://postgr.es/m/CA%2BhUKGLhNs5geZaVNj2EJ79Dx9W8fyWUU3HxcpZy55sMGcY%3DiA%40mail.gmail.com
---
 config/llvm.m4                          |   6 +-
 configure                               |  39 +-----
 doc/src/sgml/installation.sgml          |   4 +-
 meson.build                             |   2 +-
 src/backend/jit/llvm/llvmjit.c          | 158 +-----------------------
 src/backend/jit/llvm/llvmjit_error.cpp  |  35 ------
 src/backend/jit/llvm/llvmjit_expr.c     |   6 +-
 src/backend/jit/llvm/llvmjit_inline.cpp |  51 +-------
 src/backend/jit/llvm/llvmjit_wrap.cpp   |  63 +---------
 src/include/pg_config.h.in              |  12 --
 src/tools/msvc/Solution.pm              |   3 -
 11 files changed, 12 insertions(+), 367 deletions(-)

diff --git a/config/llvm.m4 b/config/llvm.m4
index 3a75cd8b4d..a99f29ffdc 100644
--- a/config/llvm.m4
+++ b/config/llvm.m4
@@ -25,8 +25,8 @@ AC_DEFUN([PGAC_LLVM_SUPPORT],
     AC_MSG_ERROR([$LLVM_CONFIG does not work])
   fi
   # and whether the version is supported
-  if echo $pgac_llvm_version | $AWK -F '.' '{ if ([$]1 >= 4 || ([$]1 == 3 && [$]2 >= 9)) exit 1; else exit 0;}';then
-    AC_MSG_ERROR([$LLVM_CONFIG version is $pgac_llvm_version but at least 3.9 is required])
+  if echo $pgac_llvm_version | $AWK -F '.' '{ if ([$]1 >= 14) exit 1; else exit 0;}';then
+    AC_MSG_ERROR([$LLVM_CONFIG version is $pgac_llvm_version but at least 14 is required])
   fi
 
   # need clang to create some bitcode files
@@ -114,8 +114,6 @@ AC_DEFUN([PGAC_CHECK_LLVM_FUNCTIONS],
   # Check which functionality is present
   SAVE_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$CPPFLAGS $LLVM_CPPFLAGS"
-  AC_CHECK_DECLS([LLVMOrcGetSymbolAddressIn], [], [], [[#include <llvm-c/OrcBindings.h>]])
-  AC_CHECK_DECLS([LLVMGetHostCPUName, LLVMGetHostCPUFeatures], [], [], [[#include <llvm-c/TargetMachine.h>]])
   AC_CHECK_DECLS([LLVMCreateGDBRegistrationListener, LLVMCreatePerfJITEventListener], [], [], [[#include <llvm-c/ExecutionEngine.h>]])
   CPPFLAGS="$SAVE_CPPFLAGS"
 ])# PGAC_CHECK_LLVM_FUNCTIONS
diff --git a/configure b/configure
index d47e0f8b26..f4a04485e1 100755
--- a/configure
+++ b/configure
@@ -5120,8 +5120,8 @@ fi
     as_fn_error $? "$LLVM_CONFIG does not work" "$LINENO" 5
   fi
   # and whether the version is supported
-  if echo $pgac_llvm_version | $AWK -F '.' '{ if ($1 >= 4 || ($1 == 3 && $2 >= 9)) exit 1; else exit 0;}';then
-    as_fn_error $? "$LLVM_CONFIG version is $pgac_llvm_version but at least 3.9 is required" "$LINENO" 5
+  if echo $pgac_llvm_version | $AWK -F '.' '{ if ($1 >= 14) exit 1; else exit 0;}';then
+    as_fn_error $? "$LLVM_CONFIG version is $pgac_llvm_version but at least 14 is required" "$LINENO" 5
   fi
 
   # need clang to create some bitcode files
@@ -16571,41 +16571,6 @@ if test "$with_llvm" = yes; then
   # Check which functionality is present
   SAVE_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$CPPFLAGS $LLVM_CPPFLAGS"
-  ac_fn_c_check_decl "$LINENO" "LLVMOrcGetSymbolAddressIn" "ac_cv_have_decl_LLVMOrcGetSymbolAddressIn" "#include <llvm-c/OrcBindings.h>
-"
-if test "x$ac_cv_have_decl_LLVMOrcGetSymbolAddressIn" = xyes; then :
-  ac_have_decl=1
-else
-  ac_have_decl=0
-fi
-
-cat >>confdefs.h <<_ACEOF
-#define HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN $ac_have_decl
-_ACEOF
-
-  ac_fn_c_check_decl "$LINENO" "LLVMGetHostCPUName" "ac_cv_have_decl_LLVMGetHostCPUName" "#include <llvm-c/TargetMachine.h>
-"
-if test "x$ac_cv_have_decl_LLVMGetHostCPUName" = xyes; then :
-  ac_have_decl=1
-else
-  ac_have_decl=0
-fi
-
-cat >>confdefs.h <<_ACEOF
-#define HAVE_DECL_LLVMGETHOSTCPUNAME $ac_have_decl
-_ACEOF
-ac_fn_c_check_decl "$LINENO" "LLVMGetHostCPUFeatures" "ac_cv_have_decl_LLVMGetHostCPUFeatures" "#include <llvm-c/TargetMachine.h>
-"
-if test "x$ac_cv_have_decl_LLVMGetHostCPUFeatures" = xyes; then :
-  ac_have_decl=1
-else
-  ac_have_decl=0
-fi
-
-cat >>confdefs.h <<_ACEOF
-#define HAVE_DECL_LLVMGETHOSTCPUFEATURES $ac_have_decl
-_ACEOF
-
   ac_fn_c_check_decl "$LINENO" "LLVMCreateGDBRegistrationListener" "ac_cv_have_decl_LLVMCreateGDBRegistrationListener" "#include <llvm-c/ExecutionEngine.h>
 "
 if test "x$ac_cv_have_decl_LLVMCreateGDBRegistrationListener" = xyes; then :
diff --git a/doc/src/sgml/installation.sgml b/doc/src/sgml/installation.sgml
index f4b1f81189..20453ab586 100644
--- a/doc/src/sgml/installation.sgml
+++ b/doc/src/sgml/installation.sgml
@@ -965,7 +965,7 @@ build-postgresql:
          linkend="jit"/>)</phrase>.  This
          requires the <productname>LLVM</productname> library to be installed.
          The minimum required version of <productname>LLVM</productname> is
-         currently 3.9.
+         currently 14.
         </para>
         <para>
          <command>llvm-config</command><indexterm><primary>llvm-config</primary></indexterm>
@@ -2432,7 +2432,7 @@ ninja install
         condition="standalone-ignore"> (see <xref linkend="jit"/>)</phrase>.
         This requires the <productname>LLVM</productname> library to be
         installed.  The minimum required version of
-        <productname>LLVM</productname> is currently 3.9.  Disabled by
+        <productname>LLVM</productname> is currently 14.  Disabled by
         default.
        </para>
 
diff --git a/meson.build b/meson.build
index 862c955453..4c12c60bb6 100644
--- a/meson.build
+++ b/meson.build
@@ -748,7 +748,7 @@ endif
 llvmopt = get_option('llvm')
 llvm = not_found_dep
 if add_languages('cpp', required: llvmopt, native: false)
-  llvm = dependency('llvm', version: '>=3.9', method: 'config-tool', required: llvmopt)
+  llvm = dependency('llvm', version: '>=14', method: 'config-tool', required: llvmopt)
 
   if llvm.found()
 
diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
index 58f638859a..d31e4759a6 100644
--- a/src/backend/jit/llvm/llvmjit.c
+++ b/src/backend/jit/llvm/llvmjit.c
@@ -21,23 +21,17 @@
 #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>
 #include <llvm-c/LLJIT.h>
-#else
-#include <llvm-c/OrcBindings.h>
-#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"
@@ -52,13 +46,8 @@
 /* Handle of a module emitted via ORC JIT */
 typedef struct LLVMJitHandle
 {
-#if LLVM_VERSION_MAJOR > 11
 	LLVMOrcLLJITRef lljit;
 	LLVMOrcResourceTrackerRef resource_tracker;
-#else
-	LLVMOrcJITStackRef stack;
-	LLVMOrcModuleHandle orc_handle;
-#endif
 } LLVMJitHandle;
 
 
@@ -105,14 +94,9 @@ static LLVMContextRef llvm_context;
 
 
 static LLVMTargetRef llvm_targetref;
-#if LLVM_VERSION_MAJOR > 11
 static LLVMOrcThreadSafeContextRef llvm_ts_context;
 static LLVMOrcLLJITRef llvm_opt0_orc;
 static LLVMOrcLLJITRef llvm_opt3_orc;
-#else							/* LLVM_VERSION_MAJOR > 11 */
-static LLVMOrcJITStackRef llvm_opt0_orc;
-static LLVMOrcJITStackRef llvm_opt3_orc;
-#endif							/* LLVM_VERSION_MAJOR > 11 */
 
 
 static void llvm_release_context(JitContext *context);
@@ -126,10 +110,8 @@ static void llvm_set_target(void);
 static void llvm_recreate_llvm_context(void);
 static uint64_t llvm_resolve_symbol(const char *name, void *ctx);
 
-#if LLVM_VERSION_MAJOR > 11
 static LLVMOrcLLJITRef llvm_create_jit_instance(LLVMTargetMachineRef tm);
 static char *llvm_error_message(LLVMErrorRef error);
-#endif							/* LLVM_VERSION_MAJOR > 11 */
 
 PG_MODULE_MAGIC;
 
@@ -270,7 +252,6 @@ llvm_release_context(JitContext *context)
 	{
 		LLVMJitHandle *jit_handle = (LLVMJitHandle *) lfirst(lc);
 
-#if LLVM_VERSION_MAJOR > 11
 		{
 			LLVMOrcExecutionSessionRef ee;
 			LLVMOrcSymbolStringPoolRef sp;
@@ -288,11 +269,6 @@ llvm_release_context(JitContext *context)
 			sp = LLVMOrcExecutionSessionGetSymbolStringPool(ee);
 			LLVMOrcSymbolStringPoolClearDeadEntries(sp);
 		}
-#else							/* LLVM_VERSION_MAJOR > 11 */
-		{
-			LLVMOrcRemoveModule(jit_handle->stack, jit_handle->orc_handle);
-		}
-#endif							/* LLVM_VERSION_MAJOR > 11 */
 
 		pfree(jit_handle);
 	}
@@ -354,10 +330,7 @@ llvm_expand_funcname(struct LLVMJitContext *context, const char *basename)
 void *
 llvm_get_function(LLVMJitContext *context, const char *funcname)
 {
-#if LLVM_VERSION_MAJOR > 11 || \
-	defined(HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN) && HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN
 	ListCell   *lc;
-#endif
 
 	llvm_assert_in_fatal_section();
 
@@ -375,7 +348,6 @@ llvm_get_function(LLVMJitContext *context, const char *funcname)
 	 * to mangle here.
 	 */
 
-#if LLVM_VERSION_MAJOR > 11
 	foreach(lc, context->handles)
 	{
 		LLVMJitHandle *handle = (LLVMJitHandle *) lfirst(lc);
@@ -405,41 +377,6 @@ llvm_get_function(LLVMJitContext *context, const char *funcname)
 		if (addr)
 			return (void *) (uintptr_t) addr;
 	}
-#elif defined(HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN) && HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN
-	foreach(lc, context->handles)
-	{
-		LLVMOrcTargetAddress addr;
-		LLVMJitHandle *handle = (LLVMJitHandle *) lfirst(lc);
-
-		addr = 0;
-		if (LLVMOrcGetSymbolAddressIn(handle->stack, &addr, handle->orc_handle, funcname))
-			elog(ERROR, "failed to look up symbol \"%s\"", funcname);
-		if (addr)
-			return (void *) (uintptr_t) addr;
-	}
-#elif LLVM_VERSION_MAJOR < 5
-	{
-		LLVMOrcTargetAddress addr;
-
-		if ((addr = LLVMOrcGetSymbolAddress(llvm_opt0_orc, funcname)))
-			return (void *) (uintptr_t) addr;
-		if ((addr = LLVMOrcGetSymbolAddress(llvm_opt3_orc, funcname)))
-			return (void *) (uintptr_t) addr;
-	}
-#else
-	{
-		LLVMOrcTargetAddress addr;
-
-		if (LLVMOrcGetSymbolAddress(llvm_opt0_orc, &addr, funcname))
-			elog(ERROR, "failed to look up symbol \"%s\"", funcname);
-		if (addr)
-			return (void *) (uintptr_t) addr;
-		if (LLVMOrcGetSymbolAddress(llvm_opt3_orc, &addr, funcname))
-			elog(ERROR, "failed to look up symbol \"%s\"", funcname);
-		if (addr)
-			return (void *) (uintptr_t) addr;
-	}
-#endif
 
 	elog(ERROR, "failed to JIT: %s", funcname);
 
@@ -526,7 +463,7 @@ llvm_copy_attributes_at_index(LLVMValueRef v_from, LLVMValueRef v_to, uint32 ind
 	int			num_attributes;
 	LLVMAttributeRef *attrs;
 
-	num_attributes = LLVMGetAttributeCountAtIndexPG(v_from, index);
+	num_attributes = LLVMGetAttributeCountAtIndex(v_from, index);
 
 	/*
 	 * Not just for efficiency: LLVM <= 3.9 crashes when
@@ -736,11 +673,7 @@ llvm_compile_module(LLVMJitContext *context)
 	MemoryContext oldcontext;
 	instr_time	starttime;
 	instr_time	endtime;
-#if LLVM_VERSION_MAJOR > 11
 	LLVMOrcLLJITRef compile_orc;
-#else
-	LLVMOrcJITStackRef compile_orc;
-#endif
 
 	if (context->base.flags & PGJIT_OPT3)
 		compile_orc = llvm_opt3_orc;
@@ -797,7 +730,6 @@ llvm_compile_module(LLVMJitContext *context)
 	 * faster instruction selection mechanism is used.
 	 */
 	INSTR_TIME_SET_CURRENT(starttime);
-#if LLVM_VERSION_MAJOR > 11
 	{
 		LLVMOrcThreadSafeModuleRef ts_module;
 		LLVMErrorRef error;
@@ -825,36 +757,6 @@ llvm_compile_module(LLVMJitContext *context)
 
 		/* LLVMOrcLLJITAddLLVMIRModuleWithRT takes ownership of the module */
 	}
-#elif LLVM_VERSION_MAJOR > 6
-	{
-		handle->stack = compile_orc;
-		if (LLVMOrcAddEagerlyCompiledIR(compile_orc, &handle->orc_handle, context->module,
-										llvm_resolve_symbol, NULL))
-			elog(ERROR, "failed to JIT module");
-
-		/* LLVMOrcAddEagerlyCompiledIR takes ownership of the module */
-	}
-#elif LLVM_VERSION_MAJOR > 4
-	{
-		LLVMSharedModuleRef smod;
-
-		smod = LLVMOrcMakeSharedModule(context->module);
-		handle->stack = compile_orc;
-		if (LLVMOrcAddEagerlyCompiledIR(compile_orc, &handle->orc_handle, smod,
-										llvm_resolve_symbol, NULL))
-			elog(ERROR, "failed to JIT module");
-
-		LLVMOrcDisposeSharedModuleRef(smod);
-	}
-#else							/* LLVM 4.0 and 3.9 */
-	{
-		handle->stack = compile_orc;
-		handle->orc_handle = LLVMOrcAddEagerlyCompiledIR(compile_orc, context->module,
-														 llvm_resolve_symbol, NULL);
-
-		LLVMDisposeModule(context->module);
-	}
-#endif
 
 	INSTR_TIME_SET_CURRENT(endtime);
 	INSTR_TIME_ACCUM_DIFF(context->base.instr.emission_counter,
@@ -966,7 +868,6 @@ llvm_session_initialize(void)
 	/* force symbols in main binary to be loaded */
 	LLVMLoadLibraryPermanently(NULL);
 
-#if LLVM_VERSION_MAJOR > 11
 	{
 		llvm_ts_context = LLVMOrcCreateNewThreadSafeContext();
 
@@ -976,31 +877,6 @@ llvm_session_initialize(void)
 		llvm_opt3_orc = llvm_create_jit_instance(opt3_tm);
 		opt3_tm = 0;
 	}
-#else							/* LLVM_VERSION_MAJOR > 11 */
-	{
-		llvm_opt0_orc = LLVMOrcCreateInstance(opt0_tm);
-		llvm_opt3_orc = LLVMOrcCreateInstance(opt3_tm);
-
-#if defined(HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER) && HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER
-		if (jit_debugging_support)
-		{
-			LLVMJITEventListenerRef l = LLVMCreateGDBRegistrationListener();
-
-			LLVMOrcRegisterJITEventListener(llvm_opt0_orc, l);
-			LLVMOrcRegisterJITEventListener(llvm_opt3_orc, l);
-		}
-#endif
-#if defined(HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER) && HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER
-		if (jit_profiling_support)
-		{
-			LLVMJITEventListenerRef l = LLVMCreatePerfJITEventListener();
-
-			LLVMOrcRegisterJITEventListener(llvm_opt0_orc, l);
-			LLVMOrcRegisterJITEventListener(llvm_opt3_orc, l);
-		}
-#endif
-	}
-#endif							/* LLVM_VERSION_MAJOR > 11 */
 
 	on_proc_exit(llvm_shutdown, 0);
 
@@ -1030,7 +906,6 @@ llvm_shutdown(int code, Datum arg)
 		elog(PANIC, "LLVMJitContext in use count not 0 at exit (is %zu)",
 			 llvm_jit_context_in_use_count);
 
-#if LLVM_VERSION_MAJOR > 11
 	{
 		if (llvm_opt3_orc)
 		{
@@ -1048,31 +923,6 @@ llvm_shutdown(int code, Datum arg)
 			llvm_ts_context = NULL;
 		}
 	}
-#else							/* LLVM_VERSION_MAJOR > 11 */
-	{
-		/* unregister profiling support, needs to be flushed to be useful */
-
-		if (llvm_opt3_orc)
-		{
-#if defined(HAVE_DECL_LLVMORCREGISTERPERF) && HAVE_DECL_LLVMORCREGISTERPERF
-			if (jit_profiling_support)
-				LLVMOrcUnregisterPerf(llvm_opt3_orc);
-#endif
-			LLVMOrcDisposeInstance(llvm_opt3_orc);
-			llvm_opt3_orc = NULL;
-		}
-
-		if (llvm_opt0_orc)
-		{
-#if defined(HAVE_DECL_LLVMORCREGISTERPERF) && HAVE_DECL_LLVMORCREGISTERPERF
-			if (jit_profiling_support)
-				LLVMOrcUnregisterPerf(llvm_opt0_orc);
-#endif
-			LLVMOrcDisposeInstance(llvm_opt0_orc);
-			llvm_opt0_orc = NULL;
-		}
-	}
-#endif							/* LLVM_VERSION_MAJOR > 11 */
 }
 
 /* helper for llvm_create_types, returning a function's return type */
@@ -1242,8 +1092,6 @@ llvm_resolve_symbol(const char *symname, void *ctx)
 	return (uint64_t) addr;
 }
 
-#if LLVM_VERSION_MAJOR > 11
-
 static LLVMErrorRef
 llvm_resolve_symbols(LLVMOrcDefinitionGeneratorRef GeneratorObj, void *Ctx,
 					 LLVMOrcLookupStateRef *LookupState, LLVMOrcLookupKind Kind,
@@ -1262,9 +1110,7 @@ llvm_resolve_symbols(LLVMOrcDefinitionGeneratorRef GeneratorObj, void *Ctx,
 	{
 		const char *name = LLVMOrcSymbolStringPoolEntryStr(LookupSet[i].Name);
 
-#if LLVM_VERSION_MAJOR > 12
 		LLVMOrcRetainSymbolStringPoolEntry(LookupSet[i].Name);
-#endif
 		symbols[i].Name = LookupSet[i].Name;
 		symbols[i].Sym.Address = llvm_resolve_symbol(name, NULL);
 		symbols[i].Sym.Flags.GenericFlags = LLVMJITSymbolGenericFlagsExported;
@@ -1392,5 +1238,3 @@ llvm_error_message(LLVMErrorRef error)
 
 	return msg;
 }
-
-#endif							/* LLVM_VERSION_MAJOR > 11 */
diff --git a/src/backend/jit/llvm/llvmjit_error.cpp b/src/backend/jit/llvm/llvmjit_error.cpp
index ea40f5a124..bcf51f12a9 100644
--- a/src/backend/jit/llvm/llvmjit_error.cpp
+++ b/src/backend/jit/llvm/llvmjit_error.cpp
@@ -29,16 +29,8 @@ static int fatal_new_handler_depth = 0;
 static std::new_handler old_new_handler = NULL;
 
 static void fatal_system_new_handler(void);
-#if LLVM_VERSION_MAJOR > 4
 static void fatal_llvm_new_handler(void *user_data, const char *reason, bool gen_crash_diag);
-#if LLVM_VERSION_MAJOR < 14
-static void fatal_llvm_new_handler(void *user_data, const std::string& reason, bool gen_crash_diag);
-#endif
-#endif
 static void fatal_llvm_error_handler(void *user_data, const char *reason, bool gen_crash_diag);
-#if LLVM_VERSION_MAJOR < 14
-static void fatal_llvm_error_handler(void *user_data, const std::string& reason, bool gen_crash_diag);
-#endif
 
 
 /*
@@ -65,9 +57,7 @@ llvm_enter_fatal_on_oom(void)
 	if (fatal_new_handler_depth == 0)
 	{
 		old_new_handler = std::set_new_handler(fatal_system_new_handler);
-#if LLVM_VERSION_MAJOR > 4
 		llvm::install_bad_alloc_error_handler(fatal_llvm_new_handler);
-#endif
 		llvm::install_fatal_error_handler(fatal_llvm_error_handler);
 	}
 	fatal_new_handler_depth++;
@@ -83,9 +73,7 @@ llvm_leave_fatal_on_oom(void)
 	if (fatal_new_handler_depth == 0)
 	{
 		std::set_new_handler(old_new_handler);
-#if LLVM_VERSION_MAJOR > 4
 		llvm::remove_bad_alloc_error_handler();
-#endif
 		llvm::remove_fatal_error_handler();
 	}
 }
@@ -110,9 +98,7 @@ llvm_reset_after_error(void)
 	if (fatal_new_handler_depth != 0)
 	{
 		std::set_new_handler(old_new_handler);
-#if LLVM_VERSION_MAJOR > 4
 		llvm::remove_bad_alloc_error_handler();
-#endif
 		llvm::remove_fatal_error_handler();
 	}
 	fatal_new_handler_depth = 0;
@@ -133,7 +119,6 @@ fatal_system_new_handler(void)
 			 errdetail("while in LLVM")));
 }
 
-#if LLVM_VERSION_MAJOR > 4
 static void
 fatal_llvm_new_handler(void *user_data,
 					   const char *reason,
@@ -144,16 +129,6 @@ fatal_llvm_new_handler(void *user_data,
 			 errmsg("out of memory"),
 			 errdetail("While in LLVM: %s", reason)));
 }
-#if LLVM_VERSION_MAJOR < 14
-static void
-fatal_llvm_new_handler(void *user_data,
-					   const std::string& reason,
-					   bool gen_crash_diag)
-{
-	fatal_llvm_new_handler(user_data, reason.c_str(), gen_crash_diag);
-}
-#endif
-#endif
 
 static void
 fatal_llvm_error_handler(void *user_data,
@@ -164,13 +139,3 @@ fatal_llvm_error_handler(void *user_data,
 			(errcode(ERRCODE_OUT_OF_MEMORY),
 			 errmsg("fatal llvm error: %s", reason)));
 }
-
-#if LLVM_VERSION_MAJOR < 14
-static void
-fatal_llvm_error_handler(void *user_data,
-						 const std::string& reason,
-						 bool gen_crash_diag)
-{
-	fatal_llvm_error_handler(user_data, reason.c_str(), gen_crash_diag);
-}
-#endif
diff --git a/src/backend/jit/llvm/llvmjit_expr.c b/src/backend/jit/llvm/llvmjit_expr.c
index a3a0876bff..a950564afa 100644
--- a/src/backend/jit/llvm/llvmjit_expr.c
+++ b/src/backend/jit/llvm/llvmjit_expr.c
@@ -2644,12 +2644,8 @@ create_LifetimeEnd(LLVMModuleRef mod)
 	LLVMTypeRef param_types[2];
 	LLVMContextRef lc;
 
-	/* LLVM 5+ has a variadic pointer argument */
-#if LLVM_VERSION_MAJOR < 5
-	const char *nm = "llvm.lifetime.end";
-#else
+	/* variadic pointer argument */
 	const char *nm = "llvm.lifetime.end.p0i8";
-#endif
 
 	fn = LLVMGetNamedFunction(mod, nm);
 	if (fn)
diff --git a/src/backend/jit/llvm/llvmjit_inline.cpp b/src/backend/jit/llvm/llvmjit_inline.cpp
index d92d7f3c88..693bc6510b 100644
--- a/src/backend/jit/llvm/llvmjit_inline.cpp
+++ b/src/backend/jit/llvm/llvmjit_inline.cpp
@@ -49,12 +49,7 @@ extern "C"
 #include <llvm/ADT/StringSet.h>
 #include <llvm/ADT/StringMap.h>
 #include <llvm/Analysis/ModuleSummaryAnalysis.h>
-#if LLVM_VERSION_MAJOR > 3
 #include <llvm/Bitcode/BitcodeReader.h>
-#else
-#include <llvm/Bitcode/ReaderWriter.h>
-#include <llvm/Support/Error.h>
-#endif
 #include <llvm/IR/Attributes.h>
 #include <llvm/IR/DebugInfo.h>
 #include <llvm/IR/IntrinsicInst.h>
@@ -267,14 +262,12 @@ llvm_build_inline_plan(LLVMContextRef lc, llvm::Module *mod)
 
 			fs = llvm::cast<llvm::FunctionSummary>(gvs);
 
-#if LLVM_VERSION_MAJOR > 3
 			if (gvs->notEligibleToImport())
 			{
 				ilog(DEBUG1, "ineligibile to import %s due to summary",
 					 symbolName.data());
 				continue;
 			}
-#endif
 
 			if ((int) fs->instCount() > inlineState.costLimit)
 			{
@@ -458,16 +451,9 @@ llvm_execute_inline_plan(llvm::Module *mod, ImportMapTy *globalsToInline)
 
 		}
 
-#if LLVM_VERSION_MAJOR > 4
-#define IRMOVE_PARAMS , /*IsPerformingImport=*/false
-#elif LLVM_VERSION_MAJOR > 3
-#define IRMOVE_PARAMS , /*LinkModuleInlineAsm=*/false, /*IsPerformingImport=*/false
-#else
-#define IRMOVE_PARAMS
-#endif
 		if (Mover.move(std::move(importMod), GlobalsToImport.getArrayRef(),
-					   [](llvm::GlobalValue &, llvm::IRMover::ValueAdder) {}
-					   IRMOVE_PARAMS))
+					   [](llvm::GlobalValue &, llvm::IRMover::ValueAdder) {},
+					   /*IsPerformingImport=*/false))
 			elog(FATAL, "function import failed with linker error");
 	}
 }
@@ -608,10 +594,6 @@ function_inlinable(llvm::Function &F,
 	if (F.materialize())
 		elog(FATAL, "failed to materialize metadata");
 
-#if LLVM_VERSION_MAJOR < 14
-#define hasFnAttr hasFnAttribute
-#endif
-
 	if (F.getAttributes().hasFnAttr(llvm::Attribute::NoInline))
 	{
 		ilog(DEBUG1, "ineligibile to import %s due to noinline",
@@ -793,7 +775,6 @@ llvm_load_summary(llvm::StringRef path)
 	{
 		llvm::MemoryBufferRef ref(*MBOrErr.get().get());
 
-#if LLVM_VERSION_MAJOR > 3
 		llvm::Expected<std::unique_ptr<llvm::ModuleSummaryIndex> > IndexOrErr =
 			llvm::getModuleSummaryIndex(ref);
 		if (IndexOrErr)
@@ -801,15 +782,6 @@ llvm_load_summary(llvm::StringRef path)
 		elog(FATAL, "failed to load summary \"%s\": %s",
 			 path.data(),
 			 toString(IndexOrErr.takeError()).c_str());
-#else
-		llvm::ErrorOr<std::unique_ptr<llvm::ModuleSummaryIndex> > IndexOrErr =
-			llvm::getModuleSummaryIndex(ref, [](const llvm::DiagnosticInfo &) {});
-		if (IndexOrErr)
-			return std::move(IndexOrErr.get());
-		elog(FATAL, "failed to load summary \"%s\": %s",
-			 path.data(),
-			 IndexOrErr.getError().message().c_str());
-#endif
 	}
 	return nullptr;
 }
@@ -853,22 +825,12 @@ summaries_for_guid(const InlineSearchPath& path, llvm::GlobalValue::GUID guid)
 
 	for (auto index : path)
 	{
-#if LLVM_VERSION_MAJOR > 4
 		llvm::ValueInfo funcVI = index->getValueInfo(guid);
 
 		/* if index doesn't know function, we don't have a body, continue */
 		if (funcVI)
 			for (auto &gv : funcVI.getSummaryList())
 				matches.push_back(gv.get());
-#else
-		const llvm::const_gvsummary_iterator &I =
-			index->findGlobalValueSummaryList(guid);
-		if (I != index->end())
-		{
-			for (auto &gv : I->second)
-				matches.push_back(gv.get());
-		}
-#endif
 	}
 
 	return matches;
@@ -889,9 +851,6 @@ create_redirection_function(std::unique_ptr<llvm::Module> &importMod,
 	llvm::Function *AF;
 	llvm::BasicBlock *BB;
 	llvm::CallInst *fwdcall;
-#if LLVM_VERSION_MAJOR < 14
-	llvm::Attribute inlineAttribute;
-#endif
 
 	AF = llvm::Function::Create(F->getFunctionType(),
 								LinkageTypes::AvailableExternallyLinkage,
@@ -900,13 +859,7 @@ create_redirection_function(std::unique_ptr<llvm::Module> &importMod,
 
 	Builder.SetInsertPoint(BB);
 	fwdcall = Builder.CreateCall(F, &*AF->arg_begin());
-#if LLVM_VERSION_MAJOR < 14
-	inlineAttribute = llvm::Attribute::get(Context,
-										   llvm::Attribute::AlwaysInline);
-	fwdcall->addAttribute(~0U, inlineAttribute);
-#else
 	fwdcall->addFnAttr(llvm::Attribute::AlwaysInline);
-#endif
 	Builder.CreateRet(fwdcall);
 
 	return AF;
diff --git a/src/backend/jit/llvm/llvmjit_wrap.cpp b/src/backend/jit/llvm/llvmjit_wrap.cpp
index 90a41b9191..52ce09c981 100644
--- a/src/backend/jit/llvm/llvmjit_wrap.cpp
+++ b/src/backend/jit/llvm/llvmjit_wrap.cpp
@@ -19,18 +19,9 @@ extern "C"
 #include <llvm-c/Core.h>
 
 /* Avoid macro clash with LLVM's C++ headers */
-#undef Min
+//#undef Min
 
-#include <llvm/IR/Attributes.h>
 #include <llvm/IR/Function.h>
-#if LLVM_VERSION_MAJOR < 17
-#include <llvm/MC/SubtargetFeature.h>
-#endif
-#if LLVM_VERSION_MAJOR > 16
-#include <llvm/TargetParser/Host.h>
-#else
-#include <llvm/Support/Host.h>
-#endif
 
 #include "jit/llvmjit.h"
 
@@ -38,50 +29,6 @@ extern "C"
 /*
  * C-API extensions.
  */
-#if defined(HAVE_DECL_LLVMGETHOSTCPUNAME) && !HAVE_DECL_LLVMGETHOSTCPUNAME
-char *LLVMGetHostCPUName(void) {
-	return strdup(llvm::sys::getHostCPUName().data());
-}
-#endif
-
-
-#if defined(HAVE_DECL_LLVMGETHOSTCPUFEATURES) && !HAVE_DECL_LLVMGETHOSTCPUFEATURES
-char *LLVMGetHostCPUFeatures(void) {
-	llvm::SubtargetFeatures Features;
-	llvm::StringMap<bool> HostFeatures;
-
-	if (llvm::sys::getHostCPUFeatures(HostFeatures))
-		for (auto &F : HostFeatures)
-			Features.AddFeature(F.first(), F.second);
-
-	return strdup(Features.getString().c_str());
-}
-#endif
-
-/*
- * Like LLVM's LLVMGetAttributeCountAtIndex(), works around a bug in LLVM 3.9.
- *
- * In LLVM <= 3.9, LLVMGetAttributeCountAtIndex() segfaults if there are no
- * attributes at an index (fixed in LLVM commit ce9bb1097dc2).
- */
-unsigned
-LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx)
-{
-	/*
-	 * This is more expensive, so only do when using a problematic LLVM
-	 * version.
-	 */
-#if LLVM_VERSION_MAJOR < 4
-	if (!llvm::unwrap<llvm::Function>(F)->getAttributes().hasAttributes(Idx))
-		return 0;
-#endif
-
-	/*
-	 * There is no nice public API to determine the count nicely, so just
-	 * always fall back to LLVM's C API.
-	 */
-	return LLVMGetAttributeCountAtIndex(F, Idx);
-}
 
 LLVMTypeRef
 LLVMGetFunctionReturnType(LLVMValueRef r)
@@ -94,11 +41,3 @@ LLVMGetFunctionType(LLVMValueRef r)
 {
 	return llvm::wrap(llvm::unwrap<llvm::Function>(r)->getFunctionType());
 }
-
-#if LLVM_VERSION_MAJOR < 8
-LLVMTypeRef
-LLVMGlobalGetValueType(LLVMValueRef g)
-{
-	return llvm::wrap(llvm::unwrap<llvm::GlobalValue>(g)->getValueType());
-}
-#endif
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index d8a2985567..481470ee59 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -103,18 +103,6 @@
    `LLVMCreatePerfJITEventListener', and to 0 if you don't. */
 #undef HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER
 
-/* Define to 1 if you have the declaration of `LLVMGetHostCPUFeatures', and to
-   0 if you don't. */
-#undef HAVE_DECL_LLVMGETHOSTCPUFEATURES
-
-/* Define to 1 if you have the declaration of `LLVMGetHostCPUName', and to 0
-   if you don't. */
-#undef HAVE_DECL_LLVMGETHOSTCPUNAME
-
-/* Define to 1 if you have the declaration of `LLVMOrcGetSymbolAddressIn', and
-   to 0 if you don't. */
-#undef HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN
-
 /* Define to 1 if you have the declaration of `posix_fadvise', and to 0 if you
    don't. */
 #undef HAVE_DECL_POSIX_FADVISE
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index a50f730260..5066bd9e28 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -235,9 +235,6 @@ sub GenerateFiles
 		HAVE_DECL_F_FULLFSYNC => 0,
 		HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER => 0,
 		HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER => 0,
-		HAVE_DECL_LLVMGETHOSTCPUNAME => 0,
-		HAVE_DECL_LLVMGETHOSTCPUFEATURES => 0,
-		HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN => 0,
 		HAVE_DECL_POSIX_FADVISE => 0,
 		HAVE_DECL_PREADV => 0,
 		HAVE_DECL_PWRITEV => 0,
-- 
2.39.3 (Apple Git-145)

