[PATCH] D136692: [FuncSpec] Do not overestime the specialization bonus for users inside loops.

Alexandros Lamprineas via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 25 08:18:03 PDT 2022


labrinea created this revision.
labrinea added reviewers: SjoerdMeijer, ChuanqiXu, momchil.velikov.
Herald added subscribers: snehasish, ormris, hiraditya.
Herald added a project: All.
labrinea requested review of this revision.
Herald added a project: LLVM.

When calculating the specialization bonus for a given function argument, we recursively traverse the chain of (certain) users, accumulating the instruction costs. Then we exponentially increase the bonus to account for loop nests. This is problematic for two reasons: (a) the users might not themselves be inside the loop nest, (b) if they are we are accounting for it multiple times. Instead we should be adjusting the bonus before traversing the user chain.

This reduces the instruction count for CTMark (newPM-O3) when Function Specialization is enabled without actually reducing the amount of specializations performed.

| **testname**     | **delta % non-LTO** | **delta % LTO** |
| ClamAV           | -0.005              | 0.039           |
| 7zip             | 0.012               | -0.007          |
| tramp3d-v4       | -0.013              | -0.011          |
| kimwitu++        | -0.011              | 0.146           |
| sqlite3          | 0.04                | -0.445          |
| mafft            | 0.006               | 0.011           |
| lencod           | -0.02               | -0.023          |
| SPASS            | -0.006              | -1.06           |
| consumer-typeset | 0.005               | -2.644          |
| Bullet           | -0.015              | -0.029          |
| geomean          | -0.001              | -0.406          |


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136692

Files:
  llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
  llvm/test/Transforms/FunctionSpecialization/function-specialization-loop.ll


Index: llvm/test/Transforms/FunctionSpecialization/function-specialization-loop.ll
===================================================================
--- llvm/test/Transforms/FunctionSpecialization/function-specialization-loop.ll
+++ llvm/test/Transforms/FunctionSpecialization/function-specialization-loop.ll
@@ -1,4 +1,4 @@
-; RUN: opt -function-specialization -func-specialization-avg-iters-cost=3 -func-specialization-size-threshold=10 -S < %s | FileCheck %s
+; RUN: opt -function-specialization -func-specialization-avg-iters-cost=5 -func-specialization-size-threshold=10 -S < %s | FileCheck %s
 
 ; Check that the loop depth results in a larger specialization bonus.
 ; CHECK: @foo.1(
@@ -60,4 +60,4 @@
 return:
   %retval.0 = phi i32 [ %call, %if.then ], [ %call1, %if.else ]
   ret i32 %retval.0
-}
\ No newline at end of file
+}
Index: llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
===================================================================
--- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -576,15 +576,16 @@
     InstructionCost Cost =
         TTI.getInstructionCost(U, TargetTransformInfo::TCK_SizeAndLatency);
 
+    // Increase the cost if it is inside the loop.
+    unsigned LoopDepth = LI.getLoopDepth(I->getParent());
+    Cost *= std::pow((double)AvgLoopIterationCount, LoopDepth);
+
     // Traverse recursively if there are more uses.
     // TODO: Any other instructions to be added here?
     if (I->mayReadFromMemory() || I->isCast())
       for (auto *User : I->users())
         Cost += getUserBonus(User, TTI, LI);
 
-    // Increase the cost if it is inside the loop.
-    auto LoopDepth = LI.getLoopDepth(I->getParent());
-    Cost *= std::pow((double)AvgLoopIterationCount, LoopDepth);
     return Cost;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136692.470494.patch
Type: text/x-patch
Size: 1820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221025/7ffa475a/attachment.bin>


More information about the llvm-commits mailing list