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

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 10:46:49 PST 2026


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

>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 1/5] [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);

>From 5e0c78e3affa2ad46606b0fcac4f266af84e2623 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Tue, 24 Nov 2026 12:59:33 -0800
Subject: [PATCH 2/5] Make SveCount a non-vector type

---
 .../CIR/Dialect/IR/CIRDialectBytecode.td      |  2 +
 .../include/clang/CIR/Dialect/IR/CIRTypes.td  | 42 ++++++++++++-------
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp         |  3 +-
 clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp    |  7 ++--
 clang/lib/CIR/Dialect/IR/CIRTypes.cpp         | 29 +++++++------
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 11 +++--
 clang/test/CIR/Bytecode/types.cir             |  5 ++-
 clang/test/CIR/CodeGen/sve-count.c            | 11 ++---
 clang/test/CIR/IR/invalid-vector.cir          | 24 +----------
 clang/test/CIR/IR/vector.cir                  |  2 -
 .../unittests/CIR/VectorTypeABIAlignTest.cpp  | 26 ++++++++----
 11 files changed, 83 insertions(+), 79 deletions(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialectBytecode.td b/clang/include/clang/CIR/Dialect/IR/CIRDialectBytecode.td
index 31ccff87f8004b..bbfb8746d430d7 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDialectBytecode.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDialectBytecode.td
@@ -378,6 +378,7 @@ def CUDADeviceTextureType : DialectType<(type)>;
 def EhTokenType : DialectType<(type)>;
 def CleanupTokenType : DialectType<(type)>;
 def CatchTokenType : DialectType<(type)>;
+def SveCountType : DialectType<(type)>;
 
 def CIRDialectAttributes : DialectAttributes<"CIR"> {
   let elems = [
@@ -444,6 +445,7 @@ def CIRDialectTypes : DialectTypes<"CIR"> {
     EhTokenType,
     CleanupTokenType,
     CatchTokenType,
+    SveCountType,
     // APPEND ONLY, as for the attributes. Types not listed here, including
     // the mutable struct and union types, keep the assembly-format fallback.
   ];
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index 16089a441115b9..1146cb91322e49 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -503,8 +503,7 @@ 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<"SVECount", 3, "sve_count">
+    I32EnumCase<"SVEPredicate", 2, "sve_predicate">
 ]>;
 
 def CIR_VectorType : CIR_Type<"Vector", "vector", [
@@ -525,7 +524,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` | `sve_count`
+    kind ::= `sve_data` | `sve_predicate`
     ```
 
     The `element-type` must be a scalar CIR type. Zero-sized vectors are not
@@ -559,19 +558,15 @@ 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` or `sve_count` vector is 2 bytes, whatever their size.
+    `sve_predicate` 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>
     ```
@@ -600,12 +595,10 @@ 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, predicate, or count
-    /// vector.
+    /// Return whether this vector is an AArch64 SVE data or predicate vector.
     bool isSVE() const {
       return getKind() == VectorAbiKind::SVEData ||
-             getKind() == VectorAbiKind::SVEPredicate ||
-             getKind() == VectorAbiKind::SVECount;
+             getKind() == VectorAbiKind::SVEPredicate;
     }
 
     /// Return a fixed-width vector with the given element type, size, and
@@ -651,6 +644,25 @@ def CIR_VectorType : CIR_Type<"Vector", "vector", [
   let hasCustomAssemblyFormat = 1;
 }
 
+//===----------------------------------------------------------------------===//
+// SveCountType
+//===----------------------------------------------------------------------===//
+
+def CIR_SveCountType : CIR_Type<"SveCount", "sve_count", [
+    DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
+    DeclareTypeInterfaceMethods<CIR_SizedTypeInterface>
+]> {
+  let summary = "AArch64 SVE predicate-as-counter type";
+  let description = [{
+    `!cir.sve_count` represents the AArch64 `__SVCount_t` (`svcount_t`) 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. It lowers to the LLVM type `target("aarch64.svcount")`.
+
+    The size of the type is a scalable 16 bits, and its alignment is 2 bytes.
+  }];
+}
+
 //===----------------------------------------------------------------------===//
 // MatrixType
 //===----------------------------------------------------------------------===//
@@ -1354,9 +1366,9 @@ def CIR_CatchTokenType : CIR_Type<"CatchToken", "catch_token"> {
 //===----------------------------------------------------------------------===//
 
 def CIR_AnyType : AnyTypeOf<[
-  CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_VectorType, CIR_MatrixType,
-  CIR_IntType, CIR_AnyFloatType, CIR_PointerType, CIR_FuncType, CIR_StructType,
-  CIR_UnionType, CIR_BitFieldType,
+  CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_VectorType, CIR_SveCountType,
+  CIR_MatrixType, CIR_IntType, CIR_AnyFloatType, CIR_PointerType, CIR_FuncType,
+  CIR_StructType, CIR_UnionType, CIR_BitFieldType,
   CIR_ComplexType, CIR_VPtrType, CIR_CUDADeviceSurfaceType,
   CIR_CUDADeviceTextureType,
   CIR_DataMemberType, CIR_MethodType,
diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
index 6c6b1259b72cac..df39ec52528493 100644
--- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
@@ -423,8 +423,7 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
       }
 
     case BuiltinType::SveCount:
-      resultType = cir::VectorType::getScalable(builder.getUIntNTy(1), 16,
-                                                cir::VectorAbiKind::SVECount);
+      resultType = cir::SveCountType::get(&getMLIRContext());
       break;
 
     // Unsigned integral types.
diff --git a/clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp b/clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp
index d4db9b7f14ceee..7a89faaf433abc 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp
@@ -75,12 +75,11 @@ 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 or
-        // count is 2-byte aligned, regardless of its length.
+        // An SVE data vector is 16-byte aligned and an SVE predicate is
+        // 2-byte aligned, regardless of its length.
         if (vecTy.getKind() == cir::VectorAbiKind::SVEData)
           return 16;
-        if (vecTy.getKind() == cir::VectorAbiKind::SVEPredicate ||
-            vecTy.getKind() == cir::VectorAbiKind::SVECount)
+        if (vecTy.getKind() == cir::VectorAbiKind::SVEPredicate)
           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 f096e97d32a71e..f130148a7fdd7b 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -1704,16 +1704,6 @@ 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");
 }
@@ -1774,8 +1764,7 @@ 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', 'sve_predicate', or "
-                                   "'sve_count'");
+      odsParser.emitError(kindLoc, "expected 'sve_data' or 'sve_predicate'");
       return {};
     }
     kind = *parsedKind;
@@ -1806,6 +1795,22 @@ void cir::VectorType::print(mlir::AsmPrinter &odsPrinter) const {
   odsPrinter << ">";
 }
 
+//===----------------------------------------------------------------------===//
+// SveCountType Definitions
+//===----------------------------------------------------------------------===//
+
+llvm::TypeSize
+SveCountType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
+                                mlir::DataLayoutEntryListRef params) const {
+  return llvm::TypeSize::getScalable(16);
+}
+
+uint64_t
+SveCountType::getABIAlignment(const mlir::DataLayout &dataLayout,
+                              mlir::DataLayoutEntryListRef params) const {
+  return 2;
+}
+
 //===----------------------------------------------------------------------===//
 // MatrixType Definitions
 //===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 0cb25b867e779b..40885695344311 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -109,9 +109,7 @@ static uint64_t getMemoryFallbackAlignment(mlir::Operation *op,
                                            mlir::Type llvmMemType,
                                            const mlir::DataLayout &dataLayout) {
   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))
