Skip to content

Commit

Permalink
threads: support for pthread apis
Browse files Browse the repository at this point in the history
  • Loading branch information
csegarragonz committed Feb 16, 2024
1 parent d69921e commit 47be01e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 29 deletions.
8 changes: 6 additions & 2 deletions LLVM.makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ very-clean-libc: clean-libc

.PHONY: clean-libs
clean-libs: clean-libc
# compiler-rt clean
rm -rf $(BUILD_DIR)/compiler-rt $(BUILD_DIR)/compiler-rt.BUILT
# libcxx clean
rm -rf $(BUILD_DIR)/libcxx $(BUILD_DIR)/libcxx.BUILT
rm -rf $(FAASM_SYSROOT)/include/wasm32-wasi/c++
rm -rf $(FAASM_SYSROOT)/include/wasm32-wasi-threads/c++

.PHONY: clean-all
clean-all:
Expand Down Expand Up @@ -185,8 +189,8 @@ $(BUILD_DIR)/libcxx.BUILT: $(BUILD_DIR)/llvm.BUILT ${BUILD_DIR}/wasi-libc.BUILT
mkdir -p $(BUILD_DIR)/libcxx-threads
cd $(BUILD_DIR)/libcxx-threads && cmake -G Ninja $(call LIBCXX_CMAKE_FLAGS,ON,OFF) \
-DCMAKE_SYSROOT=${FAASM_SYSROOT} \
-DCMAKE_C_FLAGS="--target=wasm32-wasi-threads" \
-DCMAKE_CXX_FLAGS="--target=wasm32-wasi-threads" \
-DCMAKE_C_FLAGS="-pthread --target=wasm32-wasi-threads" \
-DCMAKE_CXX_FLAGS="-pthread --target=wasm32-wasi-threads" \
-DLIBCXX_LIBDIR_SUFFIX=/wasm32-wasi-threads \
-DLIBCXXABI_LIBDIR_SUFFIX=/wasm32-wasi-threads \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
Expand Down
26 changes: 13 additions & 13 deletions WasiToolchain.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
cmake_minimum_required(VERSION 3.5.0)
# project("Faasm WASM compilation toolchain" C CXX)

# -----------------------------------------
# A useful reference for this file is the wasi-sdk equivalent:
Expand All @@ -19,19 +18,20 @@ set(UNIX 1)
set(CMAKE_SYSTEM_NAME WASI)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR wasm32)
set(WASM_TRIPLE $ENV{FAASM_WASM_TRIPLE})
set(WASM_TRIPLE $ENV{FAASM_WASM_TRIPLE})

set(WASI_HOST_EXE_SUFFIX "")

if(FAASM_BUILD_SHARED)
message(STATUS "Faasm building SHARED libraries")

# See note in README about shared libraries targeting Emscripten
# TODO: what happens with shared lirbaries now?
set(WASM_TRIPLE $ENV{FAASM_WASM_HOST_SHARED})
else()
message(STATUS "Faasm building STATIC libraries")

set(WASM_TRIPLE $ENV{FAASM_WASM_HOST_STATIC})
# set(WASM_TRIPLE $ENV{WASM_TRIPLE})
endif()

# Specify LLVM toolchain
Expand All @@ -41,10 +41,10 @@ set(CMAKE_ASM_COMPILER $ENV{FAASM_WASM_CC})
set(CMAKE_AR $ENV{FAASM_WASM_AR} CACHE STRING "faasm build")
set(CMAKE_NM $ENV{FAASM_WASM_NM} CACHE STRING "faasm build")
set(CMAKE_RANLIB $ENV{FAASM_WASM_RANLIB} CACHE STRING "faasm build")
set(CMAKE_C_COMPILER_TARGET ${WASM_TRIPLE} CACHE STRING "faasm build")
set(CMAKE_CXX_COMPILER_TARGET ${WASM_TRIPLE} CACHE STRING "faasm build")
set(CMAKE_ASM_COMPILER_TARGET ${WASM_TRIPLE} CACHE STRING "faasm build")
message(STATUS "Faasm building target ${CMAKE_CXX_COMPILER_TARGET}")
set(CMAKE_C_COMPILER_TARGET $ENV{WASM_TRIPLE} CACHE STRING "faasm build")
set(CMAKE_CXX_COMPILER_TARGET $ENV{WASM_TRIPLE} CACHE STRING "faasm build")
set(CMAKE_ASM_COMPILER_TARGET $ENV{WASM_TRIPLE} CACHE STRING "faasm build")
message(STATUS "Faasm building target $ENV{CMAKE_CXX_COMPILER_TARGET}")

# We define dynamic linking functions in Faasm
unset(CMAKE_DL_LIBS CACHE)
Expand All @@ -56,12 +56,12 @@ if(FAASM_BUILD_SHARED)
set(FAASM_COMPILER_FLAGS "$ENV{FAASM_WASM_CFLAGS} $ENV{FAASM_WASM_CFLAGS_SHARED}")
endif()

