Skip to content

Commit 1194214

Browse files
RossComputerGuyyuxuanchen1997
authored andcommitted
[BOLT] Enable standalone build (#97130)
Summary: Continue from #87196 as author did not have much time, I have taken over working on this PR. We would like to have this so it'll be easier to package for Nix. Can be tested by copying cmake, bolt, third-party, and llvm directories out into their own directory with this PR applied and then build bolt. --------- Co-authored-by: pca006132 <[email protected]> Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250600
1 parent 185f5e7 commit 1194214

File tree

14 files changed

+199
-26
lines changed

14 files changed

+199
-26
lines changed

bolt/CMakeLists.txt

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
13
set(LLVM_SUBPROJECT_TITLE "BOLT")
24

3-
include(ExternalProject)
5+
if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
6+
set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
7+
endif()
8+
include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
9+
NO_POLICY_SCOPE)
10+
11+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
12+
project(bolt)
13+
set(BOLT_BUILT_STANDALONE TRUE)
14+
endif()
415

516
set(BOLT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
617
set(BOLT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
@@ -9,6 +20,42 @@ set(CMAKE_CXX_STANDARD 17)
920
# Add path for custom modules.
1021
list(INSERT CMAKE_MODULE_PATH 0 "${BOLT_SOURCE_DIR}/cmake/modules")
1122

23+
include(GNUInstallDirs)
24+
25+
# standalone build, copied from clang
26+
if(BOLT_BUILT_STANDALONE)
27+
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
28+
set(CMAKE_CXX_STANDARD_REQUIRED YES)
29+
set(CMAKE_CXX_EXTENSIONS NO)
30+
31+
if(NOT MSVC_IDE)
32+
set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
33+
CACHE BOOL "Enable assertions")
34+
# Assertions should follow llvm-config's.
35+
mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
36+
endif()
37+
38+
find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
39+
list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
40+
41+
set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
42+
find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
43+
NO_DEFAULT_PATH)
44+
45+
# They are used as destination of target generators.
46+
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
47+
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
48+
49+
include(AddLLVM)
50+
include(TableGen)
51+
include_directories(${LLVM_INCLUDE_DIRS})
52+
link_directories("${LLVM_LIBRARY_DIR}")
53+
54+
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_INSTALL_BINDIR}" )
55+
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_INSTALL_LIBDIR}/${LLVM_LIBDIR_SUFFIX}" )
56+
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_INSTALL_LIBDIR}/${LLVM_LIBDIR_SUFFIX}")
57+
endif() # standalone
58+
1259
# Determine default set of targets to build -- the intersection of
1360
# those BOLT supports and those LLVM is targeting.
1461
set(BOLT_TARGETS_TO_BUILD_all "AArch64;X86;RISCV")
@@ -94,6 +141,8 @@ if (BOLT_ENABLE_RUNTIME)
94141
if(CMAKE_SYSROOT)
95142
list(APPEND extra_args -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
96143
endif()
144+
145+
include(ExternalProject)
97146
ExternalProject_Add(bolt_rt
98147
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/runtime"
99148
STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-stamps
@@ -104,6 +153,7 @@ if (BOLT_ENABLE_RUNTIME)
104153
-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
105154
-DLLVM_LIBDIR_SUFFIX=${LLVM_LIBDIR_SUFFIX}
106155
-DLLVM_LIBRARY_DIR=${LLVM_LIBRARY_DIR}
156+
-DBOLT_BUILT_STANDALONE=${BOLT_BUILT_STANDALONE}
107157
${extra_args}
108158
INSTALL_COMMAND ""
109159
BUILD_ALWAYS True
@@ -113,6 +163,8 @@ if (BOLT_ENABLE_RUNTIME)
113163
add_llvm_install_targets(install-bolt_rt
114164
DEPENDS bolt_rt bolt
115165
COMPONENT bolt)
166+
set(LIBBOLT_RT_INSTR "${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-bins/lib/libbolt_rt_instr.a")
167+
set(LIBBOLT_RT_HUGIFY "${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-bins/lib/libbolt_rt_hugify.a")
116168
endif()
117169

118170
find_program(GNU_LD_EXECUTABLE NAMES ${LLVM_DEFAULT_TARGET_TRIPLE}-ld.bfd ld.bfd DOC "GNU ld")

bolt/include/bolt/RuntimeLibs/RuntimeLibrary.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ class RuntimeLibrary {
5858
uint64_t RuntimeFiniAddress{0};
5959
uint64_t RuntimeStartAddress{0};
6060

61-
/// Get the full path to a runtime library specified by \p LibFileName.
61+
/// Get the full path to a runtime library specified by \p LibFileName and \p
62+
/// ToolPath.
63+
static std::string getLibPathByToolPath(StringRef ToolPath,
64+
StringRef LibFileName);
65+
66+
/// Get the full path to a runtime library by the install directory.
67+
static std::string getLibPathByInstalled(StringRef LibFileName);
68+
69+
/// Gets the full path to a runtime library based on whether it exists
70+
/// in the install libdir or runtime libdir.
6271
static std::string getLibPath(StringRef ToolPath, StringRef LibFileName);
6372

6473
/// Load a static runtime library specified by \p LibPath.

bolt/lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
add_compile_definitions(CMAKE_INSTALL_FULL_LIBDIR="${CMAKE_INSTALL_FULL_LIBDIR}")
2+
13
add_subdirectory(Core)
24
add_subdirectory(Passes)
35
add_subdirectory(Profile)

bolt/lib/RuntimeLibs/HugifyRuntimeLibrary.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ cl::opt<bool>
3232
"(which is what --hot-text relies on)."),
3333
cl::cat(BoltOptCategory));
3434

35-
static cl::opt<std::string> RuntimeHugifyLib(
36-
"runtime-hugify-lib",
37-
cl::desc("specify file name of the runtime hugify library"),
38-
cl::init("libbolt_rt_hugify.a"), cl::cat(BoltOptCategory));
35+
static cl::opt<std::string>
36+
RuntimeHugifyLib("runtime-hugify-lib",
37+
cl::desc("specify path of the runtime hugify library"),
38+
cl::init("libbolt_rt_hugify.a"), cl::cat(BoltOptCategory));
3939

4040
} // namespace opts
4141

