Skip to content

Commit dd0356d

Browse files
tstellarpetrhosek
andauthored
[CMake][PGO] Add option for using an external project to generate profile data (#78879)
The new CLANG_PGO_TRAINING_DATA_SOURCE_DIR allows users to specify a CMake project to use for generating the profile data. For example, to use the llvm-test-suite to generate profile data you would do: $ cmake -G Ninja -B build -S llvm -C <path to source>/clang/cmake/caches/PGO.cmake \ -DBOOTSTRAP_CLANG_PGO_TRAINING_DATA_SOURCE_DIR=<path to llvm-test-suite> \ -DBOOTSTRAP_CLANG_PGO_TRAINING_DEPS=runtimes Note that the CLANG_PERF_TRAINING_DEPS has been renamed to CLANG_PGO_TRAINING_DEPS. --------- Co-authored-by: Petr Hosek <phosek@google.com>
1 parent bc06cd5 commit dd0356d

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

Diff for: clang/utils/perf-training/CMakeLists.txt

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
include(LLVMExternalProjectUtils)
2+
13
set(CLANG_PGO_TRAINING_DATA "${CMAKE_CURRENT_SOURCE_DIR}" CACHE PATH
24
"The path to a lit testsuite containing samples for PGO and order file generation"
35
)
6+
set(CLANG_PGO_TRAINING_DATA_SOURCE_DIR OFF CACHE STRING "Path to source directory containing cmake project with source files to use for generating pgo data")
7+
set(CLANG_PGO_TRAINING_DEPS "" CACHE STRING "Extra dependencies needed to build the PGO training data.")
48

59
if(LLVM_BUILD_INSTRUMENTED)
610
configure_lit_site_cfg(
@@ -11,11 +15,11 @@ if(LLVM_BUILD_INSTRUMENTED)
1115
add_lit_testsuite(generate-profraw "Generating clang PGO data"
1216
${CMAKE_CURRENT_BINARY_DIR}/pgo-data/
1317
EXCLUDE_FROM_CHECK_ALL
14-
DEPENDS clang clear-profraw ${CLANG_PERF_TRAINING_DEPS}
18+
DEPENDS clang clear-profraw ${CLANG_PGO_TRAINING_DEPS}
1519
)
1620

1721
add_custom_target(clear-profraw
18-
COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} profraw
22+
COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_BINARY_DIR}/profiles/ profraw
1923
COMMENT "Clearing old profraw data")
2024

2125
if(NOT LLVM_PROFDATA)
@@ -26,9 +30,14 @@ if(LLVM_BUILD_INSTRUMENTED)
2630
message(STATUS "To enable merging PGO data LLVM_PROFDATA has to point to llvm-profdata")
2731
else()
2832
add_custom_target(generate-profdata
29-
COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py merge ${LLVM_PROFDATA} ${CMAKE_CURRENT_BINARY_DIR}/clang.profdata ${CMAKE_CURRENT_BINARY_DIR}
33+
COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py merge ${LLVM_PROFDATA} ${CMAKE_CURRENT_BINARY_DIR}/clang.profdata ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_BINARY_DIR}/profiles/
3034
COMMENT "Merging profdata"
3135
DEPENDS generate-profraw)
36+
if (CLANG_PGO_TRAINING_DATA_SOURCE_DIR)
37+
llvm_ExternalProject_Add(generate-profraw-external ${CLANG_PGO_TRAINING_DATA_SOURCE_DIR}
38+
USE_TOOLCHAIN EXLUDE_FROM_ALL NO_INSTALL DEPENDS generate-profraw)
39+
add_dependencies(generate-profdata generate-profraw-external)
40+
endif()
3241
endif()
3342
endif()
3443

Diff for: clang/utils/perf-training/perf-helper.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ def findFilesWithExtension(path, extension):
3030

3131

