[GSoC'20]Improve MegreFunctions to incorporate MergeSimilarFunctions patches and ThinLTO Support


Briefly summarizing the results of my GSoC’20 project here as it’ll be easy for someone working in the future to search.

  • A protected method has been declared in each subclass of the IR which says how two instructions of a specific IR type can be compared. This will help in introducing total ordering between IR instructions during comparison to merge. Also, a method has been added in the “Instruction” class which compares two IR instructions making use of the newly added protected method. This approach minimizes the number of files to touch for a change in IR instructions. But, when there’s a new operand is added to an instruction and the comparator method has not been updated, there’s no error generated but this can be easily checked as the comparator method lies in the subclass level which is where the new operand will have to be added. Testcases have been added for each IR type to avoid such a scenario. This approach provides methods which act as a centralized place for IR comparison.

  • The MergeSimilarFunctions pass has been integrated into the MergeFunctions pass. The pass now needs a parameter which tells whether to do only precise merging(merge exact replicates of functions) or partial merging(merge functions which are similar) too. The MergeSimilarFunctions makes use of a newly added class, PartialFunctionComparator, which compares functions and stores the differing instructions and also records the degree of similarity between functions. Both passes delegate the comparison of IR to the newly added protected methods(through FunctionComparator classes) for IR comparison so that there won’t be any mis-compiles if the comparator method is updated.


  1. https://reviews.llvm.org/D82892 ([NFC] Methods to compare IR added in each IR subclass)
  2. https://reviews.llvm.org/D85616 (Improved MergeFunctions to merge similar functions)

The benchmarks and a much more detailed report explaining the alternate approach tried can be found at https://gist.github.com/vish99/7f1454a03d78d466989b6c982ccf59dc