Skip to content

Commit 24a4f02

Browse files
committed
[libc] Disable -ffreestanding and -fno-builtin on the GPU build
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 594bc52 commit 24a4f02

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)
@@ -60,7 +62,19 @@ function(_get_common_compile_options output_var flags)
6062
list(APPEND compile_options "-ffixed-point")
6163
endif()
6264

63-
list(APPEND compile_options "-fno-builtin")
65+
# Builtin recognition causes issues when trying to implement the builtin
66+
# functions themselves. The GPU backends do not use libcalls so we disable
67+
# the known problematic ones. This allows inlining during LTO linking.
68+
if(LIBC_TARGET_OS_IS_GPU)
69+
set(libc_builtins bcmp strlen memmem bzero memcmp memcpy memmem memmove
70+
memset strcmp strstr)
71+
foreach(builtin ${libc_builtins})
72+
list(APPEND compile_options "-fno-builtin-${builtin}")
73+
endforeach()
74+
else()
75+
list(APPEND compile_options "-fno-builtin")
76+
endif()
77+
6478
list(APPEND compile_options "-fno-exceptions")
6579
list(APPEND compile_options "-fno-lax-vector-conversions")
6680
list(APPEND compile_options "-fno-unwind-tables")

0 commit comments

Comments
 (0)