[clang] [CIR] Add support for __SVCount (PR #245456)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 17:00:07 PST 2026


https://github.com/andykaylor created https://github.com/llvm/llvm-project/pull/245456

This adds CIR codegen support for __SVCount and adds a new entry to the VectorAbiKind enum for it. Calling convention for this type is not yet implemented. That will be handled in a future change.

Assisted-by: Cursor / claude-opus-5.5

>From d7edf49211502da992d2055078e5b561489d923f Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Fri, 20 Nov 2026 15:59:01 -0800
Subject: [PATCH] [CIR] Add support for __SVCount

This adds CIR codegen support for __SVCount and adds a new entry to the
VectorAbiKind enum for it. Calling convention for this type is not yet
implemented. That will be handled in a future change.

Assisted-by: Cursor / claude-opus-5.5
---
 .../include/clang/CIR/Dialect/IR/CIRTypes.td  | 17 +++++++---
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp         |  5 +++
 clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp    |  7 +++--
 clang/lib/CIR/Dialect/IR/CIRTypes.cpp         | 13 +++++++-
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 23 +++++++++-----
 clang/test/CIR/CodeGen/sve-count.c            | 31 +++++++++++++++++++
 clang/test/CIR/IR/invalid-vector.cir          | 24 ++++++++++++--
 clang/test/CIR/IR/vector.cir                  |  2 ++
 .../unittests/CIR/VectorTypeABIAlignTest.cpp  |  9 ++++--
 9 files changed, 111 insertions(+), 20 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/sve-count.c

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index 6c63ea5efb79fb..16089a441115b9 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -503,7 +503,8 @@ def CIR_ArrayType : CIR_Type<"Array", "array", [
 def CIR_VectorAbiKind : CIR_I32Enum<"VectorAbiKind", "vector ABI kind", [
     I32EnumCase<"Generic", 0, "generic">,
     I32EnumCase<"SVEData", 1, "sve_data">,
-    I32EnumCase<"SVEPredicate", 2, "sve_predicate">
+    I32EnumCase<"SVEPredicate", 2, "sve_predicate">,
+    I32EnumCase<"SVECount", 3, "sve_count">
 ]>;
 
 def CIR_VectorType : CIR_Type<"Vector", "vector", [
@@ -524,7 +525,7 @@ def CIR_VectorType : CIR_Type<"Vector", "vector", [
     vector-type ::= !cir.vector<size x element-type (`,` kind)?>
     size ::= (decimal-literal | `[` decimal-literal `]`)
     element-type ::= float-type | integer-type | bool-type
-    kind ::= `sve_data` | `sve_predicate`
+    kind ::= `sve_data` | `sve_predicate` | `sve_count`
     ```
 
     The `element-type` must be a scalar CIR type. Zero-sized vectors are not
@@ -558,15 +559,19 @@ def CIR_VectorType : CIR_Type<"Vector", "vector", [
     - A scalable `sve_predicate` vector has `!cir.int<u, 1>` elements. A
       fixed-length `sve_predicate` vector holds the predicate one bit per lane
       in `!cir.int<u, 8>` elements.
+    - An `sve_count` vector is `__SVCount_t`, an opaque predicate-as-counter.
+      It has the shape of `svbool_t`, `[16]` `!cir.int<u, 1>` elements, and
+      lowers to the LLVM type `target("aarch64.svcount")`.
 
     A scalable vector must have a kind other than generic. The language
     alignment of an `sve_data` vector is 16 bytes and that of an
-    `sve_predicate` vector is 2 bytes, whatever their size.
+    `sve_predicate` or `sve_count` vector is 2 bytes, whatever their size.
 
     Examples:
     ```
     !cir.vector<[4] x !cir.int<s, 32>, sve_data>
     !cir.vector<[16] x !cir.int<u, 1>, sve_predicate>
+    !cir.vector<[16] x !cir.int<u, 1>, sve_count>
     !cir.vector<16 x !cir.int<s, 32>, sve_data>
     !cir.vector<8 x !cir.int<u, 8>, sve_predicate>
     ```
@@ -595,10 +600,12 @@ def CIR_VectorType : CIR_Type<"Vector", "vector", [
     /// Return whether this vector is scalable.
     bool isScalable() const { return getSize().isScalable(); }
 
-    /// Return whether this vector is an AArch64 SVE data or predicate vector.
+    /// Return whether this vector is an AArch64 SVE data, predicate, or count
+    /// vector.
     bool isSVE() const {
       return getKind() == VectorAbiKind::SVEData ||
-             getKind() == VectorAbiKind::SVEPredicate;
+             getKind() == VectorAbiKind::SVEPredicate ||
+             getKind() == VectorAbiKind::SVECount;
     }
 
     /// Return a fixed-width vector with the given element type, size, and
diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
index fa96c9b655c1c2..6c6b1259b72cac 100644
--- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
@@ -422,6 +422,11 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
         break;
       }
 
+    case BuiltinType::SveCount:
+      resultType = cir::VectorType::getScalable(builder.getUIntNTy(1), 16,
+                                                cir::VectorAbiKind::SVECount);
+      break;
+
     // Unsigned integral types.
     case BuiltinType::Char8:
     case BuiltinType::Char16:
diff --git a/clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp b/clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp
index 7a89faaf433abc..d4db9b7f14ceee 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp
@@ -75,11 +75,12 @@ uint64_t CIRDataLayout::getTypeLanguageAlignment(mlir::Type ty) const {
         return maxMemberAlignment(recTy.getMembers());
       })
       .Case([&](cir::VectorType vecTy) -> uint64_t {
-        // An SVE data vector is 16-byte aligned and an SVE predicate is
-        // 2-byte aligned, regardless of its length.
+        // An SVE data vector is 16-byte aligned and an SVE predicate or
+        // count is 2-byte aligned, regardless of its length.
         if (vecTy.getKind() == cir::VectorAbiKind::SVEData)
           return 16;
-        if (vecTy.getKind() == cir::VectorAbiKind::SVEPredicate)
+        if (vecTy.getKind() == cir::VectorAbiKind::SVEPredicate ||
+            vecTy.getKind() == cir::VectorAbiKind::SVECount)
           return 2;
         uint64_t align = layout.getTypeABIAlignment(vecTy);
         if (vectorMaxAlignBits)
diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
index e82d5f55cbd974..f096e97d32a71e 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -1704,6 +1704,16 @@ mlir::LogicalResult cir::VectorType::verify(
                          << " elements";
     return success();
   }
+  case cir::VectorAbiKind::SVECount: {
+    auto intTy = mlir::dyn_cast<cir::IntType>(elementType);
+    if (!size.isScalable() || size.getKnownMinValue() != 16 || !intTy ||
+        intTy.isSigned() || intTy.isBitInt() || intTy.getWidth() != 1)
+      return emitError() << "an SVE count vector must have [16] "
+                         << cir::IntType::get(elementType.getContext(), 1,
+                                              /*isSigned=*/false)
+                         << " elements";
+    return success();
+  }
   }
   llvm_unreachable("unknown vector kind");
 }
@@ -1764,7 +1774,8 @@ mlir::Type cir::VectorType::parse(::mlir::AsmParser &odsParser) {
     std::optional<cir::VectorAbiKind> parsedKind =
         cir::symbolizeVectorAbiKind(kindName);
     if (!parsedKind || *parsedKind == cir::VectorAbiKind::Generic) {
-      odsParser.emitError(kindLoc, "expected 'sve_data' or 'sve_predicate'");
+      odsParser.emitError(kindLoc, "expected 'sve_data', 'sve_predicate', or "
+                                   "'sve_count'");
       return {};
     }
     kind = *parsedKind;
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 8ca12695812802..0cb25b867e779b 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -102,14 +102,18 @@ static mlir::InFlightDiagnostic emitTypeConversionError(mlir::Location loc,
 /// Alignment to use for a memory access whose op carries no explicit alignment.
 /// For _BitInt the storage integer iM's ABI alignment (e.g. i128's 16)
 /// over-aligns the value, so use the _BitInt language alignment (e.g. 8).
+/// An SVE count lowers to target("aarch64.svcount"), which the MLIR data
+/// layout cannot size, so it also uses its language alignment.
 static uint64_t getMemoryFallbackAlignment(mlir::Operation *op,
                                            mlir::Type cirType,
                                            mlir::Type llvmMemType,
                                            const mlir::DataLayout &dataLayout) {
-  if (auto intTy = mlir::dyn_cast<cir::IntType>(cirType);
-      intTy && intTy.isBitInt())
+  auto intTy = mlir::dyn_cast<cir::IntType>(cirType);
+  auto vecTy = mlir::dyn_cast<cir::VectorType>(cirType);
+  if ((intTy && intTy.isBitInt()) ||
+      (vecTy && vecTy.getKind() == cir::VectorAbiKind::SVECount))
     return cir::CIRDataLayout(op->getParentOfType<mlir::ModuleOp>())
-        .getTypeLanguageAlignment(intTy);
+        .getTypeLanguageAlignment(cirType);
   return dataLayout.getTypeABIAlignment(llvmMemType);
 }
 
@@ -2793,8 +2797,9 @@ mlir::LogicalResult CIRToLLVMLoadOpLowering::matchAndRewrite(
     return emitTypeConversionError(op, op.getType());
   mlir::LLVM::AtomicOrdering ordering = getLLVMMemOrder(op.getMemOrder());
   std::optional<size_t> opAlign = op.getAlignment();
-  unsigned alignment = (unsigned)opAlign.value_or(
-      getMemoryFallbackAlignment(op, op.getType(), llvmTy, dataLayout));
+  unsigned alignment = opAlign ? *opAlign
+                               : getMemoryFallbackAlignment(op, op.getType(),
+                                                            llvmTy, dataLayout);
 
   assert(!cir::MissingFeatures::lowerModeOptLevel());
 
@@ -2851,8 +2856,9 @@ mlir::LogicalResult CIRToLLVMStoreOpLowering::matchAndRewrite(
     return op.emitError()
            << "NYI: lowering store of a type with no memory representation";
   std::optional<size_t> opAlign = op.getAlignment();
-  unsigned alignment = (unsigned)opAlign.value_or(
-      getMemoryFallbackAlignment(op, valueType, llvmTy, dataLayout));
+  unsigned alignment =
+      opAlign ? *opAlign
+              : getMemoryFallbackAlignment(op, valueType, llvmTy, dataLayout);
 
   assert(!cir::MissingFeatures::lowerModeOptLevel());
 
@@ -4606,6 +4612,9 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
     return mlir::LLVM::LLVMArrayType::get(ty, type.getSize());
   });
   converter.addConversion([&](cir::VectorType type) -> mlir::Type {
+    if (type.getKind() == cir::VectorAbiKind::SVECount)
+      return mlir::LLVM::LLVMTargetExtType::get(type.getContext(),
+                                                "aarch64.svcount", {}, {});
     // Vector-of-_BitInt memory layout is not modeled here (the element would
     // stay at its literal width); report a conversion failure rather than emit
     // an unvalidated vector.
diff --git a/clang/test/CIR/CodeGen/sve-count.c b/clang/test/CIR/CodeGen/sve-count.c
new file mode 100644
index 00000000000000..f88a21c7844135
--- /dev/null
+++ b/clang/test/CIR/CodeGen/sve-count.c
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sve \
+// RUN:   -target-feature +sme2 -fclangir -emit-cir -o %t.cir %s
+// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
+// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sve \
+// RUN:   -target-feature +sme2 -fclangir -emit-llvm -o %t-cir.ll %s
+// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
+// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sve \
+// RUN:   -target-feature +sme2 -emit-llvm -o %t.ll %s
+// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
+
+// REQUIRES: aarch64-registered-target
+
+// __SVCount_t is a scalable vector with the sve_count kind in CIR and lowers
+// to target("aarch64.svcount").
+
+void copy_count(void) {
+  __SVCount_t c;
+  __SVCount_t d = c;
+}
+
+// CIR-LABEL: cir.func {{.*}} @copy_count()
+// CIR:         %[[C:.*]] = cir.alloca "c" align(2) : !cir.ptr<!cir.vector<[16] x !cir.int<u, 1>, sve_count>>
+// CIR:         %[[D:.*]] = cir.alloca "d" align(2) init : !cir.ptr<!cir.vector<[16] x !cir.int<u, 1>, sve_count>>
+// CIR:         %[[VAL:.*]] = cir.load align(2) %[[C]] : !cir.ptr<!cir.vector<[16] x !cir.int<u, 1>, sve_count>>, !cir.vector<[16] x !cir.int<u, 1>, sve_count>
+// CIR:         cir.store align(2) %[[VAL]], %[[D]] : !cir.vector<[16] x !cir.int<u, 1>, sve_count>, !cir.ptr<!cir.vector<[16] x !cir.int<u, 1>, sve_count>>
+
+// LLVM-LABEL: define dso_local void @copy_count()
+// LLVM:         %[[C:.*]] = alloca target("aarch64.svcount"), align 2
+// LLVM:         %[[D:.*]] = alloca target("aarch64.svcount"), align 2
+// LLVM:         %[[VAL:.*]] = load target("aarch64.svcount"), ptr %[[C]], align 2
+// LLVM:         store target("aarch64.svcount") %[[VAL]], ptr %[[D]], align 2
diff --git a/clang/test/CIR/IR/invalid-vector.cir b/clang/test/CIR/IR/invalid-vector.cir
index f7606cfd0bcdad..bfc36e92a14029 100644
--- a/clang/test/CIR/IR/invalid-vector.cir
+++ b/clang/test/CIR/IR/invalid-vector.cir
@@ -208,14 +208,14 @@ cir.func private @scalable_generic(!cir.vector<[4] x !s32i>)
 
 !s32i = !cir.int<s, 32>
 
-// expected-error @below {{expected 'sve_data' or 'sve_predicate'}}
+// expected-error @below {{expected 'sve_data', 'sve_predicate', or 'sve_count'}}
 cir.func private @kind_generic(!cir.vector<4 x !s32i, generic>)
 
 // -----
 
 !s32i = !cir.int<s, 32>
 
-// expected-error @below {{expected 'sve_data' or 'sve_predicate'}}
+// expected-error @below {{expected 'sve_data', 'sve_predicate', or 'sve_count'}}
 cir.func private @kind_unknown(!cir.vector<4 x !s32i, neon>)
 
 // -----
@@ -286,3 +286,23 @@ module {
     cir.return
   }
 }
+
+// -----
+
+// expected-error @below {{an SVE count vector must have [16] '!cir.int<u, 1>' elements}}
+cir.func private @sve_count_fixed(!cir.vector<16 x !cir.int<u, 1>, sve_count>)
+
+// -----
+
+// expected-error @below {{an SVE count vector must have [16] '!cir.int<u, 1>' elements}}
+cir.func private @sve_count_8(!cir.vector<[8] x !cir.int<u, 1>, sve_count>)
+
+// -----
+
+// expected-error @below {{an SVE count vector must have [16] '!cir.int<u, 1>' elements}}
+cir.func private @sve_count_s1(!cir.vector<[16] x !cir.int<s, 1>, sve_count>)
+
+// -----
+
+// expected-error @below {{an SVE count vector must have [16] '!cir.int<u, 1>' elements}}
+cir.func private @sve_count_u8(!cir.vector<[16] x !cir.int<u, 8>, sve_count>)
diff --git a/clang/test/CIR/IR/vector.cir b/clang/test/CIR/IR/vector.cir
index 64d371d9b755c1..cea6d96f92bf87 100644
--- a/clang/test/CIR/IR/vector.cir
+++ b/clang/test/CIR/IR/vector.cir
@@ -268,6 +268,7 @@ cir.func @vector_reduce_test(%svec: !cir.vector<4 x !s32i>,
 cir.func @vec_sve_kinds(
     %data: !cir.vector<[4] x !s32i, sve_data>,
     %pred: !cir.vector<[16] x !cir.int<u, 1>, sve_predicate>,
+    %count: !cir.vector<[16] x !cir.int<u, 1>, sve_count>,
     %fixed_data: !cir.vector<16 x !s32i, sve_data>,
     %fixed_float: !cir.vector<8 x !cir.double, sve_data>,
     %fixed_pred: !cir.vector<8 x !cir.int<u, 8>, sve_predicate>,
@@ -278,6 +279,7 @@ cir.func @vec_sve_kinds(
 // CHECK: cir.func{{.*}} @vec_sve_kinds(
 // CHECK-SAME: %{{.*}}: !cir.vector<[4] x !s32i, sve_data>,
 // CHECK-SAME: %{{.*}}: !cir.vector<[16] x !cir.int<u, 1>, sve_predicate>,
+// CHECK-SAME: %{{.*}}: !cir.vector<[16] x !cir.int<u, 1>, sve_count>,
 // CHECK-SAME: %{{.*}}: !cir.vector<16 x !s32i, sve_data>,
 // CHECK-SAME: %{{.*}}: !cir.vector<8 x !cir.double, sve_data>,
 // CHECK-SAME: %{{.*}}: !cir.vector<8 x !u8i, sve_predicate>,
diff --git a/clang/unittests/CIR/VectorTypeABIAlignTest.cpp b/clang/unittests/CIR/VectorTypeABIAlignTest.cpp
index 935b6e321f898b..cdb6125e5eb8bb 100644
--- a/clang/unittests/CIR/VectorTypeABIAlignTest.cpp
+++ b/clang/unittests/CIR/VectorTypeABIAlignTest.cpp
@@ -166,8 +166,8 @@ TEST_F(VectorTypeABIAlignTest, MaxAlignLeavesABIAlignment) {
 }
 
 // The language alignment of an SVE vector is 16 bytes for data and 2 bytes for
-// a predicate, whether it is scalable or fixed-length. The ABI alignment
-// follows the vector's size, as for a generic vector.
+// a predicate or count, whether it is scalable or fixed-length. The ABI
+// alignment follows the vector's size, as for a generic vector.
 TEST_F(VectorTypeABIAlignTest, SVEVectorLanguageAlignment) {
   OpBuilder builder(&context);
   auto module = ModuleOp::create(builder.getUnknownLoc());
@@ -184,6 +184,11 @@ TEST_F(VectorTypeABIAlignTest, SVEVectorLanguageAlignment) {
   EXPECT_EQ(dl.layout.getTypeABIAlignment(svbool), 2u);
   EXPECT_EQ(dl.getTypeLanguageAlignment(svbool), 2u);
 
+  auto svcount =
+      cir::VectorType::getScalable(u1, 16, cir::VectorAbiKind::SVECount);
+  EXPECT_EQ(dl.layout.getTypeABIAlignment(svcount), 2u);
+  EXPECT_EQ(dl.getTypeLanguageAlignment(svcount), 2u);
+
   // A 512-bit fixed_float32_t.
   auto fixedFloat32 =
       cir::VectorType::getFixed(f32(), 16, cir::VectorAbiKind::SVEData);



More information about the cfe-commits mailing list