Skip to content

Commit 7ca388f

Browse files
committed
jit: Use -mno-outline-atomics for bitcode on ARM.
If the executable's .o files were produced by a compiler (probably gcc) not using -moutline-atomics, and the corresponding .bc files were produced by clang using -moutline-atomics (probably by default), then the generated bitcode functions would have the target attribute "+outline-atomics", and could fail at runtime when inlined. If the target ISA at bitcode generation time was armv8-a (the most conservative aarch64 target, no LSE), then LLVM IR atomic instructions would generate calls to functions in libgcc.a or libclang_rt.*.a that switch between LL/SC and faster LSE instructions depending on a runtime AT_HWCAP check. Since the corresponding .o files didn't need those functions, they wouldn't have been included in the executable, and resolution would fail. At least Debian and Ubuntu are known to ship gcc and clang compilers that target armv8-a but differ on the use of outline atomics by default. Fix, by suppressing the outline atomics attribute in bitcode explicitly. Inline LL/SC instructions will be generated for atomic operations in bitcode built for armv8-a. Only configure scripts are adjusted for now, because the meson build system doesn't generate bitcode yet. This doesn't seem to be a new phenomenon, so real cases of functions using atomics that are inlined by JIT must be rare in the wild given how long it took for a bug report to arrive. The reported case could be reduced to: postgres=# set jit_inline_above_cost = 0; SET postgres=# set jit_above_cost = 0; SET postgres=# select pg_last_wal_receive_lsn(); WARNING: failed to resolve name __aarch64_swp4_acq_rel FATAL: fatal llvm error: Program used external function '__aarch64_swp4_acq_rel' which could not be resolved! The change doesn't affect non-ARM systems or later target ISAs. Back-patch to all supported releases. Reported-by: Alexander Kozhemyakin <a.kozhemyakin@postgrespro.ru> Discussion: https://postgr.es/m/18610-37bf303f904fede3%40postgresql.org
1 parent 1c5fe56 commit 7ca388f

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

configure

+105
Original file line numberDiff line numberDiff line change
@@ -7394,6 +7394,111 @@ if test x"$pgac_cv_prog_CLANGXX_cxxflags__Xclang__no_opaque_pointers" = x"yes";
73947394
fi
73957395

73967396

