65 Commits

Author SHA1 Message Date
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
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
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
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
4bf356bbd2
[clang][bytecode] Start implementing builtin_is_within_lifetime (#137765) 2025-04-29 10:57:30 +02:00
Timm Baeder
980d027a3f
[clang][bytecode] Allow This pointers in CPCE mode (#137761)
The outermost function doesn't have a This pointers, but inner calls
can. This still doesn't match the diagnostic output of the current
interpreter properly, but it's closer I think.
2025-04-29 10:14:15 +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
adba24aa3c
[clang][bytecode] Add missing __builtin_memcpy checks (#135975)
Add a test for type punning and tests and the necessary checks for
non-trivially-copyable types and incomplete types.
2025-04-17 06:19:37 +02:00
Timm Baeder
ab7e0c0fc0
[clang][bytecode] Implement __builtin_wmem{cpy,move} (#135969) 2025-04-16 17:08:10 +02:00
Timm Baeder
a0e1e680d2
[clang][bytecode] Return Invalid() on non-constexpr builtins (#133700)
So the diagnostic output matches with the current interpreter
2025-03-31 18:53:12 +02:00
Timm Baeder
0bc2c5b2a4
Reapply "[clang][bytecode] Implement __builtin_{wcscmp,wcsncmp} (#132… (#132963)
…723)"

This reverts commit 1e2ad6793ac205607e7c809283cf69e1cc36a69a.


Fix the previous commit on big-endian hosts by _not_ falling through to
the `uint8_t` code path.
2025-03-26 08:19:31 +01:00
Timm Bäder
1e2ad6793a Revert "[clang][bytecode] Implement __builtin_{wcscmp,wcsncmp} (#132723)"
This reverts commit f7aea4d081f77dba48b0fc019f59b678fb679aa8.

This broke the clang-solaris11-sparcv9 builder:
https://lab.llvm.org/buildbot/#/builders/13/builds/6151
2025-03-25 07:15:30 +01:00
Timm Baeder
061b1d1149
[clang][bytecode] Redo RUN lines in the builtin-functions test (#132762)
Make sure we run each configuration once with the bytecode interpreter
and once with the current one. Add a triple to the one that was
previously without.
2025-03-24 18:33:36 +01:00
Timm Baeder
f7aea4d081
[clang][bytecode] Implement __builtin_{wcscmp,wcsncmp} (#132723) 2025-03-24 15:03:49 +01:00
Timm Baeder
9ab3b6a006
[clang][bytecode] Diagnose integral source/dest in memcpy (#132715)
Like the current interpreter does.
2025-03-24 12:44:35 +01:00
Timm Baeder
c7f14f601f
[clang][bytecode] Implement __builtin_wcschr (#132708)
This is already almost implemented, just need to enable support for it.
2025-03-24 12:28:23 +01:00
Timm Baeder
d67951694b
[clang][bytecode] Support overlapping regions in __builtin_memmove (#132523)
Unfortunately, a few circumstances make the implementation here less
than ideal, but we need to handle overlapping regions anyway.
2025-03-22 07:12:27 +01:00
Timm Baeder
7492666482
[clang][bytecode] Implement __builtin_wmemchr (#132254) 2025-03-20 21:01:14 +01:00
Timm Baeder
cfa07ccdfc
[clang][bytecode] Fix builtin_memchr with non-0 start index (#131633) 2025-03-17 19:02:55 +01:00
Timm Baeder
0f732481ac
[clang][bytecode] Fix getting pointer element type in __builtin_memcmp (#130485)
When such a pointer is heap allocated, the type we get is a pointer
type. Take the pointee type in that case.
2025-03-09 12:57:42 +01:00
Timm Baeder
46d218d1af
[clang][bytecode] Implement __builtin_{memchr,strchr,char_memchr} (#130420)
llvm has recently started to use `__builitn_memchr` at compile time, so
implement this. Still needs some work but the basics are done.
2025-03-08 16:52:06 +01:00
Timm Baeder
f838a5e96c
[clang][bytecode] Fix diagnostic difference with opaque call cmps (#129702)
Try to dig out the call expression and diagnose this as an opaque call.
2025-03-04 15:04:57 +01:00
Timm Baeder
53d433e702
[clang][bytecode] Only emit literal_comparison for string literals (#129691)
This is what the current interpreter does as well.
2025-03-04 14:07:53 +01:00
Timm Baeder
dfbd764a55
[clang][bytecode][NFC] Add failing memmove testcase (#126682)
Reduced from
libcxx/test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/move.pass.cpp
2025-02-11 08:45:24 +01:00
Timm Baeder
fd6baa477f
[clang][ExprConst] Add diagnostics for invalid binary arithmetic (#118475)
... between unrelated declarations or literals.

Leaving this small (I haven't run the whole test suite locally) to get
some feedback on the wording and implementation first.

The output of the sample in
https://github.com/llvm/llvm-project/issues/117409 is now:
```console
./array.cpp:57:6: warning: expression result unused [-Wunused-value]
   57 |   am - aj.af();
      |   ~~ ^ ~~~~~~~
./array.cpp:70:8: error: call to consteval function 'L::L<bx>' is not a constant expression
   70 |   q(0, [] {
      |        ^
./array.cpp:57:6: note: arithmetic on addresses of literals has unspecified value
   57 |   am - aj.af();
      |      ^
./array.cpp:62:5: note: in call to 'al(&""[0], {&""[0]})'
   62 |     al(bp.af(), k);
      |     ^~~~~~~~~~~~~~
./array.cpp:70:8: note: in call to 'L<bx>({})'
   70 |   q(0, [] {
      |        ^~~~
   71 |     struct bx {
      |     ~~~~~~~~~~~
   72 |       constexpr operator ab<g<l<decltype(""[0])>::e>::e>() { return t(""); }
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |     };
      |     ~~
   74 |     return bx();
      |     ~~~~~~~~~~~~
   75 |   }());
      |   ~~~
```

The output for 
```c++
int a, b;
constexpr int n = &b - &a
```

is now:
```console
./array.cpp:80:15: error: constexpr variable 'n' must be initialized by a constant expression
   80 | constexpr int n = &b - &a;
      |               ^   ~~~~~~~
./array.cpp:80:22: note: arithmetic involving '&b' and '&a' has unspecified value
   80 | constexpr int n = &b - &a;
      |                      ^
1 error generated.

```
2025-01-09 11:42:35 +01:00
Timm Baeder
34097c07e1
[clang][bytecode] Consider unknown-size arrays in memcpy/memcmp (#121462)
When emitting diagnostics for the number of elements.
2025-01-02 10:59:08 +01:00
Timm Baeder
d5c8af492f
[clang][bytecode] Consider start index when copying composite array (#121461)
... elements.
2025-01-02 10:31:49 +01:00
Timm Baeder
f0d60170cc
[clang][bytecode] Check memove/memcpy for available elements (#121383)
Both destination and source pointer need to have at least as many
elements as requested.
2024-12-31 12:14:27 +01:00
Timm Baeder
1f2d934525
[clang][bytecode] Support pointers in __builtin_mem{move,cpy} (#120560)
Unfortunately, that means we can't use the __builtin_bit_cast
implementation for this.
2024-12-19 16:38:58 +01:00
Timm Baeder
dd6f6a096a
[clang][bytecode] Handle builtin_wmemcmp (#120070) 2024-12-16 13:41:14 +01:00
Timm Baeder
98470c0b2e
[clang][bytecode] Handle __builtin_bcmp (#119678)
... the same as `__builtin_memcmp`. Also fix a bug we still had when we
couldn't find a difference in the two inputs after `Size` bytes.
2024-12-12 10:57:39 +01:00
Timm Baeder
8713914d76
[clang][bytecode] Handle __builtin_memcmp (#119544) 2024-12-12 08:59:35 +01:00
Timm Baeder
4dde52d76b
[clang][bytecode] Check for overlapping memcpy regions (#119535) 2024-12-11 12:06:56 +01:00
Timm Baeder
ce1587346b
[clang][bytecode] Allow checking builtin functions... (#119328)
... in checkingPotentialConstantExpression mode. This is what the
current interpreter does, yet it doesn't do so for
`__builtin_operator_new`.
2024-12-10 07:42:13 +01:00
Timm Baeder
6168739f00
[clang][bytecode] Reject memcpy sizes with element size remainder (#119209) 2024-12-09 16:57:49 +01:00
Timm Baeder
d17e51f306
[clang][bytecode] Handle __builtin_strncmp (#119208) 2024-12-09 16:17:39 +01:00
Timm Baeder
00b50c917b
[clang][bytecode] Handle __builtin_wcslen (#119187)
Handle different char widths in builtin_strlen.
2024-12-09 12:34:25 +01:00
Timm Baeder
abc27039be
[clang][bytecode] Pass __builtin_memcpy size along (#118649)
To DoBitCastPtr, so we know how many bytes we want to read.
2024-12-05 06:55:18 +01:00
Timm Bäder
61c2ac03d8 Revert "[clang][bytecode] Handle __builtin_wcslen (#118446)"
This reverts commit 89a0ee89973c3d213c4bc11c26b41eab67e06da0.

This breaks builders:
https://lab.llvm.org/buildbot/#/builders/13/builds/3885
2024-12-03 13:35:57 +01:00
Timm Baeder
3f39c5df08
[clang][bytecode] Reject memcpy dummy pointers after null check (#118460)
To match the diagnostic output of the current interpreter.
2024-12-03 11:51:49 +01:00
Timm Baeder
0f4dc4276f
[clang][bytecode] Initialize elements in __builtin_elementwise_popcount (#118457) 2024-12-03 11:14:06 +01:00
Timm Baeder
89a0ee8997
[clang][bytecode] Handle __builtin_wcslen (#118446)
... just like strlen.
2024-12-03 10:20:30 +01:00
Timm Baeder
789551362e
[clang][bytecode] Handle memmove like memcpy (#118431)
This is the same thing for us, except for diagnostic differences.
2024-12-03 07:56:06 +01:00
Timm Baeder
fc9052ee25
[clang][bytecode] Check __builtin_memcpy for null pointers (#118313) 2024-12-03 03:36:57 +01:00