[clang] [X86][Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - allow element extraction/insertion intrinsics to be used in constexpr #159753 (PR #161302)

Aditya Chaudhari via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 30 06:37:34 PDT 2025


================
@@ -2878,6 +2878,66 @@ static bool interp__builtin_x86_insert_subvector(InterpState &S, CodePtr OpPC,
   return true;
 }
 
+static bool interp__builtin_vec_ext(InterpState &S, CodePtr OpPC,
+                                    const CallExpr *Call, unsigned ID) {
+  assert(Call->getNumArgs() == 2);
+
+  APSInt ImmAPS = popToAPSInt(S, Call->getArg(1));
+  const Pointer &Vec = S.Stk.pop<Pointer>();
+  if (!Vec.getFieldDesc()->isPrimitiveArray())
+    return false;
+
+  unsigned NumElts = Vec.getNumElems();
+  unsigned Index =
+      (NumElts == 0)
+          ? 0u
+          : static_cast<unsigned>(ImmAPS.getZExtValue() & (NumElts - 1));
+
+  switch (ID) {
+  case X86::BI__builtin_ia32_vec_ext_v4sf:
+    S.Stk.push<Floating>(Vec.elem<Floating>(Index));
+    return true;
+  default: {
+    PrimType ElemPT = Vec.getFieldDesc()->getPrimType();
+    INT_TYPE_SWITCH_NO_BOOL(ElemPT, {
+      APSInt V = Vec.elem<T>(Index).toAPSInt();
+      pushInteger(S, V, Call->getType());
+    });
+    return true;
+  }
+  }
+}
+
+static bool interp__builtin_vec_set(InterpState &S, CodePtr OpPC,
+                                    const CallExpr *Call, unsigned ID) {
+  assert(Call->getNumArgs() == 3);
+
+  APSInt ImmAPS = popToAPSInt(S, Call->getArg(2));
+  APSInt ValAPS = popToAPSInt(S, Call->getArg(1));
+
+  const Pointer &Base = S.Stk.pop<Pointer>();
+  if (!Base.getFieldDesc()->isPrimitiveArray())
+    return false;
+
+  const Pointer &Dst = S.Stk.peek<Pointer>();
+
+  unsigned NumElts = Base.getNumElems();
+  unsigned Index =
+      (NumElts == 0)
----------------
AdityaC4 wrote:

Fair point, I overlooked that Sema guarantees >= 1 elements. I'll drop that check.

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


More information about the cfe-commits mailing list