Skip to content

Commit 584a2d7

Browse files
committed
[CGData] Global Merge Functions
1 parent 09f1ec7 commit 584a2d7

16 files changed

+1077
-0
lines changed

llvm/include/llvm/CGData/CodeGenData.h

+11
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ class CodeGenData {
145145
const OutlinedHashTree *getOutlinedHashTree() {
146146
return PublishedHashTree.get();
147147
}
148+
const StableFunctionMap *getStableFunctionMap() {
149+
return PublishedStableFunctionMap.get();
150+
}
148151

149152
/// Returns true if we should write codegen data.
150153
bool emitCGData() { return EmitCGData; }
@@ -169,10 +172,18 @@ inline bool hasOutlinedHashTree() {
169172
return CodeGenData::getInstance().hasOutlinedHashTree();
170173
}
171174

175+
inline bool hasStableFunctionMap() {
176+
return CodeGenData::getInstance().hasStableFunctionMap();
177+
}
178+
172179
inline const OutlinedHashTree *getOutlinedHashTree() {
173180
return CodeGenData::getInstance().getOutlinedHashTree();
174181
}
175182

183+
inline const StableFunctionMap *getStableFunctionMap() {
184+
return CodeGenData::getInstance().getStableFunctionMap();
185+
}
186+
176187
inline bool emitCGData() { return CodeGenData::getInstance().emitCGData(); }
177188

178189
inline void

llvm/include/llvm/InitializePasses.h

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ void initializeGCEmptyBasicBlocksPass(PassRegistry &);
123123
void initializeGCMachineCodeAnalysisPass(PassRegistry &);
124124
void initializeGCModuleInfoPass(PassRegistry &);
125125
void initializeGVNLegacyPassPass(PassRegistry &);
126+
void initializeGlobalMergeFuncPass(PassRegistry &);
126127
void initializeGlobalMergePass(PassRegistry &);
127128
void initializeGlobalsAAWrapperPassPass(PassRegistry &);
128129
void initializeHardwareLoopsLegacyPass(PassRegistry &);

llvm/include/llvm/LinkAllPasses.h

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct ForcePassLinking {
7979
(void)llvm::createDomOnlyViewerWrapperPassPass();
8080
(void)llvm::createDomViewerWrapperPassPass();
8181
(void)llvm::createAlwaysInlinerLegacyPass();
82+
(void)llvm::createGlobalMergeFuncPass();
8283
(void)llvm::createGlobalsAAWrapperPass();
8384
(void)llvm::createInstSimplifyLegacyPass();
8485
(void)llvm::createInstructionCombiningPass();

llvm/include/llvm/Passes/CodeGenPassBuilder.h

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include "llvm/Target/CGPassBuilderOption.h"
7575
#include "llvm/Target/TargetMachine.h"
7676
#include "llvm/Transforms/CFGuard.h"
77+
#include "llvm/Transforms/IPO/GlobalMergeFunctions.h"
7778
#include "llvm/Transforms/Scalar/ConstantHoisting.h"
7879
#include "llvm/Transforms/Scalar/LoopPassManager.h"
7980
#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"

llvm/include/llvm/Transforms/IPO.h

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

58+
Pass *createGlobalMergeFuncPass();
59+
5860
} // End llvm namespace
5961

6062
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 file defines global merge functions pass and related data structure.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef PIKA_TRANSFORMS_UTILS_GLOBALMERGEFUNCTIONS_H
14+
#define PIKA_TRANSFORMS_UTILS_GLOBALMERGEFUNCTIONS_H
15+
16+
#include "llvm/ADT/DenseMap.h"
17+
#include "llvm/ADT/StableHashing.h"
18+
#include "llvm/ADT/StringRef.h"
19+
#include "llvm/CGData/StableFunctionMap.h"
20+
#include "llvm/IR/Instructions.h"
21+
#include "llvm/IR/Module.h"
22+
#include "llvm/Pass.h"
23+
#include <map>
24+
#include <mutex>
25+
26+
enum class HashFunctionMode {
27+
Local,
28+
BuildingHashFuncion,
29+
UsingHashFunction,
30+
};
31+
32+
namespace llvm {
33+
34+
// A vector of locations (the pair of (instruction, operand) indices) reachable
35+
// from a parameter.
36+
using ParamLocs = SmallVector<IndexPair, 4>;
37+
// A vector of parameters
38+
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.
48+
class GlobalMergeFunc : public ModulePass {
49+
HashFunctionMode MergerMode = HashFunctionMode::Local;
50+
51+
std::unique_ptr<StableFunctionMap> LocalFunctionMap;
52+
53+
public:
54+
static char ID;
55+
56+
GlobalMergeFunc();
57+
58+
void initializeMergerMode(const Module &M);
59+
60+
bool runOnModule(Module &M) override;
61+
62+
/// Analyze module to create stable function into LocalFunctionMap.
63+
void analyze(Module &M);
64+
65+
/// Emit LocalFunctionMap into __llvm_merge section.
66+
void emitFunctionMap(Module &M);
67+
68+
/// Merge functions in the module using the global function map.
69+
bool merge(Module &M, const StableFunctionMap *FunctionMap);
70+
};
71+
72+
} // end namespace llvm
73+
#endif // PIKA_TRANSFORMS_UTILS_GLOBALMERGEFUNCTIONS_H

llvm/lib/CodeGen/TargetPassConfig.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ static cl::opt<RunOutliner> EnableMachineOutliner(
141141
"Disable all outlining"),
142142
// Sentinel value for unspecified option.
143143
clEnumValN(RunOutliner::AlwaysOutline, "", "")));
144+
cl::opt<bool> EnableGlobalMergeFunc(
145+
"enable-global-merge-func", cl::Hidden,
146+
cl::desc("Enable global merge functions that are based on hash function"));
144147
// Disable the pass to fix unwind information. Whether the pass is included in
145148
// the pipeline is controlled via the target options, this option serves as
146149
// manual override.

llvm/lib/LTO/LTO.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "llvm/Support/raw_ostream.h"
5454
#include "llvm/Target/TargetOptions.h"
5555
#include "llvm/Transforms/IPO.h"
56+
#include "llvm/Transforms/IPO/GlobalMergeFunctions.h"
5657
#include "llvm/Transforms/IPO/MemProfContextDisambiguation.h"
5758
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
5859
#include "llvm/Transforms/Utils/FunctionImportUtils.h"

llvm/lib/Transforms/IPO/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_llvm_component_library(LLVMipo
2121
GlobalDCE.cpp
2222
GlobalOpt.cpp
2323
GlobalSplit.cpp
24+
GlobalMergeFunctions.cpp
2425
HotColdSplitting.cpp
2526
IPO.cpp
2627
IROutliner.cpp
@@ -60,6 +61,7 @@ add_llvm_component_library(LLVMipo
6061
Analysis
6162
BitReader
6263
BitWriter
64+
CGData
6365
Core
6466
FrontendOpenMP
6567
InstCombine

0 commit comments

Comments
 (0)