[libc-commits] [libc] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)

Nikita Popov via libc-commits libc-commits at lists.llvm.org
Mon Oct 16 09:17:35 PDT 2023


================
@@ -3083,6 +3083,19 @@ ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
                 E->getTypeOfArgument()->getPointeeType()))
             .getQuantity();
     return llvm::ConstantInt::get(CGF.SizeTy, Alignment);
+  } else if (E->getKind() == UETT_VectorElements) {
+    // For scalable vectors, we don't know the size at compile time. We can use
+    // @llvm.vscale to calculate it at runtime.
+    if (E->getTypeOfArgument()->isSizelessVectorType()) {
+      auto *VecTy = dyn_cast<llvm::ScalableVectorType>(
+          ConvertType(E->getTypeOfArgument()));
+      uint64_t NumUnscaledElements = VecTy->getMinNumElements();
+
+      llvm::Value *VScale =
+          Builder.CreateVScale(llvm::ConstantInt::get(CGF.SizeTy, 1));
+      return Builder.CreateMul(
+          VScale, llvm::ConstantInt::get(CGF.SizeTy, NumUnscaledElements));
----------------
nikic wrote:

You can use `Builder.CreateElementCount(CGF.SizeTy, VecTy->getElementCount())` or so here. (Incidentally, this works for both fixed and scalable vectors.)

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


More information about the libc-commits mailing list