[llvm] [DirectX] Add support for vector_reduce_add (PR #117646)

Farzon Lotfi via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 2 13:07:37 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);
----------------
farzonl wrote:

Order shouldn't matter. It wouldn't make sense for NaN to change the Commutative Properties of Addition.

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


More information about the llvm-commits mailing list