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()) {

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3083,6 +3083,9 @@ ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
30833083
E->getTypeOfArgument()->getPointeeType()))
30843084
.getQuantity();
30853085
return llvm::ConstantInt::get(CGF.SizeTy, Alignment);
3086+
} else if (E->getKind() == UETT_VectorElements) {
3087+
auto *VecTy = cast<llvm::VectorType>(ConvertType(E->getTypeOfArgument()));
3088+
return Builder.CreateElementCount(CGF.SizeTy, VecTy->getElementCount());
30863089
}
30873090

30883091
// If this isn't sizeof(vla), the result must be constant; use the constant

clang/lib/Parse/ParseExpr.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,7 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind,
14631463
case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
14641464
// unary-expression: '__builtin_omp_required_simd_align' '(' type-name ')'
14651465
case tok::kw___builtin_omp_required_simd_align:
1466+
case tok::kw___builtin_vectorelements:
14661467
if (NotPrimaryExpression)
14671468
*NotPrimaryExpression = true;
14681469
AllowSuffix = false;
@@ -2339,7 +2340,8 @@ Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
23392340
assert(OpTok.isOneOf(tok::kw_typeof, tok::kw_typeof_unqual, tok::kw_sizeof,
23402341
tok::kw___alignof, tok::kw_alignof, tok::kw__Alignof,
23412342
tok::kw_vec_step,
2342-
tok::kw___builtin_omp_required_simd_align) &&
2343+
tok::kw___builtin_omp_required_simd_align,
2344+
tok::kw___builtin_vectorelements) &&
23432345
"Not a typeof/sizeof/alignof/vec_step expression!");
23442346

23452347
ExprResult Operand;
@@ -2460,7 +2462,8 @@ ExprResult Parser::ParseSYCLUniqueStableNameExpression() {
24602462
ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
24612463
assert(Tok.isOneOf(tok::kw_sizeof, tok::kw___alignof, tok::kw_alignof,
24622464
tok::kw__Alignof, tok::kw_vec_step,
2463-
tok::kw___builtin_omp_required_simd_align) &&
2465+
tok::kw___builtin_omp_required_simd_align,
2466+
tok::kw___builtin_vectorelements) &&
24642467
"Not a sizeof/alignof/vec_step expression!");
24652468
Token OpTok = Tok;
24662469
ConsumeToken();
@@ -2539,6 +2542,8 @@ ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
25392542
ExprKind = UETT_VecStep;
25402543
else if (OpTok.is(tok::kw___builtin_omp_required_simd_align))
25412544
ExprKind = UETT_OpenMPRequiredSimdAlign;
2545+
else if (OpTok.is(tok::kw___builtin_vectorelements))
2546+
ExprKind = UETT_VectorElements;
25422547

25432548
if (isCastExpr)
25442549
return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),

clang/lib/Sema/SemaChecking.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8752,8 +8752,9 @@ ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo,
87528752
diag::err_convertvector_non_vector)
87538753
<< E->getSourceRange());
87548754
if (!DstTy->isVectorType() && !DstTy->isDependentType())
8755-
return ExprError(Diag(BuiltinLoc,
8756-
diag::err_convertvector_non_vector_type));
8755+
return ExprError(Diag(BuiltinLoc, diag::err_builtin_non_vector_type)
8756+
<< "second"
8757+
<< "__builtin_convertvector");
87578758

