133 Commits

Author SHA1 Message Date
Timm Baeder
0eebb8bbe5
[clang][bytecode][NFC] Check hasTrivialDtor() in RunDestructors (#155381)
We do this when calling Free() on dynamically allocated memory.
2025-08-26 13:03:17 +02:00
Timm Baeder
97b3cb2239
[clang][bytecode] Don't call getIndex() on one-past-end pointers (#155173)
That doesn't work.

Fixes #152903
2025-08-26 06:33:24 +02:00
Timm Baeder
e0acf6592b
[clang][bytecode] Call CheckFinalLoad in all language modes (#154496)
Fixes #153997
2025-08-21 08:24:09 +02:00
Timm Baeder
e16ced3ef4
[clang][bytecode] Diagnose one-past-end reads from global arrays (#154484)
Fixes #154312
2025-08-20 10:34:44 +02:00
Timm Baeder
8f0da9b8bd
[clang][bytecode] Disable EndLifetime op for array elements (#154119)
This breaks a ton of libc++ tests otherwise, since calling
std::destroy_at will currently end the lifetime of the entire array not
just the given element.

See https://github.com/llvm/llvm-project/issues/147528
2025-08-18 16:32:50 +02:00
Timm Baeder
b08e86cb7f
[clang][bytecode] Move CheckExtern call into isAccessible() block (#152926)
This is where it belongs, but it was accidentally left where it was.
2025-08-10 18:56:15 +02:00
Timm Baeder
7a6c9813d6
[clang][bytecode] Add AccessFlags to Block (#152590)
This way, we can check a single uint8_t for != 0 to know whether this
block is accessible or not. If not, we still need to figure out why not
and diagnose appropriately of course.
2025-08-09 15:46:28 +02:00
Matheus Izvekov
91cdd35008
[clang] Improve nested name specifier AST representation (#147835)
This is a major change on how we represent nested name qualifications in
the AST.

* The nested name specifier itself and how it's stored is changed. The
prefixes for types are handled within the type hierarchy, which makes
canonicalization for them super cheap, no memory allocation required.
Also translating a type into nested name specifier form becomes a no-op.
An identifier is stored as a DependentNameType. The nested name
specifier gains a lightweight handle class, to be used instead of
passing around pointers, which is similar to what is implemented for
TemplateName. There is still one free bit available, and this handle can
be used within a PointerUnion and PointerIntPair, which should keep
bit-packing aficionados happy.
* The ElaboratedType node is removed, all type nodes in which it could
previously apply to can now store the elaborated keyword and name
qualifier, tail allocating when present.
* TagTypes can now point to the exact declaration found when producing
these, as opposed to the previous situation of there only existing one
TagType per entity. This increases the amount of type sugar retained,
and can have several applications, for example in tracking module
ownership, and other tools which care about source file origins, such as
IWYU. These TagTypes are lazily allocated, in order to limit the
increase in AST size.

This patch offers a great performance benefit.

It greatly improves compilation time for
[stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for
`test_on2.cpp` in that project, which is the slowest compiling test,
this patch improves `-c` compilation time by about 7.2%, with the
`-fsyntax-only` improvement being at ~12%.

This has great results on compile-time-tracker as well:

![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831)

This patch also further enables other optimziations in the future, and
will reduce the performance impact of template specialization resugaring
when that lands.

It has some other miscelaneous drive-by fixes.

About the review: Yes the patch is huge, sorry about that. Part of the
reason is that I started by the nested name specifier part, before the
ElaboratedType part, but that had a huge performance downside, as
ElaboratedType is a big performance hog. I didn't have the steam to go
back and change the patch after the fact.

There is also a lot of internal API changes, and it made sense to remove
ElaboratedType in one go, versus removing it from one type at a time, as
that would present much more churn to the users. Also, the nested name
specifier having a different API avoids missing changes related to how
prefixes work now, which could make existing code compile but not work.

How to review: The important changes are all in
`clang/include/clang/AST` and `clang/lib/AST`, with also important
changes in `clang/lib/Sema/TreeTransform.h`.

The rest and bulk of the changes are mostly consequences of the changes
in API.

PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just
for easier to rebasing. I plan to rename it back after this lands.

Fixes #136624
Fixes https://github.com/llvm/llvm-project/issues/43179
Fixes https://github.com/llvm/llvm-project/issues/68670
Fixes https://github.com/llvm/llvm-project/issues/92757
2025-08-09 05:06:53 -03:00
Timm Baeder
1b1f352cb9
[clang][bytecode] Handle reads on zero-size arrays (#152706) 2025-08-08 16:03:02 +02:00
Timm Baeder
193995d5a2
[clang][bytecode] Handle more invalid member pointer casts (#152546) 2025-08-07 19:24:01 +02:00
Timm Baeder
c869ef6ebc
[clang][bytecode] Refactor Check* functions (#152300)
... so we don't have to create Pointer instances when we don't need
them.
2025-08-07 11:32:39 +02:00
Timm Baeder
8f7dfc689c
[clang][bytecode] Call CheckLocalLoad in GetLocal (#152090)
I forgot to call this here as well. It was only used in the EvalEmitter
implementation of the function. Also fix a problem where we didn't
diagnose out-of-lifetime reads here.
2025-08-05 08:46:22 +02:00
Timm Baeder
76e38ca981
[clang][bytecode] Try to load primitive values directly (#151833)
Instead of doing a GetPtrLocal + Load or GetPtrGlobal + Load pair, try
to load the value directly.
2025-08-04 12:28:33 +02:00
Timm Baeder
904de95e71
[clang][bytecode][NFC] Fix a few clang-tidy complaints (#150940) 2025-07-28 15:57:49 +02:00
Timm Baeder
ef49ed4829
[clang][bytecode] Use bytecode interpreter in isPotentialConstantExprU… (#149462)
…nevaluated

Fake a function call to the given function and evaluate the given
expression as if it was part of that function call.

Fixes #149383
2025-07-19 07:18:51 +02:00
Timm Baeder
b7660a5415
[clang][bytecode] Fix const-in-mutable fields (#149286)
For mutable and const fields, we have two bits in InlineDescriptor,
which both get inherited down the hierarchy. When a field is both const
and mutable, we CAN read from it if it is a mutable-in-const field, but
we _can't_ read from it if it is a const-in-mutable field. We need
another bit to distinguish the two cases.
2025-07-18 11:20:48 +02:00
Timm Baeder
3b8a18c27a
[clang][bytecode] Fix contains check using llvm::find (#149050)
We need to compare to the end() interator.
2025-07-16 13:26:10 +02:00
Timm Baeder
bbfbe7d789
[clang][bytecode][NFC] Remove unused function prototypes (#149031) 2025-07-16 11:25:19 +02:00
Timm Baeder
17d3029331
[clang][bytecode] Make union activation more granular (#148835)
Only activate things if the syntactical structure suggests so. This adds
a bunch of new opcodes to control whether to activate in stores, etc.

Fixes #134789
2025-07-16 09:03:33 +02:00
Timm Baeder
38b9c660a7
[clang][bytecode] Remove needless global check (#148163)
Remove the call to D->hasGlobalStorage(), since we never reach this
point for local variables.
2025-07-11 15:47:32 +02:00
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
36cbd43ae8
[clang][bytecode] Check new/delete mismatch earlier (#147732)
This fixes a mismatch in diagnostic output with the current intepreter.
2025-07-10 07:33:33 +02:00
Rahul Joshi
6cf656eca7
[NFC][Clang][AST] Drop llvm:: in front of ArrayRef/MutableArrayRef (#145207) 2025-06-23 13:10:42 -07: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
478bdd8b90
[clang][bytecode] Save Constexpr bit in Function (#142793)
Rename isConstexpr to isValid, the former was always a bad name. Save a
constexpr bit in Function so we don't have to access the decl in
CheckCallable.
2025-06-05 06:38:48 +02:00
Timm Baeder
2e70da3fba
[clang][bytecode] Partially address string literal uniqueness (#142555)
This still leaves the case of the

constexpr auto b3 = name1() == name1();

test from cxx20.cpp broken.
2025-06-03 16:26:31 +02:00
Timm Baeder
722d6b55ed
[clang][bytecode] Only check expr in CheckThis() if we have to (#141951)
Pre C++11, we dont't need to get the value of IsImplicit.
2025-05-29 17:17:59 +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
857ffa1915
[clang][bytecode] Recursively start lifetimes as well (#141742)
The constructor starts the lifetime of all the subobjects.
2025-05-28 16:16:00 +02:00
Timm Baeder
294643e4bd
[clang][bytecode] Check lifetime of all ptr bases in placement-new (#141272)
placement-new'ing an object with a dead base object is not allowed, so
we need to check all the pointer bases.
2025-05-24 14:17:26 +02:00
Timm Baeder
319feac43d
[clang][bytecode] Fix AccessKinds in placement new CheckStore() call (#141123)
CheckStore is for assignments, but we're constructing something here, so
pass AK_Construct instead. We already diagnosed the test case, but as an
assignment.
2025-05-23 13:00:57 +02:00
Timm Baeder
32805964fc
[clang][bytecode] Diagnose placement-new'ing to a temporary (#141099)
... that's been created in a different evaluation.
2025-05-23 08:17:22 +02:00
Timm Baeder
d45f1d0827
[clang][bytecode] Fix self-init diagnostics in C++23 (#141044) 2025-05-22 14:26:52 +02:00
Timm Baeder
7cfeedf64a
[clang][bytecode] Change diagnostics for self-initialization (#141006)
Change the diagnostics when reading from the variable we're currently
initializing do be the same as the one the current interpreter emits.
2025-05-22 10:31:39 +02:00
Timm Baeder
572add0601
[clang][bytecode] Diagnose failed constexpr assertions differently (#140000)
Adjust to the new way the ast walker is doing it.
2025-05-19 13:29:09 +02:00
Timm Baeder
9a26dff748
[clang][bytecode] Check dtor calls for one-past-end pointers (#140047) 2025-05-15 15:43:07 +02:00
Timm Baeder
b26adacc85
[clang][bytecode] Check destructors for temporaries (#140039)
Also, increase the EvalID in isPotentialConstantExpr(), since this is
its own evaluation.
2025-05-15 14:34:35 +02:00
Timm Baeder
c14acb7442
[clang][bytecode] Save Immediate bit in Function (#139671)
Otherwise, we have to look at the FunctionDecl at every function call.
2025-05-13 12:56:08 +02:00
Timm Baeder
98763433e6
[clang][bytecode] Optimize enum value range checks (#139672)
Only do the work if we really have to.
2025-05-13 10:55:24 +02:00
Timm Baeder
98d68e49f7
[clang][bytecode][NFC] Remove unused function (#139355) 2025-05-10 07:57:44 +02:00
Timm Baeder
d6f1de3107
[clang][bytecode] Bail out on invalid function declarations (#138503) 2025-05-05 16:24:53 +02:00
Timm Baeder
c51be1be3a
[clang][bytecode] Fix checking for integer overflow (#137962)
We need to evaluate both the True/False expressions of a conditional
operator as well as the LHS/RHS of a binary operator in more cases.
2025-05-01 07:35:33 +02:00
Timm Baeder
f8724bd873
[clang][bytecode] Check live-ness when calling dtors (#137645)
We can't call a destructor on a dead pointer.
2025-04-28 17:47:30 +02:00
Timm Baeder
959905a5ad
[clang][bytecode] Don't create Function instances for builtins (#137618)
Now that we don't use them anymore in InterpBuiltin.cpp and we don't
create frames for them anymore anyway, just don't create Function
instances.
2025-04-28 14:08:42 +02:00
Timm Baeder
e086d7b146
[clang][bytecode] Don't create function frames for builtin calls (#137607)
They don't have local variables etc. so don't create frames for them.
2025-04-28 13:11:15 +02:00
Timm Baeder
60b3a5b7e7
[clang][bytecode] Fix two small builtin_constant_p cases (#137587)
Only accept string literals if we're pointing to the first index and do
accept complex literals.
2025-04-28 09:21:49 +02:00