diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake --- a/libc/cmake/modules/LLVMLibCTestRules.cmake +++ b/libc/cmake/modules/LLVMLibCTestRules.cmake @@ -551,7 +551,8 @@ -mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto --target=${LIBC_GPU_TARGET_TRIPLE}) elseif(LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX) get_nvptx_compile_options(nvptx_options ${LIBC_GPU_TARGET_ARCHITECTURE}) - list(APPEND ${nvptx_options} --target=${LIBC_GPU_TARGET_TRIPLE}) + list(APPEND LIBC_HERMETIC_TEST_COMPILE_OPTIONS + ${nvptx_options} -fno-use-cxa-atexit --target=${LIBC_GPU_TARGET_TRIPLE}) endif() # Rule to add a hermetic test. A hermetic test is one whose executable is fully diff --git a/libc/test/CMakeLists.txt b/libc/test/CMakeLists.txt --- a/libc/test/CMakeLists.txt +++ b/libc/test/CMakeLists.txt @@ -22,10 +22,8 @@ return() endif() -if(NOT LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX) - add_subdirectory(src) - add_subdirectory(utils) -endif() +add_subdirectory(src) +add_subdirectory(utils) if(LLVM_LIBC_FULL_BUILD AND NOT LIBC_TARGET_OS_IS_BAREMETAL) add_subdirectory(IntegrationTest) diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt --- a/libc/test/UnitTest/CMakeLists.txt +++ b/libc/test/UnitTest/CMakeLists.txt @@ -13,8 +13,16 @@ LibcDeathTestExecutors.cpp ExecuteFunctionUnix.cpp) endif() +# The Nvidia 'nvlink' linker does not support static libraries. +if(LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX) + set(library_type OBJECT) +else() + set(library_type STATIC) +endif() + add_library( LibcUnitTest + ${library_type} EXCLUDE_FROM_ALL ${libc_test_srcs_common} ${libc_death_test_srcs} @@ -22,6 +30,7 @@ add_library( LibcHermeticTest + ${library_type} EXCLUDE_FROM_ALL ${libc_test_srcs_common} HermeticTestUtils.cpp @@ -29,6 +38,7 @@ add_library( LibcUnitTestMain + ${library_type} EXCLUDE_FROM_ALL LibcTestMain.cpp ) @@ -36,6 +46,7 @@ add_library( LibcHermeticTestMain + ${library_type} EXCLUDE_FROM_ALL LibcTestMain.cpp ) @@ -97,6 +108,7 @@ add_library( LibcMemoryHelpers + ${library_type} MemoryMatcher.h MemoryMatcher.cpp ) diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp --- a/libc/test/UnitTest/HermeticTestUtils.cpp +++ b/libc/test/UnitTest/HermeticTestUtils.cpp @@ -17,6 +17,7 @@ void *memcpy(void *__restrict, const void *__restrict, size_t); void *memmove(void *dst, const void *src, size_t count); void *memset(void *ptr, int value, size_t count); +int atexit(void (*func)(void)); } // namespace __llvm_libc @@ -57,6 +58,9 @@ return __llvm_libc::memset(ptr, value, count); } +// This is needed if the test was compiled with '-fno-use-cxa-atexit'. +int atexit(void (*func)(void)) { return __llvm_libc::atexit(func); } + void *malloc(size_t s) { void *mem = ptr; ptr += s; @@ -78,7 +82,7 @@ __builtin_trap(); } -// Integration tests are linked with -nostdlib. BFD linker expects +// Hermetic tests are linked with -nostdlib. BFD linker expects // __dso_handle when -nostdlib is used. void *__dso_handle = nullptr; diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt --- a/libc/test/src/__support/CMakeLists.txt +++ b/libc/test/src/__support/CMakeLists.txt @@ -1,14 +1,17 @@ add_custom_target(libc-support-tests) -add_libc_test( - blockstore_test - SUITE - libc-support-tests - SRCS - blockstore_test.cpp - DEPENDS - libc.src.__support.blockstore -) +# This test fails with a misaigned address on NVPTX. +if(NOT LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX) + add_libc_test( + blockstore_test + SUITE + libc-support-tests + SRCS + blockstore_test.cpp + DEPENDS + libc.src.__support.blockstore + ) +endif() add_libc_test( endian_test @@ -20,16 +23,19 @@ libc.src.__support.common ) -add_libc_test( - high_precision_decimal_test - SUITE - libc-support-tests - SRCS - high_precision_decimal_test.cpp - DEPENDS - libc.src.__support.high_precision_decimal - libc.src.__support.uint128 -) +# This test fails with a segmentation fault on NVPTX. +if(NOT LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX) + add_libc_test( + high_precision_decimal_test + SUITE + libc-support-tests + SRCS + high_precision_decimal_test.cpp + DEPENDS + libc.src.__support.high_precision_decimal + libc.src.__support.uint128 + ) +endif() add_libc_test( str_to_float_test diff --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt --- a/libc/test/src/__support/CPP/CMakeLists.txt +++ b/libc/test/src/__support/CPP/CMakeLists.txt @@ -61,16 +61,19 @@ libc.src.__support.CPP.atomic ) -add_libc_test( - stringstream_test - SUITE - libc-cpp-utils-tests - SRCS - stringstream_test.cpp - DEPENDS - libc.src.__support.CPP.span - libc.src.__support.CPP.stringstream -) +# This test fails with a segmentation fault on NVPTX. +if(NOT LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX) + add_libc_test( + stringstream_test + SUITE + libc-cpp-utils-tests + SRCS + stringstream_test.cpp + DEPENDS + libc.src.__support.CPP.span + libc.src.__support.CPP.stringstream + ) +endif() add_libc_test( optional_test diff --git a/libc/test/src/__support/CPP/atomic_test.cpp b/libc/test/src/__support/CPP/atomic_test.cpp --- a/libc/test/src/__support/CPP/atomic_test.cpp +++ b/libc/test/src/__support/CPP/atomic_test.cpp @@ -14,21 +14,21 @@ TEST(LlvmLibcAtomicTest, LoadStore) { __llvm_libc::cpp::Atomic aint(123); - ASSERT_EQ(aint.load(), 123); + ASSERT_EQ(aint.load(__llvm_libc::cpp::MemoryOrder::RELAXED), 123); - aint.store(100); - ASSERT_EQ(aint.load(), 100); + aint.store(100, __llvm_libc::cpp::MemoryOrder::RELAXED); + ASSERT_EQ(aint.load(__llvm_libc::cpp::MemoryOrder::RELAXED), 100); aint = 1234; // Equivalent of store - ASSERT_EQ(aint.load(), 1234); + ASSERT_EQ(aint.load(__llvm_libc::cpp::MemoryOrder::RELAXED), 1234); } TEST(LlvmLibcAtomicTest, CompareExchangeStrong) { int desired = 123; __llvm_libc::cpp::Atomic aint(desired); ASSERT_TRUE(aint.compare_exchange_strong(desired, 100)); - ASSERT_EQ(aint.load(), 100); + ASSERT_EQ(aint.load(__llvm_libc::cpp::MemoryOrder::RELAXED), 100); ASSERT_FALSE(aint.compare_exchange_strong(desired, 100)); - ASSERT_EQ(aint.load(), 100); + ASSERT_EQ(aint.load(__llvm_libc::cpp::MemoryOrder::RELAXED), 100); } diff --git a/libc/test/utils/UnitTest/CMakeLists.txt b/libc/test/utils/UnitTest/CMakeLists.txt --- a/libc/test/utils/UnitTest/CMakeLists.txt +++ b/libc/test/utils/UnitTest/CMakeLists.txt @@ -1,3 +1,7 @@ +if(LIBC_TARGET_ARCHITECTURE_IS_GPU) + return() +endif() + add_custom_target(libc_unittest_tests) add_libc_unittest(