Amir created this revision.
Amir added reviewers: bolt, phosek, beanz.
Herald added subscribers: wlei, ekilmer, wenlei.
Herald added a project: All.
Amir requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Factor out bolt project dependency from Clang-BOLT usage.
Allow using

- user-specified BOLT: `LLVM_BOLT` and `MERGE_FDATA` variables,
- BOLT from system path (with CMake's `find_program`),
- built from `bolt` project (the only previously supported way).

Customize BOLT-PGO.cmake cache file to build BOLT in first-stage build, and
passthrough and use it in the final clang-bolt invocation (on top of PGO 
binary).
This avoids building PGO'd BOLT (first instrumented and then optimized), cutting
the build time.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156607

Files:
  clang/CMakeLists.txt
  clang/cmake/caches/BOLT-PGO-stage2.cmake
  clang/cmake/caches/BOLT-PGO.cmake
  clang/utils/perf-training/CMakeLists.txt

Index: clang/utils/perf-training/CMakeLists.txt
===================================================================
--- clang/utils/perf-training/CMakeLists.txt
+++ clang/utils/perf-training/CMakeLists.txt
@@ -78,9 +78,21 @@
     COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py clean ${CMAKE_CURRENT_BINARY_DIR} fdata
     COMMENT "Clearing old BOLT fdata")
 
-  # Merge profiles into one using merge-fdata
-  add_custom_target(clang-bolt-profile
-    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}
-    COMMENT "Merging BOLT fdata"
-    DEPENDS merge-fdata generate-bolt-fdata)
+  add_custom_target(clang-bolt-profile-deps)
+  if ("bolt" IN_LIST LLVM_ENABLE_PROJECTS)
+    add_dependencies(clang-bolt-profile-deps merge-fdata)
+    set(MERGE_FDATA $<TARGET_FILE:merge-fdata>)
+  elseif (NOT MERGE_FDATA)
+    find_program(MERGE_FDATA merge-fdata)
+  endif()
+
+  if (NOT MERGE_FDATA AND NOT "bolt" IN_LIST LLVM_ENABLE_PROJECTS)
+    message(STATUS "To enable optimizing Clang with BOLT enable bolt project or set MERGE_FDATA")
+  else()
+    # Merge profiles into one using merge-fdata
+    add_custom_target(clang-bolt-profile
+      COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py merge-fdata ${MERGE_FDATA} ${CMAKE_CURRENT_BINARY_DIR}/prof.fdata ${CMAKE_CURRENT_BINARY_DIR}
+      COMMENT "Merging BOLT fdata"
+      DEPENDS generate-bolt-fdata)
+  endif()
 endif()
Index: clang/cmake/caches/BOLT-PGO.cmake
===================================================================
--- clang/cmake/caches/BOLT-PGO.cmake
+++ clang/cmake/caches/BOLT-PGO.cmake
@@ -11,7 +11,9 @@
   install-distribution
   CACHE STRING "")
 
+set(BOOTSTRAP_LLVM_BOLT ON)
+
 set(PGO_BUILD_CONFIGURATION
-  ${CMAKE_CURRENT_LIST_DIR}/BOLT.cmake
+  ${CMAKE_CURRENT_LIST_DIR}/BOLT-PGO-stage2.cmake
   CACHE STRING "")
 include(${CMAKE_CURRENT_LIST_DIR}/PGO.cmake)
Index: clang/cmake/caches/BOLT-PGO-stage2.cmake
===================================================================
--- /dev/null
+++ clang/cmake/caches/BOLT-PGO-stage2.cmake
@@ -0,0 +1,13 @@
+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 "clang" CACHE STRING "")
+set(LLVM_TARGETS_TO_BUILD Native CACHE STRING "")
+
+# setup toolchain
+set(LLVM_INSTALL_TOOLCHAIN_ONLY ON CACHE BOOL "")
+set(LLVM_DISTRIBUTION_COMPONENTS
+  clang
+  clang-resource-headers
+  CACHE STRING "")
Index: clang/CMakeLists.txt
===================================================================
--- clang/CMakeLists.txt
+++ clang/CMakeLists.txt
@@ -711,6 +711,16 @@
     endif()
   endif()
 
