Skip to content

Commit

Permalink
[js] export default vis
Browse files Browse the repository at this point in the history
  • Loading branch information
makslevental committed Nov 20, 2024
1 parent a5045f7 commit 20c44a3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
21 changes: 12 additions & 9 deletions build_tools/build_llvm_wasi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ echo "*********************** BUILDING LLVM *********************************"
# hack to emit html wrappers
# https://stackoverflow.com/a/75596433/9045206
sed -i.bak 's/CMAKE_EXECUTABLE_SUFFIX ".js"/CMAKE_EXECUTABLE_SUFFIX ".html"/g' "$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"
sed -i.bak 's/if(LLVM_ENABLE_PIC)/if(LLVM_ENABLE_PIC)\nreturn()\nendif()\nif(LLVM_ENABLE_PIC)/g' "${LLVM_SOURCE_DIR}/mlir/lib/ExecutionEngine/CMakeLists.txt"

# all emscripten settings here https://github.com/emscripten-core/emscripten/blob/main/src/settings.js
# wasm-ld exports https://lld.llvm.org/WebAssembly.html#exports
# -sLINKABLE=1 combined with CMAKE_CXX_VISIBILITY_PRESET=default will cause all symbols to be exported
# -sEXPORT_ALL=1 put all of the exported function in the wasm on the JS module
cmake_options=(
-GNinja
-S "${LLVM_SOURCE_DIR}/llvm"
-B "${LLVM_BUILD_DIR}"
-DCMAKE_C_FLAGS="-Os"
-DCMAKE_CXX_FLAGS="-Os"
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_EXE_LINKER_FLAGS="--emit-symbol-map -sSTANDALONE_WASM=1 -sWASM=1 -sWASM_BIGINT=1 -sEXPORT_ALL=1 -sEXPORTED_RUNTIME_METHODS=cwrap,ccall,getValue,setValue,writeAsciiToMemory,wasmTable --minify 0 -lembind"
-DCMAKE_EXE_LINKER_FLAGS="--emit-symbol-map -sSTANDALONE_WASM=1 -sWASM=1 -sWASM_BIGINT=1 -sLINKABLE=1 -sEXPORT_ALL=1 -sEXPORTED_RUNTIME_METHODS=cwrap,ccall,getValue,setValue,writeAsciiToMemory,wasmTable --minify 0 -lembind"
-DCMAKE_INSTALL_PREFIX="${LLVM_INSTALL_DIR}"
-DCMAKE_SYSTEM_NAME=Emscripten
-DCMAKE_TOOLCHAIN_FILE="$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"
Expand All @@ -58,10 +59,12 @@ cmake --build "${LLVM_BUILD_DIR}" \
--target install-mlirdevelopment-distribution

sed -i.bak 's/CMAKE_EXECUTABLE_SUFFIX ".html"/CMAKE_EXECUTABLE_SUFFIX ".js"/g' "$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"
# https://stackoverflow.com/a/1252191/9045206
sed -i.bak -e ':a' -e 'N' -e '$!ba' -e 's/if(LLVM_ENABLE_PIC)\nreturn()\nendif()\n//g' "${LLVM_SOURCE_DIR}/mlir/lib/ExecutionEngine/CMakeLists.txt"


# wasi files aren't installed for some reason
cp "${LLVM_BUILD_DIR}"/bin/* "${LLVM_INSTALL_DIR}/bin"
cp "${LLVM_BUILD_DIR}"/lib/*.symbols "${LLVM_INSTALL_DIR}/lib"
cp "${LLVM_BUILD_DIR}"/bin/mlir-opt.* "${LLVM_INSTALL_DIR}/bin"
# cp "${LLVM_BUILD_DIR}"/lib/*.symbols "${LLVM_INSTALL_DIR}/lib"

# prevent symbol collisions
# sed -i.bak 's/if(LLVM_ENABLE_PIC)/if(LLVM_ENABLE_PIC)\nreturn()\nendif()\nif(LLVM_ENABLE_PIC)/g' "${LLVM_SOURCE_DIR}/mlir/lib/ExecutionEngine/CMakeLists.txt"
# https://stackoverflow.com/a/1252191/9045206
# sed -i.bak -e ':a' -e 'N' -e '$!ba' -e 's/if(LLVM_ENABLE_PIC)\nreturn()\nendif()\n//g' "${LLVM_SOURCE_DIR}/mlir/lib/ExecutionEngine/CMakeLists.txt"
14 changes: 7 additions & 7 deletions build_tools/cmake/llvm_wasm_cache.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ set(LLVM_ENABLE_LIBPFM OFF CACHE BOOL "")
set(LLVM_ENABLE_LIBXML2 OFF CACHE BOOL "")
set(LLVM_ENABLE_OCAMLDOC OFF CACHE BOOL "")

set(LLVM_BUILD_LLVM_DYLIB ON CACHE BOOL "")
set(MLIR_BUILD_MLIR_C_DYLIB ON CACHE BOOL "")
set(LLVM_BUILD_LLVM_DYLIB OFF CACHE BOOL "")
set(MLIR_BUILD_MLIR_C_DYLIB OFF CACHE BOOL "")
# when building libLLVM
# relocation R_WASM_MEMORY_ADDR_SLEB cannot be used against symbol
set(LLVM_ENABLE_PIC ON CACHE BOOL "")
set(LLVM_ENABLE_PIC OFF CACHE BOOL "")
set(MLIR_ENABLE_SPIRV_CPU_RUNNER OFF)
set(MLIR_ENABLE_EXECUTION_ENGINE OFF)

Expand All @@ -41,7 +41,7 @@ set(LLVM_MlirDevelopment_DISTRIBUTION_COMPONENTS
llvm-headers
llvm-libraries
cmake-exports
opt
# opt
# triggers LLVMMlirDevelopmentExports.cmake
mlirdevelopment-cmake-exports
# triggers MLIRMlirDevelopmentTargets.cmake
Expand All @@ -51,7 +51,7 @@ set(LLVM_MlirDevelopment_DISTRIBUTION_COMPONENTS
mlir-headers
mlir-libraries
mlir-opt
mlir-reduce
mlir-tblgen
mlir-translate
# mlir-reduce
# mlir-tblgen
# mlir-translate
CACHE STRING "")
22 changes: 16 additions & 6 deletions build_tools/cmake/llvm_wasm_project_include.cmake
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
# everything in here gets run at the end of the project loading
# https://github.com/emscripten-core/emscripten/issues/15276#issuecomment-1039349267
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
# so that the correct LLVM_ABI macros get set
# __ELF__ so that the correct LLVM_ABI macros get set
# https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md#function-arguments-and-return-values
# Similarly, types can either be returned directly from WebAssembly functions or returned indirectly via a pointer parameter prepended to the parameter list.
# https://github.com/llvm/llvm-project/blob/6d973b4548e281d0b8e75e85833804bb45b6a0e8/clang/lib/CodeGen/Targets/WebAssembly.cpp#L135
# https://github.com/llvm/llvm-project/commit/c285307e1457c4db2346443a4336e672d7487111#diff-b83bb889340990fea25762060e144b5cd4b4652a6fa737aaea9555d456344219
# https://github.com/llvm/llvm-project/blame/63534779b4ef1816e2961246011e2ec3be110d27/clang/lib/CodeGen/TargetInfo.cpp
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__ELF__")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__ELF__")
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-sSIDE_MODULE=1 -sEXPORT_ALL=1 --emit-symbol-map")
set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-sSIDE_MODULE=1 -sEXPORT_ALL=1 --emit-symbol-map")
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-sLINKABLE=1 -sEXPORT_ALL=1")
set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-sLINKABLE=1 -sEXPORT_ALL=1")
# prevent duplicated libs from being linked
# will need to be renamed to DEDUPLICATION when CMake version catches up
# https://github.com/Kitware/CMake/commit/5617c34c3135f7ec203d5a48b803eb323f458bc3#diff-17fc647759070cdaddd99e9ad994c4478860d9c301ed9a9a9f061a8825c8b690L20
set(CMAKE_C_LINK_LIBRARIES_PROCESSING ORDER=FORWARD UNICITY=ALL)
set(CMAKE_CXX_LINK_LIBRARIES_PROCESSING ORDER=FORWARD UNICITY=ALL)
# hack to prevent -Bsymbolic
set(LLVM_LINKER_IS_SOLARISLD_ILLUMOS ON)

## hack to prevent -Bsymbolic
#set(LLVM_LINKER_IS_SOLARISLD_ILLUMOS ON)

set(CMAKE_SHARED_LIBRARY_SUFFIX ".wasm")
set(CMAKE_STRIP FALSE)
set(CMAKE_STRIP FALSE)
set(LLVM_NO_DEAD_STRIP ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_VISIBILITY_PRESET default)

0 comments on commit 20c44a3

Please sign in to comment.