Porting Merge Similar Functions pass to ThinLTO Aditya Kumar - - PowerPoint PPT Presentation

porting merge similar functions pass to thinlto
SMART_READER_LITE
LIVE PREVIEW

Porting Merge Similar Functions pass to ThinLTO Aditya Kumar - - PowerPoint PPT Presentation

Porting Merge Similar Functions pass to ThinLTO Aditya Kumar Facebook ThinLTO (Intro) Source: http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html Function Merging (Intro) Inter-procedural pass: Merge Identical


slide-1
SLIDE 1

Porting “Merge Similar Functions” pass to ThinLTO

Aditya Kumar Facebook

slide-2
SLIDE 2

ThinLTO (Intro)

Source: http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html

slide-3
SLIDE 3

Function Merging (Intro)

  • Inter-procedural pass:
  • Merge Identical Functions (llvm/lib/Transforms/IPO/MergeFunctions.cpp)
  • Merge Similar Functions
  • https://reviews.llvm.org/D22051
  • https://llvm.org/devmtg/2013-11/slides/Koch-FunctionMerging.pdf
  • Deduplicates common parts of similar functions to a separate

function

  • Code size optimization
  • Specially useful for template heavy C++ code bases
slide-4
SLIDE 4

Example

Before After a.c int a_foo(int *a, int N) { int sum = 0; for (int i = 0; i < N; ++i) sum += a[i]; return sum; } int a_foo__merged(int *a, int N) { int sum = 0; for (int i = 0; i < N; ++i) sum += a[i]; return sum; } int a_foo(int *a, int N) { return a_foo__merged(a, N); } b.c int b_foo(int *a, int N) { int sum = 0; for (int i = 0; i < N; ++i) sum += a[i]; return sum; } int b_foo(int *a, int N) { return a_foo__merged(a, N); }

slide-5
SLIDE 5

Steps to port Merge Similar Functions to ThinLTO

Add hash code to function summary Make merge function decisions before the thin-lto stage Set up similar functions to be imported Merge similar functions during the thinlto stage

slide-6
SLIDE 6

Add hash code to function summary

  • Hash some structural and some semantic properties of a function
  • Number of Basic Blocks, calling convention, Has Var-args etc.
  • Type id of all formal parameters, and return type
  • Hashing is fragile to match similar functions
slide-7
SLIDE 7

Add hash code to function summary

  • Modifying Bitcode reader+writer
  • BitcodeReader.cpp
  • Read SimHash when bitcode is: FS_PERMODULE or FS_COMBINED
  • ProcessThinLTOModule
  • BitcodeWriter.cpp
  • writePerModuleFunctionSummaryRecord
slide-8
SLIDE 8

Make merge function decisions

  • Populating Module Summary Index
  • std::map <unsigned, std::vector<GlobalValue::GUID>> SimilarFunctions;
  • std::set <GlobalValue::GUID> HostSimilarFunction;
  • Decide which module is the host
  • Call computeMergeSimilarFunctions before the parallel thinlto stage
slide-9
SLIDE 9

Set up similar functions to be imported

  • FunctionImport.cpp
  • Inter-procedural, sequential pass pre-thinlto
  • For each hosting function in a module, set up similar functions to be

imported.

  • Get hosting functions for a module
  • Find all the functions similar to the hosting function from Module Summary

Index

  • Import the function (and necessary declarations)
slide-10
SLIDE 10

Merge similar functions during the thinlto

  • Schedule the pass in thinlto pipeline
  • After Inliner
  • Codegen passes run in parallel
slide-11
SLIDE 11

Using thinlto in open source projects

  • Gold plugin does not take ‘-Os, -Oz’ flags. So ‘-O3’ was used for

testing.

  • Hard to find projects with ‘simple’ build rules
  • cmake passes linker flags to ‘ar’ e.g., -flto=thin
  • Need to use llvm-ar and llvm-ranlib
slide-12
SLIDE 12

Experimental Results (Open CV Libraries)

  • 10.00
  • 8.00
  • 6.00
  • 4.00
  • 2.00

0.00 2.00 cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

  • pencv_test_gapi

libopencv_dnn.so

  • pencv_test_core

libopencv_imgproc.so libopencv_core.so

Open CV libraries % change in code-size (lower is better)

slide-13
SLIDE 13

Tuning (Cost Models)

  • Disable (forced) inlining for widely used function templates
  • C++ Standard library functions like: find, all_of, any_of
  • Profile Guided hosting of outlined function to reduce page faults
  • Minimum size of functions that should be merged
  • Dis-similarity
slide-14
SLIDE 14

Patches in Review

  • https://reviews.llvm.org/D52896
  • https://reviews.llvm.org/D52898
  • https://reviews.llvm.org/D52966
  • https://reviews.llvm.org/D53253
  • https://reviews.llvm.org/D53254

Facebook is Hiring…