https://github.com/fineg74 created https://github.com/llvm/llvm-project/pull/181049
This is to add support to build libc for building with spirv backend, for use with OpenMP kernels >From 329eca02c11370d42e9167692de61e2730cad21a Mon Sep 17 00:00:00 2001 From: "Fine, Gregory" <[email protected]> Date: Wed, 11 Feb 2026 15:45:17 -0800 Subject: [PATCH] Add support to build libc for SPIRV backend --- clang/lib/Headers/llvm_libc_wrappers/assert.h | 4 +- clang/lib/Headers/llvm_libc_wrappers/ctype.h | 4 +- .../lib/Headers/llvm_libc_wrappers/inttypes.h | 4 +- clang/lib/Headers/llvm_libc_wrappers/stdio.h | 6 +- clang/lib/Headers/llvm_libc_wrappers/stdlib.h | 4 +- clang/lib/Headers/llvm_libc_wrappers/string.h | 4 +- clang/lib/Headers/llvm_libc_wrappers/time.h | 4 +- clang/lib/Headers/openmp_wrappers/new | 2 +- .../cmake/modules/LLVMLibCArchitectures.cmake | 7 +- libc/cmake/modules/LLVMLibCLibraryRules.cmake | 8 +- libc/config/gpu/spirv64/entrypoints.txt | 693 ++++++++++++++++++ libc/config/gpu/spirv64/headers.txt | 21 + libc/include/llvm-libc-macros/math-macros.h | 2 +- libc/include/llvm-libc-macros/signal-macros.h | 2 +- libc/include/llvm-libc-macros/time-macros.h | 2 +- libc/include/llvm-libc-types/fenv_t.h | 2 +- libc/shared/rpc_util.h | 2 +- .../macros/properties/architectures.h | 6 +- .../macros/properties/cpu_features.h | 4 +- libc/src/__support/time/gpu/time_utils.cpp | 2 +- libc/src/__support/time/gpu/time_utils.h | 2 +- libc/src/math/spirv64/CMakeLists.txt | 303 ++++++++ libc/src/math/spirv64/ceil.cpp | 17 + libc/src/math/spirv64/ceilf.cpp | 17 + libc/src/math/spirv64/copysign.cpp | 19 + libc/src/math/spirv64/copysignf.cpp | 19 + libc/src/math/spirv64/fabs.cpp | 17 + libc/src/math/spirv64/fabsf.cpp | 17 + libc/src/math/spirv64/floor.cpp | 17 + libc/src/math/spirv64/floorf.cpp | 17 + libc/src/math/spirv64/fma.cpp | 19 + libc/src/math/spirv64/fmaf.cpp | 19 + libc/src/math/spirv64/fmax.cpp | 22 + libc/src/math/spirv64/fmaxf.cpp | 20 + libc/src/math/spirv64/fmin.cpp | 20 + libc/src/math/spirv64/fminf.cpp | 20 + libc/src/math/spirv64/fmod.cpp | 19 + libc/src/math/spirv64/fmodf.cpp | 19 + libc/src/math/spirv64/frexp.cpp | 20 + libc/src/math/spirv64/frexpf.cpp | 20 + libc/src/math/spirv64/ldexp.cpp | 20 + libc/src/math/spirv64/ldexpf.cpp | 20 + libc/src/math/spirv64/lgamma.cpp | 19 + libc/src/math/spirv64/lgamma_r.cpp | 21 + libc/src/math/spirv64/llrint.cpp | 20 + libc/src/math/spirv64/llrintf.cpp | 20 + libc/src/math/spirv64/lrint.cpp | 20 + libc/src/math/spirv64/lrintf.cpp | 20 + libc/src/math/spirv64/nearbyint.cpp | 19 + libc/src/math/spirv64/nearbyintf.cpp | 19 + libc/src/math/spirv64/remainder.cpp | 19 + libc/src/math/spirv64/remainderf.cpp | 19 + libc/src/math/spirv64/rint.cpp | 17 + libc/src/math/spirv64/rintf.cpp | 17 + libc/src/math/spirv64/round.cpp | 17 + libc/src/math/spirv64/roundf.cpp | 17 + libc/src/math/spirv64/scalbn.cpp | 20 + libc/src/math/spirv64/scalbnf.cpp | 20 + libc/src/math/spirv64/sqrt.cpp | 17 + libc/src/math/spirv64/sqrtf.cpp | 17 + libc/src/math/spirv64/tgamma.cpp | 21 + libc/src/math/spirv64/tgammaf.cpp | 21 + libc/src/math/spirv64/trunc.cpp | 17 + libc/src/math/spirv64/truncf.cpp | 17 + libc/startup/gpu/CMakeLists.txt | 14 +- libc/startup/gpu/spirv64/CMakeLists.txt | 16 + libc/startup/gpu/spirv64/start.cpp | 72 ++ llvm/CMakeLists.txt | 5 +- 68 files changed, 1954 insertions(+), 33 deletions(-) create mode 100644 libc/config/gpu/spirv64/entrypoints.txt create mode 100644 libc/config/gpu/spirv64/headers.txt create mode 100644 libc/src/math/spirv64/CMakeLists.txt create mode 100644 libc/src/math/spirv64/ceil.cpp create mode 100644 libc/src/math/spirv64/ceilf.cpp create mode 100644 libc/src/math/spirv64/copysign.cpp create mode 100644 libc/src/math/spirv64/copysignf.cpp create mode 100644 libc/src/math/spirv64/fabs.cpp create mode 100644 libc/src/math/spirv64/fabsf.cpp create mode 100644 libc/src/math/spirv64/floor.cpp create mode 100644 libc/src/math/spirv64/floorf.cpp create mode 100644 libc/src/math/spirv64/fma.cpp create mode 100644 libc/src/math/spirv64/fmaf.cpp create mode 100644 libc/src/math/spirv64/fmax.cpp create mode 100644 libc/src/math/spirv64/fmaxf.cpp create mode 100644 libc/src/math/spirv64/fmin.cpp create mode 100644 libc/src/math/spirv64/fminf.cpp create mode 100644 libc/src/math/spirv64/fmod.cpp create mode 100644 libc/src/math/spirv64/fmodf.cpp create mode 100644 libc/src/math/spirv64/frexp.cpp create mode 100644 libc/src/math/spirv64/frexpf.cpp create mode 100644 libc/src/math/spirv64/ldexp.cpp create mode 100644 libc/src/math/spirv64/ldexpf.cpp create mode 100644 libc/src/math/spirv64/lgamma.cpp create mode 100644 libc/src/math/spirv64/lgamma_r.cpp create mode 100644 libc/src/math/spirv64/llrint.cpp create mode 100644 libc/src/math/spirv64/llrintf.cpp create mode 100644 libc/src/math/spirv64/lrint.cpp create mode 100644 libc/src/math/spirv64/lrintf.cpp create mode 100644 libc/src/math/spirv64/nearbyint.cpp create mode 100644 libc/src/math/spirv64/nearbyintf.cpp create mode 100644 libc/src/math/spirv64/remainder.cpp create mode 100644 libc/src/math/spirv64/remainderf.cpp create mode 100644 libc/src/math/spirv64/rint.cpp create mode 100644 libc/src/math/spirv64/rintf.cpp create mode 100644 libc/src/math/spirv64/round.cpp create mode 100644 libc/src/math/spirv64/roundf.cpp create mode 100644 libc/src/math/spirv64/scalbn.cpp create mode 100644 libc/src/math/spirv64/scalbnf.cpp create mode 100644 libc/src/math/spirv64/sqrt.cpp create mode 100644 libc/src/math/spirv64/sqrtf.cpp create mode 100644 libc/src/math/spirv64/tgamma.cpp create mode 100644 libc/src/math/spirv64/tgammaf.cpp create mode 100644 libc/src/math/spirv64/trunc.cpp create mode 100644 libc/src/math/spirv64/truncf.cpp create mode 100644 libc/startup/gpu/spirv64/CMakeLists.txt create mode 100644 libc/startup/gpu/spirv64/start.cpp diff --git a/clang/lib/Headers/llvm_libc_wrappers/assert.h b/clang/lib/Headers/llvm_libc_wrappers/assert.h index 7eadb2c354aab..3d61d5db59c36 100644 --- a/clang/lib/Headers/llvm_libc_wrappers/assert.h +++ b/clang/lib/Headers/llvm_libc_wrappers/assert.h @@ -9,7 +9,7 @@ #ifndef __CLANG_LLVM_LIBC_WRAPPERS_ASSERT_H__ #define __CLANG_LLVM_LIBC_WRAPPERS_ASSERT_H__ -#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) +#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) && !defined(__SPIRV__) #error "This file is for GPU offloading compilation only" #endif @@ -17,7 +17,7 @@ #if __has_include(<llvm-libc-decls/assert.h>) -#if defined(__HIP__) || defined(__CUDA__) +#if defined(__HIP__) || defined(__CUDA__) || defined(__SPIRV__) #define __LIBC_ATTRS __attribute__((device)) #else #define __LIBC_ATTRS diff --git a/clang/lib/Headers/llvm_libc_wrappers/ctype.h b/clang/lib/Headers/llvm_libc_wrappers/ctype.h index 79b0c1e9be953..7879e2fb56db8 100644 --- a/clang/lib/Headers/llvm_libc_wrappers/ctype.h +++ b/clang/lib/Headers/llvm_libc_wrappers/ctype.h @@ -9,13 +9,13 @@ #ifndef __CLANG_LLVM_LIBC_WRAPPERS_CTYPE_H__ #define __CLANG_LLVM_LIBC_WRAPPERS_CTYPE_H__ -#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) +#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) && !defined(__SPIRV__) #error "This file is for GPU offloading compilation only" #endif #include_next <ctype.h> -#if defined(__HIP__) || defined(__CUDA__) +#if defined(__HIP__) || defined(__CUDA__) || defined(__SPIRV__) #define __LIBC_ATTRS __attribute__((device)) #else #define __LIBC_ATTRS diff --git a/clang/lib/Headers/llvm_libc_wrappers/inttypes.h b/clang/lib/Headers/llvm_libc_wrappers/inttypes.h index 22613898248f3..e99d3f556ff23 100644 --- a/clang/lib/Headers/llvm_libc_wrappers/inttypes.h +++ b/clang/lib/Headers/llvm_libc_wrappers/inttypes.h @@ -9,7 +9,7 @@ #ifndef __CLANG_LLVM_LIBC_WRAPPERS_INTTYPES_H__ #define __CLANG_LLVM_LIBC_WRAPPERS_INTTYPES_H__ -#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) +#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) && !defined(__SPIRV__) #error "This file is for GPU offloading compilation only" #endif @@ -17,7 +17,7 @@ #if __has_include(<llvm-libc-decls/inttypes.h>) -#if defined(__HIP__) || defined(__CUDA__) +#if defined(__HIP__) || defined(__CUDA__) || defined(__SPIRV__) #define __LIBC_ATTRS __attribute__((device)) #else #define __LIBC_ATTRS diff --git a/clang/lib/Headers/llvm_libc_wrappers/stdio.h b/clang/lib/Headers/llvm_libc_wrappers/stdio.h index 0c3e44823da70..b914c92dd6534 100644 --- a/clang/lib/Headers/llvm_libc_wrappers/stdio.h +++ b/clang/lib/Headers/llvm_libc_wrappers/stdio.h @@ -9,13 +9,13 @@ #ifndef __CLANG_LLVM_LIBC_WRAPPERS_STDIO_H__ #define __CLANG_LLVM_LIBC_WRAPPERS_STDIO_H__ -#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) +#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) &&!defined(__SPIRV__) #error "This file is for GPU offloading compilation only" #endif #include_next <stdio.h> -#if defined(__HIP__) || defined(__CUDA__) +#if defined(__HIP__) || defined(__CUDA__) || defined(__SPIRV__) #define __LIBC_ATTRS __attribute__((device)) #else #define __LIBC_ATTRS @@ -41,7 +41,7 @@ __LIBC_ATTRS extern FILE *stdout; #pragma omp end declare target // Restore the original macros when compiling on the host. -#if !defined(__NVPTX__) && !defined(__AMDGPU__) +#if !defined(__NVPTX__) && !defined(__AMDGPU__) && !defined(__SPIRV__) #pragma pop_macro("stderr") #pragma pop_macro("stdin") #pragma pop_macro("stdout") diff --git a/clang/lib/Headers/llvm_libc_wrappers/stdlib.h b/clang/lib/Headers/llvm_libc_wrappers/stdlib.h index 7af5e2ebe031a..24145c64694da 100644 --- a/clang/lib/Headers/llvm_libc_wrappers/stdlib.h +++ b/clang/lib/Headers/llvm_libc_wrappers/stdlib.h @@ -9,13 +9,13 @@ #ifndef __CLANG_LLVM_LIBC_WRAPPERS_STDLIB_H__ #define __CLANG_LLVM_LIBC_WRAPPERS_STDLIB_H__ -#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) +#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) && !defined(__SPIRV__) #error "This file is for GPU offloading compilation only" #endif #include_next <stdlib.h> -#if defined(__HIP__) || defined(__CUDA__) +#if defined(__HIP__) || defined(__CUDA__) || defined(__SPIRV__) #define __LIBC_ATTRS __attribute__((device)) #else #define __LIBC_ATTRS diff --git a/clang/lib/Headers/llvm_libc_wrappers/string.h b/clang/lib/Headers/llvm_libc_wrappers/string.h index 766a58f5b6db4..78abe2eace942 100644 --- a/clang/lib/Headers/llvm_libc_wrappers/string.h +++ b/clang/lib/Headers/llvm_libc_wrappers/string.h @@ -9,13 +9,13 @@ #ifndef __CLANG_LLVM_LIBC_WRAPPERS_STRING_H__ #define __CLANG_LLVM_LIBC_WRAPPERS_STRING_H__ -#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) +#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) && !defined(__SPIRV__) #error "This file is for GPU offloading compilation only" #endif #include_next <string.h> -#if defined(__HIP__) || defined(__CUDA__) +#if defined(__HIP__) || defined(__CUDA__) || defined(__SPIRV__) #define __LIBC_ATTRS __attribute__((device)) #else #define __LIBC_ATTRS diff --git a/clang/lib/Headers/llvm_libc_wrappers/time.h b/clang/lib/Headers/llvm_libc_wrappers/time.h index d38eea327a199..9ed8ccc39e72b 100644 --- a/clang/lib/Headers/llvm_libc_wrappers/time.h +++ b/clang/lib/Headers/llvm_libc_wrappers/time.h @@ -9,13 +9,13 @@ #ifndef __CLANG_LLVM_LIBC_WRAPPERS_TIME_H__ #define __CLANG_LLVM_LIBC_WRAPPERS_TIME_H__ -#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) +#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) && !defined(__SPIRV__) #error "This file is for GPU offloading compilation only" #endif #include_next <time.h> -#if defined(__HIP__) || defined(__CUDA__) +#if defined(__HIP__) || defined(__CUDA__) || defined(__SPIRV__) #define __LIBC_ATTRS __attribute__((device)) #else #define __LIBC_ATTRS diff --git a/clang/lib/Headers/openmp_wrappers/new b/clang/lib/Headers/openmp_wrappers/new index 8bad3f19d6251..de40cb9a210a1 100644 --- a/clang/lib/Headers/openmp_wrappers/new +++ b/clang/lib/Headers/openmp_wrappers/new @@ -13,7 +13,7 @@ // which do not use nothrow_t are provided without the <new> header. #include_next <new> -#if (defined(__NVPTX__) || defined(__AMDGPU__)) && defined(_OPENMP) +#if (defined(__NVPTX__) || defined(__AMDGPU__) || defined(__SPIRV__)) && defined(_OPENMP) #include <cstdlib> diff --git a/libc/cmake/modules/LLVMLibCArchitectures.cmake b/libc/cmake/modules/LLVMLibCArchitectures.cmake index 939fc1226a4e9..5f2be539e7169 100644 --- a/libc/cmake/modules/LLVMLibCArchitectures.cmake +++ b/libc/cmake/modules/LLVMLibCArchitectures.cmake @@ -71,10 +71,15 @@ function(get_arch_and_system_from_triple triple arch_var sys_var) # Setting OS name for GPU architectures. list(GET triple_comps -1 gpu_target_sys) - if(gpu_target_sys MATCHES "^amdhsa" OR gpu_target_sys MATCHES "^cuda") + if(gpu_target_sys MATCHES "^amdhsa" OR gpu_target_sys MATCHES "^cuda" OR target_arch MATCHES "^spirv64") set(target_sys "gpu") endif() + if (target_arch MATCHES "^spirv64") + list(APPEND + LIBC_COMPILE_OPTIONS_DEFAULT "-emit-llvm") + endif() + set(${sys_var} ${target_sys} PARENT_SCOPE) endfunction(get_arch_and_system_from_triple) diff --git a/libc/cmake/modules/LLVMLibCLibraryRules.cmake b/libc/cmake/modules/LLVMLibCLibraryRules.cmake index 80439de6bb4b0..7352802bc9ce4 100644 --- a/libc/cmake/modules/LLVMLibCLibraryRules.cmake +++ b/libc/cmake/modules/LLVMLibCLibraryRules.cmake @@ -109,8 +109,14 @@ function(add_bitcode_entrypoint_library target_name base_target_name) endforeach() add_executable(${target_name} ${objects}) - target_link_options(${target_name} PRIVATE "${LIBC_COMPILE_OPTIONS_DEFAULT}" + if("${LLVM_DEFAULT_TARGET_TRIPLE}" MATCHES "^spirv" OR + "${CMAKE_CXX_COMPILER_TARGET}" MATCHES "^spirv") + target_link_options(${target_name} PRIVATE "${LIBC_COMPILE_OPTIONS_DEFAULT}" + "-nostdlib" "-emit-llvm") + else() + target_link_options(${target_name} PRIVATE "${LIBC_COMPILE_OPTIONS_DEFAULT}" "-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm") + endif() endfunction(add_bitcode_entrypoint_library) # A rule to build a library from a collection of entrypoint objects. diff --git a/libc/config/gpu/spirv64/entrypoints.txt b/libc/config/gpu/spirv64/entrypoints.txt new file mode 100644 index 0000000000000..0dda7d5c683ec --- /dev/null +++ b/libc/config/gpu/spirv64/entrypoints.txt @@ -0,0 +1,693 @@ +set(TARGET_LIBC_ENTRYPOINTS + # assert.h entrypoints + libc.src.assert.__assert_fail + + # ctype.h entrypoints + libc.src.ctype.isalnum + libc.src.ctype.isalnum_l + libc.src.ctype.isalpha + libc.src.ctype.isalpha_l + libc.src.ctype.isascii + libc.src.ctype.isblank + libc.src.ctype.isblank_l + libc.src.ctype.iscntrl + libc.src.ctype.iscntrl_l + libc.src.ctype.isdigit + libc.src.ctype.isdigit_l + libc.src.ctype.isgraph + libc.src.ctype.isgraph_l + libc.src.ctype.islower + libc.src.ctype.islower_l + libc.src.ctype.isprint + libc.src.ctype.isprint_l + libc.src.ctype.ispunct + libc.src.ctype.ispunct_l + libc.src.ctype.isspace + libc.src.ctype.isspace_l + libc.src.ctype.isupper + libc.src.ctype.isupper_l + libc.src.ctype.isxdigit + libc.src.ctype.isxdigit_l + libc.src.ctype.toascii + libc.src.ctype.tolower + libc.src.ctype.tolower_l + libc.src.ctype.toupper + libc.src.ctype.toupper_l + + # string.h entrypoints + libc.src.string.memccpy + libc.src.string.memchr + libc.src.string.memcmp + libc.src.string.memcpy + libc.src.string.memmem + libc.src.string.memmove + libc.src.string.mempcpy + libc.src.string.memrchr + libc.src.string.memset + libc.src.string.stpcpy + libc.src.string.stpncpy + libc.src.string.strcasestr + libc.src.string.strcat + libc.src.string.strchr + libc.src.string.strchrnul + libc.src.string.strcmp + libc.src.string.strcoll + libc.src.string.strcoll_l + libc.src.string.strcpy + libc.src.string.strcspn + libc.src.string.strdup + libc.src.string.strerror + libc.src.string.strlcat + libc.src.string.strlcpy + libc.src.string.strlen + libc.src.string.strncat + libc.src.string.strncmp + libc.src.string.strncpy + libc.src.string.strndup + libc.src.string.strnlen + libc.src.string.strpbrk + libc.src.string.strrchr + libc.src.string.strsep + libc.src.string.strspn + libc.src.string.strstr + libc.src.string.strtok + libc.src.string.strtok_r + libc.src.string.strxfrm + libc.src.string.strxfrm_l + + # strings.h entrypoints + libc.src.strings.bcmp + libc.src.strings.bcopy + libc.src.strings.bzero + libc.src.strings.index + libc.src.strings.rindex + libc.src.strings.strcasecmp + libc.src.strings.strncasecmp + + # stdbit.h entrypoints + libc.src.stdbit.stdc_bit_ceil_uc + libc.src.stdbit.stdc_bit_ceil_ui + libc.src.stdbit.stdc_bit_ceil_ul + libc.src.stdbit.stdc_bit_ceil_ull + libc.src.stdbit.stdc_bit_ceil_us + libc.src.stdbit.stdc_bit_floor_uc + libc.src.stdbit.stdc_bit_floor_ui + libc.src.stdbit.stdc_bit_floor_ul + libc.src.stdbit.stdc_bit_floor_ull + libc.src.stdbit.stdc_bit_floor_us + libc.src.stdbit.stdc_bit_width_uc + libc.src.stdbit.stdc_bit_width_ui + libc.src.stdbit.stdc_bit_width_ul + libc.src.stdbit.stdc_bit_width_ull + libc.src.stdbit.stdc_bit_width_us + libc.src.stdbit.stdc_count_ones_uc + libc.src.stdbit.stdc_count_ones_ui + libc.src.stdbit.stdc_count_ones_ul + libc.src.stdbit.stdc_count_ones_ull + libc.src.stdbit.stdc_count_ones_us + libc.src.stdbit.stdc_count_zeros_uc + libc.src.stdbit.stdc_count_zeros_ui + libc.src.stdbit.stdc_count_zeros_ul + libc.src.stdbit.stdc_count_zeros_ull + libc.src.stdbit.stdc_count_zeros_us + libc.src.stdbit.stdc_first_leading_one_uc + libc.src.stdbit.stdc_first_leading_one_ui + libc.src.stdbit.stdc_first_leading_one_ul + libc.src.stdbit.stdc_first_leading_one_ull + libc.src.stdbit.stdc_first_leading_one_us + libc.src.stdbit.stdc_first_leading_zero_uc + libc.src.stdbit.stdc_first_leading_zero_ui + libc.src.stdbit.stdc_first_leading_zero_ul + libc.src.stdbit.stdc_first_leading_zero_ull + libc.src.stdbit.stdc_first_leading_zero_us + libc.src.stdbit.stdc_first_trailing_one_uc + libc.src.stdbit.stdc_first_trailing_one_ui + libc.src.stdbit.stdc_first_trailing_one_ul + libc.src.stdbit.stdc_first_trailing_one_ull + libc.src.stdbit.stdc_first_trailing_one_us + libc.src.stdbit.stdc_first_trailing_zero_uc + libc.src.stdbit.stdc_first_trailing_zero_ui + libc.src.stdbit.stdc_first_trailing_zero_ul + libc.src.stdbit.stdc_first_trailing_zero_ull + libc.src.stdbit.stdc_first_trailing_zero_us + libc.src.stdbit.stdc_has_single_bit_uc + libc.src.stdbit.stdc_has_single_bit_ui + libc.src.stdbit.stdc_has_single_bit_ul + libc.src.stdbit.stdc_has_single_bit_ull + libc.src.stdbit.stdc_has_single_bit_us + libc.src.stdbit.stdc_leading_ones_uc + libc.src.stdbit.stdc_leading_ones_ui + libc.src.stdbit.stdc_leading_ones_ul + libc.src.stdbit.stdc_leading_ones_ull + libc.src.stdbit.stdc_leading_ones_us + libc.src.stdbit.stdc_leading_zeros_uc + libc.src.stdbit.stdc_leading_zeros_ui + libc.src.stdbit.stdc_leading_zeros_ul + libc.src.stdbit.stdc_leading_zeros_ull + libc.src.stdbit.stdc_leading_zeros_us + libc.src.stdbit.stdc_trailing_ones_uc + libc.src.stdbit.stdc_trailing_ones_ui + libc.src.stdbit.stdc_trailing_ones_ul + libc.src.stdbit.stdc_trailing_ones_ull + libc.src.stdbit.stdc_trailing_ones_us + libc.src.stdbit.stdc_trailing_zeros_uc + libc.src.stdbit.stdc_trailing_zeros_ui + libc.src.stdbit.stdc_trailing_zeros_ul + libc.src.stdbit.stdc_trailing_zeros_ull + libc.src.stdbit.stdc_trailing_zeros_us + + # stdlib.h entrypoints + libc.src.stdlib._Exit + libc.src.stdlib.abort + libc.src.stdlib.abs + libc.src.stdlib.atexit + libc.src.stdlib.atof + libc.src.stdlib.atoi + libc.src.stdlib.atol + libc.src.stdlib.atoll + libc.src.stdlib.bsearch + libc.src.stdlib.div + libc.src.stdlib.exit + libc.src.stdlib.labs + libc.src.stdlib.ldiv + libc.src.stdlib.llabs + libc.src.stdlib.lldiv + libc.src.stdlib.memalignment + libc.src.stdlib.qsort + libc.src.stdlib.qsort_r + libc.src.stdlib.rand + libc.src.stdlib.srand + libc.src.stdlib.strtod + libc.src.stdlib.strtod_l + libc.src.stdlib.strtof + libc.src.stdlib.strtof_l + libc.src.stdlib.strtol + libc.src.stdlib.strtol_l + libc.src.stdlib.strtold + libc.src.stdlib.strtold_l + libc.src.stdlib.strtoll + libc.src.stdlib.strtoll_l + libc.src.stdlib.strtoul + libc.src.stdlib.strtoul_l + libc.src.stdlib.strtoull + libc.src.stdlib.strtoull_l + libc.src.stdlib.at_quick_exit + libc.src.stdlib.quick_exit + libc.src.stdlib.getenv + libc.src.stdlib.system + + # TODO: Implement these correctly + libc.src.stdlib.aligned_alloc + libc.src.stdlib.calloc + libc.src.stdlib.free + libc.src.stdlib.malloc + libc.src.stdlib.realloc + + # errno.h entrypoints + libc.src.errno.errno + + # stdio.h entrypoints + libc.src.stdio.clearerr + libc.src.stdio.fclose + libc.src.stdio.printf + libc.src.stdio.vprintf + libc.src.stdio.fprintf + libc.src.stdio.vfprintf + libc.src.stdio.snprintf + libc.src.stdio.sprintf + libc.src.stdio.vsnprintf + libc.src.stdio.vsprintf + libc.src.stdio.asprintf + libc.src.stdio.vasprintf + libc.src.stdio.scanf + libc.src.stdio.vscanf + libc.src.stdio.fscanf + libc.src.stdio.vfscanf + libc.src.stdio.sscanf + libc.src.stdio.vsscanf + libc.src.stdio.feof + libc.src.stdio.ferror + libc.src.stdio.fflush + libc.src.stdio.fgetc + libc.src.stdio.fgets + libc.src.stdio.fopen + libc.src.stdio.fputc + libc.src.stdio.fputs + libc.src.stdio.fread + libc.src.stdio.fseek + libc.src.stdio.ftell + libc.src.stdio.fwrite + libc.src.stdio.getc + libc.src.stdio.getchar + libc.src.stdio.putc + libc.src.stdio.putchar + libc.src.stdio.puts + libc.src.stdio.remove + libc.src.stdio.rename + libc.src.stdio.stderr + libc.src.stdio.stdin + libc.src.stdio.stdout + libc.src.stdio.ungetc + + # inttypes.h entrypoints + libc.src.inttypes.imaxabs + libc.src.inttypes.imaxdiv + libc.src.inttypes.strtoimax + libc.src.inttypes.strtoumax + + # time.h entrypoints + libc.src.time.clock + libc.src.time.clock_gettime + libc.src.time.timespec_get + libc.src.time.nanosleep + libc.src.time.strftime + libc.src.time.strftime_l + libc.src.time.mktime + + # wchar.h entrypoints + libc.src.wchar.wcslen + libc.src.wchar.wctob + + # locale.h entrypoints + libc.src.locale.localeconv + libc.src.locale.duplocale + libc.src.locale.freelocale + libc.src.locale.localeconv + libc.src.locale.newlocale + libc.src.locale.setlocale + libc.src.locale.uselocale +) + +set(TARGET_LIBM_ENTRYPOINTS + # math.h entrypoints + libc.src.math.acos + libc.src.math.acosf + libc.src.math.acoshf + libc.src.math.asin + libc.src.math.asinf + libc.src.math.asinhf + libc.src.math.atan + libc.src.math.atan2 + libc.src.math.atan2f + libc.src.math.atan2l + libc.src.math.atanf + libc.src.math.atanhf + libc.src.math.canonicalize + libc.src.math.canonicalizef + libc.src.math.canonicalizel + libc.src.math.cbrt + libc.src.math.cbrtf + libc.src.math.ceil + libc.src.math.ceilf + libc.src.math.ceill + libc.src.math.copysign + libc.src.math.copysignf + libc.src.math.copysignl + libc.src.math.cos + libc.src.math.cosf + libc.src.math.coshf + libc.src.math.cospif + libc.src.math.ddivl + libc.src.math.dfmal + libc.src.math.dmull + libc.src.math.dsqrtl + libc.src.math.erff + libc.src.math.exp + libc.src.math.exp10 + libc.src.math.exp10f + libc.src.math.exp2 + libc.src.math.exp2f + libc.src.math.exp2m1f + libc.src.math.expf + libc.src.math.expm1 + libc.src.math.expm1f + libc.src.math.fabs + libc.src.math.fabsf + libc.src.math.fabsl + libc.src.math.fadd + libc.src.math.faddl + libc.src.math.fdim + libc.src.math.fdimf + libc.src.math.fdiml + libc.src.math.fdiv + libc.src.math.fdivl + libc.src.math.ffma + libc.src.math.ffmal + libc.src.math.floor + libc.src.math.floorf + libc.src.math.floorl + libc.src.math.fma + libc.src.math.fmaf + libc.src.math.fmax + libc.src.math.fmaxf + libc.src.math.fmaximum + libc.src.math.fmaximumf + libc.src.math.fmaximuml + libc.src.math.fmaximum_mag + libc.src.math.fmaximum_magf + libc.src.math.fmaximum_magl + libc.src.math.fmaximum_mag_num + libc.src.math.fmaximum_mag_numf + libc.src.math.fmaximum_mag_numl + libc.src.math.fmaximum_num + libc.src.math.fmaximum_numf + libc.src.math.fmaximum_numl + libc.src.math.fmaxl + libc.src.math.fmin + libc.src.math.fminf + libc.src.math.fminimum + libc.src.math.fminimumf + libc.src.math.fminimuml + libc.src.math.fminimum_mag + libc.src.math.fminimum_magf + libc.src.math.fminimum_magl + libc.src.math.fminimum_mag_num + libc.src.math.fminimum_mag_numf + libc.src.math.fminimum_mag_numl + libc.src.math.fminimum_num + libc.src.math.fminimum_numf + libc.src.math.fminimum_numl + libc.src.math.fminl + libc.src.math.fmod + libc.src.math.fmodf + libc.src.math.fmodl + libc.src.math.fmul + libc.src.math.fmull + libc.src.math.frexp + libc.src.math.frexpf + libc.src.math.frexpl + libc.src.math.fromfp + libc.src.math.fromfpf + libc.src.math.fromfpl + libc.src.math.fromfpx + libc.src.math.fromfpxf + libc.src.math.fromfpxl + libc.src.math.fsqrt + libc.src.math.fsqrtl + libc.src.math.fsub + libc.src.math.fsubl + libc.src.math.getpayload + libc.src.math.getpayloadf + libc.src.math.getpayloadl + libc.src.math.hypot + libc.src.math.hypotf + libc.src.math.ilogb + libc.src.math.ilogbf + libc.src.math.ilogbl + libc.src.math.isnan + libc.src.math.isnanf + libc.src.math.isnanl + libc.src.math.ldexp + libc.src.math.ldexpf + libc.src.math.ldexpl + libc.src.math.lgamma + libc.src.math.lgamma_r + libc.src.math.llogb + libc.src.math.llogbf + libc.src.math.llogbl + libc.src.math.llrint + libc.src.math.llrintf + libc.src.math.llrintl + libc.src.math.llround + libc.src.math.llroundf + libc.src.math.llroundl + libc.src.math.log + libc.src.math.log10 + libc.src.math.log10f + libc.src.math.log1p + libc.src.math.log1pf + libc.src.math.log2 + libc.src.math.log2f + libc.src.math.logb + libc.src.math.logbf + libc.src.math.logbl + libc.src.math.logf + libc.src.math.lrint + libc.src.math.lrintf + libc.src.math.lrintl + libc.src.math.lround + libc.src.math.lroundf + libc.src.math.lroundl + libc.src.math.modf + libc.src.math.modff + libc.src.math.modfl + libc.src.math.nan + libc.src.math.nanf + libc.src.math.nanl + libc.src.math.nearbyint + libc.src.math.nearbyintf + libc.src.math.nearbyintl + libc.src.math.nextafter + libc.src.math.nextafterf + libc.src.math.nextafterl + libc.src.math.nextdown + libc.src.math.nextdownf + libc.src.math.nextdownl + libc.src.math.nexttoward + libc.src.math.nexttowardf + libc.src.math.nexttowardl + libc.src.math.nextup + libc.src.math.nextupf + libc.src.math.nextupl + libc.src.math.pow + libc.src.math.powf + libc.src.math.remainder + libc.src.math.remainderf + libc.src.math.remainderl + libc.src.math.remquo + libc.src.math.remquof + libc.src.math.remquol + libc.src.math.rint + libc.src.math.rintf + libc.src.math.rintl + libc.src.math.roundeven + libc.src.math.roundevenf + libc.src.math.roundevenl + libc.src.math.round + libc.src.math.roundf + libc.src.math.roundl + libc.src.math.scalbln + libc.src.math.scalblnf + libc.src.math.scalblnl + libc.src.math.scalbn + libc.src.math.scalbnf + libc.src.math.scalbnl + libc.src.math.setpayload + libc.src.math.setpayloadf + libc.src.math.setpayloadl + libc.src.math.setpayloadsig + libc.src.math.setpayloadsigf + libc.src.math.setpayloadsigl + libc.src.math.sin + libc.src.math.sincos + libc.src.math.sincosf + libc.src.math.sinf + libc.src.math.sinhf + libc.src.math.sinpif + libc.src.math.sqrt + libc.src.math.sqrtf + libc.src.math.sqrtl + libc.src.math.tan + libc.src.math.tanf + libc.src.math.tanhf + libc.src.math.tanpif + libc.src.math.tgamma + libc.src.math.tgammaf + libc.src.math.totalorder + libc.src.math.totalorderf + libc.src.math.totalorderl + libc.src.math.totalordermag + libc.src.math.totalordermagf + libc.src.math.totalordermagl + libc.src.math.trunc + libc.src.math.truncf + libc.src.math.truncl + libc.src.math.ufromfp + libc.src.math.ufromfpf + libc.src.math.ufromfpl + libc.src.math.ufromfpx + libc.src.math.ufromfpxf + libc.src.math.ufromfpxl +) + +if(LIBC_TYPES_HAS_FLOAT16) + list(APPEND TARGET_LIBM_ENTRYPOINTS + # math.h C23 _Float16 entrypoints + libc.src.math.acosf16 + libc.src.math.acoshf16 + libc.src.math.acospif16 + libc.src.math.asinf16 + libc.src.math.asinhf16 + libc.src.math.atanf16 + libc.src.math.atanhf16 + libc.src.math.canonicalizef16 + libc.src.math.ceilf16 + libc.src.math.copysignf16 + libc.src.math.cosf16 + libc.src.math.coshf16 + libc.src.math.cospif16 + libc.src.math.exp10f16 + libc.src.math.exp10m1f16 + libc.src.math.exp2f16 + # libc.src.math.exp2m1f16 + libc.src.math.expf16 + libc.src.math.expm1f16 + libc.src.math.f16add + libc.src.math.f16addf + libc.src.math.f16addl + libc.src.math.f16div + libc.src.math.f16divf + libc.src.math.f16divl + libc.src.math.f16fma + libc.src.math.f16fmaf + libc.src.math.f16fmal + libc.src.math.f16mul + libc.src.math.f16mulf + libc.src.math.f16mull + libc.src.math.f16sqrt + libc.src.math.f16sqrtf + libc.src.math.f16sqrtl + libc.src.math.f16sub + libc.src.math.f16subf + libc.src.math.f16subl + libc.src.math.fabsf16 + libc.src.math.fdimf16 + libc.src.math.floorf16 + libc.src.math.fmaf16 + libc.src.math.fmaxf16 + libc.src.math.fmaximum_mag_numf16 + libc.src.math.fmaximum_magf16 + libc.src.math.fmaximum_numf16 + libc.src.math.fmaximumf16 + libc.src.math.fminf16 + libc.src.math.fminimum_mag_numf16 + libc.src.math.fminimum_magf16 + libc.src.math.fminimum_numf16 + libc.src.math.fminimumf16 + libc.src.math.fmodf16 + libc.src.math.frexpf16 + libc.src.math.fromfpf16 + libc.src.math.fromfpxf16 + libc.src.math.getpayloadf16 + libc.src.math.hypotf16 + libc.src.math.ilogbf16 + libc.src.math.iscanonicalf16 + libc.src.math.issignalingf16 + libc.src.math.ldexpf16 + libc.src.math.llogbf16 + libc.src.math.llrintf16 + libc.src.math.llroundf16 + libc.src.math.log10f16 + libc.src.math.log2f16 + libc.src.math.logbf16 + libc.src.math.logf16 + libc.src.math.lrintf16 + libc.src.math.lroundf16 + libc.src.math.modff16 + libc.src.math.nanf16 + libc.src.math.nearbyintf16 + libc.src.math.nextafterf16 + libc.src.math.nextdownf16 + libc.src.math.nexttowardf16 + libc.src.math.nextupf16 + libc.src.math.remainderf16 + libc.src.math.remquof16 + libc.src.math.rintf16 + libc.src.math.roundevenf16 + libc.src.math.roundf16 + libc.src.math.scalblnf16 + libc.src.math.scalbnf16 + libc.src.math.setpayloadf16 + libc.src.math.setpayloadsigf16 + libc.src.math.sinf16 + libc.src.math.sinhf16 + libc.src.math.sinpif16 + libc.src.math.sqrtf16 + libc.src.math.tanf16 + libc.src.math.tanhf16 + libc.src.math.tanpif16 + libc.src.math.totalorderf16 + libc.src.math.totalordermagf16 + libc.src.math.truncf16 + libc.src.math.ufromfpf16 + libc.src.math.ufromfpxf16 + ) +endif() + +list(APPEND TARGET_LIBM_ENTRYPOINTS + # bfloat16 entrypoints + libc.src.math.bf16add + libc.src.math.bf16addf + libc.src.math.bf16addl + libc.src.math.bf16div + libc.src.math.bf16divf + libc.src.math.bf16divl + libc.src.math.bf16fma + libc.src.math.bf16fmaf + libc.src.math.bf16fmal + libc.src.math.bf16mul + libc.src.math.bf16mulf + libc.src.math.bf16mull + libc.src.math.bf16sub + libc.src.math.bf16subf + libc.src.math.bf16subl + libc.src.math.canonicalizebf16 + libc.src.math.ceilbf16 + libc.src.math.copysignbf16 + libc.src.math.fabsbf16 + libc.src.math.fdimbf16 + libc.src.math.floorbf16 + libc.src.math.fmaxbf16 + libc.src.math.fmaximumbf16 + libc.src.math.fmaximum_magbf16 + libc.src.math.fmaximum_mag_numbf16 + libc.src.math.fmaximum_numbf16 + libc.src.math.fminbf16 + libc.src.math.fminimumbf16 + libc.src.math.fminimum_magbf16 + libc.src.math.fminimum_mag_numbf16 + libc.src.math.fminimum_numbf16 + libc.src.math.fmodbf16 + libc.src.math.frexpbf16 + libc.src.math.fromfpbf16 + libc.src.math.fromfpxbf16 + libc.src.math.getpayloadbf16 + libc.src.math.ilogbbf16 + libc.src.math.iscanonicalbf16 + libc.src.math.issignalingbf16 + libc.src.math.ldexpbf16 + libc.src.math.llogbbf16 + libc.src.math.llrintbf16 + libc.src.math.llroundbf16 + libc.src.math.log_bf16 + libc.src.math.logbbf16 + libc.src.math.lrintbf16 + libc.src.math.lroundbf16 + libc.src.math.modfbf16 + libc.src.math.nanbf16 + libc.src.math.nearbyintbf16 + libc.src.math.nextafterbf16 + libc.src.math.nextdownbf16 + libc.src.math.nexttowardbf16 + libc.src.math.nextupbf16 + libc.src.math.remainderbf16 + libc.src.math.remquobf16 + libc.src.math.rintbf16 + libc.src.math.roundbf16 + libc.src.math.roundevenbf16 + libc.src.math.scalblnbf16 + libc.src.math.scalbnbf16 + libc.src.math.setpayloadbf16 + libc.src.math.setpayloadsigbf16 + libc.src.math.sqrtbf16 + libc.src.math.truncbf16 + libc.src.math.totalorderbf16 + libc.src.math.totalordermagbf16 + libc.src.math.ufromfpbf16 + libc.src.math.ufromfpxbf16 +) + +set(TARGET_LLVMLIBC_ENTRYPOINTS + ${TARGET_LIBC_ENTRYPOINTS} + ${TARGET_LIBM_ENTRYPOINTS} +) diff --git a/libc/config/gpu/spirv64/headers.txt b/libc/config/gpu/spirv64/headers.txt new file mode 100644 index 0000000000000..fa8ad7c11ba8b --- /dev/null +++ b/libc/config/gpu/spirv64/headers.txt @@ -0,0 +1,21 @@ +set(TARGET_PUBLIC_HEADERS + libc.include.assert + libc.include.ctype + libc.include.string + libc.include.strings + libc.include.signal + libc.include.float + libc.include.stdint + libc.include.inttypes + libc.include.limits + libc.include.math + libc.include.fenv + libc.include.time + libc.include.errno + libc.include.stdlib + libc.include.stdio + libc.include.wchar + libc.include.uchar + libc.include.features + libc.include.locale +) diff --git a/libc/include/llvm-libc-macros/math-macros.h b/libc/include/llvm-libc-macros/math-macros.h index e1b12e3010fe9..2051e36f72e28 100644 --- a/libc/include/llvm-libc-macros/math-macros.h +++ b/libc/include/llvm-libc-macros/math-macros.h @@ -44,7 +44,7 @@ // Math error handling. Target support is assumed to be existent unless // explicitly disabled. -#if defined(__NVPTX__) || defined(__AMDGPU__) || defined(__FAST_MATH__) || \ +#if defined(__NVPTX__) || defined(__AMDGPU__) || defined(__SPIRV__) || defined(__FAST_MATH__) || \ defined(__NO_MATH_ERRNO__) #define __LIBC_SUPPORTS_MATH_ERRNO 0 #else diff --git a/libc/include/llvm-libc-macros/signal-macros.h b/libc/include/llvm-libc-macros/signal-macros.h index fbe929a0fea25..163c8742593b8 100644 --- a/libc/include/llvm-libc-macros/signal-macros.h +++ b/libc/include/llvm-libc-macros/signal-macros.h @@ -11,7 +11,7 @@ #if defined(__linux__) #include "linux/signal-macros.h" -#elif defined(__NVPTX__) || defined(__AMDGPU__) +#elif defined(__NVPTX__) || defined(__AMDGPU__) || defined(__SPIRV__) #include "gpu/signal-macros.h" #endif diff --git a/libc/include/llvm-libc-macros/time-macros.h b/libc/include/llvm-libc-macros/time-macros.h index c026df29b1e7f..d00d5e051d757 100644 --- a/libc/include/llvm-libc-macros/time-macros.h +++ b/libc/include/llvm-libc-macros/time-macros.h @@ -1,7 +1,7 @@ #ifndef LLVM_LIBC_MACROS_TIME_MACROS_H #define LLVM_LIBC_MACROS_TIME_MACROS_H -#if defined(__AMDGPU__) || defined(__NVPTX__) +#if defined(__AMDGPU__) || defined(__NVPTX__) || defined(__SPIRV__) #include "gpu/time-macros.h" #elif defined(__linux__) #include "linux/time-macros.h" diff --git a/libc/include/llvm-libc-types/fenv_t.h b/libc/include/llvm-libc-types/fenv_t.h index c83f23894c0c8..2cfeff7b8a9f8 100644 --- a/libc/include/llvm-libc-types/fenv_t.h +++ b/libc/include/llvm-libc-types/fenv_t.h @@ -25,7 +25,7 @@ typedef struct { } fenv_t; #elif defined(__riscv) typedef unsigned int fenv_t; -#elif defined(__AMDGPU__) || defined(__NVPTX__) +#elif defined(__AMDGPU__) || defined(__NVPTX__) || defined(__SPIRV__) typedef struct { unsigned int __fpc; } fenv_t; diff --git a/libc/shared/rpc_util.h b/libc/shared/rpc_util.h index 687814b7ff2ae..1785920ec2504 100644 --- a/libc/shared/rpc_util.h +++ b/libc/shared/rpc_util.h @@ -12,7 +12,7 @@ #include <stddef.h> #include <stdint.h> -#if (defined(__NVPTX__) || defined(__AMDGPU__)) && \ +#if (defined(__NVPTX__) || defined(__AMDGPU__) || defined(__SPIRV__)) && \ !((defined(__CUDA__) && !defined(__CUDA_ARCH__)) || \ (defined(__HIP__) && !defined(__HIP_DEVICE_COMPILE__))) #include <gpuintrin.h> diff --git a/libc/src/__support/macros/properties/architectures.h b/libc/src/__support/macros/properties/architectures.h index 21e9bc4288cd7..f05fe9e18e130 100644 --- a/libc/src/__support/macros/properties/architectures.h +++ b/libc/src/__support/macros/properties/architectures.h @@ -13,11 +13,15 @@ #define LIBC_TARGET_ARCH_IS_AMDGPU #endif +#if defined(__SPIRV__) +#define LIBC_TARGET_ARCH_IS_SPIRV +#endif + #if defined(__NVPTX__) #define LIBC_TARGET_ARCH_IS_NVPTX #endif -#if defined(LIBC_TARGET_ARCH_IS_NVPTX) || defined(LIBC_TARGET_ARCH_IS_AMDGPU) +#if defined(LIBC_TARGET_ARCH_IS_NVPTX) || defined(LIBC_TARGET_ARCH_IS_AMDGPU) || defined(LIBC_TARGET_ARCH_IS_SPIRV) #define LIBC_TARGET_ARCH_IS_GPU #endif diff --git a/libc/src/__support/macros/properties/cpu_features.h b/libc/src/__support/macros/properties/cpu_features.h index 1fe20d9b23a34..00129927263d6 100644 --- a/libc/src/__support/macros/properties/cpu_features.h +++ b/libc/src/__support/macros/properties/cpu_features.h @@ -91,13 +91,13 @@ #endif // LIBC_TARGET_CPU_HAS_RISCV_FPU_DOUBLE #endif // __riscv_flen -#if defined(__NVPTX__) || defined(__AMDGPU__) +#if defined(__NVPTX__) || defined(__AMDGPU__) || defined(__SPIRV__) #define LIBC_TARGET_CPU_HAS_FPU_FLOAT #define LIBC_TARGET_CPU_HAS_FPU_DOUBLE #endif #if defined(__ARM_FEATURE_FMA) || (defined(__AVX2__) && defined(__FMA__)) || \ - defined(__NVPTX__) || defined(__AMDGPU__) || defined(__riscv_flen) + defined(__NVPTX__) || defined(__AMDGPU__) || defined(__riscv_flen) || defined(__SPIRV__) #define LIBC_TARGET_CPU_HAS_FMA // Provide a more fine-grained control of FMA instruction for ARM targets. #if defined(LIBC_TARGET_CPU_HAS_FPU_HALF) diff --git a/libc/src/__support/time/gpu/time_utils.cpp b/libc/src/__support/time/gpu/time_utils.cpp index 38e09f600f369..bfdcb856153ee 100644 --- a/libc/src/__support/time/gpu/time_utils.cpp +++ b/libc/src/__support/time/gpu/time_utils.cpp @@ -11,7 +11,7 @@ namespace LIBC_NAMESPACE_DECL { -#if defined(LIBC_TARGET_ARCH_IS_AMDGPU) +#if defined(LIBC_TARGET_ARCH_IS_AMDGPU) || defined(__SPIRV__) // This is expected to be initialized by the runtime if the default value is // insufficient. // TODO: Once we have another use-case for this we should put it in a common diff --git a/libc/src/__support/time/gpu/time_utils.h b/libc/src/__support/time/gpu/time_utils.h index 315506c897dcf..343a3b968c1dc 100644 --- a/libc/src/__support/time/gpu/time_utils.h +++ b/libc/src/__support/time/gpu/time_utils.h @@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL { -#if defined(LIBC_TARGET_ARCH_IS_AMDGPU) +#if defined(LIBC_TARGET_ARCH_IS_AMDGPU) || defined(__SPIRV__) // AMDGPU does not have a single set frequency. Different architectures and // cards can have different values. The actualy frequency needs to be read from // the kernel driver and will be between 25 MHz and 100 MHz on most cards. All diff --git a/libc/src/math/spirv64/CMakeLists.txt b/libc/src/math/spirv64/CMakeLists.txt new file mode 100644 index 0000000000000..d05d519b74b4f --- /dev/null +++ b/libc/src/math/spirv64/CMakeLists.txt @@ -0,0 +1,303 @@ +add_entrypoint_object( + ceil + SRCS + ceil.cpp + HDRS + ../ceil.h +) + +add_entrypoint_object( + ceilf + SRCS + ceilf.cpp + HDRS + ../ceilf.h +) + +add_entrypoint_object( + copysign + SRCS + copysign.cpp + HDRS + ../copysign.h +) + +add_entrypoint_object( + copysignf + SRCS + copysignf.cpp + HDRS + ../copysignf.h +) + +add_entrypoint_object( + fabs + SRCS + fabs.cpp + HDRS + ../fabs.h +) + +add_entrypoint_object( + fabsf + SRCS + fabsf.cpp + HDRS + ../fabsf.h +) + +add_entrypoint_object( + floor + SRCS + floor.cpp + HDRS + ../floor.h +) + +add_entrypoint_object( + floorf + SRCS + floorf.cpp + HDRS + ../floorf.h +) + +add_entrypoint_object( + fma + SRCS + fma.cpp + HDRS + ../fma.h +) + +add_entrypoint_object( + fmaf + SRCS + fmaf.cpp + HDRS + ../fmaf.h +) + +add_entrypoint_object( + fmax + SRCS + fmax.cpp + HDRS + ../fmax.h +) + +add_entrypoint_object( + fmaxf + SRCS + fmaxf.cpp + HDRS + ../fmaxf.h +) + +add_entrypoint_object( + fmin + SRCS + fmin.cpp + HDRS + ../fmin.h +) + +add_entrypoint_object( + fminf + SRCS + fminf.cpp + HDRS + ../fminf.h +) + +add_entrypoint_object( + fmod + SRCS + fmod.cpp + HDRS + ../fmod.h +) + +add_entrypoint_object( + fmodf + SRCS + fmodf.cpp + HDRS + ../fmodf.h +) + +add_entrypoint_object( + nearbyint + SRCS + nearbyint.cpp + HDRS + ../nearbyint.h +) + +add_entrypoint_object( + nearbyintf + SRCS + nearbyintf.cpp + HDRS + ../nearbyintf.h +) + +add_entrypoint_object( + remainder + SRCS + remainder.cpp + HDRS + ../remainder.h +) + +add_entrypoint_object( + remainderf + SRCS + remainderf.cpp + HDRS + ../remainderf.h +) + +add_entrypoint_object( + rint + SRCS + rint.cpp + HDRS + ../rint.h +) + +add_entrypoint_object( + rintf + SRCS + rintf.cpp + HDRS + ../rintf.h +) + +add_entrypoint_object( + round + SRCS + round.cpp + HDRS + ../round.h +) + +add_entrypoint_object( + sqrt + SRCS + sqrt.cpp + HDRS + ../sqrt.h +) + +add_entrypoint_object( + sqrtf + SRCS + sqrtf.cpp + HDRS + ../sqrtf.h +) + +add_entrypoint_object( + trunc + SRCS + trunc.cpp + HDRS + ../trunc.h +) + +add_entrypoint_object( + truncf + SRCS + truncf.cpp + HDRS + ../truncf.h +) + +add_entrypoint_object( + frexp + SRCS + frexp.cpp + HDRS + ../frexp.h +) + +add_entrypoint_object( + frexpf + SRCS + frexpf.cpp + HDRS + ../frexpf.h +) + +add_entrypoint_object( + scalbn + SRCS + scalbn.cpp + HDRS + ../scalbn.h +) + +add_entrypoint_object( + scalbnf + SRCS + scalbnf.cpp + HDRS + ../scalbnf.h +) + +add_entrypoint_object( + ldexp + SRCS + ldexp.cpp + HDRS + ../ldexp.h +) + +add_entrypoint_object( + ldexpf + SRCS + ldexpf.cpp + HDRS + ../ldexpf.h +) + +add_entrypoint_object( + tgamma + SRCS + tgamma.cpp + HDRS + ../tgamma.h + COMPILE_OPTIONS + ${bitcode_link_flags} +) + +add_entrypoint_object( + tgammaf + SRCS + tgammaf.cpp + HDRS + ../tgammaf.h + COMPILE_OPTIONS + ${bitcode_link_flags} +) + +add_entrypoint_object( + lgamma + SRCS + lgamma.cpp + HDRS + ../lgamma.h + COMPILE_OPTIONS + ${bitcode_link_flags} +) + +add_entrypoint_object( + lgamma_r + SRCS + lgamma_r.cpp + HDRS + ../lgamma_r.h + COMPILE_OPTIONS + ${bitcode_link_flags} +) diff --git a/libc/src/math/spirv64/ceil.cpp b/libc/src/math/spirv64/ceil.cpp new file mode 100644 index 0000000000000..8834c7b560a1b --- /dev/null +++ b/libc/src/math/spirv64/ceil.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the ceil function for GPU -----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/ceil.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, ceil, (double x)) { return __builtin_ceil(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/ceilf.cpp b/libc/src/math/spirv64/ceilf.cpp new file mode 100644 index 0000000000000..5d26a30c849cd --- /dev/null +++ b/libc/src/math/spirv64/ceilf.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the ceilf function for GPU ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/ceilf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, ceilf, (float x)) { return __builtin_ceilf(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/copysign.cpp b/libc/src/math/spirv64/copysign.cpp new file mode 100644 index 0000000000000..06ef36fb3595f --- /dev/null +++ b/libc/src/math/spirv64/copysign.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the copysign function for GPU -------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/copysign.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, copysign, (double x, double y)) { + return __builtin_copysign(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/copysignf.cpp b/libc/src/math/spirv64/copysignf.cpp new file mode 100644 index 0000000000000..aea94f3577d8f --- /dev/null +++ b/libc/src/math/spirv64/copysignf.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the copysignf function for GPU ------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/copysignf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, copysignf, (float x, float y)) { + return __builtin_copysignf(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/fabs.cpp b/libc/src/math/spirv64/fabs.cpp new file mode 100644 index 0000000000000..bb37596b9d563 --- /dev/null +++ b/libc/src/math/spirv64/fabs.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the fabs function for GPU -----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/fabs.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, fabs, (double x)) { return __builtin_fabs(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/fabsf.cpp b/libc/src/math/spirv64/fabsf.cpp new file mode 100644 index 0000000000000..2698618f3f1e1 --- /dev/null +++ b/libc/src/math/spirv64/fabsf.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the fabsf function for GPU ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/fabsf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, fabsf, (float x)) { return __builtin_fabsf(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/floor.cpp b/libc/src/math/spirv64/floor.cpp new file mode 100644 index 0000000000000..564efa9a7da38 --- /dev/null +++ b/libc/src/math/spirv64/floor.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the floor function for GPU ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/floor.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, floor, (double x)) { return __builtin_floor(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/floorf.cpp b/libc/src/math/spirv64/floorf.cpp new file mode 100644 index 0000000000000..6717c8f60c992 --- /dev/null +++ b/libc/src/math/spirv64/floorf.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the floorf function for GPU ---------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/floorf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, floorf, (float x)) { return __builtin_floorf(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/fma.cpp b/libc/src/math/spirv64/fma.cpp new file mode 100644 index 0000000000000..c4a117e42a3aa --- /dev/null +++ b/libc/src/math/spirv64/fma.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the fma function for GPU ------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/fma.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, fma, (double x, double y, double z)) { + return __builtin_fma(x, y, z); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/fmaf.cpp b/libc/src/math/spirv64/fmaf.cpp new file mode 100644 index 0000000000000..c088bd5b30fea --- /dev/null +++ b/libc/src/math/spirv64/fmaf.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the fmaf function for GPU -----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/fmaf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, fmaf, (float x, float y, float z)) { + return __builtin_fmaf(x, y, z); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/fmax.cpp b/libc/src/math/spirv64/fmax.cpp new file mode 100644 index 0000000000000..474019733598f --- /dev/null +++ b/libc/src/math/spirv64/fmax.cpp @@ -0,0 +1,22 @@ +//===-- Implementation of the fmax function for GPU -----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/fmax.h" + +#include "src/__support/CPP/bit.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" +#include "src/__support/macros/optimization.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, fmax, (double x, double y)) { + return __builtin_fmax(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/fmaxf.cpp b/libc/src/math/spirv64/fmaxf.cpp new file mode 100644 index 0000000000000..59ee8259c025a --- /dev/null +++ b/libc/src/math/spirv64/fmaxf.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the fmaxf function for GPU ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/fmaxf.h" + +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, fmaxf, (float x, float y)) { + return __builtin_fmaxf(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/fmin.cpp b/libc/src/math/spirv64/fmin.cpp new file mode 100644 index 0000000000000..694eb664b599b --- /dev/null +++ b/libc/src/math/spirv64/fmin.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the fmin function for GPU -----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/fmin.h" + +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, fmin, (double x, double y)) { + return __builtin_fmin(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/fminf.cpp b/libc/src/math/spirv64/fminf.cpp new file mode 100644 index 0000000000000..2060b71b0841f --- /dev/null +++ b/libc/src/math/spirv64/fminf.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the fminf function for GPU ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/fminf.h" + +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, fminf, (float x, float y)) { + return __builtin_fminf(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/fmod.cpp b/libc/src/math/spirv64/fmod.cpp new file mode 100644 index 0000000000000..49d19c4decb96 --- /dev/null +++ b/libc/src/math/spirv64/fmod.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the fmod function for GPU -----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/fmod.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, fmod, (double x, double y)) { + return __builtin_fmod(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/fmodf.cpp b/libc/src/math/spirv64/fmodf.cpp new file mode 100644 index 0000000000000..8fbcb0cc2ad9e --- /dev/null +++ b/libc/src/math/spirv64/fmodf.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the fmodf function for GPU ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/fmodf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, fmodf, (float x, float y)) { + return __builtin_fmodf(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/frexp.cpp b/libc/src/math/spirv64/frexp.cpp new file mode 100644 index 0000000000000..4ae2b00198ec3 --- /dev/null +++ b/libc/src/math/spirv64/frexp.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the frexp function for GPU ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/frexp.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, frexp, (double x, int *p)) { + return __builtin_frexp(x, p); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/frexpf.cpp b/libc/src/math/spirv64/frexpf.cpp new file mode 100644 index 0000000000000..fd53f65701f59 --- /dev/null +++ b/libc/src/math/spirv64/frexpf.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the frexpf function for GPU ---------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/frexpf.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, frexpf, (float x, int *p)) { + return __builtin_frexpf(x, p); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/ldexp.cpp b/libc/src/math/spirv64/ldexp.cpp new file mode 100644 index 0000000000000..5b786a9b21e70 --- /dev/null +++ b/libc/src/math/spirv64/ldexp.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the ldexp function for GPU ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/ldexp.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, ldexp, (double x, int y)) { + return __builtin_ldexp(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/ldexpf.cpp b/libc/src/math/spirv64/ldexpf.cpp new file mode 100644 index 0000000000000..d3aa77fe592cb --- /dev/null +++ b/libc/src/math/spirv64/ldexpf.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the ldexpf function for GPU ---------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/ldexpf.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, ldexpf, (float x, int y)) { + return __builtin_ldexpf(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/lgamma.cpp b/libc/src/math/spirv64/lgamma.cpp new file mode 100644 index 0000000000000..2916ed5e77a5b --- /dev/null +++ b/libc/src/math/spirv64/lgamma.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the lgamma function for GPU ---------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/lgamma.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +extern "C" double __attribute__((overloadable)) __spirv_ocl_lgamma(double); +LLVM_LIBC_FUNCTION(double, lgamma, (double x)) { return __spirv_ocl_lgamma(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/lgamma_r.cpp b/libc/src/math/spirv64/lgamma_r.cpp new file mode 100644 index 0000000000000..2b01988d2a87f --- /dev/null +++ b/libc/src/math/spirv64/lgamma_r.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of the lgamma_r function for GPU -------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/lgamma_r.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +extern "C" double __attribute__((overloadable)) __spirv_ocl_lgamma_r(double, int*); +LLVM_LIBC_FUNCTION(double, lgamma_r, (double x, int *signp)) { + return __spirv_ocl_lgamma_r(x, signp); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/llrint.cpp b/libc/src/math/spirv64/llrint.cpp new file mode 100644 index 0000000000000..6e0f57a5a0387 --- /dev/null +++ b/libc/src/math/spirv64/llrint.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the llrint function for GPU ---------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/llrint.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(long long, llrint, (double x)) { + return static_cast<long long>(__builtin_rint(x)); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/llrintf.cpp b/libc/src/math/spirv64/llrintf.cpp new file mode 100644 index 0000000000000..d8de23fac3c8b --- /dev/null +++ b/libc/src/math/spirv64/llrintf.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the llrintf function for GPU --------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/llrintf.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(long long, llrintf, (float x)) { + return static_cast<long long>(__builtin_rintf(x)); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/lrint.cpp b/libc/src/math/spirv64/lrint.cpp new file mode 100644 index 0000000000000..5ba70ec890b2c --- /dev/null +++ b/libc/src/math/spirv64/lrint.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the lrint function for GPU ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/lrint.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(long, lrint, (double x)) { + return static_cast<long>(__builtin_rint(x)); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/lrintf.cpp b/libc/src/math/spirv64/lrintf.cpp new file mode 100644 index 0000000000000..1c985b01c2dc1 --- /dev/null +++ b/libc/src/math/spirv64/lrintf.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the lrintf function for GPU ---------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/lrintf.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(long, lrintf, (float x)) { + return static_cast<long>(__builtin_rintf(x)); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/nearbyint.cpp b/libc/src/math/spirv64/nearbyint.cpp new file mode 100644 index 0000000000000..7d78c7241d023 --- /dev/null +++ b/libc/src/math/spirv64/nearbyint.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the GPU nearbyint function ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/nearbyint.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, nearbyint, (double x)) { + return __builtin_nearbyint(x); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/nearbyintf.cpp b/libc/src/math/spirv64/nearbyintf.cpp new file mode 100644 index 0000000000000..4bd20dc58fb89 --- /dev/null +++ b/libc/src/math/spirv64/nearbyintf.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the GPU nearbyintf function ---------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/nearbyintf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, nearbyintf, (float x)) { + return __builtin_nearbyintf(x); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/remainder.cpp b/libc/src/math/spirv64/remainder.cpp new file mode 100644 index 0000000000000..9027204312e00 --- /dev/null +++ b/libc/src/math/spirv64/remainder.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the GPU remainder function ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/remainder.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, remainder, (double x, double y)) { + return __builtin_remainder(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/remainderf.cpp b/libc/src/math/spirv64/remainderf.cpp new file mode 100644 index 0000000000000..50df3b2ce25c1 --- /dev/null +++ b/libc/src/math/spirv64/remainderf.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of the GPU remainderf function ---------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/remainderf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, remainderf, (float x, float y)) { + return __builtin_remainderf(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/rint.cpp b/libc/src/math/spirv64/rint.cpp new file mode 100644 index 0000000000000..ac6837a4abc37 --- /dev/null +++ b/libc/src/math/spirv64/rint.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the GPU rint function ---------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/rint.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, rint, (double x)) { return __builtin_rint(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/rintf.cpp b/libc/src/math/spirv64/rintf.cpp new file mode 100644 index 0000000000000..94093471a8d92 --- /dev/null +++ b/libc/src/math/spirv64/rintf.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the GPU rintf function --------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/rintf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, rintf, (float x)) { return __builtin_rintf(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/round.cpp b/libc/src/math/spirv64/round.cpp new file mode 100644 index 0000000000000..0d2765f2e959f --- /dev/null +++ b/libc/src/math/spirv64/round.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the GPU round function --------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/round.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, round, (double x)) { return __builtin_round(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/roundf.cpp b/libc/src/math/spirv64/roundf.cpp new file mode 100644 index 0000000000000..86e8ba3ac83d6 --- /dev/null +++ b/libc/src/math/spirv64/roundf.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the GPU roundf function -------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/roundf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, roundf, (float x)) { return __builtin_roundf(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/scalbn.cpp b/libc/src/math/spirv64/scalbn.cpp new file mode 100644 index 0000000000000..ade0c40d8dc84 --- /dev/null +++ b/libc/src/math/spirv64/scalbn.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the GPU scalbn function -------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/scalbn.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, scalbn, (double x, int y)) { + return __builtin_ldexp(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/scalbnf.cpp b/libc/src/math/spirv64/scalbnf.cpp new file mode 100644 index 0000000000000..b5cb06f7bda0a --- /dev/null +++ b/libc/src/math/spirv64/scalbnf.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of the GPU scalbnf function ------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/scalbnf.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, scalbnf, (float x, int y)) { + return __builtin_ldexpf(x, y); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/sqrt.cpp b/libc/src/math/spirv64/sqrt.cpp new file mode 100644 index 0000000000000..ed83b6e5c6cae --- /dev/null +++ b/libc/src/math/spirv64/sqrt.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the GPU sqrt function ---------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/sqrt.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, sqrt, (double x)) { return __builtin_sqrt(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/sqrtf.cpp b/libc/src/math/spirv64/sqrtf.cpp new file mode 100644 index 0000000000000..851922c316452 --- /dev/null +++ b/libc/src/math/spirv64/sqrtf.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the GPU sqrtf function --------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/sqrtf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, sqrtf, (float x)) { return __builtin_sqrtf(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/tgamma.cpp b/libc/src/math/spirv64/tgamma.cpp new file mode 100644 index 0000000000000..49691a29b7ac7 --- /dev/null +++ b/libc/src/math/spirv64/tgamma.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of the GPU tgamma function -------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/tgamma.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +extern "C" double __attribute__((overloadable)) __spirv_ocl_tgamma(double); +LLVM_LIBC_FUNCTION(double, tgamma, (double x)) { + return __spirv_ocl_tgamma(x); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/tgammaf.cpp b/libc/src/math/spirv64/tgammaf.cpp new file mode 100644 index 0000000000000..1c9aa95a7250c --- /dev/null +++ b/libc/src/math/spirv64/tgammaf.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of the GPU tgammaf function ------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/tgammaf.h" +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +extern "C" float __attribute__((overloadable)) __spirv_ocl_tgamma(float); +LLVM_LIBC_FUNCTION(float, tgammaf, (float x)) { + return __spirv_ocl_tgamma(x); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/trunc.cpp b/libc/src/math/spirv64/trunc.cpp new file mode 100644 index 0000000000000..f60caa2a71d78 --- /dev/null +++ b/libc/src/math/spirv64/trunc.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the GPU trunc function --------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/trunc.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(double, trunc, (double x)) { return __builtin_trunc(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/spirv64/truncf.cpp b/libc/src/math/spirv64/truncf.cpp new file mode 100644 index 0000000000000..a6c9b8f188f02 --- /dev/null +++ b/libc/src/math/spirv64/truncf.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of the GPU truncf function -------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/truncf.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, truncf, (float x)) { return __builtin_truncf(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/startup/gpu/CMakeLists.txt b/libc/startup/gpu/CMakeLists.txt index 63e2a6c5dee1a..f83e86819cad7 100644 --- a/libc/startup/gpu/CMakeLists.txt +++ b/libc/startup/gpu/CMakeLists.txt @@ -33,9 +33,17 @@ function(add_startup_object name) set_target_properties(${fq_target_name}.exe PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${LIBC_LIBRARY_DIR} RUNTIME_OUTPUT_NAME ${name}.o) - target_link_options(${fq_target_name}.exe PRIVATE - ${LIBC_COMPILE_OPTIONS_DEFAULT} - "-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm") + + if("${LLVM_DEFAULT_TARGET_TRIPLE}" MATCHES "^spirv" OR + "${CMAKE_CXX_COMPILER_TARGET}" MATCHES "^spirv") + target_link_options(${fq_target_name}.exe PRIVATE + ${LIBC_COMPILE_OPTIONS_DEFAULT} + "-nostdlib" "-emit-llvm") + else() + target_link_options(${fq_target_name}.exe PRIVATE + ${LIBC_COMPILE_OPTIONS_DEFAULT} + "-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm") + endif() endif() endfunction() diff --git a/libc/startup/gpu/spirv64/CMakeLists.txt b/libc/startup/gpu/spirv64/CMakeLists.txt new file mode 100644 index 0000000000000..f41ae8ce24a08 --- /dev/null +++ b/libc/startup/gpu/spirv64/CMakeLists.txt @@ -0,0 +1,16 @@ +add_startup_object( + crt1 + SRC + start.cpp + DEPENDS + libc.config.app_h + libc.src.__support.RPC.rpc_client + libc.src.__support.GPU.utils + libc.src.stdlib.exit + libc.src.stdlib.atexit + COMPILE_OPTIONS + -ffreestanding # To avoid compiler warnings about calling the main function. + -fno-builtin + -emit-llvm +) +get_fq_target_name(crt1 fq_name) diff --git a/libc/startup/gpu/spirv64/start.cpp b/libc/startup/gpu/spirv64/start.cpp new file mode 100644 index 0000000000000..780ec68c2a08a --- /dev/null +++ b/libc/startup/gpu/spirv64/start.cpp @@ -0,0 +1,72 @@ +//===-- Implementation of crt for amdgpu ----------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "config/gpu/app.h" +#include "src/__support/GPU/utils.h" +#include "src/__support/RPC/rpc_client.h" +#include "src/__support/macros/config.h" +#include "src/stdlib/atexit.h" +#include "src/stdlib/exit.h" + +extern "C" int main(int argc, char **argv, char **envp); +extern "C" void __cxa_finalize(void *dso); + +namespace LIBC_NAMESPACE_DECL { + +// FIXME: Touch this symbol to force this to be linked in statically. +volatile void *dummy = &LIBC_NAMESPACE::rpc::client; + +DataEnvironment app; + +extern "C" uintptr_t __init_array_start[]; +extern "C" uintptr_t __init_array_end[]; +extern "C" uintptr_t __fini_array_start[]; +extern "C" uintptr_t __fini_array_end[]; + +using InitCallback = void(int, char **, char **); +using FiniCallback = void(void); + +static void call_init_array_callbacks(int argc, char **argv, char **env) { + size_t init_array_size = __init_array_end - __init_array_start; + for (size_t i = 0; i < init_array_size; ++i) + reinterpret_cast<InitCallback *>(__init_array_start[i])(argc, argv, env); +} + +static void call_fini_array_callbacks() { + size_t fini_array_size = __fini_array_end - __fini_array_start; + for (size_t i = fini_array_size; i > 0; --i) + reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])(); +} + +} // namespace LIBC_NAMESPACE_DECL + +extern "C" [[gnu::visibility("protected"), clang::device_kernel]] void +_begin(int argc, char **argv, char **env) { + __atomic_store_n(&LIBC_NAMESPACE::app.env_ptr, + reinterpret_cast<uintptr_t *>(env), __ATOMIC_RELAXED); + // We want the fini array callbacks to be run after other atexit + // callbacks are run. So, we register them before running the init + // array callbacks as they can potentially register their own atexit + // callbacks. + LIBC_NAMESPACE::atexit(&LIBC_NAMESPACE::call_fini_array_callbacks); + LIBC_NAMESPACE::call_init_array_callbacks(argc, argv, env); +} + +extern "C" [[gnu::visibility("protected"), clang::device_kernel]] void +_start(int argc, char **argv, char **envp, int *ret) { + // Invoke the 'main' function with every active thread that the user launched + // the _start kernel with. + __atomic_fetch_or(ret, main(argc, argv, envp), __ATOMIC_RELAXED); +} + +extern "C" [[gnu::visibility("protected"), clang::device_kernel]] void +_end() { + // Only a single thread should call the destructors registred with 'atexit'. + // The loader utility will handle the actual exit and return code cleanly. + __cxa_finalize(nullptr); +} diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index 2e2c269c0abbc..1f88c84f2c60c 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -225,13 +225,14 @@ endif() foreach(_name ${LLVM_RUNTIME_TARGETS}) if("libc" IN_LIST RUNTIMES_${_name}_LLVM_ENABLE_RUNTIMES) - if("${_name}" STREQUAL "amdgcn-amd-amdhsa" OR "${_name}" STREQUAL "nvptx64-nvidia-cuda") + if("${_name}" STREQUAL "amdgcn-amd-amdhsa" OR "${_name}" STREQUAL "nvptx64-nvidia-cuda" OR "${_name}" STREQUAL "spirv64-intel-unknown") set(LLVM_LIBC_GPU_BUILD ON) endif() endif() endforeach() if("${LIBC_TARGET_TRIPLE}" STREQUAL "amdgcn-amd-amdhsa" OR - "${LIBC_TARGET_TRIPLE}" STREQUAL "nvptx64-nvidia-cuda") + "${LIBC_TARGET_TRIPLE}" STREQUAL "nvptx64-nvidia-cuda" OR + "${LIBC_TARGET_TRIPLE}" STREQUAL "spirv64-intel-unknown") set(LLVM_LIBC_GPU_BUILD ON) endif() _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
