Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 63f08b5

Browse files
SC llvm teamSC llvm team
authored andcommitted
Merged main:d40413013425 into amd-gfx:85fdb0385e9e
Local branch amd-gfx 85fdb03 Manually merged main:416884544e02 into amd-gfx:39064028b3b3 Remote branch main d404130 [libc] Fix accidental LIBC_NAMESPACE_syscall definition (llvm#69548)
2 parents 85fdb03 + d404130 commit 63f08b5

File tree

49 files changed

+801
-88
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+801
-88
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,14 @@ Let ``T`` be one of the following types:
619619

620620
For scalar types, consider the operation applied to a vector with a single element.
621621

622+
*Vector Size*
623+
To determine the number of elements in a vector, use ``__builtin_vectorelements()``.
624+
For fixed-sized vectors, e.g., defined via ``__attribute__((vector_size(N)))`` or ARM
625+
NEON's vector types (e.g., ``uint16x8_t``), this returns the constant number of
626+
elements at compile-time. For scalable vectors, e.g., SVE or RISC-V V, the number of
627+
elements is not known at compile-time and is determined at runtime. This builtin can
628+
be used, e.g., to increment the loop-counter in vector-type agnostic loops.
629+
622630
*Elementwise Builtins*
623631

624632
Each builtin returns a vector equivalent to applying the specified operation

clang/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ C23 Feature Support
182182
Non-comprehensive list of changes in this release
183183
-------------------------------------------------
184184

185+
* Clang now has a ``__builtin_vectorelements()`` function that determines the number of elements in a vector.
186+
For fixed-sized vectors, e.g., defined via ``__attribute__((vector_size(N)))`` or ARM NEON's vector types
187+
(e.g., ``uint16x8_t``), this returns the constant number of elements at compile-time.
188+
For scalable vectors, e.g., SVE or RISC-V V, the number of elements is not known at compile-time and is
189+
determined at runtime.
190+
185191
New Compiler Flags
186192
------------------
187193

clang/include/clang/AST/Type.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,9 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
20602060
bool isSizelessType() const;
20612061
bool isSizelessBuiltinType() const;
20622062

2063+
/// Returns true for all scalable vector types.
2064+
bool isSizelessVectorType() const;
2065+
20632066
/// Returns true for SVE scalable vector types.
20642067
bool isSVESizelessBuiltinType() const;
20652068

clang/include/clang/Basic/Builtins.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ BUILTIN(__builtin_debugtrap, "v", "n")
674674
BUILTIN(__builtin_unreachable, "v", "nr")
675675
BUILTIN(__builtin_shufflevector, "v." , "nct")
676676
BUILTIN(__builtin_convertvector, "v." , "nct")
677+
BUILTIN(__builtin_vectorelements, "v." , "nct")
677678
BUILTIN(__builtin_alloca, "v*z" , "Fn")
678679
BUILTIN(__builtin_alloca_uninitialized, "v*z", "Fn")
679680
BUILTIN(__builtin_alloca_with_align, "v*zIz", "Fn")

clang/include/clang/Basic/DiagnosticASTKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ def note_constexpr_unsupported_layout : Note<
394394
"type %0 has unexpected layout">;
395395
def note_constexpr_unsupported_flexible_array : Note<
396396
"flexible array initialization is not yet supported">;
397+
def note_constexpr_non_const_vectorelements : Note<
398+
"cannot determine number of elements for sizeless vectors in a constant expression">;
397399
def err_experimental_clang_interp_failed : Error<
398400
"the experimental clang interpreter failed to evaluate an expression">;
399401

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10169,8 +10169,8 @@ def err_shufflevector_argument_too_large : Error<
1016910169

1017010170
def err_convertvector_non_vector : Error<
1017110171
"first argument to __builtin_convertvector must be a vector">;
10172-
def err_convertvector_non_vector_type : Error<
10173-
"second argument to __builtin_convertvector must be a vector type">;
10172+
def err_builtin_non_vector_type : Error<
10173+
"%0 argument to %1 must be of vector type">;
1017410174
def err_convertvector_incompatible_vector : Error<
1017510175
"first two arguments to __builtin_convertvector must have the same number of elements">;
1017610176

clang/include/clang/Basic/TokenKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ ALIAS("_pascal" , __pascal , KEYBORLAND)
746746

747747
// Clang Extensions.
748748
KEYWORD(__builtin_convertvector , KEYALL)
749+
UNARY_EXPR_OR_TYPE_TRAIT(__builtin_vectorelements, VectorElements, KEYALL)
749750
ALIAS("__char16_t" , char16_t , KEYCXX)
750751
ALIAS("__char32_t" , char32_t , KEYCXX)
751752
KEYWORD(__builtin_bit_cast , KEYALL)

clang/include/clang/Basic/arm_sve.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,7 @@ def SVBGRP_N : SInst<"svbgrp[_n_{d}]", "dda", "UcUsUiUl", MergeNone, "aarch64_sv
18621862
let TargetGuard = "sve2p1" in {
18631863
def SVFCLAMP : SInst<"svclamp[_{d}]", "dddd", "hfd", MergeNone, "aarch64_sve_fclamp", [], []>;
18641864
def SVPTRUE_COUNT : SInst<"svptrue_{d}", "}v", "QcQsQiQl", MergeNone, "aarch64_sve_ptrue_{d}", [IsOverloadNone], []>;
1865+
def SVPFALSE_COUNT_ALIAS : SInst<"svpfalse_c", "}v", "", MergeNone, "", [IsOverloadNone]>;
18651866

18661867
def SVPEXT_SINGLE : SInst<"svpext_lane_{d}", "P}i", "QcQsQiQl", MergeNone, "aarch64_sve_pext", [], [ImmCheck<1, ImmCheck0_3>]>;
18671868
def SVPEXT_X2 : SInst<"svpext_lane_{d}_x2", "2.P}i", "QcQsQiQl", MergeNone, "aarch64_sve_pext_x2", [], [ImmCheck<1, ImmCheck0_1>]>;

clang/lib/AST/ExprConstant.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13595,6 +13595,20 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
1359513595
Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
1359613596
.getQuantity(),
1359713597
E);
13598+
case UETT_VectorElements: {
13599+
QualType Ty = E->getTypeOfArgument();
13600+
// If the vector has a fixed size, we can determine the number of elements
13601+
// at compile time.
13602+
if (Ty->isVectorType())
13603+
return Success(Ty->castAs<VectorType>()->getNumElements(), E);
13604+
13605+
assert(Ty->isSizelessVectorType());
13606+
if (Info.InConstantContext)
13607+
Info.CCEDiag(E, diag::note_constexpr_non_const_vectorelements)
13608+
<< E->getSourceRange();
13609+
13610+
return false;
13611+
}
1359813612
}
1359913613

1360013614
llvm_unreachable("unknown expr/type trait");

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5127,6 +5127,14 @@ void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity,
51275127
Diags.Report(DiagID);
51285128
return;
51295129
}
5130+
case UETT_VectorElements: {
5131+
DiagnosticsEngine &Diags = Context.getDiags();
5132+
unsigned DiagID = Diags.getCustomDiagID(
5133+
DiagnosticsEngine::Error,
5134+
"cannot yet mangle __builtin_vectorelements expression");
5135+
Diags.Report(DiagID);
5136+
return;
5137+
}
51305138
}
51315139
break;
51325140
}

0 commit comments

Comments
 (0)