254 Commits

Author SHA1 Message Date
KaiWeng
d9c7c76269
Revert "Ignore trailing NullStmts in StmtExprs for GCC compatibility." (#166036)
This reverts commit b1e511bf5a4c702ace445848b30070ac2e021241.

https://github.com/llvm/llvm-project/issues/160243
Reverting because the GCC C front end is incorrect.

---------

Co-authored-by: Jim Lin <jim@andestech.com>
2025-11-07 09:30:53 -05:00
Timm Baeder
e4467fbf30
[clang][ExprConst] Handle dependent switch case statements (#166533)
By rejecting them.

Fixes https://github.com/llvm/llvm-project/issues/165555
2025-11-06 12:52:11 +01:00
Timm Baeder
14c76437ee
[clang][bytecode] Check types when loading values (#165385)
We need to allow BitCasts between pointer types to different prim types,
but that means we need to catch the problem at a later stage, i.e. when
loading the values.

Fixes https://github.com/llvm/llvm-project/issues/158527
Fixes https://github.com/llvm/llvm-project/issues/163778
2025-11-05 16:02:19 +01:00
Timm Baeder
9af49ee474
[clang][bytecode] Handle discarded AddrLabelExprs properly (#165000)
emitDummyPtr() doesn't like to be called with DiscardResult set, so
check this first.

Fixes https://github.com/llvm/llvm-project/issues/164979
2025-10-27 10:28:09 +01:00
Timm Baeder
7c441b21b7
[clang][bytecode] Catch placement-new into invalid destination (#164804)
We failed to check for null and non-block pointers.

Fixes https://github.com/llvm/llvm-project/issues/152952
2025-10-24 08:08:18 +02:00
Timm Baeder
93bb5c699f
[clang][bytecode] Fix CXXConstructExpr for multidim arrays (#164760)
This is a thing apparently.

Fixes https://github.com/llvm/llvm-project/issues/153803
2025-10-24 06:36:05 +02:00
Timm Baeder
ece83ed7df
[clang][bytecode] Fix instance pointer in IndirectFieldDecl inits (#164751)
Points to the first chain link in this case.
2025-10-23 15:22:17 +02:00
Timm Baeder
0591297c8a
[clang][bytecode] Fix initializing primitive fields via initlist (#164789)
Fixes https://github.com/llvm/llvm-project/issues/152901
2025-10-23 14:19:13 +02:00
Timm Baeder
954a719056
[clang][bytecode] Fix unsigned wraparound behavior with bitfields (#164445)
and inc/dec operations. We need to truncate the new value to the
bitfield bitwidth.
2025-10-22 11:01:33 +02:00
Timm Baeder
9e7209c061
[clang][bytecode][NFC] Clean up variable creation (#164415) 2025-10-21 17:31:05 +02:00
Timm Baeder
7b91bb2046
[clang][bytecode] Fix redeclaring global externs without initializer (#164409)
Return the same value, whether we've already allocated the variable or
not.
2025-10-21 15:14:26 +02:00
Timm Baeder
c5f9a2a2b2
[clang][bytecode] Don't emit checkNull for function pointers (#164376)
Diagnose them later when we try to call the function pointer.
2025-10-21 14:49:03 +02:00
Matheus Izvekov
b516dcc998
[clang] NFC: rename TagType::getOriginalDecl back to getDecl (#163271)
This rename was made as part of
https://github.com/llvm/llvm-project/pull/147835 in order to ease
rebasing the PR, and give a nice window for other patches to get rebased
as well.

It has been a while already, so lets go ahead and rename it back.
2025-10-15 16:11:17 -03:00
Timm Baeder
93c830597c
[clang][bytecode] Fix integral cast edge case (#161506)
We were converting the `ASInt` to as sign-less `APInt` too early and
losing the sign information.
2025-10-01 13:56:38 +02:00
Timm Baeder
c750487292
[clang][bytecode] Diagnose volatile writes (#160350) 2025-09-28 07:21:40 +02:00
Timm Baeder
492b60937b
[clang][bytecode] Remove bogus Initializing special case (#159933)
This doesn't seem to be needed anymore and causes problems.

Fixes #159787
2025-09-23 12:55:48 +02:00
Timm Baeder
242a1e2fb1
[clang][bytecode] Load value of non-lvalue ArraySubscriptExpr (#160024)
As happens in C.

Fixes #158482
2025-09-23 09:47:23 +02:00
Timm Baeder
03e16c8e42
[clang][bytecode] Move generic lambda handling to Compiler (#159733)
So the static invoker's Function still points to the static invoker
instead of the call operator of the lambda record. This is important for
a later commit.
2025-09-19 12:51:02 +02:00
Timm Baeder
29c55d0f7b
[clang][bytecode] Improve error detection in BitCastPrim op (#158575)
Reject bitcasts to pointer types unless it's to `nullptr_t` and always
reject bitcasts to member pointer types.


Fixes #156174
2025-09-16 10:02:18 +02:00
Timm Baeder
c8b5b6e0a6
[clang][ExprConst] Reject unary vector shuffles (#158589)
This is not implemented at compile time and asserts in assertion builds,
so reject it here.


Fixed the coding style in `BuiltinShuffleVector` at the same time.

Fixes #158471
2025-09-16 07:58:15 +02:00
Timm Baeder
c46cf1ea3b
[clang][bytecode][NFC] Remove BlockScope (#158656)
Unused these days.
2025-09-16 05:10:13 +02:00
Timm Baeder
03c356c554
[clang][bytecode] Pass initializer along in evaluateAsInitializer() (#158056)
We just called `getInit()`, which isn't always correct and used the
wrong initializer in the module test case.
2025-09-15 07:39:11 +02:00
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