Skip to content

Commit d913eef

Browse files
committed
Remove dependency on libatomic on Linux
Due to some unfortunate interplay between clang and libstdc++, clang was not able to correctly identify to alignment of PoolRange and SideTableRefCountBits, causing it to emit library calls instead of inlining atomic operations. This was fixed by adding the appropriate alignment to those types. In addition to that the march for the Linux target was set to 'core2', which is the earliest architecture to support cx16, which is necessary for the atomic operations on PoolRange.
1 parent de9ffcb commit d913eef

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ function(_add_variant_c_compile_flags)
349349
list(APPEND result "-D__ANDROID_API__=${SWIFT_ANDROID_API_LEVEL}")
350350
endif()
351351

352+
if("${CFLAGS_SDK}" STREQUAL "LINUX")
353+
# this is the minimum architecture that supports 16 byte CAS, which is necessary to avoid a dependency to libatomic
354+
list(APPEND result "-march=core2")
355+
endif()
356+
352357
set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
353358
endfunction()
354359

@@ -466,7 +471,7 @@ function(_add_variant_link_flags)
466471
RESULT_VAR_NAME result
467472
MACCATALYST_BUILD_FLAVOR "${LFLAGS_MACCATALYST_BUILD_FLAVOR}")
468473
if("${LFLAGS_SDK}" STREQUAL "LINUX")
469-
list(APPEND link_libraries "pthread" "atomic" "dl")
474+
list(APPEND link_libraries "pthread" "dl")
470475
elseif("${LFLAGS_SDK}" STREQUAL "FREEBSD")
471476
list(APPEND link_libraries "pthread")
472477
elseif("${LFLAGS_SDK}" STREQUAL "CYGWIN")

stdlib/public/SwiftShims/RefCount.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ class RefCountBitsT {
631631

632632
typedef RefCountBitsT<RefCountIsInline> InlineRefCountBits;
633633

634-
class SideTableRefCountBits : public RefCountBitsT<RefCountNotInline>
634+
class alignas(sizeof(void*) * 2) SideTableRefCountBits : public RefCountBitsT<RefCountNotInline>
635635
{
636636
uint32_t weakBits;
637637

stdlib/public/runtime/Metadata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5115,7 +5115,7 @@ void swift::checkMetadataDependencyCycle(const Metadata *startMetadata,
51155115
/***************************************************************************/
51165116

51175117
namespace {
5118-
struct PoolRange {
5118+
struct alignas(sizeof(uintptr_t) * 2) PoolRange {
51195119
static constexpr uintptr_t PageSize = 16 * 1024;
51205120
static constexpr uintptr_t MaxPoolAllocationSize = PageSize / 2;
51215121

0 commit comments

Comments
 (0)