[libc-commits] [libc] f4002c1 - [libc] Enable running libc unit tests on NVPTX

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Thu May 4 06:36:38 PDT 2023


Author: Joseph Huber
Date: 2023-05-04T08:36:29-05:00
New Revision: f4002c1415a70701d6603c293afbed4f094da4a4

URL: https://github.com/llvm/llvm-project/commit/f4002c1415a70701d6603c293afbed4f094da4a4
DIFF: https://github.com/llvm/llvm-project/commit/f4002c1415a70701d6603c293afbed4f094da4a4.diff

LOG: [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

Added: 
    

Modified: 
    libc/cmake/modules/LLVMLibCTestRules.cmake
    libc/test/CMakeLists.txt
    libc/test/UnitTest/CMakeLists.txt
    libc/test/UnitTest/HermeticTestUtils.cpp
    libc/test/src/__support/CMakeLists.txt
    libc/test/src/__support/CPP/CMakeLists.txt
    libc/test/src/__support/CPP/atomic_test.cpp
    libc/test/utils/UnitTest/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index a3a34136604de..4c1b143b66186 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -551,7 +551,8 @@ if(LIBC_GPU_TARGET_ARCHITECTURE_IS_AMDGPU)
        -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
index 01baeaeabf57a..027028de71640 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 d87ad8075af9c..7a54d502b50ce 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 ea819c4174371..32e0044a4ee45 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 d400b0aadbf32..f0192c24b3002 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 792a28e8cbba3..577c281eccd37 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 3bd1d752ae9eb..16408f7ad1467 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 ea9bd6c56f772..6f61e0ffefb00 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(


        


More information about the libc-commits mailing list