Skip to content

[libc] Tweak the runtimes cross-build for GPU#178548

Merged
jhuber6 merged 4 commits intollvm:mainfrom
jhuber6:CMakeFixes
Feb 3, 2026
Merged

[libc] Tweak the runtimes cross-build for GPU#178548
jhuber6 merged 4 commits intollvm:mainfrom
jhuber6:CMakeFixes

Conversation

@jhuber6
Copy link
Copy Markdown
Contributor

@jhuber6 jhuber6 commented Jan 29, 2026

Summary:
We should likely use -DLLVM_DEFAULT_TARGET_TRIPLE as the general
source of truth, make the handling work with that since we use it for
the output directories. Fix the creation of startup files in this mode
and make sure it can detect the GPU properly.

Fixes: #179375

@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Jan 29, 2026

@llvm/pr-subscribers-libc

Author: Joseph Huber (jhuber6)

Changes

Summary:
We should likely use -DLLVM_DEFAULT_TARGET_TRIPLE as the general
source of truth, make the handling work with that since we use it for
the output directories. Fix the creation of startup files in this mode
and make sure it can detect the GPU properly.


Full diff: https://github.com/llvm/llvm-project/pull/178548.diff

4 Files Affected:

  • (modified) libc/cmake/modules/LLVMLibCArchitectures.cmake (+2)
  • (modified) libc/cmake/modules/prepare_libc_gpu_build.cmake (+2-2)
  • (modified) libc/docs/gpu/building.rst (+6-34)
  • (modified) libc/startup/gpu/CMakeLists.txt (+1)
diff --git a/libc/cmake/modules/LLVMLibCArchitectures.cmake b/libc/cmake/modules/LLVMLibCArchitectures.cmake
index 939fc1226a4e9..3c28158d5219f 100644
--- a/libc/cmake/modules/LLVMLibCArchitectures.cmake
+++ b/libc/cmake/modules/LLVMLibCArchitectures.cmake
@@ -108,6 +108,8 @@ if(LLVM_RUNTIMES_TARGET)
   set(explicit_target_triple ${LLVM_RUNTIMES_TARGET})
 elseif(LIBC_TARGET_TRIPLE)
   set(explicit_target_triple ${LIBC_TARGET_TRIPLE})
+elseif(LLVM_DEFAULT_TARGET_TRIPLE)
+  set(explicit_target_triple ${LLVM_DEFAULT_TARGET_TRIPLE})
 endif()
 
 # The libc's target architecture and OS are set to match the compiler's default
diff --git a/libc/cmake/modules/prepare_libc_gpu_build.cmake b/libc/cmake/modules/prepare_libc_gpu_build.cmake
index 4d12a5917a56f..55cd0242eedeb 100644
--- a/libc/cmake/modules/prepare_libc_gpu_build.cmake
+++ b/libc/cmake/modules/prepare_libc_gpu_build.cmake
@@ -17,8 +17,8 @@ if(NOT LLVM_LIBC_FULL_BUILD)
 endif()
 
 # Set the required flags globally so standard CMake utilities can compile.
-if(LIBC_TARGET_TRIPLE)
-  set(CMAKE_REQUIRED_FLAGS "--target=${LIBC_TARGET_TRIPLE}")
+if(NOT CMAKE_CXX_COMPILER_TARGET)
+  set(CMAKE_REQUIRED_FLAGS "${LIBC_COMPILE_OPTIONS_DEFAULT}")
 endif()
 
 # Optionally set up a job pool to limit the number of GPU tests run in parallel.
diff --git a/libc/docs/gpu/building.rst b/libc/docs/gpu/building.rst
index 9f9528b30d9bf..a7791331423dc 100644
--- a/libc/docs/gpu/building.rst
+++ b/libc/docs/gpu/building.rst
@@ -100,12 +100,12 @@ targeting a GPU architecture.
   $> TARGET_C_COMPILER=</path/to/clang>
   $> TARGET_CXX_COMPILER=</path/to/clang++>
   $> cmake ../runtimes \ # Point to the runtimes build
-     -G Ninja                                  \
-     -DLLVM_ENABLE_RUNTIMES=libc               \
-     -DCMAKE_C_COMPILER=$TARGET_C_COMPILER     \
-     -DCMAKE_CXX_COMPILER=$TARGET_CXX_COMPILER \
-     -DLLVM_LIBC_FULL_BUILD=ON                 \
-     -DLLVM_RUNTIMES_TARGET=$TARGET_TRIPLE     \
+     -G Ninja                                    \
+     -DLLVM_ENABLE_RUNTIMES=libc                 \
+     -DCMAKE_C_COMPILER=$TARGET_C_COMPILER       \
+     -DCMAKE_CXX_COMPILER=$TARGET_CXX_COMPILER   \
+     -DLLVM_LIBC_FULL_BUILD=ON                   \
+     -DLLVM_DEFAULT_TARGET_TRIPLE=$TARGET_TRIPLE \
      -DCMAKE_BUILD_TYPE=Release
   $> ninja install
 
