https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/219767
>From 19cfe3c37d8c02e54d39958763e7f0fc7d2a090f Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Sun, 30 Aug 2026 10:57:14 +0530 Subject: [PATCH 1/5] [clang] Reject references to vector types in __builtin_vectorelements Fixes #216997 CheckUnaryExprOrTypeTraitOperand looks through reference types for all trait kinds before dispatching to the __builtin_vectorelements check, so a reference to a vector type passed Sema while the expression kept the reference as its argument type. Constant evaluation and codegen require that type to be a vector type and assert otherwise. Check the __builtin_vectorelements operand before the reference is looked through, so it is diagnosed like any other non-vector type. --- clang/docs/ReleaseNotes.md | 2 ++ clang/lib/Sema/SemaExpr.cpp | 10 ++++++---- clang/test/SemaCXX/GH216997.cpp | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 clang/test/SemaCXX/GH216997.cpp diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index a49971adef86f..db73eb43a6cec 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -522,6 +522,8 @@ features cannot lower the translation-unit ABI level; format warnings to errors. (#GH211943) - Fixed a wrong code generation in `__builtin_clear_padding` wherein the wrong bits of the `_BitInt` type were cleared in big-endian mode. +- Fixed a crash when `__builtin_vectorelements` is applied to a reference to a + vector type; this is now diagnosed as an invalid argument type. (#GH216997) #### Bug Fixes to Attribute Support diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index c93efeb928c56..e5e8274620799 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -4742,6 +4742,12 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, if (ExprType->isDependentType()) return false; + // A reference to a vector type is not a vector type; check this before the + // reference is looked through below. + if (ExprKind == UETT_VectorElements) + return CheckVectorElementsTraitOperandType(*this, ExprType, OpLoc, + ExprRange); + // C++ [expr.sizeof]p2: // When applied to a reference or a reference type, the result // is the size of the referenced type. @@ -4767,10 +4773,6 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, if (ExprKind == UETT_VecStep) return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); - if (ExprKind == UETT_VectorElements) - return CheckVectorElementsTraitOperandType(*this, ExprType, OpLoc, - ExprRange); - if (ExprKind == UETT_PtrAuthTypeDiscriminator) return checkPtrAuthTypeDiscriminatorOperandType(*this, ExprType, OpLoc, ExprRange); diff --git a/clang/test/SemaCXX/GH216997.cpp b/clang/test/SemaCXX/GH216997.cpp new file mode 100644 index 0000000000000..407c618421484 --- /dev/null +++ b/clang/test/SemaCXX/GH216997.cpp @@ -0,0 +1,32 @@ +// RUN: %clang_cc1 -triple x86_64 -std=c++20 -fsyntax-only -verify %s +// RUN: %clang_cc1 -triple x86_64 -std=c++20 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s + +// Reproducer from GH216997. +using vec __attribute__((vector_size(16))) = int &bar; // expected-error {{type-id cannot have a name}} +int baz = __builtin_vectorelements(vec); // expected-error {{argument to __builtin_vectorelements must be of vector type}} + +using vec_ref __attribute__((vector_size(16))) = int &; +static_assert(sizeof(vec_ref) == 16, ""); +int a = __builtin_vectorelements(vec_ref); // expected-error {{argument to __builtin_vectorelements must be of vector type}} + +typedef int veci4 __attribute__((vector_size(16))); +int b = __builtin_vectorelements(veci4 &); // expected-error {{argument to __builtin_vectorelements must be of vector type}} +int c = __builtin_vectorelements(veci4 &&); // expected-error {{argument to __builtin_vectorelements must be of vector type}} +int d = __builtin_vectorelements(const veci4 &); // expected-error {{argument to __builtin_vectorelements must be of vector type}} + +veci4 v; +int e = __builtin_vectorelements(decltype((v))); // expected-error {{argument to __builtin_vectorelements must be of vector type}} + +template <typename T> +int f() { + return __builtin_vectorelements(T); // expected-error {{argument to __builtin_vectorelements must be of vector type}} +} +int g = f<veci4>(); +int h = f<veci4 &>(); // expected-note {{in instantiation of function template specialization}} + +void ok(veci4 &r, veci4 &&rr) { + (void)__builtin_vectorelements(r); + (void)__builtin_vectorelements(rr); + (void)__builtin_vectorelements(const veci4); + (void)__builtin_vectorelements(decltype(v)); +} >From 5d7223cec34102415765b0170a3f68b1b56b140a Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Sun, 30 Aug 2026 11:02:56 +0530 Subject: [PATCH 2/5] Fix assertion failure for __builtin_vectorelements with vector type references --- clang/docs/ReleaseNotes.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index db73eb43a6cec..739ae543f0e3e 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -524,6 +524,9 @@ features cannot lower the translation-unit ABI level; wrong bits of the `_BitInt` type were cleared in big-endian mode. - Fixed a crash when `__builtin_vectorelements` is applied to a reference to a vector type; this is now diagnosed as an invalid argument type. (#GH216997) +- Fixed an assertion failure when `__builtin_vectorelements` is applied to a + reference to a vector type; this is now diagnosed as an invalid argument type. + (#GH216997) #### Bug Fixes to Attribute Support >From b237ea44350d36d53735e29f3da4a022ed67af9c Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Sun, 30 Aug 2026 14:00:55 +0530 Subject: [PATCH 3/5] [clang] Shorten release note for __builtin_vectorelements fix --- clang/docs/ReleaseNotes.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 739ae543f0e3e..2f5142b390487 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -525,8 +525,7 @@ features cannot lower the translation-unit ABI level; - Fixed a crash when `__builtin_vectorelements` is applied to a reference to a vector type; this is now diagnosed as an invalid argument type. (#GH216997) - Fixed an assertion failure when `__builtin_vectorelements` is applied to a - reference to a vector type; this is now diagnosed as an invalid argument type. - (#GH216997) + reference to a vector type. (#GH216997) #### Bug Fixes to Attribute Support >From 2ab038f16bae9a5382810a90c29331fa4507bb2f Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Wed, 9 Sep 2026 11:17:03 +0530 Subject: [PATCH 4/5] Remove duplicated release note entry from rebase --- clang/docs/ReleaseNotes.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 2f5142b390487..2b73d3ae4304a 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -522,8 +522,6 @@ features cannot lower the translation-unit ABI level; format warnings to errors. (#GH211943) - Fixed a wrong code generation in `__builtin_clear_padding` wherein the wrong bits of the `_BitInt` type were cleared in big-endian mode. -- Fixed a crash when `__builtin_vectorelements` is applied to a reference to a - vector type; this is now diagnosed as an invalid argument type. (#GH216997) - Fixed an assertion failure when `__builtin_vectorelements` is applied to a reference to a vector type. (#GH216997) >From 55b4d153fa37183e2d7ff2d7c2f942a2cf9781aa Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Wed, 9 Sep 2026 19:43:41 +0530 Subject: [PATCH 5/5] Move GH216997 tests into builtin_vectorelements.cpp --- clang/test/SemaCXX/GH216997.cpp | 32 ------------------- clang/test/SemaCXX/builtin_vectorelements.cpp | 27 ++++++++++++++++ 2 files changed, 27 insertions(+), 32 deletions(-) delete mode 100644 clang/test/SemaCXX/GH216997.cpp diff --git a/clang/test/SemaCXX/GH216997.cpp b/clang/test/SemaCXX/GH216997.cpp deleted file mode 100644 index 407c618421484..0000000000000 --- a/clang/test/SemaCXX/GH216997.cpp +++ /dev/null @@ -1,32 +0,0 @@ -// RUN: %clang_cc1 -triple x86_64 -std=c++20 -fsyntax-only -verify %s -// RUN: %clang_cc1 -triple x86_64 -std=c++20 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s - -// Reproducer from GH216997. -using vec __attribute__((vector_size(16))) = int &bar; // expected-error {{type-id cannot have a name}} -int baz = __builtin_vectorelements(vec); // expected-error {{argument to __builtin_vectorelements must be of vector type}} - -using vec_ref __attribute__((vector_size(16))) = int &; -static_assert(sizeof(vec_ref) == 16, ""); -int a = __builtin_vectorelements(vec_ref); // expected-error {{argument to __builtin_vectorelements must be of vector type}} - -typedef int veci4 __attribute__((vector_size(16))); -int b = __builtin_vectorelements(veci4 &); // expected-error {{argument to __builtin_vectorelements must be of vector type}} -int c = __builtin_vectorelements(veci4 &&); // expected-error {{argument to __builtin_vectorelements must be of vector type}} -int d = __builtin_vectorelements(const veci4 &); // expected-error {{argument to __builtin_vectorelements must be of vector type}} - -veci4 v; -int e = __builtin_vectorelements(decltype((v))); // expected-error {{argument to __builtin_vectorelements must be of vector type}} - -template <typename T> -int f() { - return __builtin_vectorelements(T); // expected-error {{argument to __builtin_vectorelements must be of vector type}} -} -int g = f<veci4>(); -int h = f<veci4 &>(); // expected-note {{in instantiation of function template specialization}} - -void ok(veci4 &r, veci4 &&rr) { - (void)__builtin_vectorelements(r); - (void)__builtin_vectorelements(rr); - (void)__builtin_vectorelements(const veci4); - (void)__builtin_vectorelements(decltype(v)); -} diff --git a/clang/test/SemaCXX/builtin_vectorelements.cpp b/clang/test/SemaCXX/builtin_vectorelements.cpp index b23675ea0ac6a..33a97c185bf6b 100644 --- a/clang/test/SemaCXX/builtin_vectorelements.cpp +++ b/clang/test/SemaCXX/builtin_vectorelements.cpp @@ -40,6 +40,33 @@ void test_builtin_vectorelements() { } +namespace GH216997 { +// Reproducer from GH216997. +using vec __attribute__((vector_size(16))) = int &bar; // expected-error {{type-id cannot have a name}} +int baz = __builtin_vectorelements(vec); // expected-error {{argument to __builtin_vectorelements must be of vector type}} + +typedef int veci4 __attribute__((vector_size(16))); +int a = __builtin_vectorelements(veci4 &); // expected-error {{argument to __builtin_vectorelements must be of vector type}} +int b = __builtin_vectorelements(veci4 &&); // expected-error {{argument to __builtin_vectorelements must be of vector type}} +int c = __builtin_vectorelements(const veci4 &); // expected-error {{argument to __builtin_vectorelements must be of vector type}} + +veci4 v; +int d = __builtin_vectorelements(decltype((v))); // expected-error {{argument to __builtin_vectorelements must be of vector type}} + +template <typename T> +int f() { + return __builtin_vectorelements(T); // expected-error {{argument to __builtin_vectorelements must be of vector type}} +} +int e = f<veci4>(); +int g = f<veci4 &>(); // expected-note {{in instantiation of function template specialization}} + +void ok(veci4 &r, veci4 &&rr) { + (void)__builtin_vectorelements(r); + (void)__builtin_vectorelements(rr); + (void)__builtin_vectorelements(const veci4); +} +} // namespace GH216997 + #if defined(__ARM_FEATURE_SVE) #include <arm_sve.h> _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
