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

Tobias Edler von Koch via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 6 09:44:18 PDT 2016


tobiasvk created this revision.
tobiasvk added a subscriber: llvm-commits.
Herald added a subscriber: jfb.

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

http://reviews.llvm.org/D22051

Files:
  include/llvm/InitializePasses.h
  include/llvm/LinkAllPasses.h
  include/llvm/Transforms/IPO.h
  lib/Transforms/IPO/CMakeLists.txt
  lib/Transforms/IPO/IPO.cpp
  lib/Transforms/IPO/MergeSimilarFunctions.cpp
  test/Transforms/MergeSimilarFunc/
  test/Transforms/MergeSimilarFunc/merge-alloca.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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22051.62893.patch
Type: text/x-patch
Size: 142167 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160706/3a18ab3e/attachment.bin>


More information about the llvm-commits mailing list