JIT on FreeBSD ARMv7 - Mailing list pgsql-hackers

From Andrew Gierth
Subject JIT on FreeBSD ARMv7
Date
Msg-id 87imxbgtm9.fsf@news-spur.riddles.org.uk
Whole thread Raw
Responses Re: JIT on FreeBSD ARMv7  (Andrew Gierth <andrew@tao11.riddles.org.uk>)
List pgsql-hackers
This seems to be the cleanest way to get a JIT build working on FreeBSD
on the armv7 platform. Without this, it fails because the ABI functions
__aeabi_ldivmod and so on are not found by the symbol lookup for JITted
code.

It's completely unclear who (llvm, freebsd, or us) is at fault here for
it not working before; some of the LLVM code contains special-case
symbol lookups for the ARM __aeabi funcs, but only enabled when
compiling on Android; the response I got from asking about it on
#bsdmips (which despite its name is the main freebsd/arm hangout) was
"this is an ongoing mess that isn't really getting worked on". And
honestly I suspect the set of people actually trying to use LLVM for JIT
on freebsd/arm is negligible; I only did it because testing weird edge
cases is often useful.

This patch should be harmless on any 32-bit arm platform where JIT
already works, because all it's doing is forcibly preloading symbols.

Test platform details:
FreeBSD 12.0-STABLE r344243 arm
CPU: ARM Cortex-A7  (Raspberry Pi 2B)
LLVM 7.0.1 (known to fail on LLVM 6 unless you patch LLVM itself)

./configure --prefix=/data/small/andrew/pgsql \
            --with-includes=/usr/local/include \
            --with-libs=/usr/local/lib \
            --with-openssl \
            --enable-debug \
            --enable-depend \
            --enable-cassert \
            --with-llvm \
            LLVM_CONFIG=llvm-config70 \
            CC="ccache clang70" \
            CLANG="ccache clang70" \
            CXX="ccache clang++70" \
            CFLAGS="-O2 -mcpu=cortex-a7"

-- 
Andrew (irc:RhodiumToad)

diff --git a/src/backend/jit/llvm/abi_funcs_arm.h b/src/backend/jit/llvm/abi_funcs_arm.h
new file mode 100644
index 0000000000..48ef0a5f6b
--- /dev/null
+++ b/src/backend/jit/llvm/abi_funcs_arm.h
@@ -0,0 +1,82 @@
+/*
+ * List of ARM ABI functions to forcibly import.
+ */
+
+/* There is intentionally no #if guard here */
+
+ABISYM(__aeabi_atexit)
+ABISYM(__aeabi_cdcmpeq)
+ABISYM(__aeabi_cdcmple)
+ABISYM(__aeabi_cdrcmple)
+ABISYM(__aeabi_cfcmpeq)
+ABISYM(__aeabi_cfcmple)
+ABISYM(__aeabi_cfrcmple)
+ABISYM(__aeabi_d2f)
+ABISYM(__aeabi_d2h)
+ABISYM(__aeabi_d2iz)
+ABISYM(__aeabi_d2lz)
+ABISYM(__aeabi_d2ulz)
+ABISYM(__aeabi_dadd)
+ABISYM(__aeabi_dcmpeq)
+ABISYM(__aeabi_dcmpge)
+ABISYM(__aeabi_dcmpgt)
+ABISYM(__aeabi_dcmple)
+ABISYM(__aeabi_dcmplt)
+ABISYM(__aeabi_dcmpun)
+ABISYM(__aeabi_ddiv)
+ABISYM(__aeabi_dmul)
+ABISYM(__aeabi_dsub)
+ABISYM(__aeabi_f2d)
+ABISYM(__aeabi_f2h)
+ABISYM(__aeabi_f2iz)
+ABISYM(__aeabi_f2lz)
+ABISYM(__aeabi_f2ulz)
+ABISYM(__aeabi_fadd)
+ABISYM(__aeabi_fcmpeq)
+ABISYM(__aeabi_fcmpge)
+ABISYM(__aeabi_fcmpgt)
+ABISYM(__aeabi_fcmple)
+ABISYM(__aeabi_fcmplt)
+ABISYM(__aeabi_fcmpun)
+ABISYM(__aeabi_fdiv)
+ABISYM(__aeabi_fmul)
+ABISYM(__aeabi_fsub)
+ABISYM(__aeabi_h2f)
+ABISYM(__aeabi_i2d)
+ABISYM(__aeabi_i2f)
+ABISYM(__aeabi_idiv)
+ABISYM(__aeabi_idivmod)
+ABISYM(__aeabi_l2d)
+ABISYM(__aeabi_l2f)
+ABISYM(__aeabi_lasr)
+ABISYM(__aeabi_lcmp)
+ABISYM(__aeabi_ldivmod)
+ABISYM(__aeabi_llsl)
+ABISYM(__aeabi_llsr)
+ABISYM(__aeabi_lmul)
+ABISYM(__aeabi_memclr)
+ABISYM(__aeabi_memclr4)
+ABISYM(__aeabi_memclr8)
+ABISYM(__aeabi_memcmp)
+ABISYM(__aeabi_memcmp4)
+ABISYM(__aeabi_memcmp8)
+ABISYM(__aeabi_memcpy)
+ABISYM(__aeabi_memcpy4)
+ABISYM(__aeabi_memcpy8)
+ABISYM(__aeabi_memmove)
+ABISYM(__aeabi_memmove4)
+ABISYM(__aeabi_memmove8)
+ABISYM(__aeabi_memset)
+ABISYM(__aeabi_memset4)
+ABISYM(__aeabi_memset8)
+ABISYM(__aeabi_read_tp)
+ABISYM(__aeabi_ui2d)
+ABISYM(__aeabi_ui2f)
+ABISYM(__aeabi_uidiv)
+ABISYM(__aeabi_uidivmod)
+ABISYM(__aeabi_ul2d)
+ABISYM(__aeabi_ul2f)
+ABISYM(__aeabi_ulcmp)
+ABISYM(__aeabi_uldivmod)
+
+/* end */
diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
index 82c4afb701..b450e70c37 100644
--- a/src/backend/jit/llvm/llvmjit.c
+++ b/src/backend/jit/llvm/llvmjit.c
@@ -606,6 +606,22 @@ llvm_compile_module(LLVMJitContext *context)
              errhidecontext(true)));
 }
 
+/*
+ * Handle any ABI issues.
+ *
+ * Add more entries here for other archs as needed.
+ */
+static void
+llvm_load_abi(void)
+{
+#if (defined(__arm__) || defined(__arm)) && \
+    !(defined(__aarch64__) || defined(__aarch64))
+#define ABISYM(s) { extern void s(void); LLVMAddSymbol(#s, &s); }
+#include "abi_funcs_arm.h"
+#undef ABISYM
+#endif
+}
+
 /*
  * Per session initialization.
  */
@@ -664,6 +680,9 @@ llvm_session_initialize(void)
     LLVMDisposeMessage(features);
     features = NULL;
 
+    /* force load any ABI symbols needed by the platform */
+    llvm_load_abi();
+
     /* force symbols in main binary to be loaded */
     LLVMLoadLibraryPermanently(NULL);


pgsql-hackers by date:

Previous
From: Chapman Flack
Date:
Subject: Re: proposal: variadic argument support for least, greatest function
Next
From: Chapman Flack
Date:
Subject: Re: proposal: variadic argument support for least, greatest function