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

Lawrence Benson via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 16 09:11:33 PDT 2023


================
@@ -13595,6 +13595,15 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
                     Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))
             .getQuantity(),
         E);
+  case UETT_VectorElements: {
+    QualType Ty = E->getTypeOfArgument();
+    // If the vector has a fixed size, we can determine the number of elements
+    // at compile time.
+    if (Ty->isVectorType())
+      return Success(Ty->castAs<VectorType>()->getNumElements(), E);
+
+    return false;
----------------
lawben wrote:

maybe my understanding here is wrong, so please correct me in that case. If the vector is fixed-sized, we can determine the number of elements at compile time, which is why we return the number here in `ExprConstant.cpp`. if it is a scalable vector, we `return false`, as we cannot determine a constant value. in that case, we move along and hit `CGExprScalar.cpp`, which is where we emit the runtime `vscale` code for anything that is not constant. 

So in my understanding, we either emit a constant in `ExprConstant` or the runtime logic in `CGExprScalar`. I can add assertions to check that we are always in a good case, but i've not come across a fixed-sized vector path that does not get caught in `ExprConstant`. if there are alternative code paths, then you are correct and I should add checks. but I don't want to duplicate logic if this case can never happen anyway.

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


More information about the cfe-commits mailing list