Skip to content

Commit 221d5c5

Browse files
authored
[libc] Disable -ffreestanding and -fno-builtin on the GPU build (#97636)
Summary: This patch removed the `-ffreestanding` and `-fno-builtin` flags from the publically installed version of the GPU library. The presense of `-fno-builtin` caused issues that prevented all inlining in the GPU C library, see https://discourse.llvm.org/t/rfc-libc-ffreestanding-fno-builtin/75883. Previously, I attempted to fix this by loosening the restriction that `"no-builtins"` functions cannot be inlined into functions without that attribute. However, that opened up a lot of extra issues that stalled that approach. This patch instead removes that and instead passes `-fno-builtin-<xyz>` for the few calls that are known to be problematic. I believe this works in general as the GPU backends do not emit any libcalls and the implementations of most of these simply reduce to builtins right now. This is a very useful patch as we can now actually inline calls.
1 parent 2cba218 commit 221d5c5

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

libc/cmake/modules/LLVMLibCCompileOptionRules.cmake

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ function(_get_common_compile_options output_var flags)
4343
list(APPEND compile_options "-fpie")
4444

4545
if(LLVM_LIBC_FULL_BUILD)
46+
# Only add -ffreestanding flag in non-GPU full build mode.
47+
if(NOT LIBC_TARGET_OS_IS_GPU)
48+
list(APPEND compile_options "-ffreestanding")
49+
endif()
4650
list(APPEND compile_options "-DLIBC_FULL_BUILD")
47-
# Only add -ffreestanding flag in full build mode.
48-
list(APPEND compile_options "-ffreestanding")
4951
# Manually disable standard include paths to prevent system headers from
5052
# being included.
5153
if(LIBC_CC_SUPPORTS_NOSTDLIBINC)
@@ -68,7 +70,19 @@ function(_get_common_compile_options output_var flags)
6870
list(APPEND compile_options "-ffixed-point")
6971
endif()
7072

71-
list(APPEND compile_options "-fno-builtin")
73+
# Builtin recognition causes issues when trying to implement the builtin
74+
# functions themselves. The GPU backends do not use libcalls so we disable
75+
# the known problematic ones. This allows inlining during LTO linking.
76+
if(LIBC_TARGET_OS_IS_GPU)
77+
set(libc_builtins bcmp strlen memmem bzero memcmp memcpy memmem memmove
78+
memset strcmp strstr)
79+
foreach(builtin ${libc_builtins})
80+
list(APPEND compile_options "-fno-builtin-${builtin}")
81+
endforeach()
82+
else()
83+
list(APPEND compile_options "-fno-builtin")
84+
endif()
85+
7286
list(APPEND compile_options "-fno-exceptions")
7387
list(APPEND compile_options "-fno-lax-vector-conversions")
7488
list(APPEND compile_options "-fno-unwind-tables")

0 commit comments

Comments
 (0)