37 Commits

Author SHA1 Message Date
Timm Baeder
d368773249
[clang][bytecode] Use in Expr::tryEvaluateObjectSize() (#179197)
This is like https://github.com/llvm/llvm-project/pull/179033, which
broke a few builders for reasons I still don't really understand. I ran
the other clang tests and this version fixes a few of the introduced
regressions.

This still regresses `CodeGen/pass-object-size.c`, but that's a
pre-existing issue.

Patch is of coursed based on #179033 by @mariusdr.
2026-02-02 14:03:13 +01:00
Timm Baeder
deafb6b105
[clang][bytecode][NFC] Use Block::deref() in EvalEmitter (#178630)
Instead of doing the casting around `Block::data()` ourselves.
2026-01-29 13:26:40 +01:00
Timm Baeder
ab56c9bfbc
[clang][bytecode] Check isActive() in EvalEmitter::speculate() (#174183)
If this opcode is being jumped-over, we have to ignore it.

Fixes #172191
2026-01-02 09:48:41 +01:00
Timm Baeder
db557bee1e
[clang][bytecode][NFC] Add Block::getBlockDesc<T>() (#172218)
Which returns the block-level descriptor. This way we don't have to do
the reinterpret_cast dance everywhere.
2025-12-15 09:21:41 +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
f5ffedf81a
[clang][bytecode] Pass SourceInfo objects by value (#159532)
They are only pointer-sized and copying them is cheaper than taking the
const ref.
2025-09-18 13:08:42 +02:00
Timm Baeder
6a16f1a6f4
[clang][ExprConst] Assert that EvaluateAsInitializer has non-null VD (#159274)
Both the expression (the initializer) as well as the VarDecl can't be
null here. Assert that.
2025-09-17 13:23:02 +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
94d5c54a4f
[clang][bytecode] Don't update temporary in InitGlobalTemp* (#158022)
We can save ourselves the conversion to an APValue here since we will do
that later in updateGlobalTemporaries() anyway.
2025-09-11 12:32:58 +02:00
Timm Baeder
8ba0d0f72f
[clang][bytcode][NFC] Use UnsignedOrNone for global ids (#157328) 2025-09-08 08:56:46 +02:00
Timm Baeder
83ca877159
[clang][bytecode] Reject final ltor casts on string literals (#156669)
Similar to what the current interpreter does.
2025-09-04 14:08:04 +02:00
Timm Baeder
02ade22aa8
[clang][bytecode] Avoid copying APValue into EvaluationResult (#155757)
Move the `APValue` into `EvaluationResult` instead.

For a large primitive array (`#embed` of the sqlite3 amalgamation), this
improves compile times by around 25%.
2025-08-28 08:46:06 +02:00
Timm Baeder
e0acf6592b
[clang][bytecode] Call CheckFinalLoad in all language modes (#154496)
Fixes #153997
2025-08-21 08:24:09 +02:00
Timm Baeder
870aa979c4
[clang][bytecode] Use visitExpr() in interpretCall (#152857)
This is the correct function to use and it will create a variable scope.

Fixes #152822
2025-08-09 18:30:26 +02: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
dc7c3c2b2b
[clang][bytecode] Disable location tracking for implicit field inits (#150190) 2025-08-05 13:14:01 +02:00
Timm Baeder
d02714c85a
[clang][bytecode] Return success for pointers to locals (#151980)
They aren't valid, but we leave them successful here and
CheckLValueConstantExpression will diagnose them later.
2025-08-05 05:18:47 +02:00
Timm Baeder
76e38ca981
[clang][bytecode] Try to load primitive values directly (#151833)
Instead of doing a GetPtrLocal + Load or GetPtrGlobal + Load pair, try
to load the value directly.
2025-08-04 12:28:33 +02:00
Timm Baeder
e39ee62c5b
[clang][bytecode] Use OptPrimType instead of std::optional<PrimType> (#149812)
We use this construct a lot. Use something similar to clang's
UnsignedOrNone.

This results in some slighy compile time improvements:
https://llvm-compile-time-tracker.com/compare.php?from=17a4b0399d161a3b89d8f0ce82add1638f23f5d4&to=a251d81ecd0ed45dd190462663155fdb303ef04d&stat=instructions:u
2025-07-21 17:16:13 +02:00
Timm Baeder
ef49ed4829
[clang][bytecode] Use bytecode interpreter in isPotentialConstantExprU… (#149462)
…nevaluated

Fake a function call to the given function and evaluate the given
expression as if it was part of that function call.

Fixes #149383
2025-07-19 07:18:51 +02:00
Timm Baeder
172c2817e5
[clang][bytecode] Use a SmallVector for EvalEmitter's locals (#140513)
The offset we return for them are just indices, so we can use a vector
here.
2025-05-19 11:19:35 +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
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
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
d08cf7900d
[clang][bytecode] Implement __builtin_constant_p (#130143)
Use the regular code paths for interpreting.

Add new instructions: `StartSpeculation` will reset the diagnostics
pointers to `nullptr`, which will keep us from reporting any diagnostics
during speculation. `EndSpeculation` will undo this.

The rest depends on what `Emitter` we use.

For `EvalEmitter`, we have no bytecode, so we implement `speculate()` by
simply visiting the first argument of `__builtin_constant_p`. If the
evaluation fails, we push a `0` on the stack, otherwise a `1`.

For `ByteCodeEmitter`, add another instrucion called `BCP`, that
interprets all the instructions following it until the next
`EndSpeculation` instruction. If any of those instructions fails, we
jump to the `EndLabel`, which brings us right before the
`EndSpeculation`. We then push the result on the stack.
2025-03-08 06:06:14 +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
6b8d076b1f
[clang][bytecode] Return failure for dead non-temporary pointers (#125247)
... even when we're not performing a lvalue-to-rvalue conversion.
2025-01-31 20:06:29 +01:00
Timm Bäder
a0bd40e5a3 Revert "[clang][bytecode] Mark global decls with diagnostics invalid (#122895)"
This reverts commit c1de9b920935e7d2fcc8dd7b39c1b7285783e948.

It looks like this breaks the memory-sanitizer builder:
https://lab.llvm.org/buildbot/#/builders/94/builds/3745
2025-01-14 18:37:36 +01:00
Timm Baeder
c1de9b9209
[clang][bytecode] Mark global decls with diagnostics invalid (#122895)
Even if their evaluation succeeds, mark them as invalid. This fixes some
long standing differences to the ast walker interpeter.
2025-01-14 15:10:48 +01:00
Kazu Hirata
dec6324cb0
[AST] Remove unused includes (NFC) (#116549)
Identified with misc-include-cleaner.
2024-11-17 09:36:48 -08:00
Timm Baeder
3eaf4a9d1a
[clang][bytecode] Check for memory leaks after destroying global scope (#112868)
The global scope we create when evaluating expressions might free some
of the dynamic memory allocations, so we can't check for memory leaks
before destroying it.
2024-10-18 13:03:13 +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
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
a14c730990
[clang][bytecode] Fix diagnostic in final ltor cast (#105292)
Don't diagnose volatile reads but diagnose a few other accesses earlier.
2024-08-21 08:08:32 +02: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