232 Commits

Author SHA1 Message Date
Timm Baeder
859e8a6d6b
[clang][bytecode] Implement C23 named loops (#156856) 2025-09-11 09:19:17 +02:00
Timm Baeder
e2a067e7e5
[clang][bytecode][NFC] Remove an else after a return (#157999) 2025-09-11 09:02:46 +02:00
Timm Baeder
8ba0d0f72f
[clang][bytcode][NFC] Use UnsignedOrNone for global ids (#157328) 2025-09-08 08:56:46 +02:00
Timm Baeder
e9499e82ed
[clang][bytecode][NFC] Remove some unnecessary if statements (#157329)
We already checked that `IsStatic` is true above.
2025-09-07 18:55:48 +02:00
Timm Baeder
46f2b35a98
[clang][bytecode][NFC] Remove instance pointer from emitDestruction (#157040)
We only call this when we just pushed a new pointer to the stack, so try
to save the folling PopPtr op by removing the pointer inside
emitDestruction directly, e.g. by letting the Call op just remove it.
2025-09-05 10:29:43 +02:00
Timm Baeder
9e755445f1
[clang][bytecode] Create implicit variables for wider base types (#156658)
If we create an implicit local variable for a derived-to-base cast, we
still should allocate enough space for the entire derived type.

Fixes #156219
2025-09-04 15:38:14 +02:00
Timm Baeder
917b45539b
[clang][bytecode] Remove superfluous check for complex types (#156666)
`!E->getType()->isAnyComplexType()` is implied by `!canClassify()`.
2025-09-04 07:40:48 +02:00
Timm Baeder
1bbac057f6
[clang][bytecode] Fix ignoring comparisons in C (#156180)
Our comparison ops always return bool, and we do the pop before the
conversion to in in C.

Fixes #156178
2025-08-30 17:41:08 +02:00
Timm Baeder
1eb6c2bd59
[clang][bytecode] Reject vectors with non-primitive element types (#155597)
This happens for e.g. arm's float8 types.
2025-08-27 14:09:54 +02:00
Timm Baeder
16494be2e8
[clang][bytecode] Reject dependent RequiresExprs (#155230)
Fixes #152899
2025-08-27 12:04:44 +02:00
Timm Baeder
d5c5ed3723
[clang][bytecode] Handle vector assignments (#155573) 2025-08-27 11:55:37 +02:00
Matheus Izvekov
88438ba1f3
[clang] AST: fix getAs canonicalization of leaf types (#155028) 2025-08-27 06:20:14 -03:00
Matheus Izvekov
2ec71d93ad
[clang] NFC: introduce Type::getAsEnumDecl, and cast variants for all TagDecls (#155463)
And make use of those.

These changes are split from prior PR #155028, in order to decrease the
size of that PR and facilitate review.
2025-08-26 16:05:59 -03:00
Timm Baeder
1739a0e5fb
[clang][bytecode] Error if calls have fewer arguments than parameters (#155151)
Shouldn't happen, but does.

Fixes #155147
2025-08-26 06:17:39 +02:00
Vincent
fefdb494bf
[Clang] Fix Variable Length Array _Countof Crash (#154627)
Check for missing VLA size expressions (e.g. in int a[*][10]) before
evaluation to avoid crashes in _Countof and constant expression checks.

Fixes #152826
2025-08-25 21:06:49 +02:00
Timm Baeder
c540678920
[clang][bytecode] Fix OptionScope initializer (#155149)
Initialize the `OldToLValue` member with the actual old value of
`ToLValue`.

Pointed out by Shafik in
https://github.com/llvm/llvm-project/pull/153601#discussion_r2294319428
2025-08-25 12:55:34 +02:00
Timm Baeder
ab8b4f6629
[clang][bytecode][NFC] Replace std::optional<unsigned> with UnsignedO… (#154286)
…rNone
2025-08-19 11:37:32 +02:00
Timm Baeder
a1039c1b84
[clang][bytecode] Fix initializing float elements from #embed (#154285)
Fixes #152885
2025-08-19 11:04:21 +02:00
Timm Baeder
da05208bfb
[clang][bytecode] Create temporary before discarding CXXConstructExpr (#154280)
Fixes #154110
2025-08-19 10:27:26 +02:00
Timm Baeder
6ce13ae1c2
[clang][bytecode] Always track item types in InterpStack (#151088)
This has been a long-standing problem, but we didn't use to call the
destructors of items on the stack unless we explicitly `pop()` or
`discard()` them.

When interpretation was interrupted midway-through (because something
failed), we left `Pointer`s on the stack. Since all `Block`s track what
`Pointer`s point to them (via a doubly-linked list in the `Pointer`),
that meant we potentially leave deallocated pointers in that list. We
used to work around this by removing the `Pointer` from the list before
deallocating the block.

However, we now want to track pointers to global blocks as well, which
poses a problem since the blocks are never deallocated and thus those
pointers are always left dangling.

I've tried a few different approaches to fixing this but in the end I
just gave up on the idea of never knowing what items are in the stack.
We already have an `ItemTypes` vector that we use for debugging
assertions. This patch simply enables this vector unconditionally and
uses it in the abort case to properly `discard()` all elements from the
stack. That's a little sad IMO but I don't know of another way of
solving this problem.

As expected, this is a slight hit to compile times:
https://llvm-compile-time-tracker.com/compare.php?from=574d0a92060bf4808776b7a0239ffe91a092b15d&to=0317105f559093cfb909bfb01857a6b837991940&stat=instructions:u
2025-08-18 17:15:31 +02:00
Timm Baeder
0d05c42b6a
[clang][bytecode] Improve __builtin_{,dynamic_}object_size implementation (#153601) 2025-08-18 11:12:33 +02:00
Timm Baeder
e44784fb44
[clang][bytecode] Fix pseudo dtor calls on non-pointers (#153970)
The isGLValue() check made us ignore expressions we shouldn't ignore.
2025-08-17 08:47:57 +02:00
Timm Baeder
373206d5e0
[clang][bytecode] Prefer ParmVarDecls as function parameters (#153952)
We might create a local temporary variable for a ParmVarDecl, in which
case a DeclRefExpr for that ParmVarDecl should _still_ result in us
choosing the parameter, not that local.
2025-08-16 17:22:14 +02:00
Timm Baeder
2b4b721faf
[clang][bytecode] Emit embed element casts directly (#152928)
The element initializer is known to be an IntegerLiteral, the previous
signature of the lambda just didn't specify that. So we can also do the
cast directly instead of doing it via a Cast op.
2025-08-10 19:02:08 +02:00
Timm Baeder
2c40fd6dd1
[clang][bytecode] Use Param decl as variable source if we can (#152909)
This results in diagnostics that are closer to what the current
interpreter produces.
2025-08-10 16:58:42 +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
Timm Baeder
b9fef09e8e
[clang][bytecode] Add canClassify() helper (#152755)
Sometimes we don't need the return value of a classsify() call, we only
need to know if we can map the given Expr/Type to a primitive type. Add
canClassify() for that.
2025-08-09 15:08:14 +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
3ea76af3a1
[clang][bytecode][NFC] Remove a useless local variable (#152711)
We can just check NonNullArgs.empty().
2025-08-08 15:52:23 +02:00
Timm Baeder
90d1d23507
[clang][bytecode] Overrride locs for certain CXXConstructExprs (#152185)
Do it only if we will end up skipping the initializer anyway because
it's a trivial copy or move constructor.
2025-08-06 09:07:40 +02:00
Timm Baeder
dc7c3c2b2b
[clang][bytecode] Disable location tracking for implicit field inits (#150190) 2025-08-05 13:14:01 +02:00
Timm Baeder
df3f629735
[clang][bytecode][NFC] Only collect non-null args if we have to (#152074)
Only do this if the function really has a NonNullArg.
2025-08-05 07:54:25 +02:00
Kazu Hirata
39ca925ffe
[AST] Use llvm::iterator_range::empty (NFC) (#151904) 2025-08-04 07:40:38 -07: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
a1dfcc6e81
[clang][bytecode] Fix D3DCOLORtoUBYTE4 hlsl test (#151819)
HLSL is using CK_FloatingToIntegral casts to cast to vectors, which we
don't support (neither does the current interpreter). Also fix a crash
when trying to promote the HLSL bool, which can't be promoted it seems.
2025-08-02 16:18:38 +02:00
Timm Baeder
f4aaf6fe5c
[clang][bytecode] Fix a crash in codegen (#151515)
getRecord() can return nullptr if any one of the fields does, in this
case because the array is too large for us to allocate.
2025-07-31 15:50:02 +02:00
Timm Baeder
2c00df3c93
[clang][bytecode][NFC] Remove LabelScope (#151498)
It did almost nothing
2025-07-31 13:34:21 +02:00
Eli Friedman
0bbe1b30fa
[clang] Forbid reinterpret_cast of function pointers in constexpr. (#150557)
This has been explicitly forbidden since C++11, but somehow the edge
case of converting a function pointer to void* using a cast like
`(void*)f` wasn't handled.

Fixes #150340 .
2025-07-30 18:15:17 -07:00
Timm Baeder
904de95e71
[clang][bytecode][NFC] Fix a few clang-tidy complaints (#150940) 2025-07-28 15:57:49 +02:00
Timm Baeder
23eef9a7c4
[clang][bytecode] Activate primitive fields before initializing them (#149963)
The initializer itself might need the field to be active.
2025-07-23 06:09:27 +02:00
Timm Baeder
01b47eb86c
[clang][bytecode] Only implicitly start lifetime of trivially-default-constructible union members (#149835)
See
faee39baa8
2025-07-23 05:28:37 +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
fed6db45df
[clang][bytecode] Diagnose dereferencing a null pointer (#149330) 2025-07-19 21:39:06 +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
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
c0422733e3
[clang][bytecode] Check lambda captures before binding decls (#148130)
If the given decls is a lambda capture (but also a BindingDecl), handle
it as a lambda capture instead of a BindingDecl.
2025-07-11 10:51:59 +02:00
Timm Baeder
376b3f7049
[clang][bytecode] Devirtualize calls during con- and destruction (#147685)
When compiliung compiling a ctor or dtor, we need to devirtualize the
virtual function calls so we always call the implementation of the
current class.
2025-07-09 14:37:10 +02:00
Timm Baeder
65f94d7518
[clang][bytecode] Don't crash on erroneous switch conditions (#147533)
Not attaching a test since I've only seen this when compiling a large
c++26 test case as c++17.
2025-07-08 17:14:02 +02:00
Timm Baeder
4c98da2cad
[clang][bytecode] Create a temporary for discarded CXXBindTemporaryExprs (#147303)
So we run the destructor.
2025-07-08 06:00:52 +02:00
Timm Baeder
9c7320e5d3
[clang][bytecode] Fix visiting for-range loop variable (#147188)
Make sure we're properly registering DecompositionDecls.
2025-07-06 18:59:24 +02:00