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

Sander de Smalen via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 30 06:16:08 PDT 2025


================
@@ -2524,10 +2529,15 @@ void VPReductionRecipe::execute(VPTransformState &State) {
     NewRed = createSimpleReduction(State.Builder, NewVecOp, Kind);
     if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind))
       NextInChain = createMinMaxOp(State.Builder, Kind, NewRed, PrevInChain);
-    else
+    else {
+      // Sub reductions aren't commutative so the operands need to be swapped.
+      bool IsSub = Kind == RecurKind::Sub;
+      Value *LHS = IsSub ? PrevInChain : NewRed;
----------------
sdesmalen-arm wrote:

For commutative operations the order doesn't matter, so I think you can always swap the order of PrevInChain/NewRed, avoiding the need for `IsSub`, `LHS` and `RHS`. That is best done in a separate (NFCI) patch though, because various tests require updating.

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


More information about the llvm-commits mailing list