Skip to content

Reduce CppInterOp Emscripten shared library size #655

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions .github/workflows/emscripten.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ jobs:
-DLLVM_ENABLE_LIBPFM=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0" \
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0" \
-DLLVM_ENABLE_LTO=Full \
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not sure, we necessarily need to change the way llvm is built for this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We absolutely need to change the llvm build to get this reduction in size of Emscripten shared library. The majority of the size reduction this PR provides comes from adding these flags to the llvm build.

../llvm
emmake ninja clang cling lld gtest_main
else
Expand Down Expand Up @@ -225,6 +228,9 @@ jobs:
-DCLANG_BUILD_TOOLS=OFF \
-G Ninja \
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0" \
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0" \
-DLLVM_ENABLE_LTO=Full \
../llvm
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
fi
Expand Down Expand Up @@ -295,6 +301,9 @@ jobs:
-DLLVM_BUILD_TOOLS=OFF `
-DLLVM_ENABLE_LIBPFM=OFF `
-DCLANG_BUILD_TOOLS=OFF `
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0" `
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0" `
-DLLVM_ENABLE_LTO=Full `
..\llvm
emmake make clang cling lld gtest_main
}
Expand Down Expand Up @@ -335,6 +344,9 @@ jobs:
-DLLVM_BUILD_TOOLS=OFF `
-DLLVM_ENABLE_LIBPFM=OFF `
-DCLANG_BUILD_TOOLS=OFF `
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0" `
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0" `
-DLLVM_ENABLE_LTO=Full `
-G Ninja `
..\llvm
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
Expand Down
6 changes: 6 additions & 0 deletions Emscripten-build-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_LIBPFM=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0" \
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0" \
-DLLVM_ENABLE_LTO=Full \
../llvm
emmake make libclang -j $(nproc --all)
emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
Expand Down Expand Up @@ -130,6 +133,9 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release `
-DLLVM_ENABLE_LIBPFM=OFF `
-DCLANG_BUILD_TOOLS=OFF `
-G Ninja `
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0" `
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0" `
-DLLVM_ENABLE_LTO=Full `
..\llvm
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
```
Expand Down
6 changes: 6 additions & 0 deletions docs/Emscripten-build-instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ and osx
-DLLVM_ENABLE_LIBPFM=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0" \
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0" \
-DLLVM_ENABLE_LTO=Full \
../llvm
emmake make libclang -j $(nproc --all)
emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
Expand Down Expand Up @@ -150,6 +153,9 @@ or executing
-DLLVM_ENABLE_LIBPFM=OFF `
-DCLANG_BUILD_TOOLS=OFF `
-G Ninja `
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0" `
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0" `
-DLLVM_ENABLE_LTO=Full `
..\llvm
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm

Expand Down
6 changes: 6 additions & 0 deletions lib/CppInterOp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,16 @@ if(EMSCRIPTEN)
set_target_properties(clangCppInterOp
PROPERTIES NO_SONAME 1
)
target_compile_options(clangCppInterOp
PRIVATE "SHELL: -Oz"
PRIVATE "SHELL: -flto"
)
target_link_options(clangCppInterOp
PRIVATE "SHELL: -s WASM_BIGINT"
PRIVATE "SHELL: -s SIDE_MODULE=1"
PRIVATE "SHELL: ${SYMBOLS_LIST}"
PRIVATE "SHELL: -Oz"
PRIVATE "SHELL: -flto"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm why is lto being involved here ? I've tried using it in the past and I remember it behaves notoriously.
How is it helping us here ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adding these flags gets us a further reduction in the size of the Emscripten shared library of a few Mb. Needed them for both compile and link flags for it to have any effect. I didn't see any bad side effects your comment suggests (I don't know exactly what you mean by behaves notoriously).

)
else()
target_link_options(clangCppInterOp
Expand Down
3 changes: 2 additions & 1 deletion lib/CppInterOp/exports.ld
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@
-Wl,--export=_ZNK5clang4Type14isFloatingTypeEv
-Wl,--export=_ZNK5clang12FunctionDecl12getNumParamsEv
-Wl,--export=__clang_Interpreter_SetValueNoAlloc
-Wl,--export=__clang_Interpreter_SetValueWithAlloc
-Wl,--export=__clang_Interpreter_SetValueWithAlloc
-Wl,--export=_ZN4llvm15SmallVectorBaseIjE8set_sizeEm
Loading