1456 Commits

Author SHA1 Message Date
Timm Baeder
685e41e777
[clang][ExprConst] Reject field access with nullptr base (#113885)
Reject them if the base is null, not only if the entire pointer is null.

Fixes #113821
2024-11-21 15:53:11 +01:00
c8ef
ddb62d26cb
[clang] constexpr built-in reduce or and xor function. (#116976)
Part of #51787.
Follow up of #116822.

This patch adds constexpr support for the built-in reduce `or` and `xor`
functions.
2024-11-21 21:02:29 +08:00
c8ef
af0e0ded7b
[clang] constexpr built-in reduce and function. (#116822)
Part of #51787.
Follow up of #116626.

This patch adds constexpr support for the built-in reduce and function.
2024-11-20 19:57:41 +08:00
c8ef
b03a747fc0
[clang] constexpr built-in reduce mul function. (#116626)
Part of #51787.
Follow up of #116243.

This patch adds constexpr support for the built-in reduce mul function.
2024-11-19 20:47:42 +08:00
c8ef
1e4646d819
[clang] constexpr built-in reduce add function. (#116243)
Part of #51787.

This patch adds constexpr support for the built-in reduce add function.
If this is the right way to go, I will add support for other reduce
functions in later patches.

---------

Co-authored-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
2024-11-18 20:47:07 +08:00
Kazu Hirata
dec6324cb0
[AST] Remove unused includes (NFC) (#116549)
Identified with misc-include-cleaner.
2024-11-17 09:36:48 -08:00
Oleksandr T.
61a456bd5a
[Clang] prevent assertion failure in value-dependent initializer expressions (#112612)
Fixes #112140

--- 

```
CXXConstructExpr 0x14209e580 'const S':'const struct S' contains-errors 'void (const int &)' list
`-CXXDefaultArgExpr 0x14209e500 'const int' contains-errors
  `-RecoveryExpr 0x14209daf0 'const int' contains-errors
```

This change resolves an issue with evaluating `ArrayFiller` initializers
in _dependent_ contexts, especially when they involve a `RecoveryExpr`.
In certain cases, `ArrayFiller` initializers containing a `RecoveryExpr`
from earlier errors are incorrectly passed to `EvaluateInPlace`, causing
evaluation failures when they are value-dependent.

When this is the case, the initializer is processed through
`EvaluateDependentExpr`, which prevents unnecessary evaluation attempts
and ensures proper handling of value-dependent initializers in
`ArrayFillers`.
2024-10-24 10:34:58 +02:00
c8ef
332ac18e31
[clang] constexpr built-in abs function. (#112539)
According to [P0533R9](https://wg21.link/P0533R9), the C++ standard
library functions corresponding to the C macros in `[c.math.abs]` are
now `constexpr`.

To implement this feature in libc++, we must make the built-in abs
function `constexpr`. This patch adds the implementation of a
`constexpr` abs function for the current constant evaluator and the new
bytecode interpreter.

It is important to note that in 2's complement systems, the absolute
value of the most negative value is out of range. In gcc, it will result
in an out-of-range error and will not be evaluated as constants. We
follow the same approach here.
2024-10-18 19:03:50 +08:00
Timm Baeder
154929169a
[clang] Implement constexpr __builtin_bit_cast for complex types (#109981)
Fixes https://github.com/llvm/llvm-project/issues/94620
2024-10-16 10:47:12 +02:00
Helena Kotas
3b4512074e
[HLSL] Make HLSLAttributedResourceType canonical and add code paths to convert HLSL types to DirectX target types (#110327)
Translates `RWBuffer` and `StructuredBuffer` resources buffer types to
DirectX target types `dx.TypedBuffer` and `dx.RawBuffer`.

Includes a change of `HLSLAttributesResourceType` from 'sugar' type to
full canonical type. This is required for codegen and other clang
infrastructure to work property on HLSL resource types.

Fixes #95952 (part 2/2)
2024-10-15 13:38:15 -07:00
YunQiang Su
5bf81e53db
Clang: Support minimumnum and maximumnum intrinsics (#96281)
We just introduce llvm.minimumnum and llvm.maximumnum intrinsics support
to llvm. Let's support them in Clang.

See: #93033
2024-10-14 15:49:01 +08:00
Timm Baeder
80c15c48d1
[clang][bytecode] Implement __builtin_assume_aligned (#111968) 2024-10-11 11:46:33 +02:00
Timm Baeder
42ec740d03
[clang][ExprConstant] Remove an outdated TODO comment (#111959)
Seems like passing the quantities directly seems to work fine.
2024-10-11 11:36:55 +02:00
Erich Keane
d412cea8c4
[OpenACC] Implement 'tile' attribute AST (#110999)
The 'tile' clause shares quite a bit of the rules with 'collapse', so a
followup patch will add those tests/behaviors. This patch deals with
adding the AST node.

The 'tile' clause takes a series of integer constant expressions, or *.
The asterisk is now represented by a new OpenACCAsteriskSizeExpr node,
else this clause is very similar to others.
2024-10-03 08:34:43 -07:00
Simon Pilgrim
c7fb0eed05
[clang][x86] Add constexpr support for ADC/SBB + ADX intrinsics (#110668)
ADC and ADX use the same internal intrinsics - for testing I've taken the same approach as the generic builtin overflow tests, putting the intrinsics in a constexpr test wrapper and comparing the carry/result value pair.

I've added the addcarry/subborrow intrinsics to the clang language extension list - I'm not sure if we want to add all ISA intrinsics to the list (although we can if people think it useful?), but I felt we should at least include the baseline x86 intrinsics.
2024-10-02 15:45:02 +01:00
Jay Foad
fe61dbf1d3
[AMDGPU] Specify width and align for all AMDGPU builtin types. NFC. (#109656)
This will be used in ASTContext::getTypeInfo which needs this
information for all builtin types, not just pointers.
2024-10-01 14:12:34 +01:00
Simon Pilgrim
271dc4a3aa
[clang][x86] Add constexpr support for PDEP/PEXT intrinsics (#110535) 2024-10-01 10:35:20 +01:00
Simon Pilgrim
f256222964
[clang][x86] Add constexpr support for BZHI intrinsics (#110508) 2024-09-30 16:30:30 +01:00
Simon Pilgrim
93af9d6cb4
[clang][x86] Add constexpr support for LZCNT/TZCNT intrinsics (#110499) 2024-09-30 15:49:32 +01:00
Simon Pilgrim
1c2ed36741
[clang][x86] Add constexpr support for BMI/TBM BEXTR intrinsics (#109577)
This is an initial patch for constexpr handling of the BEXTR intrinsics - the plan is to support all x86 bit manipulation intrinsics eventually (and then SSE/AVX intrinsics), but I wanted to treat this as an initial test patch.

Hopefully this will unstick #94161 as well.
2024-09-28 17:52:54 +01:00
Richard Smith
d8a2815903
[clang] implement current direction of CWG2765 for string literal comparisons in constant evaluation (#109208)
Track the identity of each string literal object produced by evaluation
with a global version number. Accept comparisons between literals of the
same version, and between literals of different versions that cannot
possibly be placed in overlapping storage. Treat the remaining
comparisons as non-constant.

---------

Co-authored-by: Timm Baeder <tbaeder@redhat.com>
Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
2024-09-26 15:56:33 -07:00
Timm Bäder
95a0b4f729 Revert "[clang][ExprConst] Allow comparisons with string literals (#106733)"
This reverts commit 5d1d2f08c4a92580e7f6b3b6b77b2b6f6184e126.

See the discussion in https://github.com/llvm/llvm-project/pull/106733
and https://github.com/llvm/llvm-project/issues/58754
2024-09-17 05:58:54 +02:00
Timm Baeder
5d1d2f08c4
[clang][ExprConst] Allow comparisons with string literals (#106733)
Don't diagnose them, but literals still have distinct addresses.

Fixes https://github.com/llvm/llvm-project/issues/58754
2024-09-13 10:30:02 +02:00
Chris B
a29afb754f
[HLSL] Allow truncation to scalar (#104844)
HLSL allows implicit conversions to truncate vectors to scalar
pr-values. These conversions are scored as vector truncations and should
warn appropriately.

This change allows forming a truncation cast to a pr-value, but not an
l-value. Truncating a vector to a scalar is performed by loading the
first element of the vector and disregarding the remaining elements.

Fixes #102964
2024-09-11 17:27:09 -05: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
Mital Ashok
2a07509c8d
[Clang] Add __builtin_is_within_lifetime to implement P2641R4's std::is_within_lifetime (#91895)
[P2641R4](https://wg21.link/P2641R4)

This new builtin function is declared `consteval`. Support for
`-fexperimental-new-constant-interpreter` will be added in a later
patch.

---------

Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2024-09-05 14:42:59 +02:00
Mital Ashok
84cf3a573e
[Clang] CWG2749: relational operators involving pointers to void (#93046)
https://cplusplus.github.io/CWG/issues/2749.html

This DR's effects are backported to C++98.

Does not affect C where integral constant expressions cannot involve
pointers.

---------

Co-authored-by: Vlad Serebrennikov <serebrennikov.vladislav@gmail.com>
Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2024-09-05 14:23:08 +02:00
c8ef
eaea4d15ac
[clang] The ms-extension __noop should return zero in a constexpr context. (#106849)
Fixes #106713.
2024-09-02 11:15:44 +02:00
Chris B
89fb8490a9
[HLSL] Implement output parameter (#101083)
HLSL output parameters are denoted with the `inout` and `out` keywords
in the function declaration. When an argument to an output parameter is
constructed a temporary value is constructed for the argument.

For `inout` pamameters the argument is initialized via copy-initialization
from the argument lvalue expression to the parameter type. For `out`
parameters the argument is not initialized before the call.

In both cases on return of the function the temporary value is written
back to the argument lvalue expression through an implicit assignment
binary operator with casting as required.

This change introduces a new HLSLOutArgExpr ast node which represents
the output argument behavior. The OutArgExpr has three defined children:
- An OpaqueValueExpr of the argument lvalue expression.
- An OpaqueValueExpr of the copy-initialized parameter.
- A BinaryOpExpr assigning the first with the value of the second.

Fixes #87526

---------

Co-authored-by: Damyan Pepper <damyanp@microsoft.com>
Co-authored-by: John McCall <rjmccall@gmail.com>
2024-08-31 10:59:08 -05:00
ofAlpaca
e0fa2f1c29
__noop not marked as constexpr #102064 (#105983)
Fixes #102064
2024-08-30 09:52:24 +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
cor3ntin
6e78aef646
[Clang] Implement P2747 constexpr placement new (#104586)
The implementation follows the resolution of CWG2922
2024-08-23 17:24:08 +02:00
c8ef
dd40632b52
[clang] fix divide by zero in ComplexExprEvaluator (#104666)
fix: #55390.

---------

Co-authored-by: Sergei Barannikov <barannikov88@gmail.com>
2024-08-18 17:32:44 +02:00
Mital Ashok
1125934359
[Clang] constexpr builtin floating point classification / comparison functions (#94118)
As per [P0533R9](https://wg21.link/P0533R9), the corresponding C++
`[c.math.fpclass]` standard library functions for the C macros are now
`constexpr`.

The only classification function that wasn't already `constexpr` was
`__builtin_signbit`.
The floating point comparison functions `__builtin_isgreater`,
`__builtin_isgreaterequal`, `__builtin_isless`, `__builtin_islessequal`,
`__builtin_islessgreater` and `__builtin_isunordered` are now
`constexpr`.
The C23 macro `iseqsig` is not currently supported because
`__bulitin_iseqsig` doesn't exist yet (and C++26 is still currently
based on C18).

This also allows them to be constant folded in C, matching the behaviour
of GCC.
2024-08-18 13:50:42 +04: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
Carlos Galvez
2dec83cc8e
[clang] Turn -Wenum-constexpr-conversion into a hard error (#102364)
The warning has been active for a few releases now, first only in user
code, later in system headers, and finally as an error by default.

Therefore, we believe it is now time to transition into a hard error, as
required by the C++ Standard. The main affected C++ projects have by now
fixed the error, or there's a pending patch for review that does it.

Fixes #59036
2024-08-14 20:51:45 +02:00
Vikram Hegde
77534291fc
[clang][ExprConst] allow single element access of vector object to be constant expression (#101126)
This is a slightly updated version of https://github.com/llvm/llvm-project/pull/72607,

originally authored by @yuanfang-chen
2024-08-07 16:11:08 +05:30
Helena Kotas
52956b0f70
[HLSL] Implement intangible AST type (#97362)
HLSL has a set of intangible types which are described in in the
[draft HLSL Specification
(**[Basic.types]**)](https://microsoft.github.io/hlsl-specs/specs/hlsl.pdf):
  There are special implementation-defined types such as handle types,
  which fall into a category of standard intangible types. Intangible
  types are types that have no defined object representation or value
  representation, as such the size is unknown at compile time.
    
  A class type T is an intangible class type if it contains an base
  classes or members of intangible class type, standard intangible type,
  or arrays of such types. Standard intangible types and intangible class
  types are collectively called intangible
  types([9](https://microsoft.github.io/hlsl-specs/specs/hlsl.html#Intangible)).

This PR implements one standard intangible type `__hlsl_resource_t`
and sets up the infrastructure that will make it easier to add more
in the future, such as samplers or raytracing payload handles. The
HLSL intangible types are declared in
`clang/include/clang/Basic/HLSLIntangibleTypes.def` and this file is
included with related macro definition in most places that require edits
when a new type is added.

The new types are added as keywords and not typedefs to make sure they
cannot be redeclared, and they can only be declared in builtin implicit
headers. The `__hlsl_resource_t` type represents a handle to a memory
resource and it is going to be used in builtin HLSL buffer types like this:

        template <typename T>
        class RWBuffer {
          [[hlsl::contained_type(T)]]
          [[hlsl::is_rov(false)]]
          [[hlsl::resource_class(uav)]]  
          __hlsl_resource_t Handle;
        };

Part 1/3 of llvm/llvm-project#90631.

---------

Co-authored-by: Justin Bogner <mail@justinbogner.com>
2024-08-05 10:50:34 -07:00
Eli Friedman
20eff68420
[ExprConstant] Handle shift overflow the same way as other kinds of overflow (#99579)
We have a mechanism to allow folding expressions that aren't ICEs as an
extension; use it more consistently.

This ends up causing bad effects on diagnostics in a few cases, but
that's not specific to shifts; it's a general issue with the way those
uses handle overflow diagnostics.
2024-07-24 12:36:08 -07:00
Akira Hatanaka
666e3326fe
[PAC] Define __builtin_ptrauth_type_discriminator (#100204)
The builtin computes the discriminator for a type, which can be used to
sign/authenticate function pointers and member function pointers.

If the type passed to the builtin is a C++ member function pointer type,
the result is the discriminator used to signed member function pointers
of that type. If the type is a function, function pointer, or function
reference type, the result is the discriminator used to sign functions
of that type. It is ill-formed to use this builtin with any other type.

A call to this function is an integer constant expression.

Co-Authored-By: John McCall rjmccall@apple.com
2024-07-24 02:04:37 -07:00
Timm Baeder
dbe308c000
[clang][ExprConst] Allow non-literal types in C++23 (#100062)
Instead of diagnosing non-literal types in C++23, allow them and later
diagnose them differently, e.g. because they have a non-constexpr
constructor, destructor, etc.

For this test:
```c++
struct NonLiteral {
  NonLiteral() {}
};
constexpr int foo() {
  NonLiteral L;
  return 1;
}

// static_assert(foo() == 1);
```
The current diagnostics with c++20/c++23 are:
```console
~/code/llvm-project/build » clang -c array.cpp -std=c++20
array.cpp:91:14: error: variable of non-literal type 'NonLiteral' cannot be defined in a constexpr function before C++23
   91 |   NonLiteral L;
      |              ^
array.cpp:87:8: note: 'NonLiteral' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors
   87 | struct NonLiteral {
      |        ^
1 error generated.
------------------------------------------------------------
~/code/llvm-project/build » clang -c array.cpp -std=c++23
(no output)
```

With the `static_assert` enabled, compiling with `-std=c++23` prints:
```console
array.cpp:95:15: error: static assertion expression is not an integral constant expression
   95 | static_assert(foo() == 1);
      |               ^~~~~~~~~~
array.cpp:91:14: note: non-literal type 'NonLiteral' cannot be used in a constant expression
   91 |   NonLiteral L;
      |              ^
array.cpp:95:15: note: in call to 'foo()'
   95 | static_assert(foo() == 1);
      |               ^~~~~
1 error generated.
```

As mentioned in #60311, this is confusing. The output with c++20
suggests that using c++23 will make the problem go away, but it's
diagnosed the same when running the function.

With this commit, the output instead diagnoses _why_ the non-literal
type can't be used:
```console
array.cpp:95:15: error: static assertion expression is not an integral constant expression
   95 | static_assert(foo() == 1);
      |               ^~~~~~~~~~
array.cpp:91:14: note: non-constexpr constructor 'NonLiteral' cannot be used in a constant expression
   91 |   NonLiteral L;
      |              ^
array.cpp:95:15: note: in call to 'foo()'
   95 | static_assert(foo() == 1);
      |               ^~~~~
array.cpp:88:3: note: declared here
   88 |   NonLiteral() {}
      |   ^
1 error generated.

``` 




Fixes #60311
2024-07-24 08:09:06 +02:00
James Y Knight
511e93b96e
Handle constant "pointers" for __atomic_always_lock_free/__atomic_is_lock_free. (#99340)
The second argument passed to these builtins is used to validate whether
the object's alignment is sufficient for atomic operations of the given
size.

Currently, the builtins can be folded at compile time only when the
argument is 0/nullptr, or if the _type_ of the pointer guarantees
appropriate alignment.

This change allows the compiler to also evaluate non-null constant
pointers, which enables callers to check a specified alignment, instead
of only the type or an exact object. E.g.:
 `__atomic_is_lock_free(sizeof(T), (void*)4)`
can be potentially evaluated to true at compile time, instead of
generating a libcall. This is also supported by GCC, and used by
libstdc++, and is also useful for libc++'s atomic_ref.

Also helps with (but doesn't fix) issue #75081.

This also fixes a crash bug, when the second argument was a non-pointer
implicitly convertible to a pointer (such as an array, or a function).
2024-07-22 14:20:25 -04:00
Mital Ashok
554febd3aa
[Clang] Fix some assertions not looking through type sugar (#92299)
Fixes #92284

Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2024-07-17 16:42:02 +02:00
Budimir Aranđelović
e4163c0927
[clang] Emit bad shift warnings (#70307)
Diagnose bad shifts and emit warnings
2024-07-11 14:21:21 +02:00
Zhikai Zeng
874ca08645
[Clang][ExprConstant] fix constant expression did not evaluate to integer (#97146)
fixes https://github.com/llvm/llvm-project/issues/96670

The cause is that we might return a lvalue here at


3e53c97d33/clang/lib/AST/ExprConstant.cpp (L15861-L15865)

This PR will make sure we return a rvalue in `FastEvaluateAsRValue`.
2024-07-06 16:28:23 +08:00
Akira Hatanaka
2604830aac
Add support for __builtin_verbose_trap (#79230)
The builtin causes the program to stop its execution abnormally and
shows a human-readable description of the reason for the termination
when a debugger is attached or in a symbolicated crash log.

The motivation for the builtin is explained in the following RFC:

https://discourse.llvm.org/t/rfc-adding-builtin-verbose-trap-string-literal/75845

clang's CodeGen lowers the builtin to `llvm.trap` and emits debugging
information that represents an artificial inline frame whose name
encodes the category and reason strings passed to the builtin.
2024-06-25 08:33:05 -07:00
Ahmed Bougacha
7c814c13d0
[clang] Define ptrauth_sign_constant builtin. (#93904)
This is a constant-expression equivalent to
ptrauth_sign_unauthenticated.  Its constant nature lets us guarantee
a non-attackable sequence is generated, unlike
ptrauth_sign_unauthenticated which we generally discourage using.

It being a constant also allows its usage in global initializers, though
requiring constant pointers and discriminators.

The value must be a constant expression of pointer type which evaluates
to a non-null pointer.

The key must be a constant expression of type ptrauth_key.
The extra data must be a constant expression of pointer or integer type;
if an integer, it will be coerced to ptrauth_extra_data_t.
The result will have the same type as the original value.

This can be used in constant expressions.

Co-authored-by: John McCall <rjmccall@apple.com>
2024-06-20 12:09:54 -07:00
Ahmed Bougacha
50b9193781
[clang] Define ptrauth_string_discriminator builtin. (#93903)
This exposes the ABI-stable hash function that allows computing a 16-bit
discriminator from a constant string.

This allows manually matching the implicit string discriminators
computed in the ABI (e.g., from mangled names for vtable pointer/entry
signing), as well as enabling the use of interesting discriminators when
manually annotating specific pointers with the __ptrauth qualifier.

The argument must be a string literal of char character type.  The
result has type ptrauth_extra_data_t.
The result value is never zero and always within range for both the
__ptrauth qualifier and ptrauth_blend_discriminator.
This can be used in constant expressions.

Co-authored-by: John McCall <rjmccall@apple.com>
2024-06-20 11:55:41 -07:00
Mital Ashok
482c41e992
[Clang] [Sema] Diagnose unknown std::initializer_list layout in SemaInit (#95580)
This checks if the layout of `std::initializer_list` is something Clang
can handle much earlier and deduplicates the checks in
CodeGen/CGExprAgg.cpp and AST/ExprConstant.cpp

Also now diagnose `union initializer_list` (Fixes #95495), bit-field for
the size (Fixes a crash that would happen during codegen if it were
unnamed), base classes (that wouldn't be initialized) and polymorphic
classes (whose vtable pointer wouldn't be initialized).
2024-06-20 19:44:06 +02:00
Mariya Podchishchaeva
41c6e43792
Reland [clang][Sema, Lex, Parse] Preprocessor embed in C and C++ (#95802)
This commit implements the entirety of the now-accepted [N3017
-Preprocessor
Embed](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3017.htm) and
its sister C++ paper [p1967](https://wg21.link/p1967). It implements
everything in the specification, and includes an implementation that
drastically improves the time it takes to embed data in specific
scenarios (the initialization of character type arrays). The mechanisms
used to do this are used under the "as-if" rule, and in general when the
system cannot detect it is initializing an array object in a variable
declaration, will generate EmbedExpr AST node which will be expanded by
AST consumers (CodeGen or constant expression evaluators) or expand
embed directive as a comma expression.

This reverts commit
682d461d5a.

---------

Co-authored-by: The Phantom Derpstorm <phdofthehouse@gmail.com>
Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
Co-authored-by: cor3ntin <corentinjabot@gmail.com>
Co-authored-by: H. Vetinari <h.vetinari@gmx.com>
2024-06-20 14:38:46 +02:00