166 Commits

Author SHA1 Message Date
Timm Baeder
8fae5a5132
[clang][bytecode] Check builtin carryops for dummy pointers (#157490)
Fixes #157422
2025-09-11 06:57:33 +02:00
Timm Baeder
004231aaeb
[clang][bytecode] Check strlen impl for primitive arrays (#157494)
Fixes #157428
2025-09-11 05:08:08 +02:00
Bhasawut Singhaphan
3ce16565c8
[Headers][X86] Enable constexpr handling for MMX/SSE/AVX/AVX512 avg intrinsics (#157464)
This PR updates the avg builtins to support constant expression handling, by extending the VectorExprEvaluator::VisitCallExpr that handles elementwise integer binop builtins.

Closes #155390

---------

Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-09-09 16:58:56 +00:00
Simon Pilgrim
8a682b7f06
[clang][bytecode][X86] Allow AVX512 funnel shift by scalar immediate intrinsics to be used in constexpr (#157681)
Extends interp__builtin_elementwise_triop to handle (vector, vector, scalar) trinary op intrinsics

Fixes #153152
2025-09-09 16:48:32 +00:00
Simon Pilgrim
33316d503b
[X86] Allow remaining AVX512 per-element shift intrinsics to be used in constexpr (#157696)
Handle missing AVX512F epi64 and AVX512BW epi16 cases with existing code paths

Fixes #154287
2025-09-09 16:10:23 +00:00
Simon Pilgrim
13d50c24a0
[X86][bytecode] Allow SSE/AVX BLENDVPD/PD intrinsics to be used in constexpr (#157126)
BLENDV intrinsics use the signbit of the condition mask to select
between the LHS (false) and RHS (true) operands

Fixes #157066
2025-09-05 20:14:29 +00:00
Simon Pilgrim
816eb9e71d
[Clang][bytecode] Add interp__builtin_elementwise_triop_fp to handle general 3-operand floating point intrinsics (#157106)
Refactor interp__builtin_elementwise_fma into something similar to interp__builtin_elementwise_triop with a callback function argument to allow reuse with other intrinsics.

This will allow reuse with some upcoming x86 intrinsics
2025-09-05 14:57:07 +00:00
Simon Pilgrim
9b24a8a68f
[X86][bytecode] Allow SSE/AVX PBLENDVB intrinsics to be used in constexpr (#157100)
BLENDV intrinsics use the signbit of the condition mask to select between the LHS (false) and RHS (true) operands

First part of #157066 - the BLENDVPS/D requires floatbits hacking which I need to do some prep work for
2025-09-05 14:34:37 +00:00
Simon Pilgrim
4b3ad50698
[Clang][bytecode] interp__builtin_elementwise_binop - remove unused BuiltinID argument. NFC (#157109) 2025-09-05 15:06:04 +01:00
Simon Pilgrim
d1050bf86d
[Clang][bytecode] Add interp__builtin_elementwise_triop to handle general 3-operand integer intrinsics (#156944)
Refactor interp__builtin_elementwise_fsh into something similar to interp__builtin_elementwise_int_binop with a callback function argument to allow reuse with other intrinsics

This will allow reuse with some upcoming x86 intrinsics

We can flesh out handling for mixed vector/scalar args as the need arises
2025-09-05 08:19:32 +00:00
Chaitanya Koparkar
79ea319231
[Clang] Enable constexpr handling for builtin elementwise fshl/fshr (#153572)
Fixes #153151.
2025-09-04 16:00:06 +01:00
Simon Pilgrim
0f3ede911a
[X86] Allow AVX512 512-bit variants of AVX2 per-element i32 shift intrinsics to be used in constexpr (#156480)
Followup to #154780
2025-09-03 07:58:22 +00:00
Simon Pilgrim
aa491fceb6
[X86] Add constexpr handling for XOP/AVX512 rotate by immediate intrinsics (#156047) 2025-08-30 07:35:43 +00:00
Justin Riddell
f9e16fa6ab
[Headers][X86] VisitCallExpr constexpr immediate shifts (#154293) (#155542)
Fixes #154293 

Implement VectorExprEvaluator::VisitCallExpr constexpr support for left, right, arithmetic shift for MMX/SSE/AVX2/AVX512 intrinsics

_mm*_slli_epi*
_mm*_srli_epi*
_mm*_srai_epi*
_mm*_mask_slli_epi*
_mm*_maskz_slli_epi*

NOTE: not all intrinsics have all widths i.e. _mm_srli_pi32 doesn't have pi64 etc.
2025-08-29 15:01:51 +00:00
Simon Pilgrim
0d9c0ced14
[clang][x86] Ensure we use the shifted value bit width to check for out of bounds per-element shift amounts (#156019)
This should allow us to reuse these cases for the shift-by-immediate builtins in #155542
2025-08-29 13:21:25 +00:00
Simon Pilgrim
fcc786738a
[Clang][bytecode] interp__builtin_elementwise_int_binop - use APSInt callback instead of repeated switch statement (#155891)
Users of interp__builtin_elementwise_int_binop are going to be very well
defined, we can use a simple callback mechanism (including existing
llvm::APIntOps static methods) to perform the evaluation and avoid a
repeated switch statement.

Hopefully this will help keep interp__builtin_elementwise_int_binop
clean as we add more uses
2025-08-29 11:49:20 +00:00
Joseph Huber
62ac4e3014
[Clang] Support generic bit counting builtins on fixed boolean vectors (#154203)
Summary:
Boolean vectors as implemented in clang can be bit-casted to an integer
that is rounded up to the next primitive sized integer. Users can do
this themselves, but since the counting bits are very likely to be used
with bitmasks like this and the generic forms are expected to be
generic it seems reasonable that we handle this case directly.
2025-08-26 08:35:10 -05:00
Timm Baeder
773e6c3a35
[clang][bytecode] Support remaining add_sat like X86 builtins (#155358) 2025-08-26 13:51:30 +02:00
Timm Baeder
9df7824e19
[clang][bytecode][NFC] Check InitializingBlocks in _within_lifetime (#155378)
This kind of check is exactly why InterpState::InitializingBlocks
exists.
2025-08-26 11:25:46 +02:00
Matheus Izvekov
dc8596d548
[clang] NFC: change more places to use Type::getAsTagDecl and friends (#155313)
This changes a bunch of places which use getAs<TagType>, including
derived types, just to obtain the tag definition.

This is preparation for #155028, offloading all the changes that PR used
to introduce which don't depend on any new helpers.
2025-08-25 20:18:56 -03:00
Timm Baeder
3923adfa3f
[clang][bytecode] Guard strcmp against differing element types (#154777)
This can happen when casts are involved.

Fixes #154006
2025-08-21 18:02:08 +02:00
Timm Baeder
3f97736181
[clang][bytecode] Implement ia32_select* builtins (#154758) 2025-08-21 16:34:57 +02:00
Timm Baeder
f09ac1bf86
[clang][bytecode] Fix an out-of-bounds access with ia32_pmul* (#154750)
... builtins. We used to access the I'th index of the output vector, but
that doesn't work since the output vector is only half the size of the
input vector.
2025-08-21 15:09:08 +02:00
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