Skip to content

Commit f0022a5

Browse files
committed
Add an LLVM pass to merge similar functions.
It's like LLVM's MergeFunctions pass, except that it can also merge functions which differ by some constants. The intention is to merge specialized functions which only differ by metadata lookups. But it can also merge other types of functions. It gives ~7% code size reducation for the stdlib. There are still some open TODOs, e.g. to share common code with LLVM's MergeFunctions pass (currently much code is just copied).
1 parent 74424b0 commit f0022a5

File tree

7 files changed

+2467
-1
lines changed

7 files changed

+2467
-1
lines changed

include/swift/LLVMPasses/PassesFwd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ namespace llvm {
2525
void initializeSwiftARCContractPass(PassRegistry &);
2626
void initializeSwiftStackPromotionPass(PassRegistry &);
2727
void initializeInlineTreePrinterPass(PassRegistry &);
28+
void initializeSwiftMergeFunctionsPass(PassRegistry &);
2829
}
2930

3031
namespace swift {
3132
llvm::FunctionPass *createSwiftARCOptPass();
3233
llvm::FunctionPass *createSwiftARCContractPass();
3334
llvm::FunctionPass *createSwiftStackPromotionPass();
3435
llvm::ModulePass *createInlineTreePrinterPass();
36+
llvm::ModulePass *createSwiftMergeFunctionsPass();
3537
llvm::ImmutablePass *createSwiftAAWrapperPass();
3638
llvm::ImmutablePass *createSwiftRCIdentityPass();
3739
} // end namespace swift

lib/IRGen/IRGen.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ static void addSwiftStackPromotionPass(const PassManagerBuilder &Builder,
8484
PM.add(createSwiftStackPromotionPass());
8585
}
8686

87+
static void addSwiftMergeFunctionsPass(const PassManagerBuilder &Builder,
88+
PassManagerBase &PM) {
89+
if (Builder.OptLevel > 0)
90+
PM.add(createSwiftMergeFunctionsPass());
91+
}
92+
8793
static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
8894
legacy::PassManagerBase &PM) {
8995
PM.add(createAddressSanitizerFunctionPass());
@@ -163,6 +169,8 @@ void swift::performLLVMOptimizations(IRGenOptions &Opts, llvm::Module *Module,
163169
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
164170
addThreadSanitizerPass);
165171
}
172+
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
173+
addSwiftMergeFunctionsPass);
166174

167175
// Configure the function passes.
168176
legacy::FunctionPassManager FunctionPasses(Module);

lib/LLVMPasses/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_swift_library(swiftLLVMPasses
55
LLVMARCContract.cpp
66
LLVMInlineTree.cpp
77
LLVMStackPromotion.cpp
8+
LLVMMergeFunctions.cpp
89
)
910

1011
add_dependencies(swiftLLVMPasses LLVMAnalysis)

0 commit comments

Comments
 (0)