59 Commits

Author SHA1 Message Date
Timm Baeder
87e38d3b97
[clang][bytecode][NFC] Add Pointer::canDeref (#179618) 2026-02-04 09:18:23 +01:00
Timm Baeder
ab366170db
[clang][bytecode] Add lifetime information for primitive array elements (#173333)
Double the allocated size of the `InitMap` and use the second half for
lifetime information.
2026-01-21 09:41:35 +01:00
Timm Baeder
a15adf5b9b
[clang][bytecode][NFC] Add Pointer::stripBaseCasts() helper (#176875)
We do this is in a few places, so add a helper function for it.
2026-01-20 11:21:22 +01:00
keinflue
2d45a91095
[clang][bytecode] Fix void*-to-ptr casts originating from new/new[] (#174132)
In void*-to-ptr casts, the type of the pointed-to object in the source
operand needs to be compared to the target pointee type.

If a block was created for a `new`/`new[]`/`std::allocator` expression,
then a pointer needs to be stripped from the type of the expression
(which points to the single-object allocation or first element of the
allocation) to get the former.

`Descriptor::getType` did not do this and `Descriptor::getDataType`
returns an array type for array allocations. Therefore this introduces a
new function `Descriptor::getDataElemType` with the same behavior as
`Descriptor::getDataType`, except that it always produces the element
type in the array case and avoids the need for an `ASTContext`
reference. Make `Pointer::getType` use this function instead.

Fixes #174131
2026-01-02 19:07:19 +01:00
Timm Baeder
4ecca392d2
[clang][bytecode] Use record layout in Pointer::computeOffsetForComparison() (#172322)
Our internal representation for records does not include things like
alignment of the fields or padding bits, so we can't only rely on those
values, not even for just comparing two pointers.

Try to look up the offsets from the `ASTRecordLayout` instead.

Fixes https://github.com/llvm/llvm-project/issues/172165

---------

Co-authored-by: Shafik Yaghmour <shafik.yaghmour@intel.com>
2025-12-24 08:16:01 +01:00
Timm Bäder
773a6a9cc8 Revert "[clang][bytecode] Allocate InitMaps via Program/InterpState allocators (#170272)"
This reverts commit 8fe38c4c9c71c7a86ecdba476ee5bae4c02c0dfe.

This breaks the clang-armv7-2stage build bot:
https://lab.llvm.org/buildbot/#/builders/79/builds/2531
2025-12-09 06:19:09 +01:00
Timm Baeder
8fe38c4c9c
[clang][bytecode] Allocate InitMaps via Program/InterpState allocators (#170272)
Save them as a pointer intead of using a shared_ptr. This we we can use
the pointer integer value to differentiate the "no initmap yet" and "all
values initialzed" cases.

This regresses one test case in const-eval.c, but as it turns out, that
only worked coincidentally before.
2025-12-06 06:37:45 +01:00
Timm Baeder
bd95a74a2c
[clang][bytecode] Check for invalid record decls in IntPointer::atOffset (#169786)
We can't access the RecordLayout of an invalid decl, so return failure
if that happens.

Fixes https://github.com/llvm/llvm-project/issues/167076
2025-11-27 13:06:15 +01:00
Timm Baeder
90e1391d18
[clang][bytecode] Check pointers in GetPtrField{,Pop} (#167335)
The pointer needs to point to a record.

Fixes https://github.com/llvm/llvm-project/issues/166371
2025-11-17 08:24:44 +01:00
Timm Baeder
5f55048ed4
[clang][bytecode] Print field descriptor in Pointer::print() (#168002)
This is almost always useful information and ::print() is debug-only
code.
2025-11-14 06:06:23 +01:00
Simon Pilgrim
3c98be4146
[clang][bytecode] Pointer::isZero - fix MSVC "not all control paths return a value" warning. NFC. (#161168) 2025-09-29 11:03:25 +00:00
Timm Baeder
23473247c2
[clang][bytecode][NFC] Use switches for pointer type distinction (#160879)
In the important places. They are all fully covered switch statements so
we know where to add code when adding a new pointer type.
2025-09-26 16:59:16 +02:00
Timm Baeder
2155f17d39
[clang][bytecode] Optimize InitElem{,Pop} (#159084)
Try harder to avoid creating a new `Pointer` for the element.
2025-09-17 09:27:07 +02:00
Timm Baeder
c60972a2cf
[clang][bytecode][NFC] Surround Pointer diagram in \verbatim (#158550)
See if this fixes the documentation.
2025-09-15 09:19:26 +02:00
Timm Baeder
839916c8c9
[clang][bytecode] Speed up EvaluationResult::CheckArrayInitialized() (#155756)
For large primitive arrays, avoid creating a new `Pointer` for every
element (via `Pointer::isElementInitialized()`) or avoid iterating over
the array altogether (via `Pointer::allElementsInitialized()`).
2025-08-28 09:14:22 +02:00
Timm Baeder
9cc89bb238
[clang][bytecode] Simplify Pointer (#155170)
Now that we don't have the PointeeStorage pointer anymore, it's simpler
to access the members of the anonymous union directly instead of using
asBlockPointer(), etc.
2025-08-26 06:01:06 +02:00
Timm Baeder
07b2ba2600
Reapply "[clang][bytecode] Fix incorrect offset in elem() (#155157)" (#155276)
This reverts commit 9642aadf7064192164d1687378d28d6bda1978c9.

Since elem() only works on primitive arrays anyway, we don't have to do
the isArrayRoot() check at all.
2025-08-26 05:24:18 +02:00
Timm Bäder
9642aadf70 Revert "[clang][bytecode] Fix incorrect offset in elem() (#155157)"
This reverts commit 0f4db1a7955e511844d7a685103aa32617fc279d.

This breaks the armv8-quick builder:
https://lab.llvm.org/buildbot/#/builders/154/builds/20676
2025-08-25 15:00:02 +02:00
Timm Baeder
0f4db1a795
[clang][bytecode] Fix incorrect offset in elem() (#155157)
We need to use the base offset in both cases.
Also, add additional assertions to make sure we don't miss this case
again.

Fixes #155132
2025-08-25 14:09:59 +02:00
Timm Baeder
437d15a5ba
[clang][bytecode][NFC] Remove double initializer (#155150)
As pointed out in
https://github.com/llvm/llvm-project/pull/154405#discussion_r2296441643
2025-08-25 12:43:30 +02:00
Timm Baeder
8b091961b1
[clang][bytecode] Implement Pointer::getType() for function pointers (#154788)
Fixes #152920
2025-08-21 20:01:22 +02:00
Timm Baeder
e16ced3ef4
[clang][bytecode] Diagnose one-past-end reads from global arrays (#154484)
Fixes #154312
2025-08-20 10:34:44 +02:00
Timm Baeder
a9de444aa1
[clang][bytecode][NFC] Use an anonymous union in Pointer (#154405)
So we can save ourselves writing PointeeStorage all the time.
2025-08-20 09:23:19 +02:00
Timm Baeder
fb8ee3adb6
[clang][bytecode] Move pointers from extern globals to new decls (#154273) 2025-08-19 10:54:33 +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
31d2db2a68
[clang][bytecode][NFC] Use UnsignedOrNone for Block::DeclID (#154104) 2025-08-18 15:40:44 +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
fb49c6784a
[clang][bytecode] Move Pointer::{Prev,Next} into BlockPointer (#151097)
They are only relevant for block pointers.
2025-07-30 10:17:21 +02:00
Timm Baeder
4a44a85c89
[clang][bytecode] Add Pointer::initializeAllElements() (#151151)
To initialize all elements of a primitive array at once. This saves us
from creating the InitMap just to destroy it again after all elements
have been initialized.
2025-07-29 19:30:01 +02:00
Timm Baeder
904de95e71
[clang][bytecode][NFC] Fix a few clang-tidy complaints (#150940) 2025-07-28 15:57:49 +02:00
Timm Baeder
5b0935f1f0
[clang][bytecode] Reintroduce Pointer::elem() (#149693)
As a way of writing atIndex(I).deref<T>(), which creates an intermediate
Pointer, which in turn adds (and removes) that pointer from the pointer
list of the Block. This way we can avoid that.
2025-07-20 10:59:50 +02:00
Timm Baeder
b7660a5415
[clang][bytecode] Fix const-in-mutable fields (#149286)
For mutable and const fields, we have two bits in InlineDescriptor,
which both get inherited down the hierarchy. When a field is both const
and mutable, we CAN read from it if it is a mutable-in-const field, but
we _can't_ read from it if it is a const-in-mutable field. We need
another bit to distinguish the two cases.
2025-07-18 11:20:48 +02:00
Timm Baeder
130e6121f5
[clang][bytecode][NFC] Move Pointer::StorageKind above the union (#147942)
This is easier to read in debuggers and more common.
2025-07-10 15:05:28 +02:00
Timm Baeder
fb2c7610e8
[clang][bytecode] Fix comparing pointers pointing to base classes (#146285)
In the attached test case, one pointer points to the `Derived` class and
one to `Base`, but they should compare equal. They didn't because those
two bases are saved at different offsets in the block. Use
`computeOffsetForComparison` not just for unions and fix it to work in
the more general cases.
2025-07-05 19:42:50 +02:00
Timm Baeder
2e70da3fba
[clang][bytecode] Partially address string literal uniqueness (#142555)
This still leaves the case of the

constexpr auto b3 = name1() == name1();

test from cxx20.cpp broken.
2025-06-03 16:26:31 +02:00
Timm Baeder
e5f8998ac8
[clang][bytecode] Explicitly start variable lifetimes via placement new (#140221)
placement new /std::construct{,_at} can resurrect a variable after it's
destructor has been called.
2025-05-16 12:48:22 +02:00
Timm Baeder
ecc73a6f1c
[clang][bytecode] Use bytecode interpreter in EvaluateCharRangeAsString (#138461)
This was always using the ast walker.
2025-05-05 09:06:34 +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
9ae7aa79b1
[clang][bytecode] Diagnose comparing pointers to fields... (#137159)
... with different access specifiers.
2025-04-24 17:04:36 +02:00
Timm Baeder
559df834df
[clang][bytecode] Fix subtracting zero-sized pointers (#135929)
Add the appropriate diagnostic and fix the d-d case.
2025-04-16 10:48:42 +02:00
Timm Baeder
fafeaab6d9
[clang][bytecode] Misc TypeidPointer fixes (#135322)
Fix comparing type id pointers, add mor info when print()ing them, use
the most derived type in GetTypeidPtr() and the canonically unqualified
type when we know the type statically.
2025-04-11 10:35:28 +02:00
Timm Baeder
02f923f8e4
[clang][bytecode] Classify function pointers as PT_Ptr (#135026)
The Pointer class already has the capability to be a function pointer,
but we still classifed function pointers as PT_FnPtr/FunctionPointer.
This means when converting from a Pointer to a FunctionPointer, we lost
the information of what the original Pointer pointed to.
2025-04-10 06:40:54 +02:00
Timm Baeder
bdd087023f
[clang][bytecode] Fix various issues with multidimensional arrays (#134628)
This issue is very convoluted, but in essence, in the new version:

For a Pointer P that points to the root of a multidimensional, primitive
array:

`P.narrow()` does nothing.
`P.atIndex(0)` points `P[0]`
`P.atIndex(0).atIndex(0)` is the same as `P.atIndex(0)` (as before)
`P.atIndex(0).narrow().atIndex(0)` points to `P[0][0]`
`P.atIndex(0).narrow().narrow()` is the same as `P.atIndex(0).narrow()`.
2025-04-08 05:48:55 +02:00
Timm Baeder
7267dbfe10
[clang][bytecode] Fix comparing the addresses of union members (#133852)
Union members get the same address, so we can't just use
`Pointer::getByteOffset()`.
2025-04-01 09:00:46 +02:00
Timm Baeder
dff2ca424c
[clang][bytecode] Add special case for anonymous unions (#128681)
This fixes the expected output to match the one of the current
interpreter.
2025-02-25 12:46:06 +01:00
Timm Baeder
ee25a85ccc
[clang][bytecode] Handle CXXPseudoDestructorExprs (#125835)
Make lifetime management more explicit. We're only using this for
CXXPseudoDestructorExprs for now but we need this to handle
std::construct_at/placement-new after destructor calls later anyway.
2025-02-05 12:40:30 +01:00
Timm Baeder
51c7338cc6
[clang][bytecode] Fix dummy handling for p2280r4 (#124396)
This makes some other problems show up like the fact that we didn't
suppress diagnostics during __builtin_constant_p evaluation.
2025-01-29 09:32:35 +01:00
Timm Baeder
e86b68ff56
[clang][bytecode] Add support for typeid pointers (#121251)
Add it as another kind of pointer, saving both a `Type*` for the result
of the typeid() expression as well as one for the type of the typeid
expression.
2024-12-28 14:07:01 +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
ec871cfcdf
[clang][bytecode][NFC] Remove Pointer::elem() (#114046)
Unused.
2024-10-29 15:00:36 +01:00