[llvm] dbeaf6b - [FuncSpec] Do not overestimate the specialization bonus for users inside loops.

Alexandros Lamprineas via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 27 07:39:57 PDT 2022


Author: Alexandros Lamprineas
Date: 2022-10-27T15:26:11+01:00
New Revision: dbeaf6baa2ad12112e7092f5814396f444733958

URL: https://github.com/llvm/llvm-project/commit/dbeaf6baa2ad12112e7092f5814396f444733958
DIFF: https://github.com/llvm/llvm-project/commit/dbeaf6baa2ad12112e7092f5814396f444733958.diff

LOG: [FuncSpec] Do not overestimate the specialization bonus for users inside loops.

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 (geomean: -0.001% non-LTO, -0.406% LTO).

Differential Revision: https://reviews.llvm.org/D136692

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
index 4bf16883dc14..460b92f384ac 100644
--- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -574,15 +574,16 @@ class FunctionSpecializer {
     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;
   }
 

diff  --git a/llvm/test/Transforms/FunctionSpecialization/function-specialization-loop.ll b/llvm/test/Transforms/FunctionSpecialization/function-specialization-loop.ll
index c997b05daff6..919f583b27a8 100644
--- a/llvm/test/Transforms/FunctionSpecialization/function-specialization-loop.ll
+++ b/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 @@ if.else:
 return:
   %retval.0 = phi i32 [ %call, %if.then ], [ %call1, %if.else ]
   ret i32 %retval.0
-}
\ No newline at end of file
+}


        


More information about the llvm-commits mailing list