Skip to content

Commit 632fa37

Browse files
committed
[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
1 parent 17faae9 commit 632fa37

File tree

12 files changed

+52
-45
lines changed

12 files changed

+52
-45
lines changed

libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
# Initialize ALL_CPU_FEATURES as empty list.
66
set(ALL_CPU_FEATURES "")
77

8-
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
9-
return()
10-
endif()
11-
128
if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
139
set(ALL_CPU_FEATURES SSE2 SSE4_2 AVX AVX2 AVX512F AVX512BW FMA)
1410
set(LIBC_COMPILE_OPTIONS_NATIVE -march=native)
@@ -26,6 +22,10 @@ list(SORT ALL_CPU_FEATURES)
2622
# <list of cpu features>
2723
# )
2824
function(cpu_supports output_var features)
25+
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
26+
unset(${output_var} PARENT_SCOPE)
27+
return()
28+
endif()
2929
_intersection(var "${LIBC_CPU_FEATURES}" "${features}")
3030
if("${var}" STREQUAL "${features}")
3131
set(${output_var} TRUE PARENT_SCOPE)

libc/config/gpu/entrypoints.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,8 @@ set(TARGET_LIBC_ENTRYPOINTS
5656

5757
# stdlib.h entrypoints
5858
libc.src.stdlib.abs
59-
libc.src.stdlib.atoi
60-
libc.src.stdlib.atof
61-
libc.src.stdlib.atol
62-
libc.src.stdlib.atoll
6359
libc.src.stdlib.labs
6460
libc.src.stdlib.llabs
65-
libc.src.stdlib.strtod
66-
libc.src.stdlib.strtof
67-
libc.src.stdlib.strtol
68-
libc.src.stdlib.strtold
69-
libc.src.stdlib.strtoll
70-
libc.src.stdlib.strtoul
71-
libc.src.stdlib.strtoull
7261

7362
# stdlib.h entrypoints
7463
libc.src.stdlib._Exit

libc/docs/gpu/support.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ stdlib.h
8585
Function Name Available RPC Required
8686
============= ========= ============
8787
abs |check|
88-
atoi |check|
89-
atof |check|
90-
atol |check|
91-
atoll |check|
88+
atoi
89+
atof
90+
atol
91+
atoll
9292
labs |check|
9393
llabs |check|
94-
strtod |check|
95-
strtof |check|
96-
strtol |check|
97-
strtold |check|
98-
strtoll |check|
99-
strtoul |check|
100-
strtoull |check|
94+
strtod
95+
strtof
96+
strtol
97+
strtold
98+
strtoll
99+
strtoul
100+
strtoull
101101
============= ========= ============

libc/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if(LIBC_TARGET_ARCHITECTURE_IS_GPU AND
2222
return()
2323
endif()
2424

25-
if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
25+
if(NOT LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX)
2626
add_subdirectory(src)
2727
add_subdirectory(utils)
2828
endif()

libc/test/UnitTest/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,12 @@ foreach(lib LibcFPTestHelpers LibcFPExceptionHelpers LibcMemoryHelpers
138138
target_compile_options(${lib} PRIVATE -fno-exceptions -fno-rtti)
139139
target_link_libraries(${lib} LibcUnitTest)
140140
endforeach()
141+
142+
# The GPU needs these flags applied to override the system triple.
143+
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
144+
foreach(lib LibcMemoryHelpers)
145+
target_include_directories(${lib} PRIVATE ${LIBC_BUILD_DIR}/include)
146+
target_compile_options(${lib}
147+
PRIVATE ${LIBC_HERMETIC_TEST_COMPILE_OPTIONS} -ffreestanding -nostdlib -nostdlib++)
148+
endforeach()
149+
endif()

libc/test/src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function(add_fp_unittest name)
99

1010
if(MATH_UNITTEST_NEED_MPFR)
1111
if(NOT LIBC_TESTS_CAN_USE_MPFR)
12-
message("WARNING: Math test ${name} will be skipped as MPFR library is not available.")
12+
message(VERBOSE "Math test ${name} will be skipped as MPFR library is not available.")
1313
return()
1414
endif()
1515
endif()

libc/test/src/__support/CMakeLists.txt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,18 @@ add_libc_test(
5454
libc.src.__support.CPP.string_view
5555
)
5656

57-
add_libc_test(
58-
arg_list_test
59-
SUITE
60-
libc-support-tests
61-
SRCS
62-
arg_list_test.cpp
63-
DEPENDS
64-
libc.src.__support.arg_list
65-
)
57+
# The GPU does not support varargs currently.
58+
if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
59+
add_libc_test(
60+
arg_list_test
61+
SUITE
62+
libc-support-tests
63+
SRCS
64+
arg_list_test.cpp
65+
DEPENDS
66+
libc.src.__support.arg_list
67+
)
68+
endif()
6669

6770
add_libc_test(
6871
uint_test

libc/test/src/__support/File/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
if(NOT (TARGET libc.src.__support.threads.mutex))
1+
if(NOT (TARGET libc.src.__support.threads.mutex) OR LIBC_TARGET_ARCHITECTURE_IS_GPU)
22
# Not all platforms have a mutex implementation. If mutex is unvailable,
3-
# we just skip everything about files.
3+
# we just skip everything about files. The GPU does not currently support
4+
# files as well.
45
return()
56
endif()
67

libc/test/src/__support/blockstore_test.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterateReverse10) {
9090
populate_and_iterate<4, 10, true>();
9191
}
9292

93-
TEST_F(LlvmLibcBlockStoreTest, Back) {
94-
back_test<false>();
95-
back_test<true>();
96-
}
93+
TEST_F(LlvmLibcBlockStoreTest, Back) { back_test<false>(); }
94+
95+
// FIXME: Combing this test with the above test makes the AMDGPU backend
96+
// generate code which hangs. This should be fixed in the clang compiler.
97+
TEST_F(LlvmLibcBlockStoreTest, BackReverse) { back_test<true>(); }
9798

9899
TEST_F(LlvmLibcBlockStoreTest, Empty) {
99100
empty_test<false>();

libc/test/src/errno/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if(NOT LLVM_LIBC_FULL_BUILD)
1+
if(NOT LLVM_LIBC_FULL_BUILD OR LIBC_TARGET_ARCHITECTURE_IS_GPU)
22
return()
33
endif()
44

libc/test/src/stdio/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ add_libc_unittest(
318318
libc.src.stdio.setvbuf
319319
)
320320

321+
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
322+
return()
323+
endif()
324+
321325
add_subdirectory(printf_core)
322326
add_subdirectory(scanf_core)
323327
add_subdirectory(testdata)

libc/test/src/string/StrchrTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ template <auto Func> struct StrrchrTest : public __llvm_libc::testing::Test {
120120
}
121121

122122
void findsLastBehindFirstNullTerminator() {
123-
const char src[6] = {'a', 'a', '\0', 'b', '\0', 'c'};
123+
static const char src[6] = {'a', 'a', '\0', 'b', '\0', 'c'};
124124
// 'b' is behind a null terminator, so should not be found.
125125
ASSERT_STREQ(Func(src, 'b'), nullptr);
126126
// Same goes for 'c'.

0 commit comments

Comments
 (0)