[llvm] A few improvement in fcmla pattern recognitions (PR #173818)

via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 28 20:13:29 PST 2025


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 origin/main HEAD --extensions cpp -- llvm/lib/CodeGen/ComplexDeinterleavingPass.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

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

``````````diff
diff --git a/llvm/lib/CodeGen/ComplexDeinterleavingPass.cpp b/llvm/lib/CodeGen/ComplexDeinterleavingPass.cpp
index 5906b660a..1cc86e877 100644
--- a/llvm/lib/CodeGen/ComplexDeinterleavingPass.cpp
+++ b/llvm/lib/CodeGen/ComplexDeinterleavingPass.cpp
@@ -281,7 +281,10 @@ public:
     CompositeNode *UncommonNode;
     CompositeNode *CommonNode{nullptr};
     ComplexDeinterleavingRotation Rotation;
-    bool IsCommonReal() const { return Rotation == ComplexDeinterleavingRotation::Rotation_0 || Rotation == ComplexDeinterleavingRotation::Rotation_180; }
+    bool IsCommonReal() const {
+      return Rotation == ComplexDeinterleavingRotation::Rotation_0 ||
+             Rotation == ComplexDeinterleavingRotation::Rotation_180;
+    }
   };
 
   explicit ComplexDeinterleavingGraph(const TargetLowering *TL,
@@ -383,8 +386,8 @@ private:
   }
 
   CompositeNode *negCompositeNode(CompositeNode *Node) {
-    auto NegNode = prepareCompositeNode(ComplexDeinterleavingOperation::Symmetric,
-                                        nullptr, nullptr);
+    auto NegNode = prepareCompositeNode(
+        ComplexDeinterleavingOperation::Symmetric, nullptr, nullptr);
     NegNode->Opcode = Instruction::FNeg;
     NegNode->addOperand(Node);
     return submitCompositeNode(NegNode);
@@ -402,8 +405,9 @@ private:
   /// 270: r: cr + ai * bi
   ///      i: ci - ai * br
   CompositeNode *identifyPartialMul(Instruction *Real, Instruction *Imag,
-                                    bool RealPositive=true, bool ImagPositive=true,
-                                    PartialMulNode *PN=nullptr);
+                                    bool RealPositive = true,
+                                    bool ImagPositive = true,
+                                    PartialMulNode *PN = nullptr);
 
   /// Identifies a complex add pattern and its rotation, based on the following
   /// patterns.
@@ -435,9 +439,9 @@ private:
                                    CompositeNode *Accumulator,
                                    bool &AccumPositive);
 
