Skip to content

Commit be06761

Browse files
authored
[clang][cmake] Fixes for PGO builds when invoking ninja twice (#92591)
This fixes two problems with the 2-stage PGO builds. The first problem was that the stage2-instrumented and stage2 targets would not be built on the second ninja invocation. For example: This would work as expected. $ ninja -v -C build stage2-instrumented-generate-profdata Edit a file. $ touch llvm/lib/Support/StringExtras.cpp This would rebuild stage1 only and not build stage2-instrumented or regenerate the profile data. $ ninja -v -C build stage2-instrumented-generate-profdata The second problem was that in some cases, the profile data would be regenerated, but not all of the stage2 targets would be rebuilt. One common scenario where this would happen is: This would work as expected. $ ninja -C build stage2-check-all This would regenerate the profile data, but then only build the targets that were needed for install, but weren't needed for check-all. This would cause errors like: ld.lld: error: Function Import: link error: linking module flags 'ProfileSummary': IDs have conflicting values in ... This is because the executibles being built for the install target used the new profile data, but they were linking with libraries that used the old profile data. $ ninja -C build stage2-install With this change we can re-enable PGO for the release builds.
1 parent 52050f3 commit be06761

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

clang/CMakeLists.txt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -848,23 +848,17 @@ if (CLANG_ENABLE_BOOTSTRAP)
848848
set(CLANG_BOOTSTRAP_TARGETS check-llvm check-clang check-all)
849849
endif()
850850
foreach(target ${CLANG_BOOTSTRAP_TARGETS})
851-
# Install targets have side effects, so we always want to execute them.
852-
# "install" is reserved by CMake and can't be used as a step name for
853-
# ExternalProject_Add_Step, so we can match against "^install-" instead of
854-
# "^install" to get a tighter match. CMake's installation scripts already
855-
# skip up-to-date files, so there's no behavior change if you install to the
856-
# same destination multiple times.
857-
if(target MATCHES "^install-")
858-
set(step_always ON)
859-
else()
860-
set(step_always OFF)
861-
endif()
862851

863852
ExternalProject_Add_Step(${NEXT_CLANG_STAGE} ${target}
864853
COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target ${target}
865854
COMMENT "Performing ${target} for '${NEXT_CLANG_STAGE}'"
866855
DEPENDEES configure
867-
ALWAYS ${step_always}
856+
# We need to set ALWAYS to ON here, otherwise these targets won't be
857+
# built on a second invocation of ninja. The targets have their own
858+
# logic to determine if they should build or not so setting ALWAYS ON
859+
# here does not mean the targets will always rebuild it just means that
860+
# they will check their dependenices and see if they need to be built.
861+
ALWAYS ON
868862
EXCLUDE_FROM_MAIN ON
869863
USES_TERMINAL 1
870864
)

clang/cmake/caches/Release.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ endfunction()
3030
#
3131
# cmake -D LLVM_RELEASE_ENABLE_PGO=ON -C Release.cmake
3232
set(LLVM_RELEASE_ENABLE_LTO THIN CACHE STRING "")
33-
set(LLVM_RELEASE_ENABLE_PGO OFF CACHE BOOL "")
33+
set(LLVM_RELEASE_ENABLE_PGO ON CACHE BOOL "")
3434
set(LLVM_RELEASE_ENABLE_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind" CACHE STRING "")
3535
set(LLVM_RELEASE_ENABLE_PROJECTS "clang;lld;lldb;clang-tools-extra;bolt;polly;mlir;flang" CACHE STRING "")
3636
# Note we don't need to add install here, since it is one of the pre-defined

clang/utils/perf-training/CMakeLists.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if(LLVM_BUILD_INSTRUMENTED)
1515
add_lit_testsuite(generate-profraw "Generating clang PGO data"
1616
${CMAKE_CURRENT_BINARY_DIR}/pgo-data/
1717
EXCLUDE_FROM_CHECK_ALL
18-
DEPENDS clang clear-profraw ${CLANG_PGO_TRAINING_DEPS}
18+
DEPENDS clear-profraw
1919
)
2020

2121
add_custom_target(clear-profraw
@@ -29,10 +29,21 @@ if(LLVM_BUILD_INSTRUMENTED)
2929
if(NOT LLVM_PROFDATA)
3030
message(STATUS "To enable merging PGO data LLVM_PROFDATA has to point to llvm-profdata")
3131
else()
32-
add_custom_target(generate-profdata
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
3342
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/
3443
COMMENT "Merging profdata"
35-
DEPENDS generate-profraw)
44+
DEPENDS clang ${CLANG_PGO_TRAINING_DEPS}
45+
)
46+
add_custom_target(generate-profdata DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang.profdata)
3647
if (CLANG_PGO_TRAINING_DATA_SOURCE_DIR)
3748
llvm_ExternalProject_Add(generate-profraw-external ${CLANG_PGO_TRAINING_DATA_SOURCE_DIR}
3849
USE_TOOLCHAIN EXLUDE_FROM_ALL NO_INSTALL DEPENDS generate-profraw)

0 commit comments

Comments
 (0)