Skip to content

Commit 7b14e95

Browse files
committed
Change related to 2nd comments from nocchijiang
- Move isProfitable to offline (llvm-cgdata) - skip-trim flag to skip trimming codegen data. - The default behavior is to trim unnecessary content from the codegen data. - With this flag enabled, we skip trimming, which is often useful to create a synthetic test.
1 parent 7007030 commit 7b14e95

File tree

13 files changed

+94
-81
lines changed

13 files changed

+94
-81
lines changed

llvm/include/llvm/CGData/StableFunctionMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct StableFunctionMap {
110110
size_t size(SizeType Type = UniqueHashCount) const;
111111

112112
/// Finalize the stable function map by trimming content.
113-
void finalize();
113+
void finalize(bool SkipTrim = false);
114114

115115
private:
116116
/// Insert a `StableFunctionEntry` into the function map directly. This

llvm/include/llvm/CGData/StableFunctionMapRecord.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct StableFunctionMapRecord {
4949
void deserializeYAML(yaml::Input &YIS);
5050

5151
/// Finalize the stable function map by trimming content.
52-
void finalize() { FunctionMap->finalize(); }
52+
void finalize(bool SkipTrim = false) { FunctionMap->finalize(SkipTrim); }
5353

5454
/// Merge the stable function map into this one.
5555
void merge(const StableFunctionMapRecord &Other) {

llvm/lib/CGData/StableFunctionMap.cpp

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,43 @@
1414
//===----------------------------------------------------------------------===//
1515

1616
#include "llvm/CGData/StableFunctionMap.h"
17+
#include "llvm/Support/CommandLine.h"
18+
#include "llvm/Support/Debug.h"
1719

1820
#define DEBUG_TYPE "stable-function-map"
1921

2022
using namespace llvm;
2123

24+
static cl::opt<unsigned>
25+
GlobalMergingMinMerges("global-merging-min-merges",
26+
cl::desc("Minimum number of similar functions with "
27+
"the same hash required for merging."),
28+
cl::init(2), cl::Hidden);
29+
static cl::opt<unsigned> GlobalMergingMinInstrs(
30+
"global-merging-min-instrs",
31+
cl::desc("The minimum instruction count required when merging functions."),
32+
cl::init(1), cl::Hidden);
33+
static cl::opt<unsigned> GlobalMergingMaxParams(
34+
"global-merging-max-params",
35+
cl::desc(
36+
"The maximum number of parameters allowed when merging functions."),
37+
cl::init(std::numeric_limits<unsigned>::max()), cl::Hidden);
38+
static cl::opt<unsigned> GlobalMergingParamOverhead(
39+
"global-merging-param-overhead",
40+
cl::desc("The overhead cost associated with each parameter when merging "
41+
"functions."),
42+
cl::init(2), cl::Hidden);
43+
static cl::opt<unsigned>
44+
GlobalMergingCallOverhead("global-merging-call-overhead",
45+
cl::desc("The overhead cost associated with each "
46+
"function call when merging functions."),
47+
cl::init(1), cl::Hidden);
48+
static cl::opt<unsigned> GlobalMergingExtraThreshold(
49+
"global-merging-extra-threshold",
50+
cl::desc("An additional cost threshold that must be exceeded for merging "
51+
"to be considered beneficial."),
52+
cl::init(0), cl::Hidden);
53+
2254
unsigned StableFunctionMap::getIdOrCreateForName(StringRef Name) {
2355
auto It = NameToId.find(Name);
2456
if (It != NameToId.end())
@@ -117,7 +149,38 @@ static void removeIdenticalIndexPair(
117149
SF->IndexOperandHashMap->erase(Pair);
118150
}
119151

120-
void StableFunctionMap::finalize() {
152+
static bool isProfitable(
153+
const SmallVector<std::unique_ptr<StableFunctionMap::StableFunctionEntry>>
154+
&SFS) {
155+
unsigned StableFunctionCount = SFS.size();
156+
if (StableFunctionCount < GlobalMergingMinMerges)
157+
return false;
158+
159+
unsigned InstCount = SFS[0]->InstCount;
160+
if (InstCount < GlobalMergingMinInstrs)
161+
return false;
162+
163+
unsigned ParamCount = SFS[0]->IndexOperandHashMap->size();
164+
if (ParamCount > GlobalMergingMaxParams)
165+
return false;
166+
167+
unsigned Benefit = InstCount * (StableFunctionCount - 1);
168+
unsigned Cost =
169+
(GlobalMergingParamOverhead * ParamCount + GlobalMergingCallOverhead) *
170+
StableFunctionCount +
171+
GlobalMergingExtraThreshold;
172+
173+
bool Result = Benefit > Cost;
174+
LLVM_DEBUG(dbgs() << "isProfitable: Hash = " << SFS[0]->Hash << ", "
175+
<< "StableFunctionCount = " << StableFunctionCount
176+
<< ", InstCount = " << InstCount
177+
<< ", ParamCount = " << ParamCount
178+
<< ", Benefit = " << Benefit << ", Cost = " << Cost
179+
<< ", Result = " << (Result ? "true" : "false") << "\n");
180+
return Result;
181+
}
182+
183+
void StableFunctionMap::finalize(bool SkipTrim) {
121184
for (auto It = HashToFuncs.begin(); It != HashToFuncs.end(); ++It) {
122185
auto &[StableHash, SFS] = *It;
123186

@@ -158,9 +221,15 @@ void StableFunctionMap::finalize() {
158221
continue;
159222
}
160223

224+
if (SkipTrim)
225+
continue;
226+
161227
// Trim the index pair that has the same operand hash across
162228
// stable functions.
163229
removeIdenticalIndexPair(SFS);
230+
231+
if (!isProfitable(SFS))
232+
HashToFuncs.erase(It);
164233
}
165234

166235
Finalized = true;

llvm/lib/CodeGen/GlobalMergeFunctions.cpp

Lines changed: 10 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,6 @@ static cl::opt<bool>
3131
"the codegen data generation or use. Local "
3232
"merging is still enabled within a module."),
3333
cl::init(false));
34-
static cl::opt<unsigned> GlobalMergingMinInstrs(
35-
"global-merging-min-instrs",
36-
cl::desc("The minimum instruction count required when merging functions."),
37-
cl::init(1), cl::Hidden);
38-
static cl::opt<unsigned> GlobalMergingMaxParams(
39-
"global-merging-max-params",
40-
cl::desc(
41-
"The maximum number of parameters allowed when merging functions."),
42-
cl::init(std::numeric_limits<unsigned>::max()), cl::Hidden);
43-
static cl::opt<unsigned> GlobalMergingParamOverhead(
44-
"global-merging-param-overhead",
45-
cl::desc("The overhead cost associated with each parameter when merging "
46-
"functions."),
47-
cl::init(2), cl::Hidden);
48-
static cl::opt<unsigned>
49-
GlobalMergingCallOverhead("global-merging-call-overhead",
50-
cl::desc("The overhead cost associated with each "
51-
"function call when merging functions."),
52-
cl::init(1), cl::Hidden);
53-
static cl::opt<unsigned> GlobalMergingExtraThreshold(
54-
"global-merging-extra-threshold",
55-
cl::desc("An additional cost threshold that must be exceeded for merging "
56-
"to be considered beneficial."),
57-
cl::init(0), cl::Hidden);
5834

5935
STATISTIC(NumMismatchedFunctionHash,
6036
"Number of mismatched function hash for global merge function");
@@ -442,40 +418,6 @@ static ParamLocsVecTy computeParamInfo(
442418
return ParamLocsVec;
443419
}
444420

445-
static bool isProfitable(
446-
const SmallVector<std::unique_ptr<StableFunctionMap::StableFunctionEntry>>
447-
&SFS,
448-
const Function *F) {
449-
// No interest if the number of candidates are less than 2.
450-
unsigned StableFunctionCount = SFS.size();
451-
if (StableFunctionCount < 2)
452-
return false;
453-
454-
unsigned InstCount = SFS[0]->InstCount;
455-
if (InstCount < GlobalMergingMinInstrs)
456-
return false;
457-
458-
unsigned ParamCount = SFS[0]->IndexOperandHashMap->size();
459-
unsigned TotalParamCount = ParamCount + F->getFunctionType()->getNumParams();
460-
if (TotalParamCount > GlobalMergingMaxParams)
461-
return false;
462-
463-
unsigned Benefit = InstCount * (StableFunctionCount - 1);
464-
unsigned Cost =
465-
(GlobalMergingParamOverhead * ParamCount + GlobalMergingCallOverhead) *
466-
StableFunctionCount +
467-
GlobalMergingExtraThreshold;
468-
469-
bool Result = Benefit > Cost;
470-
LLVM_DEBUG(dbgs() << "isProfitable: Function = " << F->getName() << ", "
471-
<< "StableFunctionCount = " << StableFunctionCount
472-
<< ", InstCount = " << InstCount
473-
<< ", ParamCount = " << ParamCount
474-
<< ", Benefit = " << Benefit << ", Cost = " << Cost
475-
<< ", Result = " << (Result ? "true" : "false") << "\n");
476-
return Result;
477-
}
478-
479421
bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
480422
bool Changed = false;
481423

@@ -488,12 +430,9 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
488430

489431
auto ModId = M.getModuleIdentifier();
490432
for (auto &[Hash, SFS] : FunctionMap->getFunctionMap()) {
491-
// Compute the parameter locations based on the unique hash sequences
433+
// Parameter locations based on the unique hash sequences
492434
// across the candidates.
493-
auto ParamLocsVec = computeParamInfo(SFS);
494-
LLVM_DEBUG(dbgs() << "[GlobalMergeFunc] Merging hash: " << Hash
495-
<< " with Params " << ParamLocsVec.size() << "\n");
496-
435+
std::optional<ParamLocsVecTy> ParamLocsVec;
497436
Function *MergedFunc = nullptr;
498437
std::string MergedModId;
499438
SmallVector<FuncMergeInfo> FuncMergeInfos;
@@ -542,15 +481,17 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
542481
++NumMismatchedConstHash;
543482
continue;
544483
}
484+
if (!ParamLocsVec.has_value()) {
485+
ParamLocsVec = computeParamInfo(SFS);
486+
LLVM_DEBUG(dbgs() << "[GlobalMergeFunc] Merging hash: " << Hash
487+
<< " with Params " << ParamLocsVec->size() << "\n");
488+
}
545489
if (!checkConstLocationCompatible(*SF, *FI.IndexInstruction,
546-
ParamLocsVec)) {
490+
*ParamLocsVec)) {
547491
++NumMismatchedConstHash;
548492
continue;
549493
}
550494

551-
if (!isProfitable(SFS, F))
552-
break;
553-
554495
if (MergedFunc) {
555496
// Check if the matched functions fall into the same (first) module.
556497
// This module check is not strictly necessary as the functions can move
@@ -583,7 +524,7 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
583524
// the parameters. Populate parameters pointing to the original constants.
584525
SmallVector<Constant *> Params;
585526
SmallVector<Type *> ParamTypes;
586-
for (auto &ParamLocs : ParamLocsVec) {
527+
for (auto &ParamLocs : *ParamLocsVec) {
587528
assert(!ParamLocs.empty());
588529
auto &[InstIndex, OpndIndex] = ParamLocs[0];
589530
auto *Inst = FMI.IndexInstruction->lookup(InstIndex);
@@ -594,7 +535,7 @@ bool GlobalMergeFunc::merge(Module &M, const StableFunctionMap *FunctionMap) {
594535

595536
// Create a merged function derived from the current function.
596537
Function *MergedFunc =
597-
createMergedFunction(FMI, ParamTypes, ParamLocsVec);
538+
createMergedFunction(FMI, ParamTypes, *ParamLocsVec);
598539

599540
LLVM_DEBUG({
600541
dbgs() << "[GlobalMergeFunc] Merged function (hash:" << FMI.SF->Hash

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static cl::opt<RunOutliner> EnableMachineOutliner(
141141
"Disable all outlining"),
142142
// Sentinel value for unspecified option.
143143
clEnumValN(RunOutliner::AlwaysOutline, "", "")));
144-
cl::opt<bool> EnableGlobalMergeFunc(
144+
static cl::opt<bool> EnableGlobalMergeFunc(
145145
"enable-global-merge-func", cl::Hidden,
146146
cl::desc("Enable global merge functions that are based on hash function"));
147147
// Disable the pass to fix unwind information. Whether the pass is included in

llvm/test/tools/llvm-cgdata/merge-combined-funcmap-hashtree.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ RUN: sed "s/<RAW_2_BYTES>/$(cat %t/raw-funcmap-bytes.txt)/g" %t/merge-both-hasht
2121
RUN: llc -filetype=obj -mtriple arm64-apple-darwin %t/merge-both-hashtree-funcmap.ll -o %t/merge-both-hashtree-funcmap.o
2222

2323
# Merge an object file having cgdata (__llvm_outline and __llvm_merge)
24-
RUN: llvm-cgdata -m %t/merge-both-hashtree-funcmap.o -o %t/merge-both-hashtree-funcmap.cgdata
24+
RUN: llvm-cgdata -m --skip-trim %t/merge-both-hashtree-funcmap.o -o %t/merge-both-hashtree-funcmap.cgdata
2525
RUN: llvm-cgdata -s %t/merge-both-hashtree-funcmap.cgdata | FileCheck %s
2626

2727
CHECK: Outlined hash tree:

llvm/test/tools/llvm-cgdata/merge-funcmap-archive.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ RUN: llc -filetype=obj -mtriple arm64-apple-darwin %t/merge-2.ll -o %t/merge-2.o
2121
RUN: llvm-ar rcs %t/merge-archive.a %t/merge-1.o %t/merge-2.o
2222

2323
# Merge the archive into the codegen data file.
24-
RUN: llvm-cgdata --merge %t/merge-archive.a -o %t/merge-archive.cgdata
24+
RUN: llvm-cgdata --merge --skip-trim %t/merge-archive.a -o %t/merge-archive.cgdata
2525
RUN: llvm-cgdata --show %t/merge-archive.cgdata | FileCheck %s
2626

2727
RUN: llvm-cgdata --show %t/merge-archive.cgdata| FileCheck %s

llvm/test/tools/llvm-cgdata/merge-funcmap-concat.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ RUN: od -t x1 -j 32 -An %t/raw-2.cgdata | tr -d '\n\r\t' | sed 's/[ ]*$//' | sed
1515
RUN: sed "s/<RAW_2_BYTES>/$(cat %t/raw-2-bytes.txt)/g" %t/merge-concat-template-2.ll > %t/merge-concat.ll
1616

1717
RUN: llc -filetype=obj -mtriple arm64-apple-darwin %t/merge-concat.ll -o %t/merge-concat.o
18-
RUN: llvm-cgdata --merge %t/merge-concat.o -o %t/merge-concat.cgdata
18+
RUN: llvm-cgdata --merge --skip-trim %t/merge-concat.o -o %t/merge-concat.cgdata
1919
RUN: llvm-cgdata --show %t/merge-concat.cgdata | FileCheck %s
2020

2121
CHECK: Stable function map:

llvm/test/tools/llvm-cgdata/merge-funcmap-double.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ RUN: sed "s/<RAW_2_BYTES>/$(cat %t/raw-2-bytes.txt)/g" %t/merge-2-template.ll >
1818
RUN: llc -filetype=obj -mtriple arm64-apple-darwin %t/merge-2.ll -o %t/merge-2.o
1919

2020
# Merge two object files into the codegen data file.
21-
RUN: llvm-cgdata --merge %t/merge-1.o %t/merge-2.o -o %t/merge.cgdata
21+
RUN: llvm-cgdata --merge --skip-trim %t/merge-1.o %t/merge-2.o -o %t/merge.cgdata
2222

2323
RUN: llvm-cgdata --show %t/merge.cgdata | FileCheck %s
2424
CHECK: Stable function map:

llvm/test/tools/llvm-cgdata/merge-funcmap-single.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RUN: sed "s/<RAW_1_BYTES>/$(cat %t/raw-single-bytes.txt)/g" %t/merge-single-temp
1313
RUN: llc -filetype=obj -mtriple arm64-apple-darwin %t/merge-single.ll -o %t/merge-single.o
1414

1515
# Merge an object file having cgdata (__llvm_merge)
16-
RUN: llvm-cgdata -m %t/merge-single.o -o %t/merge-single.cgdata
16+
RUN: llvm-cgdata -m --skip-trim %t/merge-single.o -o %t/merge-single.cgdata
1717
RUN: llvm-cgdata -s %t/merge-single.cgdata | FileCheck %s
1818
CHECK: Stable function map:
1919
CHECK-NEXT: Unique hash Count: 1

0 commit comments

Comments
 (0)