[clang] [clang][docs] Document that C library builtins may call the library (PR #245968)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 03:18:07 PST 2026
https://github.com/Glitch-On created https://github.com/llvm/llvm-project/pull/245968
Builtins such as __builtin_abort and __builtin_sqrt are named like compiler intrinsics, but they have the semantics of the C library function they are named after and may be compiled to a plain call to it. Users have assumed otherwise, for example expecting __builtin_sqrt to always become a hardware instruction without -fno-math-errno.
Add a section to LanguageExtensions.md that describes this, notes the math errno behaviour, and points to __builtin_memcpy_inline and __builtin_memset_inline for cases where no external call may be emitted.
Fixes #243983
>From 549b10f1dc5fb8898adbca62e98ee9126bba5bbb Mon Sep 17 00:00:00 2001
From: Vansh <gaurvansh133 at gmail.com>
Date: Wed, 25 Nov 2026 16:46:34 +0530
Subject: [PATCH] [clang][docs] Document that C library builtins may call the
library
Builtins such as __builtin_abort and __builtin_sqrt are named like compiler intrinsics, but they have the semantics of the C library function they are named after and may be compiled to a plain call to it. Users have assumed otherwise, for example expecting __builtin_sqrt to always become a hardware instruction without -fno-math-errno.
Add a section to LanguageExtensions.md that describes this, notes the math errno behaviour, and points to __builtin_memcpy_inline and __builtin_memset_inline for cases where no external call may be emitted.
Fixes #243983
---
clang/docs/LanguageExtensions.md | 33 ++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/clang/docs/LanguageExtensions.md b/clang/docs/LanguageExtensions.md
index 21299f45545e0..81243e13cfa3b 100644
--- a/clang/docs/LanguageExtensions.md
+++ b/clang/docs/LanguageExtensions.md
@@ -3167,6 +3167,39 @@ implemented directly in terms of {ref}`extended vector support
<langext-vectors>` instead of builtins, in order to reduce the number of
builtins that we need to implement.
+(langext-libbuiltins)=
+
+### Builtins for C library functions
+
+Many builtins are named after a C standard library function with a `__builtin_`
+prefix, for example `__builtin_abort`, `__builtin_printf`, `__builtin_sqrt`,
+`__builtin_memcpy` and `__builtin_strlen`. Despite the name, these are not
+guaranteed to be handled by the compiler. Each one has the same meaning as the
+library function it is named after, and Clang may compile a call to it as an
+ordinary call to that function.
+
+Clang may optimize these calls, for example by evaluating them at compile time,
+replacing a small `__builtin_memcpy` with inline loads and stores, or replacing
+`__builtin_sqrt` with a hardware instruction. Such optimizations are allowed only
+where they preserve the library function's semantics, and they are never
+guaranteed. In particular:
+
+- Your program may still need to link against a library that provides the
+ function. Use `__builtin_memcpy_inline` or `__builtin_memset_inline` if a call
+ to an external function must not be emitted.
+- Math builtins follow the same `errno` rules as their library functions. When
+ math functions may set `errno` (`-fmath-errno`, the default on many targets),
+ `__builtin_sqrt(x)` may still call `sqrt` so that `errno` can be set for a
+ negative argument. Compile with `-fno-math-errno` to let Clang use a hardware
+ instruction instead.
+
+Not every builtin with a `__builtin_` prefix works this way. Builtins such as
+`__builtin_expect`, `__builtin_unreachable` and `__builtin_popcount` have no
+library counterpart and are always handled by the compiler.
+
+See also GCC's documentation of
+[Builtins for C Library Functions](https://gcc.gnu.org/onlinedocs/gcc/Library-Builtins.html).
+
### `__builtin_alloca`
`__builtin_alloca` is used to dynamically allocate memory on the stack. Memory
More information about the cfe-commits
mailing list