7397+
# Ideally bitcode should perhaps match $CC's use, or not, of outline atomic
7398+
# functions, but for now we err on the side of suppressing them in bitcode,
7399+
# because we can't assume they're available at runtime. This affects aarch64
7400+
# builds using the basic armv8-a ISA without LSE support.
7401+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CLANG} supports -mno-outline-atomics, for BITCODE_CFLAGS" >&5
7402+
$as_echo_n "checking whether ${CLANG} supports -mno-outline-atomics, for BITCODE_CFLAGS... " >&6; }
7403+
if ${pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics+:} false; then :
7404+
$as_echo_n "(cached) " >&6
7405+
else
7406+
pgac_save_CXXFLAGS=$CXXFLAGS
7407+
pgac_save_CXX=$CXX
7408+
CXX=${CLANG}
7409+
CXXFLAGS="${BITCODE_CFLAGS} -mno-outline-atomics"
7410+
ac_save_cxx_werror_flag=$ac_cxx_werror_flag
7411+
ac_cxx_werror_flag=yes
7412+
ac_ext=cpp
7413+
ac_cpp='$CXXCPP $CPPFLAGS'
7414+
ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
7415+
ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
7416+
ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
7417+
7418+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
7419+
/* end confdefs.h. */
7420+
7421+
int
7422+
main ()
7423+
{
7424+
7425+
;
7426+
return 0;
7427+
}
7428+
_ACEOF
7429+
if ac_fn_cxx_try_compile "$LINENO"; then :
7430+
pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics=yes
7431+
else
7432+
pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics=no
7433+
fi
7434+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
7435+
ac_ext=c
7436+
ac_cpp='$CPP $CPPFLAGS'
7437+
ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
7438+
ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
7439+
ac_compiler_gnu=$ac_cv_c_compiler_gnu
7440+
7441+
ac_cxx_werror_flag=$ac_save_cxx_werror_flag
7442+
CXXFLAGS="$pgac_save_CXXFLAGS"
7443+
CXX="$pgac_save_CXX"
7444+
fi
7445+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" >&5
7446+
$as_echo "$pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" >&6; }
7447+
if test x"$pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" = x"yes"; then
7448+
BITCODE_CFLAGS="${BITCODE_CFLAGS} -mno-outline-atomics"
7449+
fi
7450+
7451+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CLANG} supports -mno-outline-atomics, for BITCODE_CXXFLAGS" >&5
7452+
$as_echo_n "checking whether ${CLANG} supports -mno-outline-atomics, for BITCODE_CXXFLAGS... " >&6; }
7453+
if ${pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics+:} false; then :
7454+
$as_echo_n "(cached) " >&6
7455+
else
7456+
pgac_save_CXXFLAGS=$CXXFLAGS
7457+
pgac_save_CXX=$CXX
7458+
CXX=${CLANG}
7459+
CXXFLAGS="${BITCODE_CXXFLAGS} -mno-outline-atomics"
7460+
ac_save_cxx_werror_flag=$ac_cxx_werror_flag
7461+
ac_cxx_werror_flag=yes
7462+
ac_ext=cpp
7463+
ac_cpp='$CXXCPP $CPPFLAGS'
7464+
ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
7465+
ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
7466+
ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
7467+
7468+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
7469+
/* end confdefs.h. */
7470+
7471+
int
7472+
main ()
7473+
{
7474+
7475+
;
7476+
return 0;
7477+
}
7478+
_ACEOF
7479+
if ac_fn_cxx_try_compile "$LINENO"; then :
7480+
pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics=yes
7481+
else
7482+
pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics=no
7483+
fi
7484+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
7485+
ac_ext=c
7486+
ac_cpp='$CPP $CPPFLAGS'
7487+
ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
7488+
ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
7489+
ac_compiler_gnu=$ac_cv_c_compiler_gnu
7490+
7491+
ac_cxx_werror_flag=$ac_save_cxx_werror_flag
7492+
CXXFLAGS="$pgac_save_CXXFLAGS"
7493+
CXX="$pgac_save_CXX"
7494+
fi
7495+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" >&5
7496+
$as_echo "$pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" >&6; }
7497+
if test x"$pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" = x"yes"; then
7498+
BITCODE_CXXFLAGS="${BITCODE_CXXFLAGS} -mno-outline-atomics"
7499+
fi
7500+
7501+
73977502
NOT_THE_CFLAGS=""
73987503
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CLANG} supports -Wunused-command-line-argument, for NOT_THE_CFLAGS" >&5
73997504
$as_echo_n "checking whether ${CLANG} supports -Wunused-command-line-argument, for NOT_THE_CFLAGS... " >&6; }

configure.ac

+7
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,13 @@ if test "$with_llvm" = yes ; then
609609
PGAC_PROG_VARCC_VARFLAGS_OPT(CLANG, BITCODE_CFLAGS, [-Xclang -no-opaque-pointers])
610610
PGAC_PROG_VARCXX_VARFLAGS_OPT(CLANGXX, BITCODE_CXXFLAGS, [-Xclang -no-opaque-pointers])
611611

612+
# Ideally bitcode should perhaps match $CC's use, or not, of outline atomic
613+
# functions, but for now we err on the side of suppressing them in bitcode,
614+
# because we can't assume they're available at runtime. This affects aarch64
615+
# builds using the basic armv8-a ISA without LSE support.
616+
PGAC_PROG_VARCXX_VARFLAGS_OPT(CLANG, BITCODE_CFLAGS, [-mno-outline-atomics])
617+
PGAC_PROG_VARCXX_VARFLAGS_OPT(CLANG, BITCODE_CXXFLAGS, [-mno-outline-atomics])
618+
612619
NOT_THE_CFLAGS=""
613620
PGAC_PROG_VARCC_VARFLAGS_OPT(CLANG, NOT_THE_CFLAGS, [-Wunused-command-line-argument])
614621
if test -n "$NOT_THE_CFLAGS"; then

0 commit comments

Comments
 (0)