[clang] 58b49ce - [clang][Interp] Support __builtin_vectorelements

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 16 02:21:50 PDT 2024


Author: Timm Bäder
Date: 2024-04-16T11:21:41+02:00
New Revision: 58b49cef1d772a922a433fd4a42e41db3f18d34b

URL: https://github.com/llvm/llvm-project/commit/58b49cef1d772a922a433fd4a42e41db3f18d34b
DIFF: https://github.com/llvm/llvm-project/commit/58b49cef1d772a922a433fd4a42e41db3f18d34b.diff

LOG: [clang][Interp] Support __builtin_vectorelements

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/vectors.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 5866228663dca2..93059edc4622f8 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1251,6 +1251,15 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryExprOrTypeTraitExpr(
     return this->emitConst(Size.getQuantity(), E);
   }
 
+  if (Kind == UETT_VectorElements) {
+    if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>())
+      return this->emitConst(VT->getNumElements(), E);
+
+    // FIXME: Apparently we need to catch the fact that a sizeless vector type
+    // has been passed and diagnose that (at run time).
+    assert(E->getTypeOfArgument()->isSizelessVectorType());
+  }
+
   return false;
 }
 

diff  --git a/clang/test/AST/Interp/vectors.cpp b/clang/test/AST/Interp/vectors.cpp
index fb5787a9eda9a9..6c5d916f51f563 100644
--- a/clang/test/AST/Interp/vectors.cpp
+++ b/clang/test/AST/Interp/vectors.cpp
@@ -13,12 +13,14 @@ namespace Vector {
     return VI4 { n * 3, n + 4, n - 5, n / 6 };
   }
   constexpr auto v1 = f(10);
+  static_assert(__builtin_vectorelements(v1) == (16 / sizeof(int)), "");
 
   typedef double __attribute__((vector_size(32))) VD4;
   constexpr VD4 g(int n) {
     return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 };
   }
   constexpr auto v2 = g(4);
+  static_assert(__builtin_vectorelements(v2) == (32 / sizeof(double)), "");
 }
 
 /// FIXME: We need to support BitCasts between vector types.


        


More information about the cfe-commits mailing list