143 Commits

Author SHA1 Message Date
Kazu Hirata
f487c0e63c [AST] Fix warnings
This patch fixes:

  clang/lib/AST/ByteCode/InterpBuiltin.cpp:1827:21: error: unused
  variable 'ASTCtx' [-Werror,-Wunused-variable]

  clang/lib/AST/ByteCode/InterpBuiltin.cpp:2724:18: error: unused
  variable 'Arg2Type' [-Werror,-Wunused-variable]

  clang/lib/AST/ByteCode/InterpBuiltin.cpp:2725:18: error: unused
  variable 'Arg3Type' [-Werror,-Wunused-variable]

  clang/lib/AST/ByteCode/InterpBuiltin.cpp:2748:18: error: unused
  variable 'ElemT' [-Werror,-Wunused-variable]
2025-08-20 08:58:59 -07:00
Chaitanya Koparkar
f649605bcf
[clang] Enable constexpr handling for __builtin_elementwise_fma (#152919)
Fixes https://github.com/llvm/llvm-project/issues/152455.
2025-08-20 14:51:40 +01:00
Fraser Cormack
8b128388b5
[clang] Introduce elementwise ctlz/cttz builtins (#131995)
These builtins are modeled on the clzg/ctzg builtins, which accept an
optional second argument. This second argument is returned if the first
argument is 0. These builtins unconditionally exhibit zero-is-undef
behaviour, regardless of target preference for the other ctz/clz
builtins. The builtins have constexpr support.

Fixes #154113
2025-08-20 12:18:28 +01:00
Timm Baeder
965b7c2bfc
[clang][bytecode] Implement ia32_pmul* builtins (#154315) 2025-08-19 16:05:20 +02:00
Timm Baeder
eb7a1d91b2
[clang][bytecode] Support pmul X86 builtins (#154275) 2025-08-19 09:10:50 +02:00
Timm Baeder
0d05c42b6a
[clang][bytecode] Improve __builtin_{,dynamic_}object_size implementation (#153601) 2025-08-18 11:12:33 +02:00
Shafik Yaghmour
868efdcf38
[Clang][Bytecode][NFC] Move Result into APSInt constructor (#153664)
Static analysis flagged this line because we are copying Result instead
of moving it.
2025-08-15 08:52:49 -07:00
Vincent
d3bbdc7bde
[clang] constexpr __builtin_elementwise_abs support (#152497)
Added constant evaluation support for `__builtin_elementwise_abs` on integer, float and vector type.

fixes #152276

---------

Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-08-14 12:34:23 +01:00
Timm Baeder
56131e3959
[clang][bytecode] Diagnose incomplete types more consistently (#153368)
To match the diagnostics of the current interpreter.
2025-08-13 10:40:21 +02:00
Iris Shi
713ec58dec
[clang] constexpr integer __builtin_elementwise_{max,min} (#152294)
- Closes #152278
- Part of #51787

This PR adds support for the bulitin elementwise max/min functions for
integer types.
2025-08-12 09:49:05 +08:00
Timm Baeder
875841c93d
[clang][bytecode] Avoid a getValue() call in builtin_isinf (#152939)
Get the APFloat once and work with that, instead of calling isInf() and
potentially isNegative().
2025-08-11 07:11:38 +02:00
Timm Baeder
09ff631b92
[clang][bytecode][NFC] Fix ternary operators with known IsArray values (#152894)
After https://github.com/llvm/llvm-project/pull/146471, the values here
are known.
2025-08-10 10:32:34 +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
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
4a44a85c89
[clang][bytecode] Add Pointer::initializeAllElements() (#151151)
To initialize all elements of a primitive array at once. This saves us
from creating the InitMap just to destroy it again after all elements
have been initialized.
2025-07-29 19:30:01 +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
e39ee62c5b
[clang][bytecode] Use OptPrimType instead of std::optional<PrimType> (#149812)
We use this construct a lot. Use something similar to clang's
UnsignedOrNone.

This results in some slighy compile time improvements:
https://llvm-compile-time-tracker.com/compare.php?from=17a4b0399d161a3b89d8f0ce82add1638f23f5d4&to=a251d81ecd0ed45dd190462663155fdb303ef04d&stat=instructions:u
2025-07-21 17:16:13 +02:00
Timm Baeder
5b0935f1f0
[clang][bytecode] Reintroduce Pointer::elem() (#149693)
As a way of writing atIndex(I).deref<T>(), which creates an intermediate
Pointer, which in turn adds (and removes) that pointer from the pointer
list of the Block. This way we can avoid that.
2025-07-20 10:59:50 +02:00
Timm Baeder
3bb4355bb8
[clang][bytecode] Report mutable reads when copying unions (#149320) 2025-07-18 11:10:57 +02:00
Timm Baeder
3f991f5067
[clang][bytecode][NFC] Remove unused includes (#149460) 2025-07-18 09:30:42 +02:00
Timm Baeder
d72d84cb0d
[clang][bytecode] Implement missing elementwise builtins (#147892) 2025-07-10 08:26:08 +02:00
Timm Baeder
5cefb9a367
[clang][bytecode] Fix __builtin_is_within_lifetime in initializers (#147480) 2025-07-08 17:17:15 +02:00
Timm Baeder
ec9eefcef5
[clang][bytecode] Fix a crash in overflow builtins (#147189)
Only initialize pointers that can be initialized.
2025-07-06 16:42:06 +02:00
Timm Baeder
83401ed6a2
[clang][bytecode] Narrow allocated single-array Pointer (#147160)
Since the result should not be an array element.
2025-07-05 22:15:58 +02:00
Timm Baeder
1fe993c251
[clang][bytecode] Allocate operator new data as array (#146471)
Even if we only allocate one element, we still need to allocate it as a
single-element array. This matches what the current interpreter does.
2025-07-01 15:45:50 +02:00
Shafik Yaghmour
a4be46e0e5
[Clang][ByteCode][NFC] Misc minor performance fixes (#145988)
Static analysis flagged multiple places we could move instead of copy.
In one case I realized we could avoid computing the same thing multiple
times and did that fix instead.
2025-06-27 11:00:16 -07: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
23ba0fdbfc
[clang][bytecode] Fix assignInteger() with allocated primtypes (#145302) 2025-06-23 14:43:11 +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
4f9e6bad84
[clang][bytecode] Fix calling operator new with nothrow/align parameter (#144271)
Discard all the parameters we don't care about.
2025-06-16 08:28:52 +02:00
Shafik Yaghmour
3ca6ea0f3a
[Clang][ByteCode][NFC] Move APInt into pushInteger since it is being passed by value (#143578)
Static analysis flagged that we could move APInt instead of copy, indeed
it has a move constructor and so we should move into values for APInt.
2025-06-11 08:02:44 -07:00
Jie Fu
627e49e249 [AST] Fix an unused-function warning (NFC)
/llvm-project/clang/lib/AST/ByteCode/InterpBuiltin.cpp:26:13:
error: unused function 'isNoopBuiltin' [-Werror,-Wunused-function]
static bool isNoopBuiltin(unsigned ID) {
            ^
1 error generated.
2025-06-05 14:11:30 +08:00
Timm Baeder
c7a93efa50
[clang][bytecode] Pop builtin args from the stack while evaluating (#142832)
Instead of just peek()ing the values when evaluating the builtins and
later classify()ing all the call args once again to remove them, just do
it while evaluating. This saves quite a bit of code.
2025-06-05 07:36:25 +02:00
DeanSturtevant1
397fdb172a
Enclose code in nondebug in #ifndef NDEBUG/#endif (#142189)
A previous change to InterpBuiltin.cpp fixed an unused variable warning
by using [[maybe unused]] and (void).
The code actually serves no useful purpose in non-debug builds, so let's
not include it there.
2025-05-30 16:34:55 -04:00
Kazu Hirata
882e7331c4 [AST] Fix a warning
This patch fixes:

  clang/lib/AST/ByteCode/InterpBuiltin.cpp:767:12: error: unused
  variable 'PtrT' [-Werror,-Wunused-variable]
2025-05-29 15:48:14 -07:00
google-yfyang
8229f72b7b
Add "maybe_unused" to variable only used in assert (#142049) 2025-05-29 18:45:50 -04:00
Timm Baeder
b411f70c3b
[clang][bytecode] Make some builtins no-ops (#141952)
For some builtins, we dont' need to do anything, but due to the cleanup
code being the same for all builtins, we still had to duplicate the
value on the stack. Remove that and get rid of all the unnecessary
pressure on the InterpStack.
2025-05-29 20:02:37 +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
4bf356bbd2
[clang][bytecode] Start implementing builtin_is_within_lifetime (#137765) 2025-04-29 10:57:30 +02:00
Timm Baeder
0ddb5794b7
[clang][bytecode] Remove base casts before doing memcpy (#137754)
We have to copy the entire thing, not just one of the bases.
2025-04-29 08:06:31 +02:00
Timm Baeder
15579a8e72
[clang][bytecode] Check array sizes against step limit (#137679) 2025-04-29 05:47:22 +02:00
Timm Baeder
c52fdbe697
[clang][bytecode] Fix ia32_addcarry/subborrow (#137642)
RHS is followed by another Pointer.
2025-04-28 17:44:19 +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
32059ce121
[clang][bytecode][NFC] Stop using Function in InterpBuiltin (#137597)
Prepare for the ultimate removal of Function instances for builtin
functions.
2025-04-28 11:41:49 +02:00