Skip to content

Commit

Permalink
tweaks to enable NativeAOT-LLVM on Linux with WASI-SDK 22
Browse files Browse the repository at this point in the history
Some of these changes are borrowed from
dotnet#2569.

Note that I had to manually copy pthread.h from the
wasi-sdk-22/share/wasi-sysroot/include/wasm32-wasi-threads directory to the
wasi-sdk-22/share/wasi-sysroot/include/wasm32-wasi directory as a workaround
until WebAssembly/wasi-libc#501 is addressed.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
  • Loading branch information
dicej committed May 23, 2024
1 parent 07ff833 commit 72682cb
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 26 deletions.
14 changes: 7 additions & 7 deletions eng/native/gen-buildsys.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ for i in "${@:6}"; do
esac
done

cmake_extra_defines=
cmake_extra_defines=()
if [[ "$CROSSCOMPILE" == "1" ]]; then
platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
# OSX doesn't use rootfs
Expand All @@ -71,17 +71,17 @@ if [[ "$CROSSCOMPILE" == "1" ]]; then
TARGET_BUILD_ARCH="$host_arch"
export TARGET_BUILD_ARCH

cmake_extra_defines="$cmake_extra_defines -C $scriptroot/tryrun.cmake"
cmake_extra_defines+=("-C" "$scriptroot/tryrun.cmake")

if [[ "$platform" == "darwin" ]]; then
cmake_extra_defines="$cmake_extra_defines -DCMAKE_SYSTEM_NAME=Darwin"
cmake_extra_defines+=("-DCMAKE_SYSTEM_NAME=Darwin")
else
cmake_extra_defines="$cmake_extra_defines -DCMAKE_TOOLCHAIN_FILE=$scriptroot/../common/cross/toolchain.cmake"
cmake_extra_defines+=("-DCMAKE_TOOLCHAIN_FILE=$scriptroot/../common/cross/toolchain.cmake")
fi
fi

if [[ "$host_arch" == "armel" ]]; then
cmake_extra_defines="$cmake_extra_defines -DARM_SOFTFP=1"
cmake_extra_defines+=("-DARM_SOFTFP=1")
fi

if ! cmake_command=$(command -v cmake); then
Expand All @@ -97,7 +97,7 @@ if [[ "$host_arch" == "wasm" ]]; then
if [[ "$target_os" == "browser" ]]; then
cmake_command="emcmake $cmake_command"
elif [[ "$target_os" == "wasi" ]]; then
true
cmake_extra_defines+=("-DCLR_CMAKE_TARGET_OS=wasi" "-DCLR_CMAKE_TARGET_ARCH=wasm" "-DWASI_SDK_PREFIX=$WASI_SDK_PATH" "-DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk.cmake" "-DCMAKE_CROSSCOMPILING_EMULATOR=node --experimental-wasm-bigint --experimental-wasi-unstable-preview1")
else
echo "target_os was not specified"
exit 1
Expand All @@ -109,7 +109,7 @@ $cmake_command \
-G "$generator" \
"-DCMAKE_BUILD_TYPE=$buildtype" \
"-DCMAKE_INSTALL_PREFIX=$__CMakeBinDir" \
$cmake_extra_defines \
"${cmake_extra_defines[@]}" \
$__UnprocessedCMakeArgs \
-S "$1" \
-B "$2"
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/build-runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ if [[ "$__CMakeTarget" == *"wasmjit"* ]]; then
echo The LLVM_CMAKE_CONFIG environment variable pointing to llvm-build-dir/lib/cmake/llvm must be set. 1>&2
exit 1
fi

export LLVM_CMAKE_CONFIG
fi

if [[ -z "$__CMakeTarget" ]]; then
Expand Down
30 changes: 19 additions & 11 deletions src/coreclr/jit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,24 @@ endif()
set(JIT_DLL_MAIN_FILE ${CMAKE_CURRENT_LIST_DIR}/dllmain.cpp)

if(CLR_CMAKE_TARGET_WIN32)
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.exports)
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClrJit.exports.def)
if(CLR_CMAKE_BUILD_LLVM_JIT)
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.Llvm.exports)
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClrJit.Llvm.exports.def)
else()
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.exports)
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClrJit.exports.def)
endif()
preprocess_file (${CLRJIT_EXPORTS} ${JIT_EXPORTS_FILE})

set(JIT_DEF_FILE ${JIT_EXPORTS_FILE})
else()
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.PAL.exports)

set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/clrjit.exports)
if(CLR_CMAKE_BUILD_LLVM_JIT)
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.Llvm.exports)
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClrJit.Llvm.exports.def)
else()
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.PAL.exports)
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/clrjit.exports)
endif()
generate_exports_file(${CLRJIT_EXPORTS} ${JIT_EXPORTS_FILE})

if(CLR_CMAKE_TARGET_LINUX OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_NETBSD OR CLR_CMAKE_TARGET_SUNOS)
Expand Down Expand Up @@ -706,16 +715,15 @@ else()
create_standalone_jit(TARGET clrjit_win_x86_${ARCH_HOST_NAME} OS win ARCH x86 DESTINATIONS .)
endif (CLR_CMAKE_TARGET_ARCH_RISCV64)

