[libc-commits] [libc] [libc] Suppress GCC's <mm_malloc.h> in freestanding builds (PR #234863)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Fri Oct 23 00:15:21 PDT 2026
https://github.com/labath updated https://github.com/llvm/llvm-project/pull/234863
>From da75f00a46be65894639bb9c504a18c87c9e9520 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Fri, 23 Oct 2026 07:01:27 +0000
Subject: [PATCH 1/2] [libc] Suppress GCC's <mm_malloc.h> in freestanding
builds
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
---
libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 7 +++++++
libc/cmake/modules/LLVMLibCTestRules.cmake | 4 ++++
2 files changed, 11 insertions(+)
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 98aab039a63621..dda44f23daf5bc 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 1c7a4252a69d6b..2cca0def2f2f48 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")
>From 211afcae4815c87db924b3d55a1a762d8dd0655e Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Fri, 23 Oct 2026 07:14:39 +0000
Subject: [PATCH 2/2] format cmake
---
libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 2 +-
libc/cmake/modules/LLVMLibCTestRules.cmake | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index dda44f23daf5bc..b26c1957880a5f 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -246,7 +246,7 @@ function(_get_common_compile_options_impl output_var)
# -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))
+ (LIBC_TARGET_ARCHITECTURE_IS_X86_64 OR LIBC_TARGET_ARCHITECTURE_IS_X86))
list(APPEND compile_options "-D_MM_MALLOC_H_INCLUDED")
endif()
endif()
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 2cca0def2f2f48..fccf6aa8524c37 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -54,7 +54,7 @@ function(_get_common_test_compile_options_impl output_var c_test)
# 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))
+ (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")
More information about the libc-commits
mailing list