Skip to content

Commit 89e61e7

Browse files
committed
[Sema][AArch64] Add semantics for arm_sve_vector_bits attribute
Summary: This patch implements semantics for the 'arm_sve_vector_bits' type attribute, defined by the Arm C Language Extensions (ACLE) for SVE [1]. The purpose of this attribute is to define fixed-length (VLST) versions of existing sizeless types (VLAT). Implemented in this patch is the the behaviour described in section 3.7.3.2 and minimal parts of sections 3.7.3.3 and 3.7.3.4, this includes: * Defining VLST globals, structs, unions, and local variables * Implicit casting between VLAT <=> VLST. * Diagnosis of ill-formed conditional expressions of the form: C ? E1 : E2 where E1 is a VLAT type and E2 is a VLST, or vice-versa. This avoids any ambiguity about the nature of the result type (i.e is it sized or sizeless). * For vectors: * sizeof(VLST) == N/8 * alignof(VLST) == 16 * For predicates: * sizeof(VLST) == N/64 * alignof(VLST) == 2 VLSTs have the same representation as VLATs in the AST but are wrapped with a TypeAttribute. Scalable types are currently emitted in the IR for uses such as globals and structs which don't support these types, this is addressed in the next patch with codegen, where VLSTs are lowered to sized arrays for globals, structs / unions and arrays. Not implemented in this patch is the behaviour guarded by the feature macros: * __ARM_FEATURE_SVE_VECTOR_OPERATORS * __ARM_FEATURE_SVE_PREDICATE_OPERATORS As such, the GNU __attribute__((vector_size)) extension is not available and operators such as binary '+' are not supported for VLSTs. Support for this is intended to be addressed by later patches. [1] https://developer.arm.com/documentation/100987/latest This is patch 2/4 of a patch series. Reviewers: sdesmalen, rsandifo-arm, efriedma, cameron.mcinally, ctetreau, rengolin, aaron.ballman Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D83551
1 parent 25203e7 commit 89e61e7

File tree

9 files changed

+251
-10
lines changed

9 files changed

+251
-10
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,10 @@ class ASTContext : public RefCountedBase<ASTContext> {
20862086
return getTypeSizeInCharsIfKnown(QualType(Ty, 0));
20872087
}
20882088

2089+
/// Returns the bitwidth of \p T, an SVE type attributed with
2090+
/// 'arm_sve_vector_bits'. Should only be called if T->isVLST().
2091+
unsigned getBitwidthForAttributedSveType(const Type *T) const;
2092+
20892093
/// Return the ABI-specified alignment of a (complete) type \p T, in
20902094
/// bits.
20912095
unsigned getTypeAlign(QualType T) const { return getTypeInfo(T).Align; }

clang/include/clang/AST/Type.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,9 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
19251925
bool isSizelessType() const;
19261926
bool isSizelessBuiltinType() const;
19271927

1928+
/// Determines if this is a vector-length-specific type (VLST), i.e. a
1929+
/// sizeless type with the 'arm_sve_vector_bits' attribute applied.
1930+
bool isVLST() const;
19281931
/// Determines if this is a sizeless type supported by the
19291932
/// 'arm_sve_vector_bits' type attribute, which can be applied to a single
19301933
/// SVE vector or predicate, excluding tuple types such as svint32x4_t.