+  if ((intTy && intTy.isBitInt()) || mlir::isa<cir::SveCountType>(cirType))
     return cir::CIRDataLayout(op->getParentOfType<mlir::ModuleOp>())
         .getTypeLanguageAlignment(cirType);
   return dataLayout.getTypeABIAlignment(llvmMemType);
@@ -4611,10 +4609,11 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
       return {};
     return mlir::LLVM::LLVMArrayType::get(ty, type.getSize());
   });
+  converter.addConversion([&](cir::SveCountType type) -> mlir::Type {
+    return mlir::LLVM::LLVMTargetExtType::get(type.getContext(),
+                                              "aarch64.svcount", {}, {});
+  });
   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/Bytecode/types.cir b/clang/test/CIR/Bytecode/types.cir
index 9747f8873ae7ae..86c9d3904bfd28 100644
--- a/clang/test/CIR/Bytecode/types.cir
+++ b/clang/test/CIR/Bytecode/types.cir
@@ -71,7 +71,8 @@ module {
       !cir.method<!cir.func<()> in !rec_S>)
 
   cir.func private @opaque(!cir.cuda_surface, !cir.cuda_texture, !cir.eh_token,
-                           !cir.cleanup_token, !cir.catch_token)
+                           !cir.cleanup_token, !cir.catch_token,
+                           !cir.sve_count)
 }
 
 // CHECK: cir.func private @ints(!u8i, !s32i, !s64i, !cir.int<s, 13, bitint>, !u128i_bitint)