87588759
if (!SrcTy->isDependentType() && !DstTy->isDependentType()) {
87598760
unsigned SrcElts = SrcTy->castAs<VectorType>()->getNumElements();

clang/lib/Sema/SemaExpr.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "clang/Basic/SourceManager.h"
3636
#include "clang/Basic/Specifiers.h"
3737
#include "clang/Basic/TargetInfo.h"
38+
#include "clang/Basic/TypeTraits.h"
3839
#include "clang/Lex/LiteralSupport.h"
3940
#include "clang/Lex/Preprocessor.h"
4041
#include "clang/Sema/AnalysisBasedWarnings.h"
@@ -4353,6 +4354,18 @@ static bool CheckVecStepTraitOperandType(Sema &S, QualType T,
43534354
return false;
43544355
}
43554356

4357+
static bool CheckVectorElementsTraitOperandType(Sema &S, QualType T,
4358+
SourceLocation Loc,
4359+
SourceRange ArgRange) {
4360+
// builtin_vectorelements supports both fixed-sized and scalable vectors.
4361+
if (!T->isVectorType() && !T->isSizelessVectorType())
4362+
return S.Diag(Loc, diag::err_builtin_non_vector_type)
4363+
<< ""
4364+
<< "__builtin_vectorelements" << T << ArgRange;
4365+
4366+
return false;
4367+
}
4368+
43564369
static bool CheckExtensionTraitOperandType(Sema &S, QualType T,
43574370
SourceLocation Loc,
43584371
SourceRange ArgRange,
@@ -4454,6 +4467,10 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E,
44544467
return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(),
44554468
E->getSourceRange());
44564469

4470+
if (ExprKind == UETT_VectorElements)
4471+
return CheckVectorElementsTraitOperandType(*this, ExprTy, E->getExprLoc(),
4472+
E->getSourceRange());
4473+
44574474
// Explicitly list some types as extensions.
44584475
if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(),
44594476
E->getSourceRange(), ExprKind))
@@ -4745,6 +4762,10 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType,
47454762
if (ExprKind == UETT_VecStep)
47464763
return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange);
47474764

