From dbeaf6baa2ad12112e7092f5814396f444733958 Mon Sep 17 00:00:00 2001 From: Alexandros Lamprineas Date: Tue, 25 Oct 2022 16:45:18 +0100 Subject: [PATCH] [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 --- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp | 7 ++++--- .../FunctionSpecialization/function-specialization-loop.ll | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp index 4bf1688..460b92f 100644 --- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -574,15 +574,16 @@ private: 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 c997b05..919f583 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 +} -- 2.7.4