Skip to content

Commit 1d2436e

Browse files
committed
Address comments from nocchijiang
1 parent ac87d92 commit 1d2436e

File tree

18 files changed

+142
-93
lines changed

18 files changed

+142
-93
lines changed

llvm/include/llvm/Transforms/IPO/GlobalMergeFunctions.h renamed to llvm/include/llvm/CodeGen/GlobalMergeFunctions.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
//
2323
//===----------------------------------------------------------------------===//
2424

25-
#ifndef LLVM_TRANSFORMS_IPO_GLOBALMERGEFUNCTIONS_H
26-
#define LLVM_TRANSFORMS_IPO_GLOBALMERGEFUNCTIONS_H
25+
#ifndef LLVM_CODEGEN_GLOBALMERGEFUNCTIONS_H
26+
#define LLVM_CODEGEN_GLOBALMERGEFUNCTIONS_H
2727

2828
#include "llvm/CGData/StableFunctionMap.h"
2929
#include "llvm/IR/Module.h"
30+
#include "llvm/IR/PassManager.h"
3031
#include "llvm/Pass.h"
3132

3233
enum class HashFunctionMode {
@@ -46,28 +47,24 @@ using ParamLocsVecTy = SmallVector<ParamLocs, 8>;
4647
/// GlobalMergeFunc is a ModulePass that implements a function merging mechanism
4748
/// using stable function hashes. It identifies and merges functions with
4849
/// matching hashes across modules to optimize binary size.
49-
class GlobalMergeFunc : public ModulePass {
50+
class GlobalMergeFunc {
5051
HashFunctionMode MergerMode = HashFunctionMode::Local;
5152

5253
std::unique_ptr<StableFunctionMap> LocalFunctionMap;
5354

54-
public:
55-
static char ID;
55+
const ModuleSummaryIndex *Index;
5656

57+
public:
5758
/// The suffix used to identify the merged function that parameterizes
5859
/// the constant values. Note that the original function, without this suffix,
5960
/// becomes a thunk supplying contexts to the merged function via parameters.
6061
static constexpr const char MergingInstanceSuffix[] = ".Tgm";
6162

62-
GlobalMergeFunc();
63-
64-
StringRef getPassName() const override;
65-
66-
void getAnalysisUsage(AnalysisUsage &AU) const override;
63+
GlobalMergeFunc(const ModuleSummaryIndex *Index) : Index(Index){};
6764

6865
void initializeMergerMode(const Module &M);
6966

70-
bool runOnModule(Module &M) override;
67+
bool run(Module &M);
7168

7269
/// Analyze module to create stable function into LocalFunctionMap.
7370
void analyze(Module &M);
@@ -79,5 +76,10 @@ class GlobalMergeFunc : public ModulePass {
7976
bool merge(Module &M, const StableFunctionMap *FunctionMap);
8077
};
8178

79+
/// Global function merging pass for new pass manager.
80+
struct GlobalMergeFuncPass : public PassInfoMixin<GlobalMergeFuncPass> {
81+
PreservedAnalyses run(Module &M, AnalysisManager<Module> &);
82+
};
83+
8284
} // end namespace llvm
83-
#endif // LLVM_TRANSFORMS_IPO_GLOBALMERGEFUNCTIONS_H
85+
#endif // LLVM_CODEGEN_GLOBALMERGEFUNCTIONS_H

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ namespace llvm {
507507
/// This pass frees the memory occupied by the MachineFunction.
508508
FunctionPass *createFreeMachineFunctionPass();
509509

510+
/// This pass performs merging similar functions globally.
511+
ModulePass *createGlobalMergeFuncPass();
512+
510513
/// This pass performs outlining on machine instructions directly before
511514
/// printing assembly.
512515
ModulePass *createMachineOutlinerPass(bool RunOnAllFunctions = true);

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void initializeGCEmptyBasicBlocksPass(PassRegistry &);
123123
void initializeGCMachineCodeAnalysisPass(PassRegistry &);
124124
void initializeGCModuleInfoPass(PassRegistry &);
125125
void initializeGVNLegacyPassPass(PassRegistry &);
126-
void initializeGlobalMergeFuncPass(PassRegistry &);
126+
void initializeGlobalMergeFuncPassWrapperPass(PassRegistry &);
127127
void initializeGlobalMergePass(PassRegistry &);
128128
void initializeGlobalsAAWrapperPassPass(PassRegistry &);
129129
void initializeHardwareLoopsLegacyPass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "llvm/CodeGen/FinalizeISel.h"
3636
#include "llvm/CodeGen/GCMetadata.h"
3737
#include "llvm/CodeGen/GlobalMerge.h"
38+
#include "llvm/CodeGen/GlobalMergeFunctions.h"
3839
#include "llvm/CodeGen/IndirectBrExpand.h"
3940
#include "llvm/CodeGen/InterleavedAccess.h"
4041
#include "llvm/CodeGen/InterleavedLoadCombine.h"
@@ -713,6 +714,9 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addIRPasses(
713714
// Convert conditional moves to conditional jumps when profitable.
714715
if (getOptLevel() != CodeGenOptLevel::None && !Opt.DisableSelectOptimize)
715716
addPass(SelectOptimizePass(&TM));
717+
718+
if (Opt.EnableGlobalMergeFunc)
719+
addPass(GlobalMergeFuncPass());
716720
}
717721

718722
/// Turn exception handling constructs into something the code generators can

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ MODULE_PASS("jmc-instrumenter", JMCInstrumenterPass())
2929
MODULE_PASS("lower-emutls", LowerEmuTLSPass())
3030
MODULE_PASS("pre-isel-intrinsic-lowering", PreISelIntrinsicLoweringPass())
3131
MODULE_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass())
32+
MODULE_PASS("global-merge-func", GlobalMergeFuncPass())
3233
#undef MODULE_PASS
3334

3435
#ifndef FUNCTION_ANALYSIS

llvm/include/llvm/Target/CGPassBuilderOption.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct CGPassBuilderOption {
3131
bool DisableVerify = false;
3232
bool EnableImplicitNullChecks = false;
3333
bool EnableBlockPlacementStats = false;
34+
bool EnableGlobalMergeFunc = false;
3435
bool EnableMachineFunctionSplitter = false;
3536
bool MISchedPostRA = false;
3637
bool EarlyLiveIntervals = false;

llvm/include/llvm/Transforms/IPO.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ 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.
61-
Pass *createGlobalMergeFuncPass();
62-
6358
} // End llvm namespace
6459

6560
#endif

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ add_llvm_component_library(LLVMCodeGen
7171
GCMetadataPrinter.cpp
7272
GCRootLowering.cpp
7373
GlobalMerge.cpp
74+
GlobalMergeFunctions.cpp
7475
HardwareLoops.cpp
7576
IfConversion.cpp
7677
ImplicitNullChecks.cpp

llvm/lib/Transforms/IPO/GlobalMergeFunctions.cpp renamed to llvm/lib/CodeGen/GlobalMergeFunctions.cpp

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "llvm/Transforms/IPO/GlobalMergeFunctions.h"
13+
#include "llvm/CodeGen/GlobalMergeFunctions.h"
1414
#include "llvm/ADT/Statistic.h"
1515
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
1616
#include "llvm/CGData/CodeGenData.h"
@@ -56,8 +56,6 @@ static cl::opt<unsigned> GlobalMergingExtraThreshold(
5656
"to be considered beneficial."),
5757
cl::init(0), cl::Hidden);
5858

59-
extern cl::opt<bool> EnableGlobalMergeFunc;
60-
6159
STATISTIC(NumMismatchedFunctionHash,
6260
"Number of mismatched function hash for global merge function");
6361
STATISTIC(NumMismatchedInstCount,
@@ -87,8 +85,12 @@ static bool canParameterizeCallOperand(const CallBase *CI, unsigned OpIdx) {
8785
if (Callee) {
8886
if (Callee->isIntrinsic())
8987
return false;
88+
auto Name = Callee->getName();
9089
// objc_msgSend stubs must be called, and can't have their address taken.
91-
if (Callee->getName().starts_with("objc_msgSend$"))
90+
if (Name.starts_with("objc_msgSend$"))
91+
return false;
92+
// Calls to dtrace probes must generate unique patchpoints.
93+
if (Name.starts_with("__dtrace"))
9294
return false;
9395
}
9496
if (isCalleeOperand(CI, OpIdx) &&
@@ -105,7 +107,8 @@ bool isEligibleFunction(Function *F) {
105107
if (F->isDeclaration())
106108
return false;
107109

108-
if (F->hasFnAttribute(llvm::Attribute::NoMerge))
110+
if (F->hasFnAttribute(llvm::Attribute::NoMerge) ||
111+
F->hasFnAttribute(llvm::Attribute::AlwaysInline))
109112
return false;
110113

111114
if (F->hasAvailableExternallyLinkage())
@@ -552,8 +555,8 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
552555
// Check if the matched functions fall into the same (first) module.
553556
// This module check is not strictly necessary as the functions can move
554557
// around. We just want to avoid merging functions from different
555-
// modules than the first one in the functon map, as they may not end up
556-
// with not being ICFed by the linker.
558+
// modules than the first one in the function map, as they may not end
559+
// up with being ICFed by the linker.
557560
if (MergedModId != *FunctionMap->getNameForId(SF->ModuleNameId)) {
558561
++NumMismatchedModuleId;
559562
continue;
@@ -614,30 +617,6 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
614617
return Changed;
615618
}
616619

617-
char GlobalMergeFunc::ID = 0;
618-
INITIALIZE_PASS_BEGIN(GlobalMergeFunc, "global-merge-func",
619-
"Global merge function pass", false, false)
620-
INITIALIZE_PASS_END(GlobalMergeFunc, "global-merge-func",
621-
"Global merge function pass", false, false)
622-
623-
StringRef GlobalMergeFunc::getPassName() const {
624-
return "Global Merge Functions";
625-
}
626-
627-
void GlobalMergeFunc::getAnalysisUsage(AnalysisUsage &AU) const {
628-
AU.addUsedIfAvailable<ImmutableModuleSummaryIndexWrapperPass>();
629-
AU.setPreservesAll();
630-
ModulePass::getAnalysisUsage(AU);
631-
}
632-
633-
GlobalMergeFunc::GlobalMergeFunc() : ModulePass(ID) {
634-
initializeGlobalMergeFuncPass(*llvm::PassRegistry::getPassRegistry());
635-
}
636-
637-
namespace llvm {
638-
Pass *createGlobalMergeFuncPass() { return new GlobalMergeFunc(); }
639-
} // namespace llvm
640-
641620
void GlobalMergeFunc::initializeMergerMode(const Module &M) {
642621
// Initialize the local function map regardless of the merger mode.
643622
LocalFunctionMap = std::make_unique<StableFunctionMap>();
@@ -646,14 +625,10 @@ void GlobalMergeFunc::initializeMergerMode(const Module &M) {
646625
if (DisableGlobalMerging)
647626
return;
648627

649-
if (auto *IndexWrapperPass =
650-
getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>()) {
651-
auto *TheIndex = IndexWrapperPass->getIndex();
652-
// (Full)LTO module does not have functions added to the index.
653-
// In this case, we run a local merger without using codegen data.
654-
if (TheIndex && !TheIndex->hasExportedFunctions(M))
655-
return;
656-
}
628+
// (Full)LTO module does not have functions added to the index.
629+
// In this case, we run a local merger without using codegen data.
630+
if (Index && !Index->hasExportedFunctions(M))
631+
return;
657632

658633
if (cgdata::emitCGData())
659634
MergerMode = HashFunctionMode::BuildingHashFuncion;
@@ -681,7 +656,7 @@ void GlobalMergeFunc::emitFunctionMap(Module &M) {
681656
Align(4));
682657
}
683658

684-
bool GlobalMergeFunc::runOnModule(Module &M) {
659+
bool GlobalMergeFunc::run(Module &M) {
685660
initializeMergerMode(M);
686661

687662
const StableFunctionMap *FuncMap;
@@ -700,3 +675,58 @@ bool GlobalMergeFunc::runOnModule(Module &M) {
700675

701676
return merge(M, FuncMap);
702677
}
678+
679+
namespace {
680+
681+
class GlobalMergeFuncPassWrapper : public ModulePass {
682+
683+
public:
684+
static char ID;
685+
686+
GlobalMergeFuncPassWrapper();
687+
688+
void getAnalysisUsage(AnalysisUsage &AU) const override {
689+
AU.addUsedIfAvailable<ImmutableModuleSummaryIndexWrapperPass>();
690+
AU.setPreservesAll();
691+
ModulePass::getAnalysisUsage(AU);
692+
}
693+
694+
StringRef getPassName() const override { return "Global Merge Functions"; }
695+
696+
bool runOnModule(Module &M) override;
697+
};
698+
699+
} // namespace
700+
701+
char GlobalMergeFuncPassWrapper::ID = 0;
702+
INITIALIZE_PASS_BEGIN(GlobalMergeFuncPassWrapper, "global-merge-func",
703+
"Global merge function pass", false, false)
704+
INITIALIZE_PASS_END(GlobalMergeFuncPassWrapper, "global-merge-func",
705+
"Global merge function pass", false, false)
706+
707+
namespace llvm {
708+
ModulePass *createGlobalMergeFuncPass() {
709+
return new GlobalMergeFuncPassWrapper();
710+
}
711+
} // namespace llvm
712+
713+
GlobalMergeFuncPassWrapper::GlobalMergeFuncPassWrapper() : ModulePass(ID) {
714+
initializeGlobalMergeFuncPassWrapperPass(
715+
*llvm::PassRegistry::getPassRegistry());
716+
}
717+
718+
bool GlobalMergeFuncPassWrapper::runOnModule(Module &M) {
719+
const ModuleSummaryIndex *Index = nullptr;
720+
if (auto *IndexWrapperPass =
721+
getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>())
722+
Index = IndexWrapperPass->getIndex();
723+
724+
return GlobalMergeFunc(Index).run(M);
725+
}
726+
727+
PreservedAnalyses GlobalMergeFuncPass::run(Module &M,
728+
AnalysisManager<Module> &AM) {
729+
ModuleSummaryIndex *Index = &(AM.getResult<ModuleSummaryIndexAnalysis>(M));
730+
bool Changed = GlobalMergeFunc(Index).run(M);
731+
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
732+
}

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ CGPassBuilderOption llvm::getCGPassBuilderOption() {
492492

493493
SET_BOOLEAN_OPTION(EarlyLiveIntervals)
494494
SET_BOOLEAN_OPTION(EnableBlockPlacementStats)
495+
SET_BOOLEAN_OPTION(EnableGlobalMergeFunc)
495496
SET_BOOLEAN_OPTION(EnableImplicitNullChecks)
496497
SET_BOOLEAN_OPTION(EnableMachineOutliner)
497498
SET_BOOLEAN_OPTION(MISchedPostRA)
@@ -890,6 +891,9 @@ void TargetPassConfig::addIRPasses() {
890891
// Convert conditional moves to conditional jumps when profitable.
891892
if (getOptLevel() != CodeGenOptLevel::None && !DisableSelectOptimize)
892893
addPass(createSelectOptimizePass());
894+
895+
if (EnableGlobalMergeFunc)
896+
addPass(createGlobalMergeFuncPass());
893897
}
894898

895899
/// Turn exception handling constructs into something the code generators can

0 commit comments

Comments
 (0)