# Note that we currently do not support building the LLVM Jit on Unix.
# Note as well that we need this "CLR_CMAKE_BUILD_LLVM_JIT", defined by the build scripts,
# Note that we need this "CLR_CMAKE_BUILD_LLVM_JIT", defined by the build scripts,
# because there is no way in CMake to ask "what --target's' am I being asked to configure?".
if (CLR_CMAKE_BUILD_LLVM_JIT)
# The LLVM clrjit needs to be the last clrjit to use create_standalone_jit as it modifies some cmake variables.
# LLVM clrjit has an extra export - registerLlvmCallbacks.
set(CLRJIT_EXPORTS ${CMAKE_CURRENT_LIST_DIR}/ClrJit.Llvm.exports)
set(JIT_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/ClrJit.Llvm.exports.def)
preprocess_file (${CLRJIT_EXPORTS} ${JIT_EXPORTS_FILE})
set(JIT_DEF_FILE ${JIT_EXPORTS_FILE})
if (CLR_CMAKE_TARGET_LINUX)
add_compile_options(-Wno-switch)
add_compile_options(-Wno-format)
endif (CLR_CMAKE_TARGET_LINUX)

# Exclude cpp files that are not required when not processing beyond rationalized LIR.
# Use REGEX as this list contains the absolute paths.
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/jit/llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ CORINFO_GENERIC_HANDLE Llvm::getSymbolHandleForClassToken(mdToken token)
template <EEApiId Func, typename TReturn, typename... TArgs>
TReturn CallEEApi(TArgs... args)
{
return static_cast<TReturn (*)(TArgs...)>(g_callbacks[static_cast<int>(Func)])(args...);
return reinterpret_cast<TReturn (*)(TArgs...)>(g_callbacks[static_cast<int>(Func)])(args...);
}

const char* Llvm::GetMangledMethodName(CORINFO_METHOD_HANDLE methodHandle)
Expand Down Expand Up @@ -866,8 +866,8 @@ extern "C" DLLEXPORT void registerLlvmCallbacks(void** jitImports, void** jitExp
assert(jitExports != nullptr);

memcpy(g_callbacks, jitImports, static_cast<int>(EEApiId::Count) * sizeof(void*));
jitExports[static_cast<int>(JitApiId::StartSingleThreadedCompilation)] = &Llvm::StartSingleThreadedCompilation;
jitExports[static_cast<int>(JitApiId::FinishSingleThreadedCompilation)] = &Llvm::FinishSingleThreadedCompilation;
jitExports[static_cast<int>(JitApiId::StartSingleThreadedCompilation)] = (void*)&Llvm::StartSingleThreadedCompilation;
jitExports[static_cast<int>(JitApiId::FinishSingleThreadedCompilation)] = (void*)&Llvm::FinishSingleThreadedCompilation;
jitExports[static_cast<int>(JitApiId::Count)] = (void*)0x1234;
}

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/llvmcodegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,7 @@ void Llvm::visitNode(GenTree* node)
break;
case GT_JMP:
NYI("LLVM/GT_JMP"); // Requires support for explicit tailcalls.
LLVM_FALLTHROUGH;
default:
unreached();
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/llvmlssa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ class ShadowStackAllocator
{
if (block != m_currentBlock)
{
m_actions.Push({AllocationActionKind::Block, m_currentBlockIndex++});
m_actions.Push({AllocationActionKind::Block, {m_currentBlockIndex++}});
m_currentBlock = block;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ The .NET Foundation licenses this file to you under the MIT license.

<Target Name="CheckWasmSdks">
<Error Text="Emscripten not found, not compiling to WebAssembly. To enable WebAssembly compilation, install Emscripten and ensure the EMSDK environment variable points to the directory containing upstream/emscripten/emcc.bat"
Condition="'$(EMSDK)' == ''" />
Condition="'$(EMSDK)' == '' and '$(_targetOS)' == 'browser'" />
<Error Text="Wasi SDK not found, not compiling to WebAssembly. To enable WebAssembly compilation, install Wasi SDK and ensure the WASI_SDK_PATH environment variable points to the directory containing share/wasi-sysroot"
Condition="'$(WASI_SDK_PATH)' == '' and '$(_targetOS)' == 'wasi'" />
</Target>
Expand Down Expand Up @@ -420,8 +420,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<CompileWasmArgs>$(CompileWasmArgs) -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -disable-lsr --sysroot=&quot;$(WASI_SDK_PATH)/share/wasi-sysroot&quot; -target $(IlcLlvmTarget)</CompileWasmArgs>

<ExeExt Condition="'$(OS)' == 'Windows_NT'">.exe</ExeExt>
<!-- using Emscripten's clang++ because of a crash in wasi-sdk's clang++ (https://github.com/WebAssembly/wasi-sdk/issues/326) -->
<WasmCompilerPath>&quot;$(EMSDK)/upstream/bin/clang++$(ExeExt)&quot;</WasmCompilerPath>
<WasmCompilerPath>&quot;$(WASI_SDK_PATH)/bin/clang++$(ExeExt)&quot;</WasmCompilerPath>
<WasmLinkerPath>&quot;$(WASI_SDK_PATH)/bin/clang&quot;</WasmLinkerPath>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/native/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER OR CLR_CMAKE_TARGET_WASI)
endif ()

set(CMAKE_INSTALL_PREFIX $ENV{__CMakeBinDir})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wno-declaration-after-statement")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wno-declaration-after-statement -Wno-switch-default")

add_compile_options(-I${CMAKE_CURRENT_SOURCE_DIR}/Common)
add_compile_options(-I${CMAKE_CURRENT_BINARY_DIR}/Common)
Expand Down
2 changes: 2 additions & 0 deletions src/native/libs/System.Native/pal_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
#elif HAVE_SYS_STATVFS_H && !HAVE_NON_LEGACY_STATFS // SunOS
#include <sys/types.h>
#include <sys/statvfs.h>
#ifndef TARGET_WASI
#include <sys/vfs.h>
#endif
#endif

#ifdef _AIX
#include <alloca.h>
Expand Down

0 comments on commit 72682cb

Please sign in to comment.