[libc-commits] [libc] 632fa37 - [libc] Enable running libc unit tests on AMDGPU

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Thu May 4 04:33:03 PDT 2023


Author: Joseph Huber
Date: 2023-05-04T06:32:52-05:00
New Revision: 632fa3798cc3cdb9d91867c1ae978809ec4bb253

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

LOG: [libc] Enable running libc unit tests on AMDGPU

The previous patches added the necessary support for global constructors
used to register tests. This patch enables the AMDGPU 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.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D149517

Added: 
    

Modified: 
    libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
    libc/config/gpu/entrypoints.txt
    libc/docs/gpu/support.rst
    libc/test/CMakeLists.txt
    libc/test/UnitTest/CMakeLists.txt
    libc/test/src/CMakeLists.txt
    libc/test/src/__support/CMakeLists.txt
    libc/test/src/__support/File/CMakeLists.txt
    libc/test/src/__support/blockstore_test.cpp
    libc/test/src/errno/CMakeLists.txt
    libc/test/src/stdio/CMakeLists.txt
    libc/test/src/string/StrchrTest.h

Removed: 
    


################################################################################
diff  --git a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
index f70cffbf81347..0b522318aaa12 100644
--- a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
+++ b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
@@ -5,10 +5,6 @@
 # Initialize ALL_CPU_FEATURES as empty list.
 set(ALL_CPU_FEATURES "")
 
-if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
-  return()
-endif()
-
 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
   set(ALL_CPU_FEATURES SSE2 SSE4_2 AVX AVX2 AVX512F AVX512BW FMA)
   set(LIBC_COMPILE_OPTIONS_NATIVE -march=native)
@@ -26,6 +22,10 @@ list(SORT ALL_CPU_FEATURES)
 #   <list of cpu features>
 # )
 function(cpu_supports output_var features)
+  if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
+    unset(${output_var} PARENT_SCOPE)
+    return()
+  endif()
   _intersection(var "${LIBC_CPU_FEATURES}" "${features}")
   if("${var}" STREQUAL "${features}")
     set(${output_var} TRUE PARENT_SCOPE)

