[llvm] [LoopUnroll] Simplify reduction operations after a loop unroll (PR #84805)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 11 11:12:56 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 034cc2f5d0abcf7a465665246f16a1b75fbde93a 1ec52ec6c2e1e4edefd0b2a02fad005655c1303e -- llvm/lib/Transforms/Utils/LoopUnroll.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
index b14d05d642..4471477cc9 100644
--- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
@@ -85,9 +85,10 @@ STATISTIC(NumUnrolledNotLatch, "Number of loops unrolled without a conditional "
                                "latch (completely or otherwise)");
 
 static cl::opt<bool>
-UnrollSimplifyReductions("unroll-simplify-reductions", cl::init(true),
-                         cl::Hidden, cl::desc("Try to simplify reductions "
-                                              "after unrolling a loop."));
+    UnrollSimplifyReductions("unroll-simplify-reductions", cl::init(true),
+                             cl::Hidden,
+                             cl::desc("Try to simplify reductions "
+                                      "after unrolling a loop."));
 
 static cl::opt<bool>
 UnrollRuntimeEpilog("unroll-runtime-epilog", cl::init(false), cl::Hidden,
@@ -265,8 +266,8 @@ static bool trySimplifyReductions(Instruction &I) {
   // Attempt to construct a list of instructions that are chained together
   // (i.e. that perform a reduction).
   SmallVector<BinaryOperator *, 16> Ops;
-  for (Instruction *Cur = PN, *Next = nullptr; /* true */; Cur = Next,
-                                                           Next = nullptr) {
+  for (Instruction *Cur = PN, *Next = nullptr; /* true */;
+       Cur = Next, Next = nullptr) {
     // Try to find the next element in the reduction chain.
     for (auto *U : Cur->users()) {
       auto *Candidate = dyn_cast<Instruction>(U);
@@ -298,11 +299,10 @@ static bool trySimplifyReductions(Instruction &I) {
   if (Ops.size() < 2)
     return false;
 
-  LLVM_DEBUG(
-  dbgs() << "Found candidate reduction: " << I << "\n";
-  for (auto const *Op : Ops)
-    dbgs() << "                         | " << *Op << "\n";
-  );
+  LLVM_DEBUG(dbgs() << "Found candidate reduction: " << I << "\n";
+             for (auto const *Op
+                  : Ops) dbgs()
+             << "                         | " << *Op << "\n";);
 
   // Ensure all instructions perform the same operation and that the operation
   // is associative and commutative so that we can break the chain apart and
@@ -365,7 +365,7 @@ static bool trySimplifyReductions(Instruction &I) {
     // Find the index of the operand of Op to replace. The first Op reads its
     // value from the first Phi node. The other Ops read their value from the
     // previous Op.
-    Value *OperandToReplace = i == 0 ? cast<Value>(PN) : Ops[i-1];
+    Value *OperandToReplace = i == 0 ? cast<Value>(PN) : Ops[i - 1];
     unsigned OperandIdx = Op->getOperand(0) == OperandToReplace ? 0 : 1;
     assert(Op->getOperand(OperandIdx) == OperandToReplace &&
            "Operand mismatch. Perhaps a malformed chain?");
@@ -407,9 +407,9 @@ static bool trySimplifyReductions(Instruction &I) {
   // Helper function to create a new binary op.
   // Note: We copy the flags from Ops[0]. Could this be too permissive?
   auto CreateBinOp = [&](Value *V1, Value *V2) {
-    auto Name = PN->getName()+".red";
-    return BinaryOperator::CreateWithCopiedFlags(Opcode, V1, V2, Ops[0],
-                                                 Name, &BB->back());
+    auto Name = PN->getName() + ".red";
+    return BinaryOperator::CreateWithCopiedFlags(Opcode, V1, V2, Ops[0], Name,
+                                                 &BB->back());
   };
 
   // Compute the partial sums of the Ops:
@@ -419,11 +419,11 @@ static bool trySimplifyReductions(Instruction &I) {
   //           = Ops[k-1] + SOps[k-1],
   // so if we compute SOps in order (i.e. from 0 to N) we can reuse partial
   // results.
-  SmallVector<Value *, 16> SOps(N+1);
-  SOps[0] = nullptr;  // alternatively we could use NeutralElem
+  SmallVector<Value *, 16> SOps(N + 1);
+  SOps[0] = nullptr; // alternatively we could use NeutralElem
   SOps[1] = Ops.front();
   for (unsigned k = 2; k <= N; k++)
-    SOps[k] = CreateBinOp(SOps[k-1], Ops[k-1]);
+    SOps[k] = CreateBinOp(SOps[k - 1], Ops[k - 1]);
 
   // Compute the partial sums of the Phis:
   //   SPhis[k] = \sum_{k <= i < N} Phis[i], 0 <= k <= N.
@@ -432,20 +432,20 @@ static bool trySimplifyReductions(Instruction &I) {
   //            = Phis[k] + SPhis[k+1],
   // so if we compute SPhis in reverse (i.e. from N down to 0) we can reuse the
   // partial sums computed thus far.
-  SmallVector<Value *, 16> SPhis(N+1);
-  SPhis[N] = nullptr;  // alternatively we could use NeutralElem
-  SPhis[N-1] = Phis.back();
-  for (signed k = N-2; k >= 0; k--)
-    SPhis[k] = CreateBinOp(SPhis[k+1], Phis[k]);
+  SmallVector<Value *, 16> SPhis(N + 1);
+  SPhis[N] = nullptr; // alternatively we could use NeutralElem
+  SPhis[N - 1] = Phis.back();
+  for (signed k = N - 2; k >= 0; k--)
+    SPhis[k] = CreateBinOp(SPhis[k + 1], Phis[k]);
 
   // Finally, compute the total sums for PN and Ops from:
   //   Sums[k] = SOps[k] + SPhis[k], 0 <= k <= N.
   // These sums might be dead so we had them to a weak tracking vector for
   // cleanup after.
-  SmallVector<WeakTrackingVH, 16> Sums(N+1);
+  SmallVector<WeakTrackingVH, 16> Sums(N + 1);
   for (unsigned k = 0; k <= N; k++) {
     // Pick the Op we want to compute the new total for.
-    Value *Op = k == 0 ? cast<Value>(PN) : Ops[k-1];
+    Value *Op = k == 0 ? cast<Value>(PN) : Ops[k - 1];
 
     Value *SOp = SOps[k], *SPhi = SPhis[k];
     if (SOp && SPhi)

``````````

</details>


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


More information about the llvm-commits mailing list