[PATCH] D76517: MergeSimilarFunctions 1/n: a code size pass to merge functions with small differences

Vishal Chebrolu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 20 10:50:41 PDT 2020


vish99 created this revision.
vish99 added reviewers: tobiasvk, sebpop, adasgupt, pranavb, tejohnson, SirishP, brzycki.
vish99 added projects: LLVM, clang.

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

[Porting MergeSimilarFunctions 2/n] Changes to DataLayout

Depends on https://reviews.llvm.org/D52896

Maybe I can merge both the patches together?

[Merge SImilar Function ThinLTO 3/n] Add hash code to function summary

Depends on: https://reviews.llvm.org/D52898


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76517

Files:
  llvm/include/llvm/IR/ModuleSummaryIndex.h
  llvm/include/llvm/IR/ModuleSummaryIndexYAML.h
  llvm/include/llvm/InitializePasses.h
  llvm/include/llvm/LinkAllPasses.h
  llvm/include/llvm/Transforms/IPO.h
  llvm/include/llvm/Transforms/Utils/Cloning.h
  llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
  llvm/lib/AsmParser/LLParser.cpp
  llvm/lib/Bitcode/Reader/BitcodeReader.cpp
  llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
  llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp
  llvm/lib/Target/AMDGPU/R600OpenCLImageTypeLoweringPass.cpp
  llvm/lib/Transforms/Coroutines/CoroSplit.cpp
  llvm/lib/Transforms/IPO/CMakeLists.txt
  llvm/lib/Transforms/IPO/IPO.cpp
  llvm/lib/Transforms/IPO/MergeSimilarFunctions.cpp
  llvm/lib/Transforms/IPO/PassManagerBuilder.cpp
  llvm/lib/Transforms/Utils/CloneFunction.cpp
  llvm/lib/Transforms/Utils/CloneModule.cpp
  llvm/test/Transforms/MergeSimilarFunc/merge-alloca.ll
  llvm/test/Transforms/MergeSimilarFunc/merge-debug-info-2.ll
  llvm/test/Transforms/MergeSimilarFunc/merge-debug-info.ll
  llvm/test/Transforms/MergeSimilarFunc/merge-equivalent-template.ll
  llvm/test/Transforms/MergeSimilarFunc/merge-equivalent.ll
  llvm/test/Transforms/MergeSimilarFunc/merge-noinline.ll
  llvm/test/Transforms/MergeSimilarFunc/merge-ret.ll
  llvm/test/Transforms/MergeSimilarFunc/merge-ret2.ll
  llvm/test/Transforms/MergeSimilarFunc/multi-merge.ll
  llvm/test/Transforms/MergeSimilarFunc/self-ref.ll
  llvm/test/Transforms/MergeSimilarFunc/strong-weak.ll
  llvm/unittests/Transforms/Utils/CloningTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76517.251703.patch
Type: text/x-patch
Size: 176439 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200320/0f79d615/attachment-0001.bin>


More information about the cfe-commits mailing list