From d913eefcc93f8c80d6d1a6de4ea898a2838d8b6f Mon Sep 17 00:00:00 2001 From: Dario Rexin Date: Fri, 31 Jan 2020 15:59:40 -0800 Subject: [PATCH 1/8] 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. --- cmake/modules/AddSwift.cmake | 7 ++++++- stdlib/public/SwiftShims/RefCount.h | 2 +- stdlib/public/runtime/Metadata.cpp | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index a89442f485d85..81d62031ea970 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -349,6 +349,11 @@ function(_add_variant_c_compile_flags) list(APPEND result "-D__ANDROID_API__=${SWIFT_ANDROID_API_LEVEL}") endif() + if("${CFLAGS_SDK}" STREQUAL "LINUX") + # this is the minimum architecture that supports 16 byte CAS, which is necessary to avoid a dependency to libatomic + list(APPEND result "-march=core2") + endif() + set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE) endfunction() @@ -466,7 +471,7 @@ function(_add_variant_link_flags) RESULT_VAR_NAME result MACCATALYST_BUILD_FLAVOR "${LFLAGS_MACCATALYST_BUILD_FLAVOR}") if("${LFLAGS_SDK}" STREQUAL "LINUX") - list(APPEND link_libraries "pthread" "atomic" "dl") + list(APPEND link_libraries "pthread" "dl") elseif("${LFLAGS_SDK}" STREQUAL "FREEBSD") list(APPEND link_libraries "pthread") elseif("${LFLAGS_SDK}" STREQUAL "CYGWIN") diff --git a/stdlib/public/SwiftShims/RefCount.h b/stdlib/public/SwiftShims/RefCount.h index 1de032c56f49b..c4fee4c22dab0 100644 --- a/stdlib/public/SwiftShims/RefCount.h +++ b/stdlib/public/SwiftShims/RefCount.h @@ -631,7 +631,7 @@ class RefCountBitsT { typedef RefCountBitsT InlineRefCountBits; -class SideTableRefCountBits : public RefCountBitsT +class alignas(sizeof(void*) * 2) SideTableRefCountBits : public RefCountBitsT { uint32_t weakBits; diff --git a/stdlib/public/runtime/Metadata.cpp b/stdlib/public/runtime/Metadata.cpp index 2546d90dec5cd..687dd83dbb7e4 100644 --- a/stdlib/public/runtime/Metadata.cpp +++ b/stdlib/public/runtime/Metadata.cpp @@ -5115,7 +5115,7 @@ void swift::checkMetadataDependencyCycle(const Metadata *startMetadata, /***************************************************************************/ namespace { - struct PoolRange { + struct alignas(sizeof(uintptr_t) * 2) PoolRange { static constexpr uintptr_t PageSize = 16 * 1024; static constexpr uintptr_t MaxPoolAllocationSize = PageSize / 2; From 6db05fdbca83f8d3cacdea94531c643bff94716c Mon Sep 17 00:00:00 2001 From: Dario Rexin Date: Sat, 1 Feb 2020 23:25:31 -0800 Subject: [PATCH 2/8] Set march for unittest on Linux --- cmake/modules/AddSwiftUnittests.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/modules/AddSwiftUnittests.cmake b/cmake/modules/AddSwiftUnittests.cmake index 5da0ae3ac44e3..2c46a289c4960 100644 --- a/cmake/modules/AddSwiftUnittests.cmake +++ b/cmake/modules/AddSwiftUnittests.cmake @@ -48,8 +48,8 @@ function(add_swift_unittest test_dirname) "${android_system_libs}") set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_LIBRARIES "log") elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") - set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_LIBRARIES - "atomic") + set_property(TARGET "${test_dirname}" APPEND PROPERTY COMPILE_FLAGS + "-march=core2") endif() find_program(LDLLD_PATH "ld.lld") From 34a331eb4d3d298066fd5f16ca66fd5cefb7f71a Mon Sep 17 00:00:00 2001 From: Dario Rexin Date: Sun, 2 Feb 2020 00:29:09 -0800 Subject: [PATCH 3/8] Fix march flag in unittest --- cmake/modules/AddSwiftUnittests.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/modules/AddSwiftUnittests.cmake b/cmake/modules/AddSwiftUnittests.cmake index 2c46a289c4960..773d603d80afa 100644 --- a/cmake/modules/AddSwiftUnittests.cmake +++ b/cmake/modules/AddSwiftUnittests.cmake @@ -49,7 +49,7 @@ function(add_swift_unittest test_dirname) set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_LIBRARIES "log") elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") set_property(TARGET "${test_dirname}" APPEND PROPERTY COMPILE_FLAGS - "-march=core2") + "-Xcc -march=core2") endif() find_program(LDLLD_PATH "ld.lld") From feb7bb1a0654384add15de1ba4005910c630f474 Mon Sep 17 00:00:00 2001 From: Dario Rexin Date: Sun, 2 Feb 2020 09:53:18 -0800 Subject: [PATCH 4/8] one more try --- cmake/modules/AddSwiftUnittests.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmake/modules/AddSwiftUnittests.cmake b/cmake/modules/AddSwiftUnittests.cmake index 773d603d80afa..6de6909f1e1e7 100644 --- a/cmake/modules/AddSwiftUnittests.cmake +++ b/cmake/modules/AddSwiftUnittests.cmake @@ -49,7 +49,9 @@ function(add_swift_unittest test_dirname) set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_LIBRARIES "log") elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") set_property(TARGET "${test_dirname}" APPEND PROPERTY COMPILE_FLAGS - "-Xcc -march=core2") + " -march=core2") + set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_FLAGS + " -march=core2") endif() find_program(LDLLD_PATH "ld.lld") From 525f408445181d58e0d4b63a6b114a88ff1d9f98 Mon Sep 17 00:00:00 2001 From: Dario Rexin Date: Mon, 3 Feb 2020 11:28:22 -0800 Subject: [PATCH 5/8] Update AddSwiftUnittests.cmake --- cmake/modules/AddSwiftUnittests.cmake | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmake/modules/AddSwiftUnittests.cmake b/cmake/modules/AddSwiftUnittests.cmake index 6de6909f1e1e7..a939730f65172 100644 --- a/cmake/modules/AddSwiftUnittests.cmake +++ b/cmake/modules/AddSwiftUnittests.cmake @@ -50,8 +50,6 @@ function(add_swift_unittest test_dirname) elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") set_property(TARGET "${test_dirname}" APPEND PROPERTY COMPILE_FLAGS " -march=core2") - set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_FLAGS - " -march=core2") endif() find_program(LDLLD_PATH "ld.lld") From ddd003fd4d20e9954ec7ae7a54006d456942d7a1 Mon Sep 17 00:00:00 2001 From: Dario Rexin Date: Mon, 3 Feb 2020 13:44:27 -0800 Subject: [PATCH 6/8] Update AddSwiftUnittests.cmake --- cmake/modules/AddSwiftUnittests.cmake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmake/modules/AddSwiftUnittests.cmake b/cmake/modules/AddSwiftUnittests.cmake index a939730f65172..6d506c9d91df7 100644 --- a/cmake/modules/AddSwiftUnittests.cmake +++ b/cmake/modules/AddSwiftUnittests.cmake @@ -48,8 +48,10 @@ function(add_swift_unittest test_dirname) "${android_system_libs}") set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_LIBRARIES "log") elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") - set_property(TARGET "${test_dirname}" APPEND PROPERTY COMPILE_FLAGS - " -march=core2") + if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") + set_property(TARGET "${test_dirname}" APPEND_STRING PROPERTY COMPILE_FLAGS + " -march=core2") + endif() endif() find_program(LDLLD_PATH "ld.lld") From 62a5ebb6a2927c0976a00211470c6ac4c869fc86 Mon Sep 17 00:00:00 2001 From: Dario Rexin Date: Mon, 3 Feb 2020 13:45:47 -0800 Subject: [PATCH 7/8] Update AddSwift.cmake --- cmake/modules/AddSwift.cmake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 81d62031ea970..dee7278559026 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -350,8 +350,10 @@ function(_add_variant_c_compile_flags) endif() if("${CFLAGS_SDK}" STREQUAL "LINUX") - # this is the minimum architecture that supports 16 byte CAS, which is necessary to avoid a dependency to libatomic - list(APPEND result "-march=core2") + if(${CFLAGS_ARCH} STREQUAL x86_64) + # this is the minimum architecture that supports 16 byte CAS, which is necessary to avoid a dependency to libatomic + list(APPEND result "-march=core2") + endif() endif() set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE) From 29749e64d76e8ba606eb0e4d4676de7a71a35903 Mon Sep 17 00:00:00 2001 From: Dario Rexin Date: Wed, 5 Feb 2020 08:51:00 -0800 Subject: [PATCH 8/8] Update AddSwiftUnittests.cmake --- cmake/modules/AddSwiftUnittests.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/modules/AddSwiftUnittests.cmake b/cmake/modules/AddSwiftUnittests.cmake index 6d506c9d91df7..3e53e6696e444 100644 --- a/cmake/modules/AddSwiftUnittests.cmake +++ b/cmake/modules/AddSwiftUnittests.cmake @@ -49,8 +49,8 @@ function(add_swift_unittest test_dirname) set_property(TARGET "${test_dirname}" APPEND PROPERTY LINK_LIBRARIES "log") elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") - set_property(TARGET "${test_dirname}" APPEND_STRING PROPERTY COMPILE_FLAGS - " -march=core2") + target_compile_options(${test_dirname} PRIVATE + -march=core2) endif() endif()