[clang] [clang][ExprConst] allow single element access of vector object to be constant expression (PR #72607)
sethp wrote: Ah, sorry: my specific question is whether the APValue::LValuePathEntry ought to grow an understanding of vector types rather than re-using the array machinery, as here. It sounds like arrays express a couple of properties vectors don't, so there's potential for the evaluator to want to distinguish an LValuePath that ends in an array element from one that references a vector component. That said, I don't have a strong sense of whether it's worth doing. "Sema won't build an AST that the evaluator would need to reject" is a fair way to avoid telling the two apart, but seems much harder to me to demonstrate: "none of the ways we tried to get the evaluator to mistreat a vector as an array" is only the same property if we've managed to test exhaustively. For what my non-expert opinion is worth, something like this seems more robust to me: ```diff void addVectorUnchecked(QualType EltTy, uint64_t Size, uint64_t Idx) { - Entries.push_back(PathEntry::ArrayIndex(Idx)); + Entries.push_back(PathEntry::VectorElement(Idx)); - // This is technically a most-derived object, though in practice this - // is unlikely to matter. MostDerivedType = EltTy; - MostDerivedIsArrayElement = true; - MostDerivedArraySize = Size; ``` (assuming that's coherent, it's been a few months) https://github.com/llvm/llvm-project/pull/72607 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][Sema] Print more static_assert exprs (PR #74852)
sethp wrote: Ping @cor3ntin @cjdb https://github.com/llvm/llvm-project/pull/74852 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] 2903df0 - Squashed commit of the following:
Author: Pol Marcet Sardà Date: 2024-04-29T09:09:59-07:00 New Revision: 2903df02fb3c057849aaa796a91289b01950a5f0 URL: https://github.com/llvm/llvm-project/commit/2903df02fb3c057849aaa796a91289b01950a5f0 DIFF: https://github.com/llvm/llvm-project/commit/2903df02fb3c057849aaa796a91289b01950a5f0.diff LOG: Squashed commit of the following: commit 8d41d93e3fceb3f3af77266f5a8388fc585150a5 Author: Pol Marcet Sardà Date: Sat Apr 20 12:19:49 2024 +0200 Address some misc comments; added a diagnostic and expanded macros in testing. commit 9493c0f290b558947d8b3ae8e1adf909b0fb9dcd Author: Pol Marcet Sardà Date: Sun Mar 31 18:18:45 2024 +0200 Following the review of sethp, I have made the following changes: -- Added diagnostic for the undefined shuffle of -1 -- Validated support for _BitInt -- A bunch of other minnor tweaks here and there commit 8273abc8d56ef8225cf4dba84f66a1e54a2ef036 Author: Pol Marcet Sardà Date: Thu Jan 4 12:31:08 2024 +0100 Fix typo in file name commit ff68f23921966c7d9605f91a47d6b481bf1d7a7b Author: Pol Marcet Sardà Date: Thu Jan 4 11:26:08 2024 +0100 Address suggestions from RKSimon commit c14783de45687c754253c0cbf8a7834c7f986d80 Author: Pol Marcet Sardà Date: Sat Dec 30 13:59:00 2023 +0100 [clang] Constexpr for __builtin_shufflevector and __builtin_convertvector Summary: This patch adds constexpr support for __builtin_shufflevector and __builtin_convertvector. A small oddity encountered was that the arg to the intrinsics may be an lvalue without any sort of implicit cast of any kind. I solved this through the EvaluateVectorOrLValue function, which treats the lvalue as if it was in an rvalue cast, which gets me the desired vector. Co-Authored-By: Seth Pellegrino Added: clang/test/Sema/constant_builtins_vector.cpp Modified: clang/docs/LanguageExtensions.rst clang/docs/ReleaseNotes.rst clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/AST/ExprConstant.cpp clang/test/Sema/convertvector.c Removed: diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index 87cb743856b07e..c2e90f4e7d587a 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -2931,7 +2931,7 @@ Query for this feature with ``__has_builtin(__builtin_dump_struct)`` ``__builtin_shufflevector`` is used to express generic vector permutation/shuffle/swizzle operations. This builtin is also very important for the implementation of various target-specific header files like -. +. This builtin can be used within constant expressions. **Syntax**: @@ -2958,7 +2958,7 @@ for the implementation of various target-specific header files like // Concatenate every other element of 8-element vectors V1 and V2. __builtin_shufflevector(V1, V2, 0, 2, 4, 6, 8, 10, 12, 14) - // Shuffle v1 with some elements being undefined + // Shuffle v1 with some elements being undefined. Not allowed in constexpr. __builtin_shufflevector(v1, v1, 3, -1, 1, -1) **Description**: @@ -2971,6 +2971,7 @@ starting with the first vector, continuing into the second vector. Thus, if ``vec1`` is a 4-element vector, index 5 would refer to the second element of ``vec2``. An index of -1 can be used to indicate that the corresponding element in the returned vector is a don't care and can be optimized by the backend. +Values of -1 are not supported in constant expressions. The result of ``__builtin_shufflevector`` is a vector with the same element type as ``vec1``/``vec2`` but that has an element count equal to the number of @@ -2985,7 +2986,8 @@ Query for this feature with ``__has_builtin(__builtin_shufflevector)``. ``__builtin_convertvector`` is used to express generic vector type-conversion operations. The input vector and the output vector -type must have the same number of elements. +type must have the same number of elements. This builtin can be used within +constant expressions. **Syntax**: diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 4cb2462ae64956..2d0e663d93e46f 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -233,6 +233,9 @@ Non-comprehensive list of changes in this release * ``-fdenormal-fp-math=preserve-sign`` is no longer implied by ``-ffast-math`` on x86 systems. +- Builtins ``__builtin_shufflevector()`` and ``__builtin_convertvector()`` may + now be used within constant expressions. + New Compiler Flags -- - ``-fsanitize=implicit-bitfield-conversion`` checks implicit truncation and diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 8486fa3a02e833..502c771a2d679d 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@
[clang] 347a02b - [clang][NFC] Repair tests for __builtin_convertvector on big-endian
Author: Seth Pellegrino Date: 2024-04-29T13:45:00-07:00 New Revision: 347a02b408567ba15fdc68646129c1f5de97ab7e URL: https://github.com/llvm/llvm-project/commit/347a02b408567ba15fdc68646129c1f5de97ab7e DIFF: https://github.com/llvm/llvm-project/commit/347a02b408567ba15fdc68646129c1f5de97ab7e.diff LOG: [clang][NFC] Repair tests for __builtin_convertvector on big-endian Previously, the macro expansion to be explicit for the test cases had unintentionally forced the tests to always run in little-endian mode. This change restores the tests' endianness switch, and ensures that the tests always run on both a little- and big-endian platform. Added: Modified: clang/test/Sema/constant_builtins_vector.cpp Removed: diff --git a/clang/test/Sema/constant_builtins_vector.cpp b/clang/test/Sema/constant_builtins_vector.cpp index 68620d436fc43e..ddb78696ce624d 100644 --- a/clang/test/Sema/constant_builtins_vector.cpp +++ b/clang/test/Sema/constant_builtins_vector.cpp @@ -1,4 +1,6 @@ // RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only -Wno-bit-int-extension %s +// RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only -Wno-bit-int-extension -triple ppc64-unknown-linux %s +// RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only -Wno-bit-int-extension -triple ppc64le-unknown-linux %s #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ #define LITTLE_END 1 @@ -102,13 +104,13 @@ static_assert(__builtin_bit_cast( __builtin_shufflevector(from_vector4double_to_vector4char_var, from_vector4double_to_vector4char_var, 0, 1, 2, 3)) == - (1 ? 0x03020100 : 0x00010203)); + (LITTLE_END ? 0x03020100 : 0x00010203)); static_assert(__builtin_bit_cast(unsigned long long, __builtin_shufflevector( from_vector4double_to_vector4short_var, from_vector4double_to_vector4short_var, 0, 1, 2, 3)) == - (1 ? 0x000300020001 : 0x000100020003)); + (LITTLE_END ? 0x000300020001 : 0x000100020003)); constexpr vector4double from_vector4float_to_vector4double_var = __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4double); constexpr vector4float from_vector4float_to_vector4float_var = @@ -131,13 +133,13 @@ static_assert(__builtin_bit_cast(unsigned, __builtin_shufflevector( from_vector4float_to_vector4char_var, from_vector4float_to_vector4char_var, 0, 1, - 2, 3)) == (1 ? 0x03020100 : 0x00010203)); + 2, 3)) == (LITTLE_END ? 0x03020100 : 0x00010203)); static_assert(__builtin_bit_cast( unsigned long long, __builtin_shufflevector(from_vector4float_to_vector4short_var, from_vector4float_to_vector4short_var, 0, 1, 2, 3)) == - (1 ? 0x000300020001 : 0x000100020003)); + (LITTLE_END ? 0x000300020001 : 0x000100020003)); constexpr vector4double from_vector4long_to_vector4double_var = __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4double); constexpr vector4float from_vector4long_to_vector4float_var = @@ -160,13 +162,13 @@ static_assert(__builtin_bit_cast(unsigned, __builtin_shufflevector( from_vector4long_to_vector4char_var, from_vector4long_to_vector4char_var, 0, 1, - 2, 3)) == (1 ? 0x03020100 : 0x00010203)); + 2, 3)) == (LITTLE_END ? 0x03020100 : 0x00010203)); static_assert(__builtin_bit_cast( unsigned long long, __builtin_shufflevector(from_vector4long_to_vector4short_var, from_vector4long_to_vector4short_var, 0, 1, 2, 3)) == - (1 ? 0x000300020001 : 0x000100020003)); + (LITTLE_END ? 0x000300020001 : 0x000100020003)); constexpr vector4double from_vector4int_to_vector4double_var = __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4double); constexpr vector4float from_vector4int_to_vector4float_var = @@ -189,13 +191,13 @@ static_assert(__builtin_bit_cast(unsigned, __builtin_shufflevector( from_vector4int_to_vector4char_var, from_vector4int_to_vector4char_var, 0, 1, - 2, 3)) == (1 ?