[llvm] [AArch64] Add flag to control unrolling for small multi-exit loops (PR #131998)

David Green via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 28 04:46:25 PDT 2025


================
@@ -4370,6 +4375,81 @@ getFalkorUnrollingPreferences(Loop *L, ScalarEvolution &SE,
   }
 }
 
+static bool shouldUnrollLoopWithInstruction(Instruction &I,
+                                            AArch64TTIImpl &TTI) {
+  // Don't unroll vectorised loop.
+  if (I.getType()->isVectorTy())
+    return false;
+
+  if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
+    if (const Function *F = cast<CallBase>(I).getCalledFunction())
+      if (!TTI.isLoweredToCall(F))
+        return true;
+    return false;
+  }
+
+  return true;
+}
+
+static InstructionCost getSizeOfLoop(Loop *L, AArch64TTIImpl &TTI) {
+  // Estimate the size of the loop.
+  InstructionCost Size = 0;
+  for (auto *BB : L->getBlocks()) {
+    for (auto &I : *BB) {
+      if (!shouldUnrollLoopWithInstruction(I, TTI))
+        return InstructionCost::getInvalid();
+
+      SmallVector<const Value *, 4> Operands(I.operand_values());
+      InstructionCost Cost =
+          TTI.getInstructionCost(&I, Operands, TTI::TCK_CodeSize);
+      // This can happen with intrinsics that don't currently have a cost model
+      // or for some operations that require SVE.
+      if (!Cost.isValid())
+        return InstructionCost::getInvalid();
+      Size += *Cost.getValue();
----------------
davemgreen wrote:

Size += Cost;?

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


More information about the llvm-commits mailing list