diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt --- a/clang/CMakeLists.txt +++ b/clang/CMakeLists.txt @@ -443,7 +443,7 @@ "HAVE_CLANG_PLUGIN_SUPPORT" OFF) # If libstdc++ is statically linked, clang-repl needs to statically link libstdc++ -# itself, which is not possible in many platforms because of current limitations in +# itself, which is not possible in many platforms because of current limitations in # JIT stack. (more platforms need to be supported by JITLink) if(NOT LLVM_STATIC_LINK_CXX_STDLIB) set(HAVE_CLANG_REPL_SUPPORT ON) @@ -878,6 +878,114 @@ endforeach() endif() +if (CLANG_BOLT_INSTRUMENT) + set(CLANG_PATH ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang) + set(CLANGXX_PATH ${CLANG_PATH}++) + set(CLANG_INSTRUMENTED ${CLANG_PATH}-bolt.inst) + set(CLANGXX_INSTRUMENTED ${CLANGXX_PATH}-bolt.inst) + set(CLANG_OPTIMIZED ${CLANG_PATH}-bolt) + set(CLANGXX_OPTIMIZED ${CLANGXX_PATH}-bolt) + + # Instrument clang with BOLT + add_custom_target(clang-instrumented + DEPENDS ${CLANG_INSTRUMENTED} + ) + add_custom_command(OUTPUT ${CLANG_INSTRUMENTED} + DEPENDS clang llvm-bolt + COMMAND llvm-bolt ${CLANG_PATH} -o ${CLANG_INSTRUMENTED} + -instrument --instrumentation-file-append-pid + --instrumentation-file=${CMAKE_CURRENT_BINARY_DIR}/prof.fdata + COMMENT "Instrumenting clang binary with BOLT" + VERBATIM + ) + + # Make a symlink from clang-bolt.inst to clang++-bolt.inst + add_custom_target(clang++-instrumented + DEPENDS ${CLANGXX_INSTRUMENTED} + ) + add_custom_command(OUTPUT ${CLANGXX_INSTRUMENTED} + DEPENDS clang-instrumented + COMMAND ${CMAKE_COMMAND} -E create_symlink + ${CLANG_INSTRUMENTED} + ${CLANGXX_INSTRUMENTED} + COMMENT "Creating symlink from BOLT instrumented clang to clang++" + VERBATIM + ) + + # Build specified targets with instrumented Clang to collect the profile + set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt-instrumented-clang-stamps/) + set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt-instrumented-clang-bins/) + set(build_configuration "$") + include(ExternalProject) + ExternalProject_Add(bolt-instrumentation-profile + DEPENDS clang++-instrumented + PREFIX bolt-instrumentation-profile + SOURCE_DIR ${CMAKE_SOURCE_DIR} + STAMP_DIR ${STAMP_DIR} + BINARY_DIR ${BINARY_DIR} + EXCLUDE_FROM_ALL 1 + 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_C_COMPILER=${CLANG_INSTRUMENTED} + -DCMAKE_CXX_COMPILER=${CLANGXX_INSTRUMENTED} + -DCMAKE_ASM_COMPILER=${CLANG_INSTRUMENTED} + -DCMAKE_ASM_COMPILER_ID=Clang + -DCMAKE_BUILD_TYPE=Release + -DLLVM_ENABLE_PROJECTS=${CLANG_BOLT_INSTRUMENT_PROJECTS} + -DLLVM_TARGETS_TO_BUILD=${LLVM_TARGETS_TO_BUILD} + BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR} + --config ${build_configuration} + --target ${CLANG_BOLT_INSTRUMENT_TARGETS} + INSTALL_COMMAND "" + STEP_TARGETS configure build + USES_TERMINAL_CONFIGURE 1 + USES_TERMINAL_BUILD 1 + USES_TERMINAL_INSTALL 1 + ) + + # Merge profiles into one using merge-fdata + add_custom_target(clang-bolt-profile + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/prof.fdata + ) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/prof.fdata + DEPENDS merge-fdata bolt-instrumentation-profile-build + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND sh -c "$ prof.fdata.* -o prof.fdata" + COMMENT "Preparing BOLT profile" + VERBATIM + ) + + # Optimize original (pre-bolt) Clang using the collected profile + add_custom_target(clang-bolt + DEPENDS ${CLANG_OPTIMIZED} + ) + add_custom_command(OUTPUT ${CLANG_OPTIMIZED} + DEPENDS clang-bolt-profile + COMMAND llvm-bolt ${CLANG_PATH} + -o ${CLANG_OPTIMIZED} + -data ${CMAKE_CURRENT_BINARY_DIR}/prof.fdata + -reorder-blocks=ext-tsp -reorder-functions=hfsort+ -split-functions + -split-all-cold -split-eh -dyno-stats -icf=1 -use-gnu-stack + COMMENT "Optimizing Clang with BOLT" + VERBATIM + ) + + # Make a symlink from clang-bolt to clang++-bolt + add_custom_target(clang++-bolt + DEPENDS ${CLANGXX_OPTIMIZED} + ) + add_custom_command(OUTPUT ${CLANGXX_OPTIMIZED} + DEPENDS clang-bolt + COMMAND ${CMAKE_COMMAND} -E create_symlink + ${CLANG_OPTIMIZED} + ${CLANGXX_OPTIMIZED} + COMMENT "Creating symlink from BOLT optimized clang to clang++" + VERBATIM + ) +endif() + if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION) add_subdirectory(utils/ClangVisualizers) endif() diff --git a/clang/cmake/caches/BOLT.cmake b/clang/cmake/caches/BOLT.cmake new file mode 100644 --- /dev/null +++ b/clang/cmake/caches/BOLT.cmake @@ -0,0 +1,14 @@ +set(CMAKE_BUILD_TYPE Release CACHE STRING "") +set(CLANG_BOLT_INSTRUMENT ON CACHE BOOL "") +set(CLANG_BOLT_INSTRUMENT_PROJECTS "llvm" CACHE STRING "") +set(CLANG_BOLT_INSTRUMENT_TARGETS "count" CACHE STRING "") +set(CMAKE_EXE_LINKER_FLAGS "-Wl,--emit-relocs,-znow" CACHE STRING "") + +set(LLVM_ENABLE_PROJECTS "bolt;clang" 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()