Skip to content

Commit 706fb4a

Browse files
committed
[FnSpecialization] Allow functions that are too small to specailize as part of a chain
This way we can still more accurately see the effect of the specialization.
1 parent 9773c8b commit 706fb4a

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ struct Spec {
179179
// Index within AllSpecs
180180
unsigned Loc = 0;
181181

182+
bool SpecializeOnOwn = true;
183+
182184
Spec(Function *F, CallBase *CallSite, const SpecSig &S,
183185
CallSiteStatusT Status)
184186
: F(F), Clone(nullptr), Sig(S), Score(), CodeSize(), FuncSize(),

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,19 @@ bool FunctionSpecializer::runOneSpec(Spec &S, bool Chained, SpecMap &SM,
740740
// If the code metrics reveal that we shouldn't duplicate the function,
741741
// or if the code size implies that this function is easy to get inlined,
742742
// then we shouldn't specialize it.
743-
if (Metrics.notDuplicatable || !Metrics.NumInsts.isValid() ||
744-
(RequireMinSize && Metrics.NumInsts < MinFunctionSize))
743+
if (Metrics.notDuplicatable || !Metrics.NumInsts.isValid())
745744
return false;
746745

746+
if (RequireMinSize && Metrics.NumInsts < MinFunctionSize) {
747+
if (Chained) {
748+
// Want to specialize as part of chain still so we can more accurately
749+
// assess the chain specialization
750+
S.SpecializeOnOwn = false;
751+
} else {
752+
return false;
753+
}
754+
}
755+
747756
// When specialization on literal constants is disabled, only consider
748757
// recursive functions when running multiple times to save wasted analysis,
749758
// as we will not be able to specialize on any newly found literal constant
@@ -800,7 +809,7 @@ bool FunctionSpecializer::run() {
800809

801810
unsigned IndepSpecs = 0;
802811
for (auto &S : AllSpecs)
803-
if (!S.AllChains)
812+
if (S.SpecializeOnOwn && !S.AllChains)
804813
++IndepSpecs;
805814
if (!NumCandidates || !IndepSpecs) {
806815
LLVM_DEBUG(
@@ -813,9 +822,9 @@ bool FunctionSpecializer::run() {
813822
// specialization budget, which is derived from maximum number of
814823
// specializations per specialization candidate function.
815824
auto CompareScore = [&AllSpecs](unsigned I, unsigned J) {
816-
if (AllSpecs[J].AllChains)
825+
if (!AllSpecs[J].SpecializeOnOwn || AllSpecs[J].AllChains)
817826
return true;
818-
if (AllSpecs[I].AllChains)
827+
if (!AllSpecs[I].SpecializeOnOwn || AllSpecs[I].AllChains)
819828
return false;
820829
if (AllSpecs[I].Score != AllSpecs[J].Score)
821830
return AllSpecs[I].Score > AllSpecs[J].Score;

0 commit comments

Comments
 (0)