@@ -113,34 +113,6 @@ The above steps will result in a build targeting one of the supported GPU
 architectures. Building for multiple targets requires separate CMake
 invocations.
 
-Standalone cross build
-----------------------
-
-The GPU build can also be targeted directly as long as the compiler used is a
-supported ``clang`` compiler. This method is generally not recommended as it can
-only target a single GPU architecture.
-
-.. code-block:: sh
-
-  $> cd llvm-project  # The llvm-project checkout
-  $> mkdir build # A different build directory for the build tools
-  $> cd build
-  $> CLANG_C_COMPILER=</path/to/clang> # Must be a trunk build
-  $> CLANG_CXX_COMPILER=</path/to/clang++> # Must be a trunk build
-  $> TARGET_TRIPLE=<amdgcn-amd-amdhsa or nvptx64-nvidia-cuda>
-  $> cmake ../llvm \ # Point to the llvm directory
-     -G Ninja                                 \
-     -DLLVM_ENABLE_PROJECTS=libc              \
-     -DCMAKE_C_COMPILER=$CLANG_C_COMPILER     \
-     -DCMAKE_CXX_COMPILER=$CLANG_CXX_COMPILER \
-     -DLLVM_LIBC_FULL_BUILD=ON                \
-     -DLIBC_TARGET_TRIPLE=$TARGET_TRIPLE      \
-     -DCMAKE_BUILD_TYPE=Release
-  $> ninja install
-
-This will build and install the GPU C library along with all the other LLVM
-libraries.
-
 Build overview
 ==============
 
diff --git a/libc/startup/gpu/CMakeLists.txt b/libc/startup/gpu/CMakeLists.txt
index fa326ef46a9d1..63e2a6c5dee1a 100644
--- a/libc/startup/gpu/CMakeLists.txt
+++ b/libc/startup/gpu/CMakeLists.txt
@@ -34,6 +34,7 @@ function(add_startup_object name)
       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")
   endif()
 endfunction()

Summary:
We should likely use `-DLLVM_DEFAULT_TARGET_TRIPLE` as the general
source of truth, make the handling work with that since we use it for
the output directories. Fix the creation of startup files in this mode
and make sure it can detect the GPU properly.
@jhuber6
Copy link
Copy Markdown
Contributor Author

jhuber6 commented Jan 29, 2026

Anyone know why the Ubuntu CI is failing with this?

 FAILED: [code=1] libc/test/src/string/memory_utils/CMakeFiles/libc.test.src.string.memory_utils.utils_test.__unit__.__build__.dir/op_tests.cpp.o 
sccache /usr/bin/clang++ -DLIBC_NAMESPACE=__llvm_libc_23_0_0_git -D_DEBUG -I/home/runner/work/llvm-project/llvm-project/libc -I/home/runner/work/llvm-project/llvm-project/build/libc -isystem /home/runner/work/llvm-project/llvm-project/build/libc/include -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wno-pass-failed -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -std=gnu++17 --target=x86_64-unknown-linux-gnu -DLIBC_QSORT_IMPL=LIBC_QSORT_QUICK_SORT -DLIBC_COPT_STRING_LENGTH_IMPL=clang_vector -DLIBC_COPT_FIND_FIRST_CHARACTER_IMPL=word -DLIBC_ADD_NULL_CHECKS -DLIBC_ERRNO_MODE=LIBC_ERRNO_MODE_DEFAULT -DLIBC_THREAD_MODE=LIBC_THREAD_MODE_PLATFORM -DLIBC_CONF_WCTYPE_MODE=LIBC_WCTYPE_MODE_ASCII -DLIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT=100 -fpie -Wall -Wextra -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wno-c99-extensions -Wno-gnu-imaginary-constant -Wno-pedantic -Wstrict-prototypes -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wthread-safety -DLIBC_TEST=UNIT -MD -MT libc/test/src/string/memory_utils/CMakeFiles/libc.test.src.string.memory_utils.utils_test.__unit__.__build__.dir/op_tests.cpp.o -MF libc/test/src/string/memory_utils/CMakeFiles/libc.test.src.string.memory_utils.utils_test.__unit__.__build__.dir/op_tests.cpp.o.d -o libc/test/src/string/memory_utils/CMakeFiles/libc.test.src.string.memory_utils.utils_test.__unit__.__build__.dir/op_tests.cpp.o -c /home/runner/work/llvm-project/llvm-project/libc/test/src/string/memory_utils/op_tests.cpp
In file included from /home/runner/work/llvm-project/llvm-project/libc/test/src/string/memory_utils/op_tests.cpp:15:
/home/runner/work/llvm-project/llvm-project/libc/src/string/memory_utils/op_generic.h:418:17: error: static assertion failed due to requirement 'is_element_type_v<__attribute__((__vector_size__(2 * sizeof(long long)))) long long>'
  418 |   static_assert(is_element_type_v<T>);
      |                 ^~~~~~~~~~~~~~~~~~~~
