116 Commits

Author SHA1 Message Date
Rahul Joshi
6cf656eca7
[NFC][Clang][AST] Drop llvm:: in front of ArrayRef/MutableArrayRef (#145207) 2025-06-23 13:10:42 -07: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
4f9e6bad84
[clang][bytecode] Fix calling operator new with nothrow/align parameter (#144271)
Discard all the parameters we don't care about.
2025-06-16 08:28:52 +02:00
Shafik Yaghmour
3ca6ea0f3a
[Clang][ByteCode][NFC] Move APInt into pushInteger since it is being passed by value (#143578)
Static analysis flagged that we could move APInt instead of copy, indeed
it has a move constructor and so we should move into values for APInt.
2025-06-11 08:02:44 -07:00
Jie Fu
627e49e249 [AST] Fix an unused-function warning (NFC)
/llvm-project/clang/lib/AST/ByteCode/InterpBuiltin.cpp:26:13:
error: unused function 'isNoopBuiltin' [-Werror,-Wunused-function]
static bool isNoopBuiltin(unsigned ID) {
            ^
1 error generated.
2025-06-05 14:11:30 +08:00
Timm Baeder
c7a93efa50
[clang][bytecode] Pop builtin args from the stack while evaluating (#142832)
Instead of just peek()ing the values when evaluating the builtins and
later classify()ing all the call args once again to remove them, just do
it while evaluating. This saves quite a bit of code.
2025-06-05 07:36:25 +02:00
DeanSturtevant1
397fdb172a
Enclose code in nondebug in #ifndef NDEBUG/#endif (#142189)
A previous change to InterpBuiltin.cpp fixed an unused variable warning
by using [[maybe unused]] and (void).
The code actually serves no useful purpose in non-debug builds, so let's
not include it there.
2025-05-30 16:34:55 -04:00
Kazu Hirata
882e7331c4 [AST] Fix a warning
This patch fixes:

  clang/lib/AST/ByteCode/InterpBuiltin.cpp:767:12: error: unused
  variable 'PtrT' [-Werror,-Wunused-variable]
2025-05-29 15:48:14 -07:00
google-yfyang
8229f72b7b
Add "maybe_unused" to variable only used in assert (#142049) 2025-05-29 18:45:50 -04:00
Timm Baeder
b411f70c3b
[clang][bytecode] Make some builtins no-ops (#141952)
For some builtins, we dont' need to do anything, but due to the cleanup
code being the same for all builtins, we still had to duplicate the
value on the stack. Remove that and get rid of all the unnecessary
pressure on the InterpStack.
2025-05-29 20:02:37 +02:00
Timm Baeder
4efe7a590d
[clang][bytecode] Simplify diagnoseUnknownDecl if we're not diagnosing (#141910)
See the added comment.

This improves compile times a bit:

https://llvm-compile-time-tracker.com/compare.php?from=ac62f73f19ae9fb415d3fc423949b8d7543e8717&to=0d6cf47197a4ee11cdd1ee4a48ea38a2907c3d45&stat=instructions:u
2025-05-29 13:02:50 +02:00
Timm Baeder
4bf356bbd2
[clang][bytecode] Start implementing builtin_is_within_lifetime (#137765) 2025-04-29 10:57:30 +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
15579a8e72
[clang][bytecode] Check array sizes against step limit (#137679) 2025-04-29 05:47:22 +02:00
Timm Baeder
c52fdbe697
[clang][bytecode] Fix ia32_addcarry/subborrow (#137642)
RHS is followed by another Pointer.
2025-04-28 17:44:19 +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
e086d7b146
[clang][bytecode] Don't create function frames for builtin calls (#137607)
They don't have local variables etc. so don't create frames for them.
2025-04-28 13:11:15 +02:00
Timm Baeder
32059ce121
[clang][bytecode][NFC] Stop using Function in InterpBuiltin (#137597)
Prepare for the ultimate removal of Function instances for builtin
functions.
2025-04-28 11:41:49 +02:00
Timm Baeder
031101c66f
[clang][bytecode] Diagnose delete of non-heap-allocated blocks (#137475)
With std::allocator::deallocate() calls
2025-04-27 08:57:57 +02:00
Timm Baeder
211b51e471
[clang][bytecode] Propagate IsVolatile bit to subobjects (#137293)
For
```c++
  struct S {
    constexpr S(int=0) : i(1) {}
    int i;
  };
  constexpr volatile S vs;
```

reading from `vs.i` is not allowed, even though `i` is not volatile
qualified. Propagate the IsVolatile bit down the hierarchy, so we know
reading from `vs.i` is a volatile read.
2025-04-25 11:23:34 +02:00
Timm Baeder
1a78ef9a9e
[clang][bytecode] Allow casts from void* only in std::allocator calls (#136714)
Otherwise, add the missing diagnostic.
2025-04-23 08:00:57 +02:00
Timm Baeder
ea3eb8d625
[clang][bytecode] Fix bos/bdos with non-zero offset applied (#136482)
Compute the offset from the record layout.
Unfortunately, not all the test cases from the current interpreter work.
2025-04-20 20:12:47 +02:00
Timm Baeder
90c845fb3b
[clang][bytecode] Start implementing __builtin_{,dynamic}_object_size (#136478) 2025-04-20 12:20:21 +02:00
Timm Baeder
70e2acf0c5
[clang][bytecode] Check if operator delete calls are in the right frame (#136141)
This is only permitted in a std::allocator::deallocate frame.
2025-04-17 17:30:22 +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
2d63faead4
[clang][bytecode][NFC] Remove PT_FnPtr (#135947)
We don't need this anymore since we don't return it from classify()
anymore.
2025-04-16 13:21:25 +02:00
Timm Baeder
98ea512f72
[clang][bytecode] Clear inactive union fields when copying (#134982)
When copying unions, we need to only copy the active field of the source
union, which we were already doing. However, we also need to zero out
the (now) inactive fields, so we don't end up with dangling pointers in
those inactive fields.
2025-04-10 06:12:00 +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 Baeder
bcedb368e3
[clang][bytecode] Support composite arrays in memcpy op (#132775)
See the attached test case.
2025-03-25 07:17:10 +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
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
849e5ea94f
[clang][bytecode] Add Descriptor::getDataType() (#132681)
This returns the type of data in the Block, which might be different
than the type of the expression or declaration we created the block for.
This lets us remove some special cases from CheckNewDeleteForms() and
CheckNewTypeMismatch().
2025-03-24 09:47:52 +01:00
Timm Baeder
5fc891b965
[clang][bytecode][NFC] Use getElemType() in __builtin_memchr as well (#132550)
For consistency.
2025-03-22 16:34:22 +01:00
Timm Baeder
c51d396f4d
[clang][bytecode] Fix __builtin_memmove type diagnostics (#132544)
Set the source type when allocating primitives so we can later retrieve
it.
2025-03-22 14:58:32 +01:00
Timm Baeder
6c8e9a6192
[clang][bytecode][NFC] Add assert to ptrauth_string_discriminator (#132527)
As pointed out by @shafik, this confuses static analysis and most
probably humans as well. Add an assertion to ensure the given array has
at least one element.
2025-03-22 08:05:28 +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
cf6a520a7a
[clang][bytecode] Fix builtin_memcmp buffer sizes for pointers (#130570)
Don't use the pointer size, but the number of elements multiplied by the
element size.
2025-03-10 15:51:31 +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
227f9544a4
[clang][bytecode][NFC] Bail out on non constant evaluated builtins (#130431)
If the ASTContext says so, don't bother trying to constant evaluate the
given builtin.
2025-03-09 06:35:19 +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