[all-commits] [llvm/llvm-project] c66be2: [clang][bytecode] Allocate IntegralAP and Floating...

Timm Baeder via All-commits all-commits at lists.llvm.org
Tue Jun 17 09:31:28 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: c66be289901b3f035187d391e80e3610d7d6232e
      https://github.com/llvm/llvm-project/commit/c66be289901b3f035187d391e80e3610d7d6232e
  Author: Timm Baeder <tbaeder at redhat.com>
  Date:   2025-06-17 (Tue, 17 Jun 2025)

  Changed paths:
    M clang/lib/AST/ByteCode/Compiler.cpp
    M clang/lib/AST/ByteCode/Compiler.h
    M clang/lib/AST/ByteCode/Descriptor.cpp
    M clang/lib/AST/ByteCode/Disasm.cpp
    M clang/lib/AST/ByteCode/Floating.h
    M clang/lib/AST/ByteCode/Integral.h
    M clang/lib/AST/ByteCode/IntegralAP.h
    M clang/lib/AST/ByteCode/Interp.cpp
    M clang/lib/AST/ByteCode/Interp.h
    M clang/lib/AST/ByteCode/InterpBuiltin.cpp
    M clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
    M clang/lib/AST/ByteCode/InterpState.h
    M clang/lib/AST/ByteCode/Opcodes.td
    M clang/lib/AST/ByteCode/PrimType.h
    M clang/lib/AST/ByteCode/Program.h
    M clang/test/AST/ByteCode/builtin-bit-cast-long-double.cpp
    M clang/test/AST/ByteCode/builtin-functions.cpp

  Log Message:
  -----------
  [clang][bytecode] Allocate IntegralAP and Floating types using an allocator (#144246)

Both `APInt` and `APFloat` will heap-allocate memory themselves using
the system allocator when the size of their data exceeds 64 bits.

This is why clang has `APNumericStorage`, which allocates its memory
using an allocator (via `ASTContext`) instead. Calling `getValue()` on
an ast node like that will then create a new `APInt`/`APFloat` , which
will copy the data (in the `APFloat` case, we even copy it twice).
That's sad but whatever.

In the bytecode interpreter, we have a similar problem. Large integers
and floating-point values are placement-new allocated into the
`InterpStack` (or into the bytecode, which is a `vector<std::byte>`).
When we then later interrupt interpretation, we don't run the destructor
for all items on the stack, which means we leak the memory the
`APInt`/`APFloat` (which backs the `IntegralAP`/`Floating` the
interpreter uses).

Fix this by using an approach similar to the one used in the AST. Add an
allocator to `InterpState`, which is used for temporaries and local
values. Those values will be freed at the end of interpretation. For
global variables, we need to promote the values to global lifetime,
which we do via `InitGlobal` and `FinishInitGlobal` ops.

Interestingly, this results in a slight _improvement_ in compile times:
https://llvm-compile-time-tracker.com/compare.php?from=6bfcdda9b1ddf0900f82f7e30cb5e3253a791d50&to=88d1d899127b408f0fb0f385c2c58e6283195049&stat=instructions:u
(but don't ask me why).

Fixes https://github.com/llvm/llvm-project/issues/139012



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