Skip to content

Commit

Permalink
[PGO] Include the mem ops into the function hash.
Browse files Browse the repository at this point in the history
To avoid hash collisions when the only difference is in mem ops.

Differential Revision: https://reviews.llvm.org/D84782
  • Loading branch information
hjyamauchi committed Jul 29, 2020
1 parent 01aa147 commit 120e66b
Show file tree
Hide file tree
Showing 50 changed files with 144 additions and 80 deletions.
2 changes: 1 addition & 1 deletion clang/test/CodeGen/Inputs/thinlto_expect1.proftext
Expand Up @@ -2,7 +2,7 @@
:ir
foo
# Func Hash:
25571299074
784007059655560962
# Num Counters:
2
# Counter Values:
Expand Down
4 changes: 2 additions & 2 deletions clang/test/CodeGen/Inputs/thinlto_expect2.proftext
Expand Up @@ -2,7 +2,7 @@
:csir
foo
# Func Hash:
25571299074
784007059655560962
# Num Counters:
2
# Counter Values:
Expand All @@ -11,7 +11,7 @@ foo

foo
# Func Hash:
1152921530178146050
1936928564262407938
# Num Counters:
2
# Counter Values:
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGenCXX/Inputs/profile-remap.proftext
@@ -1,6 +1,6 @@
:ir
_ZN3Foo8functionENS_1XE
29667547796
146835647075900052
2
10
90
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGenCXX/Inputs/profile-remap_entry.proftext
@@ -1,7 +1,7 @@
:ir
:entry_first
_ZN3Foo8functionENS_1XE
29667547796
146835647075900052
2
100
90
Expand Down
Expand Up @@ -2,7 +2,7 @@
:ir
main
# Func Hash:
34137660316
1063705162469825436
# Num Counters:
2
# Counter Values:
Expand Down
Expand Up @@ -3,7 +3,7 @@
:entry_first
main
# Func Hash:
34137660316
1063705162469825436
# Num Counters:
2
# Counter Values:
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/test/profile/Linux/instrprof-value-merge.c
Expand Up @@ -45,7 +45,7 @@ int main(int argc, char *argv[]) {

// CHECK: Counters:
// CHECK: main:
// CHECK: Hash: 0x00030012a7ab6e87
// CHECK: Hash: 0x0a9bd81e87ab6e87
// CHECK: Counters: 6
// CHECK: Indirect Call Site Count: 3
// CHECK: Number of Memory Intrinsics Calls: 3
Expand Down
49 changes: 40 additions & 9 deletions llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
Expand Up @@ -261,6 +261,10 @@ extern cl::opt<PGOViewCountsType> PGOViewCounts;
// Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name=
extern cl::opt<std::string> ViewBlockFreqFuncName;

static cl::opt<bool>
PGOOldCFGHashing("pgo-instr-old-cfg-hashing", cl::init(false), cl::Hidden,
cl::desc("Use the old CFG function hashing"));

// Return a string describing the branch condition that can be
// used in static branch probability heuristics:
static std::string getBranchCondString(Instruction *TI) {
Expand Down Expand Up @@ -620,7 +624,8 @@ template <class Edge, class BBInfo> class FuncPGOInstrumentation {
} // end anonymous namespace

// Compute Hash value for the CFG: the lower 32 bits are CRC32 of the index
// value of each BB in the CFG. The higher 32 bits record the number of edges.
// value of each BB in the CFG. The higher 32 bits are the CRC32 of the numbers
// of selects, indirect calls, mem ops and edges.
template <class Edge, class BBInfo>
void FuncPGOInstrumentation<Edge, BBInfo>::computeCFGHash() {
std::vector<uint8_t> Indexes;
Expand All @@ -639,12 +644,34 @@ void FuncPGOInstrumentation<Edge, BBInfo>::computeCFGHash() {
}
JC.update(Indexes);

// Hash format for context sensitive profile. Reserve 4 bits for other
// information.
FunctionHash = (uint64_t)SIVisitor.getNumOfSelectInsts() << 56 |
(uint64_t)ValueSites[IPVK_IndirectCallTarget].size() << 48 |
//(uint64_t)ValueSites[IPVK_MemOPSize].size() << 40 |
(uint64_t)MST.AllEdges.size() << 32 | JC.getCRC();
JamCRC JCH;
if (PGOOldCFGHashing) {
// Hash format for context sensitive profile. Reserve 4 bits for other
// information.
FunctionHash = (uint64_t)SIVisitor.getNumOfSelectInsts() << 56 |
(uint64_t)ValueSites[IPVK_IndirectCallTarget].size() << 48 |
//(uint64_t)ValueSites[IPVK_MemOPSize].size() << 40 |
(uint64_t)MST.AllEdges.size() << 32 | JC.getCRC();
} else {
// The higher 32 bits.
union {
uint64_t N;
uint8_t C[8];
} Data;
Data.N = (uint64_t)SIVisitor.getNumOfSelectInsts();
JCH.update(Data.C);
Data.N = (uint64_t)ValueSites[IPVK_IndirectCallTarget].size();
JCH.update(Data.C);
Data.N = (uint64_t)ValueSites[IPVK_MemOPSize].size();
JCH.update(Data.C);
Data.N = (uint64_t)MST.AllEdges.size();
JCH.update(Data.C);

// Hash format for context sensitive profile. Reserve 4 bits for other
// information.
FunctionHash = (((uint64_t)JCH.getCRC()) << 28) + JC.getCRC();
}

// Reserve bit 60-63 for other information purpose.
FunctionHash &= 0x0FFFFFFFFFFFFFFF;
if (IsCS)
Expand All @@ -653,8 +680,12 @@ void FuncPGOInstrumentation<Edge, BBInfo>::computeCFGHash() {
<< " CRC = " << JC.getCRC()
<< ", Selects = " << SIVisitor.getNumOfSelectInsts()
<< ", Edges = " << MST.AllEdges.size() << ", ICSites = "
<< ValueSites[IPVK_IndirectCallTarget].size()
<< ", Hash = " << FunctionHash << "\n";);
<< ValueSites[IPVK_IndirectCallTarget].size());
if (!PGOOldCFGHashing) {
LLVM_DEBUG(dbgs() << ", Memops = " << ValueSites[IPVK_MemOPSize].size()
<< ", High32 CRC = " << JCH.getCRC());
}
LLVM_DEBUG(dbgs() << ", Hash = " << FunctionHash << "\n";);
}

// Check if we can safely rename this Comdat function.
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/PGOProfile/Inputs/PR41279.proftext
@@ -1,6 +1,6 @@
:ir
foo
60927483247
1096621588030135663
4
3
2
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/PGOProfile/Inputs/PR41279_2.proftext
@@ -1,6 +1,6 @@
:ir
f
62077759478
1096621589180411894
2
3
2
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/PGOProfile/Inputs/branch1.proftext
@@ -1,7 +1,7 @@
# :ir is the flag to indicate this is IR level profile.
:ir
test_br_1
25571299074
784007059655560962
2
3
2
Expand Down
@@ -1,7 +1,7 @@
# :ir is the flag to indicate this is IR level profile.
:ir
test_br_1
25571299074
784007059655560962
2
12884901888
8589934592
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/PGOProfile/Inputs/branch2.proftext
@@ -1,7 +1,7 @@
# :ir is the flag to indicate this is IR level profile.
:ir
test_br_2
29667547796
146835647075900052
2
1
1
Expand Down
Expand Up @@ -2,7 +2,7 @@
:ir
:entry_first
test_br_2
29667547796
146835647075900052
2
2
1
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/PGOProfile/Inputs/criticaledge.proftext
@@ -1,7 +1,7 @@
# :ir is the flag to indicate this is IR level profile.
:ir
test_criticalEdge
82323253069
93478046750287693
8
2
1
Expand All @@ -13,7 +13,7 @@ test_criticalEdge
1

<stdin>:bar
12884901887
742261418966908927
1
7

Expand Up @@ -2,7 +2,7 @@
:ir
:entry_first
test_criticalEdge
82323253069
93478046750287693
8
7
2
Expand All @@ -14,7 +14,7 @@ test_criticalEdge
1

<stdin>:bar
12884901887
742261418966908927
1
7

16 changes: 8 additions & 8 deletions llvm/test/Transforms/PGOProfile/Inputs/cspgo.proftext
Expand Up @@ -54,7 +54,7 @@ bar_m2

foo
# Func Hash:
1152921640672869708
1456607294772657484
# Num Counters:
10
# Counter Values:
Expand All @@ -71,7 +71,7 @@ foo

foo
# Func Hash:
29212902728
146835646621254984
# Num Counters:
2
# Counter Values:
Expand All @@ -80,7 +80,7 @@ foo

bar
# Func Hash:
1152921569533132113
1440408129826749777
# Num Counters:
5
# Counter Values:
Expand All @@ -92,7 +92,7 @@ bar

bar
# Func Hash:
56228292833
567185239050791137
# Num Counters:
4
# Counter Values:
Expand All @@ -103,15 +103,15 @@ bar

main
# Func Hash:
1152921517491748863
1895182923573755903
# Num Counters:
1
# Counter Values:
1

main
# Func Hash:
12884901887
742261418966908927
# Num Counters:
1
# Counter Values:
Expand All @@ -135,15 +135,15 @@ csfdo_plain.c:barbar

goo
# Func Hash:
1152921517491748863
1895182923573755903
# Num Counters:
1
# Counter Values:
100000

goo
# Func Hash:
12884901887
742261418966908927
# Num Counters:
1
# Counter Values:
Expand Down
@@ -1,6 +1,6 @@
# :ir is the flag to indicate this is IR level profile.
:ir
foo
12884901887
48277136972185599
1
1
@@ -1,7 +1,7 @@
:ir
:entry_first
test_simple_for
34137660316
1063705162469825436
2
0
96
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/Transforms/PGOProfile/Inputs/func_entry.proftext
Expand Up @@ -3,23 +3,23 @@
:entry_first
hot
# Func Hash:
12884901887
742261418966908927
# Num Counters:
1
# Counter Values:
9000

cold
# Func Hash:
12884901887
742261418966908927
# Num Counters:
1
# Counter Values:
10

med
# Func Hash:
12884901887
742261418966908927
# Num Counters:
1
# Counter Values:
Expand Down
8 changes: 4 additions & 4 deletions llvm/test/Transforms/PGOProfile/Inputs/indirect_call.proftext
@@ -1,7 +1,7 @@
:ir
bar
# Func Hash:
281487861612543
170957022131388415
# Num Counters:
1
# Counter Values:
Expand All @@ -19,23 +19,23 @@ func3:20

func1
# Func Hash:
12884901887
742261418966908927
# Num Counters:
1
# Counter Values:
40

func2
# Func Hash:
12884901887
742261418966908927
# Num Counters:
1
# Counter Values:
80

func3
# Func Hash:
12884901887
742261418966908927
# Num Counters:
1
# Counter Values:
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/PGOProfile/Inputs/indirectbr.proftext
Expand Up @@ -2,7 +2,7 @@
:ir
foo
# Func Hash:
47485104005
844982796158316421
# Num Counters:
4
# Counter Values:
Expand Down

0 comments on commit 120e66b

Please sign in to comment.