[clang] [Clang][RISCV] Allow riscv_rvv_vector_bits typedefs shorter than VLEN*LMUL (PR #237846)

Kito Cheng via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 15 22:45:25 PST 2026


https://github.com/kito-cheng updated https://github.com/llvm/llvm-project/pull/237846

>From 4bcd25547374f73a1b042f2454f941435f3f5feb Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Tue, 13 Oct 2026 12:38:30 +0800
Subject: [PATCH 1/9] [Clang][RISCV] Record the LMUL in RVV fixed-length data
 vector kinds

A riscv_rvv_vector_bits typedef of a data type keeps only the element
type and count, and later stages re-derive the LMUL from VLEN. That
breaks once typedefs may be shorter than VLEN * LMUL.

Split VectorKind::RVVFixedLengthData into RVVFixedLengthData_MF8 to _M8
and read the LMUL from the kind. VecKind grows to 5 bits.

This patch prepares to let a riscv_rvv_vector_bits typedef be shorter
than VLEN * LMUL. It only records the LMUL and does not change any
behavior.

Assisted-by: Claude
---
 clang/include/clang/AST/TypeBase.h            | 95 ++++++++++++++++++-
 clang/lib/AST/ASTContext.cpp                  |  8 +-
 clang/lib/AST/ItaniumMangle.cpp               | 32 +++----
 clang/lib/AST/JSONNodeDumper.cpp              | 14 ++-
 clang/lib/AST/TextNodeDumper.cpp              | 11 ++-
 clang/lib/AST/TypePrinter.cpp                 | 16 +++-
 clang/lib/CodeGen/Targets/RISCV.cpp           | 79 +++++++++++----
 clang/lib/Sema/SemaExpr.cpp                   |  4 +-
 clang/lib/Sema/SemaRISCV.cpp                  |  2 +-
 clang/lib/Sema/SemaType.cpp                   | 10 +-
 ...t-dump-riscv-rvv-fixed-length-data-types.c | 40 ++++++++
 11 files changed, 258 insertions(+), 53 deletions(-)
 create mode 100644 clang/test/AST/ast-dump-riscv-rvv-fixed-length-data-types.c

diff --git a/clang/include/clang/AST/TypeBase.h b/clang/include/clang/AST/TypeBase.h
index 6373794bb69f54..09b381b512d0d4 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -2133,7 +2133,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
     /// The kind of vector, either a generic vector type or some
     /// target-specific vector type such as for AltiVec or Neon.
     LLVM_PREFERRED_TYPE(VectorKind)
-    unsigned VecKind : 4;
+    unsigned VecKind : 5;
     /// The number of elements in the vector.
     uint32_t NumElements;
   };
