-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Implement native clang launcher #27401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sbc100
wants to merge
2
commits into
emscripten-core:main
Choose a base branch
from
sbc100:native_clang_impl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| cmake_minimum_required(VERSION 3.20) | ||
| project(emcc_native CXX) | ||
|
|
||
| if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) | ||
| set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/../.." CACHE PATH "Default install prefix to Emscripten root" FORCE) | ||
| endif() | ||
|
|
||
| set(CMAKE_CXX_STANDARD 20) | ||
| set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
| set(CMAKE_CXX_EXTENSIONS OFF) | ||
|
|
||
| if(MSVC) | ||
| add_compile_options(/W4 /WX) | ||
| add_compile_definitions(_CRT_SECURE_NO_WARNINGS) | ||
| else() | ||
| add_compile_options(-Wall -Wextra -Werror) | ||
| endif() | ||
|
|
||
| enable_testing() | ||
|
|
||
| add_library(native_launcher_lib OBJECT | ||
| config.cpp | ||
| driver.cpp | ||
| exec.cpp | ||
| ) | ||
|
|
||
| include_directories(${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
|
||
| # Build `emcc` native launcher | ||
| add_executable(emcc main.cpp) | ||
| target_link_libraries(emcc PRIVATE native_launcher_lib) | ||
|
|
||
| # Create `em++` executable (symlink on Unix, copy on Windows) | ||
| if(WIN32) | ||
| set(CREATE_EMXX_COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:emcc> $<TARGET_FILE_DIR:emcc>/em++$<TARGET_FILE_SUFFIX:emcc>) | ||
| else() | ||
| set(CREATE_EMXX_COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE_NAME:emcc> $<TARGET_FILE_DIR:emcc>/em++$<TARGET_FILE_SUFFIX:emcc>) | ||
| endif() | ||
|
|
||
| add_custom_command( | ||
| TARGET emcc POST_BUILD | ||
| COMMAND ${CREATE_EMXX_COMMAND} | ||
| COMMENT "Creating em++ launcher executable" | ||
| ) | ||
|
|
||
| # Build unit tests | ||
| add_executable(native_tests tests/test_native.cpp) | ||
| target_link_libraries(native_tests PRIVATE native_launcher_lib) | ||
|
|
||
| add_test(NAME native_tests COMMAND native_tests) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should verify that even when CMake is configured in release mode, that the tests don't get compiled with -DNDEBUG because they are using raw |
||
|
|
||
| install(TARGETS emcc DESTINATION bin) | ||
| install(PROGRAMS $<TARGET_FILE_DIR:emcc>/em++$<TARGET_FILE_SUFFIX:emcc> DESTINATION bin) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Native Clang Frontend Launcher (`emcc_native`) | ||
|
|
||
| `emcc_native` is a high-performance C++ launcher for Emscripten's compiler | ||
| drivers (`emcc` and `em++`). It drastically reduces compiler startup latency for | ||
| compile-only invocations (`-c`, `-S`, `-E`, `-M`, `-MM`) by bypassing Python and directly | ||
| executing `clang` / `clang++`. | ||
|
|
||
| ## Overview & Architecture | ||
|
|
||
| When running large CMake or Ninja builds, `emcc` is launched hundreds or | ||
| thousands of times. Executing the Python interpreter for each single compile | ||
| unit adds overhead (especially on Windows). | ||
|
|
||
| `emcc_native` provides native executables (`bin/emcc`, `bin/em++`) that: | ||
| 1. **Directly invoke Clang** for pure compilation steps (`-c`, `-S`, `-E`, `-M`, `-MM`), injecting: | ||
| - Target triple (`-target wasm32-unknown-emscripten` or `wasm64-unknown-emscripten`) | ||
| - Frontend exceptions flag (`-fignore-exceptions`) | ||
| - Default LLVM backend flags (e.g. `-mllvm -enable-emscripten-sjlj`) | ||
| - Emscripten sysroot (`--sysroot=<cache>/sysroot`) | ||
| - Clang sysroot include paths (e.g. `-Xclang -iwithsysroot/include/compat`) | ||
| - SIMD/SSE/NEON preprocessor macros (`-D__SSE__=1`, `-D__SSE2__=1`, | ||
| `-D__ARM_NEON__=1`, etc.) when architecture flags are specified | ||
| - Visibility flag (`-fvisibility=default` when `-fPIC` is passed without | ||
| `-fvisibility`) | ||
| 2. **Ignore compile-unused linker flags**: Link-only flags (`--js-library`, | ||
| `--embed-file`, etc.) and linker settings (`-sEXPORTED_FUNCTIONS`, etc.) are | ||
| ignored during compilation (with diagnostic warnings matching `emcc.py`), | ||
| allowing compile steps with link flags to run natively. | ||
| 3. **Fall back to Python** (`emcc.py` / `em++.py`) when link-phase invocations | ||
| are run, or when compile-time `-s` settings or system flags (`--clear-cache`, | ||
| `--build`, `--tracing`, etc.) are present. | ||
|
|
||
| ## Building | ||
|
|
||
| Building requires CMake 3.20+ and a C++20 compiler. | ||
|
|
||
| ```bash | ||
| cmake -B out/build_emcc_native -S tools/emcc_native | ||
| cmake --build out/build_emcc_native | ||
| cmake --install out/build_emcc_native | ||
| ``` | ||
|
|
||
| The output executables (`emcc`, `em++`) will be installed in `./bin`. | ||
|
|
||
| ## Running Tests | ||
|
|
||
| To run the unit and integration tests: | ||
|
|
||
| ```bash | ||
| ctest --test-dir out/build_emcc_native --output-on-failure | ||
| ``` | ||
|
|
||
| ## Code Generation | ||
|
|
||
| Compile-time settings, link-only flags, and Emscripten warning options are | ||
| generated in `generated_settings.h`. To update this header from Python | ||
| definitions, run: | ||
|
|
||
| ```bash | ||
| ./tools/emcc_native/gen_settings.py | ||
| ``` | ||
|
|
||
| To verify whether `generated_settings.h` is up to date: | ||
|
|
||
| ```bash | ||
| ./tools/emcc_native/gen_settings.py --check | ||
| ``` | ||
|
|
||
| ## Configuration & Environment Variables | ||
|
|
||
| - `EMCC_NATIVE`: | ||
| - Set to `0` to disable the native driver and unconditionally fall back to `emcc.py`. | ||
| - Set to `1` to force strict native mode; if an invocation requires falling back to Python, `emcc_native` will print the fallback reason and exit with an error (useful for debugging). | ||
| - `EMCC_DEBUG`: When set (e.g. `EMCC_DEBUG=1`), logs launcher decision details (whether direct Clang execution or Python fallback was selected, reason, target binary, and command arguments). | ||
| - `EMSDK_PYTHON`: Path to the Python executable (defaults to `python3` or `python.exe` on Windows). | ||
| - `EM_CACHE`: Path to Emscripten cache directory (defaults to `<emscripten_root>/cache`). | ||
| - `EM_CONFIG`: Path to `.emscripten` configuration file (reads `LLVM_ROOT` and `CACHE`). | ||
| - `EM_LLVM_ROOT`: Environment variable override for the directory containing LLVM binaries (`clang`, `clang++`). | ||
|
|
||
| ## CI Benchmark Results | ||
|
|
||
| Compile-time performance is automatically benchmarked on CI across Linux, | ||
| macOS, and Windows (`embuilder build libc --force` compiling 1,075 files | ||
| sequentially with `EMCC_CORES=1`, `EMCC_USE_NINJA=0`, and | ||
| `EMCC_BATCH_BUILD=0`). | ||
|
|
||
| | Platform | Before (Python Baseline) | After (Native Launcher) | Improvement | Speedup | | ||
| | :---------: | :----------------------: | :---------------------: | :------------------------: | :-------: | | ||
| | **Linux** | 181.96 s (169.3 ms/file) | 64.82 s (60.3 ms/file) | -117.14 s (-109.0 ms/file) | **2.81x** | | ||
| | **Windows** | 343.90 s (319.9 ms/file) | 105.12 s (97.8 ms/file) | -238.78 s (-222.1 ms/file) | **3.27x** | | ||
| | **macOS** | 162.08 s (150.6 ms/file) | 64.73 s (60.1 ms/file) | -97.35 s (-90.5 ms/file) | **2.50x** | | ||
|
|
||
| As expected, because process creation and `python.exe` startup carry | ||
| significantly higher overhead on Windows than on POSIX systems, the speedup on | ||
| Windows CI (**3.27x**, saving over 222 ms per invocation) is even larger than on | ||
| Linux and macOS. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code seems unrelated, or am I not seeing it?