[clang] [Clang] Enable constexpr handling for builtin elementwise fshl/fshr (PR #153572)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 25 20:23:06 PDT 2025
================
@@ -2817,6 +2817,81 @@ static bool interp__builtin_select(InterpState &S, CodePtr OpPC,
return true;
}
+static bool interp__builtin_elementwise_fsh(InterpState &S, CodePtr OpPC,
+ const CallExpr *Call,
+ unsigned BuiltinID) {
+ assert(Call->getNumArgs() == 3);
+
+ const QualType Arg0Type = Call->getArg(0)->getType();
+ const QualType Arg1Type = Call->getArg(1)->getType();
+ const QualType Arg2Type = Call->getArg(2)->getType();
+
+ // Non-vector integer types.
+ if (!Arg0Type->isVectorType()) {
+ assert(!Arg1Type->isVectorType());
+ assert(!Arg2Type->isVectorType());
+ const APSInt &Shift = popToAPSInt(
+ S.Stk, *S.getContext().classify(Arg2Type));
+ const APSInt &Lo = popToAPSInt(
+ S.Stk, *S.getContext().classify(Arg1Type));
+ const APSInt &Hi = popToAPSInt(
+ S.Stk, *S.getContext().classify(Arg0Type));
+ APSInt Result;
+ if (BuiltinID == Builtin::BI__builtin_elementwise_fshl)
+ Result = APSInt(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned());
+ else if (BuiltinID == Builtin::BI__builtin_elementwise_fshr)
+ Result = APSInt(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned());
+ else
+ llvm_unreachable("Wrong builtin ID");
+ pushInteger(S, Result, Call->getType());
+ return true;
+ }
+
+ // Vector type.
+ assert(Arg0Type->isVectorType() && Arg1Type->isVectorType() &&
+ Arg2Type->isVectorType());
+
+ const auto *VecT = Arg0Type->castAs<VectorType>();
+ const PrimType &ElemT = *S.getContext().classify(VecT->getElementType());
+ unsigned NumElems = VecT->getNumElements();
+
+ assert(VecT->getElementType() ==
+ Arg1Type->castAs<VectorType>()->getElementType() &&
+ VecT->getElementType() ==
+ Arg2Type->castAs<VectorType>()->getElementType());
+ assert(NumElems == Arg1Type->castAs<VectorType>()->getNumElements() &&
+ NumElems == Arg2Type->castAs<VectorType>()->getNumElements());
+ assert(VecT->getElementType()->isIntegralOrEnumerationType());
+
+ const Pointer &VecShift = S.Stk.pop<Pointer>();
+ const Pointer &VecLo = S.Stk.pop<Pointer>();
+ const Pointer &VecHi = S.Stk.pop<Pointer>();
+ const Pointer &Dst = S.Stk.peek<Pointer>();
+ for (unsigned I = 0; I != NumElems; ++I) {
+ APSInt Hi;
+ APSInt Lo;
+ APSInt Shift;
+ INT_TYPE_SWITCH_NO_BOOL(ElemT, {
+ Hi = VecHi.elem<T>(I).toAPSInt();
+ Lo = VecLo.elem<T>(I).toAPSInt();
+ Shift = VecShift.elem<T>(I).toAPSInt();
+ });
+ APSInt Result;
+ if (BuiltinID == Builtin::BI__builtin_elementwise_fshl) {
----------------
tbaederr wrote:
```suggestion
if (BuiltinID == Builtin::BI__builtin_elementwise_fshl)
```
https://github.com/llvm/llvm-project/pull/153572
More information about the cfe-commits
mailing list