LLVM 20.0.0git
GlobalMergeFunctions.h
Go to the documentation of this file.
1//===------ GlobalMergeFunctions.h - Global merge functions -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass defines the implementation of a function merging mechanism
10// that utilizes a stable function hash to track differences in constants and
11// identify potential merge candidates. The process involves two rounds:
12// 1. The first round collects stable function hashes and identifies merge
13// candidates with matching hashes. It also computes the set of parameters
14// that point to different constants during the stable function merge.
15// 2. The second round leverages this collected global function information to
16// optimistically create a merged function in each module context, ensuring
17// correct transformation.
18// Similar to the global outliner, this approach uses the linker's deduplication
19// (ICF) to fold identical merged functions, thereby reducing the final binary
20// size. The work is inspired by the concepts discussed in the following paper:
21// https://dl.acm.org/doi/pdf/10.1145/3652032.3657575.
22//
23//===----------------------------------------------------------------------===//
24
25#ifndef LLVM_CODEGEN_GLOBALMERGEFUNCTIONS_H
26#define LLVM_CODEGEN_GLOBALMERGEFUNCTIONS_H
27
29#include "llvm/IR/Module.h"
30#include "llvm/IR/PassManager.h"
31#include "llvm/Pass.h"
32
33enum class HashFunctionMode {
34 Local,
37};
38
39namespace llvm {
40
41// A vector of locations (the pair of (instruction, operand) indices) reachable
42// from a parameter.
44// A vector of parameters
46
47/// GlobalMergeFunc is a ModulePass that implements a function merging mechanism
48/// using stable function hashes. It identifies and merges functions with
49/// matching hashes across modules to optimize binary size.
51 HashFunctionMode MergerMode = HashFunctionMode::Local;
52
53 std::unique_ptr<StableFunctionMap> LocalFunctionMap;
54
55 const ModuleSummaryIndex *Index;
56
57public:
58 /// The suffix used to identify the merged function that parameterizes
59 /// the constant values. Note that the original function, without this suffix,
60 /// becomes a thunk supplying contexts to the merged function via parameters.
61 static constexpr const char MergingInstanceSuffix[] = ".Tgm";
62
63 GlobalMergeFunc(const ModuleSummaryIndex *Index) : Index(Index) {};
64
65 void initializeMergerMode(const Module &M);
66
67 bool run(Module &M);
68
69 /// Analyze module to create stable function into LocalFunctionMap.
70 void analyze(Module &M);
71
72 /// Emit LocalFunctionMap into __llvm_merge section.
73 void emitFunctionMap(Module &M);
74
75 /// Merge functions in the module using the given function map.
76 bool merge(Module &M, const StableFunctionMap *FunctionMap);
77};
78
79/// Global function merging pass for new pass manager.
80struct GlobalMergeFuncPass : public PassInfoMixin<GlobalMergeFuncPass> {
86};
87
88} // end namespace llvm
89#endif // LLVM_CODEGEN_GLOBALMERGEFUNCTIONS_H
HashFunctionMode
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
GlobalMergeFunc is a ModulePass that implements a function merging mechanism using stable function ha...
void analyze(Module &M)
Analyze module to create stable function into LocalFunctionMap.
void initializeMergerMode(const Module &M)
bool merge(Module &M, const StableFunctionMap *FunctionMap)
Merge functions in the module using the given function map.
GlobalMergeFunc(const ModuleSummaryIndex *Index)
void emitFunctionMap(Module &M)
Emit LocalFunctionMap into __llvm_merge section.
static constexpr const char MergingInstanceSuffix[]
The suffix used to identify the merged function that parameterizes the constant values.
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Global function merging pass for new pass manager.
GlobalMergeFuncPass(const ModuleSummaryIndex *ImportSummary)
PreservedAnalyses run(Module &M, AnalysisManager< Module > &)
const ModuleSummaryIndex * ImportSummary
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69