Skip to content

Commit 3194154

Browse files
committed
Address comments from tschuett
1 parent 06a2500 commit 3194154

File tree

4 files changed

+51
-51
lines changed

4 files changed

+51
-51
lines changed

llvm/include/llvm/Passes/CodeGenPassBuilder.h

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
#include "llvm/Target/CGPassBuilderOption.h"
7878
#include "llvm/Target/TargetMachine.h"
7979
#include "llvm/Transforms/CFGuard.h"
80-
#include "llvm/Transforms/IPO/GlobalMergeFunctions.h"
8180
#include "llvm/Transforms/Scalar/ConstantHoisting.h"
8281
#include "llvm/Transforms/Scalar/LoopPassManager.h"
8382
#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"

llvm/include/llvm/Transforms/IPO.h

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ enum class PassSummaryAction {
5555
Export, ///< Export information to summary.
5656
};
5757

58+
/// createGlobalMergeFuncPass - This pass generates merged instances by
59+
/// parameterizing distinct constants across similar functions, utilizing stable
60+
/// function hash information.
5861
Pass *createGlobalMergeFuncPass();
5962

6063
} // End llvm namespace

llvm/include/llvm/Transforms/IPO/GlobalMergeFunctions.h

+23-22
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,29 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
///
9-
/// This file defines global merge functions pass and related data structure.
10-
///
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+
//
1123
//===----------------------------------------------------------------------===//
1224

13-
#ifndef PIKA_TRANSFORMS_UTILS_GLOBALMERGEFUNCTIONS_H
14-
#define PIKA_TRANSFORMS_UTILS_GLOBALMERGEFUNCTIONS_H
25+
#ifndef LLVM_TRANSFORMS_IPO_GLOBALMERGEFUNCTIONS_H
26+
#define LLVM_TRANSFORMS_IPO_GLOBALMERGEFUNCTIONS_H
1527

16-
#include "llvm/ADT/DenseMap.h"
17-
#include "llvm/ADT/StableHashing.h"
18-
#include "llvm/ADT/StringRef.h"
1928
#include "llvm/CGData/StableFunctionMap.h"
20-
#include "llvm/IR/Instructions.h"
2129
#include "llvm/IR/Module.h"
2230
#include "llvm/Pass.h"
23-
#include <map>
24-
#include <mutex>
2531

