Skip to content

[CMake] Add BYN_PGO_GENERATE and BYN_PGO_PROFILE_USE_FILE options - #9420

Merged
dschuff merged 3 commits into
WebAssembly:mainfrom
dschuff:byn-pgo-cmake
Nov 24, 2026
Merged

dschuff merged 3 commits into
WebAssembly:mainfrom
dschuff:byn-pgo-cmake

Conversation

@dschuff

@dschuff dschuff commented Nov 22, 2026

Copy link
Copy Markdown
Member

Add CMake options for 2-phase IR Profile-Guided Optimization (PGO)
builds with Clang:

  • BYN_PGO_GENERATE: Adds -fprofile-generate to compile flags. On
    Windows with clang-cl (MSVC), CMake invokes lld-link directly
    rather than linking through the compiler driver, so query clang-cl
    for its compiler-rt directory and pass /libpath:"<dir>" so
    lld-link resolves the embedded clang_rt.profile-*.lib directive;
    on other platforms, pass -fprofile-generate to the linker driver.
  • BYN_PGO_PROFILE_FILE: Adds -fprofile-instr-use="<path>" and
    -Wno-backend-plugin (to avoid harmless main() CFG hash-mismatch
    warnings across tools other than wasm-opt when -Werror is enabled)
    to compile flags, and passes -fprofile-instr-use="<path>" to link
    flags on non-MSVC platforms.

Add CMake options for 2-phase IR Profile-Guided Optimization (PGO)
builds with Clang:

- `BYN_PGO_GENERATE`: Adds `-fprofile-generate` to compile flags. On
  Windows with `clang-cl` (`MSVC`), CMake invokes `lld-link` directly
  rather than linking through the compiler driver, so query `clang-cl`
  for its compiler-rt directory and pass `/libpath:"<dir>"` so
  `lld-link` resolves the embedded `clang_rt.profile-*.lib` directive;
  on other platforms, pass `-fprofile-generate` to the linker driver.
- `BYN_PGO_PROFILE_FILE`: Adds `-fprofile-instr-use="<path>"` and
  `-Wno-backend-plugin` (to avoid harmless `main()` CFG hash-mismatch
  warnings across tools other than `wasm-opt` when `-Werror` is enabled)
  to compile flags, and passes `-fprofile-instr-use="<path>"` to link
  flags on non-MSVC platforms.
@github-actions
github-actions Bot requested a review from tlively November 22, 2026 00:03
@dschuff

dschuff commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

We'll use this for the emscripten-releases release-optimized builds. So... maybe we can get away without CI coverage for this on the Binaryen side?

@tlively tlively left a comment

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.

BYN_PGO_PROFILE_FILE => BYN_PGO_PROFILE_USE_FILE in the title.

Comment thread CMakeLists.txt
# 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.

Comment thread CMakeLists.txt Resolved
@dschuff dschuff changed the title [CMake] Add BYN_PGO_GENERATE and BYN_PGO_PROFILE_FILE options [CMake] Add BYN_PGO_GENERATE and BYN_PGO_PROFILE_USE_FILE options Nov 24, 2026
@dschuff

dschuff commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

OK, review comments addressed.
Also adding @kripken since @tlively might already be out.

@dschuff
dschuff requested a review from kripken November 24, 2026 00:35

@kripken kripken left a comment

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.

Maybe worth adding a test at some point, though if this is still in flux it can wait?

@dschuff

dschuff commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

The plan is that this will be covered by our release build on the emscripten-releases side. We already have some flags e.g. BUILD_SHARED_LIBS that are only tested there and not on CI here; I think the risk is low since breakage won't go undiscovered for long.

@dschuff
dschuff enabled auto-merge (squash) November 24, 2026 01:07
@dschuff
dschuff merged commit 4a5cce1 into WebAssembly:main Nov 24, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants