Skip to content

Commit 8aefd8b

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 acf94a0 commit 8aefd8b

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
@@ -7286,6 +7286,111 @@ if test x"$pgac_cv_prog_CLANGXX_cxxflags__Xclang__no_opaque_pointers" = x"yes";
72867286
fi
72877287

72887288

7289+
# Ideally bitcode should perhaps match $CC's use, or not, of outline atomic
7290+
# functions, but for now we err on the side of suppressing them in bitcode,
7291+
# because we can't assume they're available at runtime. This affects aarch64
7292+
# builds using the basic armv8-a ISA without LSE support.
7293+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CLANG} supports -mno-outline-atomics, for BITCODE_CFLAGS" >&5
7294+
$as_echo_n "checking whether ${CLANG} supports -mno-outline-atomics, for BITCODE_CFLAGS... " >&6; }
7295+
if ${pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics+:} false; then :
7296+
$as_echo_n "(cached) " >&6
7297+
else
7298+
pgac_save_CXXFLAGS=$CXXFLAGS
7299+
pgac_save_CXX=$CXX
7300+
CXX=${CLANG}
7301+
CXXFLAGS="${BITCODE_CFLAGS} -mno-outline-atomics"
7302+
ac_save_cxx_werror_flag=$ac_cxx_werror_flag
7303+
ac_cxx_werror_flag=yes
7304+
ac_ext=cpp
7305+
ac_cpp='$CXXCPP $CPPFLAGS'
7306+
ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
7307+
ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
7308+
ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
7309+
7310+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
7311+
/* end confdefs.h. */
7312+
7313+
int
7314+
main ()
7315+
{
7316+
7317+
;
7318+
return 0;
7319+
}
7320+
_ACEOF
7321+
if ac_fn_cxx_try_compile "$LINENO"; then :
7322+
pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics=yes
7323+
else
7324+
pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics=no
7325+
fi
7326+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
7327+
ac_ext=c
7328+
ac_cpp='$CPP $CPPFLAGS'
7329+
ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
7330+
ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
7331+
ac_compiler_gnu=$ac_cv_c_compiler_gnu
7332+
7333+
ac_cxx_werror_flag=$ac_save_cxx_werror_flag
7334+
CXXFLAGS="$pgac_save_CXXFLAGS"
7335+
CXX="$pgac_save_CXX"
7336+
fi
7337+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" >&5
7338+
$as_echo "$pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" >&6; }
7339+
if test x"$pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" = x"yes"; then
7340+
BITCODE_CFLAGS="${BITCODE_CFLAGS} -mno-outline-atomics"
7341+
fi
7342+
7343+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CLANG} supports -mno-outline-atomics, for BITCODE_CXXFLAGS" >&5
7344+
$as_echo_n "checking whether ${CLANG} supports -mno-outline-atomics, for BITCODE_CXXFLAGS... " >&6; }
7345+
if ${pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics+:} false; then :
7346+
$as_echo_n "(cached) " >&6
7347+
else
7348+
pgac_save_CXXFLAGS=$CXXFLAGS
7349+
pgac_save_CXX=$CXX
7350+
CXX=${CLANG}
7351+
CXXFLAGS="${BITCODE_CXXFLAGS} -mno-outline-atomics"
7352+
ac_save_cxx_werror_flag=$ac_cxx_werror_flag
7353+
ac_cxx_werror_flag=yes
7354+
ac_ext=cpp
7355+
ac_cpp='$CXXCPP $CPPFLAGS'
7356+
ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
7357+
ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
7358+
ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
7359+
7360+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
7361+
/* end confdefs.h. */
7362+
7363+
int
7364+
main ()
7365+
{
7366+
7367+
;
7368+
return 0;
7369+
}
7370+
_ACEOF
7371+
if ac_fn_cxx_try_compile "$LINENO"; then :
7372+
pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics=yes
7373+
else
7374+
pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics=no
7375+
fi
7376+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
7377+
ac_ext=c
7378+
ac_cpp='$CPP $CPPFLAGS'
7379+
ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
7380+
ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
7381+
ac_compiler_gnu=$ac_cv_c_compiler_gnu
7382+
7383+
ac_cxx_werror_flag=$ac_save_cxx_werror_flag
7384+
CXXFLAGS="$pgac_save_CXXFLAGS"
7385+
CXX="$pgac_save_CXX"
7386+
fi
7387+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" >&5
7388+
$as_echo "$pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" >&6; }
7389+
if test x"$pgac_cv_prog_CLANG_cxxflags__mno_outline_atomics" = x"yes"; then
7390+
BITCODE_CXXFLAGS="${BITCODE_CXXFLAGS} -mno-outline-atomics"
7391+
fi
7392+
7393+
72897394
NOT_THE_CFLAGS=""
72907395
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CLANG} supports -Wunused-command-line-argument, for NOT_THE_CFLAGS" >&5
72917396
$as_echo_n "checking whether ${CLANG} supports -Wunused-command-line-argument, for NOT_THE_CFLAGS... " >&6; }

configure.in

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

604+
# Ideally bitcode should perhaps match $CC's use, or not, of outline atomic
605+
# functions, but for now we err on the side of suppressing them in bitcode,
606+
# because we can't assume they're available at runtime. This affects aarch64
607+
# builds using the basic armv8-a ISA without LSE support.
608+
PGAC_PROG_VARCXX_VARFLAGS_OPT(CLANG, BITCODE_CFLAGS, [-mno-outline-atomics])
609+
PGAC_PROG_VARCXX_VARFLAGS_OPT(CLANG, BITCODE_CXXFLAGS, [-mno-outline-atomics])
610+
604611
NOT_THE_CFLAGS=""
605612
PGAC_PROG_VARCC_VARFLAGS_OPT(CLANG, NOT_THE_CFLAGS, [-Wunused-command-line-argument])
606613
if test -n "$NOT_THE_CFLAGS"; then

0 commit comments

Comments
 (0)