/home/runner/work/llvm-project/llvm-project/libc/test/src/string/memory_utils/op_tests.cpp:335:28: note: in instantiation of template class '__llvm_libc_23_0_0_git::generic::Memcmp<__attribute__((__vector_size__(2 * sizeof(long long)))) long long>' requested here
  335 |   constexpr size_t kSize = Impl::SIZE;
      |                            ^
/home/runner/work/llvm-project/llvm-project/libc/test/UnitTest/LibcTest.h:306:8: note: in instantiation of member function '__llvm_libc_23_0_0_git::LlvmLibcOpTest_Memcmp<__llvm_libc_23_0_0_git::generic::Memcmp<__attribute__((__vector_size__(2 * sizeof(long long)))) long long>>::Run' requested here
  306 | struct TestCreator<TemplatedTestClass, Head, Tail...>
      |        ^
/home/runner/work/llvm-project/llvm-project/libc/test/src/string/memory_utils/op_tests.cpp:335:32: error: incomplete definition of type '__llvm_libc_23_0_0_git::generic::Memcmp<__attribute__((__vector_size__(2 * sizeof(long long)))) long long>'
  335 |   constexpr size_t kSize = Impl::SIZE;
      |                            ~~~~^~
/home/runner/work/llvm-project/llvm-project/libc/test/UnitTest/LibcTest.h:306:8: note: in instantiation of member function '__llvm_libc_23_0_0_git::LlvmLibcOpTest_Memcmp<__llvm_libc_23_0_0_git::generic::Memcmp<__attribute__((__vector_size__(2 * sizeof(long long)))) long long>>::Run' requested here
  306 | struct TestCreator<TemplatedTestClass, Head, Tail...>
      |        ^
/home/runner/work/llvm-project/llvm-project/libc/test/src/string/memory_utils/op_tests.cpp:337:68: error: no member named 'block' in '__llvm_libc_23_0_0_git::generic::Memcmp<__attribute__((__vector_size__(2 * sizeof(long long)))) long long>'
  337 |     static constexpr auto BlockImpl = CmpBlockAdaptor<kSize, Impl::block>;
      |     

It doesn't look related.

@jhuber6
Copy link
Copy Markdown
Contributor Author

jhuber6 commented Feb 2, 2026

Okay, guess it was related. Can someone review this and I can investigate building off of LLVM_DEFAULT_TARGET_TRIPLE later.

@jhuber6 jhuber6 merged commit e07a118 into llvm:main Feb 3, 2026
27 checks passed
@jhuber6 jhuber6 added this to the LLVM 22.x Release milestone Feb 3, 2026
@jhuber6
Copy link
Copy Markdown
Contributor Author

jhuber6 commented Feb 3, 2026

/cherry-pick e07a118

@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Feb 3, 2026

/pull-request #179496

c-rhodes pushed a commit to llvmbot/llvm-project that referenced this pull request Feb 4, 2026
Summary:
We should likely use `-DLLVM_DEFAULT_TARGET_TRIPLE` as the general
source of truth, make the handling work with that since we use it for
the output directories. Fix the creation of startup files in this mode
and make sure it can detect the GPU properly.

Fixes: llvm#179375
(cherry picked from commit e07a118)
rishabhmadan19 pushed a commit to rishabhmadan19/llvm-project that referenced this pull request Feb 9, 2026
Summary:
We should likely use `-DLLVM_DEFAULT_TARGET_TRIPLE` as the general
source of truth, make the handling work with that since we use it for
the output directories. Fix the creation of startup files in this mode
and make sure it can detect the GPU properly.

Fixes: llvm#179375
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Development

Successfully merging this pull request may close these issues.

[Offload] libc-for-gpu does not support Rust's bootstrap setup

3 participants