[PATCH] D132975: [clang][BOLT] Add clangbolt target (WIP)
Amir Ayupov via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 30 14:23:52 PDT 2022
Amir created this revision.
Amir added a reviewer: bolt.
Herald added subscribers: treapster, wenlei, mgorny.
Herald added a project: All.
Amir requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
This patch adds `CLANG_BOLT_INSTRUMENT` option that applies BOLT instrumentation
to Clang, performs a bootstrap build with the resulting Clang, merges resulting
fdata files into a single profile file, and uses it to perform BOLT optimization
on the original Clang binary.
The intended use of the functionality is through BOLT CMake cache file, similar
to PGO 2-stage build:
cmake <llvm-project>/llvm -C <llvm-project>/clang/cmake/caches/BOLT.cmake
ninja clang-bolt
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D132975
Files:
clang/CMakeLists.txt
clang/cmake/caches/BOLT.cmake
Index: clang/cmake/caches/BOLT.cmake
===================================================================
--- /dev/null
+++ clang/cmake/caches/BOLT.cmake
@@ -0,0 +1,12 @@
+set(CMAKE_BUILD_TYPE Release CACHE STRING "")
+set(CLANG_BOLT_INSTRUMENT ON CACHE BOOL "")
+set(CMAKE_EXE_LINKER_FLAGS "-Wl,--emit-relocs,-znow" CACHE STRING "")
+
+set(LLVM_ENABLE_PROJECTS "bolt;clang;lld" CACHE STRING "")
+set(LLVM_TARGETS_TO_BUILD Native CACHE STRING "")
+
+# Disable function splitting enabled by default in GCC8+
+if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-reorder-blocks-and-partition")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-reorder-blocks-and-partition")
+endif()
Index: clang/CMakeLists.txt
===================================================================
--- clang/CMakeLists.txt
+++ clang/CMakeLists.txt
@@ -878,6 +878,57 @@
endforeach()
endif()
+if (CLANG_BOLT_INSTRUMENT)
+ find_program(LLVM_BOLT llvm-bolt)
+ find_program(MERGE_FDATA merge-fdata)
+
+ # Instrument clang with BOLT
+ add_custom_target(clang-instrumented
+ DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/bin/clang.bolt.inst
+ )
+ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/bin/clang.bolt.inst
+ DEPENDS clang
+ COMMAND ${LLVM_BOLT} ${CMAKE_BINARY_DIR}/bin/clang -o
+ ${CMAKE_CURRENT_BINARY_DIR}/bin/clang.bolt.inst
+ -instrument --instrumentation-file-append-pid
+ --instrumentation-file=${CMAKE_CURRENT_BINARY_DIR}/bin/prof.fdata
+ COMMENT "Instrumenting clang binary with BOLT"
+ )
+ # Make a symlink from clang.bolt.inst to clang++.bolt.inst
+ add_clang_symlink(${CMAKE_CURRENT_BINARY_DIR}/bin/clang++.bolt.inst
+ ${CMAKE_CURRENT_BINARY_DIR}/bin/clang.bolt.inst)
+
+ # Configure and build Clang with instrumented Clang to collect the profile
+ include(ExternalProject)
+ ExternalProject_Add(bolt-instrumentation-profile
+ DEPENDS clang
+ PREFIX bolt-instrumentation-profile
+ SOURCE_DIR ${CMAKE_SOURCE_DIR}
+ CMAKE_ARGS
+ # We shouldn't need to set this here, but INSTALL_DIR doesn't
+ # seem to work, so instead I'm passing this through
+ -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
+ -DCMAKE_CXX_COMPILER=${CMAKE_CURRENT_BINARY_DIR}/bin/clang++.bolt.inst
+ -DCMAKE_C_COMPILER=${CMAKE_CURRENT_BINARY_DIR}/bin/clang.bolt.inst
+ STEP_TARGETS configure build install
+ )
+ # Merge profiles into one using merge-fdata
+ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/bin/prof.fdata
+ COMMAND ${MERGE_FDATA} ${CMAKE_CURRENT_BINARY_DIR}/bin/prof.fdata.*
+ -o ${CMAKE_CURRENT_BINARY_DIR}/bin/prof.fdata
+ )
+ # Optimize original (pre-bolt) Clang using the collected profile
+ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/bin/clang.bolt
+ DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/bin/prof.fdata clang
+ COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-bolt ${CMAKE_BINARY_DIR}/bin/clang
+ -o ${CMAKE_BINARY_DIR}/bin/clang.bolt -fdata
+ ${CMAKE_CURRENT_BINARY_DIR}/bin/prof.fdata
+ )
+ add_custom_target(clang-bolt
+ DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/bin/clang.bolt
+ )
+endif()
+
if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
add_subdirectory(utils/ClangVisualizers)
endif()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132975.456796.patch
Type: text/x-patch
Size: 3293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220830/4980aff1/attachment.bin>
More information about the cfe-commits
mailing list