llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Akash Manna (akash-manna-sky) <details> <summary>Changes</summary> Fixes #<!-- -->216997 `__builtin_vectorelements` shares its operand check with `sizeof` and friends, and that shared code looks through reference types before it dispatches to the per-builtin check. So `__builtin_vectorelements(veci4 &)` was accepted by Sema, but the expression kept the reference as its argument type, and everything downstream — constant evaluation, the bytecode interpreter, CodeGen — expects that type to be a vector. Constant-evaluating the initializer in the reproducer then trips `assert(Ty->isSizelessVectorType())`. The alias syntax in the report isn't the cause; `vec` there is a well-formed reference-to-vector type. The check now runs before the reference is looked through, so a reference to a vector type gets the existing "must be of vector type" error, the same one `__builtin_vectorelements(int &)` already produced. No other trait kind is affected. LLM tools were used for this contribution. I've reviewed, built, and tested the change myself before pushing to GitHub. --- Full diff: https://github.com/llvm/llvm-project/pull/219767.diff 3 Files Affected: - (modified) clang/docs/ReleaseNotes.md (+3) - (modified) clang/lib/Sema/SemaExpr.cpp (+6-4) - (added) clang/test/SemaCXX/GH216997.cpp (+32) ``````````diff diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index bdbabf2cd98d0..ba737310919c4 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -457,6 +457,9 @@ features cannot lower the translation-unit ABI level; such as when the call is used as an `auto` non-type template argument. - Fixed a crash in ``__builtin_dump_struct`` when ``-Werror`` promotes format warnings to errors. (#GH211943) +- 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 diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 34f6ccdbc2fe6..348d3e9ef33c0 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)); +} `````````` </details> https://github.com/llvm/llvm-project/pull/219767 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