2632
enum class HashFunctionMode {
2733
Local,
@@ -36,15 +42,10 @@ namespace llvm {
3642
using ParamLocs = SmallVector<IndexPair, 4>;
3743
// A vector of parameters
3844
using ParamLocsVecTy = SmallVector<ParamLocs, 8>;
39-
// A map of stable hash to a vector of stable functions
40-
41-
/// GlobalMergeFunc finds functions which only differ by constants in
42-
/// certain instructions, e.g. resulting from specialized functions of layout
43-
/// compatible types.
44-
/// Unlike PikaMergeFunc that directly compares IRs, this uses stable function
45-
/// hash to find the merge candidate. Similar to the global outliner, we can run
46-
/// codegen twice to collect function merge candidate in the first round, and
47-
/// merge functions globally in the second round.
45+
46+
/// GlobalMergeFunc is a ModulePass that implements a function merging mechanism
47+
/// using stable function hashes. It identifies and merges functions with
48+
/// matching hashes across modules to optimize binary size.
4849
class GlobalMergeFunc : public ModulePass {
4950
HashFunctionMode MergerMode = HashFunctionMode::Local;
5051

@@ -69,9 +70,9 @@ class GlobalMergeFunc : public ModulePass {
6970
/// Emit LocalFunctionMap into __llvm_merge section.
7071
void emitFunctionMap(Module &M);
7172

72-
/// Merge functions in the module using the global function map.
73+
/// Merge functions in the module using the given function map.
7374
bool merge(Module &M, const StableFunctionMap *FunctionMap);
7475
};
7576

7677
} // end namespace llvm
77-
#endif // PIKA_TRANSFORMS_UTILS_GLOBALMERGEFUNCTIONS_H
78+
#endif // LLVM_TRANSFORMS_IPO_GLOBALMERGEFUNCTIONS_H

llvm/lib/Transforms/IPO/GlobalMergeFunctions.cpp

+25-28
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// TODO: This implements a function merge using function hash while tracking
10-
// differences in Constants. This uses stable function hash to find potential
11-
// merge candidates. The first codegen round collects stable function hashes,
12-
// and determines the merge candidates that match the stable function hashes.
13-
// The set of parameters pointing to different Constants are also computed
14-
// during the stable function merge. The second codegen round uses this global
15-
// function info to optimistically create a merged function in each module
16-
// context to guarantee correct transformation. Similar to the global outliner,
17-
// the linker's deduplication (ICF) folds the identical merged functions to save
18-
// the final binary size.
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+
// create 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.
1922
//
2023
//===----------------------------------------------------------------------===//
2124

2225
#include "llvm/Transforms/IPO/GlobalMergeFunctions.h"
2326
#include "llvm/ADT/Statistic.h"
2427
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
2528
#include "llvm/CGData/CodeGenData.h"
26-
#include "llvm/CGData/StableFunctionMap.h"
27-
#include "llvm/CodeGen/MachineStableHash.h"
28-
#include "llvm/CodeGen/Passes.h"
2929
#include "llvm/IR/IRBuilder.h"
3030
#include "llvm/IR/StructuralHash.h"
3131
#include "llvm/InitializePasses.h"
@@ -84,7 +84,7 @@ STATISTIC(NumAnalyzedModues, "Number of modules that are analyzed");
8484
STATISTIC(NumAnalyzedFunctions, "Number of functions that are analyzed");
8585
STATISTIC(NumEligibleFunctions, "Number of functions that are eligible");
8686

87-
/// Returns true if the \opIdx operand of \p CI is the callee operand.
87+
/// Returns true if the \OpIdx operand of \p CI is the callee operand.
8888
static bool isCalleeOperand(const CallBase *CI, unsigned OpIdx) {
8989
return &CI->getCalledOperandUse() == &CI->getOperandUse(OpIdx);
9090
}
@@ -148,22 +148,19 @@ bool isEligibleFunction(Function *F) {
148148
if (F->hasFnAttribute(llvm::Attribute::NoMerge))
149149
return false;
150150

151-
if (F->hasAvailableExternallyLinkage()) {
151+
if (F->hasAvailableExternallyLinkage())
152152
return false;
153-
}
154153

155-
if (F->getFunctionType()->isVarArg()) {
154+
if (F->getFunctionType()->isVarArg())
156155
return false;
157-
}
158156

159157
if (F->getCallingConv() == CallingConv::SwiftTail)
160158
return false;
161159

162-
// if function contains callsites with musttail, if we merge
160+
// If function contains callsites with musttail, if we merge
163161
// it, the merged function will have the musttail callsite, but
164162
// the number of parameters can change, thus the parameter count
165163
// of the callsite will mismatch with the function itself.
166-
// if (IgnoreMusttailFunction) {
167164
for (const BasicBlock &BB : *F) {
168165
for (const Instruction &I : BB) {
169166
const auto *CB = dyn_cast<CallBase>(&I);
@@ -203,7 +200,6 @@ static bool ignoreOp(const Instruction *I, unsigned OpIdx) {
203200
return true;
204201
}
205202

206-
// copy from merge functions.cpp
207203
static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) {
208204
Type *SrcTy = V->getType();
209205
if (SrcTy->isStructTy()) {
@@ -252,7 +248,8 @@ void GlobalMergeFunc::analyze(Module &M) {
252248

253249
auto FI = llvm::StructuralHashWithDifferences(Func, ignoreOp);
254250

255-
// Convert the map to a vector for a serialization-friendly format.
251+
// Convert the operand map to a vector for a serialization-friendly
252+
// format.
256253
IndexOperandHashVecType IndexOperandHashes;
257254
for (auto &Pair : *FI.IndexOperandHashMap)
258255
IndexOperandHashes.emplace_back(Pair);
@@ -595,7 +592,7 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
595592
// This module check is not strictly necessary as the functions can move
596593
// around. We just want to avoid merging functions from different
597594
// modules than the first one in the functon map, as they may not end up
598-
// with not being ICFed.
595+
// with not being ICFed by the linker.
599596
if (MergedModId != *FunctionMap->getNameForId(SF->ModuleNameId)) {
600597
++NumMismatchedModuleIdGlobalMergeFunction;
601598
continue;
@@ -616,12 +613,12 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
616613
dbgs() << "[GlobalMergeFunc] Merging function count " << FuncMergeInfoSize
617614
<< " in " << ModId << "\n";
618615
});
616+
619617
for (auto &FMI : FuncMergeInfos) {
620618
Changed = true;
621619

622620
// We've already validated all locations of constant operands pointed by
623-
// the parameters. Just use the first one to bookkeep the original
624-
// constants for each parameter
621+
// the parameters. Populate parameters pointing to the original constants.
625622
SmallVector<Constant *> Params;
626623
SmallVector<Type *> ParamTypes;
627624
for (auto &ParamLocs : ParamLocsVec) {
@@ -633,8 +630,7 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
633630
ParamTypes.push_back(Opnd->getType());
634631
}
635632

636-
// Create a merged function derived from the first function in the current
637-
// module context.
633+
// Create a merged function derived from the current function.
638634
Function *MergedFunc =
639635
createMergedFunction(FMI, ParamTypes, ParamLocsVec);
640636

@@ -645,7 +641,8 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
645641
MergedFunc->dump();
646642
});
647643

648-
// Create a thunk to the merged function.
644+
// Transform the current function into a thunk that calls the merged
645+
// function.
649646
createThunk(FMI, Params, MergedFunc);
650647
LLVM_DEBUG({
651648
dbgs() << "[GlobalMergeFunc] Thunk generated: \n";

0 commit comments

Comments
 (0)