@@ -4255,7 +4255,13 @@ enum class VectorKind {
   SveFixedLengthPredicate,
 
   /// is RISC-V RVV fixed-length data vector
-  RVVFixedLengthData,
+  RVVFixedLengthData_MF8,
+  RVVFixedLengthData_MF4,
+  RVVFixedLengthData_MF2,
+  RVVFixedLengthData_M1,
+  RVVFixedLengthData_M2,
+  RVVFixedLengthData_M4,
+  RVVFixedLengthData_M8,
 
   /// is RISC-V RVV fixed-length mask vector
   RVVFixedLengthMask,
@@ -4265,6 +4271,91 @@ enum class VectorKind {
   RVVFixedLengthMask_4
 };
 
+/// True if \p VK is one of the RVV fixed-length data vector kinds, i.e. a
+/// riscv_rvv_vector_bits typedef of a data (non-mask) RVV type.
+inline bool isRVVFixedLengthDataVectorKind(VectorKind VK) {
+  switch (VK) {
+  case VectorKind::RVVFixedLengthData_MF8:
+  case VectorKind::RVVFixedLengthData_MF4:
+  case VectorKind::RVVFixedLengthData_MF2:
+  case VectorKind::RVVFixedLengthData_M1:
+  case VectorKind::RVVFixedLengthData_M2:
+  case VectorKind::RVVFixedLengthData_M4:
+  case VectorKind::RVVFixedLengthData_M8:
+    return true;
+  default:
+    return false;
+  }
+}
+
+/// Return log2 of the LMUL recorded in an RVV fixed-length data vector kind:
+/// MF8 -> -3, MF4 -> -2, MF2 -> -1, M1 -> 0, M2 -> 1, M4 -> 2, M8 -> 3.
+inline int getRVVFixedLengthDataLMulLog2(VectorKind VK) {
+  switch (VK) {
+  case VectorKind::RVVFixedLengthData_MF8:
+    return -3;
+  case VectorKind::RVVFixedLengthData_MF4:
+    return -2;
+  case VectorKind::RVVFixedLengthData_MF2:
+    return -1;
+  case VectorKind::RVVFixedLengthData_M1:
+    return 0;
+  case VectorKind::RVVFixedLengthData_M2:
+    return 1;
+  case VectorKind::RVVFixedLengthData_M4:
+    return 2;
+  case VectorKind::RVVFixedLengthData_M8:
+    return 3;
+  default:
+    llvm_unreachable("not an RVV fixed-length data vector kind");
+  }
+}
+
+/// Return the LMUL of an RVV fixed-length data vector kind as a string:
+/// "mf8" .. "m8" if \p MF is true, "1/8" .. "8" otherwise.
+inline StringRef getRVVFixedLengthDataLMulString(VectorKind VK, bool MF) {
+  switch (VK) {
+  case VectorKind::RVVFixedLengthData_MF8:
+    return MF ? "mf8" : "1/8";
+  case VectorKind::RVVFixedLengthData_MF4:
+    return MF ? "mf4" : "1/4";
+  case VectorKind::RVVFixedLengthData_MF2:
+    return MF ? "mf2" : "1/2";
+  case VectorKind::RVVFixedLengthData_M1:
+    return MF ? "m1" : "1";
+  case VectorKind::RVVFixedLengthData_M2:
+    return MF ? "m2" : "2";
+  case VectorKind::RVVFixedLengthData_M4:
+    return MF ? "m4" : "4";
+  case VectorKind::RVVFixedLengthData_M8:
+    return MF ? "m8" : "8";
+  default:
+    llvm_unreachable("not an RVV fixed-length data vector kind");
+  }
+}
+
+/// Inverse of getRVVFixedLengthDataLMulLog2().
+inline VectorKind getRVVFixedLengthDataVectorKind(int LMulLog2) {
+  switch (LMulLog2) {
+  case -3:
+    return VectorKind::RVVFixedLengthData_MF8;
+  case -2:
+    return VectorKind::RVVFixedLengthData_MF4;
+  case -1:
+    return VectorKind::RVVFixedLengthData_MF2;
+  case 0:
+    return VectorKind::RVVFixedLengthData_M1;
+  case 1:
+    return VectorKind::RVVFixedLengthData_M2;
+  case 2:
+    return VectorKind::RVVFixedLengthData_M4;
+  case 3:
+    return VectorKind::RVVFixedLengthData_M8;
+  default:
+    llvm_unreachable("LMUL out of range for an RVV type");
+  }
+}
+
 /// Represents a GCC generic vector type. This type is created using
 /// __attribute__((vector_size(n)), where "n" specifies the vector size in
 /// bytes; or from an Altivec __vector or vector declaration.
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 01d71388cbc0e3..6afc80a2b908bb 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2155,7 +2155,7 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
     else if (VT->getVectorKind() == VectorKind::SveFixedLengthPredicate)
       // Adjust the alignment for fixed-length SVE predicates.
       Align = 16;
-    else if (VT->getVectorKind() == VectorKind::RVVFixedLengthData ||
+    else if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
              VT->getVectorKind() == VectorKind::RVVFixedLengthMask ||
              VT->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
              VT->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
@@ -10771,8 +10771,8 @@ bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
       First->getVectorKind() != VectorKind::SveFixedLengthPredicate &&
       Second->getVectorKind() != VectorKind::SveFixedLengthData &&
       Second->getVectorKind() != VectorKind::SveFixedLengthPredicate &&
-      First->getVectorKind() != VectorKind::RVVFixedLengthData &&
-      Second->getVectorKind() != VectorKind::RVVFixedLengthData &&
+      !isRVVFixedLengthDataVectorKind(First->getVectorKind()) &&
+      !isRVVFixedLengthDataVectorKind(Second->getVectorKind()) &&
       First->getVectorKind() != VectorKind::RVVFixedLengthMask &&
       Second->getVectorKind() != VectorKind::RVVFixedLengthMask &&
       First->getVectorKind() != VectorKind::RVVFixedLengthMask_1 &&
@@ -10884,7 +10884,7 @@ bool ASTContext::areCompatibleRVVTypes(QualType FirstType,
                  Info.ElementType == BoolTy &&
                  getTypeSize(SecondType) == ((getRVVTypeSize(*this, BT)) * 2);
         }
-        if (VT->getVectorKind() == VectorKind::RVVFixedLengthData ||
+        if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
             VT->getVectorKind() == VectorKind::Generic)
           return FirstType->isRVVVLSBuiltinType() &&
                  getTypeSize(SecondType) == getRVVTypeSize(*this, BT) &&
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 02f27cec53f699..8fd9059abcdf3d 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -4316,7 +4316,7 @@ void CXXNameMangler::mangleAArch64FixedSveVectorType(
 }
 
 void CXXNameMangler::mangleRISCVFixedRVVVectorType(const VectorType *T) {
-  assert((T->getVectorKind() == VectorKind::RVVFixedLengthData ||
+  assert((isRVVFixedLengthDataVectorKind(T->getVectorKind()) ||
           T->getVectorKind() == VectorKind::RVVFixedLengthMask ||
           T->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
           T->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
@@ -4335,7 +4335,7 @@ void CXXNameMangler::mangleRISCVFixedRVVVectorType(const VectorType *T) {
     TypeNameOS << "int8";
     break;
   case BuiltinType::UChar:
-    if (T->getVectorKind() == VectorKind::RVVFixedLengthData)
+    if (isRVVFixedLengthDataVectorKind(T->getVectorKind()))
       TypeNameOS << "uint8";
     else
       TypeNameOS << "bool";
@@ -4392,19 +4392,19 @@ void CXXNameMangler::mangleRISCVFixedRVVVectorType(const VectorType *T) {
     break;
   }
 
-  // Apend the LMUL suffix.
-  auto VScale = getASTContext().getTargetInfo().getVScaleRange(
-      getASTContext().getLangOpts(),
-      TargetInfo::ArmStreamingKind::NotStreaming);
-  unsigned VLen = VScale->first * llvm::RISCV::RVVBitsPerBlock;
-
-  if (T->getVectorKind() == VectorKind::RVVFixedLengthData) {
-    TypeNameOS << 'm';
-    if (VecSizeInBits >= VLen)
-      TypeNameOS << (VecSizeInBits / VLen);
-    else
-      TypeNameOS << 'f' << (VLen / VecSizeInBits);
+  // Append the LMUL suffix. For data types the LMUL is recorded in the vector
+  // kind, so the mangled name names the type the typedef was written against
+  // even when the typedef is shorter than VLEN * LMUL, and does not depend on
+  // VLEN at all. Mask types are still tied to a fixed VLEN, so their ratio is
+  // derived from it.
+  if (isRVVFixedLengthDataVectorKind(T->getVectorKind())) {
+    TypeNameOS << getRVVFixedLengthDataLMulString(T->getVectorKind(),
+                                                  /*MF=*/true);
   } else {
+    auto VScale = getASTContext().getTargetInfo().getVScaleRange(
+        getASTContext().getLangOpts(),
+        TargetInfo::ArmStreamingKind::NotStreaming);
+    unsigned VLen = VScale->first * llvm::RISCV::RVVBitsPerBlock;
     TypeNameOS << (VLen / VecSizeInBits);
   }
   TypeNameOS << "_t";
@@ -4445,7 +4445,7 @@ void CXXNameMangler::mangleType(const VectorType *T) {
              T->getVectorKind() == VectorKind::SveFixedLengthPredicate) {
     mangleAArch64FixedSveVectorType(T);
     return;
-  } else if (T->getVectorKind() == VectorKind::RVVFixedLengthData ||
+  } else if (isRVVFixedLengthDataVectorKind(T->getVectorKind()) ||
              T->getVectorKind() == VectorKind::RVVFixedLengthMask ||
              T->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
              T->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
@@ -4478,7 +4478,7 @@ void CXXNameMangler::mangleType(const DependentVectorType *T) {
              T->getVectorKind() == VectorKind::SveFixedLengthPredicate) {
     mangleAArch64FixedSveVectorType(T);
     return;
-  } else if (T->getVectorKind() == VectorKind::RVVFixedLengthData) {
+  } else if (isRVVFixedLengthDataVectorKind(T->getVectorKind())) {
     mangleRISCVFixedRVVVectorType(T);
     return;
   }
diff --git a/clang/lib/AST/JSONNodeDumper.cpp b/clang/lib/AST/JSONNodeDumper.cpp
index 75abc4777f717f..2555c3658df02f 100644
--- a/clang/lib/AST/JSONNodeDumper.cpp
+++ b/clang/lib/AST/JSONNodeDumper.cpp
@@ -750,8 +750,18 @@ void JSONNodeDumper::VisitVectorType(const VectorType *VT) {
   case VectorKind::SveFixedLengthPredicate:
     JOS.attribute("vectorKind", "fixed-length sve predicate vector");
     break;
-  case VectorKind::RVVFixedLengthData:
-    JOS.attribute("vectorKind", "fixed-length rvv data vector");
+  case VectorKind::RVVFixedLengthData_MF8:
+  case VectorKind::RVVFixedLengthData_MF4:
+  case VectorKind::RVVFixedLengthData_MF2:
+  case VectorKind::RVVFixedLengthData_M1:
+  case VectorKind::RVVFixedLengthData_M2:
+  case VectorKind::RVVFixedLengthData_M4:
+  case VectorKind::RVVFixedLengthData_M8:
+    JOS.attribute("vectorKind",
+                  "fixed-length rvv data vector lmul " +
+                      getRVVFixedLengthDataLMulString(VT->getVectorKind(),
+                                                      /*MF=*/false)
+                          .str());
     break;
   case VectorKind::RVVFixedLengthMask:
   case VectorKind::RVVFixedLengthMask_1:
diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp
index 497fca228e428d..f1b216d970c731 100644
--- a/clang/lib/AST/TextNodeDumper.cpp
+++ b/clang/lib/AST/TextNodeDumper.cpp
@@ -2168,8 +2168,15 @@ void TextNodeDumper::VisitVectorType(const VectorType *T) {
   case VectorKind::SveFixedLengthPredicate:
     OS << " fixed-length sve predicate vector";
     break;
-  case VectorKind::RVVFixedLengthData:
-    OS << " fixed-length rvv data vector";
+  case VectorKind::RVVFixedLengthData_MF8:
+  case VectorKind::RVVFixedLengthData_MF4:
+  case VectorKind::RVVFixedLengthData_MF2:
+  case VectorKind::RVVFixedLengthData_M1:
+  case VectorKind::RVVFixedLengthData_M2:
+  case VectorKind::RVVFixedLengthData_M4:
+  case VectorKind::RVVFixedLengthData_M8:
+    OS << " fixed-length rvv data vector lmul "
+       << getRVVFixedLengthDataLMulString(T->getVectorKind(), /*MF=*/false);
     break;
   case VectorKind::RVVFixedLengthMask:
   case VectorKind::RVVFixedLengthMask_1:
diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index 13929f6fe08005..12cce9ccc32a55 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -718,7 +718,13 @@ void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) {
     OS << ") * 8))) ";
     printBefore(T->getElementType(), OS);
     break;
-  case VectorKind::RVVFixedLengthData:
+  case VectorKind::RVVFixedLengthData_MF8:
+  case VectorKind::RVVFixedLengthData_MF4:
+  case VectorKind::RVVFixedLengthData_MF2:
+  case VectorKind::RVVFixedLengthData_M1:
+  case VectorKind::RVVFixedLengthData_M2:
+  case VectorKind::RVVFixedLengthData_M4:
+  case VectorKind::RVVFixedLengthData_M8:
   case VectorKind::RVVFixedLengthMask:
   case VectorKind::RVVFixedLengthMask_1:
   case VectorKind::RVVFixedLengthMask_2:
@@ -813,7 +819,13 @@ void TypePrinter::printDependentVectorBefore(
     OS << "))) ";
     printBefore(T->getElementType(), OS);
     break;
-  case VectorKind::RVVFixedLengthData:
+  case VectorKind::RVVFixedLengthData_MF8:
+  case VectorKind::RVVFixedLengthData_MF4:
+  case VectorKind::RVVFixedLengthData_MF2:
+  case VectorKind::RVVFixedLengthData_M1:
+  case VectorKind::RVVFixedLengthData_M2:
+  case VectorKind::RVVFixedLengthData_M4:
+  case VectorKind::RVVFixedLengthData_M8:
   case VectorKind::RVVFixedLengthMask:
   case VectorKind::RVVFixedLengthMask_1:
   case VectorKind::RVVFixedLengthMask_2:
diff --git a/clang/lib/CodeGen/Targets/RISCV.cpp b/clang/lib/CodeGen/Targets/RISCV.cpp
index 4bac5711a2dd66..c84a4f00f4bc7f 100644
--- a/clang/lib/CodeGen/Targets/RISCV.cpp
+++ b/clang/lib/CodeGen/Targets/RISCV.cpp
@@ -40,6 +40,8 @@ class RISCVABIInfo : public DefaultABIInfo {
 
   llvm::Type *detectHomogeneousRVVFixedLengthStruct(QualType Ty) const;
 
+  unsigned getRVVFixedLengthMinElts(const VectorType *VT) const;
+
 public:
   RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen,
                bool EABI)
@@ -410,6 +412,35 @@ ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
   return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
 }
 
+/// Minimum element count of the scalable vector an RVV fixed-length *data*
+/// vector is coerced to, i.e. the N in <vscale x N x T>. The LMUL comes from
+/// the vector kind - the type the typedef was written against - rather than
+/// from VLEN and the element count.
+static unsigned getRVVFixedLengthDataMinElts(VectorKind VK,
+                                             unsigned EltSizeInBits) {
+  // The underlying scalable type holds LMUL * RVVBitsPerBlock bits at
+  // vscale=1.
+  int LMulLog2 = getRVVFixedLengthDataLMulLog2(VK);
+  unsigned SizeAtVScale1 = LMulLog2 >= 0
+                               ? llvm::RISCV::RVVBitsPerBlock << LMulLog2
+                               : llvm::RISCV::RVVBitsPerBlock >> -LMulLog2;
+  assert(SizeAtVScale1 >= EltSizeInBits && "LMUL too small for element type");
+  return SizeAtVScale1 / EltSizeInBits;
+}
+
+/// Minimum element count of the scalable vector \p VT is coerced to, in units
+/// of \p VT's own Clang element type. Masks are modelled as i8 vectors and are
+/// exactly VLEN * LMUL wide, so their LMUL is recovered from VLEN.
+unsigned RISCVABIInfo::getRVVFixedLengthMinElts(const VectorType *VT) const {
+  unsigned EltSizeInBits = getContext().getTypeSize(VT->getElementType());
+  if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()))
+    return getRVVFixedLengthDataMinElts(VT->getVectorKind(), EltSizeInBits);
+
+  auto VScale = getContext().getTargetInfo().getVScaleRange(
+      getContext().getLangOpts(), TargetInfo::ArmStreamingKind::NotStreaming);
+  return llvm::divideCeil(VT->getNumElements(), VScale->first);
+}
+
 llvm::Type *RISCVABIInfo::detectVLSCCEligibleStruct(QualType Ty,
                                                     unsigned ABIVLen) const {
   // No riscv_vls_cc attribute.
@@ -556,8 +587,9 @@ RISCVABIInfo::detectHomogeneousRVVFixedLengthStruct(QualType Ty) const {
     return nullptr;
 
   auto IsFixedLengthRVVVector = [](const VectorType *VT) {
+    if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()))
+      return true;
     switch (VT->getVectorKind()) {
-    case VectorKind::RVVFixedLengthData:
     case VectorKind::RVVFixedLengthMask:
     case VectorKind::RVVFixedLengthMask_1:
     case VectorKind::RVVFixedLengthMask_2:
@@ -607,24 +639,21 @@ RISCVABIInfo::detectHomogeneousRVVFixedLengthStruct(QualType Ty) const {
 
   const auto *VT = CommonTy->castAs<VectorType>();
   llvm::Type *EltType = CGT.ConvertType(VT->getElementType());
-  auto VScale = getContext().getTargetInfo().getVScaleRange(
-      getContext().getLangOpts(), TargetInfo::ArmStreamingKind::NotStreaming);
-
-  // Ensure total register usage does not exceed 8.
-  if (Count > 1 &&
-      Count * llvm::divideCeil((uint64_t)VT->getNumElements() *
-                                   EltType->getScalarSizeInBits(),
-                               VScale->first * llvm::RISCV::RVVBitsPerBlock) >
-          8)
+  unsigned MinElts = getRVVFixedLengthMinElts(VT);
+
+  // Ensure total register usage does not exceed 8. Each vector occupies
+  // max(1, LMUL) registers.
+  uint64_t RegsPerVec =
+      llvm::divideCeil((uint64_t)MinElts * EltType->getScalarSizeInBits(),
+                       llvm::RISCV::RVVBitsPerBlock);
+  if (Count > 1 && Count * RegsPerVec > 8)
     return nullptr;
 
-  unsigned MinElts = llvm::divideCeil(VT->getNumElements(), VScale->first);
   if (Count == 1)
     return llvm::ScalableVectorType::get(EltType, MinElts);
 
-  unsigned I8EltCount = llvm::divideCeil((uint64_t)VT->getNumElements() *
-                                             EltType->getScalarSizeInBits(),
-                                         VScale->first * 8);
+  unsigned I8EltCount =
+      llvm::divideCeil((uint64_t)MinElts * EltType->getScalarSizeInBits(), 8);
   auto *I8Vec = llvm::ScalableVectorType::get(
       llvm::Type::getInt8Ty(getVMContext()), I8EltCount);
   return llvm::TargetExtType::get(getVMContext(), "riscv.vector.tuple", I8Vec,
@@ -675,7 +704,7 @@ ABIArgInfo RISCVABIInfo::coerceVLSVector(QualType Ty, unsigned ABIVLen) const {
     break;
   default:
     assert((VT->getVectorKind() == VectorKind::Generic ||
-            VT->getVectorKind() == VectorKind::RVVFixedLengthData) &&
+            isRVVFixedLengthDataVectorKind(VT->getVectorKind())) &&
            "Unexpected vector kind");
     EltType = CGT.ConvertType(VT->getElementType());
   }
@@ -683,11 +712,19 @@ ABIArgInfo RISCVABIInfo::coerceVLSVector(QualType Ty, unsigned ABIVLen) const {
   llvm::ScalableVectorType *ResType;
 
   if (ABIVLen == 0) {
-    // The MinNumElts is simplified from equation:
-    // NumElts / VScale =
-    //  (EltSize * NumElts / (VScale * RVVBitsPerBlock))
-    //    * (RVVBitsPerBlock / EltSize)
-    ResType = llvm::ScalableVectorType::get(EltType, NumElts / VScale->first);
+    unsigned MinScalableElts;
+    if (isRVVFixedLengthDataVectorKind(VT->getVectorKind())) {
+      // Use the LMUL of the type the typedef was written against.
+      MinScalableElts = getRVVFixedLengthDataMinElts(
+          VT->getVectorKind(), EltType->getScalarSizeInBits());
+    } else {
+      // The MinNumElts is simplified from equation:
+      // NumElts / VScale =
+      //  (EltSize * NumElts / (VScale * RVVBitsPerBlock))
+      //    * (RVVBitsPerBlock / EltSize)
+      MinScalableElts = NumElts / VScale->first;
+    }
+    ResType = llvm::ScalableVectorType::get(EltType, MinScalableElts);
   } else {
     // Check registers needed <= 8.
     if ((EltType->getScalarSizeInBits() * NumElts / ABIVLen) > 8)
@@ -824,7 +861,7 @@ ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
   // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/419
   if (const VectorType *VT = Ty->getAs<VectorType>();
       VT && !VT->getElementType()->isBitIntType()) {
-    if (VT->getVectorKind() == VectorKind::RVVFixedLengthData ||
+    if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
         VT->getVectorKind() == VectorKind::RVVFixedLengthMask ||
         VT->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
         VT->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 00dd8acf1f68dd..9a11c859997eb6 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11061,7 +11061,7 @@ QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS,
       if (VecType->getVectorKind() == VectorKind::SveFixedLengthData ||
           VecType->getVectorKind() == VectorKind::SveFixedLengthPredicate)
         return true;
-      if (VecType->getVectorKind() == VectorKind::RVVFixedLengthData ||
+      if (isRVVFixedLengthDataVectorKind(VecType->getVectorKind()) ||
           VecType->getVectorKind() == VectorKind::RVVFixedLengthMask ||
           VecType->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
           VecType->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
@@ -11096,7 +11096,7 @@ QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS,
             SecondVecType->getVectorKind() ==
                 VectorKind::SveFixedLengthPredicate)
           return true;
-        if (SecondVecType->getVectorKind() == VectorKind::RVVFixedLengthData ||
+        if (isRVVFixedLengthDataVectorKind(SecondVecType->getVectorKind()) ||
             SecondVecType->getVectorKind() == VectorKind::RVVFixedLengthMask ||
             SecondVecType->getVectorKind() ==
                 VectorKind::RVVFixedLengthMask_1 ||
diff --git a/clang/lib/Sema/SemaRISCV.cpp b/clang/lib/Sema/SemaRISCV.cpp
index a1ba3b3934daa6..907123cd6379c0 100644
--- a/clang/lib/Sema/SemaRISCV.cpp
+++ b/clang/lib/Sema/SemaRISCV.cpp
@@ -1634,7 +1634,7 @@ bool SemaRISCV::isValidRVVBitcast(QualType srcTy, QualType destTy) {
       return false;
 
     const auto *VecTy = SecondType->getAs<VectorType>();
-    return VecTy && VecTy->getVectorKind() == VectorKind::RVVFixedLengthData;
+    return VecTy && isRVVFixedLengthDataVectorKind(VecTy->getVectorKind());
   };
 
   return ValidScalableConversion(srcTy, destTy) ||
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index f55c78b138d9ad..9c299b9e55da8d 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -46,6 +46,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/TargetParser/RISCVTargetParser.h"
 #include <bitset>
 #include <optional>
 
@@ -8870,7 +8871,7 @@ static void HandleRISCVRVVVectorBitsTypeAttr(QualType &CurType,
       S.Context.getBuiltinVectorTypeInfo(CurType->castAs<BuiltinType>());
   unsigned MinElts = Info.EC.getKnownMinValue();
 
-  VectorKind VecKind = VectorKind::RVVFixedLengthData;
+  VectorKind VecKind = VectorKind::RVVFixedLengthMask;
   unsigned ExpectedSize = VScale->first * MinElts;
   QualType EltType = CurType->getRVVEltType(S.Context);
   unsigned EltSize = S.Context.getTypeSize(EltType);
@@ -8895,6 +8896,13 @@ static void HandleRISCVRVVVectorBitsTypeAttr(QualType &CurType,
   } else {
     ExpectedSize *= EltSize;
     NumElts = VecSize / EltSize;
+
+    // Record the LMUL of the sizeless type in the vector kind, so that later
+    // stages do not have to re-derive it from VLEN and the element count.
+    //   LMUL = MinElts * EltSize / RVVBitsPerBlock
+    VecKind = getRVVFixedLengthDataVectorKind(
+        int(llvm::Log2_32(MinElts * EltSize)) -
+        int(llvm::Log2_32(llvm::RISCV::RVVBitsPerBlock)));
   }
 
   // The attribute vector size must match -mrvv-vector-bits.
diff --git a/clang/test/AST/ast-dump-riscv-rvv-fixed-length-data-types.c b/clang/test/AST/ast-dump-riscv-rvv-fixed-length-data-types.c
new file mode 100644
index 00000000000000..4ba0ea48ba4240
--- /dev/null
+++ b/clang/test/AST/ast-dump-riscv-rvv-fixed-length-data-types.c
@@ -0,0 +1,40 @@
+// Verify that a riscv_rvv_vector_bits typedef of a data type records the LMUL
+// of the sizeless type it was written against.
+
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +zve64d \
+// RUN:   -mvscale-min=2 -mvscale-max=2 -ast-dump %s | FileCheck %s
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +zve64d \
+// RUN:   -mvscale-min=2 -mvscale-max=2 -ast-dump=json %s \
+// RUN:   | FileCheck %s --check-prefix=JSON
+
+// VLEN=128.
+typedef __rvv_int8mf8_t fixed_i8mf8 __attribute__((riscv_rvv_vector_bits(16)));
+typedef __rvv_int16mf4_t fixed_i16mf4 __attribute__((riscv_rvv_vector_bits(32)));
+typedef __rvv_int32mf2_t fixed_i32mf2 __attribute__((riscv_rvv_vector_bits(64)));
+typedef __rvv_int32m1_t fixed_i32m1 __attribute__((riscv_rvv_vector_bits(128)));
+typedef __rvv_int32m2_t fixed_i32m2 __attribute__((riscv_rvv_vector_bits(256)));
+typedef __rvv_int64m4_t fixed_i64m4 __attribute__((riscv_rvv_vector_bits(512)));
+typedef __rvv_int8m8_t fixed_i8m8 __attribute__((riscv_rvv_vector_bits(1024)));
+
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_i8mf8
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 1/8 2
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_i16mf4
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 1/4 2
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_i32mf2
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 1/2 2
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_i32m1
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 1 4
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_i32m2
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 2 8
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_i64m4
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 4 8
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_i8m8
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 8 128
+
+// JSON: "vectorKind": "fixed-length rvv data vector lmul 1/8"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul 1/4"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul 1/2"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul 1"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul 2"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul 4"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul 8"

>From 9f1b8bc603b48f5a72ba88c0efa8ac4b38dc76a5 Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Tue, 13 Oct 2026 12:38:30 +0800
Subject: [PATCH 2/9] [Clang][RISCV] Allow riscv_rvv_vector_bits typedefs
 shorter than VLEN*LMUL

riscv_rvv_vector_bits(N) on a data type now accepts any N up to
VLEN * LMUL that gives a power-of-2 number of elements. Without
-mrvv-vector-bits=, VLEN is the minimum VLEN from -march.

A shorter typedef keeps the LMUL of its sizeless type and uses only the
low lanes. It converts implicitly to and from that sizeless type only.

Mask types are unchanged, that would be follow up PR for easier reivew.

Assisted-by: Claude
---
 clang/docs/ReleaseNotes.md                    |   6 +
 clang/include/clang/Basic/AttrDocs.td         |  23 ++-
 .../clang/Basic/DiagnosticSemaKinds.td        |   9 +
 clang/lib/AST/ASTContext.cpp                  |  30 ++-
 clang/lib/Sema/SemaType.cpp                   |  50 +++--
 .../CodeGen/RISCV/attr-rvv-vector-bits-lmul.c | 180 ++++++++++++++++++
 .../RISCV/attr-rvv-vector-bits-no-flag.c      |  40 ++++
 .../RISCV/attr-rvv-vector-bits-relaxed-call.c |  50 +++++
 .../attr-rvv-vector-bits-relaxed-convert.c    |  94 +++++++++
 .../riscv-mangle-rvv-fixed-vectors-lmul.cpp   |  39 ++++
 clang/test/Driver/riscv-rvv-vector-bits.c     |  15 +-
 .../Sema/attr-riscv-rvv-vector-bits-lmul.c    | 125 ++++++++++++
 .../Sema/attr-riscv-rvv-vector-bits-no-flag.c |  80 ++++++++
 .../Sema/attr-riscv-rvv-vector-bits-relaxed.c |  67 +++++++
 14 files changed, 778 insertions(+), 30 deletions(-)
 create mode 100644 clang/test/CodeGen/RISCV/attr-rvv-vector-bits-lmul.c
 create mode 100644 clang/test/CodeGen/RISCV/attr-rvv-vector-bits-no-flag.c
 create mode 100644 clang/test/CodeGen/RISCV/attr-rvv-vector-bits-relaxed-call.c
 create mode 100644 clang/test/CodeGen/RISCV/attr-rvv-vector-bits-relaxed-convert.c
 create mode 100644 clang/test/CodeGenCXX/riscv-mangle-rvv-fixed-vectors-lmul.cpp
 create mode 100644 clang/test/Sema/attr-riscv-rvv-vector-bits-lmul.c
 create mode 100644 clang/test/Sema/attr-riscv-rvv-vector-bits-no-flag.c
 create mode 100644 clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed.c

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index a367196a601bc7..ed34bed7df8604 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1327,6 +1327,12 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   `-mcpu` is present, the ISA will be selected from the host CPU and the tune
   CPU will be selected from `-mcpu`.
 
+- `riscv_rvv_vector_bits(N)` now accepts any `N` up to `VLEN*LMUL` bits for
+  data types. For them it no longer requires `-mrvv-vector-bits`; without it,
+  `VLEN` is the minimum required by `-march`. A shorter typedef keeps the LMUL
+  of the type it was written against, and converts implicitly to and from that
+  sizeless type (truncating, or leaving the upper lanes undefined).
+
 #### CUDA/HIP Language Changes
 
 - HIP compilations now add the `include/libhipcxx` directory from the selected
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 687e022c7bd61f..89b8b86f3b9ed9 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -3347,15 +3347,24 @@ can be used in globals, structs, unions, and arrays, all of which are
 unsupported for sizeless types.
 
 The attribute can be attached to a single RVV vector (such as `vint8m1_t`).
-The attribute will be rejected unless
-`N==(__riscv_v_fixed_vlen*LMUL)`, the implementation defined feature macro that
-is enabled under the `-mrvv-vector-bits` flag. `__riscv_v_fixed_vlen` can
-only be a power of 2 between 64 and 65536.
 
-For types where LMUL!=1, `__riscv_v_fixed_vlen` needs to be scaled by the LMUL
-of the type before passing to the attribute.
+For data types, `N` is a size in bits. It must be a positive multiple of the
+element size, `N` divided by the element size must be a power of 2, and `N`
+must not exceed `VLEN*LMUL`.
 
-For `vbool*_t` types, `__riscv_v_fixed_vlen` needs to be divided by the
+A data type with `N` smaller than `VLEN*LMUL` uses only the low lanes of the
+register group:
+
+- It keeps the LMUL of the type it was written against. For example,
+  `vint32m1_t` with `riscv_rvv_vector_bits(64)` occupies one vector register
+  and is a different type from `vint32mf2_t` with `riscv_rvv_vector_bits(64)`.
+- It converts implicitly to and from its own sizeless type only. Converting to
+  the sizeless type leaves the upper lanes undefined; converting back keeps the
+  first `N` bits. When such a value is passed to an RVV intrinsic, the
+  programmer must keep `vl` (and any `vrgather` index) within those lanes.
+
+For `vbool*_t` types, the attribute will be rejected unless
+`-mrvv-vector-bits` is given and `N` is `__riscv_v_fixed_vlen` divided by the
 number from the type name. For example, `vbool8_t` needs to use
 `__riscv_v_fixed_vlen` / 8. If the resulting value is not a multiple of 8,
 the type is not supported for that value of `__riscv_v_fixed_vlen`.
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 56cc9721781063..bbebbb37a3ce24 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3457,6 +3457,15 @@ def err_attribute_riscv_rvv_bits_unsupported : Error<
 def err_attribute_bad_rvv_vector_size : Error<
   "invalid RVV vector size '%0', expected size is '%1' based on LMUL of type "
   "and '-mrvv-vector-bits'">;
+def err_attribute_bad_rvv_vector_size_too_large : Error<
+  "invalid RVV vector size '%0', must not exceed '%1' based on LMUL of type "
+  "and the minimum VLEN">;
+def err_attribute_bad_rvv_vector_size_not_multiple_of_elem : Error<
+  "invalid RVV vector size '%0', must be a positive multiple of the element "
+  "size '%1'">;
+def err_attribute_bad_rvv_vector_size_not_pow2 : Error<
+  "invalid RVV vector size '%0', the resulting element count '%1' must be a "
+  "power of 2">;
 def err_attribute_invalid_rvv_type : Error<
   "%0 attribute applied to non-RVV type %1">;
 def err_attribute_requires_positive_integer : Error<
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 6afc80a2b908bb..c821fb50d3b0f2 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -91,6 +91,7 @@
 #include "llvm/Support/SipHash.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TargetParser/AArch64TargetParser.h"
+#include "llvm/TargetParser/RISCVTargetParser.h"
 #include "llvm/TargetParser/Triple.h"
 #include <algorithm>
 #include <cassert>
@@ -10850,6 +10851,16 @@ static uint64_t getRVVTypeSize(ASTContext &Context, const BuiltinType *Ty) {
   return VScale->first * MinElts * EltSize;
 }
 
+/// Return log2 of the LMUL of a sizeless RVV data type, as
+/// getRVVFixedLengthDataLMulLog2() does for a vector kind.
+static int getRVVBuiltinLMulLog2(ASTContext &Context, const BuiltinType *Ty) {
+  ASTContext::BuiltinVectorTypeInfo Info = Context.getBuiltinVectorTypeInfo(Ty);
+  uint64_t SizeAtVScale1 =
+      Info.EC.getKnownMinValue() * Context.getTypeSize(Info.ElementType);
+  return int(llvm::Log2_64(SizeAtVScale1)) -
+         int(llvm::Log2_32(llvm::RISCV::RVVBitsPerBlock));
+}
+
 bool ASTContext::areCompatibleRVVTypes(QualType FirstType,
                                        QualType SecondType) {
   assert(
@@ -10885,11 +10896,20 @@ bool ASTContext::areCompatibleRVVTypes(QualType FirstType,
                  getTypeSize(SecondType) == ((getRVVTypeSize(*this, BT)) * 2);
         }
         if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
-            VT->getVectorKind() == VectorKind::Generic)
-          return FirstType->isRVVVLSBuiltinType() &&
-                 getTypeSize(SecondType) == getRVVTypeSize(*this, BT) &&
-                 hasSameType(VT->getElementType(),
-                             getBuiltinVectorTypeInfo(BT).ElementType);
+            VT->getVectorKind() == VectorKind::Generic) {
+          bool IsRVVFixed = isRVVFixedLengthDataVectorKind(VT->getVectorKind());
+          BuiltinVectorTypeInfo Info = getBuiltinVectorTypeInfo(BT);
+          // A riscv_rvv_vector_bits typedef may be shorter than the sizeless
+          // type, but must have the same LMUL.
+          bool SizeOk =
+              IsRVVFixed ? getTypeSize(SecondType) <= getRVVTypeSize(*this, BT)
+                         : getTypeSize(SecondType) == getRVVTypeSize(*this, BT);
+          bool LMulOk = !IsRVVFixed || Info.ElementType == BoolTy ||
+                        getRVVFixedLengthDataLMulLog2(VT->getVectorKind()) ==
+                            getRVVBuiltinLMulLog2(*this, BT);
+          return FirstType->isRVVVLSBuiltinType() && SizeOk && LMulOk &&
+                 hasSameType(VT->getElementType(), Info.ElementType);
+        }
       }
     }
     return false;
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 9c299b9e55da8d..f6137dd75f86fa 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -8843,15 +8843,6 @@ static void HandleRISCVRVVVectorBitsTypeAttr(QualType &CurType,
     return;
   }
 
-  auto VScale = S.Context.getTargetInfo().getVScaleRange(
-      S.getLangOpts(), TargetInfo::ArmStreamingKind::NotStreaming);
-  if (!VScale || !VScale->first || VScale->first != VScale->second) {
-    S.Diag(Attr.getLoc(), diag::err_attribute_riscv_rvv_bits_unsupported)
-        << Attr;
-    Attr.setInvalid();
-    return;
-  }
-
   // The vector size must be an integer constant expression.
   llvm::APSInt RVVVectorSizeInBits(32);
   if (!verifyValidIntegerConstantExpr(S, Attr, RVVVectorSizeInBits))
@@ -8870,13 +8861,25 @@ static void HandleRISCVRVVVectorBitsTypeAttr(QualType &CurType,
   ASTContext::BuiltinVectorTypeInfo Info =
       S.Context.getBuiltinVectorTypeInfo(CurType->castAs<BuiltinType>());
   unsigned MinElts = Info.EC.getKnownMinValue();
+  bool IsMask = Info.ElementType == S.Context.BoolTy;
+
+  // A data type needs only the minimum VLEN; a mask type needs a fixed VLEN.
+  auto VScale = S.Context.getTargetInfo().getVScaleRange(
+      S.getLangOpts(), TargetInfo::ArmStreamingKind::NotStreaming);
+  if (!VScale || !VScale->first ||
+      (IsMask && VScale->first != VScale->second)) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_riscv_rvv_bits_unsupported)
+        << Attr;
+    Attr.setInvalid();
+    return;
+  }
 
   VectorKind VecKind = VectorKind::RVVFixedLengthMask;
   unsigned ExpectedSize = VScale->first * MinElts;
   QualType EltType = CurType->getRVVEltType(S.Context);
   unsigned EltSize = S.Context.getTypeSize(EltType);
   unsigned NumElts;
-  if (Info.ElementType == S.Context.BoolTy) {
+  if (IsMask) {
     NumElts = VecSize / S.Context.getCharWidth();
     if (!NumElts) {
       NumElts = 1;
@@ -8905,14 +8908,37 @@ static void HandleRISCVRVVVectorBitsTypeAttr(QualType &CurType,
         int(llvm::Log2_32(llvm::RISCV::RVVBitsPerBlock)));
   }
 
-  // The attribute vector size must match -mrvv-vector-bits.
-  if (VecSize != ExpectedSize) {
+  // The size of a mask type must match -mrvv-vector-bits.
+  if (IsMask && VecSize != ExpectedSize) {
     S.Diag(Attr.getLoc(), diag::err_attribute_bad_rvv_vector_size)
         << VecSize << ExpectedSize;
     Attr.setInvalid();
     return;
   }
 
+  // A data type may be shorter than VLEN * LMUL, with a power-of-2 number of
+  // elements.
+  if (VecSize > ExpectedSize) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_bad_rvv_vector_size_too_large)
+        << VecSize << ExpectedSize;
+    Attr.setInvalid();
+    return;
+  }
+  if (!IsMask && (VecSize == 0 || (VecSize % EltSize) != 0)) {
+    S.Diag(Attr.getLoc(),
+           diag::err_attribute_bad_rvv_vector_size_not_multiple_of_elem)
+        << VecSize << EltSize;
+    Attr.setInvalid();
+    return;
+  }
+  unsigned NumLanes = IsMask ? VecSize : NumElts;
+  if (!llvm::isPowerOf2_32(NumLanes)) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_bad_rvv_vector_size_not_pow2)
+        << VecSize << NumLanes;
+    Attr.setInvalid();
+    return;
+  }
+
   CurType = S.Context.getVectorType(EltType, NumElts, VecKind);
 }
 
diff --git a/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-lmul.c b/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-lmul.c
new file mode 100644
index 00000000000000..d851acc974a466
--- /dev/null
+++ b/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-lmul.c
@@ -0,0 +1,180 @@
+// Verify that a riscv_rvv_vector_bits typedef keeps the LMUL of the sizeless
+// RVV type it was written against, rather than having it re-derived from VLEN
+// and the declared lane count.
+//
+// A typedef may declare fewer lanes than
+// VLEN * LMUL, so NumElements / vscale under-estimates the LMUL. Two typedefs
+// with the same element type and lane count but different LMULs must map to
+// different scalable vector types.
+//
+// VLEN=128 (vscale=2): vint32mf2_t holds 2 i32 lanes at full size,
+// vint32m1_t 4, vint32m2_t 8, vint32m4_t 16.
+
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -mvscale-min=2 -mvscale-max=2 \
+// RUN:   -O1 -emit-llvm -o - %s | FileCheck %s
+
+// REQUIRES: riscv-registered-target
+
+#include <riscv_vector.h>
+
+// All of these hold 2 i32 lanes; only the LMUL differs.
+typedef vint32mf2_t mf2_64 __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m1_t  m1_64  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m2_t  m2_64  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m4_t  m4_64  __attribute__((riscv_rvv_vector_bits(64)));
+
+// A single lane out of an LMUL=1 group.
+typedef vint32m1_t m1_32 __attribute__((riscv_rvv_vector_bits(32)));
+
+// Other element sizes.
+typedef vint8m1_t    i8m1_32   __attribute__((riscv_rvv_vector_bits(32)));
+typedef vint8mf4_t   i8mf4_16  __attribute__((riscv_rvv_vector_bits(16)));
+typedef vfloat32m1_t f32m1_64  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vfloat64m1_t f64m1_64  __attribute__((riscv_rvv_vector_bits(64)));
+
+//===----------------------------------------------------------------------===//
+// Same lane count, different LMUL => different ABI types.
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: define dso_local noundef <vscale x 1 x i32> @id_mf2_64(
+// CHECK-SAME:    <vscale x 1 x i32> {{.*}}%x.coerce)
+mf2_64 id_mf2_64(mf2_64 x) { return x; }
+
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @id_m1_64(
+// CHECK-SAME:    <vscale x 2 x i32> {{.*}}%x.coerce)
+m1_64 id_m1_64(m1_64 x) { return x; }
+
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 4 x i32> @id_m2_64(
+// CHECK-SAME:    <vscale x 4 x i32> {{.*}}%x.coerce)
+m2_64 id_m2_64(m2_64 x) { return x; }
+
+// Two lanes of an LMUL=4 type still occupy the whole LMUL=4 group.
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 8 x i32> @id_m4_64(
+// CHECK-SAME:    <vscale x 8 x i32> {{.*}}%x.coerce)
+m4_64 id_m4_64(m4_64 x) { return x; }
+
+// One lane still occupies a whole LMUL=1 group.
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @id_m1_32(
+// CHECK-SAME:    <vscale x 2 x i32> {{.*}}%x.coerce)
+m1_32 id_m1_32(m1_32 x) { return x; }
+
+//===----------------------------------------------------------------------===//
+// Other element sizes.
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 8 x i8> @id_i8m1_32(
+// CHECK-SAME:    <vscale x 8 x i8> {{.*}}%x.coerce)
+i8m1_32 id_i8m1_32(i8m1_32 x) { return x; }
+
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x i8> @id_i8mf4_16(
+// CHECK-SAME:    <vscale x 2 x i8> {{.*}}%x.coerce)
+i8mf4_16 id_i8mf4_16(i8mf4_16 x) { return x; }
+
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x float> @id_f32m1_64(
+// CHECK-SAME:    <vscale x 2 x float> {{.*}}%x.coerce)
+f32m1_64 id_f32m1_64(f32m1_64 x) { return x; }
+
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 1 x double> @id_f64m1_64(
+// CHECK-SAME:    <vscale x 1 x double> {{.*}}%x.coerce)
+f64m1_64 id_f64m1_64(f64m1_64 x) { return x; }
+
+//===----------------------------------------------------------------------===//
+// Calling RVV intrinsics with these typedefs. Because the ABI type already
+// matches the LMUL the intrinsic expects, the argument flows straight into the
+// intrinsic with no llvm.vector.insert / llvm.vector.extract in between.
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @add_m1(
+// CHECK-SAME:    <vscale x 2 x i32> {{.*}}%a.coerce, <vscale x 2 x i32> {{.*}}%b.coerce)
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    [[R:%.*]] = {{.*}}call <vscale x 2 x i32> @llvm.riscv.vadd.nxv2i32.nxv2i32.i64(<vscale x 2 x i32> poison, <vscale x 2 x i32> %a.coerce, <vscale x 2 x i32> %b.coerce, i64 2)
+// CHECK-NEXT:    ret <vscale x 2 x i32> [[R]]
+m1_64 add_m1(m1_64 a, m1_64 b) { return __riscv_vadd_vv_i32m1(a, b, 2); }
+
+// Same lane count as add_m1, but the mf2 typedef selects the mf2 intrinsic.
+// CHECK-LABEL: define dso_local <vscale x 1 x i32> @add_mf2(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    [[R:%.*]] = {{.*}}call <vscale x 1 x i32> @llvm.riscv.vadd.nxv1i32.nxv1i32.i64(<vscale x 1 x i32> poison, <vscale x 1 x i32> %a.coerce, <vscale x 1 x i32> %b.coerce, i64 2)
+// CHECK-NEXT:    ret <vscale x 1 x i32> [[R]]
+mf2_64 add_mf2(mf2_64 a, mf2_64 b) { return __riscv_vadd_vv_i32mf2(a, b, 2); }
+
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 4 x i32> @add_m2(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    [[R:%.*]] = {{.*}}call <vscale x 4 x i32> @llvm.riscv.vadd.nxv4i32.nxv4i32.i64(<vscale x 4 x i32> poison, <vscale x 4 x i32> %a.coerce, <vscale x 4 x i32> %b.coerce, i64 2)
+// CHECK-NEXT:    ret <vscale x 4 x i32> [[R]]
+m2_64 add_m2(m2_64 a, m2_64 b) { return __riscv_vadd_vv_i32m2(a, b, 2); }
+
+// Fractional LMUL with a narrow element type.
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x i8> @add_i8mf4(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    [[R:%.*]] = {{.*}}call <vscale x 2 x i8> @llvm.riscv.vadd.nxv2i8.nxv2i8.i64(<vscale x 2 x i8> poison, <vscale x 2 x i8> %a.coerce, <vscale x 2 x i8> %b.coerce, i64 2)
+// CHECK-NEXT:    ret <vscale x 2 x i8> [[R]]
+i8mf4_16 add_i8mf4(i8mf4_16 a, i8mf4_16 b) {
+  return __riscv_vadd_vv_i8mf4(a, b, 2);
+}
+
+// Floating point.
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x float> @fadd_m1(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    [[R:%.*]] = {{.*}}call <vscale x 2 x float> @llvm.riscv.vfadd.nxv2f32.nxv2f32.i64(<vscale x 2 x float> poison, <vscale x 2 x float> %a.coerce, <vscale x 2 x float> %b.coerce, i64 7, i64 2)
+// CHECK-NEXT:    ret <vscale x 2 x float> [[R]]
+f32m1_64 fadd_m1(f32m1_64 a, f32m1_64 b) {
+  return __riscv_vfadd_vv_f32m1(a, b, 2);
+}
+
+// Load returning into the shorter typedef.
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @load_m1(
+// CHECK:         [[R:%.*]] = {{.*}}call <vscale x 2 x i32> @llvm.riscv.vle.nxv2i32.p0.i64(<vscale x 2 x i32> poison, ptr %p, i64 2)
+// CHECK-NEXT:    ret <vscale x 2 x i32> [[R]]
+m1_64 load_m1(const int *p) { return __riscv_vle32_v_i32m1(p, 2); }
+
+// Store taking the shorter typedef.
+// CHECK-LABEL: define dso_local void @store_m1(
+// CHECK:         call void @llvm.riscv.vse.nxv2i32.p0.i64(<vscale x 2 x i32> %v.coerce, ptr %p, i64 2)
+void store_m1(int *p, m1_64 v) { __riscv_vse32_v_i32m1(p, v, 2); }
+
+// Mask-producing and masked intrinsics: the mask LMUL follows the data LMUL.
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @masked_add_m1(
+// CHECK:         [[M:%.*]] = {{.*}}call <vscale x 2 x i1> @llvm.riscv.vmseq.nxv2i32.nxv2i32.i64(<vscale x 2 x i32> %a.coerce, <vscale x 2 x i32> %b.coerce, i64 2)
+// CHECK-NEXT:    [[R:%.*]] = {{.*}}call <vscale x 2 x i32> @llvm.riscv.vadd.mask.nxv2i32.nxv2i32.i64(<vscale x 2 x i32> poison, <vscale x 2 x i32> %a.coerce, <vscale x 2 x i32> %b.coerce, <vscale x 2 x i1> [[M]], i64 2, i64 3)
+// CHECK-NEXT:    ret <vscale x 2 x i32> [[R]]
+m1_64 masked_add_m1(m1_64 a, m1_64 b) {
+  vbool32_t m = __riscv_vmseq_vv_i32m1_b32(a, b, 2);
+  return __riscv_vadd_vv_i32m1_m(m, a, b, 2);
+}
+
+//===----------------------------------------------------------------------===//
+// Structs. The number of vector registers a field needs follows its LMUL, not
+// the number of lanes it declares.
+//===----------------------------------------------------------------------===//
+
+struct s1_m1 { m1_64 a; };
+// CHECK-LABEL: define dso_local void @st1_m1(
+// CHECK-SAME:    <vscale x 2 x i32>
+void st1_m1(struct s1_m1 s) {}
+
+struct s2_m1 { m1_64 a, b; };
+// CHECK-LABEL: define dso_local void @st2_m1(
+// CHECK-SAME:    target("riscv.vector.tuple", <vscale x 8 x i8>, 2)
+void st2_m1(struct s2_m1 s) {}
+
+struct s2_mf2 { mf2_64 a, b; };
+// CHECK-LABEL: define dso_local void @st2_mf2(
+// CHECK-SAME:    target("riscv.vector.tuple", <vscale x 4 x i8>, 2)
+void st2_mf2(struct s2_mf2 s) {}
+
+// 2 * LMUL4 = 8 registers, right at the limit.
+struct s2_m4 { m4_64 a, b; };
+// CHECK-LABEL: define dso_local void @st2_m4(
+// CHECK-SAME:    target("riscv.vector.tuple", <vscale x 32 x i8>, 2)
+void st2_m4(struct s2_m4 s) {}
+
+// 3 * LMUL4 = 12 registers, over the limit, so the struct is passed
+// indirectly. Deriving the LMUL from the lane count would have made this look
+// like 3 * LMUL(1/2) and wrongly passed it in registers.
+struct s3_m4 { m4_64 a, b, c; };
+// CHECK-LABEL: define dso_local void @st3_m4(
+// CHECK-SAME:    ptr
+void st3_m4(struct s3_m4 s) {}
diff --git a/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-no-flag.c b/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-no-flag.c
new file mode 100644
index 00000000000000..eab187b70b2214
--- /dev/null
+++ b/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-no-flag.c
@@ -0,0 +1,40 @@
+// Verify that riscv_rvv_vector_bits typedefs work without -mrvv-vector-bits=,
+// using only the ISA's Zvl*b extension as the lower bound. The fixed<->scalable
+// conversion IR landmarks must be present.
+
+// zvl128b, no -mrvv-vector-bits=:
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -target-feature +zvl128b \
+// RUN:   -O1 -emit-llvm -o - %s | FileCheck %s
+
+// The fixed<->scalable conversions fold away at -O1 now that both sides use
+// the same LMUL, so check the landmarks on unoptimized IR.
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -target-feature +zvl128b \
+// RUN:   -O1 -disable-llvm-passes -emit-llvm -o - %s \
+// RUN:   | FileCheck %s --check-prefix=RAW
+
+// REQUIRES: riscv-registered-target
+
+#include <riscv_vector.h>
+
+typedef vint32m1_t m1_64 __attribute__((riscv_rvv_vector_bits(64)));
+
+// Fixed -> scalable conversion uses insert into poison. m1_64 keeps LMUL 1, so
+// the ABI type already matches vint32m1_t and the conversion folds away.
+// RAW-LABEL: define dso_local <vscale x 2 x i32> @widen
+// RAW: call <vscale x 2 x i32> @llvm.vector.insert.nxv2i32.v2i32(<vscale x 2 x i32> poison
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @widen
+// CHECK-NEXT: entry:
+// CHECK-NEXT: ret <vscale x 2 x i32> %x.coerce
+vint32m1_t widen(m1_64 x) { return x; }
+
+// Scalable -> fixed conversion uses extract.
+// RAW-LABEL: define dso_local <vscale x 2 x i32> @narrow
+// RAW: call <2 x i32> @llvm.vector.extract.v2i32.nxv2i32(
+// CHECK-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @narrow
+// CHECK-NEXT: entry:
+// CHECK-NEXT: ret <vscale x 2 x i32> %x
+m1_64 narrow(vint32m1_t x) { return x; }
diff --git a/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-relaxed-call.c b/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-relaxed-call.c
new file mode 100644
index 00000000000000..26a6c4988d5a3f
--- /dev/null
+++ b/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-relaxed-call.c
@@ -0,0 +1,50 @@
+// Verify CodeGen coercion for shorter riscv_rvv_vector_bits typedefs.
+//
+// A shorter typedef keeps the LMUL of the sizeless type it was written
+// against, so it is passed in the same register group as the full-size
+// typedef; only the lanes it declares hold defined data.
+//
+// VLEN=128 (vscale=2): vint32m1_t has 4 i32 lanes at full size.
+//   - bits(128) -> <4 x i32> -> <vscale x 2 x i32> (m1, all 4 lanes used)
+//   - bits(64)  -> <2 x i32> -> <vscale x 2 x i32> (m1, upper 2 lanes garbage)
+//   - bits(32)  -> <1 x i32> -> <vscale x 2 x i32> (m1, upper 3 lanes garbage)
+
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -mvscale-min=2 -mvscale-max=2 \
+// RUN:   -O1 -emit-llvm -o - %s | FileCheck %s
+
+// REQUIRES: riscv-registered-target
+
+#include <riscv_vector.h>
+
+typedef vint32m1_t fixed_i32x4 __attribute__((riscv_rvv_vector_bits(128)));
+typedef vint32m1_t fixed_i32x2 __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m1_t fixed_i32x1 __attribute__((riscv_rvv_vector_bits(32)));
+typedef vint8m1_t  fixed_i8x4  __attribute__((riscv_rvv_vector_bits(32)));
+
+// Full-size case still works the same as the strict path.
+// CHECK-LABEL: define dso_local{{.*}} <vscale x 2 x i32> @id_i32x4(
+// CHECK-SAME:    <vscale x 2 x i32> {{.*}}%x.coerce)
+// CHECK:         ret <vscale x 2 x i32> %x.coerce
+fixed_i32x4 id_i32x4(fixed_i32x4 x) { return x; }
+
+// Half the lanes, same LMUL: still <vscale x 2 x i32>, not <vscale x 1 x i32>.
+// CHECK-LABEL: define dso_local{{.*}} <vscale x 2 x i32> @id_i32x2(
+// CHECK-SAME:    <vscale x 2 x i32> {{.*}}%x.coerce)
+fixed_i32x2 id_i32x2(fixed_i32x2 x) { return x; }
+
+// A single lane still occupies a whole LMUL=1 register group.
+// CHECK-LABEL: define dso_local{{.*}} <vscale x 2 x i32> @id_i32x1(
+// CHECK-SAME:    <vscale x 2 x i32> {{.*}}%x.coerce)
+fixed_i32x1 id_i32x1(fixed_i32x1 x) { return x; }
+
+// 4 i8 lanes = 32 bits out of a 128-bit LMUL=1 group, which holds 8 i8 lanes
+// at vscale=1.
+// CHECK-LABEL: define dso_local{{.*}} <vscale x 8 x i8> @id_i8x4(
+// CHECK-SAME:    <vscale x 8 x i8> {{.*}}%x.coerce)
+fixed_i8x4 id_i8x4(fixed_i8x4 x) { return x; }
+
+// Note: implicit conversion between shorter fixed types and the sizeless
+// VLA type (e.g. calling sizeless_callee(fixed_i32x2)) is handled by
+// Phase 3 and is intentionally not exercised here.
diff --git a/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-relaxed-convert.c b/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-relaxed-convert.c
new file mode 100644
index 00000000000000..7d0c278bfc0513
--- /dev/null
+++ b/clang/test/CodeGen/RISCV/attr-rvv-vector-bits-relaxed-convert.c
@@ -0,0 +1,94 @@
+// Verify implicit conversion semantics between a shorter
+// riscv_rvv_vector_bits typedef and the underlying sizeless RVV type.
+//
+// VLEN=128 (vscale=2): vint32m1_t = <vscale x 2 x i32> (4 lanes total).
+// A shorter typedef of vint32m1_t keeps LMUL 1, so it ABI-coerces to
+// <vscale x 2 x i32> as well and only differs in how many lanes carry
+// defined data:
+//   fixed_i32x2 (bits(64)) declares 2 of the 4 i32 lanes.
+//   fixed_i32x1 (bits(32)) declares 1 of the 4 i32 lanes.
+//
+// RAW checks the conversions Clang emits: llvm.vector.extract to truncate
+// down to the declared lanes and llvm.vector.insert into poison to widen back
+// with garbage. OPT checks that, because both sides now agree on LMUL, the
+// round trips fold away entirely instead of leaving a change of register
+// group behind.
+
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -mvscale-min=2 -mvscale-max=2 \
+// RUN:   -O1 -disable-llvm-passes -emit-llvm -o - %s \
+// RUN:   | FileCheck %s --check-prefix=RAW
+
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -mvscale-min=2 -mvscale-max=2 \
+// RUN:   -O1 -emit-llvm -o - %s | FileCheck %s --check-prefix=OPT
+
+// REQUIRES: riscv-registered-target
+
+#include <riscv_vector.h>
+
+typedef vint32m1_t fixed_i32x4 __attribute__((riscv_rvv_vector_bits(128)));
+typedef vint32m1_t fixed_i32x2 __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m1_t fixed_i32x1 __attribute__((riscv_rvv_vector_bits(32)));
+
+vint32m1_t sizeless_callee(vint32m1_t x);
+
+// fixed_i32x2 -> vint32m1_t: garbage-fill the lanes the typedef does not
+// declare.
+// RAW-LABEL: define dso_local <vscale x 2 x i32> @widen(
+// RAW-SAME:    <vscale x 2 x i32> {{.*}}%x.coerce)
+// RAW:         [[T0:%.*]] = call <2 x i32> @llvm.vector.extract.v2i32.nxv2i32(<vscale x 2 x i32> %x.coerce, i64 0)
+// RAW:         call <vscale x 2 x i32> @llvm.vector.insert.nxv2i32.v2i32(<vscale x 2 x i32> poison, <2 x i32> {{.*}}, i64 0)
+//
+// OPT-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @widen(
+// OPT-NEXT:  entry:
+// OPT-NEXT:    ret <vscale x 2 x i32> %x.coerce
+vint32m1_t widen(fixed_i32x2 x) { return x; }
+
+// vint32m1_t -> fixed_i32x2: truncate to the first 2 lanes.
+// RAW-LABEL: define dso_local <vscale x 2 x i32> @narrow(
+// RAW:         [[T0:%.*]] = call <2 x i32> @llvm.vector.extract.v2i32.nxv2i32(<vscale x 2 x i32> {{.*}}, i64 0)
+//
+// OPT-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @narrow(
+// OPT-NEXT:  entry:
+// OPT-NEXT:    ret <vscale x 2 x i32> %x
+fixed_i32x2 narrow(vint32m1_t x) { return x; }
+
+// vint32m1_t -> fixed_i32x1: truncate to the first lane, still returned in a
+// whole LMUL=1 register.
+// RAW-LABEL: define dso_local <vscale x 2 x i32> @narrow_to_1(
+// RAW:         call <1 x i32> @llvm.vector.extract.v1i32.nxv2i32(<vscale x 2 x i32> {{.*}}, i64 0)
+// RAW:         call <vscale x 2 x i32> @llvm.vector.insert.nxv2i32.v1i32(<vscale x 2 x i32> poison, <1 x i32> {{.*}}, i64 0)
+fixed_i32x1 narrow_to_1(vint32m1_t x) { return x; }
+
+// fixed_i32x1 -> vint32m1_t: 1-lane garbage fill.
+// RAW-LABEL: define dso_local <vscale x 2 x i32> @widen_from_1(
+// RAW:         call <1 x i32> @llvm.vector.extract.v1i32.nxv2i32(<vscale x 2 x i32> %x.coerce, i64 0)
+// RAW:         call <vscale x 2 x i32> @llvm.vector.insert.nxv2i32.v1i32(<vscale x 2 x i32> poison, <1 x i32> {{.*}}, i64 0)
+vint32m1_t widen_from_1(fixed_i32x1 x) { return x; }
+
+// Round-trip is a no-op after optimization: coerce in and out at the same
+// scalable type.
+// OPT-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @round_trip(
+// OPT-NEXT:  entry:
+// OPT-NEXT:    ret <vscale x 2 x i32> %x.coerce
+fixed_i32x2 round_trip(fixed_i32x2 x) {
+  vint32m1_t s = x;
+  return s;
+}
+
+// Cross-call: passing a shorter fixed type to a sizeless callee needs no
+// register-group change at all, since both are LMUL 1.
+// OPT-LABEL: define dso_local {{.*}}<vscale x 2 x i32> @pass_short(
+// OPT-NEXT:  entry:
+// OPT-NEXT:    [[C:%.*]] = {{.*}}call <vscale x 2 x i32> @sizeless_callee(<vscale x 2 x i32> %x.coerce)
+// OPT-NEXT:    ret <vscale x 2 x i32> [[C]]
+vint32m1_t pass_short(fixed_i32x2 x) { return sizeless_callee(x); }
+
+// Full-size case: unchanged from the strict path.
+// OPT-LABEL: define dso_local noundef <vscale x 2 x i32> @widen_full(
+// OPT-NEXT:  entry:
+// OPT-NEXT:    ret <vscale x 2 x i32> %x.coerce
+vint32m1_t widen_full(fixed_i32x4 x) { return x; }
diff --git a/clang/test/CodeGenCXX/riscv-mangle-rvv-fixed-vectors-lmul.cpp b/clang/test/CodeGenCXX/riscv-mangle-rvv-fixed-vectors-lmul.cpp
new file mode 100644
index 00000000000000..47890dd225ae78
--- /dev/null
+++ b/clang/test/CodeGenCXX/riscv-mangle-rvv-fixed-vectors-lmul.cpp
@@ -0,0 +1,39 @@
+// Verify that the LMUL suffix in the mangled name of a riscv_rvv_vector_bits
+// typedef comes from the type the typedef was written against, not from VLEN.
+// Two typedefs holding the same number of lanes of the same element type but
+// derived from different LMULs must mangle
+// differently, and - because the suffix no longer depends on VLEN - the same
+// source mangles identically at zvl128b and zvl256b.
+
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu %s -emit-llvm -o - \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -target-feature +zvl128b \
+// RUN:   | FileCheck %s
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu %s -emit-llvm -o - \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -target-feature +zvl256b \
+// RUN:   | FileCheck %s
+
+// REQUIRES: riscv-registered-target
+
+#include <riscv_vector.h>
+
+// All three hold 2 i32 lanes but come from different LMULs.
+typedef vint32mf2_t mf2_64 __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m1_t  m1_64  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m2_t  m2_64  __attribute__((riscv_rvv_vector_bits(64)));
+
+// Overloading on the three types only compiles if they are distinct types.
+void f(mf2_64);
+void f(m1_64);
+void f(m2_64);
+
+void use(mf2_64 a, m1_64 b, m2_64 c) {
+  f(a);
+  f(b);
+  f(c);
+}
+
+// CHECK: declare {{.*}}@_Z1f9__RVV_VLSIu16__rvv_int32mf2_tLj64EE
+// CHECK: declare {{.*}}@_Z1f9__RVV_VLSIu15__rvv_int32m1_tLj64EE
+// CHECK: declare {{.*}}@_Z1f9__RVV_VLSIu15__rvv_int32m2_tLj64EE
diff --git a/clang/test/Driver/riscv-rvv-vector-bits.c b/clang/test/Driver/riscv-rvv-vector-bits.c
index 24af5f0c73c6ef..33fe0798716171 100644
--- a/clang/test/Driver/riscv-rvv-vector-bits.c
+++ b/clang/test/Driver/riscv-rvv-vector-bits.c
@@ -44,23 +44,26 @@
 
 // CHECK-BAD-VALUE-ERROR: error: unsupported argument '{{.*}}' to option '-mrvv-vector-bits='
 
-// Error if using attribute without -mrvv-vector-bits=<bits> or if using -mrvv-vector-bits=<bits>+ syntax
+// Error if using attribute without -mrvv-vector-bits=<bits> or with
+// -mrvv-vector-bits=scalable when the ISA string does not guarantee a VLEN of
+// at least 64. Otherwise the minimum VLEN from the ISA string is enough.
 // -----------------------------------------------------------------------------
 // RUN: not %clang -c %s -o /dev/null -target riscv64-linux-gnu \
-// RUN:  -march=rv64gc_zve64x 2>&1 | FileCheck --check-prefix=CHECK-NO-FLAG-ERROR %s
+// RUN:  -march=rv64gc_zve32x 2>&1 | FileCheck --check-prefix=CHECK-NO-FLAG-ERROR %s
 // RUN: not %clang -c %s -o /dev/null -target riscv64-linux-gnu \
-// RUN:  -march=rv64gc_zve64x -mrvv-vector-bits=scalable 2>&1 | FileCheck --check-prefix=CHECK-NO-FLAG-ERROR %s
+// RUN:  -march=rv64gc_zve32x -mrvv-vector-bits=scalable 2>&1 | FileCheck --check-prefix=CHECK-NO-FLAG-ERROR %s
 
 typedef __rvv_int32m1_t vint32m1_t;
-typedef vint32m1_t noflag __attribute__((riscv_rvv_vector_bits(256)));
+typedef __rvv_bool32_t vbool32_t;
+typedef vbool32_t noflag __attribute__((riscv_rvv_vector_bits(4)));
 
 // CHECK-NO-FLAG-ERROR: error: 'riscv_rvv_vector_bits' is only supported when '-mrvv-vector-bits=<bits>' is specified with a value of "zvl" or a power 2 in the range [64,65536]
 
-// Error if attribute vector size != -mrvv-vector-bits
+// Error if attribute vector size > -mrvv-vector-bits
 // -----------------------------------------------------------------------------
 // RUN: not %clang -c %s -o /dev/null -target riscv64-linux-gnu \
 // RUN:  -march=rv64gc_zve64x -mrvv-vector-bits=128 2>&1 | FileCheck --check-prefix=CHECK-BAD-VECTOR-SIZE-ERROR %s
 
 typedef vint32_t bad_vector_size __attribute__((riscv_rvv_vector_bits(256)));
 
-// CHECK-BAD-VECTOR-SIZE-ERROR: error: invalid RVV vector size '256', expected size is '128' based on LMUL of type and '-mrvv-vector-bits'
+// CHECK-BAD-VECTOR-SIZE-ERROR: error: invalid RVV vector size '256', must not exceed '128' based on LMUL of type and the minimum VLEN
diff --git a/clang/test/Sema/attr-riscv-rvv-vector-bits-lmul.c b/clang/test/Sema/attr-riscv-rvv-vector-bits-lmul.c
new file mode 100644
index 00000000000000..1516f0e3344aae
--- /dev/null
+++ b/clang/test/Sema/attr-riscv-rvv-vector-bits-lmul.c
@@ -0,0 +1,125 @@
+// Verify that a riscv_rvv_vector_bits typedef keeps the LMUL of the sizeless
+// RVV type it was written against. Two typedefs can hold the same number of lanes of the same element type while
+// coming from different LMULs, and they must stay distinct types.
+
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -ffreestanding -fsyntax-only -verify=expected -mvscale-min=2 \
+// RUN:   -mvscale-max=2 %s
+
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -ffreestanding -fsyntax-only -verify=expected,nolax -mvscale-min=2 \
+// RUN:   -mvscale-max=2 \
+// RUN:   -flax-vector-conversions=none %s
+
+// REQUIRES: riscv-registered-target
+
+#include <riscv_vector.h>
+
+// VLEN=128: vint32m1_t holds 4 i32 lanes at full size, vint32mf2_t holds 2,
+// vint32m2_t holds 8. All three typedefs below hold 2 i32 lanes.
+typedef vint32m1_t  m1_64  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32mf2_t mf2_64 __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m2_t  m2_64  __attribute__((riscv_rvv_vector_bits(64)));
+
+//===----------------------------------------------------------------------===//
+// Same element type and lane count, different LMUL => distinct types.
+//
+// They are still the same size, so a lax vector conversion allows a direct
+// assignment between them as a reinterpret, exactly as it does for GNU
+// vectors. -flax-vector-conversions defaults to "all", so this is on unless
+// asked otherwise; turning it off shows that the types really are distinct.
+//===----------------------------------------------------------------------===//
+
+void distinct_types(void) {
+  m1_64 a;
+  mf2_64 b;
+  m2_64 c;
+
+  a = b; // nolax-error {{assigning to 'm1_64' (vector of 2 'int' values) from incompatible type 'mf2_64'}}
+  b = a; // nolax-error {{assigning to 'mf2_64' (vector of 2 'int' values) from incompatible type 'm1_64'}}
+  a = c; // nolax-error {{assigning to 'm1_64' (vector of 2 'int' values) from incompatible type 'm2_64'}}
+}
+
+//===----------------------------------------------------------------------===//
+// Conversion to and from the sizeless type is only allowed for the matching
+// LMUL. This goes through areCompatibleRVVTypes, so it is rejected regardless
+// of the lax vector conversion setting.
+//===----------------------------------------------------------------------===//
+
+void to_sizeless(void) {
+  m1_64 a;
+
+  vint32m1_t ok = a;
+  (void)ok;
+
+  vint32mf2_t narrower = a; // expected-error {{initializing 'vint32mf2_t' (aka '__rvv_int32mf2_t') with an expression of incompatible type 'm1_64'}}
+  (void)narrower;
+
+  vint32m2_t wider = a; // expected-error {{initializing 'vint32m2_t' (aka '__rvv_int32m2_t') with an expression of incompatible type 'm1_64'}}
+  (void)wider;
+}
+
+void from_sizeless(vint32m1_t v1, vint32mf2_t vh, vint32m2_t v2) {
+  m1_64 ok = v1;
+  (void)ok;
+
+  m1_64 bad_narrower = vh; // expected-error {{with an expression of incompatible type 'vint32mf2_t' (aka '__rvv_int32mf2_t')}}
+  (void)bad_narrower;
+
+  m1_64 bad_wider = v2; // expected-error {{with an expression of incompatible type 'vint32m2_t' (aka '__rvv_int32m2_t')}}
+  (void)bad_wider;
+}
+
+//===----------------------------------------------------------------------===//
+// Calling RVV intrinsics: only the intrinsic of the matching LMUL accepts a
+// shorter typedef.
+//===----------------------------------------------------------------------===//
+
+void intrinsics_m1(m1_64 a, m1_64 b) {
+  // Matching LMUL: fine.
+  m1_64 sum = __riscv_vadd_vv_i32m1(a, b, 2);
+  (void)sum;
+
+  // Fewer lanes than the typedef's LMUL provides.
+  (void)__riscv_vadd_vv_i32mf2(a, b, 2); // expected-error {{passing 'm1_64' (vector of 2 'int' values) to parameter of incompatible type '__rvv_int32mf2_t'}} \
+                                         // expected-note {{passing argument to parameter here}}
+
+  // More lanes than the typedef's LMUL provides.
+  (void)__riscv_vadd_vv_i32m2(a, b, 2); // expected-error {{passing 'm1_64' (vector of 2 'int' values) to parameter of incompatible type '__rvv_int32m2_t'}} \
+                                        // expected-note {{passing argument to parameter here}}
+}
+
+void intrinsics_mf2(mf2_64 a, mf2_64 b) {
+  mf2_64 sum = __riscv_vadd_vv_i32mf2(a, b, 2);
+  (void)sum;
+
+  (void)__riscv_vadd_vv_i32m1(a, b, 2); // expected-error {{passing 'mf2_64' (vector of 2 'int' values) to parameter of incompatible type '__rvv_int32m1_t'}} \
+                                        // expected-note {{passing argument to parameter here}}
+}
+
+// Loads and stores of the matching LMUL are fine.
+void intrinsics_mem(const int *p, int *q) {
+  m1_64 v = __riscv_vle32_v_i32m1(p, 2);
+  __riscv_vse32_v_i32m1(q, v, 2);
+}
+
+//===----------------------------------------------------------------------===//
+// A full-size typedef behaves the same way: LMUL comes from the typedef, not
+// from the lane count.
+//===----------------------------------------------------------------------===//
+
+typedef vint32m1_t m1_128 __attribute__((riscv_rvv_vector_bits(128)));
+
+// The mf2 type cannot even hold 128 bits at this VLEN.
+typedef vint32mf2_t mf2_128 __attribute__((riscv_rvv_vector_bits(128))); // expected-error {{invalid RVV vector size '128', must not exceed '64' based on LMUL of type and the minimum VLEN}}
+
+void full_size(void) {
+  m1_128 a;
+  vint32m1_t ok = a;
+  (void)ok;
+
+  vint32m2_t bad = a; // expected-error {{initializing 'vint32m2_t' (aka '__rvv_int32m2_t') with an expression of incompatible type 'm1_128'}}
+  (void)bad;
+}
diff --git a/clang/test/Sema/attr-riscv-rvv-vector-bits-no-flag.c b/clang/test/Sema/attr-riscv-rvv-vector-bits-no-flag.c
new file mode 100644
index 00000000000000..807a83f68eb25d
--- /dev/null
+++ b/clang/test/Sema/attr-riscv-rvv-vector-bits-no-flag.c
@@ -0,0 +1,80 @@
+// Tests for riscv_rvv_vector_bits when no fixed VLEN is supplied (i.e. no
+// -mrvv-vector-bits=). The bound for riscv_rvv_vector_bits(N) on
+// a data type then comes from the ISA's Zvl*b extension, scaled by LMUL.
+//
+// Bound: N <= MinElts * EltSize * zvl / RVVBitsPerBlock = zvl * LMUL (bits)
+//
+// vintXm1_t  -> bound = zvl
+// vintXm2_t  -> bound = zvl * 2
+// vintXmf2_t -> bound = zvl / 2
+//
+// Mask types still need -mrvv-vector-bits=.
+
+// zvl128b: ISA minimum VLEN = 128.
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -target-feature +zvl128b \
+// RUN:   -ffreestanding -fsyntax-only -verify \
+// RUN:   %s
+
+// zvl256b: ISA minimum VLEN = 256.
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -target-feature +zvl256b \
+// RUN:   -ffreestanding -fsyntax-only -verify \
+// RUN:   -DZVL256 %s
+
+typedef __rvv_int32m1_t  vint32m1_t;
+typedef __rvv_int32m2_t  vint32m2_t;
+typedef __rvv_int32mf2_t vint32mf2_t;
+typedef __rvv_bool32_t   vbool32_t;
+
+#if defined(ZVL256)
+
+// zvl256b: m1 bound is 256.
+typedef vint32m1_t  m1_eq  __attribute__((riscv_rvv_vector_bits(256)));
+typedef vint32m1_t  m1_lt  __attribute__((riscv_rvv_vector_bits(128)));
+typedef vint32m1_t  m1_too __attribute__((riscv_rvv_vector_bits(512)));
+// expected-error at -1 {{invalid RVV vector size '512', must not exceed '256' based on LMUL of type and the minimum VLEN}}
+
+// zvl256b: m2 bound is 512.
+typedef vint32m2_t  m2_eq  __attribute__((riscv_rvv_vector_bits(512)));
+typedef vint32m2_t  m2_lt  __attribute__((riscv_rvv_vector_bits(256)));
+typedef vint32m2_t  m2_too __attribute__((riscv_rvv_vector_bits(1024)));
+// expected-error at -1 {{invalid RVV vector size '1024', must not exceed '512' based on LMUL of type and the minimum VLEN}}
+
+// zvl256b: mf2 bound is 128.
+typedef vint32mf2_t mf2_eq  __attribute__((riscv_rvv_vector_bits(128)));
+typedef vint32mf2_t mf2_lt  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32mf2_t mf2_too __attribute__((riscv_rvv_vector_bits(256)));
+// expected-error at -1 {{invalid RVV vector size '256', must not exceed '128' based on LMUL of type and the minimum VLEN}}
+
+#else // zvl128b
+
+// zvl128b: m1 bound is 128.
+typedef vint32m1_t  m1_eq  __attribute__((riscv_rvv_vector_bits(128)));
+typedef vint32m1_t  m1_lt  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m1_t  m1_min __attribute__((riscv_rvv_vector_bits(32)));
+typedef vint32m1_t  m1_too __attribute__((riscv_rvv_vector_bits(256)));
+// expected-error at -1 {{invalid RVV vector size '256', must not exceed '128' based on LMUL of type and the minimum VLEN}}
+
+// zvl128b: m2 bound is 256.
+typedef vint32m2_t  m2_eq  __attribute__((riscv_rvv_vector_bits(256)));
+typedef vint32m2_t  m2_lt  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m2_t  m2_too __attribute__((riscv_rvv_vector_bits(512)));
+// expected-error at -1 {{invalid RVV vector size '512', must not exceed '256' based on LMUL of type and the minimum VLEN}}
+
+// zvl128b: mf2 bound is 64.
+typedef vint32mf2_t mf2_eq  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32mf2_t mf2_lt  __attribute__((riscv_rvv_vector_bits(32)));
+typedef vint32mf2_t mf2_too __attribute__((riscv_rvv_vector_bits(128)));
+// expected-error at -1 {{invalid RVV vector size '128', must not exceed '64' based on LMUL of type and the minimum VLEN}}
+
+// Lane count must be a power of two.
+typedef vint32m1_t m1_npow2 __attribute__((riscv_rvv_vector_bits(96)));
+// expected-error at -1 {{invalid RVV vector size '96', the resulting element count '3' must be a power of 2}}
+
+typedef vbool32_t mask __attribute__((riscv_rvv_vector_bits(4)));
+// expected-error at -1 {{'riscv_rvv_vector_bits' is only supported when '-mrvv-vector-bits=<bits>' is specified with a value of "zvl" or a power 2 in the range [64,65536]}}
+
+#endif
diff --git a/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed.c b/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed.c
new file mode 100644
index 00000000000000..d1d9776bab8699
--- /dev/null
+++ b/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed.c
@@ -0,0 +1,67 @@
+// VLEN=128: vint32m1_t has 4 elems at full size, vint8m1_t has 16 elems.
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -target-feature +zvl128b \
+// RUN:   -ffreestanding -fsyntax-only -verify \
+// RUN:   %s
+
+// VLEN=512: vint32m1_t has 16 elems at full size.
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -target-feature +zvl512b \
+// RUN:   -ffreestanding -fsyntax-only -verify \
+// RUN:   -DVLEN512 %s
+
+typedef __rvv_int32m1_t vint32m1_t;
+typedef __rvv_int8m1_t vint8m1_t;
+typedef __rvv_bool32_t vbool32_t;
+
+#if !defined(VLEN512) // VLEN=128
+
+// Equal-size case still works (compat).
+typedef vint32m1_t v_eq_i32 __attribute__((riscv_rvv_vector_bits(128)));
+
+// Shorter sizes (power of 2) are accepted.
+typedef vint32m1_t v_i32x2 __attribute__((riscv_rvv_vector_bits(64)));   // 2 elems
+typedef vint32m1_t v_i32x1 __attribute__((riscv_rvv_vector_bits(32)));   // 1 elem
+typedef vint8m1_t  v_i8x8  __attribute__((riscv_rvv_vector_bits(64)));   // 8 elems
+typedef vint8m1_t  v_i8x4  __attribute__((riscv_rvv_vector_bits(32)));   // 4 elems
+
+// Reject N > vlen*lmul.
+typedef vint32m1_t too_big __attribute__((riscv_rvv_vector_bits(256)));
+// expected-error at -1 {{invalid RVV vector size '256', must not exceed '128' based on LMUL of type and the minimum VLEN}}
+
+// Reject element count that is not a power of 2.
+typedef vint32m1_t three_elems __attribute__((riscv_rvv_vector_bits(96)));
+// expected-error at -1 {{invalid RVV vector size '96', the resulting element count '3' must be a power of 2}}
+
+// Reject sizes that aren't a multiple of the element size.
+typedef vint32m1_t odd_bits __attribute__((riscv_rvv_vector_bits(40)));
+// expected-error at -1 {{invalid RVV vector size '40', must be a positive multiple of the element size '32'}}
+
+// Reject zero.
+typedef vint32m1_t zero_bits __attribute__((riscv_rvv_vector_bits(0)));
+// expected-error at -1 {{invalid RVV vector size '0', must be a positive multiple of the element size '32'}}
+
+// Masks must still match exactly, counted in lanes: vbool32_t has 4 lanes.
+typedef vbool32_t mask_full __attribute__((riscv_rvv_vector_bits(4)));
+typedef vbool32_t mask_shorter __attribute__((riscv_rvv_vector_bits(2)));
+// expected-error at -1 {{invalid RVV vector size '2', expected size is '4' based on LMUL of type and '-mrvv-vector-bits'}}
+
+#else // VLEN=512
+
+typedef vint32m1_t v_full   __attribute__((riscv_rvv_vector_bits(512)));
+typedef vint32m1_t v_i32x8  __attribute__((riscv_rvv_vector_bits(256)));
+typedef vint32m1_t v_i32x4  __attribute__((riscv_rvv_vector_bits(128)));
+typedef vint32m1_t v_i32x2  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m1_t v_i32x1  __attribute__((riscv_rvv_vector_bits(32)));
+
+// Still reject N > vlen*lmul.
+typedef vint32m1_t too_big __attribute__((riscv_rvv_vector_bits(1024)));
+// expected-error at -1 {{invalid RVV vector size '1024', must not exceed '512' based on LMUL of type and the minimum VLEN}}
+
+// Reject non-pow2 element count (5 * 32 = 160).
+typedef vint32m1_t five_elems __attribute__((riscv_rvv_vector_bits(160)));
+// expected-error at -1 {{invalid RVV vector size '160', the resulting element count '5' must be a power of 2}}
+
+#endif

>From c977e3f680bceb2680f2e4d95f9ceecd0db52e5e Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Mon, 2 Nov 2026 19:32:44 +0800
Subject: [PATCH 3/9] fixup! [Clang][RISCV] Allow riscv_rvv_vector_bits
 typedefs shorter than VLEN*LMUL

---
 .../attr-riscv-rvv-vector-bits-relaxed-mask.c | 12 +++++++
 ...r-riscv-rvv-vector-bits-relaxed-zvl128b.c} | 36 +------------------
 ...tr-riscv-rvv-vector-bits-relaxed-zvl512b.c | 21 +++++++++++
 3 files changed, 34 insertions(+), 35 deletions(-)
 create mode 100644 clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-mask.c
 rename clang/test/Sema/{attr-riscv-rvv-vector-bits-relaxed.c => attr-riscv-rvv-vector-bits-relaxed-zvl128b.c} (51%)
 create mode 100644 clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-zvl512b.c

diff --git a/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-mask.c b/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-mask.c
new file mode 100644
index 00000000000000..774caa8c2688e6
--- /dev/null
+++ b/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-mask.c
@@ -0,0 +1,12 @@
+// Mask types still need a fixed VLEN.
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -mvscale-min=2 -mvscale-max=2 \
+// RUN:   -ffreestanding -fsyntax-only -verify %s
+
+typedef __rvv_bool32_t vbool32_t;
+
+// Masks must still match exactly, counted in lanes: vbool32_t has 4 lanes.
+typedef vbool32_t mask_full __attribute__((riscv_rvv_vector_bits(4)));
+typedef vbool32_t mask_shorter __attribute__((riscv_rvv_vector_bits(2)));
+// expected-error at -1 {{invalid RVV vector size '2', expected size is '4' based on LMUL of type and '-mrvv-vector-bits'}}
diff --git a/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed.c b/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-zvl128b.c
similarity index 51%
rename from clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed.c
rename to clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-zvl128b.c
index d1d9776bab8699..7ca487401b5e95 100644
--- a/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed.c
+++ b/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-zvl128b.c
@@ -2,21 +2,10 @@
 // RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
 // RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
 // RUN:   -target-feature +zvl128b \
-// RUN:   -ffreestanding -fsyntax-only -verify \
-// RUN:   %s
-
-// VLEN=512: vint32m1_t has 16 elems at full size.
-// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
-// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
-// RUN:   -target-feature +zvl512b \
-// RUN:   -ffreestanding -fsyntax-only -verify \
-// RUN:   -DVLEN512 %s
+// RUN:   -ffreestanding -fsyntax-only -verify %s
 
 typedef __rvv_int32m1_t vint32m1_t;
 typedef __rvv_int8m1_t vint8m1_t;
-typedef __rvv_bool32_t vbool32_t;
-
-#if !defined(VLEN512) // VLEN=128
 
 // Equal-size case still works (compat).
 typedef vint32m1_t v_eq_i32 __attribute__((riscv_rvv_vector_bits(128)));
@@ -42,26 +31,3 @@ typedef vint32m1_t odd_bits __attribute__((riscv_rvv_vector_bits(40)));
 // Reject zero.
 typedef vint32m1_t zero_bits __attribute__((riscv_rvv_vector_bits(0)));
 // expected-error at -1 {{invalid RVV vector size '0', must be a positive multiple of the element size '32'}}
-
-// Masks must still match exactly, counted in lanes: vbool32_t has 4 lanes.
-typedef vbool32_t mask_full __attribute__((riscv_rvv_vector_bits(4)));
-typedef vbool32_t mask_shorter __attribute__((riscv_rvv_vector_bits(2)));
-// expected-error at -1 {{invalid RVV vector size '2', expected size is '4' based on LMUL of type and '-mrvv-vector-bits'}}
-
-#else // VLEN=512
-
-typedef vint32m1_t v_full   __attribute__((riscv_rvv_vector_bits(512)));
-typedef vint32m1_t v_i32x8  __attribute__((riscv_rvv_vector_bits(256)));
-typedef vint32m1_t v_i32x4  __attribute__((riscv_rvv_vector_bits(128)));
-typedef vint32m1_t v_i32x2  __attribute__((riscv_rvv_vector_bits(64)));
-typedef vint32m1_t v_i32x1  __attribute__((riscv_rvv_vector_bits(32)));
-
-// Still reject N > vlen*lmul.
-typedef vint32m1_t too_big __attribute__((riscv_rvv_vector_bits(1024)));
-// expected-error at -1 {{invalid RVV vector size '1024', must not exceed '512' based on LMUL of type and the minimum VLEN}}
-
-// Reject non-pow2 element count (5 * 32 = 160).
-typedef vint32m1_t five_elems __attribute__((riscv_rvv_vector_bits(160)));
-// expected-error at -1 {{invalid RVV vector size '160', the resulting element count '5' must be a power of 2}}
-
-#endif
diff --git a/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-zvl512b.c b/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-zvl512b.c
new file mode 100644
index 00000000000000..7697a648830e9b
--- /dev/null
+++ b/clang/test/Sema/attr-riscv-rvv-vector-bits-relaxed-zvl512b.c
@@ -0,0 +1,21 @@
+// VLEN=512: vint32m1_t has 16 elems at full size.
+// RUN: %clang_cc1 -triple riscv64-none-linux-gnu \
+// RUN:   -target-feature +f -target-feature +d -target-feature +zve64d \
+// RUN:   -target-feature +zvl512b \
+// RUN:   -ffreestanding -fsyntax-only -verify %s
+
+typedef __rvv_int32m1_t vint32m1_t;
+
+typedef vint32m1_t v_full   __attribute__((riscv_rvv_vector_bits(512)));
+typedef vint32m1_t v_i32x8  __attribute__((riscv_rvv_vector_bits(256)));
+typedef vint32m1_t v_i32x4  __attribute__((riscv_rvv_vector_bits(128)));
+typedef vint32m1_t v_i32x2  __attribute__((riscv_rvv_vector_bits(64)));
+typedef vint32m1_t v_i32x1  __attribute__((riscv_rvv_vector_bits(32)));
+
+// Still reject N > vlen*lmul.
+typedef vint32m1_t too_big __attribute__((riscv_rvv_vector_bits(1024)));
+// expected-error at -1 {{invalid RVV vector size '1024', must not exceed '512' based on LMUL of type and the minimum VLEN}}
+
+// Reject non-pow2 element count (5 * 32 = 160).
+typedef vint32m1_t five_elems __attribute__((riscv_rvv_vector_bits(160)));
+// expected-error at -1 {{invalid RVV vector size '160', the resulting element count '5' must be a power of 2}}

>From b403710c1b536f5736a8a597941b83cb88196a3b Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Tue, 3 Nov 2026 09:23:59 +0800
Subject: [PATCH 4/9] fixup! [Clang][RISCV] Record the LMUL in RVV fixed-length
 data vector kinds

Record the ratio in RVV fixed-length mask vector kinds too. Replace
RVVFixedLengthMask{,_1,_2,_4} with RVVFixedLengthMaskRatio<R> and
RVVFixedLengthMaskRatio<R>_{1,2,4}, and read R from the kind instead of
re-deriving it from VLEN. VecKind grows to 6 bits.

Assisted-by: Claude
---
 clang/include/clang/AST/TypeBase.h            | 217 +++++++++++++++++-
 clang/lib/AST/ASTContext.cpp                  |  44 +---
 clang/lib/AST/ItaniumMangle.cpp               |  45 +---
 clang/lib/AST/JSONNodeDumper.cpp              |  33 ++-
 clang/lib/AST/TextNodeDumper.cpp              |  25 +-
 clang/lib/AST/TypePrinter.cpp                 |  73 ++----
 clang/lib/CodeGen/Targets/RISCV.cpp           |  61 ++---
 clang/lib/Sema/SemaExpr.cpp                   |  13 +-
 clang/lib/Sema/SemaType.cpp                   |  26 +--
 ...ump-riscv-rvv-fixed-length-vector-kinds.c} |  20 +-
 10 files changed, 335 insertions(+), 222 deletions(-)
 rename clang/test/AST/{ast-dump-riscv-rvv-fixed-length-data-types.c => ast-dump-riscv-rvv-fixed-length-vector-kinds.c} (64%)

diff --git a/clang/include/clang/AST/TypeBase.h b/clang/include/clang/AST/TypeBase.h
index 09b381b512d0d4..4f4f4b8a22d63c 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -2133,7 +2133,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
     /// The kind of vector, either a generic vector type or some
     /// target-specific vector type such as for AltiVec or Neon.
     LLVM_PREFERRED_TYPE(VectorKind)
-    unsigned VecKind : 5;
+    unsigned VecKind : 6;
     /// The number of elements in the vector.
     uint32_t NumElements;
   };
@@ -4263,12 +4263,36 @@ enum class VectorKind {
   RVVFixedLengthData_M4,
   RVVFixedLengthData_M8,
 
-  /// is RISC-V RVV fixed-length mask vector
-  RVVFixedLengthMask,
-
-  RVVFixedLengthMask_1,
-  RVVFixedLengthMask_2,
-  RVVFixedLengthMask_4
+  /// is RISC-V RVV fixed-length mask vector of vbool<R>_t; the _<L> kinds
+  /// have only L lanes in a single byte
+  RVVFixedLengthMaskRatio1,
+  RVVFixedLengthMaskRatio1_1,
+  RVVFixedLengthMaskRatio1_2,
+  RVVFixedLengthMaskRatio1_4,
+  RVVFixedLengthMaskRatio2,
+  RVVFixedLengthMaskRatio2_1,
+  RVVFixedLengthMaskRatio2_2,
+  RVVFixedLengthMaskRatio2_4,
+  RVVFixedLengthMaskRatio4,
+  RVVFixedLengthMaskRatio4_1,
+  RVVFixedLengthMaskRatio4_2,
+  RVVFixedLengthMaskRatio4_4,
+  RVVFixedLengthMaskRatio8,
+  RVVFixedLengthMaskRatio8_1,
+  RVVFixedLengthMaskRatio8_2,
+  RVVFixedLengthMaskRatio8_4,
+  RVVFixedLengthMaskRatio16,
+  RVVFixedLengthMaskRatio16_1,
+  RVVFixedLengthMaskRatio16_2,
+  RVVFixedLengthMaskRatio16_4,
+  RVVFixedLengthMaskRatio32,
+  RVVFixedLengthMaskRatio32_1,
+  RVVFixedLengthMaskRatio32_2,
+  RVVFixedLengthMaskRatio32_4,
+  RVVFixedLengthMaskRatio64,
+  RVVFixedLengthMaskRatio64_1,
+  RVVFixedLengthMaskRatio64_2,
+  RVVFixedLengthMaskRatio64_4
 };
 
 /// True if \p VK is one of the RVV fixed-length data vector kinds, i.e. a
@@ -4356,6 +4380,178 @@ inline VectorKind getRVVFixedLengthDataVectorKind(int LMulLog2) {
   }
 }
 
+/// True if \p VK is one of the RVV fixed-length mask vector kinds, i.e. a
+/// riscv_rvv_vector_bits typedef of a vbool<R>_t type.
+inline bool isRVVFixedLengthMaskVectorKind(VectorKind VK) {
+  switch (VK) {
+  case VectorKind::RVVFixedLengthMaskRatio1:
+  case VectorKind::RVVFixedLengthMaskRatio1_1:
+  case VectorKind::RVVFixedLengthMaskRatio1_2:
+  case VectorKind::RVVFixedLengthMaskRatio1_4:
+  case VectorKind::RVVFixedLengthMaskRatio2:
+  case VectorKind::RVVFixedLengthMaskRatio2_1:
+  case VectorKind::RVVFixedLengthMaskRatio2_2:
+  case VectorKind::RVVFixedLengthMaskRatio2_4:
+  case VectorKind::RVVFixedLengthMaskRatio4:
+  case VectorKind::RVVFixedLengthMaskRatio4_1:
+  case VectorKind::RVVFixedLengthMaskRatio4_2:
+  case VectorKind::RVVFixedLengthMaskRatio4_4:
+  case VectorKind::RVVFixedLengthMaskRatio8:
+  case VectorKind::RVVFixedLengthMaskRatio8_1:
+  case VectorKind::RVVFixedLengthMaskRatio8_2:
+  case VectorKind::RVVFixedLengthMaskRatio8_4:
+  case VectorKind::RVVFixedLengthMaskRatio16:
+  case VectorKind::RVVFixedLengthMaskRatio16_1:
+  case VectorKind::RVVFixedLengthMaskRatio16_2:
+  case VectorKind::RVVFixedLengthMaskRatio16_4:
+  case VectorKind::RVVFixedLengthMaskRatio32:
+  case VectorKind::RVVFixedLengthMaskRatio32_1:
+  case VectorKind::RVVFixedLengthMaskRatio32_2:
+  case VectorKind::RVVFixedLengthMaskRatio32_4:
+  case VectorKind::RVVFixedLengthMaskRatio64:
+  case VectorKind::RVVFixedLengthMaskRatio64_1:
+  case VectorKind::RVVFixedLengthMaskRatio64_2:
+  case VectorKind::RVVFixedLengthMaskRatio64_4:
+    return true;
+  default:
+    return false;
+  }
+}
+
+/// Return log2 of the R of the vbool<R>_t type recorded in an RVV fixed-length
+/// mask vector kind: Ratio1 -> 0, Ratio2 -> 1, ..., Ratio64 -> 6.
+inline unsigned getRVVFixedLengthMaskRatioLog2(VectorKind VK) {
+  switch (VK) {
+  case VectorKind::RVVFixedLengthMaskRatio1:
+  case VectorKind::RVVFixedLengthMaskRatio1_1:
+  case VectorKind::RVVFixedLengthMaskRatio1_2:
+  case VectorKind::RVVFixedLengthMaskRatio1_4:
+    return 0;
+  case VectorKind::RVVFixedLengthMaskRatio2:
+  case VectorKind::RVVFixedLengthMaskRatio2_1:
+  case VectorKind::RVVFixedLengthMaskRatio2_2:
+  case VectorKind::RVVFixedLengthMaskRatio2_4:
+    return 1;
+  case VectorKind::RVVFixedLengthMaskRatio4:
+  case VectorKind::RVVFixedLengthMaskRatio4_1:
+  case VectorKind::RVVFixedLengthMaskRatio4_2:
+  case VectorKind::RVVFixedLengthMaskRatio4_4:
+    return 2;
+  case VectorKind::RVVFixedLengthMaskRatio8:
+  case VectorKind::RVVFixedLengthMaskRatio8_1:
+  case VectorKind::RVVFixedLengthMaskRatio8_2:
+  case VectorKind::RVVFixedLengthMaskRatio8_4:
+    return 3;
+  case VectorKind::RVVFixedLengthMaskRatio16:
+  case VectorKind::RVVFixedLengthMaskRatio16_1:
+  case VectorKind::RVVFixedLengthMaskRatio16_2:
+  case VectorKind::RVVFixedLengthMaskRatio16_4:
+    return 4;
+  case VectorKind::RVVFixedLengthMaskRatio32:
+  case VectorKind::RVVFixedLengthMaskRatio32_1:
+  case VectorKind::RVVFixedLengthMaskRatio32_2:
+  case VectorKind::RVVFixedLengthMaskRatio32_4:
+    return 5;
+  case VectorKind::RVVFixedLengthMaskRatio64:
+  case VectorKind::RVVFixedLengthMaskRatio64_1:
+  case VectorKind::RVVFixedLengthMaskRatio64_2:
+  case VectorKind::RVVFixedLengthMaskRatio64_4:
+    return 6;
+  default:
+    llvm_unreachable("not an RVV fixed-length mask vector kind");
+  }
+}
+
+/// Return the number of lanes (1, 2 or 4) of an RVV fixed-length mask vector
+/// kind that fits in less than one byte, or 0 if its lanes fill whole bytes.
+inline unsigned getRVVFixedLengthMaskSubByteLanes(VectorKind VK) {
+  switch (VK) {
+  case VectorKind::RVVFixedLengthMaskRatio1:
+  case VectorKind::RVVFixedLengthMaskRatio2:
+  case VectorKind::RVVFixedLengthMaskRatio4:
+  case VectorKind::RVVFixedLengthMaskRatio8:
+  case VectorKind::RVVFixedLengthMaskRatio16:
+  case VectorKind::RVVFixedLengthMaskRatio32:
+  case VectorKind::RVVFixedLengthMaskRatio64:
+    return 0;
+  case VectorKind::RVVFixedLengthMaskRatio1_1:
+  case VectorKind::RVVFixedLengthMaskRatio2_1:
+  case VectorKind::RVVFixedLengthMaskRatio4_1:
+  case VectorKind::RVVFixedLengthMaskRatio8_1:
+  case VectorKind::RVVFixedLengthMaskRatio16_1:
+  case VectorKind::RVVFixedLengthMaskRatio32_1:
+  case VectorKind::RVVFixedLengthMaskRatio64_1:
+    return 1;
+  case VectorKind::RVVFixedLengthMaskRatio1_2:
+  case VectorKind::RVVFixedLengthMaskRatio2_2:
+  case VectorKind::RVVFixedLengthMaskRatio4_2:
+  case VectorKind::RVVFixedLengthMaskRatio8_2:
+  case VectorKind::RVVFixedLengthMaskRatio16_2:
+  case VectorKind::RVVFixedLengthMaskRatio32_2:
+  case VectorKind::RVVFixedLengthMaskRatio64_2:
+    return 2;
+  case VectorKind::RVVFixedLengthMaskRatio1_4:
+  case VectorKind::RVVFixedLengthMaskRatio2_4:
+  case VectorKind::RVVFixedLengthMaskRatio4_4:
+  case VectorKind::RVVFixedLengthMaskRatio8_4:
+  case VectorKind::RVVFixedLengthMaskRatio16_4:
+  case VectorKind::RVVFixedLengthMaskRatio32_4:
+  case VectorKind::RVVFixedLengthMaskRatio64_4:
+    return 4;
+  default:
+    llvm_unreachable("not an RVV fixed-length mask vector kind");
+  }
+}
+
+/// Inverse of getRVVFixedLengthMaskRatioLog2() and
+/// getRVVFixedLengthMaskSubByteLanes().
+inline VectorKind getRVVFixedLengthMaskVectorKind(unsigned RatioLog2,
+                                                  unsigned SubByteLanes) {
+  static constexpr VectorKind Kinds[7][4] = {
+      {VectorKind::RVVFixedLengthMaskRatio1,
+       VectorKind::RVVFixedLengthMaskRatio1_1,
+       VectorKind::RVVFixedLengthMaskRatio1_2,
+       VectorKind::RVVFixedLengthMaskRatio1_4},
+      {VectorKind::RVVFixedLengthMaskRatio2,
+       VectorKind::RVVFixedLengthMaskRatio2_1,
+       VectorKind::RVVFixedLengthMaskRatio2_2,
+       VectorKind::RVVFixedLengthMaskRatio2_4},
+      {VectorKind::RVVFixedLengthMaskRatio4,
+       VectorKind::RVVFixedLengthMaskRatio4_1,
+       VectorKind::RVVFixedLengthMaskRatio4_2,
+       VectorKind::RVVFixedLengthMaskRatio4_4},
+      {VectorKind::RVVFixedLengthMaskRatio8,
+       VectorKind::RVVFixedLengthMaskRatio8_1,
+       VectorKind::RVVFixedLengthMaskRatio8_2,
+       VectorKind::RVVFixedLengthMaskRatio8_4},
+      {VectorKind::RVVFixedLengthMaskRatio16,
+       VectorKind::RVVFixedLengthMaskRatio16_1,
+       VectorKind::RVVFixedLengthMaskRatio16_2,
+       VectorKind::RVVFixedLengthMaskRatio16_4},
+      {VectorKind::RVVFixedLengthMaskRatio32,
+       VectorKind::RVVFixedLengthMaskRatio32_1,
+       VectorKind::RVVFixedLengthMaskRatio32_2,
+       VectorKind::RVVFixedLengthMaskRatio32_4},
+      {VectorKind::RVVFixedLengthMaskRatio64,
+       VectorKind::RVVFixedLengthMaskRatio64_1,
+       VectorKind::RVVFixedLengthMaskRatio64_2,
+       VectorKind::RVVFixedLengthMaskRatio64_4},
+  };
+  assert(RatioLog2 < 7 && "ratio out of range for an RVV mask type");
+  switch (SubByteLanes) {
+  case 0:
+    return Kinds[RatioLog2][0];
+  case 1:
+    return Kinds[RatioLog2][1];
+  case 2:
+    return Kinds[RatioLog2][2];
+  case 4:
+    return Kinds[RatioLog2][3];
+  default:
+    llvm_unreachable("unexpected sub-byte lane count");
+  }
+}
+
 /// Represents a GCC generic vector type. This type is created using
 /// __attribute__((vector_size(n)), where "n" specifies the vector size in
 /// bytes; or from an Altivec __vector or vector declaration.
@@ -4385,6 +4581,13 @@ class VectorType : public Type, public llvm::FoldingSetNode {
     return VectorKind(VectorTypeBits.VecKind);
   }
 
+  /// Return the number of lanes of an RVV fixed-length mask vector. Its
+  /// elements are bytes, but each lane is a single bit.
+  unsigned getRVVFixedLengthMaskLanes() const {
+    unsigned SubByteLanes = getRVVFixedLengthMaskSubByteLanes(getVectorKind());
+    return SubByteLanes ? SubByteLanes : getNumElements() * 8;
+  }
+
   void Profile(llvm::FoldingSetNodeID &ID) {
     Profile(ID, getElementType(), getNumElements(),
             getTypeClass(), getVectorKind());
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 6afc80a2b908bb..4a2bb929be42eb 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -91,6 +91,7 @@
 #include "llvm/Support/SipHash.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TargetParser/AArch64TargetParser.h"
+#include "llvm/TargetParser/RISCVTargetParser.h"
 #include "llvm/TargetParser/Triple.h"
 #include <algorithm>
 #include <cassert>
@@ -2156,10 +2157,7 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
       // Adjust the alignment for fixed-length SVE predicates.
       Align = 16;
     else if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
-             VT->getVectorKind() == VectorKind::RVVFixedLengthMask ||
-             VT->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
-             VT->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
-             VT->getVectorKind() == VectorKind::RVVFixedLengthMask_4)
+             isRVVFixedLengthMaskVectorKind(VT->getVectorKind()))
       // Adjust the alignment for fixed-length RVV vectors.
       Align = std::min<unsigned>(64, Width);
     // Vector types have their natural alignment resist reduction by
@@ -10773,14 +10771,8 @@ bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
       Second->getVectorKind() != VectorKind::SveFixedLengthPredicate &&
       !isRVVFixedLengthDataVectorKind(First->getVectorKind()) &&
       !isRVVFixedLengthDataVectorKind(Second->getVectorKind()) &&
-      First->getVectorKind() != VectorKind::RVVFixedLengthMask &&
-      Second->getVectorKind() != VectorKind::RVVFixedLengthMask &&
-      First->getVectorKind() != VectorKind::RVVFixedLengthMask_1 &&
-      Second->getVectorKind() != VectorKind::RVVFixedLengthMask_1 &&
-      First->getVectorKind() != VectorKind::RVVFixedLengthMask_2 &&
-      Second->getVectorKind() != VectorKind::RVVFixedLengthMask_2 &&
-      First->getVectorKind() != VectorKind::RVVFixedLengthMask_4 &&
-      Second->getVectorKind() != VectorKind::RVVFixedLengthMask_4)
+      !isRVVFixedLengthMaskVectorKind(First->getVectorKind()) &&
+      !isRVVFixedLengthMaskVectorKind(Second->getVectorKind()))
     return true;
 
   // In OpenCL, treat half and _Float16 vector types as compatible.
@@ -10860,29 +10852,17 @@ bool ASTContext::areCompatibleRVVTypes(QualType FirstType,
   auto IsValidCast = [this](QualType FirstType, QualType SecondType) {
     if (const auto *BT = FirstType->getAs<BuiltinType>()) {
       if (const auto *VT = SecondType->getAs<VectorType>()) {
-        if (VT->getVectorKind() == VectorKind::RVVFixedLengthMask) {
+        if (isRVVFixedLengthMaskVectorKind(VT->getVectorKind())) {
           BuiltinVectorTypeInfo Info = getBuiltinVectorTypeInfo(BT);
+          // A mask typedef converts to and from the vbool<R>_t type it was
+          // written against: vbool<R>_t has RVVBitsPerBlock / R lanes at
+          // vscale=1.
           return FirstType->isRVVVLSBuiltinType() &&
                  Info.ElementType == BoolTy &&
-                 getTypeSize(SecondType) == ((getRVVTypeSize(*this, BT)));
-        }
-        if (VT->getVectorKind() == VectorKind::RVVFixedLengthMask_1) {
-          BuiltinVectorTypeInfo Info = getBuiltinVectorTypeInfo(BT);
-          return FirstType->isRVVVLSBuiltinType() &&
-                 Info.ElementType == BoolTy &&
-                 getTypeSize(SecondType) == ((getRVVTypeSize(*this, BT) * 8));
-        }
-        if (VT->getVectorKind() == VectorKind::RVVFixedLengthMask_2) {
-          BuiltinVectorTypeInfo Info = getBuiltinVectorTypeInfo(BT);
-          return FirstType->isRVVVLSBuiltinType() &&
-                 Info.ElementType == BoolTy &&
-                 getTypeSize(SecondType) == ((getRVVTypeSize(*this, BT)) * 4);
-        }
-        if (VT->getVectorKind() == VectorKind::RVVFixedLengthMask_4) {
-          BuiltinVectorTypeInfo Info = getBuiltinVectorTypeInfo(BT);
-          return FirstType->isRVVVLSBuiltinType() &&
-                 Info.ElementType == BoolTy &&
-                 getTypeSize(SecondType) == ((getRVVTypeSize(*this, BT)) * 2);
+                 (llvm::RISCV::RVVBitsPerBlock >>
+                  getRVVFixedLengthMaskRatioLog2(VT->getVectorKind())) ==
+                     Info.EC.getKnownMinValue() &&
+                 VT->getRVVFixedLengthMaskLanes() == getRVVTypeSize(*this, BT);
         }
         if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
             VT->getVectorKind() == VectorKind::Generic)
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 8fd9059abcdf3d..6f24eb0a829aec 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -35,7 +35,6 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/TargetParser/RISCVTargetParser.h"
 #include <optional>
 
 using namespace clang;
@@ -4317,10 +4316,7 @@ void CXXNameMangler::mangleAArch64FixedSveVectorType(
 
 void CXXNameMangler::mangleRISCVFixedRVVVectorType(const VectorType *T) {
   assert((isRVVFixedLengthDataVectorKind(T->getVectorKind()) ||
-          T->getVectorKind() == VectorKind::RVVFixedLengthMask ||
-          T->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
-          T->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
-          T->getVectorKind() == VectorKind::RVVFixedLengthMask_4) &&
+          isRVVFixedLengthMaskVectorKind(T->getVectorKind())) &&
          "expected fixed-length RVV vector!");
 
   QualType EltType = T->getElementType();
@@ -4376,36 +4372,20 @@ void CXXNameMangler::mangleRISCVFixedRVVVectorType(const VectorType *T) {
     llvm_unreachable("unexpected element type for fixed-length RVV vector!");
   }
 
-  unsigned VecSizeInBits;
-  switch (T->getVectorKind()) {
-  case VectorKind::RVVFixedLengthMask_1:
-    VecSizeInBits = 1;
-    break;
-  case VectorKind::RVVFixedLengthMask_2:
-    VecSizeInBits = 2;
-    break;
-  case VectorKind::RVVFixedLengthMask_4:
-    VecSizeInBits = 4;
-    break;
-  default:
-    VecSizeInBits = getASTContext().getTypeInfo(T).Width;
-    break;
-  }
+  // A mask typedef is mangled with its lane count, which is less than its
+  // size in bits when it fits in a single byte.
+  unsigned VecSizeInBits = isRVVFixedLengthMaskVectorKind(T->getVectorKind())
+                               ? T->getRVVFixedLengthMaskLanes()
+                               : getASTContext().getTypeInfo(T).Width;
 
-  // Append the LMUL suffix. For data types the LMUL is recorded in the vector
-  // kind, so the mangled name names the type the typedef was written against
-  // even when the typedef is shorter than VLEN * LMUL, and does not depend on
-  // VLEN at all. Mask types are still tied to a fixed VLEN, so their ratio is
-  // derived from it.
+  // Append the LMUL suffix of a data type, or the ratio of a mask type. Both
+  // are recorded in the vector kind, so the mangled name names the type the
+  // typedef was written against and does not depend on VLEN.
   if (isRVVFixedLengthDataVectorKind(T->getVectorKind())) {
     TypeNameOS << getRVVFixedLengthDataLMulString(T->getVectorKind(),
                                                   /*MF=*/true);
   } else {
-    auto VScale = getASTContext().getTargetInfo().getVScaleRange(
-        getASTContext().getLangOpts(),
-        TargetInfo::ArmStreamingKind::NotStreaming);
-    unsigned VLen = VScale->first * llvm::RISCV::RVVBitsPerBlock;
-    TypeNameOS << (VLen / VecSizeInBits);
+    TypeNameOS << (1u << getRVVFixedLengthMaskRatioLog2(T->getVectorKind()));
   }
   TypeNameOS << "_t";
 
@@ -4446,10 +4426,7 @@ void CXXNameMangler::mangleType(const VectorType *T) {
     mangleAArch64FixedSveVectorType(T);
     return;
   } else if (isRVVFixedLengthDataVectorKind(T->getVectorKind()) ||
-             T->getVectorKind() == VectorKind::RVVFixedLengthMask ||
-             T->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
-             T->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
-             T->getVectorKind() == VectorKind::RVVFixedLengthMask_4) {
+             isRVVFixedLengthMaskVectorKind(T->getVectorKind())) {
     mangleRISCVFixedRVVVectorType(T);
     return;
   }
diff --git a/clang/lib/AST/JSONNodeDumper.cpp b/clang/lib/AST/JSONNodeDumper.cpp
index 2555c3658df02f..c7015feeb6e3ff 100644
--- a/clang/lib/AST/JSONNodeDumper.cpp
+++ b/clang/lib/AST/JSONNodeDumper.cpp
@@ -750,24 +750,21 @@ void JSONNodeDumper::VisitVectorType(const VectorType *VT) {
   case VectorKind::SveFixedLengthPredicate:
     JOS.attribute("vectorKind", "fixed-length sve predicate vector");
     break;
-  case VectorKind::RVVFixedLengthData_MF8:
-  case VectorKind::RVVFixedLengthData_MF4:
-  case VectorKind::RVVFixedLengthData_MF2:
-  case VectorKind::RVVFixedLengthData_M1:
-  case VectorKind::RVVFixedLengthData_M2:
-  case VectorKind::RVVFixedLengthData_M4:
-  case VectorKind::RVVFixedLengthData_M8:
-    JOS.attribute("vectorKind",
-                  "fixed-length rvv data vector lmul " +
-                      getRVVFixedLengthDataLMulString(VT->getVectorKind(),
-                                                      /*MF=*/false)
-                          .str());
-    break;
-  case VectorKind::RVVFixedLengthMask:
-  case VectorKind::RVVFixedLengthMask_1:
-  case VectorKind::RVVFixedLengthMask_2:
-  case VectorKind::RVVFixedLengthMask_4:
-    JOS.attribute("vectorKind", "fixed-length rvv mask vector");
+  default:
+    if (isRVVFixedLengthDataVectorKind(VT->getVectorKind())) {
+      JOS.attribute("vectorKind",
+                    "fixed-length rvv data vector lmul " +
+                        getRVVFixedLengthDataLMulString(VT->getVectorKind(),
+                                                        /*MF=*/false)
+                            .str());
+    } else {
+      assert(isRVVFixedLengthMaskVectorKind(VT->getVectorKind()) &&
+             "unexpected vector kind");
+      JOS.attribute("vectorKind",
+                    "fixed-length rvv mask vector ratio " +
+                        std::to_string(1u << getRVVFixedLengthMaskRatioLog2(
+                                           VT->getVectorKind())));
+    }
     break;
   }
 }
diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp
index f1b216d970c731..48c4043ad5cafa 100644
--- a/clang/lib/AST/TextNodeDumper.cpp
+++ b/clang/lib/AST/TextNodeDumper.cpp
@@ -2168,21 +2168,16 @@ void TextNodeDumper::VisitVectorType(const VectorType *T) {
   case VectorKind::SveFixedLengthPredicate:
     OS << " fixed-length sve predicate vector";
     break;
-  case VectorKind::RVVFixedLengthData_MF8:
-  case VectorKind::RVVFixedLengthData_MF4:
-  case VectorKind::RVVFixedLengthData_MF2:
-  case VectorKind::RVVFixedLengthData_M1:
-  case VectorKind::RVVFixedLengthData_M2:
-  case VectorKind::RVVFixedLengthData_M4:
-  case VectorKind::RVVFixedLengthData_M8:
-    OS << " fixed-length rvv data vector lmul "
-       << getRVVFixedLengthDataLMulString(T->getVectorKind(), /*MF=*/false);
-    break;
-  case VectorKind::RVVFixedLengthMask:
-  case VectorKind::RVVFixedLengthMask_1:
-  case VectorKind::RVVFixedLengthMask_2:
-  case VectorKind::RVVFixedLengthMask_4:
-    OS << " fixed-length rvv mask vector";
+  default:
+    if (isRVVFixedLengthDataVectorKind(T->getVectorKind())) {
+      OS << " fixed-length rvv data vector lmul "
+         << getRVVFixedLengthDataLMulString(T->getVectorKind(), /*MF=*/false);
+    } else {
+      assert(isRVVFixedLengthMaskVectorKind(T->getVectorKind()) &&
+             "unexpected vector kind");
+      OS << " fixed-length rvv mask vector ratio "
+         << (1u << getRVVFixedLengthMaskRatioLog2(T->getVectorKind()));
+    }
     break;
   }
   OS << " " << T->getNumElements();
diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index 12cce9ccc32a55..b438b41b3f8f73 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -718,37 +718,23 @@ void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) {
     OS << ") * 8))) ";
     printBefore(T->getElementType(), OS);
     break;
-  case VectorKind::RVVFixedLengthData_MF8:
-  case VectorKind::RVVFixedLengthData_MF4:
-  case VectorKind::RVVFixedLengthData_MF2:
-  case VectorKind::RVVFixedLengthData_M1:
-  case VectorKind::RVVFixedLengthData_M2:
-  case VectorKind::RVVFixedLengthData_M4:
-  case VectorKind::RVVFixedLengthData_M8:
-  case VectorKind::RVVFixedLengthMask:
-  case VectorKind::RVVFixedLengthMask_1:
-  case VectorKind::RVVFixedLengthMask_2:
-  case VectorKind::RVVFixedLengthMask_4:
+  default:
+    // The RVV fixed-length data and mask kinds.
+    assert((isRVVFixedLengthDataVectorKind(T->getVectorKind()) ||
+            isRVVFixedLengthMaskVectorKind(T->getVectorKind())) &&
+           "unexpected vector kind");
     // FIXME: We prefer to print the size directly here, but have no way
     // to get the size of the type.
     OS << "__attribute__((__riscv_rvv_vector_bits__(";
-    switch (T->getVectorKind()) {
-    case VectorKind::RVVFixedLengthMask_1:
-      OS << '1';
-      break;
-    case VectorKind::RVVFixedLengthMask_2:
-      OS << '2';
-      break;
-    case VectorKind::RVVFixedLengthMask_4:
-      OS << '4';
-      break;
-    default:
+    if (isRVVFixedLengthMaskVectorKind(T->getVectorKind()) &&
+        getRVVFixedLengthMaskSubByteLanes(T->getVectorKind())) {
+      OS << getRVVFixedLengthMaskSubByteLanes(T->getVectorKind());
+    } else {
       OS << T->getNumElements();
       OS << " * sizeof(";
       print(T->getElementType(), OS, StringRef());
       // Multiply by 8 for the number of bits.
       OS << ") * 8";
-      break;
     }
     OS << "))) ";
     printBefore(T->getElementType(), OS);
@@ -819,39 +805,20 @@ void TypePrinter::printDependentVectorBefore(
     OS << "))) ";
     printBefore(T->getElementType(), OS);
     break;
-  case VectorKind::RVVFixedLengthData_MF8:
-  case VectorKind::RVVFixedLengthData_MF4:
-  case VectorKind::RVVFixedLengthData_MF2:
-  case VectorKind::RVVFixedLengthData_M1:
-  case VectorKind::RVVFixedLengthData_M2:
-  case VectorKind::RVVFixedLengthData_M4:
-  case VectorKind::RVVFixedLengthData_M8:
-  case VectorKind::RVVFixedLengthMask:
-  case VectorKind::RVVFixedLengthMask_1:
-  case VectorKind::RVVFixedLengthMask_2:
-  case VectorKind::RVVFixedLengthMask_4:
+  default:
+    // The RVV fixed-length data and mask kinds.
+    assert((isRVVFixedLengthDataVectorKind(T->getVectorKind()) ||
+            isRVVFixedLengthMaskVectorKind(T->getVectorKind())) &&
+           "unexpected vector kind");
     // FIXME: We prefer to print the size directly here, but have no way
     // to get the size of the type.
     OS << "__attribute__((__riscv_rvv_vector_bits__(";
-    switch (T->getVectorKind()) {
-    case VectorKind::RVVFixedLengthMask_1:
-      OS << '1';
-      break;
-    case VectorKind::RVVFixedLengthMask_2:
-      OS << '2';
-      break;
-    case VectorKind::RVVFixedLengthMask_4:
-      OS << '4';
-      break;
-    default:
-      if (T->getSizeExpr()) {
-        T->getSizeExpr()->printPretty(OS, nullptr, Policy);
-        OS << " * sizeof(";
-        print(T->getElementType(), OS, StringRef());
-        // Multiply by 8 for the number of bits.
-        OS << ") * 8";
-      }
-      break;
+    if (T->getSizeExpr()) {
+      T->getSizeExpr()->printPretty(OS, nullptr, Policy);
+      OS << " * sizeof(";
+      print(T->getElementType(), OS, StringRef());
+      // Multiply by 8 for the number of bits.
+      OS << ") * 8";
     }
     OS << "))) ";
     printBefore(T->getElementType(), OS);
diff --git a/clang/lib/CodeGen/Targets/RISCV.cpp b/clang/lib/CodeGen/Targets/RISCV.cpp
index c84a4f00f4bc7f..fb539e87591b67 100644
--- a/clang/lib/CodeGen/Targets/RISCV.cpp
+++ b/clang/lib/CodeGen/Targets/RISCV.cpp
@@ -428,17 +428,23 @@ static unsigned getRVVFixedLengthDataMinElts(VectorKind VK,
   return SizeAtVScale1 / EltSizeInBits;
 }
 
+/// Minimum element count of the scalable vector a vbool<R>_t mask is coerced
+/// to, i.e. the N in <vscale x N x i1>.
+static unsigned getRVVFixedLengthMaskMinElts(VectorKind VK) {
+  return llvm::RISCV::RVVBitsPerBlock >> getRVVFixedLengthMaskRatioLog2(VK);
+}
+
 /// Minimum element count of the scalable vector \p VT is coerced to, in units
-/// of \p VT's own Clang element type. Masks are modelled as i8 vectors and are
-/// exactly VLEN * LMUL wide, so their LMUL is recovered from VLEN.
+/// of \p VT's own Clang element type. Masks are modelled as i8 vectors, so
+/// their i1 lane count is rounded up to whole bytes.
 unsigned RISCVABIInfo::getRVVFixedLengthMinElts(const VectorType *VT) const {
   unsigned EltSizeInBits = getContext().getTypeSize(VT->getElementType());
   if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()))
     return getRVVFixedLengthDataMinElts(VT->getVectorKind(), EltSizeInBits);
-
-  auto VScale = getContext().getTargetInfo().getVScaleRange(
-      getContext().getLangOpts(), TargetInfo::ArmStreamingKind::NotStreaming);
-  return llvm::divideCeil(VT->getNumElements(), VScale->first);
+  assert(isRVVFixedLengthMaskVectorKind(VT->getVectorKind()) &&
+         "not an RVV fixed-length vector");
+  return llvm::divideCeil(getRVVFixedLengthMaskMinElts(VT->getVectorKind()),
+                          EltSizeInBits);
 }
 
 llvm::Type *RISCVABIInfo::detectVLSCCEligibleStruct(QualType Ty,
@@ -587,17 +593,8 @@ RISCVABIInfo::detectHomogeneousRVVFixedLengthStruct(QualType Ty) const {
     return nullptr;
 
   auto IsFixedLengthRVVVector = [](const VectorType *VT) {
-    if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()))
-      return true;
-    switch (VT->getVectorKind()) {
-    case VectorKind::RVVFixedLengthMask:
-    case VectorKind::RVVFixedLengthMask_1:
-    case VectorKind::RVVFixedLengthMask_2:
-    case VectorKind::RVVFixedLengthMask_4:
-      return true;
-    default:
-      return false;
-    }
+    return isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
+           isRVVFixedLengthMaskVectorKind(VT->getVectorKind());
   };
 
   QualType CommonTy;
@@ -688,24 +685,16 @@ ABIArgInfo RISCVABIInfo::coerceVLSVector(QualType Ty, unsigned ABIVLen) const {
   auto VScale = getContext().getTargetInfo().getVScaleRange(
       getContext().getLangOpts(), TargetInfo::ArmStreamingKind::NotStreaming);
 
-  unsigned NumElts = VT->getNumElements();
-  llvm::Type *EltType = llvm::Type::getInt1Ty(getVMContext());
-  switch (VT->getVectorKind()) {
-  case VectorKind::RVVFixedLengthMask_1:
-    break;
-  case VectorKind::RVVFixedLengthMask_2:
-    NumElts *= 2;
-    break;
-  case VectorKind::RVVFixedLengthMask_4:
-    NumElts *= 4;
-    break;
-  case VectorKind::RVVFixedLengthMask:
-    NumElts *= 8;
-    break;
-  default:
+  unsigned NumElts;
+  llvm::Type *EltType;
+  if (isRVVFixedLengthMaskVectorKind(VT->getVectorKind())) {
+    NumElts = VT->getRVVFixedLengthMaskLanes();
+    EltType = llvm::Type::getInt1Ty(getVMContext());
+  } else {
     assert((VT->getVectorKind() == VectorKind::Generic ||
             isRVVFixedLengthDataVectorKind(VT->getVectorKind())) &&
            "Unexpected vector kind");
+    NumElts = VT->getNumElements();
     EltType = CGT.ConvertType(VT->getElementType());
   }
 
@@ -717,6 +706,9 @@ ABIArgInfo RISCVABIInfo::coerceVLSVector(QualType Ty, unsigned ABIVLen) const {
       // Use the LMUL of the type the typedef was written against.
       MinScalableElts = getRVVFixedLengthDataMinElts(
           VT->getVectorKind(), EltType->getScalarSizeInBits());
+    } else if (isRVVFixedLengthMaskVectorKind(VT->getVectorKind())) {
+      // Use the ratio of the vbool<R>_t type the typedef was written against.
+      MinScalableElts = getRVVFixedLengthMaskMinElts(VT->getVectorKind());
     } else {
       // The MinNumElts is simplified from equation:
       // NumElts / VScale =
@@ -862,10 +854,7 @@ ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
   if (const VectorType *VT = Ty->getAs<VectorType>();
       VT && !VT->getElementType()->isBitIntType()) {
     if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
-        VT->getVectorKind() == VectorKind::RVVFixedLengthMask ||
-        VT->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
-        VT->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
-        VT->getVectorKind() == VectorKind::RVVFixedLengthMask_4)
+        isRVVFixedLengthMaskVectorKind(VT->getVectorKind()))
       return coerceVLSVector(Ty);
     if (VT->getVectorKind() == VectorKind::Generic && ABIVLen != 0)
       // Generic vector without riscv_vls_cc should fall through and pass by
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 9a11c859997eb6..00a2e23497aa87 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11062,10 +11062,7 @@ QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS,
           VecType->getVectorKind() == VectorKind::SveFixedLengthPredicate)
         return true;
       if (isRVVFixedLengthDataVectorKind(VecType->getVectorKind()) ||
-          VecType->getVectorKind() == VectorKind::RVVFixedLengthMask ||
-          VecType->getVectorKind() == VectorKind::RVVFixedLengthMask_1 ||
-          VecType->getVectorKind() == VectorKind::RVVFixedLengthMask_2 ||
-          VecType->getVectorKind() == VectorKind::RVVFixedLengthMask_4) {
+          isRVVFixedLengthMaskVectorKind(VecType->getVectorKind())) {
         SVEorRVV = 1;
         return true;
       }
@@ -11097,13 +11094,7 @@ QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS,
                 VectorKind::SveFixedLengthPredicate)
           return true;
         if (isRVVFixedLengthDataVectorKind(SecondVecType->getVectorKind()) ||
-            SecondVecType->getVectorKind() == VectorKind::RVVFixedLengthMask ||
-            SecondVecType->getVectorKind() ==
-                VectorKind::RVVFixedLengthMask_1 ||
-            SecondVecType->getVectorKind() ==
-                VectorKind::RVVFixedLengthMask_2 ||
-            SecondVecType->getVectorKind() ==
-                VectorKind::RVVFixedLengthMask_4) {
+            isRVVFixedLengthMaskVectorKind(SecondVecType->getVectorKind())) {
           SVEorRVV = 1;
           return true;
         }
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 9c299b9e55da8d..e16630ce9cd844 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -8871,28 +8871,26 @@ static void HandleRISCVRVVVectorBitsTypeAttr(QualType &CurType,
       S.Context.getBuiltinVectorTypeInfo(CurType->castAs<BuiltinType>());
   unsigned MinElts = Info.EC.getKnownMinValue();
 
-  VectorKind VecKind = VectorKind::RVVFixedLengthMask;
+  VectorKind VecKind;
   unsigned ExpectedSize = VScale->first * MinElts;
   QualType EltType = CurType->getRVVEltType(S.Context);
   unsigned EltSize = S.Context.getTypeSize(EltType);
   unsigned NumElts;
   if (Info.ElementType == S.Context.BoolTy) {
+    // A mask is stored in bytes. One with fewer than 8 lanes takes a single
+    // byte, and its lane count is recorded in the vector kind.
     NumElts = VecSize / S.Context.getCharWidth();
+    unsigned SubByteLanes = 0;
     if (!NumElts) {
       NumElts = 1;
-      switch (VecSize) {
-      case 1:
-        VecKind = VectorKind::RVVFixedLengthMask_1;
-        break;
-      case 2:
-        VecKind = VectorKind::RVVFixedLengthMask_2;
-        break;
-      case 4:
-        VecKind = VectorKind::RVVFixedLengthMask_4;
-        break;
-      }
-    } else
-      VecKind = VectorKind::RVVFixedLengthMask;
+      if (VecSize == 1 || VecSize == 2 || VecSize == 4)
+        SubByteLanes = VecSize;
+    }
+    // Record the ratio of the vbool<R>_t type in the vector kind, so that
+    // later stages do not have to re-derive it from VLEN and the lane count.
+    // vbool<R>_t has RVVBitsPerBlock / R lanes at vscale=1.
+    VecKind = getRVVFixedLengthMaskVectorKind(
+        llvm::Log2_32(llvm::RISCV::RVVBitsPerBlock / MinElts), SubByteLanes);
   } else {
     ExpectedSize *= EltSize;
     NumElts = VecSize / EltSize;
diff --git a/clang/test/AST/ast-dump-riscv-rvv-fixed-length-data-types.c b/clang/test/AST/ast-dump-riscv-rvv-fixed-length-vector-kinds.c
similarity index 64%
rename from clang/test/AST/ast-dump-riscv-rvv-fixed-length-data-types.c
rename to clang/test/AST/ast-dump-riscv-rvv-fixed-length-vector-kinds.c
index 4ba0ea48ba4240..e2ce3ac4d611e6 100644
--- a/clang/test/AST/ast-dump-riscv-rvv-fixed-length-data-types.c
+++ b/clang/test/AST/ast-dump-riscv-rvv-fixed-length-vector-kinds.c
@@ -1,5 +1,5 @@
-// Verify that a riscv_rvv_vector_bits typedef of a data type records the LMUL
-// of the sizeless type it was written against.
+// Verify that a riscv_rvv_vector_bits typedef records the LMUL of the sizeless
+// data type, or the ratio of the sizeless mask type, it was written against.
 
 // RUN: %clang_cc1 -triple riscv64-none-linux-gnu -target-feature +zve64d \
 // RUN:   -mvscale-min=2 -mvscale-max=2 -ast-dump %s | FileCheck %s
@@ -15,6 +15,10 @@ typedef __rvv_int32m1_t fixed_i32m1 __attribute__((riscv_rvv_vector_bits(128)));
 typedef __rvv_int32m2_t fixed_i32m2 __attribute__((riscv_rvv_vector_bits(256)));
 typedef __rvv_int64m4_t fixed_i64m4 __attribute__((riscv_rvv_vector_bits(512)));
 typedef __rvv_int8m8_t fixed_i8m8 __attribute__((riscv_rvv_vector_bits(1024)));
+typedef __rvv_bool1_t fixed_b1 __attribute__((riscv_rvv_vector_bits(128)));
+typedef __rvv_bool8_t fixed_b8 __attribute__((riscv_rvv_vector_bits(16)));
+typedef __rvv_bool32_t fixed_b32 __attribute__((riscv_rvv_vector_bits(4)));
+typedef __rvv_bool64_t fixed_b64 __attribute__((riscv_rvv_vector_bits(2)));
 
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_i8mf8
 // CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 1/8 2
@@ -30,6 +34,14 @@ typedef __rvv_int8m8_t fixed_i8m8 __attribute__((riscv_rvv_vector_bits(1024)));
 // CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 4 8
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_i8m8
 // CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 8 128
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_b1
+// CHECK:       VectorType {{.*}} fixed-length rvv mask vector ratio 1 16
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_b8
+// CHECK:       VectorType {{.*}} fixed-length rvv mask vector ratio 8 2
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_b32
+// CHECK:       VectorType {{.*}} fixed-length rvv mask vector ratio 32 1
+// CHECK-LABEL: TypedefDecl {{.*}} fixed_b64
+// CHECK:       VectorType {{.*}} fixed-length rvv mask vector ratio 64 1
 
 // JSON: "vectorKind": "fixed-length rvv data vector lmul 1/8"
 // JSON: "vectorKind": "fixed-length rvv data vector lmul 1/4"
@@ -38,3 +50,7 @@ typedef __rvv_int8m8_t fixed_i8m8 __attribute__((riscv_rvv_vector_bits(1024)));
 // JSON: "vectorKind": "fixed-length rvv data vector lmul 2"
 // JSON: "vectorKind": "fixed-length rvv data vector lmul 4"
 // JSON: "vectorKind": "fixed-length rvv data vector lmul 8"
+// JSON: "vectorKind": "fixed-length rvv mask vector ratio 1"
+// JSON: "vectorKind": "fixed-length rvv mask vector ratio 8"
+// JSON: "vectorKind": "fixed-length rvv mask vector ratio 32"
+// JSON: "vectorKind": "fixed-length rvv mask vector ratio 64"

>From 9dc58094ef772cd8b8842f5597076c2c9f1a9bda Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Tue, 3 Nov 2026 13:43:56 +0800
Subject: [PATCH 5/9] fixup! [Clang][RISCV] Record the LMUL in RVV fixed-length
 data vector kinds

---
 clang/include/clang/AST/TypeBase.h            | 20 ++++++-------
 clang/lib/AST/ItaniumMangle.cpp               |  3 +-
 clang/lib/AST/JSONNodeDumper.cpp              |  9 +++---
 clang/lib/AST/TextNodeDumper.cpp              |  2 +-
 ...dump-riscv-rvv-fixed-length-vector-kinds.c | 28 +++++++++----------
 5 files changed, 30 insertions(+), 32 deletions(-)

diff --git a/clang/include/clang/AST/TypeBase.h b/clang/include/clang/AST/TypeBase.h
index 4f4f4b8a22d63c..c9e64040aeefe1 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -4335,24 +4335,24 @@ inline int getRVVFixedLengthDataLMulLog2(VectorKind VK) {
   }
 }
 
-/// Return the LMUL of an RVV fixed-length data vector kind as a string:
-/// "mf8" .. "m8" if \p MF is true, "1/8" .. "8" otherwise.
-inline StringRef getRVVFixedLengthDataLMulString(VectorKind VK, bool MF) {
+/// Return the LMUL of an RVV fixed-length data vector kind as a string,
+/// "mf8" .. "m8".
+inline StringRef getRVVFixedLengthDataLMulString(VectorKind VK) {
   switch (VK) {
   case VectorKind::RVVFixedLengthData_MF8:
-    return MF ? "mf8" : "1/8";
+    return "mf8";
   case VectorKind::RVVFixedLengthData_MF4:
-    return MF ? "mf4" : "1/4";
+    return "mf4";
   case VectorKind::RVVFixedLengthData_MF2:
-    return MF ? "mf2" : "1/2";
+    return "mf2";
   case VectorKind::RVVFixedLengthData_M1:
-    return MF ? "m1" : "1";
+    return "m1";
   case VectorKind::RVVFixedLengthData_M2:
-    return MF ? "m2" : "2";
+    return "m2";
   case VectorKind::RVVFixedLengthData_M4:
-    return MF ? "m4" : "4";
+    return "m4";
   case VectorKind::RVVFixedLengthData_M8:
-    return MF ? "m8" : "8";
+    return "m8";
   default:
     llvm_unreachable("not an RVV fixed-length data vector kind");
   }
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 6f24eb0a829aec..edb7ca8e33d1e1 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -4382,8 +4382,7 @@ void CXXNameMangler::mangleRISCVFixedRVVVectorType(const VectorType *T) {
   // are recorded in the vector kind, so the mangled name names the type the
   // typedef was written against and does not depend on VLEN.
   if (isRVVFixedLengthDataVectorKind(T->getVectorKind())) {
-    TypeNameOS << getRVVFixedLengthDataLMulString(T->getVectorKind(),
-                                                  /*MF=*/true);
+    TypeNameOS << getRVVFixedLengthDataLMulString(T->getVectorKind());
   } else {
     TypeNameOS << (1u << getRVVFixedLengthMaskRatioLog2(T->getVectorKind()));
   }
diff --git a/clang/lib/AST/JSONNodeDumper.cpp b/clang/lib/AST/JSONNodeDumper.cpp
index c7015feeb6e3ff..cb0d8a6d3f4ea9 100644
--- a/clang/lib/AST/JSONNodeDumper.cpp
+++ b/clang/lib/AST/JSONNodeDumper.cpp
@@ -752,11 +752,10 @@ void JSONNodeDumper::VisitVectorType(const VectorType *VT) {
     break;
   default:
     if (isRVVFixedLengthDataVectorKind(VT->getVectorKind())) {
-      JOS.attribute("vectorKind",
-                    "fixed-length rvv data vector lmul " +
-                        getRVVFixedLengthDataLMulString(VT->getVectorKind(),
-                                                        /*MF=*/false)
-                            .str());
+      JOS.attribute(
+          "vectorKind",
+          "fixed-length rvv data vector lmul " +
+              getRVVFixedLengthDataLMulString(VT->getVectorKind()).str());
     } else {
       assert(isRVVFixedLengthMaskVectorKind(VT->getVectorKind()) &&
              "unexpected vector kind");
diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp
index 48c4043ad5cafa..7812bf16986195 100644
--- a/clang/lib/AST/TextNodeDumper.cpp
+++ b/clang/lib/AST/TextNodeDumper.cpp
@@ -2171,7 +2171,7 @@ void TextNodeDumper::VisitVectorType(const VectorType *T) {
   default:
     if (isRVVFixedLengthDataVectorKind(T->getVectorKind())) {
       OS << " fixed-length rvv data vector lmul "
-         << getRVVFixedLengthDataLMulString(T->getVectorKind(), /*MF=*/false);
+         << getRVVFixedLengthDataLMulString(T->getVectorKind());
     } else {
       assert(isRVVFixedLengthMaskVectorKind(T->getVectorKind()) &&
              "unexpected vector kind");
diff --git a/clang/test/AST/ast-dump-riscv-rvv-fixed-length-vector-kinds.c b/clang/test/AST/ast-dump-riscv-rvv-fixed-length-vector-kinds.c
index e2ce3ac4d611e6..aa48f3d8487b11 100644
--- a/clang/test/AST/ast-dump-riscv-rvv-fixed-length-vector-kinds.c
+++ b/clang/test/AST/ast-dump-riscv-rvv-fixed-length-vector-kinds.c
@@ -21,19 +21,19 @@ typedef __rvv_bool32_t fixed_b32 __attribute__((riscv_rvv_vector_bits(4)));
 typedef __rvv_bool64_t fixed_b64 __attribute__((riscv_rvv_vector_bits(2)));
 
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_i8mf8
-// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 1/8 2
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul mf8 2
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_i16mf4
-// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 1/4 2
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul mf4 2
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_i32mf2
-// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 1/2 2
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul mf2 2
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_i32m1
-// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 1 4
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul m1 4
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_i32m2
-// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 2 8
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul m2 8
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_i64m4
-// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 4 8
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul m4 8
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_i8m8
-// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul 8 128
+// CHECK:       VectorType {{.*}} fixed-length rvv data vector lmul m8 128
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_b1
 // CHECK:       VectorType {{.*}} fixed-length rvv mask vector ratio 1 16
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_b8
@@ -43,13 +43,13 @@ typedef __rvv_bool64_t fixed_b64 __attribute__((riscv_rvv_vector_bits(2)));
 // CHECK-LABEL: TypedefDecl {{.*}} fixed_b64
 // CHECK:       VectorType {{.*}} fixed-length rvv mask vector ratio 64 1
 
-// JSON: "vectorKind": "fixed-length rvv data vector lmul 1/8"
-// JSON: "vectorKind": "fixed-length rvv data vector lmul 1/4"
-// JSON: "vectorKind": "fixed-length rvv data vector lmul 1/2"
-// JSON: "vectorKind": "fixed-length rvv data vector lmul 1"
-// JSON: "vectorKind": "fixed-length rvv data vector lmul 2"
-// JSON: "vectorKind": "fixed-length rvv data vector lmul 4"
-// JSON: "vectorKind": "fixed-length rvv data vector lmul 8"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul mf8"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul mf4"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul mf2"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul m1"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul m2"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul m4"
+// JSON: "vectorKind": "fixed-length rvv data vector lmul m8"
 // JSON: "vectorKind": "fixed-length rvv mask vector ratio 1"
 // JSON: "vectorKind": "fixed-length rvv mask vector ratio 8"
 // JSON: "vectorKind": "fixed-length rvv mask vector ratio 32"

>From a95fe4904055fa3e3b3f3fc506732610596ec35f Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Tue, 3 Nov 2026 21:27:09 +0800
Subject: [PATCH 6/9] fixup! [Clang][RISCV] Record the LMUL in RVV fixed-length
 data vector kinds

---
 clang/include/clang/AST/TypeBase.h  | 55 +++++++++++++++++------------
 clang/lib/AST/ASTContext.cpp        |  9 ++---
 clang/lib/AST/ItaniumMangle.cpp     | 13 ++-----
 clang/lib/AST/JSONNodeDumper.cpp    | 20 ++++-------
 clang/lib/AST/TextNodeDumper.cpp    | 15 ++++----
 clang/lib/AST/TypePrinter.cpp       |  8 ++---
 clang/lib/CodeGen/Targets/RISCV.cpp |  6 ++--
 clang/lib/Sema/SemaExpr.cpp         |  6 ++--
 8 files changed, 57 insertions(+), 75 deletions(-)

diff --git a/clang/include/clang/AST/TypeBase.h b/clang/include/clang/AST/TypeBase.h
index c9e64040aeefe1..dc48b50cb43487 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -4335,29 +4335,6 @@ inline int getRVVFixedLengthDataLMulLog2(VectorKind VK) {
   }
 }
 
-/// Return the LMUL of an RVV fixed-length data vector kind as a string,
-/// "mf8" .. "m8".
-inline StringRef getRVVFixedLengthDataLMulString(VectorKind VK) {
-  switch (VK) {
-  case VectorKind::RVVFixedLengthData_MF8:
-    return "mf8";
-  case VectorKind::RVVFixedLengthData_MF4:
-    return "mf4";
-  case VectorKind::RVVFixedLengthData_MF2:
-    return "mf2";
-  case VectorKind::RVVFixedLengthData_M1:
-    return "m1";
-  case VectorKind::RVVFixedLengthData_M2:
-    return "m2";
-  case VectorKind::RVVFixedLengthData_M4:
-    return "m4";
-  case VectorKind::RVVFixedLengthData_M8:
-    return "m8";
-  default:
-    llvm_unreachable("not an RVV fixed-length data vector kind");
-  }
-}
-
 /// Inverse of getRVVFixedLengthDataLMulLog2().
 inline VectorKind getRVVFixedLengthDataVectorKind(int LMulLog2) {
   switch (LMulLog2) {
@@ -4462,6 +4439,38 @@ inline unsigned getRVVFixedLengthMaskRatioLog2(VectorKind VK) {
   }
 }
 
+/// True if \p VK is one of the RVV fixed-length data or mask vector kinds.
+inline bool isRVVFixedLengthVectorKind(VectorKind VK) {
+  return isRVVFixedLengthDataVectorKind(VK) ||
+         isRVVFixedLengthMaskVectorKind(VK);
+}
+
+/// Return the LMUL of an RVV fixed-length data vector kind, "mf8" .. "m8", or
+/// the ratio of a mask vector kind, "1" .. "64".
+inline StringRef getRVVFixedLengthString(VectorKind VK) {
+  switch (VK) {
+  case VectorKind::RVVFixedLengthData_MF8:
+    return "mf8";
+  case VectorKind::RVVFixedLengthData_MF4:
+    return "mf4";
+  case VectorKind::RVVFixedLengthData_MF2:
+    return "mf2";
+  case VectorKind::RVVFixedLengthData_M1:
+    return "m1";
+  case VectorKind::RVVFixedLengthData_M2:
+    return "m2";
+  case VectorKind::RVVFixedLengthData_M4:
+    return "m4";
+  case VectorKind::RVVFixedLengthData_M8:
+    return "m8";
+  default:
+    break;
+  }
+  static constexpr const char *Ratios[] = {"1",  "2",  "4", "8",
+                                           "16", "32", "64"};
+  return Ratios[getRVVFixedLengthMaskRatioLog2(VK)];
+}
+
 /// Return the number of lanes (1, 2 or 4) of an RVV fixed-length mask vector
 /// kind that fits in less than one byte, or 0 if its lanes fill whole bytes.
 inline unsigned getRVVFixedLengthMaskSubByteLanes(VectorKind VK) {
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 4a2bb929be42eb..338a0eaf8d9707 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2156,8 +2156,7 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
     else if (VT->getVectorKind() == VectorKind::SveFixedLengthPredicate)
       // Adjust the alignment for fixed-length SVE predicates.
       Align = 16;
-    else if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
-             isRVVFixedLengthMaskVectorKind(VT->getVectorKind()))
+    else if (isRVVFixedLengthVectorKind(VT->getVectorKind()))
       // Adjust the alignment for fixed-length RVV vectors.
       Align = std::min<unsigned>(64, Width);
     // Vector types have their natural alignment resist reduction by
@@ -10769,10 +10768,8 @@ bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
       First->getVectorKind() != VectorKind::SveFixedLengthPredicate &&
       Second->getVectorKind() != VectorKind::SveFixedLengthData &&
       Second->getVectorKind() != VectorKind::SveFixedLengthPredicate &&
-      !isRVVFixedLengthDataVectorKind(First->getVectorKind()) &&
-      !isRVVFixedLengthDataVectorKind(Second->getVectorKind()) &&
-      !isRVVFixedLengthMaskVectorKind(First->getVectorKind()) &&
-      !isRVVFixedLengthMaskVectorKind(Second->getVectorKind()))
+      !isRVVFixedLengthVectorKind(First->getVectorKind()) &&
+      !isRVVFixedLengthVectorKind(Second->getVectorKind()))
     return true;
 
   // In OpenCL, treat half and _Float16 vector types as compatible.
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index edb7ca8e33d1e1..e226ef1c079cf1 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -4315,8 +4315,7 @@ void CXXNameMangler::mangleAArch64FixedSveVectorType(
 }
 
 void CXXNameMangler::mangleRISCVFixedRVVVectorType(const VectorType *T) {
-  assert((isRVVFixedLengthDataVectorKind(T->getVectorKind()) ||
-          isRVVFixedLengthMaskVectorKind(T->getVectorKind())) &&
+  assert(isRVVFixedLengthVectorKind(T->getVectorKind()) &&
          "expected fixed-length RVV vector!");
 
   QualType EltType = T->getElementType();
@@ -4381,12 +4380,7 @@ void CXXNameMangler::mangleRISCVFixedRVVVectorType(const VectorType *T) {
   // Append the LMUL suffix of a data type, or the ratio of a mask type. Both
   // are recorded in the vector kind, so the mangled name names the type the
   // typedef was written against and does not depend on VLEN.
-  if (isRVVFixedLengthDataVectorKind(T->getVectorKind())) {
-    TypeNameOS << getRVVFixedLengthDataLMulString(T->getVectorKind());
-  } else {
-    TypeNameOS << (1u << getRVVFixedLengthMaskRatioLog2(T->getVectorKind()));
-  }
-  TypeNameOS << "_t";
+  TypeNameOS << getRVVFixedLengthString(T->getVectorKind()) << "_t";
 
   Out << "9__RVV_VLSI";
   mangleVendorType(TypeNameStr);
@@ -4424,8 +4418,7 @@ void CXXNameMangler::mangleType(const VectorType *T) {
              T->getVectorKind() == VectorKind::SveFixedLengthPredicate) {
     mangleAArch64FixedSveVectorType(T);
     return;
-  } else if (isRVVFixedLengthDataVectorKind(T->getVectorKind()) ||
-             isRVVFixedLengthMaskVectorKind(T->getVectorKind())) {
+  } else if (isRVVFixedLengthVectorKind(T->getVectorKind())) {
     mangleRISCVFixedRVVVectorType(T);
     return;
   }
diff --git a/clang/lib/AST/JSONNodeDumper.cpp b/clang/lib/AST/JSONNodeDumper.cpp
index cb0d8a6d3f4ea9..97d67914e02ddd 100644
--- a/clang/lib/AST/JSONNodeDumper.cpp
+++ b/clang/lib/AST/JSONNodeDumper.cpp
@@ -751,19 +751,13 @@ void JSONNodeDumper::VisitVectorType(const VectorType *VT) {
     JOS.attribute("vectorKind", "fixed-length sve predicate vector");
     break;
   default:
-    if (isRVVFixedLengthDataVectorKind(VT->getVectorKind())) {
-      JOS.attribute(
-          "vectorKind",
-          "fixed-length rvv data vector lmul " +
-              getRVVFixedLengthDataLMulString(VT->getVectorKind()).str());
-    } else {
-      assert(isRVVFixedLengthMaskVectorKind(VT->getVectorKind()) &&
-             "unexpected vector kind");
-      JOS.attribute("vectorKind",
-                    "fixed-length rvv mask vector ratio " +
-                        std::to_string(1u << getRVVFixedLengthMaskRatioLog2(
-                                           VT->getVectorKind())));
-    }
+    assert(isRVVFixedLengthVectorKind(VT->getVectorKind()) &&
+           "unexpected vector kind");
+    JOS.attribute("vectorKind",
+                  (isRVVFixedLengthMaskVectorKind(VT->getVectorKind())
+                       ? "fixed-length rvv mask vector ratio "
+                       : "fixed-length rvv data vector lmul ") +
+                      getRVVFixedLengthString(VT->getVectorKind()).str());
     break;
   }
 }
diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp
index 7812bf16986195..24af510ece2ad9 100644
--- a/clang/lib/AST/TextNodeDumper.cpp
+++ b/clang/lib/AST/TextNodeDumper.cpp
@@ -2169,15 +2169,12 @@ void TextNodeDumper::VisitVectorType(const VectorType *T) {
     OS << " fixed-length sve predicate vector";
     break;
   default:
-    if (isRVVFixedLengthDataVectorKind(T->getVectorKind())) {
-      OS << " fixed-length rvv data vector lmul "
-         << getRVVFixedLengthDataLMulString(T->getVectorKind());
-    } else {
-      assert(isRVVFixedLengthMaskVectorKind(T->getVectorKind()) &&
-             "unexpected vector kind");
-      OS << " fixed-length rvv mask vector ratio "
-         << (1u << getRVVFixedLengthMaskRatioLog2(T->getVectorKind()));
-    }
+    assert(isRVVFixedLengthVectorKind(T->getVectorKind()) &&
+           "unexpected vector kind");
+    OS << (isRVVFixedLengthMaskVectorKind(T->getVectorKind())
+               ? " fixed-length rvv mask vector ratio "
+               : " fixed-length rvv data vector lmul ")
+       << getRVVFixedLengthString(T->getVectorKind());
     break;
   }
   OS << " " << T->getNumElements();
diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index b438b41b3f8f73..a90b2d2c0cb94d 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -719,9 +719,7 @@ void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) {
     printBefore(T->getElementType(), OS);
     break;
   default:
-    // The RVV fixed-length data and mask kinds.
-    assert((isRVVFixedLengthDataVectorKind(T->getVectorKind()) ||
-            isRVVFixedLengthMaskVectorKind(T->getVectorKind())) &&
+    assert(isRVVFixedLengthVectorKind(T->getVectorKind()) &&
            "unexpected vector kind");
     // FIXME: We prefer to print the size directly here, but have no way
     // to get the size of the type.
@@ -806,9 +804,7 @@ void TypePrinter::printDependentVectorBefore(
     printBefore(T->getElementType(), OS);
     break;
   default:
-    // The RVV fixed-length data and mask kinds.
-    assert((isRVVFixedLengthDataVectorKind(T->getVectorKind()) ||
-            isRVVFixedLengthMaskVectorKind(T->getVectorKind())) &&
+    assert(isRVVFixedLengthVectorKind(T->getVectorKind()) &&
            "unexpected vector kind");
     // FIXME: We prefer to print the size directly here, but have no way
     // to get the size of the type.
diff --git a/clang/lib/CodeGen/Targets/RISCV.cpp b/clang/lib/CodeGen/Targets/RISCV.cpp
index fb539e87591b67..4a2e268d2bcb05 100644
--- a/clang/lib/CodeGen/Targets/RISCV.cpp
+++ b/clang/lib/CodeGen/Targets/RISCV.cpp
@@ -593,8 +593,7 @@ RISCVABIInfo::detectHomogeneousRVVFixedLengthStruct(QualType Ty) const {
     return nullptr;
 
   auto IsFixedLengthRVVVector = [](const VectorType *VT) {
-    return isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
-           isRVVFixedLengthMaskVectorKind(VT->getVectorKind());
+    return isRVVFixedLengthVectorKind(VT->getVectorKind());
   };
 
   QualType CommonTy;
@@ -853,8 +852,7 @@ ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
   // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/419
   if (const VectorType *VT = Ty->getAs<VectorType>();
       VT && !VT->getElementType()->isBitIntType()) {
-    if (isRVVFixedLengthDataVectorKind(VT->getVectorKind()) ||
-        isRVVFixedLengthMaskVectorKind(VT->getVectorKind()))
+    if (isRVVFixedLengthVectorKind(VT->getVectorKind()))
       return coerceVLSVector(Ty);
     if (VT->getVectorKind() == VectorKind::Generic && ABIVLen != 0)
       // Generic vector without riscv_vls_cc should fall through and pass by
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 00a2e23497aa87..cec5ae80358c46 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11061,8 +11061,7 @@ QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS,
       if (VecType->getVectorKind() == VectorKind::SveFixedLengthData ||
           VecType->getVectorKind() == VectorKind::SveFixedLengthPredicate)
         return true;
-      if (isRVVFixedLengthDataVectorKind(VecType->getVectorKind()) ||
-          isRVVFixedLengthMaskVectorKind(VecType->getVectorKind())) {
+      if (isRVVFixedLengthVectorKind(VecType->getVectorKind())) {
         SVEorRVV = 1;
         return true;
       }
@@ -11093,8 +11092,7 @@ QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS,
             SecondVecType->getVectorKind() ==
                 VectorKind::SveFixedLengthPredicate)
           return true;
-        if (isRVVFixedLengthDataVectorKind(SecondVecType->getVectorKind()) ||
-            isRVVFixedLengthMaskVectorKind(SecondVecType->getVectorKind())) {
+        if (isRVVFixedLengthVectorKind(SecondVecType->getVectorKind())) {
           SVEorRVV = 1;
           return true;
         }

>From 067f675ea3e4fd9e9800804a5e4f23036fe54b07 Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Tue, 3 Nov 2026 13:44:23 +0800
Subject: [PATCH 7/9] fixup! [Clang][RISCV] Allow riscv_rvv_vector_bits
 typedefs shorter than VLEN*LMUL

x
---
 clang/docs/ReleaseNotes.md                           | 2 +-
 clang/test/Sema/attr-riscv-rvv-vector-bits-no-flag.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index ed34bed7df8604..e23a33637386a9 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1328,7 +1328,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   CPU will be selected from `-mcpu`.
 
 - `riscv_rvv_vector_bits(N)` now accepts any `N` up to `VLEN*LMUL` bits for
-  data types. For them it no longer requires `-mrvv-vector-bits`; without it,
+  data types. For them, it no longer requires `-mrvv-vector-bits`; without it,
   `VLEN` is the minimum required by `-march`. A shorter typedef keeps the LMUL
   of the type it was written against, and converts implicitly to and from that
   sizeless type (truncating, or leaving the upper lanes undefined).
diff --git a/clang/test/Sema/attr-riscv-rvv-vector-bits-no-flag.c b/clang/test/Sema/attr-riscv-rvv-vector-bits-no-flag.c
index 807a83f68eb25d..72f90c6d6cfbc0 100644
--- a/clang/test/Sema/attr-riscv-rvv-vector-bits-no-flag.c
+++ b/clang/test/Sema/attr-riscv-rvv-vector-bits-no-flag.c
@@ -2,7 +2,7 @@
 // -mrvv-vector-bits=). The bound for riscv_rvv_vector_bits(N) on
 // a data type then comes from the ISA's Zvl*b extension, scaled by LMUL.
 //
-// Bound: N <= MinElts * EltSize * zvl / RVVBitsPerBlock = zvl * LMUL (bits)
+// Bound: N <= zvl * LMUL (bits)
 //
 // vintXm1_t  -> bound = zvl
 // vintXm2_t  -> bound = zvl * 2

>From e2500049d236d3c072a6c23430d7203b5fc730fd Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Wed, 4 Nov 2026 16:58:20 +0800
Subject: [PATCH 8/9] fixup! [Clang][RISCV] Record the LMUL in RVV fixed-length
 data vector kinds

---
 clang/include/clang/AST/TypeBase.h | 261 +++++------------------------
 1 file changed, 45 insertions(+), 216 deletions(-)

diff --git a/clang/include/clang/AST/TypeBase.h b/clang/include/clang/AST/TypeBase.h
index dc48b50cb43487..d1433e336685f5 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -4295,148 +4295,57 @@ enum class VectorKind {
   RVVFixedLengthMaskRatio64_4
 };
 
+// The helpers below rely on the order of the RVV fixed-length kinds:
+// RVVFixedLengthData_MF8 .. _M8 by LMUL, then RVVFixedLengthMaskRatio<R>,
+// <R>_1, <R>_2 and <R>_4 for R = 1 .. 64.
+static_assert(unsigned(VectorKind::RVVFixedLengthData_M1) ==
+                      unsigned(VectorKind::RVVFixedLengthData_MF8) + 3 &&
+                  unsigned(VectorKind::RVVFixedLengthData_M8) ==
+                      unsigned(VectorKind::RVVFixedLengthData_MF8) + 6,
+              "unexpected order of the RVV fixed-length data vector kinds");
+static_assert(unsigned(VectorKind::RVVFixedLengthMaskRatio1_4) ==
+                      unsigned(VectorKind::RVVFixedLengthMaskRatio1) + 3 &&
+                  unsigned(VectorKind::RVVFixedLengthMaskRatio2) ==
+                      unsigned(VectorKind::RVVFixedLengthMaskRatio1) + 4 &&
+                  unsigned(VectorKind::RVVFixedLengthMaskRatio64_4) ==
+                      unsigned(VectorKind::RVVFixedLengthMaskRatio1) + 27,
+              "unexpected order of the RVV fixed-length mask vector kinds");
+
 /// True if \p VK is one of the RVV fixed-length data vector kinds, i.e. a
 /// riscv_rvv_vector_bits typedef of a data (non-mask) RVV type.
 inline bool isRVVFixedLengthDataVectorKind(VectorKind VK) {
-  switch (VK) {
-  case VectorKind::RVVFixedLengthData_MF8:
-  case VectorKind::RVVFixedLengthData_MF4:
-  case VectorKind::RVVFixedLengthData_MF2:
-  case VectorKind::RVVFixedLengthData_M1:
-  case VectorKind::RVVFixedLengthData_M2:
-  case VectorKind::RVVFixedLengthData_M4:
-  case VectorKind::RVVFixedLengthData_M8:
-    return true;
-  default:
-    return false;
-  }
+  return VK >= VectorKind::RVVFixedLengthData_MF8 &&
+         VK <= VectorKind::RVVFixedLengthData_M8;
 }
 
 /// Return log2 of the LMUL recorded in an RVV fixed-length data vector kind:
-/// MF8 -> -3, MF4 -> -2, MF2 -> -1, M1 -> 0, M2 -> 1, M4 -> 2, M8 -> 3.
+/// MF8 -> -3, ..., M1 -> 0, ..., M8 -> 3.
 inline int getRVVFixedLengthDataLMulLog2(VectorKind VK) {
-  switch (VK) {
-  case VectorKind::RVVFixedLengthData_MF8:
-    return -3;
-  case VectorKind::RVVFixedLengthData_MF4:
-    return -2;
-  case VectorKind::RVVFixedLengthData_MF2:
-    return -1;
-  case VectorKind::RVVFixedLengthData_M1:
-    return 0;
-  case VectorKind::RVVFixedLengthData_M2:
-    return 1;
-  case VectorKind::RVVFixedLengthData_M4:
-    return 2;
-  case VectorKind::RVVFixedLengthData_M8:
-    return 3;
-  default:
-    llvm_unreachable("not an RVV fixed-length data vector kind");
-  }
+  assert(isRVVFixedLengthDataVectorKind(VK) &&
+         "not an RVV fixed-length data vector kind");
+  return int(VK) - int(VectorKind::RVVFixedLengthData_M1);
 }
 
 /// Inverse of getRVVFixedLengthDataLMulLog2().
 inline VectorKind getRVVFixedLengthDataVectorKind(int LMulLog2) {
-  switch (LMulLog2) {
-  case -3:
-    return VectorKind::RVVFixedLengthData_MF8;
-  case -2:
-    return VectorKind::RVVFixedLengthData_MF4;
-  case -1:
-    return VectorKind::RVVFixedLengthData_MF2;
-  case 0:
-    return VectorKind::RVVFixedLengthData_M1;
-  case 1:
-    return VectorKind::RVVFixedLengthData_M2;
-  case 2:
-    return VectorKind::RVVFixedLengthData_M4;
-  case 3:
-    return VectorKind::RVVFixedLengthData_M8;
-  default:
-    llvm_unreachable("LMUL out of range for an RVV type");
-  }
+  assert(LMulLog2 >= -3 && LMulLog2 <= 3 &&
+         "LMUL out of range for an RVV type");
+  return VectorKind(int(VectorKind::RVVFixedLengthData_M1) + LMulLog2);
 }
 
 /// True if \p VK is one of the RVV fixed-length mask vector kinds, i.e. a
 /// riscv_rvv_vector_bits typedef of a vbool<R>_t type.
 inline bool isRVVFixedLengthMaskVectorKind(VectorKind VK) {
-  switch (VK) {
-  case VectorKind::RVVFixedLengthMaskRatio1:
-  case VectorKind::RVVFixedLengthMaskRatio1_1:
-  case VectorKind::RVVFixedLengthMaskRatio1_2:
-  case VectorKind::RVVFixedLengthMaskRatio1_4:
-  case VectorKind::RVVFixedLengthMaskRatio2:
-  case VectorKind::RVVFixedLengthMaskRatio2_1:
-  case VectorKind::RVVFixedLengthMaskRatio2_2:
-  case VectorKind::RVVFixedLengthMaskRatio2_4:
-  case VectorKind::RVVFixedLengthMaskRatio4:
-  case VectorKind::RVVFixedLengthMaskRatio4_1:
-  case VectorKind::RVVFixedLengthMaskRatio4_2:
-  case VectorKind::RVVFixedLengthMaskRatio4_4:
-  case VectorKind::RVVFixedLengthMaskRatio8:
-  case VectorKind::RVVFixedLengthMaskRatio8_1:
-  case VectorKind::RVVFixedLengthMaskRatio8_2:
-  case VectorKind::RVVFixedLengthMaskRatio8_4:
-  case VectorKind::RVVFixedLengthMaskRatio16:
-  case VectorKind::RVVFixedLengthMaskRatio16_1:
-  case VectorKind::RVVFixedLengthMaskRatio16_2:
-  case VectorKind::RVVFixedLengthMaskRatio16_4:
-  case VectorKind::RVVFixedLengthMaskRatio32:
-  case VectorKind::RVVFixedLengthMaskRatio32_1:
-  case VectorKind::RVVFixedLengthMaskRatio32_2:
-  case VectorKind::RVVFixedLengthMaskRatio32_4:
-  case VectorKind::RVVFixedLengthMaskRatio64:
-  case VectorKind::RVVFixedLengthMaskRatio64_1:
-  case VectorKind::RVVFixedLengthMaskRatio64_2:
-  case VectorKind::RVVFixedLengthMaskRatio64_4:
-    return true;
-  default:
-    return false;
-  }
+  return VK >= VectorKind::RVVFixedLengthMaskRatio1 &&
+         VK <= VectorKind::RVVFixedLengthMaskRatio64_4;
 }
 
 /// Return log2 of the R of the vbool<R>_t type recorded in an RVV fixed-length
 /// mask vector kind: Ratio1 -> 0, Ratio2 -> 1, ..., Ratio64 -> 6.
 inline unsigned getRVVFixedLengthMaskRatioLog2(VectorKind VK) {
-  switch (VK) {
-  case VectorKind::RVVFixedLengthMaskRatio1:
-  case VectorKind::RVVFixedLengthMaskRatio1_1:
-  case VectorKind::RVVFixedLengthMaskRatio1_2:
-  case VectorKind::RVVFixedLengthMaskRatio1_4:
-    return 0;
-  case VectorKind::RVVFixedLengthMaskRatio2:
-  case VectorKind::RVVFixedLengthMaskRatio2_1:
-  case VectorKind::RVVFixedLengthMaskRatio2_2:
-  case VectorKind::RVVFixedLengthMaskRatio2_4:
-    return 1;
-  case VectorKind::RVVFixedLengthMaskRatio4:
-  case VectorKind::RVVFixedLengthMaskRatio4_1:
-  case VectorKind::RVVFixedLengthMaskRatio4_2:
-  case VectorKind::RVVFixedLengthMaskRatio4_4:
-    return 2;
-  case VectorKind::RVVFixedLengthMaskRatio8:
-  case VectorKind::RVVFixedLengthMaskRatio8_1:
-  case VectorKind::RVVFixedLengthMaskRatio8_2:
-  case VectorKind::RVVFixedLengthMaskRatio8_4:
-    return 3;
-  case VectorKind::RVVFixedLengthMaskRatio16:
-  case VectorKind::RVVFixedLengthMaskRatio16_1:
-  case VectorKind::RVVFixedLengthMaskRatio16_2:
-  case VectorKind::RVVFixedLengthMaskRatio16_4:
-    return 4;
-  case VectorKind::RVVFixedLengthMaskRatio32:
-  case VectorKind::RVVFixedLengthMaskRatio32_1:
-  case VectorKind::RVVFixedLengthMaskRatio32_2:
-  case VectorKind::RVVFixedLengthMaskRatio32_4:
-    return 5;
-  case VectorKind::RVVFixedLengthMaskRatio64:
-  case VectorKind::RVVFixedLengthMaskRatio64_1:
-  case VectorKind::RVVFixedLengthMaskRatio64_2:
-  case VectorKind::RVVFixedLengthMaskRatio64_4:
-    return 6;
-  default:
-    llvm_unreachable("not an RVV fixed-length mask vector kind");
-  }
+  assert(isRVVFixedLengthMaskVectorKind(VK) &&
+         "not an RVV fixed-length mask vector kind");
+  return (unsigned(VK) - unsigned(VectorKind::RVVFixedLengthMaskRatio1)) / 4;
 }
 
 /// True if \p VK is one of the RVV fixed-length data or mask vector kinds.
@@ -4448,23 +4357,10 @@ inline bool isRVVFixedLengthVectorKind(VectorKind VK) {
 /// Return the LMUL of an RVV fixed-length data vector kind, "mf8" .. "m8", or
 /// the ratio of a mask vector kind, "1" .. "64".
 inline StringRef getRVVFixedLengthString(VectorKind VK) {
-  switch (VK) {
-  case VectorKind::RVVFixedLengthData_MF8:
-    return "mf8";
-  case VectorKind::RVVFixedLengthData_MF4:
-    return "mf4";
-  case VectorKind::RVVFixedLengthData_MF2:
-    return "mf2";
-  case VectorKind::RVVFixedLengthData_M1:
-    return "m1";
-  case VectorKind::RVVFixedLengthData_M2:
-    return "m2";
-  case VectorKind::RVVFixedLengthData_M4:
-    return "m4";
-  case VectorKind::RVVFixedLengthData_M8:
-    return "m8";
-  default:
-    break;
+  if (isRVVFixedLengthDataVectorKind(VK)) {
+    static constexpr const char *LMuls[] = {"mf8", "mf4", "mf2", "m1",
+                                            "m2",  "m4",  "m8"};
+    return LMuls[getRVVFixedLengthDataLMulLog2(VK) + 3];
   }
   static constexpr const char *Ratios[] = {"1",  "2",  "4", "8",
                                            "16", "32", "64"};
@@ -4474,91 +4370,24 @@ inline StringRef getRVVFixedLengthString(VectorKind VK) {
 /// Return the number of lanes (1, 2 or 4) of an RVV fixed-length mask vector
 /// kind that fits in less than one byte, or 0 if its lanes fill whole bytes.
 inline unsigned getRVVFixedLengthMaskSubByteLanes(VectorKind VK) {
-  switch (VK) {
-  case VectorKind::RVVFixedLengthMaskRatio1:
-  case VectorKind::RVVFixedLengthMaskRatio2:
-  case VectorKind::RVVFixedLengthMaskRatio4:
-  case VectorKind::RVVFixedLengthMaskRatio8:
-  case VectorKind::RVVFixedLengthMaskRatio16:
-  case VectorKind::RVVFixedLengthMaskRatio32:
-  case VectorKind::RVVFixedLengthMaskRatio64:
-    return 0;
-  case VectorKind::RVVFixedLengthMaskRatio1_1:
-  case VectorKind::RVVFixedLengthMaskRatio2_1:
-  case VectorKind::RVVFixedLengthMaskRatio4_1:
-  case VectorKind::RVVFixedLengthMaskRatio8_1:
-  case VectorKind::RVVFixedLengthMaskRatio16_1:
-  case VectorKind::RVVFixedLengthMaskRatio32_1:
-  case VectorKind::RVVFixedLengthMaskRatio64_1:
-    return 1;
-  case VectorKind::RVVFixedLengthMaskRatio1_2:
-  case VectorKind::RVVFixedLengthMaskRatio2_2:
-  case VectorKind::RVVFixedLengthMaskRatio4_2:
-  case VectorKind::RVVFixedLengthMaskRatio8_2:
-  case VectorKind::RVVFixedLengthMaskRatio16_2:
-  case VectorKind::RVVFixedLengthMaskRatio32_2:
-  case VectorKind::RVVFixedLengthMaskRatio64_2:
-    return 2;
-  case VectorKind::RVVFixedLengthMaskRatio1_4:
-  case VectorKind::RVVFixedLengthMaskRatio2_4:
-  case VectorKind::RVVFixedLengthMaskRatio4_4:
-  case VectorKind::RVVFixedLengthMaskRatio8_4:
-  case VectorKind::RVVFixedLengthMaskRatio16_4:
-  case VectorKind::RVVFixedLengthMaskRatio32_4:
-  case VectorKind::RVVFixedLengthMaskRatio64_4:
-    return 4;
-  default:
-    llvm_unreachable("not an RVV fixed-length mask vector kind");
-  }
+  assert(isRVVFixedLengthMaskVectorKind(VK) &&
+         "not an RVV fixed-length mask vector kind");
+  unsigned Col =
+      (unsigned(VK) - unsigned(VectorKind::RVVFixedLengthMaskRatio1)) % 4;
+  return Col ? 1u << (Col - 1) : 0;
 }
 
 /// Inverse of getRVVFixedLengthMaskRatioLog2() and
 /// getRVVFixedLengthMaskSubByteLanes().
 inline VectorKind getRVVFixedLengthMaskVectorKind(unsigned RatioLog2,
                                                   unsigned SubByteLanes) {
-  static constexpr VectorKind Kinds[7][4] = {
-      {VectorKind::RVVFixedLengthMaskRatio1,
-       VectorKind::RVVFixedLengthMaskRatio1_1,
-       VectorKind::RVVFixedLengthMaskRatio1_2,
-       VectorKind::RVVFixedLengthMaskRatio1_4},
-      {VectorKind::RVVFixedLengthMaskRatio2,
-       VectorKind::RVVFixedLengthMaskRatio2_1,
-       VectorKind::RVVFixedLengthMaskRatio2_2,
-       VectorKind::RVVFixedLengthMaskRatio2_4},
-      {VectorKind::RVVFixedLengthMaskRatio4,
-       VectorKind::RVVFixedLengthMaskRatio4_1,
-       VectorKind::RVVFixedLengthMaskRatio4_2,
-       VectorKind::RVVFixedLengthMaskRatio4_4},
-      {VectorKind::RVVFixedLengthMaskRatio8,
-       VectorKind::RVVFixedLengthMaskRatio8_1,
-       VectorKind::RVVFixedLengthMaskRatio8_2,
-       VectorKind::RVVFixedLengthMaskRatio8_4},
-      {VectorKind::RVVFixedLengthMaskRatio16,
-       VectorKind::RVVFixedLengthMaskRatio16_1,
-       VectorKind::RVVFixedLengthMaskRatio16_2,
-       VectorKind::RVVFixedLengthMaskRatio16_4},
-      {VectorKind::RVVFixedLengthMaskRatio32,
-       VectorKind::RVVFixedLengthMaskRatio32_1,
-       VectorKind::RVVFixedLengthMaskRatio32_2,
-       VectorKind::RVVFixedLengthMaskRatio32_4},
-      {VectorKind::RVVFixedLengthMaskRatio64,
-       VectorKind::RVVFixedLengthMaskRatio64_1,
-       VectorKind::RVVFixedLengthMaskRatio64_2,
-       VectorKind::RVVFixedLengthMaskRatio64_4},
-  };
   assert(RatioLog2 < 7 && "ratio out of range for an RVV mask type");
-  switch (SubByteLanes) {
-  case 0:
-    return Kinds[RatioLog2][0];
-  case 1:
-    return Kinds[RatioLog2][1];
-  case 2:
-    return Kinds[RatioLog2][2];
-  case 4:
-    return Kinds[RatioLog2][3];
-  default:
-    llvm_unreachable("unexpected sub-byte lane count");
-  }
+  assert((SubByteLanes == 0 || SubByteLanes == 1 || SubByteLanes == 2 ||
+          SubByteLanes == 4) &&
+         "unexpected sub-byte lane count");
+  unsigned Col = SubByteLanes == 4 ? 3 : SubByteLanes;
+  return VectorKind(unsigned(VectorKind::RVVFixedLengthMaskRatio1) +
+                    RatioLog2 * 4 + Col);
 }
 
 /// Represents a GCC generic vector type. This type is created using

>From 3d48e5960c859dfe824da6c9c17219da8b8ec30e Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Thu, 5 Nov 2026 14:16:45 +0800
Subject: [PATCH 9/9] fixup! [Clang][RISCV] Record the LMUL in RVV fixed-length
 data vector kinds

---
 clang/include/clang/AST/TypeBase.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/AST/TypeBase.h b/clang/include/clang/AST/TypeBase.h
index d1433e336685f5..56e31f25c26d6d 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -4385,7 +4385,7 @@ inline VectorKind getRVVFixedLengthMaskVectorKind(unsigned RatioLog2,
   assert((SubByteLanes == 0 || SubByteLanes == 1 || SubByteLanes == 2 ||
           SubByteLanes == 4) &&
          "unexpected sub-byte lane count");
-  unsigned Col = SubByteLanes == 4 ? 3 : SubByteLanes;
+  unsigned Col = SubByteLanes == 0 ? 0 : llvm::Log2_32(SubByteLanes) + 1;
   return VectorKind(unsigned(VectorKind::RVVFixedLengthMaskRatio1) +
                     RatioLog2 * 4 + Col);
 }



More information about the cfe-commits mailing list