set(CMAKE_SYSROOT ${FAASM_SYSROOT} CACHE STRING "faasm build")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{FAASM_WASM_CFLAGS}" CACHE STRING "faasm build")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{FAASM_WASM_CXXFLAGS}" CACHE STRING "faasm build")
set(CMAKE_LINKER_FLAGS $ENV{FAASM_WASM_LINKER_FLAGS} CACHE STRING "faasm build")
set(CMAKE_SHARED_LINKER_FLAGS $ENV{FAASM_WASM_SHARED_LINKER_FLAGS} CACHE STRING "faasm build")
set(CMAKE_EXE_LINKER_FLAGS $ENV{FAASM_WASM_EXE_LINKER_FLAGS} CACHE STRING "faasm build")
set(CMAKE_SYSROOT $ENV{FAASM_SYSROOT} CACHE STRING "faasm build")
set(CMAKE_C_FLAGS "$ENV{CMAKE_C_FLAGS} $ENV{FAASM_WASM_CFLAGS}" CACHE STRING "faasm build")
set(CMAKE_CXX_FLAGS "$ENV{CMAKE_CXX_FLAGS} $ENV{FAASM_WASM_CXXFLAGS}" CACHE STRING "faasm build")
set(CMAKE_LINKER_FLAGS "$ENV{FAASM_WASM_LINKER_FLAGS}" CACHE STRING "faasm build")
set(CMAKE_SHARED_LINKER_FLAGS "$ENV{FAASM_WASM_SHARED_LINKER_FLAGS}" CACHE STRING "faasm build")
set(CMAKE_EXE_LINKER_FLAGS "$ENV{FAASM_WASM_EXE_LINKER_FLAGS}" CACHE STRING "faasm build")

# This is important to ensure the right search path
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
Expand Down
18 changes: 13 additions & 5 deletions docker/cpp-sysroot.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,29 @@ RUN cd /code/cpp \
libfaasmpi --native --shared \
# Install toolchain files
&& inv install \
# Temporarily using llvm --clean as we debug new versions of the toolchain
# to avoid having to re-build the base image
# TODO: remove me
&& inv llvm --clean \
# Build wasi-libc and reset the sysroot. The second call to LLVM just
# installs some headers that are purged
&& inv llvm.libc --purge llvm \
# && inv llvm.libc --purge llvm \
# Build Faasm WASM libraries
&& inv \
libemscripten \
libemscripten --threads \
libfaasm \
libfaasm --threads \
libfaasmp \
libfaasmpi
# # Lastly, build the libraries that populate the sysroot
# && inv \
# Lastly, build the libraries that populate the sysroot
&& inv \
libffi \
libffi --threads \
zlib \
zlib --threads
# clapack \
# clapack --clean --shared \
# libffi \
# zlib

# CLI setup
WORKDIR /code/cpp
Expand Down
15 changes: 9 additions & 6 deletions faasmtools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,15 @@ def get_faasm_build_env_dict(is_threads=False):
"sign-ext",
"simd128",
]
build_env_dicts[
"FAASM_WASM_EXE_LINKER_FLAGS"
] += " -Wl,--import-memory"
build_env_dicts[
"FAASM_WASM_EXE_LINKER_FLAGS"
] += " -Wl,--export-memory"
# TODO: it seems that this import/export here are a requirement of
# the wasi-threads spec (see wasi-sdk/wasi-sdk-pthread.cmake). It
# is still not clear if we need it or not.
# build_env_dicts[
# "FAASM_WASM_EXE_LINKER_FLAGS"
# ] += " -Wl,--import-memory"
# build_env_dicts[
# "FAASM_WASM_EXE_LINKER_FLAGS"
# ] += " -Wl,--export-memory"
else:
wasm_triple = "wasm32-wasi"
build_env_dicts["FAASM_WASM_TRIPLE"] = wasm_triple
Expand Down
12 changes: 12 additions & 0 deletions libfaasmp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ set(LIB_FILES
if (CMAKE_SYSTEM_NAME STREQUAL "WASI")
message(STATUS "Libfaasmp WebAssembly build")

set(LLVM_OMP_SRC_DIR "/opt/llvm-project/openmp/runtime/src")
set(LLVM_OMP_BUILD_SRC_DIR "/opt/llvm-project/build/llvm/projects/openmp/runtime/src")
# set(LIBOMP_ASMFILES "${LLVM_OMP_SRC_DIR}/z_Linux_asm.S")
set(LIBOMP_ASMFILES "z_Linux_asm.S")
set(LIBOMP_HEADERS "${LLVM_OMP_BUILD_SRC_DIR}/omp.h")
set_source_files_properties(${LIBOMP_ASMFILES} PROPERTIES LANGUAGE C)
set(LIB_FILES ${LIBOMP_ASMFILES} ${LIB_FILES})

add_library(faasmp STATIC ${LIB_FILES})
set_target_properties(faasmp PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}")
# target_include_directories(faasmp PUBLIC ${LLVM_OMP_BUILD_SRC_DIR} ${LLVM_OMP_SRC_DIR})

# Install in WASM sysroot
install(
Expand All @@ -37,6 +42,13 @@ if (CMAKE_SYSTEM_NAME STREQUAL "WASI")
DESTINATION $ENV{FAASM_WASM_LIB_INSTALL_DIR}
RENAME libfaasmp.imports
)

# Add omp.h header copied from clang source
# TODO: can we copy it directly from LLVM source, for each LLVM version?
install(
FILES ${LIBOMP_HEADERS}
DESTINATION $ENV{FAASM_WASM_HEADER_INSTALL_DIR}
)
else ()
message(STATUS "Libfaasmp native build")

Expand Down
6 changes: 3 additions & 3 deletions libfaasmp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ and the Faasm host interface.
LLVM adds GNU compatibility data symbols on some features for synchronisation.
The wasm linker needs to be aware of the sizes and the names of the symbols,
which are not valid C/C++ identifiers, and as such are resolved in assembly in
`libfaasmp/z_Linux_asm.S` file.
`libfaasmp/z_Linux_asm.S` file.

An error from the linker when compiling new parts of the API implies you need to
you need to add the symbol to this file.
An error from the linker when compiling new parts of the API implies you need
to add the symbol to this file.

0 comments on commit 47be01e

Please sign in to comment.