[libc-commits] [libc] [libc] Suppress GCC's <mm_malloc.h> in freestanding builds (PR #234863)
via libc-commits
libc-commits at lists.llvm.org
Fri Oct 23 00:05:02 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Pavel Labath (labath)
<details>
<summary>Changes</summary>
Unlike Clang, GCC's <xmmintrin.h> (included via <immintrin.h>) unconditionally includes <mm_malloc.h> even with -ffreestanding, which in turn includes <stdlib.h>.
In full-build mode, targets that use x86 intrinsics (such as op_x86.h) don't declare a dependency on libc.include.stdlib (and they don't really need it). If these targets are built before our stdlib.h header is generated, this will end up including the system stdlib.h -- and fail miserably because of mixing of standard libraries. This can happen already, but the problem gets more acute if we remove (#<!-- -->234150) transitive full-header dependencies from the proxy headers (as it lets us build these targets much sooner).
Since we don't use _mm_malloc or _mm_free, I'm suppressing <mm_malloc.h> by defining its include guard (_MM_MALLOC_H_INCLUDED) on x86/x86_64 GCC full builds.
Assisted-by: Gemini
---
Full diff: https://github.com/llvm/llvm-project/pull/234863.diff
2 Files Affected:
- (modified) libc/cmake/modules/LLVMLibCCompileOptionRules.cmake (+7)
- (modified) libc/cmake/modules/LLVMLibCTestRules.cmake (+4)
``````````diff
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 98aab039a6362..dda44f23daf5b 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -242,6 +242,13 @@ function(_get_common_compile_options_impl output_var)
# Only add -ffreestanding flag in non-GPU full build mode.
if(NOT LIBC_TARGET_OS_IS_GPU)
list(APPEND compile_options "-ffreestanding")
+ # GCC's <xmmintrin.h> unconditionally includes <mm_malloc.h> even with
+ # -ffreestanding, which in turn includes <stdlib.h>. Suppress it by
+ # defining its header guard.
+ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
+ (LIBC_TARGET_ARCHITECTURE_IS_X86_64 OR LIBC_TARGET_ARCHITECTURE_IS_X86))
+ list(APPEND compile_options "-D_MM_MALLOC_H_INCLUDED")
+ endif()
endif()
list(APPEND compile_options "-DLIBC_FULL_BUILD")
# Manually disable standard include paths to prevent system headers from
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 1c7a4252a69d6..2cca0def2f2f4 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -53,6 +53,10 @@ function(_get_common_test_compile_options_impl output_var c_test)
list(APPEND compile_options "-DLIBC_FULL_BUILD")
# Only add -ffreestanding flag in full build mode.
list(APPEND compile_options "-ffreestanding")
+ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
+ (LIBC_TARGET_ARCHITECTURE_IS_X86_64 OR LIBC_TARGET_ARCHITECTURE_IS_X86))
+ list(APPEND compile_options "-D_MM_MALLOC_H_INCLUDED")
+ endif()
list(APPEND compile_options "-fno-exceptions")
list(APPEND compile_options "-fno-unwind-tables")
list(APPEND compile_options "-fno-asynchronous-unwind-tables")
``````````
</details>
https://github.com/llvm/llvm-project/pull/234863
More information about the libc-commits
mailing list