[PATCH] D52896: MergeSimilarFunctions: a code size pass to merge functions with small differences

Aditya Kumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 4 11:22:09 PDT 2018


hiraditya created this revision.
hiraditya added reviewers: tobiasvk, sebpop, adasgupt, pranavb.
Herald added subscribers: jfb, dexonsmith, eraman, mehdi_amini, mgorny.

Porting the merge similar functions along with bugfixes.
https://reviews.llvm.org/D22051

Not sure if I should commandeer the patch there of create a new one. I am porting this because I have subsequent patches to enable this during thin-lto (https://llvm.org/devmtg/2018-10/talk-abstracts.html#talk2). Existing Function Merging optimization is not as good as this one because this optimization can merge functions with slight differences as well.

------------------------------------------

This patch contains a version of the MergeFunctions pass that is able to merge
not just identical functions, but also functions with small differences in
their instructions to reduce code size. It does this by inserting control flow
and an additional argument in the merged function to account for the
differences.

I originally presented this work at the LLVM Dev Meeting in 2013 [1]. A more
detailed description was published in a paper at LCTES 2014 [2]. The code was
released to the community at the time, but that version has since bitrotted and
also contained a number of bugs. Meanwhile, the pass has been in production use
at QuIC for the past few years and has been actively maintained internally.

I continue to receive requests for this code every few months, so there
definitely seems to be interest for this optimization out there and I'd like to
share it with the community once again. It has now diverged quite significantly
from the upstream MergeFunctions pass, so adding it as a separate, optional
pass would probably make the most sense.

The main disadvantage - which we have not addressed since 2013 - is that the
worst-case behavior of this optimization is quadratic in the number of
functions in the module. However, thanks to hashing, this is very rarely a
problem in practice. In 3 years of production use at QuIC over large code
bases, we've seen just one source file that triggered excessive compile times
(it was auto-generated and contained several thousand nearly-identical small
functions). We even run this in (fat) LTO without problems.

Note that the patch as-is will only attempt to merge functions that were
compiled with -Os. It's best to follow it by a run of jump threading,
instcombine, and simplifycfg to clean up.

[1] http://llvm.org/devmtg/2013-11/#talk3
[2] http://dl.acm.org/citation.cfm?id=2597811


https://reviews.llvm.org/D52896

Files:
  include/llvm/InitializePasses.h
  include/llvm/LinkAllPasses.h
  include/llvm/Transforms/IPO.h
  include/llvm/Transforms/Utils/Cloning.h
  lib/ExecutionEngine/Orc/IndirectionUtils.cpp
  lib/Transforms/Coroutines/CoroSplit.cpp
  lib/Transforms/IPO/CMakeLists.txt
  lib/Transforms/IPO/IPO.cpp
  lib/Transforms/IPO/MergeSimilarFunctions.cpp
  lib/Transforms/IPO/PassManagerBuilder.cpp
  lib/Transforms/Utils/CloneFunction.cpp
  lib/Transforms/Utils/CloneModule.cpp
  test/Transforms/MergeSimilarFunc/merge-alloca.ll
  test/Transforms/MergeSimilarFunc/merge-debug-info-2.ll
  test/Transforms/MergeSimilarFunc/merge-debug-info.ll
  test/Transforms/MergeSimilarFunc/merge-equivalent-template.ll
  test/Transforms/MergeSimilarFunc/merge-equivalent.ll
  test/Transforms/MergeSimilarFunc/merge-noinline.ll
  test/Transforms/MergeSimilarFunc/merge-ret.ll
  test/Transforms/MergeSimilarFunc/merge-ret2.ll
  test/Transforms/MergeSimilarFunc/multi-merge.ll
  test/Transforms/MergeSimilarFunc/self-ref.ll
  test/Transforms/MergeSimilarFunc/strong-weak.ll
  unittests/Transforms/Utils/CloningTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52896.168331.patch
Type: text/x-patch
Size: 156687 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181004/0be1f20d/attachment-0001.bin>


More information about the llvm-commits mailing list