Skip to content

Commit 17472cc

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 a543bfb commit 17472cc

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,13 @@ function(_add_variant_c_compile_flags)
363363
endif()
364364
endif()
365365

366+
if("${CFLAGS_SDK}" STREQUAL "LINUX")
367+
if(${CFLAGS_ARCH} STREQUAL x86_64)
368+
# this is the minimum architecture that supports 16 byte CAS, which is necessary to avoid a dependency to libatomic
369+
list(APPEND result "-march=core2")
370+
endif()
371+
endif()
372+
366373
set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
367374
endfunction()
368375

@@ -460,7 +467,7 @@ function(_add_variant_link_flags)
460467
RESULT_VAR_NAME result)
461468

462469
if("${LFLAGS_SDK}" STREQUAL "LINUX")
463-
list(APPEND link_libraries "pthread" "atomic" "dl")
470+
list(APPEND link_libraries "pthread" "dl")
464471
elseif("${LFLAGS_SDK}" STREQUAL "FREEBSD")
465472
list(APPEND link_libraries "pthread")
466473
elseif("${LFLAGS_SDK}" STREQUAL "CYGWIN")

cmake/modules/AddSwiftUnittests.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ function(add_swift_unittest test_dirname)
4848
"${android_system_libs}")
4949
set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_LIBRARIES "log")
5050
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
51-
set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_LIBRARIES
52-
"atomic")
51+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
52+
target_compile_options(${test_dirname} PRIVATE
53+
-march=core2)
54+
endif()
5355
endif()
5456

5557
find_program(LDLLD_PATH "ld.lld")

stdlib/public/SwiftShims/RefCount.h

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

590590
typedef RefCountBitsT<RefCountIsInline> InlineRefCountBits;
591591

592-
class SideTableRefCountBits : public RefCountBitsT<RefCountNotInline>
592+
class alignas(sizeof(void*) * 2) SideTableRefCountBits : public RefCountBitsT<RefCountNotInline>
593593
{
594594
uint32_t weakBits;
595595

stdlib/public/runtime/Metadata.cpp

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

50565056
namespace {
5057-
struct PoolRange {
5057+
struct alignas(sizeof(uintptr_t) * 2) PoolRange {
50585058
static constexpr uintptr_t PageSize = 16 * 1024;
50595059
static constexpr uintptr_t MaxPoolAllocationSize = PageSize / 2;
50605060

0 commit comments

Comments
 (0)