clang/include/clang/Basic/Attr.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,8 +1534,10 @@ def NeonVectorType : TypeAttr {
15341534

15351535
def ArmSveVectorBits : TypeAttr {
15361536
let Spellings = [GNU<"arm_sve_vector_bits">];
1537-
let Args = [IntArgument<"NumBits">];
1537+
let Subjects = SubjectList<[TypedefName], ErrorDiag>;
1538+
let Args = [UnsignedArgument<"NumBits">];
15381539
let Documentation = [ArmSveVectorBitsDocs];
1540+
let PragmaAttributeSupport = 0;
15391541
}
15401542

15411543
def ArmMveStrictPolymorphism : TypeAttr, TargetSpecificAttr<TargetARM> {

clang/include/clang/Sema/Sema.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,10 @@ class Sema final {
19971997
bool RequireCompleteSizedType(SourceLocation Loc, QualType T, unsigned DiagID,
19981998
const Ts &... Args) {
19991999
SizelessTypeDiagnoser<Ts...> Diagnoser(DiagID, Args...);
2000-
return RequireCompleteType(Loc, T, CompleteTypeKind::Normal, Diagnoser);
2000+
CompleteTypeKind Kind = CompleteTypeKind::Normal;
2001+
if (T->isVLST())
2002+
Kind = CompleteTypeKind::AcceptSizeless;
2003+
return RequireCompleteType(Loc, T, Kind, Diagnoser);
20012004
}
20022005

20032006
void completeExprArrayBound(Expr *E);
@@ -2015,7 +2018,10 @@ class Sema final {
20152018
bool RequireCompleteSizedExprType(Expr *E, unsigned DiagID,
20162019
const Ts &... Args) {
20172020
SizelessTypeDiagnoser<Ts...> Diagnoser(DiagID, Args...);
2018-
return RequireCompleteExprType(E, CompleteTypeKind::Normal, Diagnoser);
2021+
CompleteTypeKind Kind = CompleteTypeKind::Normal;
2022+
if (E->getType()->isVLST())
2023+
Kind = CompleteTypeKind::AcceptSizeless;
2024+
return RequireCompleteExprType(E, Kind, Diagnoser);
20192025
}
20202026

20212027
bool RequireLiteralType(SourceLocation Loc, QualType T,

clang/lib/AST/ASTContext.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,50 @@ TypeInfo ASTContext::getTypeInfo(const Type *T) const {
18691869
return TI;
18701870
}
18711871

1872+
static unsigned getSveVectorWidth(const Type *T) {
1873+
// Get the vector size from the 'arm_sve_vector_bits' attribute via the
1874+
// AttributedTypeLoc associated with the typedef decl.
1875+
if (const auto *TT = T->getAs<TypedefType>()) {
1876+
const TypedefNameDecl *Typedef = TT->getDecl();
1877+
TypeSourceInfo *TInfo = Typedef->getTypeSourceInfo();
1878+
TypeLoc TL = TInfo->getTypeLoc();
1879+
if (AttributedTypeLoc ATL = TL.getAs<AttributedTypeLoc>())
1880+
if (const auto *Attr = ATL.getAttrAs<ArmSveVectorBitsAttr>())
1881+
return Attr->getNumBits();
1882+
}
1883+
1884+
llvm_unreachable("bad 'arm_sve_vector_bits' attribute!");
1885+
}
1886+
1887+
static unsigned getSvePredWidth(const ASTContext &Context, const Type *T) {
1888+
return getSveVectorWidth(T) / Context.getCharWidth();
1889+
}
1890+
1891+
unsigned ASTContext::getBitwidthForAttributedSveType(const Type *T) const {
1892+
assert(T->isVLST() &&
1893+
"getBitwidthForAttributedSveType called for non-attributed type!");
1894+
1895+
switch (T->castAs<BuiltinType>()->getKind()) {
1896+
default:
1897+
llvm_unreachable("unknown builtin type!");
1898+
case BuiltinType::SveInt8:
1899+
case BuiltinType::SveInt16:
1900+
case BuiltinType::SveInt32:
1901+
case BuiltinType::SveInt64:
1902+
case BuiltinType::SveUint8:
1903+
case BuiltinType::SveUint16:
1904+
case BuiltinType::SveUint32:
1905+
case BuiltinType::SveUint64:
1906+
case BuiltinType::SveFloat16:
1907+
case BuiltinType::SveFloat32:
1908+
case BuiltinType::SveFloat64:
1909+
case BuiltinType::SveBFloat16:
1910+
return getSveVectorWidth(T);
1911+
case BuiltinType::SveBool:
1912+
return getSvePredWidth(*this, T);
1913+
}
1914+
}
1915+
18721916
/// getTypeInfoImpl - Return the size of the specified type, in bits. This
18731917
/// method does not work on incomplete types.
18741918
///
@@ -2273,7 +2317,10 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
22732317
Align = Info.Align;
22742318
AlignIsRequired = Info.AlignIsRequired;
22752319
}
2276-
Width = Info.Width;
2320+
if (T->isVLST())
2321+
Width = getBitwidthForAttributedSveType(T);
2322+
else
2323+
Width = Info.Width;
22772324
break;
22782325
}
22792326

clang/lib/AST/Type.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,6 +2318,13 @@ bool Type::isVLSTBuiltinType() const {
23182318
return false;
23192319
}
23202320

2321+
bool Type::isVLST() const {
2322+
if (!isVLSTBuiltinType())
2323+
return false;
2324+
2325+
return hasAttr(attr::ArmSveVectorBits);
2326+
}
2327+
23212328
bool QualType::isPODType(const ASTContext &Context) const {
23222329
// C++11 has a more relaxed definition of POD.
23232330
if (Context.getLangOpts().CPlusPlus11)

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8004,7 +8004,7 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
80048004
return;
80058005
}
80068006

8007-
if (!NewVD->hasLocalStorage() && T->isSizelessType()) {
8007+
if (!NewVD->hasLocalStorage() && T->isSizelessType() && !T->isVLST()) {
80088008
Diag(NewVD->getLocation(), diag::err_sizeless_nonlocal) << T;
80098009
NewVD->setInvalidDecl();
80108010
return;

clang/lib/Sema/SemaType.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
23042304
return QualType();
23052305
}
23062306

2307-
if (T->isSizelessType()) {
2307+
if (T->isSizelessType() && !T->isVLST()) {
23082308
Diag(Loc, diag::err_array_incomplete_or_sizeless_type) << 1 << T;
23092309
return QualType();
23102310
}
@@ -7754,10 +7754,14 @@ static void HandleNeonVectorTypeAttr(QualType &CurType, const ParsedAttr &Attr,
77547754
/// HandleArmSveVectorBitsTypeAttr - The "arm_sve_vector_bits" attribute is
77557755
/// used to create fixed-length versions of sizeless SVE types defined by
77567756
/// the ACLE, such as svint32_t and svbool_t.
7757-
static void HandleArmSveVectorBitsTypeAttr(QualType &CurType,
7758-
const ParsedAttr &Attr, Sema &S) {
7757+
static void HandleArmSveVectorBitsTypeAttr(TypeProcessingState &State,
7758+
QualType &CurType,
7759+
ParsedAttr &Attr) {
7760+
Sema &S = State.getSema();
7761+
ASTContext &Ctx = S.Context;
7762+
77597763
// Target must have SVE.
7760-
if (!S.Context.getTargetInfo().hasFeature("sve")) {
7764+
if (!Ctx.getTargetInfo().hasFeature("sve")) {
77617765
S.Diag(Attr.getLoc(), diag::err_attribute_unsupported) << Attr;
77627766
Attr.setInvalid();
77637767
return;
@@ -7801,6 +7805,9 @@ static void HandleArmSveVectorBitsTypeAttr(QualType &CurType,
78017805
Attr.setInvalid();
78027806
return;
78037807
}
7808+
7809+
auto *A = ::new (Ctx) ArmSveVectorBitsAttr(Ctx, Attr, VecSize);
7810+
CurType = State.getAttributedType(A, CurType, CurType);
78047811
}
78057812

78067813
static void HandleArmMveStrictPolymorphismAttr(TypeProcessingState &State,
@@ -8067,7 +8074,7 @@ static void processTypeAttrs(TypeProcessingState &state, QualType &type,
80678074
attr.setUsedAsTypeAttr();
80688075
break;
80698076
case ParsedAttr::AT_ArmSveVectorBits:
8070-
HandleArmSveVectorBitsTypeAttr(type, attr, state.getSema());
8077+
HandleArmSveVectorBitsTypeAttr(state, type, attr);
80718078
attr.setUsedAsTypeAttr();
80728079
break;
80738080
case ParsedAttr::AT_ArmMveStrictPolymorphism: {

clang/test/Sema/attr-arm-sve-vector-bits.c

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,168 @@ typedef int badtype2 __attribute__((arm_sve_vector_bits(N))); // expec
6060
typedef float badtype3 __attribute__((arm_sve_vector_bits(N))); // expected-error {{'arm_sve_vector_bits' attribute applied to non-SVE type 'float'}}
6161
typedef svint8x2_t badtype4 __attribute__((arm_sve_vector_bits(N))); // expected-error {{'arm_sve_vector_bits' attribute applied to non-SVE type 'svint8x2_t' (aka '__clang_svint8x2_t')}}
6262
typedef svfloat32x3_t badtype5 __attribute__((arm_sve_vector_bits(N))); // expected-error {{'arm_sve_vector_bits' attribute applied to non-SVE type 'svfloat32x3_t' (aka '__clang_svfloat32x3_t')}}
63+
64+
// Attribute only applies to typedefs.
65+
svint8_t non_typedef_type __attribute__((arm_sve_vector_bits(N))); // expected-error {{'arm_sve_vector_bits' attribute only applies to typedefs}}
66+
67+
// Test that we can define non-local fixed-length SVE types (unsupported for
68+
// sizeless types).
69+
fixed_int8_t global_int8;
70+
fixed_bfloat16_t global_bfloat16;
71+
fixed_bool_t global_bool;
72+
73+
extern fixed_int8_t extern_int8;
74+
extern fixed_bfloat16_t extern_bfloat16;
75+
extern fixed_bool_t extern_bool;
76+
77+
static fixed_int8_t static_int8;
78+
static fixed_bfloat16_t static_bfloat16;
79+
static fixed_bool_t static_bool;
80+
81+
fixed_int8_t *global_int8_ptr;
82+
extern fixed_int8_t *extern_int8_ptr;
83+
static fixed_int8_t *static_int8_ptr;
84+
__thread fixed_int8_t thread_int8;
85+
86+
typedef fixed_int8_t int8_typedef;
87+
typedef fixed_int8_t *int8_ptr_typedef;
88+
89+
// Test sized expressions
90+
int sizeof_int8 = sizeof(global_int8);
91+
int sizeof_int8_var = sizeof(*global_int8_ptr);
92+
int sizeof_int8_var_ptr = sizeof(global_int8_ptr);
93+
94+
extern fixed_int8_t *extern_int8_ptr;
95+
96+
int alignof_int8 = __alignof__(extern_int8);
97+
int alignof_int8_var = __alignof__(*extern_int8_ptr);
98+
int alignof_int8_var_ptr = __alignof__(extern_int8_ptr);
99+
100+
void f(int c) {
101+
fixed_int8_t fs8;
102+
svint8_t ss8;
103+
104+
void *sel __attribute__((unused));
105+
sel = c ? ss8 : fs8; // expected-error {{incompatible operand types ('svint8_t' (aka '__SVInt8_t') and 'fixed_int8_t' (aka '__SVInt8_t'))}}
106+
sel = c ? fs8 : ss8; // expected-error {{incompatible operand types ('fixed_int8_t' (aka '__SVInt8_t') and 'svint8_t' (aka '__SVInt8_t'))}}
107+
}
108+
109+
// --------------------------------------------------------------------------//
110+
// Sizeof
111+
112+
#define VECTOR_SIZE ((N / 8))
113+
#define PRED_SIZE ((N / 64))
114+
115+
_Static_assert(sizeof(fixed_int8_t) == VECTOR_SIZE, "");
116+
117+
_Static_assert(sizeof(fixed_int16_t) == VECTOR_SIZE, "");
118+
_Static_assert(sizeof(fixed_int32_t) == VECTOR_SIZE, "");
119+
_Static_assert(sizeof(fixed_int64_t) == VECTOR_SIZE, "");
120+
121+
_Static_assert(sizeof(fixed_uint8_t) == VECTOR_SIZE, "");
122+
_Static_assert(sizeof(fixed_uint16_t) == VECTOR_SIZE, "");
123+
_Static_assert(sizeof(fixed_uint32_t) == VECTOR_SIZE, "");
124+
_Static_assert(sizeof(fixed_uint64_t) == VECTOR_SIZE, "");
125+
126+
_Static_assert(sizeof(fixed_float16_t) == VECTOR_SIZE, "");
127+
_Static_assert(sizeof(fixed_float32_t) == VECTOR_SIZE, "");
128+
_Static_assert(sizeof(fixed_float64_t) == VECTOR_SIZE, "");
129+
130+
_Static_assert(sizeof(fixed_bfloat16_t) == VECTOR_SIZE, "");
131+
132+
_Static_assert(sizeof(fixed_bool_t) == PRED_SIZE, "");
133+
134+
// --------------------------------------------------------------------------//
135+
// Alignof
136+
137+
#define VECTOR_ALIGN 16
138+
#define PRED_ALIGN 2
139+
140+
_Static_assert(__alignof__(fixed_int8_t) == VECTOR_ALIGN, "");
141+
_Static_assert(__alignof__(fixed_int16_t) == VECTOR_ALIGN, "");
142+
_Static_assert(__alignof__(fixed_int32_t) == VECTOR_ALIGN, "");
143+
_Static_assert(__alignof__(fixed_int64_t) == VECTOR_ALIGN, "");
144+
145+
_Static_assert(__alignof__(fixed_uint8_t) == VECTOR_ALIGN, "");
146+
_Static_assert(__alignof__(fixed_uint16_t) == VECTOR_ALIGN, "");
147+
_Static_assert(__alignof__(fixed_uint32_t) == VECTOR_ALIGN, "");
148+
_Static_assert(__alignof__(fixed_uint64_t) == VECTOR_ALIGN, "");
149+
150+
_Static_assert(__alignof__(fixed_float16_t) == VECTOR_ALIGN, "");
151+
_Static_assert(__alignof__(fixed_float32_t) == VECTOR_ALIGN, "");
152+
_Static_assert(__alignof__(fixed_float64_t) == VECTOR_ALIGN, "");
153+
154+
_Static_assert(__alignof__(fixed_bfloat16_t) == VECTOR_ALIGN, "");
155+
156+
_Static_assert(__alignof__(fixed_bool_t) == PRED_ALIGN, "");
157+
158+
// --------------------------------------------------------------------------//
159+
// Structs
160+
161+
struct struct_int64 { fixed_int64_t x, y[5]; };
162+
struct struct_float64 { fixed_float64_t x, y[5]; };
163+
struct struct_bfloat16 { fixed_bfloat16_t x, y[5]; };
164+
struct struct_bool { fixed_bool_t x, y[5]; };
165+
166+
// --------------------------------------------------------------------------//
167+
// Unions
168+
union union_int64 { fixed_int64_t x, y[5]; };
169+
union union_float64 { fixed_float64_t x, y[5]; };
170+
union union_bfloat16 { fixed_bfloat16_t x, y[5]; };
171+
union union_bool { fixed_bool_t x, y[5]; };
172+
173+
// --------------------------------------------------------------------------//
174+
// Implicit casts
175+
176+
#define TEST_CAST(TYPE) \
177+
sv##TYPE##_t to_sv##TYPE##_t(fixed_##TYPE##_t x) { return x; } \
178+
fixed_##TYPE##_t from_sv##TYPE##_t(sv##TYPE##_t x) { return x; }
179+
180+
TEST_CAST(int8)
181+
TEST_CAST(int16)
182+
TEST_CAST(int32)
183+
TEST_CAST(int64)
184+
TEST_CAST(uint8)
185+
TEST_CAST(uint16)
186+
TEST_CAST(uint32)
187+
TEST_CAST(uint64)
188+
TEST_CAST(float16)
189+
TEST_CAST(float32)
190+
TEST_CAST(float64)
191+
TEST_CAST(bfloat16)
192+
TEST_CAST(bool)
193+
194+
// Test the implicit conversion only applies to valid types
195+
fixed_int8_t to_fixed_int8_t__from_svuint8_t(svuint8_t x) { return x; } // expected-error {{returning 'svuint8_t' (aka '__SVUint8_t') from a function with incompatible result type 'fixed_int8_t' (aka '__SVInt8_t')}}
196+
fixed_bool_t to_fixed_bool_t__from_svint32_t(svint32_t x) { return x; } // expected-error {{returning 'svint32_t' (aka '__SVInt32_t') from a function with incompatible result type 'fixed_bool_t' (aka '__SVBool_t')}}
197+
198+
// Test the implicit conversion only applies to fixed-length types
199+
typedef signed int vSInt32 __attribute__((__vector_size__(16)));
200+
svint32_t to_svint32_t_from_gnut(vSInt32 x) { return x; } // expected-error {{returning 'vSInt32' (vector of 4 'int' values) from a function with incompatible result type 'svint32_t' (aka '__SVInt32_t')}}
201+
202+
vSInt32 to_gnut_from_svint32_t(svint32_t x) { return x; } // expected-error {{returning 'svint32_t' (aka '__SVInt32_t') from a function with incompatible result type 'vSInt32' (vector of 4 'int' values)}}
203+
204+
// --------------------------------------------------------------------------//
205+
// Test the scalable and fixed-length types can be used interchangeably
206+
207+
svint32_t __attribute__((overloadable)) svfunc(svint32_t op1, svint32_t op2);
208+
svfloat64_t __attribute__((overloadable)) svfunc(svfloat64_t op1, svfloat64_t op2);
209+
svbool_t __attribute__((overloadable)) svfunc(svbool_t op1, svbool_t op2);
210+
211+
#define TEST_CALL(TYPE) \
212+
fixed_##TYPE##_t \
213+
call_##TYPE##_ff(fixed_##TYPE##_t op1, fixed_##TYPE##_t op2) { \
214+
return svfunc(op1, op2); \
215+
} \
216+
fixed_##TYPE##_t \
217+
call_##TYPE##_fs(fixed_##TYPE##_t op1, sv##TYPE##_t op2) { \
218+
return svfunc(op1, op2); \
219+
} \
220+
fixed_##TYPE##_t \
221+
call_##TYPE##_sf(sv##TYPE##_t op1, fixed_##TYPE##_t op2) { \
222+
return svfunc(op1, op2); \
223+
}
224+
225+
TEST_CALL(int32)
226+
TEST_CALL(float64)
227+
TEST_CALL(bool)

0 commit comments

Comments
 (0)