Function Specialization Pass
authorSjoerd Meijer <sjoerd.meijer@arm.com>
Tue, 4 May 2021 14:12:44 +0000 (15:12 +0100)
committerSjoerd Meijer <sjoerd.meijer@arm.com>
Fri, 11 Jun 2021 08:11:29 +0000 (09:11 +0100)
commitc4a0969b9c14acc795ae9e841b8289c3d36220b1
treec268eaa5d768b785266b8795ddcbcf3c200a77a6
parent22f194909ae24aed817976fb54b759550e90db36
Function Specialization Pass

This adds a function specialization pass to LLVM. Constant parameters
like function pointers and constant globals are propagated to the callee by
specializing the function.

This is a first version with a number of limitations:
- The pass is off by default, so needs to be enabled on the command line,
- It does not handle specialization of recursive functions,
- It does not yet handle constants and constant ranges,
- Only 1 argument per function is specialised,
- The cost-model could be further looked into, and perhaps related,
- We are not yet caching analysis results.

This is based on earlier work by Matthew Simpson (D36432) and Vinay Madhusudan.
More recently this was also discussed on the list, see:

https://lists.llvm.org/pipermail/llvm-dev/2021-March/149380.html.

The motivation for this work is that function specialisation often comes up as
a reason for performance differences of generated code between LLVM and GCC,
which has this enabled by default from optimisation level -O3 and up. And while
this certainly helps a few cpu benchmark cases, this also triggers in real
world codes and is thus a generally useful transformation to have in LLVM.

Function specialisation has great potential to increase compile-times and
code-size.  The summary from some investigations with this patch is:
- Compile-time increases for short compile jobs is high relatively, but the
  increase in absolute numbers still low.
- For longer compile-jobs, the extra compile time is around 1%, and very much
  in line with GCC.
- It is difficult to blame one thing for compile-time increases: it looks like
  everywhere a little bit more time is spent processing more functions and
  instructions.
- But the function specialisation pass itself is not very expensive; it doesn't
  show up very high in the profile of the optimisation passes.

The goal of this work is to reach parity with GCC which means that eventually
we would like to get this enabled by default. But first we would like to address
some of the limitations before that.

Differential Revision: https://reviews.llvm.org/D93838
21 files changed:
llvm/include/llvm/InitializePasses.h
llvm/include/llvm/LinkAllPasses.h
llvm/include/llvm/Transforms/IPO.h
llvm/include/llvm/Transforms/IPO/SCCP.h
llvm/include/llvm/Transforms/Scalar/SCCP.h
llvm/include/llvm/Transforms/Utils/SCCPSolver.h
llvm/lib/Passes/PassBuilder.cpp
llvm/lib/Passes/PassRegistry.def
llvm/lib/Transforms/IPO/IPO.cpp
llvm/lib/Transforms/IPO/PassManagerBuilder.cpp
llvm/lib/Transforms/IPO/SCCP.cpp
llvm/lib/Transforms/Scalar/CMakeLists.txt
llvm/lib/Transforms/Scalar/FunctionSpecialization.cpp [new file with mode: 0644]
llvm/lib/Transforms/Scalar/SCCP.cpp
llvm/lib/Transforms/Utils/SCCPSolver.cpp
llvm/test/Transforms/FunctionSpecialization/function-specialization-recursive.ll [new file with mode: 0644]
llvm/test/Transforms/FunctionSpecialization/function-specialization.ll [new file with mode: 0644]
llvm/test/Transforms/FunctionSpecialization/function-specialization2.ll [new file with mode: 0644]
llvm/test/Transforms/FunctionSpecialization/function-specialization3.ll [new file with mode: 0644]
llvm/test/Transforms/FunctionSpecialization/function-specialization4.ll [new file with mode: 0644]
llvm/test/Transforms/FunctionSpecialization/function-specialization5.ll [new file with mode: 0644]