-  /// Extract one addend that have both real and imaginary parts positive/negative.
-  CompositeNode *extractAddend(AddendList &RealAddends,
-                               AddendList &ImagAddends,
+  /// Extract one addend that have both real and imaginary parts
+  /// positive/negative.
+  CompositeNode *extractAddend(AddendList &RealAddends, AddendList &ImagAddends,
                                bool Positive);
 
   /// Determine if sum of multiplications of complex numbers can be formed from
@@ -646,8 +650,7 @@ static const IntrinsicInst *getFMAOrMulAdd(const Instruction *I) {
 }
 
 static inline ComplexDeinterleavingRotation
-flipRotation(ComplexDeinterleavingRotation Rotation, bool Cond=true)
-{
+flipRotation(ComplexDeinterleavingRotation Rotation, bool Cond = true) {
   if (!Cond)
     return Rotation;
   return ComplexDeinterleavingRotation(unsigned(Rotation) ^ 2);
@@ -675,9 +678,9 @@ ComplexDeinterleavingGraph::identifyPartialMul(Instruction *Real,
                                                bool RealPositive,
                                                bool ImagPositive,
                                                PartialMulNode *PN) {
-  LLVM_DEBUG(dbgs() << "identifyPartialMul "
-                    << (RealPositive ? " + " : " - ") << *Real << " / "
-                    << (ImagPositive ? " + " : " - ") << *Imag << "\n");
+  LLVM_DEBUG(dbgs() << "identifyPartialMul " << (RealPositive ? " + " : " - ")
+                    << *Real << " / " << (ImagPositive ? " + " : " - ") << *Imag
+                    << "\n");
 
   auto GetProduct = [](Value *V1, Value *V2, bool IsPositive) -> Product {
     if (isNeg(V1)) {
@@ -699,14 +702,14 @@ ComplexDeinterleavingGraph::identifyPartialMul(Instruction *Real,
   };
 
   auto ProcessMulAdd = [&](Product Mul, Addend Add, bool CheckAdd,
-                          SmallVectorImpl<Product> &Muls, Addend &Addend) {
+                           SmallVectorImpl<Product> &Muls, Addend &Addend) {
     Muls.push_back(Mul);
     if (CheckAdd) {
       if (auto AddI = dyn_cast<Instruction>(Add.first)) {
         auto Op = AddI->getOpcode();
         if (Op == Instruction::FMul || Op == Instruction::Mul) {
-          Muls.emplace_back(GetProduct(AddI->getOperand(0), AddI->getOperand(1),
-                                       Add.second));
+          Muls.emplace_back(
+              GetProduct(AddI->getOperand(0), AddI->getOperand(1), Add.second));
           return;
         }
       }
@@ -724,15 +727,16 @@ ComplexDeinterleavingGraph::identifyPartialMul(Instruction *Real,
       IsPositive = !IsPositive;
     }
     if (auto II = getFMAOrMulAdd(I)) {
-      ProcessMulAdd(GetProduct(II->getArgOperand(0), II->getArgOperand(1),
-                               IsPositive),
-                    GetAddend(II->getArgOperand(2), IsPositive),
-                    II->getFastMathFlags().allowReassoc(), Muls, Addend);
+      ProcessMulAdd(
+          GetProduct(II->getArgOperand(0), II->getArgOperand(1), IsPositive),
+          GetAddend(II->getArgOperand(2), IsPositive),
+          II->getFastMathFlags().allowReassoc(), Muls, Addend);
       return true;
     }
 
     if (isa<FPMathOperator>(I) && !I->getFastMathFlags().allowContract()) {
-      LLVM_DEBUG(dbgs() << "  - Contract is missing from the FastMath flags.\n");
+      LLVM_DEBUG(
+          dbgs() << "  - Contract is missing from the FastMath flags.\n");
       return false;
     }
 
@@ -750,10 +754,9 @@ ComplexDeinterleavingGraph::identifyPartialMul(Instruction *Real,
       unsigned Opcode0 = I0->getOpcode();
       if (I0->hasOneUse() &&
           (Opcode0 == Instruction::FMul || Opcode0 == Instruction::Mul)) {
-        ProcessMulAdd(GetProduct(I0->getOperand(0), I0->getOperand(1),
-                                 IsPositive),
-                      GetAddend(Op1, IsPositive ^ IsSub),
-                      true, Muls, Addend);
+        ProcessMulAdd(
+            GetProduct(I0->getOperand(0), I0->getOperand(1), IsPositive),
+            GetAddend(Op1, IsPositive ^ IsSub), true, Muls, Addend);
         return true;
       }
     }
@@ -763,16 +766,15 @@ ComplexDeinterleavingGraph::identifyPartialMul(Instruction *Real,
           (Opcode1 == Instruction::FMul || Opcode1 == Instruction::Mul)) {
         ProcessMulAdd(GetProduct(I1->getOperand(0), I1->getOperand(1),
                                  IsPositive ^ IsSub),
-                      GetAddend(Op0, IsPositive),
-                      false, Muls, Addend);
+                      GetAddend(Op0, IsPositive), false, Muls, Addend);
         return true;
       }
     }
     return false;
   };
 
-  auto MatchCommons = [&](PartialMulNode *PN,
-                          CompositeNode *CN, bool CNPositive) -> CompositeNode* {
+  auto MatchCommons = [&](PartialMulNode *PN, CompositeNode *CN,
+                          bool CNPositive) -> CompositeNode * {
     assert(PN);
     for (auto PN0 = PN; PN0; PN0 = PN0->prev) {
       if (PN0->CommonNode)
@@ -785,8 +787,8 @@ ComplexDeinterleavingGraph::identifyPartialMul(Instruction *Real,
         auto Common1 = PN1->Common;
         if (RealCommon0 == PN1->IsCommonReal())
           continue;
-        if (auto CommonNode = (RealCommon0 ? identifyNode(Common0, Common1) :
-                               identifyNode(Common1, Common0))) {
+        if (auto CommonNode = (RealCommon0 ? identifyNode(Common0, Common1)
+                                           : identifyNode(Common1, Common0))) {
           PN0->CommonNode = CommonNode;
           PN1->CommonNode = CommonNode;
           break;
@@ -794,8 +796,9 @@ ComplexDeinterleavingGraph::identifyPartialMul(Instruction *Real,
       }
       if (!PN0->CommonNode) {
         auto PoisonCommon = PoisonValue::get(Common0->getType());
-        if (auto CommonNode = (RealCommon0 ? identifyNode(Common0, PoisonCommon) :
-                               identifyNode(PoisonCommon, Common0))) {
+        if (auto CommonNode =
+                (RealCommon0 ? identifyNode(Common0, PoisonCommon)
+                             : identifyNode(PoisonCommon, Common0))) {
           PN0->CommonNode = CommonNode;
           continue;
         }
@@ -823,13 +826,14 @@ ComplexDeinterleavingGraph::identifyPartialMul(Instruction *Real,
     return CN;
   };
 
-  SmallVector<Product,2> RealMuls{};
-  SmallVector<Product,2> ImagMuls{};
+  SmallVector<Product, 2> RealMuls{};
+  SmallVector<Product, 2> ImagMuls{};
   Addend RealAddend{nullptr, true};
   Addend ImagAddend{nullptr, true};
   if (!ProcessInst(Real, RealPositive, RealMuls, RealAddend) ||
       !ProcessInst(Imag, ImagPositive, ImagMuls, ImagAddend)) {
-    LLVM_DEBUG(dbgs() << "  - Failed to match PartialMul in Real/Imag terms.\n");
+    LLVM_DEBUG(
+        dbgs() << "  - Failed to match PartialMul in Real/Imag terms.\n");
     if (PN && RealPositive == ImagPositive) {
       auto CN = identifyNode(Real, Imag);
       if (CN) {
@@ -839,8 +843,8 @@ ComplexDeinterleavingGraph::identifyPartialMul(Instruction *Real,
         });
         return MatchCommons(PN, CN, RealPositive);
       }
-      LLVM_DEBUG(dbgs() << "  - Failed to match Addends "
-                 << *Real << " / " << *Imag << ".\n");
+      LLVM_DEBUG(dbgs() << "  - Failed to match Addends " << *Real << " / "
+                        << *Imag << ".\n");
     }
     return nullptr;
   }
@@ -848,86 +852,86 @@ ComplexDeinterleavingGraph::identifyPartialMul(Instruction *Real,
   if (RealMuls.size() != ImagMuls.size())
     return nullptr;
 
-  auto ForeachMatch = [&](Product RealMul, Product ImagMul,
-                          PartialMulNode *PN, auto &&cb) -> CompositeNode* {
+  auto ForeachMatch = [&](Product RealMul, Product ImagMul, PartialMulNode *PN,
+                          auto &&cb) -> CompositeNode * {
     PartialMulNode NewPN{};
     NewPN.prev = PN;
     if (RealMul.IsPositive) {
-      NewPN.Rotation = (ImagMul.IsPositive ?
-                        ComplexDeinterleavingRotation::Rotation_0 :
-                        ComplexDeinterleavingRotation::Rotation_270);
-    }
-    else {
-      NewPN.Rotation = (ImagMul.IsPositive ?
-                        ComplexDeinterleavingRotation::Rotation_90 :
-                        ComplexDeinterleavingRotation::Rotation_180);
-    }
-    auto IdentifyUncommon = [&] (Value *Real, Value *Imag) {
-      return (NewPN.IsCommonReal() ? identifyNode(Real, Imag) :
-              identifyNode(Imag, Real));
+      NewPN.Rotation =
+          (ImagMul.IsPositive ? ComplexDeinterleavingRotation::Rotation_0
+                              : ComplexDeinterleavingRotation::Rotation_270);
+    } else {
+      NewPN.Rotation =
+          (ImagMul.IsPositive ? ComplexDeinterleavingRotation::Rotation_90
+                              : ComplexDeinterleavingRotation::Rotation_180);
+    }
+    auto IdentifyUncommon = [&](Value *Real, Value *Imag) {
+      return (NewPN.IsCommonReal() ? identifyNode(Real, Imag)
+                                   : identifyNode(Imag, Real));
     };
     if (RealMul.Multiplier == ImagMul.Multiplier &&
-        (NewPN.UncommonNode = IdentifyUncommon(RealMul.Multiplicand,
-                                               ImagMul.Multiplicand))) {
-        NewPN.Common = RealMul.Multiplier;
-        if (auto CN = cb(&NewPN)) {
-          return CN;
-        }
+        (NewPN.UncommonNode =
+             IdentifyUncommon(RealMul.Multiplicand, ImagMul.Multiplicand))) {
+      NewPN.Common = RealMul.Multiplier;
+      if (auto CN = cb(&NewPN)) {
+        return CN;
+      }
     }
     if (ImagMul.Multiplicand != ImagMul.Multiplier &&
         RealMul.Multiplier == ImagMul.Multiplicand &&
-        (NewPN.UncommonNode = IdentifyUncommon(RealMul.Multiplicand,
-                                               ImagMul.Multiplier))) {
-        NewPN.Common = RealMul.Multiplier;
-        if (auto CN = cb(&NewPN)) {
-          return CN;
-        }
+        (NewPN.UncommonNode =
+             IdentifyUncommon(RealMul.Multiplicand, ImagMul.Multiplier))) {
+      NewPN.Common = RealMul.Multiplier;
+      if (auto CN = cb(&NewPN)) {
+        return CN;
+      }
     }
     if (RealMul.Multiplicand == RealMul.Multiplier)
       return nullptr;
     if (RealMul.Multiplicand == ImagMul.Multiplier &&
-        (NewPN.UncommonNode = IdentifyUncommon(RealMul.Multiplier,
-                                               ImagMul.Multiplicand))) {
-        NewPN.Common = RealMul.Multiplicand;
-        if (auto CN = cb(&NewPN)) {
-          return CN;
-        }
+        (NewPN.UncommonNode =
+             IdentifyUncommon(RealMul.Multiplier, ImagMul.Multiplicand))) {
+      NewPN.Common = RealMul.Multiplicand;
+      if (auto CN = cb(&NewPN)) {
+        return CN;
+      }
     }
     if (ImagMul.Multiplicand != ImagMul.Multiplier &&
         RealMul.Multiplicand == ImagMul.Multiplicand &&
-        (NewPN.UncommonNode = IdentifyUncommon(RealMul.Multiplier,
-                                               ImagMul.Multiplier))) {
-        NewPN.Common = RealMul.Multiplicand;
-        if (auto CN = cb(&NewPN)) {
-          return CN;
-        }
+        (NewPN.UncommonNode =
+             IdentifyUncommon(RealMul.Multiplier, ImagMul.Multiplier))) {
+      NewPN.Common = RealMul.Multiplicand;
+      if (auto CN = cb(&NewPN)) {
+        return CN;
+      }
     }
     return nullptr;
   };
 
   if (RealMuls.size() == 1) {
     assert(RealAddend.first && ImagAddend.first);
-    if (!isa<Instruction>(RealAddend.first) || !isa<Instruction>(ImagAddend.first)) {
+    if (!isa<Instruction>(RealAddend.first) ||
+        !isa<Instruction>(ImagAddend.first)) {
       if (RealAddend.second != ImagAddend.second)
         return nullptr;
       auto CN = identifyNode(RealAddend.first, ImagAddend.first);
-      return ForeachMatch(RealMuls[0], ImagMuls[0], PN, [&](PartialMulNode *PN) {
-        return MatchCommons(PN, CN, RealAddend.second);
-      });
+      return ForeachMatch(RealMuls[0], ImagMuls[0], PN,
+                          [&](PartialMulNode *PN) {
+                            return MatchCommons(PN, CN, RealAddend.second);
+                          });
     }
     return ForeachMatch(RealMuls[0], ImagMuls[0], PN, [&](PartialMulNode *PN) {
       return identifyPartialMul(cast<Instruction>(RealAddend.first),
                                 cast<Instruction>(ImagAddend.first),
                                 RealAddend.second, ImagAddend.second, PN);
     });
-  }
-  else {
+  } else {
     assert(RealMuls.size() == 2);
     assert(!RealAddend.first && !ImagAddend.first);
     return ForeachMatch(RealMuls[0], ImagMuls[0], PN, [&](PartialMulNode *PN) {
-      return ForeachMatch(RealMuls[1], ImagMuls[1], PN, [&](PartialMulNode *PN) {
-        return MatchCommons(PN, nullptr, true);
-      });
+      return ForeachMatch(
+          RealMuls[1], ImagMuls[1], PN,
+          [&](PartialMulNode *PN) { return MatchCommons(PN, nullptr, true); });
     });
   }
 }
@@ -1305,8 +1309,7 @@ ComplexDeinterleavingGraph::identifyReassocNodes(Instruction *Real,
            Opcode == Instruction::Sub;
   };
 
-  if (!IsOperationSupported(Real) ||
-      !IsOperationSupported(Imag))
+  if (!IsOperationSupported(Real) || !IsOperationSupported(Imag))
     return nullptr;
 
   std::optional<FastMathFlags> Flags;
@@ -1335,7 +1338,8 @@ ComplexDeinterleavingGraph::identifyReassocNodes(Instruction *Real,
   // Collect multiplications and addend instructions from the given instruction
   // while traversing it operands. Additionally, verify that all instructions
   // have the same fast math flags.
-  auto Collect = [&UpdateFlags](Instruction *Insn, SmallVectorImpl<Product> &Muls,
+  auto Collect = [&UpdateFlags](Instruction *Insn,
+                                SmallVectorImpl<Product> &Muls,
                                 AddendList &Addends) -> bool {
     SmallVector<PointerIntPair<Value *, 1, bool>> Worklist = {{Insn, true}};
     SmallPtrSet<Value *, 8> Visited;
@@ -1447,8 +1451,8 @@ ComplexDeinterleavingGraph::identifyReassocNodes(Instruction *Real,
         AddendPositive = false;
       }
     }
-    FinalNode = identifyMultiplications(RealMuls, ImagMuls, FinalNode,
-                                        AddendPositive);
+    FinalNode =
+        identifyMultiplications(RealMuls, ImagMuls, FinalNode, AddendPositive);
     if (!FinalNode)
       return nullptr;
   }
@@ -2173,16 +2177,16 @@ ComplexDeinterleavingGraph::identifyDeinterleave(ComplexValues &Vals) {
   };
 
   auto CheckValue = [&](Value *V, unsigned ExpectedIdx) {
-      if (isa<PoisonValue>(V))
-        return true;
-      auto EVI = CheckExtract(V, ExpectedIdx, II);
-      if (!EVI) {
-        II = nullptr;
-        return false;
-      }
-      if (!II)
-        II = cast<Instruction>(EVI->getAggregateOperand());
+    if (isa<PoisonValue>(V))
       return true;
+    auto EVI = CheckExtract(V, ExpectedIdx, II);
+    if (!EVI) {
+      II = nullptr;
+      return false;
+    }
+    if (!II)
+      II = cast<Instruction>(EVI->getAggregateOperand());
+    return true;
   };
 
   for (unsigned Idx = 0; Idx < Vals.size(); Idx++) {
@@ -2231,8 +2235,7 @@ ComplexDeinterleavingGraph::identifyDeinterleave(ComplexValues &Vals) {
   if (!RealShuffle) {
     Op0 = ImagShuffle->getOperand(0);
     ShuffleTy = cast<FixedVectorType>(ImagShuffle->getType());
-  }
-  else {
+  } else {
     Op0 = RealShuffle->getOperand(0);
     ShuffleTy = cast<FixedVectorType>(RealShuffle->getType());
     if (ImagShuffle) {
@@ -2257,12 +2260,13 @@ ComplexDeinterleavingGraph::identifyDeinterleave(ComplexValues &Vals) {
     return nullptr;
   }
 
-  auto CheckShuffle = [&](ShuffleVectorInst *Shuffle, int Mask0, const char *Name) -> bool {
+  auto CheckShuffle = [&](ShuffleVectorInst *Shuffle, int Mask0,
+                          const char *Name) -> bool {
     if (!Shuffle) // Poison value
       return true;
     Value *Op1 = Shuffle->getOperand(1);
     if (!isa<UndefValue>(Op1) && !isa<ConstantAggregateZero>(Op1)) {
-        LLVM_DEBUG(dbgs() << " - " << Name << "Op1 is not undef or zero.\n");
+      LLVM_DEBUG(dbgs() << " - " << Name << "Op1 is not undef or zero.\n");
       return false;
     }
     ArrayRef<int> Mask = Shuffle->getShuffleMask();
@@ -2271,7 +2275,8 @@ ComplexDeinterleavingGraph::identifyDeinterleave(ComplexValues &Vals) {
       return false;
     }
     if (Mask[0] != Mask0) {
-      LLVM_DEBUG(dbgs() << " - " << Name << "Masks do not have the correct initial value.\n");
+      LLVM_DEBUG(dbgs() << " - " << Name
+                        << "Masks do not have the correct initial value.\n");
       return false;
     }
     // Ensure that the deinterleaving shuffle only pulls from the first

``````````

</details>


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


More information about the llvm-commits mailing list