[clang] [RISCV] Improve casting between i1 scalable vectors and i8 fixed vectors for -mrvv-vector-bits (PR #139190)

Paul Walker via cfe-commits cfe-commits at lists.llvm.org
Mon May 12 06:25:50 PDT 2025


================
@@ -1366,19 +1366,29 @@ static llvm::Value *CreateCoercedLoad(Address Src, llvm::Type *Ty,
       // If we are casting a fixed i8 vector to a scalable i1 predicate
       // vector, use a vector insert and bitcast the result.
       if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
-          ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&
           FixedSrcTy->getElementType()->isIntegerTy(8)) {
         ScalableDstTy = llvm::ScalableVectorType::get(
             FixedSrcTy->getElementType(),
-            ScalableDstTy->getElementCount().getKnownMinValue() / 8);
+            llvm::divideCeil(
+                ScalableDstTy->getElementCount().getKnownMinValue(), 8));
       }
       if (ScalableDstTy->getElementType() == FixedSrcTy->getElementType()) {
         auto *Load = CGF.Builder.CreateLoad(Src);
         auto *PoisonVec = llvm::PoisonValue::get(ScalableDstTy);
         llvm::Value *Result = CGF.Builder.CreateInsertVector(
             ScalableDstTy, PoisonVec, Load, uint64_t(0), "cast.scalable");
-        if (ScalableDstTy != Ty)
-          Result = CGF.Builder.CreateBitCast(Result, Ty);
+        ScalableDstTy = cast<llvm::ScalableVectorType>(Ty);
+        if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
+            !ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&
+            FixedSrcTy->getElementType()->isIntegerTy(8))
+          ScalableDstTy = llvm::ScalableVectorType::get(
+              ScalableDstTy->getElementType(),
+              llvm::alignTo<8>(
+                  ScalableDstTy->getElementCount().getKnownMinValue()));
----------------
paulwalker-arm wrote:

What do you think to the idea of pulling `VectorType::getWithSizeAndScalar` from https://github.com/llvm/llvm-project/pull/130973 into this PR? because then this can be simplified to just:
```
ScalableDstTy = cast<llvm::ScalableVectorType>(llvm::VectorType::getWithSizeAndScalar(ScalableDstTy, Ty));
```
I suspect the same or similar change will work for the other parts of the PR as well.

https://github.com/llvm/llvm-project/pull/139190


More information about the cfe-commits mailing list