Skip to content

Commit 05c0afe

Browse files
committed
[PGO] Context sensitive PGO (part 4)
Part 4 of CSPGO changes: (1) add support in cmake for cspgo build. (2) fix an issue in big endian. (3) test cases. Differential Revision: https://reviews.llvm.org/D54175 llvm-svn: 355541
1 parent 9549f75 commit 05c0afe

21 files changed

+1049
-1
lines changed

llvm/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,12 @@ if (LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE OR
604604
endif()
605605
file(TO_NATIVE_PATH "${LLVM_PROFILE_DATA_DIR}/%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw" LLVM_PROFILE_FILE_PATTERN)
606606
endif()
607+
if(NOT LLVM_CSPROFILE_FILE_PATTERN)
608+
if(NOT LLVM_CSPROFILE_DATA_DIR)
609+
file(TO_NATIVE_PATH "${LLVM_BINARY_DIR}/csprofiles" LLVM_CSPROFILE_DATA_DIR)
610+
endif()
611+
file(TO_NATIVE_PATH "${LLVM_CSPROFILE_DATA_DIR}/%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw" LLVM_CSPROFILE_FILE_PATTERN)
612+
endif()
607613
endif()
608614

609615
if (LLVM_BUILD_STATIC)

llvm/cmake/modules/HandleLLVMOptions.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,12 @@ if (LLVM_BUILD_INSTRUMENTED)
809809
CMAKE_C_FLAGS
810810
CMAKE_EXE_LINKER_FLAGS
811811
CMAKE_SHARED_LINKER_FLAGS)
812+
elseif(uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "CSIR")
813+
append("-fcs-profile-generate='${LLVM_CSPROFILE_DATA_DIR}'"
814+
CMAKE_CXX_FLAGS
815+
CMAKE_C_FLAGS
816+
CMAKE_EXE_LINKER_FLAGS
817+
CMAKE_SHARED_LINKER_FLAGS)
812818
else()
813819
append("-fprofile-instr-generate='${LLVM_PROFILE_FILE_PATTERN}'"
814820
CMAKE_CXX_FLAGS
@@ -818,6 +824,14 @@ if (LLVM_BUILD_INSTRUMENTED)
818824
endif()
819825
endif()
820826

827+
# Need to pass -fprofile-instr-use to linker for context-sensitive PGO
828+
# compilation.
829+
if(LLVM_PROFDATA_FILE AND EXISTS ${LLVM_PROFDATA_FILE})
830+
append("-fprofile-instr-use='${LLVM_PROFDATA_FILE}'"
831+
CMAKE_EXE_LINKER_FLAGS
832+
CMAKE_SHARED_LINKER_FLAGS)
833+
endif()
834+
821835
option(LLVM_BUILD_INSTRUMENTED_COVERAGE "Build LLVM and tools with Code Coverage instrumentation" Off)
822836
mark_as_advanced(LLVM_BUILD_INSTRUMENTED_COVERAGE)
823837
append_if(LLVM_BUILD_INSTRUMENTED_COVERAGE "-fprofile-instr-generate='${LLVM_PROFILE_FILE_PATTERN}' -fcoverage-mapping"

llvm/lib/ProfileData/InstrProfReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ Error IndexedInstrProfReader::readHeader() {
813813

814814
Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur,
815815
/* UseCS */ false);
816-
if (Header->Version & VARIANT_MASK_CSIR_PROF)
816+
if (FormatVersion & VARIANT_MASK_CSIR_PROF)
817817
Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur,
818818
/* UseCS */ true);
819819

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:csir
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:ir
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; Test CSGen pass in CSPGO.
2+
; RUN: llvm-profdata merge %S/Inputs/cspgo-noncs.proftext -o %t-noncs.profdata
3+
; RUN: llvm-profdata merge %S/Inputs/cspgo-cs.proftext -o %t-cs.profdata
4+
; RUN: opt -O2 -debug-pass=Structure -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-noncs.profdata' -cspgo-kind=cspgo-instr-gen-pipeline -cs-profilegen-file=alloc %s 2>&1 |FileCheck %s --check-prefixes=CSGENDEFAULT
5+
; CSGENDEFAULT: PGOInstrumentationUse
6+
; CSGENDEFAULT: PGOInstrumentationGenCreateVar
7+
; CSGENDEFAULT: PGOInstrumentationGen
8+
9+
; Test CSUse pass in CSPGO.
10+
; RUN: opt -O2 -debug-pass=Structure -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-cs.profdata' -cspgo-kind=cspgo-instr-use-pipeline %s 2>&1 |FileCheck %s --check-prefixes=CSUSEDEFAULT
11+
; CSUSEDEFAULT: PGOInstrumentationUse
12+
; CSUSEDEFAULT-NOT: PGOInstrumentationGenCreateVar
13+
; CSUSEDEFAULT: PGOInstrumentationUse

llvm/test/Other/new-pm-cspgo.ll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; Test CSGen pass in CSPGO.
2+
; RUN: llvm-profdata merge %S/Inputs/cspgo-noncs.proftext -o %t-noncs.profdata
3+
; RUN: llvm-profdata merge %S/Inputs/cspgo-cs.proftext -o %t-cs.profdata
4+
; RUN: opt -debug-pass-manager -passes='default<O2>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-noncs.profdata' -cspgo-kind=cspgo-instr-gen-pipeline -cs-profilegen-file=alloc %s 2>&1 |FileCheck %s --check-prefixes=CSGENDEFAULT
5+
; RUN: opt -debug-pass-manager -passes='thinlto-pre-link<O2>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-noncs.profdata' -cspgo-kind=cspgo-instr-gen-pipeline -cs-profilegen-file=alloc %s 2>&1 |FileCheck %s --check-prefixes=CSGENPRELINK
6+
; RUN: opt -debug-pass-manager -passes='thinlto<O2>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-noncs.profdata' -cspgo-kind=cspgo-instr-gen-pipeline -cs-profilegen-file=alloc %s 2>&1 |FileCheck %s --check-prefixes=CSGENLTO
7+
; RUN: opt -debug-pass-manager -passes='lto-pre-link<O2>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-noncs.profdata' -cspgo-kind=cspgo-instr-gen-pipeline -cs-profilegen-file=alloc %s 2>&1 |FileCheck %s --check-prefixes=CSGENPRELINK
8+
; RUN: opt -debug-pass-manager -passes='lto<O2>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-noncs.profdata' -cspgo-kind=cspgo-instr-gen-pipeline -cs-profilegen-file=alloc %s 2>&1 |FileCheck %s --check-prefixes=CSGENLTO
9+
; CSGENDEFAULT: Running pass: PGOInstrumentationUse
10+
; CSGENDEFAULT: Running pass: PGOInstrumentationGenCreateVar
11+
; CSGENDEFAULT: Running pass: PGOInstrumentationGen
12+
; CSGENPRELINK: Running pass: PGOInstrumentationUse
13+
; CSGENPRELINK: Running pass: PGOInstrumentationGenCreateVar
14+
; CSGENPRELINK-NOT: Running pass: PGOInstrumentationGen
15+
; CSGENLTO-NOT: Running pass: PGOInstrumentationUse
16+
; CSGENLTO-NOT: Running pass: PGOInstrumentationGenCreateVar
17+
; CSGENLTO: Running pass: PGOInstrumentationGen
18+
19+
; Test CSUse pass in CSPGO.
20+
; RUN: opt -debug-pass-manager -passes='default<O2>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-cs.profdata' -cspgo-kind=cspgo-instr-use-pipeline %s 2>&1 |FileCheck %s --check-prefixes=CSUSEDEFAULT
21+
; RUN: opt -debug-pass-manager -passes='thinlto-pre-link<O2>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-cs.profdata' -cspgo-kind=cspgo-instr-use-pipeline %s 2>&1 |FileCheck %s --check-prefixes=CSUSEPRELINK
22+
; RUN: opt -debug-pass-manager -passes='thinlto<O2>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-cs.profdata' -cspgo-kind=cspgo-instr-use-pipeline %s 2>&1 |FileCheck %s --check-prefixes=CSUSELTO
23+
; RUN: opt -debug-pass-manager -passes='lto-pre-link<O2>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-noncs.profdata' -cspgo-kind=cspgo-instr-use-pipeline %s 2>&1 |FileCheck %s --check-prefixes=CSUSEPRELINK
24+
; RUN: opt -debug-pass-manager -passes='lto<O2>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t-cs.profdata' -cspgo-kind=cspgo-instr-use-pipeline %s 2>&1 |FileCheck %s --check-prefixes=CSUSELTO
25+
; CSUSEDEFAULT: Running pass: PGOInstrumentationUse
26+
; CSUSEDEFAULT-NOT: Running pass: PGOInstrumentationGenCreateVar
27+
; CSUSEDEFAULT: Running pass: PGOInstrumentationUse
28+
; CSUSEPRELINK: Running pass: PGOInstrumentationUse
29+
; CSUSEPRELINK-NOT: Running pass: PGOInstrumentationGenCreateVar
30+
; CSUSEPRELINK-NOT: Running pass: PGOInstrumentationUse
31+
; CSUSELTO: Running pass: PGOInstrumentationUse
32+
; CSUSELTO-NOT: Running pass: PGOInstrumentationUse
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# CSIR level Instrumentation Flag
2+
:csir
3+
bar_m
4+
# Func Hash:
5+
29667547796
6+
# Num Counters:
7+
2
8+
# Counter Values:
9+
99949
10+
51
11+
12+
bar_m
13+
# Func Hash:
14+
1224979111529676799
15+
# Num Counters:
16+
2
17+
# Counter Values:
18+
100000
19+
99949
20+
21+
csfdo_plain.c:cond
22+
# Func Hash:
23+
1152921517491748863
24+
# Num Counters:
25+
1
26+
# Counter Values:
27+
200000
28+
29+
csfdo_plain.c:cond
30+
# Func Hash:
31+
12884901887
32+
# Num Counters:
33+
1
34+
# Counter Values:
35+
200000
36+
37+
bar_m2
38+
# Func Hash:
39+
1152921534274394772
40+
# Num Counters:
41+
2
42+
# Counter Values:
43+
99938
44+
62
45+
46+
bar_m2
47+
# Func Hash:
48+
29667547796
49+
# Num Counters:
50+
2
51+
# Counter Values:
52+
99938
53+
62
54+
55+
foo
56+
# Func Hash:
57+
1152921640672869708
58+
# Num Counters:
59+
10
60+
# Counter Values:
61+
100000
62+
100000
63+
0
64+
66666
65+
66666
66+
0
67+
100000
68+
66667
69+
100000
70+
1
71+
72+
foo
73+
# Func Hash:
74+
29212902728
75+
# Num Counters:
76+
2
77+
# Counter Values:
78+
100000
79+
1
80+
81+
bar
82+
# Func Hash:
83+
1152921569533132113
84+
# Num Counters:
85+
5
86+
# Counter Values:
87+
0
88+
0
89+
0
90+
0
91+
0
92+
93+
bar
94+
# Func Hash:
95+
56228292833
96+
# Num Counters:
97+
4
98+
# Counter Values:
99+
800000
100+
399999
101+
100000
102+
100000
103+
104+
main
105+
# Func Hash:
106+
1152921517491748863
107+
# Num Counters:
108+
1
109+
# Counter Values:
110+
1
111+
112+
main
113+
# Func Hash:
114+
12884901887
115+
# Num Counters:
116+
1
117+
# Counter Values:
118+
1
119+
120+
csfdo_plain.c:barbar
121+
# Func Hash:
122+
1152921517491748863
123+
# Num Counters:
124+
1
125+
# Counter Values:
126+
100000
127+
128+
csfdo_plain.c:barbar
129+
# Func Hash:
130+
12884901887
131+
# Num Counters:
132+
1
133+
# Counter Values:
134+
100000
135+
136+
goo
137+
# Func Hash:
138+
1152921517491748863
139+
# Num Counters:
140+
1
141+
# Counter Values:
142+
100000
143+
144+
goo
145+
# Func Hash:
146+
12884901887
147+
# Num Counters:
148+
1
149+
# Counter Values:
150+
100000
151+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# CSIR level Instrumentation Flag
2+
:csir
3+
cond.llvm.11253644763537639171
4+
# Func Hash:
5+
1152921517491748863
6+
# Num Counters:
7+
1
8+
# Counter Values:
9+
200000
10+
11+
foo
12+
# Func Hash:
13+
29212902728
14+
# Num Counters:
15+
2
16+
# Counter Values:
17+
100000
18+
1
19+
20+
bar
21+
# Func Hash:
22+
1152921534274394772
23+
# Num Counters:
24+
2
25+
# Counter Values:
26+
0
27+
0
28+
29+
bar
30+
# Func Hash:
31+
29667547796
32+
# Num Counters:
33+
2
34+
# Counter Values:
35+
100000
36+
100000
37+
38+
main
39+
# Func Hash:
40+
1152921517491748863
41+
# Num Counters:
42+
1
43+
# Counter Values:
44+
1
45+
46+
main
47+
# Func Hash:
48+
12884901887
49+
# Num Counters:
50+
1
51+
# Counter Values:
52+
1
53+
54+
cspgo.c:foo
55+
# Func Hash:
56+
1152921563228422740
57+
# Num Counters:
58+
4
59+
# Counter Values:
60+
100000
61+
100000
62+
0
63+
1
64+
65+
cspgo_bar.c:cond
66+
# Func Hash:
67+
12884901887
68+
# Num Counters:
69+
1
70+
# Counter Values:
71+
200000
72+

0 commit comments

Comments
 (0)