15 Commits

Author SHA1 Message Date
Timm Baeder
0e5f9f67cd
[clang][bytecode] Keep a list of initializing blocks in InterpState (#148120)
So we can know what blocks we're currently running constructors or
destructors for.
2025-07-11 09:49:47 +02:00
Timm Baeder
32fc625a3f
Reapply "Reapply "[clang][bytecode] Allocate IntegralAP and Floating … (#145014)
…types usi… (#144676)"

This reverts commit 68471d29eed2c49f9b439e505b3f24d387d54f97.

IntegralAP contains a union:
  union {
    uint64_t *Memory = nullptr;
    uint64_t Val;
  };

On 64bit systems, both Memory and Val have the same size. However, on 32
bit system, Val is 64bit and Memory only 32bit. Which means the default
initializer for Memory will only zero half of Val. We fixed this by
zero-initializing Val explicitly in the IntegralAP(unsigned BitWidth)
constructor.


See also the discussion in
https://github.com/llvm/llvm-project/pull/144246
2025-06-20 18:06:01 +02:00
Timm Bäder
68471d29ee Revert "Reapply "[clang][bytecode] Allocate IntegralAP and Floating types usi… (#144676)"
This reverts commit 7c15edb306932e41c159f3d69c161ed0d89d47b7.

This still breaks clang-armv8-quick:
https://lab.llvm.org/buildbot/#/builders/154/builds/17587
2025-06-18 15:17:53 +02:00
Timm Baeder
7c15edb306
Reapply "[clang][bytecode] Allocate IntegralAP and Floating types usi… (#144676)
…ng an allocator (#144246)"

This reverts commit 57828fec760f086b334ce0cb1c465fc559dcaea4.
2025-06-18 14:37:29 +02:00
Timm Bäder
57828fec76 Revert "[clang][bytecode] Allocate IntegralAP and Floating types using an allocator (#144246)"
This reverts commit c66be289901b3f035187d391e80e3610d7d6232e.

This breaks the armv8-quick builder:
https://lab.llvm.org/buildbot/#/builders/154/builds/17549
2025-06-17 21:08:23 +02:00
Timm Baeder
c66be28990
[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
2025-06-17 18:31:06 +02:00
Timm Baeder
4efe7a590d
[clang][bytecode] Simplify diagnoseUnknownDecl if we're not diagnosing (#141910)
See the added comment.

This improves compile times a bit:

https://llvm-compile-time-tracker.com/compare.php?from=ac62f73f19ae9fb415d3fc423949b8d7543e8717&to=0d6cf47197a4ee11cdd1ee4a48ea38a2907c3d45&stat=instructions:u
2025-05-29 13:02:50 +02:00
Timm Baeder
1a78ef9a9e
[clang][bytecode] Allow casts from void* only in std::allocator calls (#136714)
Otherwise, add the missing diagnostic.
2025-04-23 08:00:57 +02:00
Timm Baeder
90ddb54440
[clang][bytecode] Enter a non-constant context when revisiting (#136104)
Otherwise, things like __builtin_is_constant_evaluated() return the
wrong value.
2025-04-17 12:50:28 +02:00
Timm Baeder
d08cf7900d
[clang][bytecode] Implement __builtin_constant_p (#130143)
Use the regular code paths for interpreting.

Add new instructions: `StartSpeculation` will reset the diagnostics
pointers to `nullptr`, which will keep us from reporting any diagnostics
during speculation. `EndSpeculation` will undo this.

The rest depends on what `Emitter` we use.

For `EvalEmitter`, we have no bytecode, so we implement `speculate()` by
simply visiting the first argument of `__builtin_constant_p`. If the
evaluation fails, we push a `0` on the stack, otherwise a `1`.

For `ByteCodeEmitter`, add another instrucion called `BCP`, that
interprets all the instructions following it until the next
`EndSpeculation` instruction. If any of those instructions fails, we
jump to the `EndLabel`, which brings us right before the
`EndSpeculation`. We then push the result on the stack.
2025-03-08 06:06:14 +01:00
Timm Baeder
06130ed341
Reapply "[clang][bytecode] Stack-allocate bottom function frame" (#12… (#125349)
…5325)

Move the BottomFrame to InterpState instead.
2025-02-01 18:12:35 +01:00
Timm Baeder
a024a0ceed
[clang][bytecode] Override InConstantContext flag for immediate calls (#109967)
And fix the diagnostics for __builtin_is_constant_evaluated(). We can be
in a non-constant context, but calling an immediate function always
makes the context constant for the duration of that call.
2024-09-25 16:46:46 +02:00
Timm Baeder
83fea8b809
[clang][bytecode] Allow continuing when discarded MemberExpr Base fails (#107231)
We don't need the value in this case, since we're discarding it anyway.
Allow continuing the interpretation but note the side effect.
2024-09-07 09:33:27 +02:00
yronglin
d9e7286019
[NFC][clang][bytecode] Rename clang::interp::State::getCtx to clang::interp::State::getASTContext (#106071)
The new constant interpreter's `clang::interp::InterpState` contains
both `clang::interp::Context` and `clang::ASTContext`. So using `S.Ctx`
and `S.getCtx()` was a bit confusing. This PR rename `getCtx()` to
`getASTContext` to make things more clearer.

Signed-off-by: yronglin <yronglin777@gmail.com>
2024-08-26 22:23:07 +08:00
Timm Baeder
a07aba5d44
[clang] Rename all AST/Interp stuff to AST/ByteCode (#104552)
"Interp" clashes with the clang interpreter and people often confuse
this.
2024-08-16 17:13:12 +02:00