From 72682cb229b15ce63766730536c70a3e01e792b9 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 22 May 2024 14:10:38 -0600 Subject: [PATCH] tweaks to enable NativeAOT-LLVM on Linux with WASI-SDK 22 Some of these changes are borrowed from https://github.com/dotnet/runtimelab/pull/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 https://github.com/WebAssembly/wasi-libc/issues/501 is addressed. Signed-off-by: Joel Dice --- eng/native/gen-buildsys.sh | 14 ++++----- src/coreclr/build-runtime.sh | 2 ++ src/coreclr/jit/CMakeLists.txt | 30 ++++++++++++------- src/coreclr/jit/llvm.cpp | 6 ++-- src/coreclr/jit/llvmcodegen.cpp | 1 + src/coreclr/jit/llvmlssa.cpp | 2 +- .../Microsoft.NETCore.Native.targets | 5 ++-- src/native/libs/CMakeLists.txt | 2 +- src/native/libs/System.Native/pal_io.c | 2 ++ 9 files changed, 38 insertions(+), 26 deletions(-) diff --git a/eng/native/gen-buildsys.sh b/eng/native/gen-buildsys.sh index 4c1ed0943c45..9d67e1819286 100755 --- a/eng/native/gen-buildsys.sh +++ b/eng/native/gen-buildsys.sh @@ -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 @@ -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 @@ -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 @@ -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" diff --git a/src/coreclr/build-runtime.sh b/src/coreclr/build-runtime.sh index 2d0685cd4399..e83b0540b81b 100755 --- a/src/coreclr/build-runtime.sh +++ b/src/coreclr/build-runtime.sh @@ -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 diff --git a/src/coreclr/jit/CMakeLists.txt b/src/coreclr/jit/CMakeLists.txt index 67848a9cc01d..c09e4c549edb 100644 --- a/src/coreclr/jit/CMakeLists.txt +++ b/src/coreclr/jit/CMakeLists.txt @@ -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) @@ -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. diff --git a/src/coreclr/jit/llvm.cpp b/src/coreclr/jit/llvm.cpp index 8175b61915e8..c33f36a4927f 100644 --- a/src/coreclr/jit/llvm.cpp +++ b/src/coreclr/jit/llvm.cpp @@ -770,7 +770,7 @@ CORINFO_GENERIC_HANDLE Llvm::getSymbolHandleForClassToken(mdToken token) template TReturn CallEEApi(TArgs... args) { - return static_cast(g_callbacks[static_cast(Func)])(args...); + return reinterpret_cast(g_callbacks[static_cast(Func)])(args...); } const char* Llvm::GetMangledMethodName(CORINFO_METHOD_HANDLE methodHandle) @@ -866,8 +866,8 @@ extern "C" DLLEXPORT void registerLlvmCallbacks(void** jitImports, void** jitExp assert(jitExports != nullptr); memcpy(g_callbacks, jitImports, static_cast(EEApiId::Count) * sizeof(void*)); - jitExports[static_cast(JitApiId::StartSingleThreadedCompilation)] = &Llvm::StartSingleThreadedCompilation; - jitExports[static_cast(JitApiId::FinishSingleThreadedCompilation)] = &Llvm::FinishSingleThreadedCompilation; + jitExports[static_cast(JitApiId::StartSingleThreadedCompilation)] = (void*)&Llvm::StartSingleThreadedCompilation; + jitExports[static_cast(JitApiId::FinishSingleThreadedCompilation)] = (void*)&Llvm::FinishSingleThreadedCompilation; jitExports[static_cast(JitApiId::Count)] = (void*)0x1234; } diff --git a/src/coreclr/jit/llvmcodegen.cpp b/src/coreclr/jit/llvmcodegen.cpp index d60ac8d5e5ab..92249ab85fc2 100644 --- a/src/coreclr/jit/llvmcodegen.cpp +++ b/src/coreclr/jit/llvmcodegen.cpp @@ -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(); } diff --git a/src/coreclr/jit/llvmlssa.cpp b/src/coreclr/jit/llvmlssa.cpp index e5d6f4a1aa10..1d5f1a72704b 100644 --- a/src/coreclr/jit/llvmlssa.cpp +++ b/src/coreclr/jit/llvmlssa.cpp @@ -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; } } diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets index dbe59baa3b94..71241d623c5e 100644 --- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets +++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets @@ -388,7 +388,7 @@ The .NET Foundation licenses this file to you under the MIT license. + Condition="'$(EMSDK)' == '' and '$(_targetOS)' == 'browser'" /> @@ -420,8 +420,7 @@ The .NET Foundation licenses this file to you under the MIT license. $(CompileWasmArgs) -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -disable-lsr --sysroot="$(WASI_SDK_PATH)/share/wasi-sysroot" -target $(IlcLlvmTarget) .exe - - "$(EMSDK)/upstream/bin/clang++$(ExeExt)" + "$(WASI_SDK_PATH)/bin/clang++$(ExeExt)" "$(WASI_SDK_PATH)/bin/clang" diff --git a/src/native/libs/CMakeLists.txt b/src/native/libs/CMakeLists.txt index ded638d85418..4bc21c1bb0b9 100644 --- a/src/native/libs/CMakeLists.txt +++ b/src/native/libs/CMakeLists.txt @@ -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) diff --git a/src/native/libs/System.Native/pal_io.c b/src/native/libs/System.Native/pal_io.c index 9c65db55c4ee..c7c321e30241 100644 --- a/src/native/libs/System.Native/pal_io.c +++ b/src/native/libs/System.Native/pal_io.c @@ -49,8 +49,10 @@ #elif HAVE_SYS_STATVFS_H && !HAVE_NON_LEGACY_STATFS // SunOS #include #include +#ifndef TARGET_WASI #include #endif +#endif #ifdef _AIX #include