[flang-commits] [flang] [Clang] Add __builtin_vectorelements to get number of elements in vector (PR #69010)
Lawrence Benson via flang-commits
flang-commits at lists.llvm.org
Mon Oct 16 09:11:32 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();
----------------
lawben wrote:
the argument here is the vector type that was passed to `__builtin_vectorelements()`. the call to `@llvm.vscale` is independent of the type, as it is a system-wide thing, so it does not need the type.
scalable vectors are represented as `<vscale x NUM_ELEMENTS_PER_MINIMUM_VECTOR x TYPE>`. so if we get `vscale`, we still need to figure out the `NUM_ELEMENTS` part so we can get the actual number of elements on this system by multiplying it with. as that depends on the vector type passed to the builtin, as the number of elements depends on the element's type. for SVE, LLVM assumes a minimum 16-byte vector, so a `<vscale x 4 x i32`> has `4 * vscale` but a `<vscale x 8 x i16>` has `8 * vscale`.
I hope this clarifies the code. if so, I'll probably add a comment to explain.
https://github.com/llvm/llvm-project/pull/69010
More information about the flang-commits
mailing list