(implementation_guidelines)=
{ref}code_style covers naming, macros, and formatting. This page covers the judgment calls that come up while writing an implementation: what to optimize for, how to use memory, when to check inputs, and how to handle headers. {ref}patterns_to_avoid lists the mistakes and outdated patterns that reviewers see most often.
When goals conflict, prefer them in this order:
undefined_behavior for what to do where the standard is silent.src/__support/ctype_utils.h are long, and anyone can see what they do.The memory functions and the math library are exceptions, where performance is an explicit goal alongside correctness.
Microbenchmarks are useful for finding a direction but are not a goal in and of themselves. The ideal performance measurement is from sampling real systems, though that‘s impractical for most changes. The project does not have a settled benchmarking setup today. LLVM’s benchmarking tips describe how to take more meaningful measurements.
code_style and handle failure.LLVM-libc does not aim to provide its own general-purpose allocator for hosted systems. On Linux the supported allocator is Scudo, and users with specific performance requirements are expected to bring the allocator they already prefer.
Scudo is not used on baremetal targets. It is designed for hosted systems and gets its memory from the operating system by mapping pages. A baremetal target has no operating system to ask, and usually has a small, fixed region of memory. On baremetal targets LLVM-libc instead provides an optimized allocator in src/__support/freelist_heap.h, which manages the memory between two symbols that the linker script provides.
LLVM-libc is written in C++. Assembly is used only in the few places where the stack or individual registers must be controlled exactly, or where an instruction has no compiler builtin. Assembly written for performance is not accepted, because it gives up what the compiler provides for C++ code: diagnostics, sanitizers, coverage and profile instrumentation, and debug information. {ref}assembly_policy has the full policy, including how patches that contain assembly are reviewed.
LIBC_CRASH_ON_NULLPTR (src/__support/macros/null_check.h). The check is lightweight, is controlled by the LIBC_ADD_NULL_CHECKS option, and exists to make a crash easy to diagnose. Put it in the entrypoint only, not in internal code. Do not add the check for a pointer that the libc only passes on to the kernel without dereferencing it. The kernel checks such pointers safely and returns EFAULT for any invalid address, not only for a null pointer.LIBC_ASSERT. It compiles away in release builds.LIBC_COPT_HARDENING_MODE was added for the allocator's integrity checks. Extending it to other code needs discussion first.undefined_behavior explains the reasoning.mode_t includes hdr/types/mode_t.h itself, even if another header it includes happens to provide the type. This applies to header files as well as .cpp files. The exception is a header whose purpose is to include another one, such as a proxy header or a header that selects the implementation for the target: include that header, not the file it forwards to.libc/hdr/ for every type and macro that the libc also provides publicly. See the header inclusion policy in {ref}code_style. Compiler-provided headers are tolerated but should also go through a proxy header where one exists. The existing #include <pthread.h> style includes under libc/src/ are incorrect and should not be copied.src/__support/CPP/ headers are included through the top-level header, for example src/__support/CPP/type_traits.h or src/__support/CPP/utility.h, not through the individual files in the subdirectories of the same name. The top-level header is a forwarding header of the kind the first point describes.(feature_test_macros)=
Public headers do not use feature-test macros such as _GNU_SOURCE or _POSIX_C_SOURCE to hide declarations. Do not add #ifdef guards of that kind to a header. The exception is a macro that a standard itself defines for this purpose, such as __STDC_WANT_LIB_EXT1__ for Annex K.
Headers are generated from the YAML files in libc/include/ (see {ref}header_generation), and a generated header declares only the functions that are enabled for the target. A user who wants a different API surface generates headers to match it, by pointing the LIBC_CONFIG_PATH CMake option at their own entrypoints.txt, headers.txt, and configuration (see {ref}custom_config_directory), instead of every user receiving one set of headers full of conditionals.
Each function in the YAML files lists the standards it comes from (stdc, posix, gnu, bsd, linux, llvm_libc_ext, and so on). Keep that field accurate. It records where each declaration comes from.
{ref}writing_public_headers has the other rules for public headers.
Every file touched by a change uses the current LLVM file header: a bare separator line, the license block, and a \file description.
//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// \file /// Implementation of fflush. /// //===----------------------------------------------------------------------===//
Comment only what the code does not already say. A straightforward function or test needs no introductory paragraph, and a function that is moved keeps exactly the comments it had. Also, don't number the steps of a function in its comments (// 1. ..., // 2. ...). It makes later refactors noisier than necessary for no tangible benefit.
Do not compare against character literals (except for equality), or do arithmetic on them to classify or convert characters. Use the helpers in src/__support/ctype_utils.h and src/__support/wctype_utils.h, which work for non-ASCII encodings and for both char and wchar_t. See {ref}design_philosophy for the model behind this.
(math_function_guidelines)=
The {ref}math page <math> describes the goals of the math library, and libc/src/math/docs/add_math_function.md is the step-by-step guide for adding a function. The points below are the ones that most often need stating.
errno, floating point exceptions, and signaling NaNs. A signaling NaN input produces a quiet NaN and raises FE_INVALID.LIBC_CONF_MATH_OPTIMIZATIONS option, for example LIBC_MATH_SKIP_ACCURATE_PASS, LIBC_MATH_NO_ERRNO, or LIBC_MATH_NO_EXCEPT. The baremetal and GPU configurations set several of these by default;math_function_structure);math.h status tables <../headers/math> record the accuracy of each function, including any that are not correctly rounded yet.errno and floating point exceptions are reported only through fputil::set_errno_if_required and fputil::raise_except_if_required.(math_function_structure)=
src/__support/math/, and the entrypoint is a thin wrapper.double_eval, float_eval, float_eval_cr_rn, integer_eval), with LIBC_MATH_<FUNC>_IMPL selecting the one to use. This is the intended structure for every function, although only some functions have been converted so far. Variants other than the default may make different guarantees.math::static_rounding take the rounding mode as an argument. They exist for callers that need a fixed rounding mode, such as compilers. They are not the primary implementation of a function. The primary implementation is whichever one is best for the target's configuration.check-libc.libc/test/src/math/exhaustive/ contains hard-to-round inputs from the CORE-MATH project, as text files (.wc) and in a compact binary form (.bin). The two forms must stay consistent.