[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);
----------------
davemgreen wrote:

This is done in some other places already (The unroller uses CodeMetrics to get the LoopSize). If we are not going to use that and our budget is quite small (6), then can we loop until we are over the budget then return false? It will help with larger loops.
Also just use `TTI.getInstructionCost(&I, TTI::TCK_CodeSize);` to let the function grab the operands.

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


More information about the llvm-commits mailing list