3232
def clean(args):
33-
if len(args) != 2:
33+
if len(args) < 2:
3434
print(
35-
"Usage: %s clean <path> <extension>\n" % __file__
35+
"Usage: %s clean <paths> <extension>\n" % __file__
3636
+ "\tRemoves all files with extension from <path>."
3737
)
3838
return 1
39-
for filename in findFilesWithExtension(args[0], args[1]):
40-
os.remove(filename)
39+
for path in args[1:-1]:
40+
for filename in findFilesWithExtension(path, args[-1]):
41+
os.remove(filename)
4142
return 0
4243

4344

4445
def merge(args):
45-
if len(args) != 3:
46+
if len(args) < 3:
4647
print(
47-
"Usage: %s merge <llvm-profdata> <output> <path>\n" % __file__
48+
"Usage: %s merge <llvm-profdata> <output> <paths>\n" % __file__
4849
+ "\tMerges all profraw files from path into output."
4950
)
5051
return 1
5152
cmd = [args[0], "merge", "-o", args[1]]
52-
cmd.extend(findFilesWithExtension(args[2], "profraw"))
53+
for path in args[2:]:
54+
cmd.extend(findFilesWithExtension(path, "profraw"))
5355
subprocess.check_call(cmd)
5456
return 0
5557

Diff for: llvm/docs/AdvancedBuilds.rst

+25-2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@ that also enables ThinTLO, use the following command:
145145
-DPGO_INSTRUMENT_LTO=Thin \
146146
<path to source>/llvm
147147
148+
By default, clang will generate profile data by compiling a simple
149+
hello world program. You can also tell clang use an external
150+
project for generating profile data that may be a better fit for your
151+
use case. The project you specify must either be a lit test suite
152+
(use the CLANG_PGO_TRAINING_DATA option) or a CMake project (use the
153+
CLANG_PERF_TRAINING_DATA_SOURCE_DIR option).
154+
155+
For example, If you wanted to use the
156+
`LLVM Test Suite <https://github.com/llvm/llvm-test-suite/>`_ to generate
157+
profile data you would use the following command:
158+
159+
.. code-block:: console
160+
161+
$ cmake -G Ninja -C <path to source>/clang/cmake/caches/PGO.cmake \
162+
-DBOOTSTRAP_CLANG_PGO_TRAINING_DATA_SOURCE_DIR=<path to llvm-test-suite> \
163+
-DBOOTSTRAP_CLANG_PGO_TRAINING_DEPS=runtimes
164+
165+
The BOOTSTRAP\_ prefixes tells CMake to pass the variables on to the instrumented
166+
stage two build. And the CLANG_PGO_TRAINING_DEPS option let's you specify
167+
additional build targets to build before building the external project. The
168+
LLVM Test Suite requires compiler-rt to build, so we need to add the
169+
`runtimes` target as a dependency.
170+
148171
After configuration, building the stage2-instrumented-generate-profdata target
149172
will automatically build the stage1 compiler, build the instrumented compiler
150173
with the stage1 compiler, and then run the instrumented compiler against the
@@ -172,12 +195,12 @@ You can feed that file into the LLVM_PROFDATA_FILE option when you build your
172195
optimized compiler.
173196

174197
It may be necessary to build additional targets before running perf training, such as
175-
builtins and runtime libraries. You can use the :code:`CLANG_PERF_TRAINING_DEPS` CMake
198+
builtins and runtime libraries. You can use the :code:`CLANG_PGO_TRAINING_DEPS` CMake
176199
variable for that purpose:
177200

178201
.. code-block:: cmake
179202
180-
set(CLANG_PERF_TRAINING_DEPS builtins runtimes CACHE STRING "")
203+
set(CLANG_PGO_TRAINING_DEPS builtins runtimes CACHE STRING "")
181204
182205
The PGO cache has a slightly different stage naming scheme than other
183206
multi-stage builds. It generates three stages: stage1, stage2-instrumented, and

0 commit comments

Comments
 (0)