@@ -85,4 +86,4 @@ module {
 // CHECK: cir.func private @matrices(!cir.matrix<2 x 3 x !cir.float>, !cir.matrix<3 x 2 x !s32i>)
 // CHECK: cir.func private @bitfields(!cir.bitfield<!u32i, [#cir.bitfield_decl<!s32i, 4>, #cir.bitfield_decl<!s64i, 20>, #cir.bitfield_decl<!u32i, 3, unnamed>]>, !cir.bitfield<[#cir.bitfield_decl<!s32i, 0, unnamed>]>, !cir.bitfield<!cir.array<!u8i x 3>, [#cir.bitfield_decl<!cir.bool, 1>]>)
 // CHECK: cir.func private @member_ptrs(!cir.data_member<!s32i in !rec_S>, !cir.method<!cir.func<(!s32i) -> !s32i> in !rec_S>, !cir.method<!cir.func<()> in !rec_S>)
-// CHECK: cir.func private @opaque(!cir.cuda_surface, !cir.cuda_texture, !cir.eh_token, !cir.cleanup_token, !cir.catch_token)
+// CHECK: cir.func private @opaque(!cir.cuda_surface, !cir.cuda_texture, !cir.eh_token, !cir.cleanup_token, !cir.catch_token, !cir.sve_count)
diff --git a/clang/test/CIR/CodeGen/sve-count.c b/clang/test/CIR/CodeGen/sve-count.c
index f88a21c7844135..f6f97b46515b4e 100644
--- a/clang/test/CIR/CodeGen/sve-count.c
+++ b/clang/test/CIR/CodeGen/sve-count.c
@@ -10,19 +10,16 @@
 
 // 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>>
+// CIR:         %[[C:.*]] = cir.alloca "c" align(2) : !cir.ptr<!cir.sve_count>
+// CIR:         %[[D:.*]] = cir.alloca "d" align(2) init : !cir.ptr<!cir.sve_count>
+// CIR:         %[[VAL:.*]] = cir.load align(2) %[[C]] : !cir.ptr<!cir.sve_count>, !cir.sve_count
+// CIR:         cir.store align(2) %[[VAL]], %[[D]] : !cir.sve_count, !cir.ptr<!cir.sve_count>
 
 // LLVM-LABEL: define dso_local void @copy_count()
 // LLVM:         %[[C:.*]] = alloca target("aarch64.svcount"), align 2
diff --git a/clang/test/CIR/IR/invalid-vector.cir b/clang/test/CIR/IR/invalid-vector.cir
index bfc36e92a14029..f7606cfd0bcdad 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', 'sve_predicate', or 'sve_count'}}
+// expected-error @below {{expected 'sve_data' or 'sve_predicate'}}
 cir.func private @kind_generic(!cir.vector<4 x !s32i, generic>)
 
 // -----
 
 !s32i = !cir.int<s, 32>
 
-// expected-error @below {{expected 'sve_data', 'sve_predicate', or 'sve_count'}}
+// expected-error @below {{expected 'sve_data' or 'sve_predicate'}}
 cir.func private @kind_unknown(!cir.vector<4 x !s32i, neon>)
 
 // -----
@@ -286,23 +286,3 @@ 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 cea6d96f92bf87..64d371d9b755c1 100644
--- a/clang/test/CIR/IR/vector.cir
+++ b/clang/test/CIR/IR/vector.cir
@@ -268,7 +268,6 @@ 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>,
@@ -279,7 +278,6 @@ 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 cdb6125e5eb8bb..9b63516895d9ca 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 or count, whether it is scalable or fixed-length. The ABI
-// alignment follows the vector's size, as for a generic vector.
+// a predicate, 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,11 +184,6 @@ 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);
@@ -204,4 +199,21 @@ TEST_F(VectorTypeABIAlignTest, SVEVectorLanguageAlignment) {
   module->erase();
 }
 
+// An SVE count has the size of an svbool_t, a scalable 16 bits, and is 2-byte
+// aligned.
+TEST_F(VectorTypeABIAlignTest, SveCountLayout) {
+  OpBuilder builder(&context);
+  auto module = ModuleOp::create(builder.getUnknownLoc());
+  cir::CIRDataLayout dl(module);
+
+  auto svcount = cir::SveCountType::get(&context);
+  EXPECT_TRUE(cir::isSized(svcount));
+  EXPECT_EQ(dl.layout.getTypeSizeInBits(svcount),
+            llvm::TypeSize::getScalable(16));
+  EXPECT_EQ(dl.layout.getTypeABIAlignment(svcount), 2u);
+  EXPECT_EQ(dl.getTypeLanguageAlignment(svcount), 2u);
+
+  module->erase();
+}
+
 } // namespace

