[compiler-rt] [compiler-rt][profile] Use libc-headers for the GPU runtimes build (PR #192814)

Yaxun Liu via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 20 20:51:42 PDT 2026


https://github.com/yxsamliu updated https://github.com/llvm/llvm-project/pull/192814

>From fc52cfdc72b580b3bb2f646d806b9cc4294ec80e Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Mon, 20 Apr 2026 10:16:29 -0400
Subject: [PATCH] [compiler-rt][profile] Use runtimes-libc-headers in the GPU
 runtimes build

When compiler-rt is built for a GPU target in the same runtimes build
as LLVM-libc, the profile sources `#include <string.h>`, `<limits.h>`,
... Those headers are generated by LLVM-libc for the GPU triple.

A concrete example is the amdgcn runtimes build:

    -DLLVM_RUNTIME_TARGETS='default;amdgcn-amd-amdhsa'
    -DRUNTIMES_amdgcn-amd-amdhsa_LLVM_ENABLE_RUNTIMES='compiler-rt;libc'
    -DRUNTIMES_amdgcn-amd-amdhsa_RUNTIMES_USE_LIBC=llvm-libc

Even though `libc` is configured before `compiler-rt`, both sets of
targets live in the same ninja graph and race each other. Ninja can
start compiling `compiler-rt/lib/profile/InstrProfiling.c` before
LLVM-libc has finished generating its GPU `string.h`, and even when
hdrgen has finished, the profile compile needs `-isystem` pointing at
the generated header tree.

Use the runtimes-side libc-provider abstraction in
`runtimes/cmake/Modules/HandleLibC.cmake`:

  - link the profile target against `runtimes-libc-headers`,
    which is the same INTERFACE target `libcxx`, `libcxxabi` and
    `libunwind` already use for selecting the C library provider
    (`system`, `llvm-libc`, `picolibc`, `newlib`),
  - in the `llvm-libc` case it transitively pulls in
    LLVM-libc's `libc-headers` (`-isystem <build>/include/<triple>`),
    waits for `generate-libc-headers`, and adds `-nostdlibinc` so the
    profile compile does not consult host C library paths.

The dependency is added only when `LLVM_RUNTIMES_BUILD` is set,
`COMPILER_RT_GPU_BUILD` is on, and the sibling `runtimes-libc-headers`
target actually exists, so host compiler-rt builds, standalone
compiler-rt builds, and runtimes builds without LLVM-libc selected
are unchanged.

`add_compiler_rt_runtime` passes `LINK_LIBS` to
`target_link_libraries(<libname> PRIVATE ...)`, so the include path is
used when compiling the profile target itself but is not propagated to
downstream consumers of `libclang_rt.profile.a`.

Related: PR #177665.
---
 compiler-rt/lib/profile/CMakeLists.txt | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/compiler-rt/lib/profile/CMakeLists.txt b/compiler-rt/lib/profile/CMakeLists.txt
index 8c196d15841a4..ee39ab1bc1202 100644
--- a/compiler-rt/lib/profile/CMakeLists.txt
+++ b/compiler-rt/lib/profile/CMakeLists.txt
@@ -186,6 +186,18 @@ append_list_if(COMPILER_RT_HAS_WD4221_FLAG /wd4221 EXTRA_FLAGS)
 # Disable 'nonstandard extension used: translation unit is empty'.
 append_list_if(COMPILER_RT_HAS_WD4206_FLAG /wd4206 EXTRA_FLAGS)
 
+# In the GPU runtimes build, link `runtimes-libc-headers` for its
+# `-isystem` include dir and for waiting on header generation.
+set(PROFILE_LINK_LIBS)
+if(LLVM_RUNTIMES_BUILD)
+  list(APPEND CMAKE_MODULE_PATH
+       "${CMAKE_CURRENT_SOURCE_DIR}/../../../runtimes/cmake/Modules")
+  include(HandleLibC)
+  if(COMPILER_RT_GPU_BUILD AND TARGET runtimes-libc-headers)
+    list(APPEND PROFILE_LINK_LIBS runtimes-libc-headers)
+  endif()
+endif()
+
 if(APPLE)
   add_compiler_rt_runtime(clang_rt.profile
     STATIC
@@ -194,6 +206,7 @@ if(APPLE)
     CFLAGS ${EXTRA_FLAGS}
     SOURCES ${PROFILE_SOURCES}
     ADDITIONAL_HEADERS ${PROFILE_HEADERS}
+    LINK_LIBS ${PROFILE_LINK_LIBS}
     PARENT_TARGET profile)
 else()
   add_compiler_rt_runtime(clang_rt.profile
@@ -202,5 +215,6 @@ else()
     CFLAGS ${EXTRA_FLAGS}
     SOURCES ${PROFILE_SOURCES}
     ADDITIONAL_HEADERS ${PROFILE_HEADERS}
+    LINK_LIBS ${PROFILE_LINK_LIBS}
     PARENT_TARGET profile)
 endif()



More information about the llvm-commits mailing list