diff options
author | Joseph Huber <jhuber6@vols.utk.edu> | 2023-04-28 04:33:44 -0500 |
---|---|---|
committer | Joseph Huber <jhuber6@vols.utk.edu> | 2023-05-04 08:36:29 -0500 |
commit | f4002c1415a70701d6603c293afbed4f094da4a4 (patch) | |
tree | 358ab675a34cc852271d57201cf97e03c3e56edb /libc/test | |
parent | 8167727b4a08799787a9596df369ee98a90f7725 (diff) | |
download | llvm-f4002c1415a70701d6603c293afbed4f094da4a4.tar.gz |
[libc] Enable running libc unit tests on NVPTX
The previous patches added the necessary support for global constructors
used to register tests. This patch enables the NVPTX target to build
and run the unit tests on the GPU. Currently this only tests the ctype
tests, but adding more should be straightforward from here on.
This ran all the ctest unit tests when run on an sm_70.
Depends on D149517 D149527
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D149532
Diffstat (limited to 'libc/test')
-rw-r--r-- | libc/test/CMakeLists.txt | 6 | ||||
-rw-r--r-- | libc/test/UnitTest/CMakeLists.txt | 12 | ||||
-rw-r--r-- | libc/test/UnitTest/HermeticTestUtils.cpp | 6 | ||||
-rw-r--r-- | libc/test/src/__support/CMakeLists.txt | 44 | ||||
-rw-r--r-- | libc/test/src/__support/CPP/CMakeLists.txt | 23 | ||||
-rw-r--r-- | libc/test/src/__support/CPP/atomic_test.cpp | 12 | ||||
-rw-r--r-- | libc/test/utils/UnitTest/CMakeLists.txt | 4 |
7 files changed, 67 insertions, 40 deletions
diff --git a/libc/test/CMakeLists.txt b/libc/test/CMakeLists.txt index 01baeaeabf57..027028de7164 100644 --- a/libc/test/CMakeLists.txt +++ b/libc/test/CMakeLists.txt @@ -22,10 +22,8 @@ if(LIBC_TARGET_ARCHITECTURE_IS_GPU AND 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 index d87ad8075af9..7a54d502b50c 100644 --- a/libc/test/UnitTest/CMakeLists.txt +++ b/libc/test/UnitTest/CMakeLists.txt @@ -13,8 +13,16 @@ if(${LIBC_TARGET_OS} STREQUAL "linux") 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( add_library( LibcHermeticTest + ${library_type} EXCLUDE_FROM_ALL ${libc_test_srcs_common} HermeticTestUtils.cpp @@ -29,6 +38,7 @@ add_library( add_library( LibcUnitTestMain + ${library_type} EXCLUDE_FROM_ALL LibcTestMain.cpp ) @@ -36,6 +46,7 @@ add_dependencies(LibcUnitTestMain LibcUnitTest) add_library( LibcHermeticTestMain + ${library_type} EXCLUDE_FROM_ALL LibcTestMain.cpp ) @@ -97,6 +108,7 @@ add_dependencies( add_library( LibcMemoryHelpers + ${library_type} MemoryMatcher.h MemoryMatcher.cpp ) diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp index ea819c417437..32e0044a4ee4 100644 --- a/libc/test/UnitTest/HermeticTestUtils.cpp +++ b/libc/test/UnitTest/HermeticTestUtils.cpp @@ -17,6 +17,7 @@ int memcmp(const void *lhs, const void *rhs, size_t count); 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 @@ void *memset(void *ptr, int value, size_t count) { 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 @@ void __cxa_pure_virtual() { __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 index d400b0aadbf3..f0192c24b300 100644 --- 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 @@ add_libc_test( 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 index 792a28e8cbba..577c281eccd3 100644 --- a/libc/test/src/__support/CPP/CMakeLists.txt +++ b/libc/test/src/__support/CPP/CMakeLists.txt @@ -61,16 +61,19 @@ add_libc_test( 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 index 3bd1d752ae9e..16408f7ad146 100644 --- 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<int> 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<int> 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 index ea9bd6c56f77..6f61e0ffefb0 100644 --- 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( |