Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ else()
endif()
option(BYN_ENABLE_LTO "Build with LTO" ${DEFAULT_LTO})

option(BYN_PGO_GENERATE "Build with IR PGO instrumentation" OFF)
set(BYN_PGO_PROFILE_USE_FILE "" CACHE FILEPATH "Profile data file for IR PGO (-fprofile-instr-use)")

# Turn this off to avoid the dependency on gtest.
option(BUILD_TESTS "Build GTest-based tests" ON)

Expand Down Expand Up @@ -272,6 +275,52 @@ if(BYN_ENABLE_LTO)
endif()
endif()

if(BYN_PGO_GENERATE AND BYN_PGO_PROFILE_USE_FILE)
message(FATAL_ERROR "BYN_PGO_GENERATE and BYN_PGO_PROFILE_USE_FILE cannot both be set")
endif()

if(BYN_PGO_GENERATE)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(FATAL_ERROR "PGO instrumentation is only supported by clang")
endif()
add_compile_flag("-fprofile-generate")
if(MSVC)
# On Windows with clang-cl, CMake invokes lld-link directly rather than
# linking through the compiler driver. clang-cl embeds a /DEFAULTLIB
# directive for clang_rt.profile-*.lib in each object file, so add clang's
# compiler-rt directory to the linker library search path.
# Note: COMMAND_ERROR_IS_FATAL requires CMake 3.19+.
execute_process(
COMMAND "${CMAKE_CXX_COMPILER}" /clang:-print-libgcc-file-name /clang:--rtlib=compiler-rt
OUTPUT_VARIABLE clang_compiler_rt_file
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY)
file(TO_CMAKE_PATH "${clang_compiler_rt_file}" clang_compiler_rt_file)
get_filename_component(clang_runtime_dir "${clang_compiler_rt_file}" DIRECTORY)
add_link_flag("/libpath:\"${clang_runtime_dir}\"")
else()
add_link_flag("-fprofile-generate")
endif()
endif()

if(BYN_PGO_PROFILE_USE_FILE)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(FATAL_ERROR "PGO profile use is only supported by clang")
endif()
if(NOT EXISTS "${BYN_PGO_PROFILE_USE_FILE}")
message(FATAL_ERROR "PGO profile file not found: ${BYN_PGO_PROFILE_USE_FILE}")
endif()
file(TO_CMAKE_PATH "${BYN_PGO_PROFILE_USE_FILE}" byn_pgo_profile_path)
add_compile_flag("-fprofile-instr-use=\"${byn_pgo_profile_path}\"")
# Multiple tools (e.g. wasm-as, wasm-reduce) define main(), which collides
# with wasm-opt's main() entry in the IR profile and triggers a harmless CFG
# hash-mismatch warning under -Wbackend-plugin (promoted by -Werror).
add_compile_flag("-Wno-backend-plugin")
if(NOT MSVC)
add_link_flag("-fprofile-instr-use=\"${byn_pgo_profile_path}\"")
endif()
endif()

if(MSVC)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# multi-core build.
Expand Down
Loading