[libc-commits] [libc] ffea91b - [libc] Suppress GCC's <mm_malloc.h> in freestanding builds (#234863)

via libc-commits libc-commits at lists.llvm.org
Fri Oct 23 12:59:00 PDT 2026


Author: Pavel Labath
Date: 2026-10-23T15:58:55-04:00
New Revision: ffea91bfcff6ba858661698311c66dd8618527ce

URL: https://github.com/llvm/llvm-project/commit/ffea91bfcff6ba858661698311c66dd8618527ce
DIFF: https://github.com/llvm/llvm-project/commit/ffea91bfcff6ba858661698311c66dd8618527ce.diff

LOG: [libc] Suppress GCC's <mm_malloc.h> in freestanding builds (#234863)

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

Added: 
    

Modified: 
    libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
    libc/cmake/modules/LLVMLibCTestRules.cmake

Removed: 
    


################################################################################
diff  --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 98aab039a6362..b26c1957880a5 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 4093a37e5c083..e9cd18ad07604 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -63,6 +63,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")


        


More information about the libc-commits mailing list