diff  --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index aa2a6eb01247c..170f21c73f808 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -56,19 +56,8 @@ set(TARGET_LIBC_ENTRYPOINTS
 
     # stdlib.h entrypoints
     libc.src.stdlib.abs
-    libc.src.stdlib.atoi
-    libc.src.stdlib.atof
-    libc.src.stdlib.atol
-    libc.src.stdlib.atoll
     libc.src.stdlib.labs
     libc.src.stdlib.llabs
-    libc.src.stdlib.strtod
-    libc.src.stdlib.strtof
-    libc.src.stdlib.strtol
-    libc.src.stdlib.strtold
-    libc.src.stdlib.strtoll
-    libc.src.stdlib.strtoul
-    libc.src.stdlib.strtoull
 
     # stdlib.h entrypoints
     libc.src.stdlib._Exit

diff  --git a/libc/docs/gpu/support.rst b/libc/docs/gpu/support.rst
index d5b9948fb779e..cc3c326f9e565 100644
--- a/libc/docs/gpu/support.rst
+++ b/libc/docs/gpu/support.rst
@@ -85,17 +85,17 @@ stdlib.h
 Function Name  Available  RPC Required
 =============  =========  ============
 abs            |check|
-atoi           |check|
-atof           |check|
-atol           |check|
-atoll          |check|
+atoi           
+atof           
+atol           
+atoll          
 labs           |check|
 llabs          |check|
-strtod         |check|
-strtof         |check|
-strtol         |check|
-strtold        |check|
-strtoll        |check|
-strtoul        |check|
-strtoull       |check|
+strtod         
+strtof         
+strtol         
+strtold        
+strtoll        
+strtoul        
+strtoull       
 =============  =========  ============

diff  --git a/libc/test/CMakeLists.txt b/libc/test/CMakeLists.txt
index ed6de64780731..01baeaeabf57a 100644
--- a/libc/test/CMakeLists.txt
+++ b/libc/test/CMakeLists.txt
@@ -22,7 +22,7 @@ if(LIBC_TARGET_ARCHITECTURE_IS_GPU AND
   return()
 endif()
 
-if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
+if(NOT LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX)
   add_subdirectory(src)
   add_subdirectory(utils)
 endif()

diff  --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index f74577bacabc9..d87ad8075af9c 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -138,3 +138,12 @@ foreach(lib LibcFPTestHelpers LibcFPExceptionHelpers LibcMemoryHelpers
   target_compile_options(${lib} PRIVATE -fno-exceptions -fno-rtti)
   target_link_libraries(${lib} LibcUnitTest)
 endforeach()
+
+# The GPU needs these flags applied to override the system triple.
+if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
+  foreach(lib LibcMemoryHelpers)
+    target_include_directories(${lib} PRIVATE ${LIBC_BUILD_DIR}/include)
+    target_compile_options(${lib}
+        PRIVATE ${LIBC_HERMETIC_TEST_COMPILE_OPTIONS} -ffreestanding -nostdlib -nostdlib++)
+  endforeach()
+endif()

diff  --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index 7e7d7fd610984..9e6c02c1f9f4b 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -9,7 +9,7 @@ function(add_fp_unittest name)
 
   if(MATH_UNITTEST_NEED_MPFR)
     if(NOT LIBC_TESTS_CAN_USE_MPFR)
-      message("WARNING: Math test ${name} will be skipped as MPFR library is not available.")
+      message(VERBOSE "Math test ${name} will be skipped as MPFR library is not available.")
       return()
     endif()
   endif()

diff  --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index fec62820718da..d400b0aadbf32 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -54,15 +54,18 @@ add_libc_test(
     libc.src.__support.CPP.string_view
 )
 
-add_libc_test(
-  arg_list_test
-  SUITE
-    libc-support-tests
-  SRCS
-    arg_list_test.cpp
-  DEPENDS
-    libc.src.__support.arg_list
-)
+# The GPU does not support varargs currently.
+if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
+  add_libc_test(
+    arg_list_test
+    SUITE
+      libc-support-tests
+    SRCS
+      arg_list_test.cpp
+    DEPENDS
+      libc.src.__support.arg_list
+  )
+endif()
 
 add_libc_test(
   uint_test

diff  --git a/libc/test/src/__support/File/CMakeLists.txt b/libc/test/src/__support/File/CMakeLists.txt
index ad4a5f4f38703..d4fed45fdaa04 100644
--- a/libc/test/src/__support/File/CMakeLists.txt
+++ b/libc/test/src/__support/File/CMakeLists.txt
@@ -1,6 +1,7 @@
-if(NOT (TARGET libc.src.__support.threads.mutex))
+if(NOT (TARGET libc.src.__support.threads.mutex) OR LIBC_TARGET_ARCHITECTURE_IS_GPU)
   # Not all platforms have a mutex implementation. If mutex is unvailable,
-  # we just skip everything about files.
+  # we just skip everything about files. The GPU does not currently support
+  # files as well.
   return()
 endif()
 

diff  --git a/libc/test/src/__support/blockstore_test.cpp b/libc/test/src/__support/blockstore_test.cpp
index d372a62a9c8e9..10277af49116d 100644
--- a/libc/test/src/__support/blockstore_test.cpp
+++ b/libc/test/src/__support/blockstore_test.cpp
@@ -90,10 +90,11 @@ TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterateReverse10) {
   populate_and_iterate<4, 10, true>();
 }
 
-TEST_F(LlvmLibcBlockStoreTest, Back) {
-  back_test<false>();
-  back_test<true>();
-}
+TEST_F(LlvmLibcBlockStoreTest, Back) { back_test<false>(); }
+
+// FIXME: Combing this test with the above test makes the AMDGPU backend
+// generate code which hangs. This should be fixed in the clang compiler.
+TEST_F(LlvmLibcBlockStoreTest, BackReverse) { back_test<true>(); }
 
 TEST_F(LlvmLibcBlockStoreTest, Empty) {
   empty_test<false>();

diff  --git a/libc/test/src/errno/CMakeLists.txt b/libc/test/src/errno/CMakeLists.txt
index 00d381c16817b..633d46a1f5f88 100644
--- a/libc/test/src/errno/CMakeLists.txt
+++ b/libc/test/src/errno/CMakeLists.txt
@@ -1,4 +1,4 @@
-if(NOT LLVM_LIBC_FULL_BUILD)
+if(NOT LLVM_LIBC_FULL_BUILD OR LIBC_TARGET_ARCHITECTURE_IS_GPU)
   return()
 endif()
 

diff  --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 57400d812c107..5b66cadcdc127 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -318,6 +318,10 @@ add_libc_unittest(
     libc.src.stdio.setvbuf
 )
 
+if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
+  return()
+endif()
+
 add_subdirectory(printf_core)
 add_subdirectory(scanf_core)
 add_subdirectory(testdata)

diff  --git a/libc/test/src/string/StrchrTest.h b/libc/test/src/string/StrchrTest.h
index 1c5bee52863cc..71903c987cacb 100644
--- a/libc/test/src/string/StrchrTest.h
+++ b/libc/test/src/string/StrchrTest.h
@@ -120,7 +120,7 @@ template <auto Func> struct StrrchrTest : public __llvm_libc::testing::Test {
   }
 
   void findsLastBehindFirstNullTerminator() {
-    const char src[6] = {'a', 'a', '\0', 'b', '\0', 'c'};
+    static const char src[6] = {'a', 'a', '\0', 'b', '\0', 'c'};
     // 'b' is behind a null terminator, so should not be found.
     ASSERT_STREQ(Func(src, 'b'), nullptr);
     // Same goes for 'c'.


        


More information about the libc-commits mailing list