4765+
if (ExprKind == UETT_VectorElements)
4766+
return CheckVectorElementsTraitOperandType(*this, ExprType, OpLoc,
4767+
ExprRange);
4768+
47484769
// Explicitly list some types as extensions.
47494770
if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange,
47504771
ExprKind))
@@ -4851,6 +4872,8 @@ Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc,
48514872
} else if (E->refersToBitField()) { // C99 6.5.3.4p1.
48524873
Diag(E->getExprLoc(), diag::err_sizeof_alignof_typeof_bitfield) << 0;
48534874
isInvalid = true;
4875+
} else if (ExprKind == UETT_VectorElements) {
4876+
isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_VectorElements);
48544877
} else {
48554878
isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf);
48564879
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +neon %s -emit-llvm -disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,NEON %s
2+
// RUN: %clang_cc1 -O1 -triple aarch64 -target-feature +sve %s -emit-llvm -disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,SVE %s
3+
// RUN: %clang_cc1 -O1 -triple riscv64 -target-feature +v %s -emit-llvm -disable-llvm-passes -o - | FileCheck --check-prefixes=CHECK,RISCV %s
4+
5+
// Note that this does not make sense to check for x86 SIMD types, because
6+
// __m128i, __m256i, and __m512i do not specify the element type. There are no
7+
// "logical" number of elements in them.
8+
9+
typedef int int1 __attribute__((vector_size(4)));
10+
typedef int int4 __attribute__((vector_size(16)));
11+
typedef int int8 __attribute__((vector_size(32)));
12+
typedef int int16 __attribute__((vector_size(64)));
13+
typedef float float2 __attribute__((vector_size(8)));
14+
typedef long extLong4 __attribute__((ext_vector_type(4)));
15+
16+
17+
int test_builtin_vectorelements_int1() {
18+
// CHECK-LABEL: i32 @test_builtin_vectorelements_int1(
19+
// CHECK: ret i32 1
20+
return __builtin_vectorelements(int1);
21+
}
22+
23+
int test_builtin_vectorelements_int4() {
24+
// CHECK-LABEL: i32 @test_builtin_vectorelements_int4(
25+
// CHECK: ret i32 4
26+
return __builtin_vectorelements(int4);
27+
}
28+
29+
int test_builtin_vectorelements_int8() {
30+
// CHECK-LABEL: i32 @test_builtin_vectorelements_int8(
31+
// CHECK: ret i32 8
32+
return __builtin_vectorelements(int8);
33+
}
34+
35+
int test_builtin_vectorelements_int16() {
36+
// CHECK-LABEL: i32 @test_builtin_vectorelements_int16(
37+
// CHECK: ret i32 16
38+
return __builtin_vectorelements(int16);
39+
}
40+
41+
int test_builtin_vectorelements_float2() {
42+
// CHECK-LABEL: i32 @test_builtin_vectorelements_float2(
43+
// CHECK: ret i32 2
44+
return __builtin_vectorelements(float2);
45+
}
46+
47+
int test_builtin_vectorelements_extLong4() {
48+
// CHECK-LABEL: i32 @test_builtin_vectorelements_extLong4(
49+
// CHECK: ret i32 4
50+
return __builtin_vectorelements(extLong4);
51+
}
52+
53+
int test_builtin_vectorelements_multiply_constant() {
54+
// CHECK-LABEL: i32 @test_builtin_vectorelements_multiply_constant(
55+
// CHECK: ret i32 32
56+
return __builtin_vectorelements(int16) * 2;
57+
}
58+
59+
60+
#if defined(__ARM_NEON)
61+
#include <arm_neon.h>
62+
63+
int test_builtin_vectorelements_neon32x4() {
64+
// NEON: i32 @test_builtin_vectorelements_neon32x4(
65+
// NEON: ret i32 4
66+
return __builtin_vectorelements(uint32x4_t);
67+
}
68+
69+
int test_builtin_vectorelements_neon64x1() {
70+
// NEON: i32 @test_builtin_vectorelements_neon64x1(
71+
// NEON: ret i32 1
72+
return __builtin_vectorelements(uint64x1_t);
73+
}
74+
#endif
75+
76+
#if defined(__ARM_FEATURE_SVE)
77+
#include <arm_sve.h>
78+
79+
long test_builtin_vectorelements_sve32() {
80+
// SVE: i64 @test_builtin_vectorelements_sve32(
81+
// SVE: [[VSCALE:%.+]] = call i64 @llvm.vscale.i64()
82+
// SVE: [[RES:%.+]] = mul i64 [[VSCALE]], 4
83+
// SVE: ret i64 [[RES]]
84+
return __builtin_vectorelements(svuint32_t);
85+
}
86+
87+
long test_builtin_vectorelements_sve8() {
88+
// SVE: i64 @test_builtin_vectorelements_sve8(
89+
// SVE: [[VSCALE:%.+]] = call i64 @llvm.vscale.i64()
90+
// SVE: [[RES:%.+]] = mul i64 [[VSCALE]], 16
91+
// SVE: ret i64 [[RES]]
92+
return __builtin_vectorelements(svuint8_t);
93+
}
94+
#endif
95+
96+
#if defined(__riscv)
97+
#include <riscv_vector.h>
98+
99+
long test_builtin_vectorelements_riscv8() {
100+
// RISCV: i64 @test_builtin_vectorelements_riscv8(
101+
// RISCV: [[VSCALE:%.+]] = call i64 @llvm.vscale.i64()
102+
// RISCV: [[RES:%.+]] = mul i64 [[VSCALE]], 8
103+
// RISCV: ret i64 [[RES]]
104+
return __builtin_vectorelements(vuint8m1_t);
105+
}
106+
107+
long test_builtin_vectorelements_riscv64() {
108+
// RISCV: i64 @test_builtin_vectorelements_riscv64(
109+
// RISCV: [[VSCALE:%.+]] = call i64 @llvm.vscale.i64()
110+
// RISCV: ret i64 [[VSCALE]]
111+
return __builtin_vectorelements(vuint64m1_t);
112+
}
113+
114+
long test_builtin_vectorelements_riscv32m2() {
115+
// RISCV: i64 @test_builtin_vectorelements_riscv32m2(
116+
// RISCV: [[VSCALE:%.+]] = call i64 @llvm.vscale.i64()
117+
// RISCV: [[RES:%.+]] = mul i64 [[VSCALE]], 4
118+
// RISCV: ret i64 [[RES]]
119+
return __builtin_vectorelements(vuint32m2_t);
120+
}
121+
#endif
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -triple aarch64 -fsyntax-only -verify -disable-llvm-passes %s
2+
3+
void test_builtin_vectorelements() {
4+
__builtin_vectorelements(int); // expected-error {{argument to __builtin_vectorelements must be of vector type}}
5+
__builtin_vectorelements(float); // expected-error {{argument to __builtin_vectorelements must be of vector type}}
6+
__builtin_vectorelements(long*); // expected-error {{argument to __builtin_vectorelements must be of vector type}}
7+
8+
int a;
9+
__builtin_vectorelements(a); // expected-error {{argument to __builtin_vectorelements must be of vector type}}
10+
11+
typedef int veci4 __attribute__((vector_size(16)));
12+
(void) __builtin_vectorelements(veci4);
13+
14+
veci4 vec;
15+
(void) __builtin_vectorelements(vec);
16+
17+
typedef veci4 some_other_vec;
18+
(void) __builtin_vectorelements(some_other_vec);
19+
20+
struct Foo { int a; };
21+
__builtin_vectorelements(struct Foo); // expected-error {{argument to __builtin_vectorelements must be of vector type}}
22+
}
23+

0 commit comments

Comments
 (0)