>From b00418424d20b7c85ab3cb03268fe5ecfe6d8ff3 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Wed, 25 Nov 2026 10:28:36 -0800
Subject: [PATCH 3/5] Address review feedback

---
 clang/include/clang/CIR/Dialect/IR/CIRTypes.td      |  2 --
 clang/lib/CIR/Dialect/IR/CIRTypes.cpp               |  4 ++++
 clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 10 ++++------
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index 1146cb91322e49..41b9f95ed6d778 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -658,8 +658,6 @@ def CIR_SveCountType : CIR_Type<"SveCount", "sve_count", [
     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. It lowers to the LLVM type `target("aarch64.svcount")`.
-
-    The size of the type is a scalable 16 bits, and its alignment is 2 bytes.
   }];
 }
 
diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
index f130148a7fdd7b..d962372415aefc 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -1802,12 +1802,16 @@ void cir::VectorType::print(mlir::AsmPrinter &odsPrinter) const {
 llvm::TypeSize
 SveCountType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
                                 mlir::DataLayoutEntryListRef params) const {
+  // The size of a predicate in bits is `VG x 8` where `VG` is `vscale x 2`.
+  // See https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#fundamental-data-types
   return llvm::TypeSize::getScalable(16);
 }
 
 uint64_t
 SveCountType::getABIAlignment(const mlir::DataLayout &dataLayout,
                               mlir::DataLayoutEntryListRef params) const {
+  // Scalable predicates are 2-byte aligned.
+  // See https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#fundamental-data-types
   return 2;
 }
 
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 40885695344311..0342e022cc4971 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -2795,9 +2795,8 @@ 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 = opAlign ? *opAlign
-                               : getMemoryFallbackAlignment(op, op.getType(),
-                                                            llvmTy, dataLayout);
+  unsigned alignment = (unsigned)opAlign.value_or(
+      getMemoryFallbackAlignment(op, op.getType(), llvmTy, dataLayout));
 
   assert(!cir::MissingFeatures::lowerModeOptLevel());
 
@@ -2854,9 +2853,8 @@ 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 =
-      opAlign ? *opAlign
-              : getMemoryFallbackAlignment(op, valueType, llvmTy, dataLayout);
+  unsigned alignment = (unsigned)opAlign.value_or(
+      getMemoryFallbackAlignment(op, valueType, llvmTy, dataLayout));
 
   assert(!cir::MissingFeatures::lowerModeOptLevel());
 

>From 2cccc71bd2afab2e94a6560a06cf916759c76cd6 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Wed, 25 Nov 2026 10:44:04 -0800
Subject: [PATCH 4/5] Introduce isBitIntType() helper

---
 clang/include/clang/CIR/Dialect/IR/CIRTypes.h |  3 +++
 clang/lib/CIR/Dialect/IR/CIRTypes.cpp         |  5 +++++
 .../Dialect/Transforms/CIRABIClassifier.cpp   |  3 +--
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 19 ++++++++-----------
 clang/lib/CIR/Lowering/LoweringHelpers.cpp    |  4 ++--
 5 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
