[clang] 93f8004 - [ABI][AARCH64][NFC] Make SveCount a unique type (#245767)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 12:08:38 PST 2026


Author: Andy Kaylor
Date: 2026-11-25T20:08:31Z
New Revision: 93f80041f374584f4939372c22e956e058a15e27

URL: https://github.com/llvm/llvm-project/commit/93f80041f374584f4939372c22e956e058a15e27
DIFF: https://github.com/llvm/llvm-project/commit/93f80041f374584f4939372c22e956e058a15e27.diff

LOG: [ABI][AARCH64][NFC] Make SveCount a unique type (#245767)

The LLVM ABI library previously modeled SveCount (the AArch64
predicate-as-counter type) as a vector with a special kind to
distinguish it. Upon further reflection, this seems to have been a bad
choice as this type has almost no overlap with other vector types apart
from the fact that it occupies a predicate register.

This change separates SveCount as a separate type in the ABI library.
The classification and the LLVM IR type to which it is mapped remain
unchanged.

Assisted-by: Cursor / various models

Added: 
    

Modified: 
    clang/lib/CodeGen/QualTypeMapper.cpp
    clang/unittests/CodeGen/QualTypeMapperTest.cpp
    llvm/include/llvm/ABI/Types.h
    llvm/lib/ABI/IRTypeMapper.cpp
    llvm/lib/ABI/TargetInfo.cpp
    llvm/lib/ABI/Targets/AArch64.cpp
    llvm/lib/ABI/Types.cpp
    llvm/unittests/ABI/AArch64TargetInfoTest.cpp
    llvm/unittests/ABI/IRTypeMapperTest.cpp
    llvm/unittests/ABI/TypesTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/QualTypeMapper.cpp b/clang/lib/CodeGen/QualTypeMapper.cpp
index 3531ec2ba0812d..9a1966fa2a0b9c 100644
--- a/clang/lib/CodeGen/QualTypeMapper.cpp
+++ b/clang/lib/CodeGen/QualTypeMapper.cpp
@@ -286,8 +286,7 @@ QualTypeMapper::convertBuiltinType(const BuiltinType *BT) {
     return convertSVEBuiltinType(BT);
 
   case BuiltinType::SveCount:
-    return Builder.getScalablePredicateOrCountVectorType(
-        getTypeAlign(QT), llvm::abi::VectorKind::SVECount);
+    return Builder.getSveCountType(getTypeAlign(QT));
 
   // __mfp8 is an opaque 8-bit float. Vector elements stay 8-bit
   // integers, which is also their representation in LLVM IR.

diff  --git a/clang/unittests/CodeGen/QualTypeMapperTest.cpp b/clang/unittests/CodeGen/QualTypeMapperTest.cpp
index dc51bb6293c851..9f2f8e0e3c413d 100644
--- a/clang/unittests/CodeGen/QualTypeMapperTest.cpp
+++ b/clang/unittests/CodeGen/QualTypeMapperTest.cpp
@@ -58,6 +58,12 @@ class QualTypeMapperSVETest : public ::testing::Test {
     return dyn_cast<llvm::abi::OpaqueFloat8Type>(Mapper.convertType(QT));
   }
 
+  /// Maps \p QT and returns it as an ABI svcount_t type, or null if it did
+  /// not map to one.
+  const llvm::abi::SveCountType *mapToSveCount(QualType QT) {
+    return dyn_cast<llvm::abi::SveCountType>(Mapper.convertType(QT));
+  }
+
   /// Returns the underlying type of the file-scope typedef named \p Name.
   QualType lookupTypedef(StringRef Name) {
     for (Decl *D : context().getTranslationUnitDecl()->decls())
@@ -253,18 +259,16 @@ TEST_F(QualTypeMapperSVETest, MFloat8VectorUsesIntegerElement) {
   EXPECT_FALSE(Elt->isSigned());
 }
 
-// __SVCount_t is opaque, and is given the shape of svbool_t because it
-// occupies a predicate register.
-TEST_F(QualTypeMapperSVETest, SVECount) {
-  const llvm::abi::VectorType *SVCount = mapToVector(context().SveCountTy);
+// __SVCount_t is opaque rather than a vector. It occupies a predicate
+// register, so its size is that of svbool_t.
+TEST_F(QualTypeMapperSVETest, SveCount) {
+  const llvm::abi::SveCountType *SVCount = mapToSveCount(context().SveCountTy);
   ASSERT_NE(SVCount, nullptr);
 
-  EXPECT_EQ(SVCount->getVectorKind(), llvm::abi::VectorKind::SVECount);
-  EXPECT_TRUE(SVCount->isSVEType());
-  EXPECT_FALSE(SVCount->isSVEPredicate());
-  EXPECT_TRUE(SVCount->isScalable());
+  EXPECT_TRUE(SVCount->isSveCount());
+  EXPECT_FALSE(SVCount->isVector());
   EXPECT_FALSE(SVCount->isTuple());
-  EXPECT_EQ(SVCount->getNumElements(), llvm::ElementCount::getScalable(16));
+  EXPECT_TRUE(SVCount->isSVESizelessType());
   EXPECT_EQ(SVCount->getSizeInBits(), llvm::TypeSize::getScalable(16));
   EXPECT_EQ(SVCount->getAlignment(), llvm::Align(2));
 }

diff  --git a/llvm/include/llvm/ABI/Types.h b/llvm/include/llvm/ABI/Types.h
index f0713312335f9b..1f82d87b3d90ed 100644
--- a/llvm/include/llvm/ABI/Types.h
+++ b/llvm/include/llvm/ABI/Types.h
@@ -34,6 +34,7 @@ enum class TypeKind {
   Integer,
   Float,
   OpaqueFloat8,
+  SveCount,
   Pointer,
   Array,
   Vector,
@@ -109,6 +110,7 @@ class Type {
   bool isInteger() const { return Kind == TypeKind::Integer; }
   bool isFloat() const { return Kind == TypeKind::Float; }
   bool isOpaqueFloat8() const { return Kind == TypeKind::OpaqueFloat8; }
+  bool isSveCount() const { return Kind == TypeKind::SveCount; }
   bool isPointer() const { return Kind == TypeKind::Pointer; }
   bool isArray() const { return Kind == TypeKind::Array; }
   bool isVector() const { return Kind == TypeKind::Vector; }
@@ -214,6 +216,19 @@ class OpaqueFloat8Type : public Type {
   }
 };
 
+/// The AArch64 __SVCount_t (svcount_t) predicate-as-counter type. Such a value
+/// contains information about the number of active lanes, the element width,
+/// and a bit that indicates whether the generated mask should be inverted.
+class SveCountType : public Type {
+public:
+  explicit SveCountType(Align ABIAlign)
+      : Type(TypeKind::SveCount, TypeSize::getScalable(16), ABIAlign) {}
+
+  static bool classof(const Type *T) {
+    return T->getKind() == TypeKind::SveCount;
+  }
+};
+
 class PointerLikeType : public Type {
 protected:
   unsigned AddrSpace;
@@ -305,12 +320,7 @@ enum class VectorKind {
   /// fixed-length arm_sve_vector_bits form keeps unsigned char (i8)
   /// elements, matching the Clang AST. Both use this kind. Tuples of
   /// these vectors use TupleType.
-  SVEPredicate,
-
-  /// The AArch64 __SVCount_t type. It is opaque rather than a real vector,
-  /// but it occupies a predicate register, so it is given the same shape as
-  /// svbool_t.
-  SVECount,
+  SVEPredicate
 };
 
 class VectorType : public Type {
@@ -346,7 +356,6 @@ class VectorType : public Type {
 
   bool isSVEData() const { return VecKind == VectorKind::SVEData; }
   bool isSVEPredicate() const { return VecKind == VectorKind::SVEPredicate; }
-  bool isSVECount() const { return VecKind == VectorKind::SVECount; }
 
   bool isFixedLengthSVEData() const { return isFixedLength() && isSVEData(); }
   bool isFixedLengthSVEPredicate() const {
@@ -570,17 +579,16 @@ class TypeBuilder {
     return new (Allocator.Allocate<TupleType>()) TupleType(Vec, NumVectors);
   }
 
-  /// Creates a scalable predicate or count vector.
-  /// Note: The AArch64 __SVCount_t type is opaque, so it is modeled with the
-  /// shape of svbool_t: a scalable vector of 16 one-bit elements.
-  const VectorType *getScalablePredicateOrCountVectorType(Align ABIAlign,
-                                                          VectorKind Kind) {
-    assert((Kind == VectorKind::SVEPredicate || Kind == VectorKind::SVECount) &&
-           "expected predicate or count vector kind");
+  /// Creates a scalable SVE predicate vector: 16 one-bit elements per granule.
+  const VectorType *getScalablePredicateVectorType(Align ABIAlign) {
     const Type *PredicateBit =
         getIntegerType(1, Align(1), /*Signed=*/false, /*IsBitInt=*/false);
     return getVectorType(PredicateBit, ElementCount::getScalable(16), ABIAlign,
-                         Kind);
+                         VectorKind::SVEPredicate);
+  }
+
+  const SveCountType *getSveCountType(Align Align) {
+    return new (Allocator.Allocate<SveCountType>()) SveCountType(Align);
   }
 
   const RecordType *getRecordType(ArrayRef<FieldInfo> Fields, TypeSize Size,

diff  --git a/llvm/lib/ABI/IRTypeMapper.cpp b/llvm/lib/ABI/IRTypeMapper.cpp
index 4194bb25008186..d3ea5911c730ed 100644
--- a/llvm/lib/ABI/IRTypeMapper.cpp
+++ b/llvm/lib/ABI/IRTypeMapper.cpp
@@ -51,6 +51,9 @@ llvm::Type *IRTypeMapper::convertType(const abi::Type *ABIType) {
     // integer or a short vector.
     Result = llvm::FixedVectorType::get(llvm::Type::getInt8Ty(Context), 1);
     break;
+  case abi::TypeKind::SveCount:
+    Result = llvm::TargetExtType::get(Context, "aarch64.svcount");
+    break;
   case abi::TypeKind::Pointer:
     Result = llvm::PointerType::get(
         Context, cast<abi::PointerType>(ABIType)->getAddrSpace());
@@ -103,9 +106,6 @@ llvm::Type *IRTypeMapper::convertArrayType(const abi::ArrayType *AT) {
 }
 
 llvm::Type *IRTypeMapper::convertVectorType(const abi::VectorType *VT) {
-  if (VT->isSVECount())
-    return llvm::TargetExtType::get(Context, "aarch64.svcount");
-
   llvm::Type *ElementType = convertType(VT->getElementType());
   return llvm::VectorType::get(ElementType, VT->getNumElements());
 }

diff  --git a/llvm/lib/ABI/TargetInfo.cpp b/llvm/lib/ABI/TargetInfo.cpp
index 3177718e4cdb49..3d37fced4c1973 100644
--- a/llvm/lib/ABI/TargetInfo.cpp
+++ b/llvm/lib/ABI/TargetInfo.cpp
@@ -24,7 +24,7 @@ bool TargetInfo::isAggregateTypeForABI(const Type *Ty) const {
 
   // Check for fundamental scalar types.
   if (Ty->isInteger() || Ty->isFloat() || Ty->isOpaqueFloat8() ||
-      Ty->isPointer() || Ty->isVector() || Ty->isTuple())
+      Ty->isSveCount() || Ty->isPointer() || Ty->isVector() || Ty->isTuple())
     return false;
 
   // Data member pointers have scalar evaluation kind.

diff  --git a/llvm/lib/ABI/Targets/AArch64.cpp b/llvm/lib/ABI/Targets/AArch64.cpp
index 4f47e7f9de014c..3bd536c43a072c 100644
--- a/llvm/lib/ABI/Targets/AArch64.cpp
+++ b/llvm/lib/ABI/Targets/AArch64.cpp
@@ -214,7 +214,7 @@ ArgInfo AArch64TargetInfo::classifyArgumentType(
     // floats are passed in a SIMD and floating-point register. A tuple
     // occupies one register of the appropriate kind per vector it contains.
     if (const auto *VT = dyn_cast<VectorType>(Ty)) {
-      if (VT->isSVEPredicate() || VT->isSVECount())
+      if (VT->isSVEPredicate())
         NPRN = std::min(NPRN + 1, 4u);
       else
         NSRN = std::min(NSRN + 1, 8u);
@@ -223,6 +223,8 @@ ArgInfo AArch64TargetInfo::classifyArgumentType(
         NPRN = std::min(NPRN + TT->getNumVectors(), 4u);
       else
         NSRN = std::min(NSRN + TT->getNumVectors(), 8u);
+    } else if (Ty->isSveCount()) {
+      NPRN = std::min(NPRN + 1, 4u);
     } else if (Ty->isFloat() || Ty->isOpaqueFloat8()) {
       NSRN = std::min(NSRN + 1, 8u);
     }
@@ -374,7 +376,7 @@ bool AArch64TargetInfo::passAsAggregateType(const Type *Ty) const {
     const auto *TupleTy = dyn_cast<TupleType>(Ty);
     assert((!TupleTy || TupleTy->getNumVectors() > 1) &&
            "unexpected single vector tuple");
-    return TupleTy && !TupleTy->getVectorType()->isSVECount();
+    return TupleTy;
   }
   return isAggregateTypeForABI(Ty);
 }
@@ -386,8 +388,7 @@ bool AArch64TargetInfo::passAsAggregateType(const Type *Ty) const {
 const VectorType *AArch64TargetInfo::convertFixedToScalableVectorType(
     const VectorType *VT) const {
   if (VT->isFixedLengthSVEPredicate())
-    return TB.getScalablePredicateOrCountVectorType(Align(2),
-                                                    VectorKind::SVEPredicate);
+    return TB.getScalablePredicateVectorType(Align(2));
 
   assert(VT->isFixedLengthSVEData() && "expected a fixed-length SVE vector!");
 
@@ -415,8 +416,7 @@ ArgInfo AArch64TargetInfo::coerceIllegalVector(const VectorType *VT,
            VT->getElementType()->getSizeInBits().getFixedValue() == 8 &&
            "unexpected element type for SVE predicate!");
     NPRN = std::min(NPRN + 1, 4u);
-    return ArgInfo::getDirect(TB.getScalablePredicateOrCountVectorType(
-        Align(2), VectorKind::SVEPredicate));
+    return ArgInfo::getDirect(TB.getScalablePredicateVectorType(Align(2)));
   }
 
   if (VT->isFixedLengthSVEData()) {
@@ -599,7 +599,7 @@ bool AArch64TargetInfo::passAsPureScalableType(
 
   if (const auto *TT = dyn_cast<TupleType>(Ty)) {
     const VectorType *VT = TT->getVectorType();
-    if (!VT->isScalable() || VT->isSVECount())
+    if (!VT->isScalable())
       return false;
 
     unsigned N = TT->getNumVectors();

diff  --git a/llvm/lib/ABI/Types.cpp b/llvm/lib/ABI/Types.cpp
index b9a53a0b127adc..1b51f0b6331850 100644
--- a/llvm/lib/ABI/Types.cpp
+++ b/llvm/lib/ABI/Types.cpp
@@ -38,6 +38,7 @@ uint64_t llvm::abi::Type::getABISizeInBits() const {
   }
   case TypeKind::Void:
   case TypeKind::OpaqueFloat8:
+  case TypeKind::SveCount:
   case TypeKind::Atomic:
   case TypeKind::MemberPointer:
   case TypeKind::Pointer:
@@ -50,6 +51,8 @@ uint64_t llvm::abi::Type::getABISizeInBits() const {
 }
 
 bool llvm::abi::Type::isSVESizelessType() const {
+  if (getKind() == TypeKind::SveCount)
+    return true;
   if (getKind() == TypeKind::Vector) {
     const VectorType *VT = static_cast<const VectorType *>(this);
     return VT->isSVEType() && VT->isScalable();

diff  --git a/llvm/unittests/ABI/AArch64TargetInfoTest.cpp b/llvm/unittests/ABI/AArch64TargetInfoTest.cpp
index 69cd9eafd81efc..6fcfddd3c82839 100644
--- a/llvm/unittests/ABI/AArch64TargetInfoTest.cpp
+++ b/llvm/unittests/ABI/AArch64TargetInfoTest.cpp
@@ -69,7 +69,7 @@ class AArch64TargetInfoTest : public ::testing::Test {
   const ABIType *V1I128;
   const llvm::abi::VectorType *SVInt32;
   const llvm::abi::VectorType *SVBool;
-  const llvm::abi::VectorType *SVCount;
+  const llvm::abi::SveCountType *SVCount;
   const llvm::abi::VectorType *SVFloat64;
   const llvm::abi::VectorType *FixedSVInt8;
   const llvm::abi::VectorType *FixedSVInt32;
@@ -137,8 +137,7 @@ class AArch64TargetInfoTest : public ::testing::Test {
         SVBool(TB.getVectorType(Bool, llvm::ElementCount::getScalable(16),
                                 llvm::Align(2),
                                 llvm::abi::VectorKind::SVEPredicate)),
-        SVCount(TB.getScalablePredicateOrCountVectorType(
-            llvm::Align(2), llvm::abi::VectorKind::SVECount)),
+        SVCount(TB.getSveCountType(llvm::Align(2))),
         SVFloat64(TB.getVectorType(F64, llvm::ElementCount::getScalable(2),
                                    llvm::Align(16),
                                    llvm::abi::VectorKind::SVEData)),
@@ -1659,8 +1658,9 @@ TEST_F(AArch64TargetInfoTest, ClassifyPureScalableZeroLengthArray) {
 }
 
 // A Pure Scalable Type that does not fit in the remaining registers is
-// indirect and leaves those registers available. An unnamed argument is
-// indirect even when registers remain. More than 8 data vectors never fits.
+// indirect and leaves those registers available. An svcount_t argument uses a
+// predicate register. An unnamed argument is indirect even when registers
+// remain. More than 8 data vectors never fits.
 TEST_F(AArch64TargetInfoTest, ClassifyPureScalableRegisterBudget) {
   std::unique_ptr<TargetInfo> TI =
       createAArch64TargetInfo(TB, AArch64ABIOptions(AArch64ABIKind::AAPCS));
@@ -1692,7 +1692,7 @@ TEST_F(AArch64TargetInfoTest, ClassifyPureScalableRegisterBudget) {
     const ABIType *TwoPred =
         makeRecord({FieldInfo(FixedSVBool, 0), FieldInfo(FixedSVBool, 256)},
                    512, llvm::Align(2), /*UnadjustedAlign=*/llvm::Align(2));
-    const ABIType *Args[] = {SVBool, SVBool, SVBool, TwoPred, Pred};
+    const ABIType *Args[] = {SVCount, SVBool, SVBool, TwoPred, Pred};
     std::unique_ptr<FunctionInfo> FI =
         FunctionInfo::create(llvm::CallingConv::C, Void, Args);
     TI->computeInfo(*FI);

diff  --git a/llvm/unittests/ABI/IRTypeMapperTest.cpp b/llvm/unittests/ABI/IRTypeMapperTest.cpp
index 238bdf7b5b9fd4..f89e22b5a4bbdc 100644
--- a/llvm/unittests/ABI/IRTypeMapperTest.cpp
+++ b/llvm/unittests/ABI/IRTypeMapperTest.cpp
@@ -79,10 +79,8 @@ TEST_F(IRTypeMapperTest, SVEPredicateVectorMapsToScalableI1Vector) {
   EXPECT_TRUE(Vec->getElementType()->isIntegerTy(1));
 }
 
-TEST_F(IRTypeMapperTest, SVECountMapsToAArch64SVCount) {
-  const llvm::abi::VectorType *SVCount =
-      TB.getScalablePredicateOrCountVectorType(llvm::Align(2),
-                                               llvm::abi::VectorKind::SVECount);
+TEST_F(IRTypeMapperTest, SveCountMapsToAArch64SVCount) {
+  const llvm::abi::SveCountType *SVCount = TB.getSveCountType(llvm::Align(2));
 
   auto *TET = llvm::dyn_cast<llvm::TargetExtType>(Mapper.convertType(SVCount));
   ASSERT_NE(TET, nullptr);

diff  --git a/llvm/unittests/ABI/TypesTest.cpp b/llvm/unittests/ABI/TypesTest.cpp
index bd5902f3522e8d..13c790763a5149 100644
--- a/llvm/unittests/ABI/TypesTest.cpp
+++ b/llvm/unittests/ABI/TypesTest.cpp
@@ -332,17 +332,17 @@ TEST_F(ABITypesTest, SVEPredicateVector) {
   EXPECT_EQ(SVBool->getAlignment(), Align(2));
 }
 
-TEST_F(ABITypesTest, SVECount) {
-  const VectorType *SVCount =
-      TB.getScalablePredicateOrCountVectorType(Align(2), VectorKind::SVECount);
+TEST_F(ABITypesTest, SveCount) {
+  const llvm::abi::SveCountType *SVCount = TB.getSveCountType(Align(2));
 
-  EXPECT_TRUE(SVCount->isSVECount());
-  EXPECT_TRUE(SVCount->isSVEType());
-  EXPECT_TRUE(SVCount->isSVESizelessType());
-  EXPECT_FALSE(SVCount->isSVEPredicate());
-  EXPECT_TRUE(SVCount->isScalable());
+  EXPECT_TRUE(SVCount->isSveCount());
+  EXPECT_EQ(SVCount->getKind(), llvm::abi::TypeKind::SveCount);
+  EXPECT_TRUE(llvm::isa<llvm::abi::SveCountType>(SVCount));
+  EXPECT_FALSE(SVCount->isVector());
   EXPECT_FALSE(SVCount->isTuple());
+  EXPECT_TRUE(SVCount->isSVESizelessType());
   EXPECT_EQ(SVCount->getSizeInBits(), TypeSize::getScalable(16));
+  EXPECT_EQ(SVCount->getAlignment(), Align(2));
 }
 
 // Scalable vectors have no fixed size, so isZeroSize() must not query one.


        


More information about the cfe-commits mailing list