35 Commits

Author SHA1 Message Date
Timm Baeder
ef409e649c
[clang][bytecode] Add an assertion in initScope() op (#189972)
I've run into a problem in a branch lately that would've benefitted from
this assertion.
2026-04-02 17:49:46 +02:00
Timm Baeder
78f267f01d
Reapply "[clang][bytecode] Allocate local variables in InterpFrame … (#187644)
…tail storage" (#187410)

This reverts commit bf1db77fc87ce9d2ca7744565321b09a5d23692f.

Avoid using an `InterpFrame` member after calling its destructor this
time. I hope that was the only problem.
2026-03-20 10:11:21 +01:00
Timm Baeder
bf1db77fc8
Revert "[clang][bytecode] Allocate local variables in InterpFrame tail storage" (#187410)
Reverts llvm/llvm-project#185835

Looks like this broke two msan builders:
https://lab.llvm.org/buildbot/#/builders/164/builds/19819
https://lab.llvm.org/buildbot/#/builders/94/builds/16257
2026-03-19 19:29:56 -07:00
Timm Baeder
63b44decb5
[clang][bytecode] Allocate local variables in InterpFrame tail storage (#185835)
Instead of heap-allocating an `InterpFrame` and then immediately
heap-allocating more space for the local variables, do only one
heap-allocation and use tail storage for the local variables.
We already know how many bytes we need to for the tail storage after
all.
This also makes `InterpFrame` a little smaller since we don't need to
save an explicit pointer for the local variable memory.






For an artificial test case doing lots of function calls with local
variables like:
```c++
constexpr int plus(int a, int b) {
        int x = a;
        int y = b;
        int z = x + y;
        return z;
}

constexpr int minus(int a, int b) {
        int x = a;
        int y = b;
        int z = x - y;
        return z;
}
constexpr int foo() {
        int a = 0;
        for (unsigned I = 0; I != 1'000'000; ++I) {
                int b = I;
                a = plus(a,b );
                a = minus(a,I);
        }
        return a;
}
static_assert(foo() == 0);
```
this saves us over 6%.

We also eliminate the per-argument `Block` heap allocation on the first
pointer-access to an argument the same way. To make this work, we change
the param ops to use the parameter index instead of the offset.
2026-03-18 17:08:23 +01:00
Timm Baeder
afb2e4f2e2
[clang][bytecode] Clean up interp::Function parameter handling (#178621)
Replace the multiple data structures with a vector + a map holding all
`ParamDescriptor`s. Update docs.
2026-01-30 10:07:23 +01:00
Timm Baeder
7c3a2113fe
[clang][ExprConst] Fix rendering of explicit this parameters (#177551)
in compile-time backtraces.

The two test cases used to be rendered as `foo(s, 0)` and `foo2(s)`.
2026-01-28 12:59:24 +01:00
Timm Baeder
9ec0eccf48
[clang][bytecode] Fix a crash with explicit this parameters (#177154)
Fixes https://github.com/llvm/llvm-project/issues/177133
2026-01-22 12:42:28 +01:00
Timm Baeder
7e1696f1e4
[clang][bytecode][NFC] Use a ListSeparator to print backtraces (#177148) 2026-01-21 13:54:55 +01:00
Timm Baeder
95f0fab7fa
[clang][bytecode] Fix conditional operator scoping wrt. local variables (#169030)
We used to create a scope for the true- and false expression of a
conditional operator. This was done so e.g. in this example:

```c++
  struct A { constexpr A(){}; ~A(); constexpr int get() { return 10; } }; // all-note 2{{declared here}}
  static_assert( (false ? A().get() : 1) == 1);
```

we did _not_ evaluate the true branch at all, meaning we did not
register the local variable for the temporary of type `A`, which means
we also didn't call it destructor.

However, this breaks the case where the temporary needs to outlive the
conditional operator and instead be destroyed via the surrounding
`ExprWithCleanups`:
```
constexpr bool test2(bool b) {
  unsigned long __ms = b ? (const unsigned long &)0 : __ms;
  return true;
}
static_assert(test2(true));
```
Before this patch, we diagnosed this example:
```console
./array.cpp:180:15: error: static assertion expression is not an integral constant expression
  180 | static_assert(test2(true));
      |               ^~~~~~~~~~~
./array.cpp:177:24: note: read of temporary whose lifetime has ended
  177 |   unsigned long __ms = b ? (const unsigned long &)0 : __ms;
      |                        ^
./array.cpp:180:15: note: in call to 'test2(true)'
  180 | static_assert(test2(true));
      |               ^~~~~~~~~~~
./array.cpp:177:51: note: temporary created here
  177 |   unsigned long __ms = b ? (const unsigned long &)0 : __ms;
      |                                                   ^
1 error generated.
```
because the temporary created for the true branch got immediately
destroyed.

The problem in essence is that since the conditional operator doesn't
create a scope at all, we register the local variables for both its
branches, but we later only execute one of them, which means we should
also only destroy the locals of one of the branches.

We fix this similar to clang codgen's `is_active` flag: In the case of a
conditional operator (which is so far the only case where this is
problematic, and this also helps minimize the performance impact of this
change), we make local variables as disabled-by-default and then emit a
`EnableLocal` opcode later, which marks them as enabled. The code
calling their destructors checks whether the local was enabled at all.
2025-11-24 07:34:48 +01:00
Timm Baeder
c750487292
[clang][bytecode] Diagnose volatile writes (#160350) 2025-09-28 07:21:40 +02:00
Timm Baeder
b98f0b8209
[clang][bytecode] Use stack offsets for This/RVO ptrs (#160285)
Instead of keeping the `Pointer`s itself in `InterpFrame`, just save
them as offsets and use stackRef<>() when we need them.
2025-09-24 10:30:51 +02:00
Timm Baeder
3f6cd761ba
[clang][bytecode] Remove InterpFrame::IsBottom flag (#159279)
The bottom frame is the one without a parent. No need to save that
information separately.
2025-09-17 10:44:34 +02:00
Timm Baeder
a805dfbc9f
[clang][bytecode] Remove split frame business (#155980)
This was never really needed.
2025-08-29 09:42:46 +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
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
3bb4355bb8
[clang][bytecode] Report mutable reads when copying unions (#149320) 2025-07-18 11:10:57 +02:00
Kazu Hirata
c9cdc33dd6
[clang] Remove unused includes (NFC) (#146254)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-06-28 20:41:46 -07:00
Timm Baeder
79eed76c58
[clang][bytecode][NFC] Remove incorrect comment (#139571)
We don't create function frames for builtin functions anymore.
2025-05-13 08:09:26 +02:00
Timm Baeder
4258998654
[clang][bytecode] Only print ptr/reference types via toAPValue() (#137965)
Otherwise, convert them to an RValue to print them. This fixes the
printing of e.g. complex values.
2025-05-01 06:04:59 +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
16c721f2d1
[clang][bytecode] Destroy local variables in reverse order (#125727)
See the attached test case.
2025-02-05 08:09:13 +01:00
Timm Baeder
06130ed341
Reapply "[clang][bytecode] Stack-allocate bottom function frame" (#12… (#125349)
…5325)

Move the BottomFrame to InterpState instead.
2025-02-01 18:12:35 +01:00
Jorge Gorbe Moya
386af4a5c6
Revert "[clang][bytecode] Stack-allocate bottom function frame" (#125325)
Reverts llvm/llvm-project#125253

It introduced an msan failure. Caught by a buildbot here:
https://lab.llvm.org/buildbot/#/builders/164/builds/6922/steps/17/logs/stdio
2025-01-31 20:09:14 -08:00
Timm Baeder
f3549814f8
[clang][bytecode] Stack-allocate bottom function frame (#125253)
Instead of heap-allocating it. This is similar to what the current
interpeter does. In C, we have no function calls, so the extra heap
allocation never makes sense.
2025-01-31 20:08:58 +01:00
Timm Baeder
3c8dfce3de
[clang][bytecode][NFC] Use RetPC in InterpFrame::getExpr() as well (#125200)
Both getLocation() and getRange() use the RetPC if the current function
doesn't have a usable body. Using PC here was just a typo.
2025-01-31 12:54:42 +01:00
Timm Baeder
ceaf6e912a
[clang][bytecode] Support ImplicitValueInitExpr for multi-dim arrays (#117312)
The attached test case from
https://github.com/llvm/llvm-project/issues/117294 used to cause an
assertion because we called classifPrim() on an array type.

The new result doesn't crash but isn't exactly perfect either. Since the
problem arises when evaluating an ImplicitValueInitExpr, we have no
proper source location to point to. Point to the caller instead.
2024-11-25 12:15:31 +01:00
Timm Baeder
4b96400240
[clang][bytecode] Allow placement-new in std functions pre-C++26 (#109753) 2024-09-24 10:28:54 +02:00
Timm Baeder
898fd396b8
[clang][bytecode] Check allocation size limit for operator new (#109590) 2024-09-23 09:59:41 +02:00
Timm Baeder
d03822d888
[clang][bytecode] Fix lookup of source locations in implicit ctors (#107992)
Implicit functions may still have a body. The !hasBody() check is
enough.
2024-09-11 07:21:49 +02:00
Timm Baeder
83fea8b809
[clang][bytecode] Allow continuing when discarded MemberExpr Base fails (#107231)
We don't need the value in this case, since we're discarding it anyway.
Allow continuing the interpretation but note the side effect.
2024-09-07 09:33:27 +02:00
Timm Baeder
733a92d7bc
[clang][bytecode] Print Pointers via APValue (#107056)
Instead of doing this ourselves, just rely on printing the APValue.
2024-09-03 11:07:19 +02:00
yronglin
d9e7286019
[NFC][clang][bytecode] Rename clang::interp::State::getCtx to clang::interp::State::getASTContext (#106071)
The new constant interpreter's `clang::interp::InterpState` contains
both `clang::interp::Context` and `clang::ASTContext`. So using `S.Ctx`
and `S.getCtx()` was a bit confusing. This PR rename `getCtx()` to
`getASTContext` to make things more clearer.

Signed-off-by: yronglin <yronglin777@gmail.com>
2024-08-26 22:23:07 +08:00
Timm Baeder
a07aba5d44
[clang] Rename all AST/Interp stuff to AST/ByteCode (#104552)
"Interp" clashes with the clang interpreter and people often confuse
this.
2024-08-16 17:13:12 +02:00