index ded2921c608c6f..ba69e3945132ec 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
@@ -75,6 +75,9 @@ inline bool isNamedBitField(RecordMemberKind kind) {
 /// void, or abstract types.
 bool isSized(mlir::Type ty);
 
+/// Returns true if the type is a `cir::IntType` representing a _BitInt.
+bool isBitIntType(mlir::Type ty);
+
 /// Returns the CIR floating-point type for the given semantics, or a null
 /// type if CIR has no type for it (e.g. PPCDoubleDouble or a Float8 format).
 /// Mirrors llvm::Type::getFloatingPointTy.
diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
index d962372415aefc..ec60e40820909d 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -40,6 +40,11 @@ bool cir::isSized(mlir::Type ty) {
   return false;
 }
 
+bool cir::isBitIntType(mlir::Type ty) {
+  auto intTy = mlir::dyn_cast_if_present<cir::IntType>(ty);
+  return intTy && intTy.isBitInt();
+}
+
 cir::FPTypeInterface cir::getFloatingPointType(const llvm::fltSemantics &sem,
                                                mlir::MLIRContext *ctx) {
   switch (llvm::APFloat::SemanticsToEnum(sem)) {
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
index 36849f803de8e9..6256539456b79e 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
@@ -218,8 +218,7 @@ static bool isSupportedType(mlir::Type ty, const DataLayout &dl) {
             if (!bfTy || dl.getTypeSizeInBits(bfTy).getFixedValue() <= 64)
               return false;
             return llvm::any_of(bfTy.getFields(), [](cir::BitFieldDeclAttr d) {
-              auto intTy = dyn_cast<cir::IntType>(d.getDeclaredType());
-              return intTy && intTy.getIsBitInt();
+              return cir::isBitIntType(d.getDeclaredType());
             });
           }))
         return false;
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 0342e022cc4971..06cc1ef3faf6fd 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -108,8 +108,7 @@ static uint64_t getMemoryFallbackAlignment(mlir::Operation *op,
                                            mlir::Type cirType,
                                            mlir::Type llvmMemType,
                                            const mlir::DataLayout &dataLayout) {
-  auto intTy = mlir::dyn_cast<cir::IntType>(cirType);
-  if ((intTy && intTy.isBitInt()) || mlir::isa<cir::SveCountType>(cirType))
+  if (cir::isBitIntType(cirType) || mlir::isa<cir::SveCountType>(cirType))
     return cir::CIRDataLayout(op->getParentOfType<mlir::ModuleOp>())
         .getTypeLanguageAlignment(cirType);
   return dataLayout.getTypeABIAlignment(llvmMemType);
@@ -215,7 +214,7 @@ static mlir::Value emitFromMemory(mlir::ConversionPatternRewriter &rewriter,
 
   // Truncate the padded storage integer back to the _BitInt's literal width.
   if (auto intTy = mlir::dyn_cast<cir::IntType>(op.getType());
-      intTy && intTy.isBitInt())
+      cir::isBitIntType(intTy))
     return castBitIntMemoryStorage(rewriter, dataLayout, intTy, value,
                                    /*toMemory=*/false);
 
@@ -261,7 +260,7 @@ static mlir::Value emitToMemory(mlir::ConversionPatternRewriter &rewriter,
 
   // Sign/zero-extend the _BitInt value to its padded storage integer.
   if (auto intTy = mlir::dyn_cast<cir::IntType>(origType);
-      intTy && intTy.isBitInt())
+      cir::isBitIntType(intTy))
     return castBitIntMemoryStorage(rewriter, dataLayout, intTy, value,
                                    /*toMemory=*/true);
 
@@ -827,9 +826,9 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::BoolAttr boolAttr) {
 mlir::Value CIRAttrToValue::visitCirAttr(cir::IntAttr intAttr) {
   mlir::DataLayout layout(parentModule);
 
-  if (auto intTy = mlir::dyn_cast<cir::IntType>(intAttr.getType());
-      intTy && intTy.isBitInt()) {
-    mlir::Type biTy = convertTypeForMemory(*converter, layout, intTy);
+  mlir::Type type = intAttr.getType();
+  if (cir::isBitIntType(type)) {
+    mlir::Type biTy = convertTypeForMemory(*converter, layout, type);
     return mlir::LLVM::ConstantOp::create(
         rewriter, parentLoc, biTy,
         getBitIntStorageAttr(rewriter, intAttr, layout));
@@ -1382,8 +1381,7 @@ class GlobalInitAttrRewriter {
     // A split-storage _BitInt's memory representation is a byte array (see
     // convertTypeForMemory), not a scalar, so its constant must be built as
     // raw bytes rather than a single sign/zero-extended IntegerAttr.
-    if (auto intTy = mlir::dyn_cast<cir::IntType>(attr.getType());
-        intTy && intTy.isBitInt())
+    if (cir::isBitIntType(attr.getType()))
       return getBitIntStorageAttr(rewriter, attr, dataLayout);
 
     return rewriter.getIntegerAttr(llvmType, attr.getValue());
@@ -4615,8 +4613,7 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
     // 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.
-    if (auto intTy = mlir::dyn_cast<cir::IntType>(type.getElementType());
-        intTy && intTy.isBitInt())
+    if (cir::isBitIntType(type.getElementType()))
       return {};
     const mlir::Type ty = converter.convertType(type.getElementType());
     if (!ty)
diff --git a/clang/lib/CIR/Lowering/LoweringHelpers.cpp b/clang/lib/CIR/Lowering/LoweringHelpers.cpp
index 249f1b84bb6965..2e0fefd785eefa 100644
--- a/clang/lib/CIR/Lowering/LoweringHelpers.cpp
+++ b/clang/lib/CIR/Lowering/LoweringHelpers.cpp
@@ -112,7 +112,7 @@ mlir::Type convertTypeForMemory(const mlir::TypeConverter &converter,
   // M/8 byte count (isSplitStorageBitInt), use a byte array instead so the
   // size stays exact.
   if (auto intTy = mlir::dyn_cast<cir::IntType>(type);
-      intTy && intTy.isBitInt()) {
+      cir::isBitIntType(intTy)) {
     if (isSplitStorageBitInt(intTy, dataLayout))
       return mlir::LLVM::LLVMArrayType::get(
           mlir::IntegerType::get(type.getContext(), 8),
@@ -131,7 +131,7 @@ mlir::Type convertTypeForLoadStore(const mlir::TypeConverter &converter,
   // convertTypeForMemory), but a load/store must still access the whole
   // integer as-is.
   if (auto intTy = mlir::dyn_cast<cir::IntType>(type);
-      intTy && intTy.isBitInt())
+      cir::isBitIntType(intTy))
     return mlir::IntegerType::get(type.getContext(),
                                   intTy.getStorageTypeWidth(dataLayout));
 

>From d076680027e42a537828a2ae244b0b8430b6f7a2 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Wed, 25 Nov 2026 10:46:22 -0800
Subject: [PATCH 5/5] Fix formatting

---
 clang/lib/CIR/Dialect/IR/CIRTypes.cpp      | 6 ++++--
 clang/lib/CIR/Lowering/LoweringHelpers.cpp | 3 +--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
index ec60e40820909d..21f0d17e65b0da 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -1808,7 +1808,8 @@ llvm::TypeSize
 SveCountType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
                                 mlir::DataLayoutEntryListRef params) const {
   // The size of a predicate in bits is `VG x 8` where `VG` is `vscale x 2`.
-  // See https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#fundamental-data-types
+  // See
+  // https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#fundamental-data-types
   return llvm::TypeSize::getScalable(16);
 }
 
@@ -1816,7 +1817,8 @@ uint64_t
 SveCountType::getABIAlignment(const mlir::DataLayout &dataLayout,
                               mlir::DataLayoutEntryListRef params) const {
   // Scalable predicates are 2-byte aligned.
-  // See https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#fundamental-data-types
+  // See
+  // https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#fundamental-data-types
   return 2;
 }
 
diff --git a/clang/lib/CIR/Lowering/LoweringHelpers.cpp b/clang/lib/CIR/Lowering/LoweringHelpers.cpp
index 2e0fefd785eefa..a2265ecae117b0 100644
--- a/clang/lib/CIR/Lowering/LoweringHelpers.cpp
+++ b/clang/lib/CIR/Lowering/LoweringHelpers.cpp
@@ -130,8 +130,7 @@ mlir::Type convertTypeForLoadStore(const mlir::TypeConverter &converter,
   // A split-storage _BitInt's memory type is a byte array (see
   // convertTypeForMemory), but a load/store must still access the whole
   // integer as-is.
-  if (auto intTy = mlir::dyn_cast<cir::IntType>(type);
-      cir::isBitIntType(intTy))
+  if (auto intTy = mlir::dyn_cast<cir::IntType>(type); cir::isBitIntType(intTy))
     return mlir::IntegerType::get(type.getContext(),
                                   intTy.getStorageTypeWidth(dataLayout));
 



More information about the cfe-commits mailing list