AK: Work around Apple Clang __builtin_subc codegen issue

Apple Clang 14.0.3 (Xcode 14.3) miscompiles this builtin on AArch64,
causing the borrow flag to be set incorrectly. I have added a detailed
writeup on Qemu's issue tracker, where the same issue led to a hang when
emulating x86:

https://gitlab.com/qemu-project/qemu/-/issues/1659#note_1408275831

I don't know of any specific issue caused by this on Lagom, but better
safe than sorry.
This commit is contained in:
Daniel Bertalan 2023-06-23 09:18:29 +00:00 committed by Andreas Kling
parent 7dadc9ff33
commit 9d4dfc1061
2 changed files with 8 additions and 1 deletions

View file

@ -196,7 +196,7 @@ ALWAYS_INLINE constexpr NativeWord add_words(NativeWord word1, NativeWord word2,
ALWAYS_INLINE constexpr NativeWord sub_words(NativeWord word1, NativeWord word2, bool& carry)
{
if (!is_constant_evaluated()) {
#if __has_builtin(__builtin_subc)
#if __has_builtin(__builtin_subc) && !defined(AK_BUILTIN_SUBC_BROKEN)
NativeWord ncarry, output;
if constexpr (SameAs<NativeWord, unsigned int>)
output = __builtin_subc(word1, word2, carry, reinterpret_cast<unsigned int*>(&ncarry));

View file

@ -117,6 +117,13 @@
# define AK_HAS_CONDITIONALLY_TRIVIAL
#endif
// Apple Clang 14.0.3 (shipped in Xcode 14.3) has a bug that causes __builtin_subc{,l,ll}
// to incorrectly return whether a borrow occurred on AArch64. See our writeup for the Qemu
// issue also caused by it: https://gitlab.com/qemu-project/qemu/-/issues/1659#note_1408275831
#if ARCH(AARCH64) && defined(__apple_build_version__) && __clang_major__ == 14
# define AK_BUILTIN_SUBC_BROKEN
#endif
#ifdef ALWAYS_INLINE
# undef ALWAYS_INLINE
#endif