Skip to content

Commit d24f23e

Browse files
[Clang] NFC: Move Arm type attributes to separate trailing object. (#78424)
This decouples the Arm type attributes from other bits, which means the data will only be allocated when a function uses these Arm attributes. The first patch adds the bit `HasArmTypeAttributes` to `FunctionTypeBitfields`, which grows from 62 bits to 63 bits. In the second patch, I've moved this bit (`HasArmTypeAttributes`) to `FunctionTypeExtraBitfields`, because it looks like the bits in `FunctionTypeBitfields` are precious and we really don't want that struct to grow beyond 64 bits. I've split this out into two patches to explain the rationale, but those can be squashed before merging.
1 parent 361016f commit d24f23e

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

clang/include/clang/AST/Type.h

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4029,6 +4029,22 @@ class FunctionType : public Type {
40294029
/// because TrailingObjects cannot handle repeated types.
40304030
struct ExceptionType { QualType Type; };
40314031

4032+
/// A simple holder for various uncommon bits which do not fit in
4033+
/// FunctionTypeBitfields. Aligned to alignof(void *) to maintain the
4034+
/// alignment of subsequent objects in TrailingObjects.
4035+
struct alignas(void *) FunctionTypeExtraBitfields {
4036+
/// The number of types in the exception specification.
4037+
/// A whole unsigned is not needed here and according to
4038+
/// [implimits] 8 bits would be enough here.
4039+
unsigned NumExceptionType : 10;
4040+
4041+
LLVM_PREFERRED_TYPE(bool)
4042+
unsigned HasArmTypeAttributes : 1;
4043+
4044+
FunctionTypeExtraBitfields()
4045+
: NumExceptionType(0), HasArmTypeAttributes(false) {}
4046+
};
4047+
40324048
/// The AArch64 SME ACLE (Arm C/C++ Language Extensions) define a number
40334049
/// of function type attributes that can be set on function types, including
40344050
/// function pointers.
@@ -4042,7 +4058,8 @@ class FunctionType : public Type {
40424058
SME_ZAMask = 0b111 << SME_ZAShift,
40434059

40444060
SME_AttributeMask = 0b111'111 // We only support maximum 6 bits because of
4045-
// the bitmask in FunctionTypeExtraBitfields.
4061+
// the bitmask in FunctionTypeArmAttributes
4062+
// and ExtProtoInfo.
40464063
};
40474064

40484065
enum ArmStateValue : unsigned {
@@ -4057,20 +4074,15 @@ class FunctionType : public Type {
40574074
return (ArmStateValue)((AttrBits & SME_ZAMask) >> SME_ZAShift);
40584075
}
40594076

4060-
/// A simple holder for various uncommon bits which do not fit in
4061-
/// FunctionTypeBitfields. Aligned to alignof(void *) to maintain the
4062-
/// alignment of subsequent objects in TrailingObjects.
4063-
struct alignas(void *) FunctionTypeExtraBitfields {
4064-
/// The number of types in the exception specification.
4065-
/// A whole unsigned is not needed here and according to
4066-
/// [implimits] 8 bits would be enough here.
4067-
unsigned NumExceptionType : 10;
4068-
4077+
/// A holder for Arm type attributes as described in the Arm C/C++
4078+
/// Language extensions which are not particularly common to all
4079+
/// types and therefore accounted separately from FunctionTypeBitfields.
4080+
struct alignas(void *) FunctionTypeArmAttributes {
40694081
/// Any AArch64 SME ACLE type attributes that need to be propagated
40704082
/// on declarations and function pointers.
40714083
unsigned AArch64SMEAttributes : 6;
4072-
FunctionTypeExtraBitfields()
4073-
: NumExceptionType(0), AArch64SMEAttributes(SME_NormalFunction) {}
4084+
4085+
FunctionTypeArmAttributes() : AArch64SMEAttributes(SME_NormalFunction) {}
40744086
};
40754087

40764088
protected:
@@ -4169,7 +4181,8 @@ class FunctionProtoType final
41694181
public llvm::FoldingSetNode,
41704182
private llvm::TrailingObjects<
41714183
FunctionProtoType, QualType, SourceLocation,
4172-
FunctionType::FunctionTypeExtraBitfields, FunctionType::ExceptionType,
4184+
FunctionType::FunctionTypeExtraBitfields,
4185+
FunctionType::FunctionTypeArmAttributes, FunctionType::ExceptionType,
41734186
Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers> {
41744187
friend class ASTContext; // ASTContext creates these.
41754188
friend TrailingObjects;
@@ -4276,7 +4289,11 @@ class FunctionProtoType final
42764289

42774290
bool requiresFunctionProtoTypeExtraBitfields() const {
42784291
return ExceptionSpec.Type == EST_Dynamic ||
4279-
AArch64SMEAttributes != SME_NormalFunction;
4292+
requiresFunctionProtoTypeArmAttributes();
4293+
}
4294+
4295+
bool requiresFunctionProtoTypeArmAttributes() const {
4296+
return AArch64SMEAttributes != SME_NormalFunction;
42804297
}
42814298

42824299
void setArmSMEAttribute(AArch64SMETypeAttributes Kind, bool Enable = true) {
@@ -4296,6 +4313,10 @@ class FunctionProtoType final
42964313
return isVariadic();
42974314
}
42984315

4316+
unsigned numTrailingObjects(OverloadToken<FunctionTypeArmAttributes>) const {
4317+
return hasArmTypeAttributes();
4318+
}
4319+
42994320
unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const {
43004321
return hasExtraBitfields();
43014322
}
@@ -4384,6 +4405,12 @@ class FunctionProtoType final
43844405

43854406
}
43864407

4408+
bool hasArmTypeAttributes() const {
4409+
return FunctionTypeBits.HasExtraBitfields &&
4410+
getTrailingObjects<FunctionTypeExtraBitfields>()
4411+
->HasArmTypeAttributes;
4412+
}
4413+
43874414
bool hasExtQualifiers() const {
43884415
return FunctionTypeBits.HasExtQuals;
43894416
}
@@ -4595,9 +4622,9 @@ class FunctionProtoType final
45954622
/// Return a bitmask describing the SME attributes on the function type, see
45964623
/// AArch64SMETypeAttributes for their values.
45974624
unsigned getAArch64SMEAttributes() const {
4598-
if (!hasExtraBitfields())
4625+
if (!hasArmTypeAttributes())
45994626
return SME_NormalFunction;
4600-
return getTrailingObjects<FunctionTypeExtraBitfields>()
4627+
return getTrailingObjects<FunctionTypeArmAttributes>()
46014628
->AArch64SMEAttributes;
46024629
}
46034630

clang/lib/AST/ASTContext.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4484,10 +4484,11 @@ QualType ASTContext::getFunctionTypeInternal(
44844484
EPI.ExceptionSpec.Type, EPI.ExceptionSpec.Exceptions.size());
44854485
size_t Size = FunctionProtoType::totalSizeToAlloc<
44864486
QualType, SourceLocation, FunctionType::FunctionTypeExtraBitfields,
4487-
FunctionType::ExceptionType, Expr *, FunctionDecl *,
4488-
FunctionProtoType::ExtParameterInfo, Qualifiers>(
4487+
FunctionType::FunctionTypeArmAttributes, FunctionType::ExceptionType,
4488+
Expr *, FunctionDecl *, FunctionProtoType::ExtParameterInfo, Qualifiers>(
44894489
NumArgs, EPI.Variadic, EPI.requiresFunctionProtoTypeExtraBitfields(),
4490-
ESH.NumExceptionType, ESH.NumExprPtr, ESH.NumFunctionDeclPtr,
4490+
EPI.requiresFunctionProtoTypeArmAttributes(), ESH.NumExceptionType,
4491+
ESH.NumExprPtr, ESH.NumFunctionDeclPtr,
44914492
EPI.ExtParameterInfos ? NumArgs : 0,
44924493
EPI.TypeQuals.hasNonFastQualifiers() ? 1 : 0);
44934494

clang/lib/AST/Type.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,6 +3459,14 @@ FunctionProtoType::FunctionProtoType(QualType result, ArrayRef<QualType> params,
34593459
FunctionTypeBits.HasExtraBitfields = false;
34603460
}
34613461

3462+
if (epi.requiresFunctionProtoTypeArmAttributes()) {
3463+
auto &ArmTypeAttrs = *getTrailingObjects<FunctionTypeArmAttributes>();
3464+
ArmTypeAttrs = FunctionTypeArmAttributes();
3465+
3466+
// Also set the bit in FunctionTypeExtraBitfields
3467+
auto &ExtraBits = *getTrailingObjects<FunctionTypeExtraBitfields>();
3468+
ExtraBits.HasArmTypeAttributes = true;
3469+
}
34623470

34633471
// Fill in the trailing argument array.
34643472
auto *argSlot = getTrailingObjects<QualType>();
@@ -3470,10 +3478,10 @@ FunctionProtoType::FunctionProtoType(QualType result, ArrayRef<QualType> params,
34703478

34713479
// Propagate the SME ACLE attributes.
34723480
if (epi.AArch64SMEAttributes != SME_NormalFunction) {
3473-
auto &ExtraBits = *getTrailingObjects<FunctionTypeExtraBitfields>();
3481+
auto &ArmTypeAttrs = *getTrailingObjects<FunctionTypeArmAttributes>();
34743482
assert(epi.AArch64SMEAttributes <= SME_AttributeMask &&
34753483
"Not enough bits to encode SME attributes");
3476-
ExtraBits.AArch64SMEAttributes = epi.AArch64SMEAttributes;
3484+
ArmTypeAttrs.AArch64SMEAttributes = epi.AArch64SMEAttributes;
34773485
}
34783486

34793487
// Fill in the exception type array if present.

0 commit comments

Comments
 (0)