From 02d27e13ee53bfae3097aaebdc182af38b037e9a Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Mon, 22 Mar 2021 06:28:51 -0500 Subject: [PATCH] Fix some compilation warnings and enable Windows JIT (#586) --- build-scripts/config_common.cmake | 10 ++- core/iwasm/aot/aot_loader.c | 13 ++-- core/iwasm/common/wasm_runtime_common.c | 4 +- core/iwasm/compilation/simd/simd_int_arith.c | 6 +- core/iwasm/interpreter/wasm_runtime.c | 4 +- .../libc-builtin/libc_builtin_wrapper.c | 4 +- core/shared/utils/bh_vector.c | 14 ++-- core/shared/utils/bh_vector.h | 4 +- core/shared/utils/uncommon/bh_getopt.c | 2 +- product-mini/platforms/windows/CMakeLists.txt | 2 +- product-mini/platforms/windows/build_llvm.py | 69 +++++++++++++++++++ product-mini/platforms/windows/main.c | 2 +- samples/workload/bwa/CMakeLists.txt | 1 + samples/workload/bwa/bwa.patch | 13 ++++ 14 files changed, 119 insertions(+), 29 deletions(-) create mode 100644 product-mini/platforms/windows/build_llvm.py create mode 100644 samples/workload/bwa/bwa.patch diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index e0ecc5407..03a4b23bd 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -79,10 +79,14 @@ if (WAMR_BUILD_JIT EQUAL 1) if (WAMR_BUILD_AOT EQUAL 1) add_definitions("-DWASM_ENABLE_JIT=1") set (LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm") - if (NOT EXISTS "${LLVM_SRC_ROOT}/build") - message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build") + set (LLVM_BUILD_ROOT "${LLVM_SRC_ROOT}/build") + if (WAMR_BUILD_PLATFORM STREQUAL "windows") + set (LLVM_BUILD_ROOT "${LLVM_SRC_ROOT}/win32build") endif () - set (CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${LLVM_BUILD_ROOT}") + message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_BUILD_ROOT}") + endif () + set (CMAKE_PREFIX_PATH "${LLVM_BUILD_ROOT};${CMAKE_PREFIX_PATH}") find_package(LLVM REQUIRED CONFIG) include_directories(${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index f533758ef..40d497369 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -574,7 +574,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, data_list[i]->offset.init_expr_type = (uint8)init_expr_type; data_list[i]->offset.u.i64 = (int64)init_expr_value; data_list[i]->func_index_count = func_index_count; - read_byte_array(buf, buf_end, data_list[i]->func_indexes, size1); + read_byte_array(buf, buf_end, data_list[i]->func_indexes, (uint32)size1); } *p_buf = buf; @@ -1444,7 +1444,7 @@ do_text_relocation(AOTModule *module, bh_memcpy_s(xmm_buf, sizeof(xmm_buf), symbol + strlen(XMM_PLT_PREFIX) + 16, 16); if (!str2uint64(xmm_buf, (uint64*)symbol_addr)) { - set_error_buf_v(error_buf, error_buf, + set_error_buf_v(error_buf, error_buf_size, "resolve symbol %s failed", symbol); goto check_symbol_fail; } @@ -1452,7 +1452,7 @@ do_text_relocation(AOTModule *module, bh_memcpy_s(xmm_buf, sizeof(xmm_buf), symbol + strlen(XMM_PLT_PREFIX), 16); if (!str2uint64(xmm_buf, (uint64*)((uint8*)symbol_addr + 8))) { - set_error_buf_v(error_buf, error_buf, + set_error_buf_v(error_buf, error_buf_size, "resolve symbol %s failed", symbol); goto check_symbol_fail; } @@ -1468,7 +1468,7 @@ do_text_relocation(AOTModule *module, bh_memcpy_s(real_buf, sizeof(real_buf), symbol + strlen(REAL_PLT_PREFIX), 16); if (!str2uint64(real_buf, (uint64*)symbol_addr)) { - set_error_buf_v(error_buf, error_buf, + set_error_buf_v(error_buf, error_buf_size, "resolve symbol %s failed", symbol); goto check_symbol_fail; } @@ -1484,7 +1484,7 @@ do_text_relocation(AOTModule *module, bh_memcpy_s(float_buf, sizeof(float_buf), symbol + strlen(REAL_PLT_PREFIX), 8); if (!str2uint32(float_buf, (uint32*)symbol_addr)) { - set_error_buf_v(error_buf, error_buf, + set_error_buf_v(error_buf, error_buf_size, "resolve symbol %s failed", symbol); goto check_symbol_fail; } @@ -2306,7 +2306,8 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, goto fail1; } - bh_memcpy_s(module->memories, size, comp_data->memories, size); + bh_memcpy_s(module->memories, (uint32)size, + comp_data->memories, (uint32)size); } module->mem_init_data_list = comp_data->mem_init_data_list; diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index f863fb4d3..95d2fd7af 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -777,7 +777,7 @@ wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst, void wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst) { - return wasm_runtime_deinstantiate_internal(module_inst, false); + wasm_runtime_deinstantiate_internal(module_inst, false); } WASMExecEnv * @@ -1962,7 +1962,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, fail: if (envp) - wasm_runtime_free(envp); + wasm_runtime_free((void*)envp); if (init_options.preopens) wasm_runtime_free(init_options.preopens); diff --git a/core/iwasm/compilation/simd/simd_int_arith.c b/core/iwasm/compilation/simd/simd_int_arith.c index 4a83e3be7..f61b67bcd 100644 --- a/core/iwasm/compilation/simd/simd_int_arith.c +++ b/core/iwasm/compilation/simd/simd_int_arith.c @@ -83,7 +83,7 @@ aot_compile_simd_i8x16_arith(AOTCompContext *comp_ctx, return simd_v128_integer_arith(comp_ctx, func_ctx, arith_op, lhs, rhs); fail: - return NULL; + return false; } bool @@ -105,7 +105,7 @@ aot_compile_simd_i16x8_arith(AOTCompContext *comp_ctx, return simd_v128_integer_arith(comp_ctx, func_ctx, arith_op, lhs, rhs); fail: - return NULL; + return false; } bool @@ -127,7 +127,7 @@ aot_compile_simd_i32x4_arith(AOTCompContext *comp_ctx, return simd_v128_integer_arith(comp_ctx, func_ctx, arith_op, lhs, rhs); fail: - return NULL; + return false; } bool diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 4b8b374e1..a17bd9035 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -1909,8 +1909,8 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) return false; memory_data = memory->memory_data; - heap_size = memory->heap_data_end - memory->heap_data; - total_size_old = memory->memory_data_end - memory_data; + heap_size = (uint32)(memory->heap_data_end - memory->heap_data); + total_size_old = (uint32)(memory->memory_data_end - memory_data); total_page_count = inc_page_count + memory->cur_page_count; total_size = memory->num_bytes_per_page * (uint64)total_page_count; heap_data_old = memory->heap_data; diff --git a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c index 23d982485..195c55ad3 100644 --- a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c +++ b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c @@ -647,7 +647,7 @@ static uint32 strcpy_wrapper(wasm_exec_env_t exec_env, char *dst, const char *src) { wasm_module_inst_t module_inst = get_module_inst(exec_env); - uint32 len = strlen(src) + 1; + uint32 len = (uint32)strlen(src) + 1; /* src has been checked by runtime */ if (!validate_native_addr(dst, len)) @@ -712,7 +712,7 @@ free_wrapper(wasm_exec_env_t exec_env, void *ptr) if (!validate_native_addr(ptr, sizeof(uint32))) return; - return module_free(addr_native_to_app(ptr)); + module_free(addr_native_to_app(ptr)); } static int32 diff --git a/core/shared/utils/bh_vector.c b/core/shared/utils/bh_vector.c index dfb8339bc..33ea7265e 100644 --- a/core/shared/utils/bh_vector.c +++ b/core/shared/utils/bh_vector.c @@ -6,12 +6,14 @@ #include "bh_vector.h" static uint8* -alloc_vector_data(uint32 length, uint32 size_elem) +alloc_vector_data(size_t length, size_t size_elem) { uint64 total_size = ((uint64)size_elem) * length; uint8 *data; - if (total_size > UINT32_MAX) { + if (length > UINT32_MAX + || size_elem > UINT32_MAX + || total_size > UINT32_MAX) { return NULL; } @@ -23,7 +25,7 @@ alloc_vector_data(uint32 length, uint32 size_elem) } static bool -extend_vector(Vector *vector, uint32 length) +extend_vector(Vector *vector, size_t length) { uint8 *data; @@ -45,7 +47,7 @@ extend_vector(Vector *vector, uint32 length) } bool -bh_vector_init(Vector *vector, uint32 init_length, uint32 size_elem) +bh_vector_init(Vector *vector, size_t init_length, size_t size_elem) { if (!vector) { LOG_ERROR("Init vector failed: vector is NULL.\n"); @@ -104,7 +106,7 @@ bool bh_vector_get(const Vector *vector, uint32 index, void *elem_buf) bool bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf) { - uint32 i; + size_t i; uint8 *p; if (!vector || !elem_buf) { @@ -182,7 +184,7 @@ bh_vector_remove(Vector *vector, uint32 index, void *old_elem_buf) return true; } -uint32 +size_t bh_vector_size(const Vector *vector) { return vector ? vector->num_elems : 0; diff --git a/core/shared/utils/bh_vector.h b/core/shared/utils/bh_vector.h index 1ae7c279b..59f432f78 100644 --- a/core/shared/utils/bh_vector.h +++ b/core/shared/utils/bh_vector.h @@ -35,7 +35,7 @@ typedef struct Vector { * @return true if success, false otherwise */ bool -bh_vector_init(Vector *vector, uint32 init_length, uint32 size_elem); +bh_vector_init(Vector *vector, size_t init_length, size_t size_elem); /** * Set element of vector @@ -104,7 +104,7 @@ bh_vector_remove(Vector *vector, uint32 index, void *old_elem_buf); * * @return return the size of the vector */ -uint32 +size_t bh_vector_size(const Vector *vector); /** diff --git a/core/shared/utils/uncommon/bh_getopt.c b/core/shared/utils/uncommon/bh_getopt.c index b7000c3db..67815e19d 100644 --- a/core/shared/utils/uncommon/bh_getopt.c +++ b/core/shared/utils/uncommon/bh_getopt.c @@ -6,6 +6,7 @@ #ifndef __GNUC__ #include "bh_getopt.h" +#include #include char* optarg = NULL; @@ -14,7 +15,6 @@ int optind = 1; int getopt(int argc, char *const argv[], const char *optstring) { static int sp = 1; - int c; int opt; char *p; diff --git a/product-mini/platforms/windows/CMakeLists.txt b/product-mini/platforms/windows/CMakeLists.txt index cc1d389ae..4f130de0c 100644 --- a/product-mini/platforms/windows/CMakeLists.txt +++ b/product-mini/platforms/windows/CMakeLists.txt @@ -58,7 +58,7 @@ endif () if (NOT DEFINED WAMR_BUILD_FAST_INTERP) # Enable fast interpreter - set (WAMR_BUILD_FAST_INTERP 1) + set (WAMR_BUILD_FAST_INTERP 0) endif () if (NOT DEFINED WAMR_BUILD_MULTI_MODULE) diff --git a/product-mini/platforms/windows/build_llvm.py b/product-mini/platforms/windows/build_llvm.py new file mode 100644 index 000000000..f476e9e6c --- /dev/null +++ b/product-mini/platforms/windows/build_llvm.py @@ -0,0 +1,69 @@ +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +#!/usr/bin/env python3 +import os +import sys +from pathlib import Path + +def clone_llvm(): + llvm_dir = Path("llvm") + if(llvm_dir.exists() == False): + print("Clone llvm to core/deps/ ..") + for line in os.popen("git clone --branch release/11.x https://github.com/llvm/llvm-project.git llvm"): + print(line) + else: + print("llvm source codes already existed") + return llvm_dir + +def main(): + current_os = sys.platform + print("current OS is ", current_os) + + current_dir = Path.cwd() + deps_dir = current_dir.joinpath( "../../../core/deps") + + os.chdir(deps_dir) + llvm_dir = clone_llvm() + os.chdir(llvm_dir) + + build_dir_name = "win32build" + llvm_file = "LLVM.sln" + + Path(build_dir_name).mkdir(exist_ok = True) + build_dir = Path(build_dir_name) + os.chdir(build_dir) + + if ( not Path(llvm_file).exists()): + core_number = os.cpu_count() + print("Build llvm with", core_number, " cores") + cmd = 'cmake ../llvm \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ + -DCMAKE_BUILD_TYPE:STRING="Release" \ + -DLLVM_TARGETS_TO_BUILD:STRING="X86;ARM;AArch64;Mips" \ + -DLLVM_INCLUDE_GO_TESTS=OFF \ + -DLLVM_INCLUDE_TOOLS=OFF \ + -DLLVM_INCLUDE_UTILS=OFF \ + -DLLVM_ENABLE_TERMINFO=OFF \ + -DLLVM_BUILD_LLVM_DYLIB:BOOL=OFF \ + -DLLVM_OPTIMIZED_TABLEGEN:BOOL=ON \ + -DLLVM_ENABLE_ZLIB:BOOL=OFF \ + -DLLVM_INCLUDE_DOCS:BOOL=OFF \ + -DLLVM_INCLUDE_EXAMPLES:BOOL=OFF \ + -DLLVM_INCLUDE_TESTS:BOOL=OFF \ + -DLLVM_INCLUDE_BENCHMARKS:BOOL=OFF \ + -DLLVM_APPEND_VC_REV:BOOL=OFF' + print(cmd) + for line in os.popen(cmd): + print(line) + else: + print("llvm has already been Cmaked") + + print("Please open LLVM.sln in {} to build *Release* version".format(build_dir.absolute())) + + os.chdir(current_dir) + +if __name__ == "__main__": + main() diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index 5cb3415a4..c9ad6821c 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -118,7 +118,7 @@ app_instance_repl(wasm_module_inst_t module_inst) size_t n; while ((printf("webassembly> "), - cmd = fgets(buffer, sizeof(buffer), stdin)) != -1) { + cmd = fgets(buffer, sizeof(buffer), stdin)) != NULL) { bh_assert(cmd); n = strlen(cmd); if (cmd[n - 1] == '\n') { diff --git a/samples/workload/bwa/CMakeLists.txt b/samples/workload/bwa/CMakeLists.txt index 5c1d4a8f8..0ea2c2947 100644 --- a/samples/workload/bwa/CMakeLists.txt +++ b/samples/workload/bwa/CMakeLists.txt @@ -117,6 +117,7 @@ ExternalProject_Add(bwa UPDATE_COMMAND git clean -fd && git checkout -- * && ${CMAKE_COMMAND} -E echo "Copying pre-installed CMakeLists.txt" && ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.bwa_wasm.txt CMakeLists.txt + && git apply ../bwa.patch CONFIGURE_COMMAND ${CMAKE_COMMAND} -DWASI_SDK_PREFIX=${WASI_SDK_HOME}/wasi-sdk -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_HOME}/wasi-sdk/share/cmake/wasi-sdk.cmake diff --git a/samples/workload/bwa/bwa.patch b/samples/workload/bwa/bwa.patch new file mode 100644 index 000000000..5599ca319 --- /dev/null +++ b/samples/workload/bwa/bwa.patch @@ -0,0 +1,13 @@ +diff --git a/utils.c b/utils.c +index 9ceb1be..323299f 100644 +--- a/utils.c ++++ b/utils.c +@@ -301,6 +301,7 @@ long peakrss(void) + #ifdef __linux__ + return r.ru_maxrss * 1024; + #else +- return r.ru_maxrss; ++ /*return r.ru_maxrss;*/ ++ return 0; + #endif + }