[llvm] [LV] Create in-loop sub reductions (PR #147026)

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 17 11:11:42 PDT 2025


================
@@ -362,17 +363,24 @@ bool RecurrenceDescriptor::AddReductionVar(
     if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent())
       return false;
 
-    // Reductions of instructions such as Div, and Sub is only possible if the
+    // Reductions of instructions such as Div is only possible if the
     // LHS is the reduction variable.
-    if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) &&
-        !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) &&
+    if ((Kind != RecurKind::Sub && !Cur->isCommutative()) && !IsAPhi &&
+        !isa<SelectInst>(Cur) && !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) &&
         !VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0))))
       return false;
----------------
MacDue wrote:

Can this restriction be lifted? I don't think the new vector code is correct for ` @reduction_sub_rhs`:

Sub reduction on RHS:
```
s0 = x0 - 0
s1 = x1 - s0
s2 = x2 - s1
s3 = x3 - s2
  
= x3 - x2 + x1 - x0
!= vec_reduce_add((x0,x1,x2,x3) - (0, 0, 0, 0)) = x0 + x1 + x2 +x3
```
Sub reduction on LHS:
```
s0 = 0 - x0
s1 = s0 - x1
s2 = s1 - x2
s3 = s2 - x3
= 0 - x0 - x1 - x2 - x3
= vec_reduce_add((0, 0, 0, 0) - (x0,x1,x2,x3))
```

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


More information about the llvm-commits mailing list