[all-commits] [llvm/llvm-project] 8c8f2d: [Clang][CodeGen] Implement code generation for __b...

Marco Elver via All-commits all-commits at lists.llvm.org
Tue Oct 28 08:55:51 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 8c8f2df23239914f3276aef02eb89a78373fcaa3
      https://github.com/llvm/llvm-project/commit/8c8f2df23239914f3276aef02eb89a78373fcaa3
  Author: Marco Elver <elver at google.com>
  Date:   2025-10-28 (Tue, 28 Oct 2025)

  Changed paths:
    M clang/docs/AllocToken.rst
    M clang/docs/ReleaseNotes.rst
    M clang/lib/CodeGen/BackendUtil.cpp
    M clang/lib/CodeGen/CGBuiltin.cpp
    M clang/test/CodeGen/lto-newpm-pipeline.c
    A clang/test/CodeGenCXX/alloc-token-builtin.cpp

  Log Message:
  -----------
  [Clang][CodeGen] Implement code generation for __builtin_infer_alloc_token() (#156842)

Implement code generation for `__builtin_infer_alloc_token()`. The
`AllocToken` pass is now registered to run unconditionally in the
optimization pipeline.  This ensures that all instances of the
`llvm.alloc.token.id` intrinsic are lowered to constant token IDs,
regardless of whether `-fsanitize=alloc-token` is enabled. This
guarantees that the builtin always resolves to a token value, providing
a consistent and reliable mechanism for compile-time token querying.

This completes `__builtin_infer_alloc_token(<malloc-args>, ...)` to
allow compile-time querying of the token ID, where the builtin arguments
mirror those normally passed to any allocation function. The argument
expressions are unevaluated operands. For type-based token modes, the
same type inference logic is used as for untyped allocation calls.

For example the ID that is passed to (with `-fsanitize=alloc-token`):

    some_malloc(sizeof(Type), ...)

is equivalent to the token ID returned by

    __builtin_infer_alloc_token(sizeof(Type), ...)

The builtin provides a mechanism to pass or compare token IDs in code
that needs to be explicitly allocation token-aware (such as inside an
allocator, or through wrapper macros).

A more concrete demonstration of __builtin_infer_alloc_token's use is
enabling type-aware Slab allocations in the Linux kernel:

  https://lore.kernel.org/all/20250825154505.1558444-1-elver@google.com/

Notably, any kind of allocation-call rewriting is a poor fit for the
Linux kernel's kmalloc-family functions, which are macros that wrap
(multiple) layers of inline and non-inline wrapper functions. Given the
Linux kernel defines its own allocation APIs, the more explicit builtin
gives the right level of control over where the type inference happens
and the resulting token is passed.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list