Skip to content

Commit de65b6b

Browse files
authored
[Clang] Add __builtin_vectorelements to get number of elements in vector (#69010)
Adds a new `__builtin_vectorelements()` function which returns the number of elements for a given vector either at compile-time for fixed-sized vectors, e.g., created via `__attribute__((vector_size(N)))` or at runtime via a call to `@llvm.vscale.i32()` for scalable vectors, e.g., SVE or RISCV V. The new builtin follows a similar path as `sizeof()`, as it essentially does the same thing but for the number of elements in vector instead of the number of bytes. This allows us to re-use a lot of the existing logic to handle types etc. A small side addition is `Type::isSizelessVectorType()`, which we need to distinguish between sizeless vectors (SVE, RISCV V) and sizeless types (WASM). This is the [corresponding discussion](https://discourse.llvm.org/t/new-builtin-function-to-get-number-of-lanes-in-simd-vectors/73911).
1 parent 9ea2fd2 commit de65b6b

18 files changed

+283
-8
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/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
}

clang/lib/AST/Type.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ bool Type::isIncompleteType(NamedDecl **Def) const {
23692369
}
23702370

23712371
bool Type::isSizelessBuiltinType() const {
2372-
if (isSVESizelessBuiltinType() || isRVVSizelessBuiltinType())
2372+
if (isSizelessVectorType())
23732373
return true;
23742374

23752375
if (const BuiltinType *BT = getAs<BuiltinType>()) {
@@ -2403,6 +2403,10 @@ bool Type::isWebAssemblyTableType() const {
24032403

24042404
bool Type::isSizelessType() const { return isSizelessBuiltinType(); }
24052405

2406+
bool Type::isSizelessVectorType() const {
2407+
return isSVESizelessBuiltinType() || isRVVSizelessBuiltinType();
2408+
}
2409+
24062410
bool Type::isSVESizelessBuiltinType() const {
24072411
if (const BuiltinType *BT = getAs<BuiltinType>()) {
24082412
switch (BT->getKind()) {

0 commit comments

Comments
 (0)