[llvm] [BOLT] Only link and initialize supported targets (PR #127509)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 17 07:38:39 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-bolt

Author: Nikita Popov (nikic)

<details>
<summary>Changes</summary>

Bolt currently links and initializes all LLVM targets. This substantially increases the binary size, and link time if LTO is used.

Instead, only link the targets specified by BOLT_TARGETS_TO_BUILD. We also have to only initialize those targets, so generate a TargetConfig.def file with the necessary information. The way the initialization is done mirrors what llvm-exegesis does.

This reduces llvm-bolt size from 137MB to 78MB for me.

---
Full diff: https://github.com/llvm/llvm-project/pull/127509.diff


14 Files Affected:

- (modified) bolt/CMakeLists.txt (+8) 
- (added) bolt/include/bolt/Core/TargetConfig.def.in (+23) 
- (modified) bolt/tools/binary-analysis/CMakeLists.txt (+1-1) 
- (modified) bolt/tools/binary-analysis/binary-analysis.cpp (+9-7) 
- (modified) bolt/tools/driver/CMakeLists.txt (+1-1) 
- (modified) bolt/tools/driver/llvm-bolt.cpp (+9-7) 
- (modified) bolt/tools/heatmap/CMakeLists.txt (+1-1) 
- (modified) bolt/tools/heatmap/heatmap.cpp (+9-7) 
- (modified) bolt/tools/llvm-bolt-fuzzer/CMakeLists.txt (+1-1) 
- (modified) bolt/tools/llvm-bolt-fuzzer/llvm-bolt-fuzzer.cpp (+9-6) 
- (modified) bolt/unittests/Core/BinaryContext.cpp (+9-6) 
- (modified) bolt/unittests/Core/CMakeLists.txt (+1-1) 
- (modified) bolt/unittests/Core/MCPlusBuilder.cpp (+9-6) 
- (modified) bolt/unittests/Core/MemoryMaps.cpp (+9-6) 


``````````diff
diff --git a/bolt/CMakeLists.txt b/bolt/CMakeLists.txt
index 04db160b64b05..f5ffa81227064 100644
--- a/bolt/CMakeLists.txt
+++ b/bolt/CMakeLists.txt
@@ -202,3 +202,11 @@ endif()
 
 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/bolt/RuntimeLibs/RuntimeLibraryVariables.inc.in
                ${CMAKE_CURRENT_BINARY_DIR}/include/bolt/RuntimeLibs/RuntimeLibraryVariables.inc @ONLY)
