274 Commits

Author SHA1 Message Date
NagaChaitanya Vellanki
595c5bcdc5
[X86][Clang] Add constexpr support for _mm_min_ss/_mm_max_ss/_mm_min_sd/_mm_max_sd/_mm_min_sh/_mm_max_sh intrinsics (#178029)
- Added boolean IsScalar argument to the helper functions in
InterpBuiltin/ExprConstant
- Made minsh_round_mask, maxsh_round_mask constexpr only for
_MM_FROUND_CUR_DIRECTION rounding mode.
- Added helper function for scalar round mask in
InterpBuiltin/ExprConstant

Resolves #175198
2026-02-23 14:03:57 +00:00
Timm Baeder
c2b69b1a17
[clang][bytecode] Do a full CheckLoad in builtin_memcmp() (#181337)
This handles unknown size arrays as well and prints the expected
diagnostics.
2026-02-13 11:06:40 +01:00
Timm Baeder
900fac763f
[clang][bytecode] Check strlen element size (#181333)
We're otherwise running into an assertion later, so do the check early.
2026-02-13 10:34:27 +01:00
Timm Baeder
6906c5c033
[clang][bytecode] Fix __builtin_nan* for non-primitive arrays (#181326)
They might've been casted, so we need t check for it, not assert.
2026-02-13 10:11:31 +01:00
Madhur Kumar
5d057a125c
[clang] Add __builtin_bitreverseg (#179126)
fixes #177125
2026-02-12 17:39:22 +00:00
Timm Baeder
0f8d8dc5b9
[clang][bytecode] Reject composite copies on primitive pointers (#180683)
This should fail.
2026-02-10 08:45:11 +01:00
Timm Baeder
4366324c89
[clang][bytecode] Improve __builtin_object_size handling (#179271)
This fixes a few more tests from `pass-object-size.c`, but we still
can't enable the entire file.
2026-02-05 08:32:50 +01:00
Timm Baeder
5da7e9a2ad
[clang][bytecode] Return Invalid() from atomic_is_lock_free calls (#179676)
If they are invalid. This is what the current interpreter does.
2026-02-04 16:11:13 +01:00
woruyu
f541056513
[Clang] Fixes builtin_bswapg builtin for bool type (#179177)
This PR resolves #178317, the related PR: #169285
2026-02-04 13:49:45 +00:00
puneeth_aditya_5656
5b80848f06
[clang][bytecode] Fix crash on __builtin_infer_alloc_token with struct argument (#178936)
## Summary
- Fix crash when passing non primitive types (structs) to
`__builtin_infer_alloc_token`
- The bytecode interpreter's discard loop dereferenced an empty
`OptPrimType` for non primitive arguments

## Test plan
- Added regression test in `clang/test/SemaCXX/alloc-token.cpp`
- Existing tests continue to pass

Fixes #178892
2026-02-02 17:15:46 +01:00
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
marius doerner
8e7a2d8b42
Revert "[clang][bytecode] Use in Expr::tryEvaluateObjectSize (#1790… (#179099)
…33)"

This reverts commit 756c321c33af2be0bd40707948aae3c06163a0a6.

Test failure in clang/test/AST/ByteCode/builtins.c in CI build

CC @tbaederr
2026-02-01 12:34:09 +01:00
marius doerner
756c321c33
[clang][bytecode] Use in Expr::tryEvaluateObjectSize (#179033)
Fixes #138474

Use new bytecode intepreter in `Expr::tryEvaluateObjectSize`. Reuses the
already existing implementation for `__builtin_object_size` in of the
intepreter.

---------

Co-authored-by: Timm Baeder <tbaeder@redhat.com>
2026-02-01 10:08:42 +01:00
puneeth_aditya_5656
5c213a9000
[clang][bytecode] Fix crash on __builtin_align_up with one-past-end pointers (#178652)
## Summary
Fix assertion failure when evaluating
`__builtin_align_up`/`__builtin_align_down`/`__builtin_is_aligned` with
one-past-end pointers like `&array[size]`.

## Root Cause
`getIndex()` calls `getOffset()` which asserts when `Offset ==
PastEndMark`. This happens for one-past-end element pointers.

## Fix
Check `isElementPastEnd()` before calling `getIndex()`. For past-end
pointers, use `getNumElems()` instead which gives the correct index
value.

## Test
Added test cases in `builtin-align-cxx.cpp` for one-past-end pointer
alignment.

Fixes #178647
2026-01-31 16:34:53 +01:00
byteforge
e72c4fc734
[clang][constexpr] Move inf/nan/denormal handling into FP binop callbacks (#178421)
Update the callback signature for `EvaluateFpBinOpExpr` and
`interp__builtin_elementwise_fp_binop` to return
`std::optional<APFloat>`, allowing individual callbacks to decide
whether to handle special floating-point cases (inf/nan/denormal).

Previously, the helper functions had hardcoded validation that forced
all callbacks to reject these cases. This blocked intrinsics needing
custom validation (e.g., rounding mode checks). Now each callback
controls its own validation and returns `std::nullopt` when the fold is
invalid.

Fixes #178416
2026-01-28 17:07:14 +00:00
Ayush Kumar Gaur
a150e8081a
[clang][bytecode] Avoid crash in constexpr wcslen on invalid argument… (#177891)
### What the problem ?

Fix a clang bytecode constant interpreter crash when evaluating
wcslen("x") in a constant-expression path.

Previously we asserted on wchar element size mismatch and crashed in
assertion builds.

### Why it happened

The expression is already ill-typed (char[2] → const wchar_t*), clang
correctly emits a diagnostic, but later ICE checking tries to fold the
expression and reaches the builtin handler. That path must not crash.

### whats the Fix

Replace the assert(ElemSize == wchar_t size) with a graceful failure
(return false / emit appropriate diagnostic) when element sizes don’t
match, so evaluation stops safely.

### how to Test it

Add a regression test that triggers ICE checking with
-fexperimental-new-constant-interpreter and ensures clang does not crash
(verifies it reports the type error / static_assert failure instead).

Fixes #177844.

<img width="1366" height="768" alt="Screenshot_2026-01-25_22_07_50"
src="https://github.com/user-attachments/assets/488dd0b0-3c17-4f45-b2d0-9339b8fd3409"
/>

where wclen_crash.c is 

`
#include <wchar.h>

static_assert(wcslen("x") == 'x');
int main() {}
`

---------

Co-authored-by: Timm Baeder <tbaeder@redhat.com>
2026-01-26 17:01:02 +01:00
Julian Pokrovsky
d836261ca3
[X86][Clang] allow CRC32 intrinsics to be used in constexp (#173908)
Mostly inspired by https://github.com/llvm/llvm-project/pull/152971

CRC32 implementation using reversed polynomial that does not match an
Intel manual, can be changed to canonical implementation if required (if
there is a canonical implementation we should use, please attach a link)

Closes #168881 
Part of #30794
2026-01-21 16:34:30 +00:00
NagaChaitanya Vellanki
370529c7f7
[clang] Implement __builtin_stdc_rotate_left, __builtin_stdc_rotate_right (#160259)
This patch adds type-generic rotate builtins that accept any unsigned
integer
type. These builtins provide:

- Support for all unsigned integer types, including _BitInt
- Constexpr evaluation capability
- Automatic normalization of rotation counts modulo the bit-width
- Proper handling of negative rotation counts (converted to equivalent
  positive rotations in the opposite direction)
- Implicit conversion support for both arguments for
  types with conversion operators. 

The builtins follow C23 naming conventions.

Resolves https://github.com/llvm/llvm-project/issues/122819
2026-01-21 10:30:45 +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
Timm Baeder
222fff827e
[clang][bytecode] Remove base casts in builtin_object_size (#176111)
If UseFieldDesc is set, we should use the "closest surrounding
subobject", which apparently cannot be a subclass pointer.

Fixes https://github.com/llvm/llvm-project/issues/176079
2026-01-15 11:44:56 +01:00
Simon Pilgrim
8784816a41
[ADT] Add APInt::truncSSatU truncation from signed to unsigned variant (#175603)
This matches the behaviour of ISD::TRUNCATE_SSAT_U and X86ISD::PACKUS
truncations: saturate signed input to unsigned result -
`truncate(smin(smax(x, 0), C))`

Add unit test coverage and update existing PACKUS constant folding to
use the APInt helper
2026-01-13 17:51:07 +00:00
Timm Baeder
25b4d1e1de
[clang][bytecode] Don't evaluate builtin_assume argument (#175740)
This is what the current interpreter does.
2026-01-13 12:33:58 +01:00
NagaChaitanya Vellanki
52d6170c9f
[X86][Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - Allow SSE/AVX FP MAX/MIN intrinsics to be used in constexpr (#171966)
* Implemented a generic function interp__builtin_elementwise_fp_binop
* NaN, Infinity, Denormal cases can be integrated into the lambda in
future. For, now these cases are hardcoded in the generic function

Resolves: #169991
2026-01-12 14:50:54 +00:00
Simon Pilgrim
7490901ea0
[ByteCode] InterpBuiltin.cpp - consistently use castAs<> if dereferencing the result (#174781)
castAs<> will at least assert the cast is valid while getAs<> will always just return nullptr and then explode
2026-01-07 16:23:12 +00:00
Timm Baeder
da5845d033
[clang][bytecode] Check builtin_memchr for non-block pointers earlier (#174192)
The getType() call might fail. We can't pull the isReadable() check up
though because that creates different diagnostic output compared to the
current interpreter.

Fixes #172202
2026-01-02 12:00:45 +01:00
Timm Baeder
db9549c4c0
[clang][bytecode] Check builtin_memchr() for one-past-end pointers (#174187)
We can't read from them and this fails later.

Fixes https://github.com/llvm/llvm-project/issues/173942
2026-01-02 10:50:55 +01:00
Timm Baeder
ed6698e28c
[clang][bytecode] Check builtin_memcpy() for non-block pointers (#174184)
This pretty hard to produce in C++ but easy in C.

Fixes #171609
2026-01-02 10:01:57 +01:00
sskzakaria
2c0565fcff
[X86][Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - allow AVX512 mask predicate intrinsics to be used in constexpr (#173739)
Enables constexpr evaluation for the following AVX512 Integer Comparison Intrinsics:
```
_mm512_kmov

_mm_movm_epi8 _mm256_movm_epi8 _mm512_movm_epi8
_mm_movm_epi16 _mm256_movm_epi16 _mm512_movm_epi16
_mm_movm_epi32 _mm256_movm_epi32 _mm512_movm_epi32
_mm_movm_epi64 _mm256_movm_epi64 _mm512_movm_epi64
```
FIXES #162072
2025-12-28 15:47:03 +00:00
Abdelrehim, Ahmed Yaser Farouk
8bd5ba7af7
[Clang] Allow AVX/AVX2 lane permute operations in constexpr (#172149)
Resolves #169312 
Enables the usage of the following X86 intrinsics in `constexpr`:
```c
_mm256_permute2f128_pd	    _mm256_permute2f128_ps
_mm256_permute2f128_si256    _mm256_permute2x128_si256
```
2025-12-18 13:41:05 +00:00
Ahmed Nour
ed79fd714f
[Clang][x86]: allow PCLMULQDQ intrinsics to be used in constexpr (#169214)
Resolves #168741
2025-12-15 10:27:17 +00: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
Aiden Grossman
392c302d79
[Clang] Fix unused variable warning from 1911ce132659222aee353882bd55… (#171223)
…70d689745a7d

These are only used in assertions so trigger warnings in release builds.
Fix this per the LLVM programming standards.
2025-12-08 23:25:09 +00:00
NagaChaitanya Vellanki
1911ce1326
[Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - Allow GFNI intrinsics to be used in constexpr (#169619)
Resolves #169295
2025-12-08 22:18:41 +00:00
adbox53
95470b6c62
Replace interp__builtin_blend with interp__builtin_ia32_shuffle_gener… (#170217)
Fixes #169994

---------

Co-authored-by: Timm Baeder <tbaeder@redhat.com>
2025-12-08 08:24:51 +00: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
Ahmed Nour
8f6e95ef45
[Clang][X86] Add constexpr support for permute4x64_pd and permute4x64_epi64 (#170442)
This PR adds constexpr support for the AVX2 cross-lane permute
intrinsics _mm256_permute4x64_pd and _mm256_permute4x64_epi64

Resolves https://github.com/llvm/llvm-project/issues/169304
2025-12-03 20:19:54 +00:00
Medha Tiwari
907c94b3c2
[X86][Clang] Add constexpr support for AVX512 kshift intrinsics (#170480)
Add AVX512 kshiftli/kshiftri mask intrinsics to be used in constexpr.

Enables constexpr evaluation for:
- `_kshiftli_mask8/16/32/64`
- `_kshiftri_mask8/16/32/64`

Fixes #162056
2025-12-03 18:38:40 +00:00
Hamza Hassanain
22d354a2f2
[X86][Clang] Support constexpr evaluation of cvtpd2ps intrinsics (#169980)
This patch implements constant evaluation support for the following X86
intrinsics:
- _mm_cvtpd_ps, _mm256_cvtpd_ps (Packed Double to Float)
- _mm_cvtsd_ss (Scalar Double to Float merge)
- Masked variants of the above

It implements the strict "Exact and Finite" rule: conversions that are
inexact, infinite, or NaN are rejected in constexpr contexts.

Fixes #169370
2025-12-03 11:59:37 +00:00
Timm Baeder
ff3d550d7e
[clang][bytecode][NFC] Add popToUInt64() to builtin evaluation (#170164)
We often don't need the APSInt at all, so add a version that pops the
integral from the stack and just static_casts to uint64_t.
2025-12-02 05:40:15 +01:00
Muhammad Abdul
dae9139d8f
[X86][Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - allow AVX512 kmov intrinsics to be used in constexp (#169895)
Resolves #166975
2025-12-01 17:24:16 +00:00
Timm Baeder
48931e5e59
[clang][bytecode] Check memcmp builtin for one-past-the-end pointers (#170097)
We can't read from those and will run into an assertion sooner or later.

Fixes https://github.com/llvm/llvm-project/issues/170031
2025-12-01 12:43:35 +01:00
Islam Imad
246528cb3a
[clang][bytecode] Unify elementwise integer builtins using callback pattern (#169957)
This patch refactors the handling of elementwise integer unary
operations to use a unified callback-based approach, eliminating code
duplication.

Changes:
- Extended interp__builtin_elementwise_int_unaryop to handle vector types
- Replaced BI__builtin_elementwise_popcount with callback invocation
- Replaced BI__builtin_elementwise_bitreverse with callback invocation
- Removed  interp__builtin_elementwise_popcount function

The new approach uses a lambda function to specify the operation
(popcount or reverseBits), which is applied uniformly to both scalar and
vector operands. This reduces code duplication and makes it easier to
add similar builtins in the future.

Fixes #169657
2025-11-29 17:38:17 +00:00
NagaChaitanya Vellanki
f8e77168c8
[Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - Allow PSLL/PSRA/PSRL var intrinsics to be used in constexpr (#169276)
Resolves #169176
2025-11-27 14:55:11 +00:00
Eric Xu
eee09ca984
[X86][Clang] Allow constexpr evaluation of F16C CVTPS2PH intrinsics (#162295)
Fixes #160312
2025-11-27 12:57:50 +00:00
NagaChaitanya Vellanki
c3c3d16773
[Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - Allow AVX512 VPMULTISHIFTQB intrinsics to be used in constexpr (#168995)
Resolves #167477
2025-11-27 10:26:32 +00:00
Moritz Zielke
954fa0f9ba
[Clang] Allow AVX/AVX512 subvector shuffles in constexpr (#168700)
Resolves #160514

Enables usage of the following x86 intrinsics in `constexpr`:

```
_mm256_shuffle_i64x2 _mm256_mask_shuffle_i64x2  _mm256_maskz_shuffle_i64x2 
_mm256_shuffle_f64x2 _mm256_mask_shuffle_f64x2  _mm256_maskz_shuffle_f64x2 
_mm512_shuffle_i64x2 _mm512_mask_shuffle_i64x2  _mm512_maskz_shuffle_i64x2 
_mm512_shuffle_f64x2 _mm512_mask_shuffle_f64x2  _mm512_maskz_shuffle_f64x2 

_mm256_shuffle_i32x4 _mm256_mask_shuffle_i32x4  _mm256_maskz_shuffle_i32x4 
_mm256_shuffle_f32x4 _mm256_mask_shuffle_f32x4  _mm256_maskz_shuffle_f32x4 
_mm512_shuffle_i32x4 _mm512_mask_shuffle_i32x4  _mm512_maskz_shuffle_i32x4 
_mm512_shuffle_f32x4 _mm512_mask_shuffle_f32x4  _mm512_maskz_shuffle_f32x4 
```
2025-11-26 17:34:21 +00:00
Timm Baeder
6459f39c37
[clang][bytecode] Add some convenience API to BitcastBuffer (#169516)
So we check the offsets before using them.
2025-11-26 06:36:58 +01:00
NagaChaitanya Vellanki
456ca91815
[Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - Allow AVX512 VPSHUFBITQMB intrinsics to be used in constexpr (#168100)
Resolves #161337
2025-11-22 17:19:27 +00:00
stomfaig
2e424deeb6
[Clang][X86] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - allow VPERMILPD/S variable mask intrinsics to be used in constexpr (#168861)
Allowing VPERMILPD/S intrinsics to be used in constexpr

Closes #167878
2025-11-22 14:37:50 +00:00
Muhammad Abdul
7305b6eb54
[clang][X86] Allow VALIGND/Q element-shift intrinsics in constexpr evaluation (#168206)
Fixes #167681
2025-11-22 11:53:34 +00:00