bolt/lib/RuntimeLibs/InstrumentationRuntimeLibrary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace opts {
2626

2727
cl::opt<std::string> RuntimeInstrumentationLib(
2828
"runtime-instrumentation-lib",
29-
cl::desc("specify file name of the runtime instrumentation library"),
29+
cl::desc("specify path of the runtime instrumentation library"),
3030
cl::init("libbolt_rt_instr.a"), cl::cat(BoltOptCategory));
3131

3232
extern cl::opt<bool> InstrumentationFileAppendPID;

bolt/lib/RuntimeLibs/RuntimeLibrary.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ using namespace bolt;
2626

2727
void RuntimeLibrary::anchor() {}
2828

29-
std::string RuntimeLibrary::getLibPath(StringRef ToolPath,
30-
StringRef LibFileName) {
29+
std::string RuntimeLibrary::getLibPathByToolPath(StringRef ToolPath,
30+
StringRef LibFileName) {
3131
StringRef Dir = llvm::sys::path::parent_path(ToolPath);
3232
SmallString<128> LibPath = llvm::sys::path::parent_path(Dir);
3333
llvm::sys::path::append(LibPath, "lib" LLVM_LIBDIR_SUFFIX);
@@ -38,13 +38,36 @@ std::string RuntimeLibrary::getLibPath(StringRef ToolPath,
3838
llvm::sys::path::append(LibPath, "lib" LLVM_LIBDIR_SUFFIX);
3939
}
4040
llvm::sys::path::append(LibPath, LibFileName);
41-
if (!llvm::sys::fs::exists(LibPath)) {
42-
errs() << "BOLT-ERROR: library not found: " << LibPath << "\n";
43-
exit(1);
44-
}
4541
return std::string(LibPath);
4642
}
4743

44+
std::string RuntimeLibrary::getLibPathByInstalled(StringRef LibFileName) {
45+
SmallString<128> LibPath(CMAKE_INSTALL_FULL_LIBDIR);
46+
llvm::sys::path::append(LibPath, LibFileName);
47+
return std::string(LibPath);
48+
}
49+
50+
std::string RuntimeLibrary::getLibPath(StringRef ToolPath,
51+
StringRef LibFileName) {
52+
if (llvm::sys::fs::exists(LibFileName)) {
53+
return std::string(LibFileName);
54+
}
55+
56+
std::string ByTool = getLibPathByToolPath(ToolPath, LibFileName);
57+
if (llvm::sys::fs::exists(ByTool)) {
58+
return ByTool;
59+
}
60+
61+
std::string ByInstalled = getLibPathByInstalled(LibFileName);
62+
if (llvm::sys::fs::exists(ByInstalled)) {
63+
return ByInstalled;
64+
}
65+
66+
errs() << "BOLT-ERROR: library not found: " << ByTool << ", " << ByInstalled
67+
<< ", or " << LibFileName << "\n";
68+
exit(1);
69+
}
70+
4871
void RuntimeLibrary::loadLibrary(StringRef LibPath, BOLTLinker &Linker,
4972
BOLTLinker::SectionsMapper MapSections) {
5073
ErrorOr<std::unique_ptr<MemoryBuffer>> MaybeBuf =

bolt/lib/Target/AArch64/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ set(LLVM_LINK_COMPONENTS
44
AArch64Desc
55
)
66

7+
if(BOLT_BUILT_STANDALONE)
8+
set(LLVM_TARGET_DEFINITIONS ${LLVM_MAIN_SRC_DIR}/lib/Target/AArch64/AArch64.td)
9+
list(APPEND LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_SRC_DIR}/lib/Target/AArch64)
10+
tablegen(LLVM AArch64GenInstrInfo.inc -gen-instr-info)
11+
tablegen(LLVM AArch64GenRegisterInfo.inc -gen-register-info)
12+
tablegen(LLVM AArch64GenSystemOperands.inc -gen-searchable-tables)
13+
tablegen(LLVM AArch64GenSubtargetInfo.inc -gen-subtarget)
14+
15+
add_public_tablegen_target(AArch64CommonTableGen)
16+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
17+
endif()
18+
719
add_llvm_library(LLVMBOLTTargetAArch64
820
AArch64MCPlusBuilder.cpp
921

bolt/lib/Target/RISCV/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ set(LLVM_LINK_COMPONENTS
44
RISCVDesc
55
)
66

7+
if(BOLT_BUILT_STANDALONE)
8+
# tablegen, copied from llvm/lib/Target/RISCV/CMakeLists.txt
9+
set(LLVM_TARGET_DEFINITIONS ${LLVM_MAIN_SRC_DIR}/lib/Target/RISCV/RISCV.td)
10+
list(APPEND LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_SRC_DIR}/lib/Target/RISCV)
11+
tablegen(LLVM RISCVGenInstrInfo.inc -gen-instr-info)
12+
tablegen(LLVM RISCVGenRegisterInfo.inc -gen-register-info)
13+
tablegen(LLVM RISCVGenSearchableTables.inc -gen-searchable-tables)
14+
tablegen(LLVM RISCVGenSubtargetInfo.inc -gen-subtarget)
15+
16+
add_public_tablegen_target(RISCVCommonTableGen)
17+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
18+
endif()
19+
720
add_llvm_library(LLVMBOLTTargetRISCV
821
RISCVMCPlusBuilder.cpp
922

bolt/lib/Target/X86/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ set(LLVM_LINK_COMPONENTS
55
X86Desc
66
)
77

8+
if(BOLT_BUILT_STANDALONE)
9+
set(LLVM_TARGET_DEFINITIONS ${LLVM_MAIN_SRC_DIR}/lib/Target/X86/X86.td)
10+
list(APPEND LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_SRC_DIR}/lib/Target/X86)
11+
tablegen(LLVM X86GenInstrInfo.inc -gen-instr-info -instr-info-expand-mi-operand-info=0)
12+
tablegen(LLVM X86GenMnemonicTables.inc -gen-x86-mnemonic-tables -asmwriternum=1)
13+
tablegen(LLVM X86GenRegisterInfo.inc -gen-register-info)
14+
tablegen(LLVM X86GenSubtargetInfo.inc -gen-subtarget)
15+
16+
add_public_tablegen_target(X86CommonTableGen)
17+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
18+
endif()
19+
820
add_llvm_library(LLVMBOLTTargetX86
921
X86MCPlusBuilder.cpp
1022
X86MCSymbolizer.cpp

bolt/lib/Utils/CMakeLists.txt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1+
find_first_existing_vc_file("${LLVM_MAIN_SRC_DIR}" llvm_vc)
2+
find_first_existing_vc_file("${BOLT_SOURCE_DIR}" bolt_vc)
3+
4+
# The VC revision include that we want to generate.
5+
set(version_inc "${CMAKE_CURRENT_BINARY_DIR}/VCSVersion.inc")
6+
7+
set(generate_vcs_version_script "${LLVM_CMAKE_DIR}/GenerateVersionFromVCS.cmake")
8+
9+
# Create custom target to generate the VC revision include.
10+
add_custom_command(OUTPUT "${version_inc}"
11+
DEPENDS "${llvm_vc}" "${bolt_vc}" "${generate_vcs_version_script}"
12+
COMMAND ${CMAKE_COMMAND} "-DNAMES=BOLT"
13+
"-DHEADER_FILE=${version_inc}"
14+
"-DBOLT_SOURCE_DIR=${BOLT_SOURCE_DIR}"
15+
"-DLLVM_VC_REPOSITORY=${llvm_vc_repository}"
16+
"-DLLVM_VC_REVISION=${llvm_vc_revision}"
17+
"-DLLVM_FORCE_VC_REVISION=${LLVM_FORCE_VC_REVISION}"
18+
"-DLLVM_FORCE_VC_REPOSITORY=${LLVM_FORCE_VC_REPOSITORY}"
19+
-P "${generate_vcs_version_script}")
20+
21+
# Mark the generated header as being generated.
22+
set_source_files_properties("${version_inc}"
23+
PROPERTIES GENERATED TRUE
24+
HEADER_FILE_ONLY TRUE)
25+
26+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
27+
128
add_llvm_library(LLVMBOLTUtils
229
CommandLineOpts.cpp
330
Utils.cpp
4-
31+
${version_inc}
532
DISABLE_LLVM_LINK_LLVM_DYLIB
633

734
LINK_LIBS
835
${LLVM_PTHREAD_LIB}
936

10-
DEPENDS
11-
llvm_vcsrevision_h
12-
1337
LINK_COMPONENTS
1438
Support
1539
)

0 commit comments

Comments
 (0)