+  if(BOOTSTRAP_LLVM_BOLT)
+    add_dependencies(clang-bootstrap-deps llvm-bolt merge-fdata)
+    set(LLVM_BOLT ${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-bolt)
+    set(MERGE_FDATA ${LLVM_RUNTIME_OUTPUT_INTDIR}/merge-fdata)
+    list(APPEND _BOOTSTRAP_DEFAULT_PASSTHROUGH
+      LLVM_BOLT
+      MERGE_FDATA
+    )
+  endif()
+
   if(BOOTSTRAP_LLVM_BUILD_INSTRUMENTED)
     add_dependencies(clang-bootstrap-deps llvm-profdata)
     set(PGO_OPT -DLLVM_PROFDATA=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-profdata)
@@ -854,35 +864,47 @@
   set(CLANG_INSTRUMENTED ${CLANG_PATH}-bolt.inst)
   set(BOLT_FDATA ${CMAKE_CURRENT_BINARY_DIR}/utils/perf-training/prof.fdata)
 
-  # 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=${BOLT_FDATA}
-    COMMENT "Instrumenting clang binary with BOLT"
-    VERBATIM
-  )
+  add_custom_target(bolt-clang-instrumented-deps)
+  if ("bolt" IN_LIST LLVM_ENABLE_PROJECTS)
+    add_dependencies(bolt-clang-instrumented-deps llvm-bolt)
+    set(LLVM_BOLT $<TARGET_FILE:llvm-bolt>)
+  elseif (NOT LLVM_BOLT)
+    find_program(LLVM_BOLT llvm-bolt)
+  endif()
 
-  # Optimize original (pre-bolt) Clang using the collected profile
-  set(CLANG_OPTIMIZED ${CMAKE_CURRENT_BINARY_DIR}/clang.bolt)
-  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 ${BOLT_FDATA}
-      -reorder-blocks=ext-tsp -reorder-functions=hfsort+ -split-functions
-      -split-all-cold -split-eh -dyno-stats -icf=1 -use-gnu-stack
-    COMMAND ${CMAKE_COMMAND} -E rename ${CLANG_OPTIMIZED} $<TARGET_FILE:clang>
-    COMMENT "Optimizing Clang with BOLT"
-    VERBATIM
-  )
+  if (NOT LLVM_BOLT AND NOT "bolt" IN_LIST LLVM_ENABLE_PROJECTS)
+    message(STATUS "To enable optimizing Clang with BOLT enable bolt project or set LLVM_BOLT")
+  else()
+    # Instrument clang with BOLT
+    add_custom_target(clang-instrumented
+      DEPENDS ${CLANG_INSTRUMENTED}
+    )
+    add_custom_command(OUTPUT ${CLANG_INSTRUMENTED}
+      DEPENDS bolt-clang-instrumented-deps clang
+      COMMAND ${LLVM_BOLT} ${CLANG_PATH} -o ${CLANG_INSTRUMENTED}
+        -instrument --instrumentation-file-append-pid
+        --instrumentation-file=${BOLT_FDATA}
+      COMMENT "Instrumenting clang binary with BOLT"
+      VERBATIM
+    )
+
+    # Optimize original (pre-bolt) Clang using the collected profile
+    set(CLANG_OPTIMIZED ${CMAKE_CURRENT_BINARY_DIR}/clang.bolt)
+    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 ${BOLT_FDATA}
+        -reorder-blocks=ext-tsp -reorder-functions=hfsort+ -split-functions
+        -split-all-cold -split-eh -dyno-stats -icf=1 -use-gnu-stack
+      COMMAND ${CMAKE_COMMAND} -E rename ${CLANG_OPTIMIZED} $<TARGET_FILE:clang>
+      COMMENT "Optimizing Clang with BOLT"
+      VERBATIM
+    )
+  endif()
 endif()
 
 if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D156607: [Clang][CMake... Amir Ayupov via Phabricator via cfe-commits

Reply via email to