1417 Commits

Author SHA1 Message Date
Timm Bäder
59bdea24b0 Revert "[clang] Avoid re-evaluating field bitwidth (#117732)"
This reverts commit 81fc3add1e627c23b7270fe2739cdacc09063e54.

This breaks some LLDB tests, e.g.
SymbolFile/DWARF/x86/no_unique_address-with-bitfields.cpp:

lldb: ../llvm-project/clang/lib/AST/Decl.cpp:4604: unsigned int clang::FieldDecl::getBitWidthValue() const: Assertion `isa<ConstantExpr>(getBitWidth())' failed.
2025-01-08 15:09:52 +01:00
Timm Baeder
81fc3add1e
[clang] Avoid re-evaluating field bitwidth (#117732)
Save the bitwidth value as a `ConstantExpr` with the value set. Remove
the `ASTContext` parameter from `getBitWidthValue()`, so the latter
simply returns the value from the `ConstantExpr` instead of
constant-evaluating the bitwidth expression every time it is called.
2025-01-08 14:45:19 +01:00
Chandler Carruth
dc3cd2e95e
Factor common code for quoting a builtin name (#120835)
This shows up in several places in order to match the quoting of other
uses of the same diagnostic. Handling it centrally simplifies the code
and reduces changes if the storage for builtin names changes.

This refactoring is extracted out of #120534 as requested in code
review.
2025-01-03 19:23:42 -08:00
c8ef
dd577c05ad
[clang] constexpr built-in reduce min/max function. (#120866)
Part of #51787.

This patch adds constexpr support for the built-in reduce min/max
function.
2024-12-23 09:06:38 +08:00
Timm Baeder
502c08e672
[clang][ExprConst] Move vector diagnostics to checkBitCastConstexprEl… (#119366)
…igibilityType

This is the function we use to diagnose invalid types, so use it for
those checks as well.

NFC.
2024-12-10 15:05:29 +01:00
Timm Baeder
b0f8f32fdc
[ExprConst] Handle floating- and char literals in FastEvaluateAsRValue (#118294)
This is part of a three-patch series that results in some nice (but not
substantial) compile-time improvements:
http://llvm-compile-time-tracker.com/compare.php?from=fe1c4f0106fe4fd6d61c38ba46e71fda8f4d1573&to=0824d621b2c035a3befb564153b31309a9a79d97&stat=instructions%3Au

The results for just this patch are here:
http://llvm-compile-time-tracker.com/compare.php?from=fe1c4f0106fe4fd6d61c38ba46e71fda8f4d1573&to=6f7f51b476a37dc7c80427fede077e6798a83be8&stat=instructions:u
2024-12-10 11:41:45 +01:00
Oleksandr T.
1094641bc0
[Clang] allow usage of placement new operator in [[msvc::constexpr]] context outside of the std namespace (#119153)
Fixes #74924
2024-12-09 18:47:38 +02:00
c8ef
f145ff3f70
[clang] constexpr built-in elementwise add_sat/sub_sat functions. (#119082)
Part of #51787.

This patch adds constexpr support for the built-in elementwise add_sat
and sub_sat functions.
2024-12-09 09:28:12 +08:00
c8ef
814ed93e48
[clang] constexpr built-in elementwise bitreverse function. (#118177)
Part of #51787.

This patch adds constexpr support for the built-in elementwise
bitreverse function.
2024-12-03 09:33:53 +08:00
c8ef
827ebf84e9
[clang] constexpr built-in elementwise popcount function. (#117473)
Part of #51787.

This patch adds constexpr support for the built-in elementwise popcount
function.
2024-11-26 20:31:29 +08:00
Congcong Cai
cbdd14ee9d
[clang][NFC]add static for internal linkage function (#117482)
Detected by misc-use-internal-linkage
2024-11-25 06:48:33 +08:00
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