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

Sam Tebbs via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 18 06:40:40 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;
----------------
SamTebbs33 wrote:

I had a look at the output IR for that test and it seemed equivalent but tracing the loop itself reveals that you're right and it's not the same. I've added this restriction back now, thanks!

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


More information about the llvm-commits mailing list