[libc-commits] [libc] ab3d742 - [libc] Add basic support for building SPIR-V libraries (#181049)
via libc-commits
libc-commits at lists.llvm.org
Mon Feb 16 18:27:44 PST 2026
Author: fineg74
Date: 2026-02-16T20:27:39-06:00
New Revision: ab3d742f6f3f78533a208788ae00c019bbc13b33
URL: https://github.com/llvm/llvm-project/commit/ab3d742f6f3f78533a208788ae00c019bbc13b33
DIFF: https://github.com/llvm/llvm-project/commit/ab3d742f6f3f78533a208788ae00c019bbc13b33.diff
LOG: [libc] Add basic support for building SPIR-V libraries (#181049)
This is to add support to build libc for building with spirv backend,
for use with OpenMP kernels
Added:
libc/config/gpu/spirv/entrypoints.txt
libc/config/gpu/spirv/headers.txt
Modified:
libc/cmake/modules/LLVMLibCArchitectures.cmake
libc/cmake/modules/LLVMLibCLibraryRules.cmake
libc/include/llvm-libc-macros/math-macros.h
libc/include/llvm-libc-macros/signal-macros.h
libc/include/llvm-libc-macros/time-macros.h
libc/include/llvm-libc-types/fenv_t.h
libc/shared/rpc_util.h
libc/src/__support/macros/properties/architectures.h
libc/src/__support/macros/properties/cpu_features.h
libc/src/__support/time/gpu/time_utils.cpp
libc/src/__support/time/gpu/time_utils.h
llvm/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/cmake/modules/LLVMLibCArchitectures.cmake b/libc/cmake/modules/LLVMLibCArchitectures.cmake
index 939fc1226a4e9..94c978a458a6d 100644
--- a/libc/cmake/modules/LLVMLibCArchitectures.cmake
+++ b/libc/cmake/modules/LLVMLibCArchitectures.cmake
@@ -51,8 +51,8 @@ function(get_arch_and_system_from_triple triple arch_var sys_var)
set(target_arch "amdgpu")
elseif(target_arch MATCHES "^nvptx64")
set(target_arch "nvptx")
- elseif(target_arch MATCHES "^spirv64")
- set(target_arch "spirv64")
+ elseif(target_arch MATCHES "^spirv")
+ set(target_arch "spirv")
else()
return()
endif()
@@ -71,7 +71,7 @@ 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 "^spirv")
set(target_sys "gpu")
endif()
@@ -181,7 +181,7 @@ elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "amdgpu")
set(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU TRUE)
elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "nvptx")
set(LIBC_TARGET_ARCHITECTURE_IS_NVPTX TRUE)
-elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "spirv64")
+elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "spirv")
set(LIBC_TARGET_ARCHITECTURE_IS_SPIRV TRUE)
else()
message(FATAL_ERROR
diff --git a/libc/cmake/modules/LLVMLibCLibraryRules.cmake b/libc/cmake/modules/LLVMLibCLibraryRules.cmake
index 80439de6bb4b0..9f8c6c0db8c6f 100644
--- a/libc/cmake/modules/LLVMLibCLibraryRules.cmake
+++ b/libc/cmake/modules/LLVMLibCLibraryRules.cmake
@@ -109,8 +109,13 @@ 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(LIBC_TARGET_ARCHITECTURE_IS_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/spirv/entrypoints.txt b/libc/config/gpu/spirv/entrypoints.txt
new file mode 100644
index 0000000000000..5c3c2c7e4303a
--- /dev/null
+++ b/libc/config/gpu/spirv/entrypoints.txt
@@ -0,0 +1,100 @@
+set(TARGET_LIBC_ENTRYPOINTS
+ # 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
+
+)
+
+set(TARGET_LIBM_ENTRYPOINTS
+)
+
+if(LIBC_TYPES_HAS_FLOAT16)
+ list(APPEND TARGET_LIBM_ENTRYPOINTS
+ )
+endif()
+
+list(APPEND TARGET_LIBM_ENTRYPOINTS
+)
+
+set(TARGET_LLVMLIBC_ENTRYPOINTS
+ ${TARGET_LIBC_ENTRYPOINTS}
+ ${TARGET_LIBM_ENTRYPOINTS}
+)
diff --git a/libc/config/gpu/spirv/headers.txt b/libc/config/gpu/spirv/headers.txt
new file mode 100644
index 0000000000000..ae5e0de66c7a2
--- /dev/null
+++ b/libc/config/gpu/spirv/headers.txt
@@ -0,0 +1,5 @@
+set(TARGET_PUBLIC_HEADERS
+ libc.include.ctype
+ libc.include.string
+ libc.include.strings
+)
diff --git a/libc/include/llvm-libc-macros/math-macros.h b/libc/include/llvm-libc-macros/math-macros.h
index e1b12e3010fe9..10d15b7e0f8cd 100644
--- a/libc/include/llvm-libc-macros/math-macros.h
+++ b/libc/include/llvm-libc-macros/math-macros.h
@@ -44,8 +44,8 @@
// Math error handling. Target support is assumed to be existent unless
// explicitly disabled.
-#if defined(__NVPTX__) || defined(__AMDGPU__) || defined(__FAST_MATH__) || \
- defined(__NO_MATH_ERRNO__)
+#if defined(__NVPTX__) || defined(__AMDGPU__) || defined(__SPIRV__) || \
+ defined(__FAST_MATH__) || defined(__NO_MATH_ERRNO__)
#define __LIBC_SUPPORTS_MATH_ERRNO 0
#else
#define __LIBC_SUPPORTS_MATH_ERRNO 1
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 c984c5eab4f50..44eb475919947 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..f73fa7a7643ab 100644
--- a/libc/src/__support/macros/properties/architectures.h
+++ b/libc/src/__support/macros/properties/architectures.h
@@ -13,11 +13,16 @@
#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..ce676d01577fb 100644
--- a/libc/src/__support/macros/properties/cpu_features.h
+++ b/libc/src/__support/macros/properties/cpu_features.h
@@ -91,13 +91,14 @@
#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
diff erent 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/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()
More information about the libc-commits
mailing list