+
+set(BOLT_ENUM_TARGETS "")
+foreach(t ${BOLT_TARGETS_TO_BUILD})
+  set(BOLT_ENUM_TARGETS "${BOLT_ENUM_TARGETS}BOLT_TARGET(${t})\n")
+endforeach(t)
+
+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/bolt/Core/TargetConfig.def.in
+               ${CMAKE_CURRENT_BINARY_DIR}/include/bolt/Core/TargetConfig.def @ONLY)
diff --git a/bolt/include/bolt/Core/TargetConfig.def.in b/bolt/include/bolt/Core/TargetConfig.def.in
new file mode 100644
index 0000000000000..a52ebd92b56fd
--- /dev/null
+++ b/bolt/include/bolt/Core/TargetConfig.def.in
@@ -0,0 +1,23 @@
+//===-- TargetConfig.def.in - Information about available targets ---------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is configured by the build system to define the available bolt
+// targets.
+//
+// The variant of this file not ending with .in has been autogenerated by the
+// LLVM build. Do not edit!
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef BOLT_TARGET
+#  error Please define the macro BOLT_TARGET(TargetName)
+#endif
+
+ at BOLT_ENUM_TARGETS@
+
+#undef BOLT_TARGET
diff --git a/bolt/tools/binary-analysis/CMakeLists.txt b/bolt/tools/binary-analysis/CMakeLists.txt
index 841fc5b371185..29f224e0f66ff 100644
--- a/bolt/tools/binary-analysis/CMakeLists.txt
+++ b/bolt/tools/binary-analysis/CMakeLists.txt
@@ -1,5 +1,5 @@
 set(LLVM_LINK_COMPONENTS
-  ${LLVM_TARGETS_TO_BUILD}
+  ${BOLT_TARGETS_TO_BUILD}
   MC
   Object
   Support
diff --git a/bolt/tools/binary-analysis/binary-analysis.cpp b/bolt/tools/binary-analysis/binary-analysis.cpp
index b03fee3e025ae..779a097ff010d 100644
--- a/bolt/tools/binary-analysis/binary-analysis.cpp
+++ b/bolt/tools/binary-analysis/binary-analysis.cpp
@@ -88,13 +88,15 @@ int main(int argc, char **argv) {
   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
 
   // Initialize targets and assembly printers/parsers.
-  llvm::InitializeAllTargetInfos();
-  llvm::InitializeAllTargetMCs();
-  llvm::InitializeAllAsmParsers();
-  llvm::InitializeAllDisassemblers();
-
-  llvm::InitializeAllTargets();
-  llvm::InitializeAllAsmPrinters();
+#define BOLT_TARGET(target) \
+  LLVMInitialize##target##TargetInfo(); \
+  LLVMInitialize##target##TargetMC(); \
+  LLVMInitialize##target##AsmParser(); \
+  LLVMInitialize##target##Disassembler(); \
+  LLVMInitialize##target##Target(); \
+  LLVMInitialize##target##AsmPrinter();
+
+#include "bolt/Core/TargetConfig.def"
 
   ParseCommandLine(argc, argv);
 
diff --git a/bolt/tools/driver/CMakeLists.txt b/bolt/tools/driver/CMakeLists.txt
index 9bf9ff85edc7b..4b3c7416de974 100644
--- a/bolt/tools/driver/CMakeLists.txt
+++ b/bolt/tools/driver/CMakeLists.txt
@@ -1,5 +1,5 @@
 set(LLVM_LINK_COMPONENTS
-  ${LLVM_TARGETS_TO_BUILD}
+  ${BOLT_TARGETS_TO_BUILD}
   MC
   Object
   Support
diff --git a/bolt/tools/driver/llvm-bolt.cpp b/bolt/tools/driver/llvm-bolt.cpp
index f151cf5f63fc5..0f29ea0d3c531 100644
--- a/bolt/tools/driver/llvm-bolt.cpp
+++ b/bolt/tools/driver/llvm-bolt.cpp
@@ -183,13 +183,15 @@ int main(int argc, char **argv) {
   std::string ToolPath = llvm::sys::fs::getMainExecutable(argv[0], nullptr);
 
   // Initialize targets and assembly printers/parsers.
-  llvm::InitializeAllTargetInfos();
-  llvm::InitializeAllTargetMCs();
-  llvm::InitializeAllAsmParsers();
-  llvm::InitializeAllDisassemblers();
-
-  llvm::InitializeAllTargets();
-  llvm::InitializeAllAsmPrinters();
+#define BOLT_TARGET(target) \
+  LLVMInitialize##target##TargetInfo(); \
+  LLVMInitialize##target##TargetMC(); \
+  LLVMInitialize##target##AsmParser(); \
+  LLVMInitialize##target##Disassembler(); \
+  LLVMInitialize##target##Target(); \
+  LLVMInitialize##target##AsmPrinter();
+
+#include "bolt/Core/TargetConfig.def"
 
   ToolName = argv[0];
 
diff --git a/bolt/tools/heatmap/CMakeLists.txt b/bolt/tools/heatmap/CMakeLists.txt
index acddc7a50e8b1..c5d3f67413929 100644
--- a/bolt/tools/heatmap/CMakeLists.txt
+++ b/bolt/tools/heatmap/CMakeLists.txt
@@ -1,5 +1,5 @@
 set(LLVM_LINK_COMPONENTS
-  ${LLVM_TARGETS_TO_BUILD}
+  ${BOLT_TARGETS_TO_BUILD}
   MC
   Object
   Support
diff --git a/bolt/tools/heatmap/heatmap.cpp b/bolt/tools/heatmap/heatmap.cpp
index 3bb9f2ce7491d..4011548b5c7d0 100644
--- a/bolt/tools/heatmap/heatmap.cpp
+++ b/bolt/tools/heatmap/heatmap.cpp
@@ -76,13 +76,15 @@ int main(int argc, char **argv) {
     opts::OutputFilename = "-";
 
   // Initialize targets and assembly printers/parsers.
-  llvm::InitializeAllTargetInfos();
-  llvm::InitializeAllTargetMCs();
-  llvm::InitializeAllAsmParsers();
-  llvm::InitializeAllDisassemblers();
-
-  llvm::InitializeAllTargets();
-  llvm::InitializeAllAsmPrinters();
+#define BOLT_TARGET(target) \
+  LLVMInitialize##target##TargetInfo(); \
+  LLVMInitialize##target##TargetMC(); \
+  LLVMInitialize##target##AsmParser(); \
+  LLVMInitialize##target##Disassembler(); \
+  LLVMInitialize##target##Target(); \
+  LLVMInitialize##target##AsmPrinter();
+
+#include "bolt/Core/TargetConfig.def"
 
   ToolName = argv[0];
   std::string ToolPath = GetExecutablePath(argv[0]);
diff --git a/bolt/tools/llvm-bolt-fuzzer/CMakeLists.txt b/bolt/tools/llvm-bolt-fuzzer/CMakeLists.txt
index f21285f634bad..7eaacb74a9da6 100644
--- a/bolt/tools/llvm-bolt-fuzzer/CMakeLists.txt
+++ b/bolt/tools/llvm-bolt-fuzzer/CMakeLists.txt
@@ -1,5 +1,5 @@
 set(LLVM_LINK_COMPONENTS
-  ${LLVM_TARGETS_TO_BUILD}
+  ${BOLT_TARGETS_TO_BUILD}
   )
 
 add_llvm_fuzzer(llvm-bolt-fuzzer
diff --git a/bolt/tools/llvm-bolt-fuzzer/llvm-bolt-fuzzer.cpp b/bolt/tools/llvm-bolt-fuzzer/llvm-bolt-fuzzer.cpp
index bdb5768a91da1..3c64e30653120 100644
--- a/bolt/tools/llvm-bolt-fuzzer/llvm-bolt-fuzzer.cpp
+++ b/bolt/tools/llvm-bolt-fuzzer/llvm-bolt-fuzzer.cpp
@@ -58,13 +58,16 @@ extern "C" int LLVMFuzzerTestOneInput(const char *Data, size_t Size) {
 
 extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
                                                         char ***argv) {
-  llvm::InitializeAllTargetInfos();
-  llvm::InitializeAllTargetMCs();
-  llvm::InitializeAllAsmParsers();
-  llvm::InitializeAllDisassemblers();
+  // Initialize targets and assembly printers/parsers.
+#define BOLT_TARGET(target) \
+  LLVMInitialize##target##TargetInfo(); \
+  LLVMInitialize##target##TargetMC(); \
+  LLVMInitialize##target##AsmParser(); \
+  LLVMInitialize##target##Disassembler(); \
+  LLVMInitialize##target##Target(); \
+  LLVMInitialize##target##AsmPrinter();
 
-  llvm::InitializeAllTargets();
-  llvm::InitializeAllAsmPrinters();
+#include "bolt/Core/TargetConfig.def"
 
   return 0;
 }
diff --git a/bolt/unittests/Core/BinaryContext.cpp b/bolt/unittests/Core/BinaryContext.cpp
index 0fefa1b83c3c2..0a58b079c3a5b 100644
--- a/bolt/unittests/Core/BinaryContext.cpp
+++ b/bolt/unittests/Core/BinaryContext.cpp
@@ -27,12 +27,15 @@ struct BinaryContextTester : public testing::TestWithParam<Triple::ArchType> {
 
 protected:
   void initalizeLLVM() {
-    llvm::InitializeAllTargetInfos();
-    llvm::InitializeAllTargetMCs();
-    llvm::InitializeAllAsmParsers();
-    llvm::InitializeAllDisassemblers();
-    llvm::InitializeAllTargets();
-    llvm::InitializeAllAsmPrinters();
+#define BOLT_TARGET(target) \
+    LLVMInitialize##target##TargetInfo(); \
+    LLVMInitialize##target##TargetMC(); \
+    LLVMInitialize##target##AsmParser(); \
+    LLVMInitialize##target##Disassembler(); \
+    LLVMInitialize##target##Target(); \
+    LLVMInitialize##target##AsmPrinter();
+
+#include "bolt/Core/TargetConfig.def"
   }
 
   void prepareElf() {
diff --git a/bolt/unittests/Core/CMakeLists.txt b/bolt/unittests/Core/CMakeLists.txt
index 208cf6ced7358..8ac88b701ea05 100644
--- a/bolt/unittests/Core/CMakeLists.txt
+++ b/bolt/unittests/Core/CMakeLists.txt
@@ -2,7 +2,7 @@ set(LLVM_LINK_COMPONENTS
   DebugInfoDWARF
   Object
   MC
-  ${LLVM_TARGETS_TO_BUILD}
+  ${BOLT_TARGETS_TO_BUILD}
   )
 
 add_bolt_unittest(CoreTests
diff --git a/bolt/unittests/Core/MCPlusBuilder.cpp b/bolt/unittests/Core/MCPlusBuilder.cpp
index 5488cae366284..06c5598da7f57 100644
--- a/bolt/unittests/Core/MCPlusBuilder.cpp
+++ b/bolt/unittests/Core/MCPlusBuilder.cpp
@@ -37,12 +37,15 @@ struct MCPlusBuilderTester : public testing::TestWithParam<Triple::ArchType> {
 
 protected:
   void initalizeLLVM() {
-    llvm::InitializeAllTargetInfos();
-    llvm::InitializeAllTargetMCs();
-    llvm::InitializeAllAsmParsers();
-    llvm::InitializeAllDisassemblers();
-    llvm::InitializeAllTargets();
-    llvm::InitializeAllAsmPrinters();
+#define BOLT_TARGET(target) \
+    LLVMInitialize##target##TargetInfo(); \
+    LLVMInitialize##target##TargetMC(); \
+    LLVMInitialize##target##AsmParser(); \
+    LLVMInitialize##target##Disassembler(); \
+    LLVMInitialize##target##Target(); \
+    LLVMInitialize##target##AsmPrinter();
+
+#include "bolt/Core/TargetConfig.def"
   }
 
   void prepareElf() {
diff --git a/bolt/unittests/Core/MemoryMaps.cpp b/bolt/unittests/Core/MemoryMaps.cpp
index 06073d0a82e14..d823655e7a701 100644
--- a/bolt/unittests/Core/MemoryMaps.cpp
+++ b/bolt/unittests/Core/MemoryMaps.cpp
@@ -38,12 +38,15 @@ struct MemoryMapsTester : public testing::TestWithParam<Triple::ArchType> {
 
 protected:
   void initalizeLLVM() {
-    llvm::InitializeAllTargetInfos();
-    llvm::InitializeAllTargetMCs();
-    llvm::InitializeAllAsmParsers();
-    llvm::InitializeAllDisassemblers();
-    llvm::InitializeAllTargets();
-    llvm::InitializeAllAsmPrinters();
+#define BOLT_TARGET(target) \
+    LLVMInitialize##target##TargetInfo(); \
+    LLVMInitialize##target##TargetMC(); \
+    LLVMInitialize##target##AsmParser(); \
+    LLVMInitialize##target##Disassembler(); \
+    LLVMInitialize##target##Target(); \
+    LLVMInitialize##target##AsmPrinter();
+
+#include "bolt/Core/TargetConfig.def"
   }
 
   void prepareElf() {

``````````

</details>


https://github.com/llvm/llvm-project/pull/127509


More information about the llvm-commits mailing list