[llvm] [LoopUnroll] Combine partial sub/fsub reduction results with add/fadd (PR #201066)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 2 02:13:37 PDT 2026


================
@@ -1533,10 +1533,17 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
         IRBuilder Builder(ExitBlock, ExitBlock->getFirstNonPHIIt());
         Builder.setFastMathFlags(Reductions.begin()->second.getFastMathFlags());
         RecurKind RK = Reductions.begin()->second.getRecurrenceKind();
+        // Partial results of sub/fsub reductions already carry the correct
+        // sign and must be summed; chaining them with sub/fsub would flip
+        // the sign of every other partial result.
+        unsigned RdxOpcode = RecurrenceDescriptor::getOpcode(RK);
+        if (RdxOpcode == Instruction::Sub)
+          RdxOpcode = Instruction::Add;
+        else if (RdxOpcode == Instruction::FSub)
+          RdxOpcode = Instruction::FAdd;
----------------
fhahn wrote:

In LV, there's also the following, more compact comment:

```
           // For sub-recurrences, each part's reduction variable is already 
          // negative, we need to do: reduce.add(-acc_uf0 + -acc_uf1)
```

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


More information about the llvm-commits mailing list