Skip to content
Merged
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
51 changes: 51 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,54 @@ 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+.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update our minimum required cmake version accordingly? Right now it is 3.16.3. Alternatively, we could more manually check return codes (like we do with git rev-parse above) without requiring a newer cmake.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes actually i was going to file a bug about that today. LLVM is currently requiring 3.20, and starting with LLVM 24 it will be 3.31. It might make sense for us to follow that, as LLVM generally has pretty conservative build requirements. Gemini also noted that mimalloc and fuzztest have higher requirements than Binaryen itself (although those are of course optional). It suggested some minor improvements we could make if we went to 3.20 and several more simplifications we could make if we went to 3.31.
Looking at several LTS versions, it looks like the oldest default CMake from the selection is Ubuntu Jammy with 3.22.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, LGTM to land this as-is while we discuss which version to update to, then.

execute_process(
COMMAND "${CMAKE_CXX_COMPILER}" -print-runtime-dir
OUTPUT_VARIABLE clang_runtime_dir
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY)
file(TO_CMAKE_PATH "${clang_runtime_dir}" clang_runtime_dir)
if(NOT IS_DIRECTORY "${clang_runtime_dir}")
message(FATAL_ERROR "Unable to find clang compiler-rt directory: ${clang_runtime_dir}")
endif()
add_link_flag("/libpath:\"${clang_runtime_dir}\"")
Comment thread
dschuff marked this conversation as resolved.
Resolved
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