[llvm] [DirectX] Add support for vector_reduce_add (PR #117646)
Tex Riddell via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 2 13:00:42 PST 2024
================
@@ -67,10 +67,46 @@ static bool isIntrinsicExpansion(Function &F) {
case Intrinsic::dx_sign:
case Intrinsic::dx_step:
case Intrinsic::dx_radians:
+ case Intrinsic::vector_reduce_add:
+ case Intrinsic::vector_reduce_fadd:
return true;
}
return false;
}
+static Value *expandVecReduceAdd(CallInst *Orig, Intrinsic::ID IntrinsicId) {
+ assert(IntrinsicId == Intrinsic::vector_reduce_add ||
+ IntrinsicId == Intrinsic::vector_reduce_fadd);
+
+ IRBuilder<> Builder(Orig);
+ bool IsFAdd = (IntrinsicId == Intrinsic::vector_reduce_fadd);
+
+ // Define the addition operation based on the intrinsic ID.
+ auto AddOp = [&Builder, IsFAdd](Value *Sum, Value *Elt) {
+ return IsFAdd ? Builder.CreateFAdd(Sum, Elt) : Builder.CreateAdd(Sum, Elt);
+ };
+
+ Value *X = Orig->getOperand(IsFAdd ? 1 : 0);
+ Type *Ty = X->getType();
+ auto *XVec = dyn_cast<FixedVectorType>(Ty);
+ unsigned XVecSize = XVec->getNumElements();
+ Value *Sum = Builder.CreateExtractElement(X, static_cast<uint64_t>(0));
+
+ // Handle the initial start value for floating-point addition.
+ if (IsFAdd) {
+ llvm::Constant *StartValue =
+ llvm::dyn_cast<llvm::Constant>(Orig->getOperand(0));
+ if (StartValue && !StartValue->isZeroValue())
+ Sum = Builder.CreateFAdd(Sum, StartValue);
----------------
tex3d wrote:
Does order matter? For instance, the resulting NaN pattern when both are NaN? If it does, shouldn't we put the operands in the opposite order so that the addition is in the order `StartValue + v[0] + v[1] + v[2] ...` rather than `v[0] + StartValue + v[1] + v[2] ...`?
https://github.com/llvm/llvm-project/pull/117646
More information about the llvm-commits
mailing list