[clang] 3f43abc - [RISCV] Reduce dependency on RISCV::RVVBitsPerBlock for calculating vector size for -mrvv-vector-bits.

Craig Topper via cfe-commits cfe-commits at lists.llvm.org
Thu May 18 10:13:54 PDT 2023


Author: Craig Topper
Date: 2023-05-18T10:09:50-07:00
New Revision: 3f43abc9e78cce99741f7c8cf54552c95cc98d9f

URL: https://github.com/llvm/llvm-project/commit/3f43abc9e78cce99741f7c8cf54552c95cc98d9f
DIFF: https://github.com/llvm/llvm-project/commit/3f43abc9e78cce99741f7c8cf54552c95cc98d9f.diff

LOG: [RISCV] Reduce dependency on RISCV::RVVBitsPerBlock for calculating vector size for -mrvv-vector-bits.

We can use the minimum value of the BuiltinType's ElementCount and
the element size.

This needs to be done to support LMUL!=1 types anyway.

I did have to make an ordering change in the error checks in
HandleRISCVRVVVectorBitsTypeAttr to check if the type is an RVV
VLS type before checking the size.

Added: 
    

Modified: 
    clang/lib/AST/ASTContext.cpp
    clang/lib/Sema/SemaType.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index d5b08649cbca..9c247b3c439a 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -85,7 +85,6 @@
 #include "llvm/Support/MD5.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/TargetParser/RISCVTargetParser.h"
 #include "llvm/TargetParser/Triple.h"
 #include <algorithm>
 #include <cassert>
@@ -9581,7 +9580,14 @@ bool ASTContext::areLaxCompatibleSveTypes(QualType FirstType,
 static uint64_t getRVVTypeSize(ASTContext &Context, const BuiltinType *Ty) {
   assert(Ty->isRVVVLSBuiltinType() && "Invalid RVV Type");
   auto VScale = Context.getTargetInfo().getVScaleRange(Context.getLangOpts());
-  return VScale ? VScale->first * llvm::RISCV::RVVBitsPerBlock : 0;
+  if (!VScale)
+    return 0;
+
+  ASTContext::BuiltinVectorTypeInfo Info = Context.getBuiltinVectorTypeInfo(Ty);
+
+  unsigned EltSize = Context.getTypeSize(Info.ElementType);
+  unsigned MinElts = Info.EC.getKnownMinValue();
+  return VScale->first * MinElts * EltSize;
 }
 
 bool ASTContext::areCompatibleRVVTypes(QualType FirstType,

diff  --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index da105e3d6ad2..06efb3dfef8e 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -8320,31 +8320,32 @@ static void HandleRISCVRVVVectorBitsTypeAttr(QualType &CurType,
   if (!verifyValidIntegerConstantExpr(S, Attr, RVVVectorSizeInBits))
     return;
 
+  // Attribute can only be attached to a single RVV vector type.
+  if (!CurType->isRVVVLSBuiltinType()) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_rvv_type)
+        << Attr << CurType;
+    Attr.setInvalid();
+    return;
+  }
+
   unsigned VecSize = static_cast<unsigned>(RVVVectorSizeInBits.getZExtValue());
 
+  ASTContext::BuiltinVectorTypeInfo Info =
+      S.Context.getBuiltinVectorTypeInfo(CurType->getAs<BuiltinType>());
+  unsigned EltSize = S.Context.getTypeSize(Info.ElementType);
+  unsigned MinElts = Info.EC.getKnownMinValue();
+
   // The attribute vector size must match -mrvv-vector-bits.
-  // FIXME: Add support for types with LMUL!=1. Need to make sure size passed
-  // to attribute is equal to LMUL*VScaleMin*RVVBitsPerBlock.
-  if (VecSize != VScale->first * llvm::RISCV::RVVBitsPerBlock) {
+  if (VecSize != VScale->first * MinElts * EltSize) {
     S.Diag(Attr.getLoc(), diag::err_attribute_bad_rvv_vector_size)
         << VecSize << VScale->first * llvm::RISCV::RVVBitsPerBlock;
     Attr.setInvalid();
     return;
   }
 
-  // Attribute can only be attached to a single RVV vector type.
-  if (!CurType->isRVVVLSBuiltinType()) {
-    S.Diag(Attr.getLoc(), diag::err_attribute_invalid_rvv_type)
-        << Attr << CurType;
-    Attr.setInvalid();
-    return;
-  }
-
-  QualType EltType = CurType->getRVVEltType(S.Context);
-  unsigned TypeSize = S.Context.getTypeSize(EltType);
   VectorType::VectorKind VecKind = VectorType::RVVFixedLengthDataVector;
-  VecSize /= TypeSize;
-  CurType = S.Context.getVectorType(EltType, VecSize, VecKind);
+  VecSize /= EltSize;
+  CurType = S.Context.getVectorType(Info.ElementType, VecSize, VecKind);
 }
 
 /// Handle OpenCL Access Qualifier Attribute.


        


More information about the cfe-commits mailing list