Debian Codegraph @

clang/utils/perf-training/CMakeLists.txt

raw
1 include(LLVMExternalProjectUtils)
2
3 set(CLANG_PGO_TRAINING_DATA "${CMAKE_CURRENT_SOURCE_DIR}" CACHE PATH
4 "The path to a lit testsuite containing samples for PGO and order file generation"
5 )
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.")
8
9 if(LLVM_BUILD_INSTRUMENTED)
10 configure_lit_site_cfg(
11 ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
12 ${CMAKE_CURRENT_BINARY_DIR}/pgo-data/lit.site.cfg
13 )
14
15 add_lit_testsuite(generate-profraw "Generating clang PGO data"
16 ${CMAKE_CURRENT_BINARY_DIR}/pgo-data/
17 EXCLUDE_FROM_CHECK_ALL
18 DEPENDS clear-profraw
19 )
20
21 add_custom_target(clear-profraw
22 COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_BINARY_DIR}/profiles/ profraw
23 COMMENT "Clearing old profraw data")
24
25 if(NOT LLVM_PROFDATA)
26 find_program(LLVM_PROFDATA llvm-profdata)
27 endif()
28
29 if(NOT LLVM_PROFDATA)
30 message(STATUS "To enable merging PGO data LLVM_PROFDATA has to point to llvm-profdata")
31 else()
32 add_custom_command(
33 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/clang.profdata
34 # generate-profraw is a custom_target which are always considered stale.
35 # If we add it here to 'DEPENDS', then it will always execute and running
36 # ninja install && ninja check-all will result in the profile data being
37 # generated twice, and cause the ninja check-all build to fail with errors like:
38 # `ld.lld: error: Function Import: link error: linking module flags 'ProfileSummary': IDs have conflicting values in`
39 # Therefor we call the generate-profraw target manually as part of this custom
40 # command, which will only run if clang or ${CLANG_PGO_TRAINING_DEPS} are updated.
41 COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target generate-profraw
42 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/
43 COMMENT "Merging profdata"
44 DEPENDS clang ${CLANG_PGO_TRAINING_DEPS}
45 )
46 add_custom_target(generate-profdata DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang.profdata)
47 if (CLANG_PGO_TRAINING_DATA_SOURCE_DIR)
48 llvm_ExternalProject_Add(generate-profraw-external ${CLANG_PGO_TRAINING_DATA_SOURCE_DIR}
49 USE_TOOLCHAIN EXLUDE_FROM_ALL NO_INSTALL DEPENDS generate-profraw)
50 add_dependencies(generate-profdata generate-profraw-external)
51 endif()
52 endif()
53 endif()
54
55 find_program(DTRACE dtrace)
56 # TODO: Look into supporting this for the driver build. It will require changing
57 # the perf-helper.py file to understand to call `llvm` as `llvm clang`.
58 if(APPLE AND DTRACE AND NOT LLVM_TOOL_LLVM_DRIVER_BUILD)
59 configure_lit_site_cfg(
60 ${CMAKE_CURRENT_SOURCE_DIR}/order-files.lit.site.cfg.in
61 ${CMAKE_CURRENT_BINARY_DIR}/order-files/lit.site.cfg
62 )
63
64 add_lit_testsuite(generate-dtrace-logs "Generating clang dtrace data"
65 ${CMAKE_CURRENT_BINARY_DIR}/order-files/
66 EXCLUDE_FROM_CHECK_ALL
67 ARGS -j 1
68 DEPENDS clang clear-dtrace-logs
69 )
70
71 add_custom_target(clear-dtrace-logs
72 COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} dtrace
73 COMMENT "Clearing old dtrace data")
74
75 if(NOT CLANG_ORDER_FILE)
76 message(FATAL_ERROR "Output clang order file is not set")
77 endif()
78
79 add_custom_target(generate-order-file
80 COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py gen-order-file --binary $<TARGET_FILE:clang> --output ${CLANG_ORDER_FILE} ${CMAKE_CURRENT_BINARY_DIR}
81 COMMENT "Generating order file"
82 DEPENDS generate-dtrace-logs)
83 endif()
84
85 if(CLANG_BOLT AND NOT LLVM_BUILD_INSTRUMENTED)
86 set(CLANG_BOLT_INSTRUMENTED "clang-bolt.inst" CACHE STRING
87 "Name of BOLT-instrumented Clang binary")
88 configure_lit_site_cfg(
89 ${CMAKE_CURRENT_SOURCE_DIR}/bolt.lit.site.cfg.in
90 ${CMAKE_CURRENT_BINARY_DIR}/bolt-fdata/lit.site.cfg
91 )
92
93 add_lit_testsuite(generate-bolt-fdata "Generating BOLT profile for Clang"
94 ${CMAKE_CURRENT_BINARY_DIR}/bolt-fdata/
95 EXCLUDE_FROM_CHECK_ALL
96 DEPENDS clang-bolt-training-deps clear-bolt-fdata clear-perf-data
97 )
98
99 add_custom_target(clear-bolt-fdata
100 COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} fdata
101 COMMENT "Clearing old BOLT fdata")
102
103 add_custom_target(clear-perf-data
104 COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} perf.data
105 COMMENT "Clearing old perf data")
106
107 string(TOUPPER "${CLANG_BOLT}" CLANG_BOLT)
108 if (CLANG_BOLT STREQUAL "LBR")
109 set(BOLT_LBR "--lbr")
110 endif()
111
112 add_custom_target(merge-fdata-deps)
113 if (CLANG_BOLT STREQUAL "INSTRUMENT")
114 add_dependencies(merge-fdata-deps generate-bolt-fdata)
115 else()
116 # Convert perf profiles into fdata
117 add_custom_target(convert-perf-fdata
118 COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py perf2bolt $<TARGET_FILE:llvm-bolt> ${CMAKE_CURRENT_BINARY_DIR} $<TARGET_FILE:clang> ${BOLT_LBR}
119 COMMENT "Converting perf files to BOLT fdata"
120 DEPENDS llvm-bolt generate-bolt-fdata)
121 add_dependencies(merge-fdata-deps convert-perf-fdata)
122 endif()
123
124 # Merge profiles into one using merge-fdata
125 add_custom_target(clang-bolt-profile
126 COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py merge-fdata $<TARGET_FILE:merge-fdata> ${CMAKE_CURRENT_BINARY_DIR}/prof.fdata ${CMAKE_CURRENT_BINARY_DIR}
127 COMMENT "Merging BOLT fdata"
128 DEPENDS merge-fdata merge-fdata-deps)
129 endif()