Add unit test suites (#3490)
This commit is contained in:
72
tests/unit/compilation/CMakeLists.txt
Normal file
72
tests/unit/compilation/CMakeLists.txt
Normal file
@ -0,0 +1,72 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required(VERSION 2.9)
|
||||
|
||||
project (test-compilation)
|
||||
|
||||
add_definitions (-DRUN_ON_LINUX)
|
||||
|
||||
add_definitions (-Dattr_container_malloc=malloc)
|
||||
add_definitions (-Dattr_container_free=free)
|
||||
add_definitions (-DWASM_ENABLE_WAMR_COMPILER=1)
|
||||
add_definitions (-DWASM_ENABLE_DUMP_CALL_STACK=1)
|
||||
add_definitions (-DWASM_ENABLE_AOT_STACK_FRAME=1)
|
||||
|
||||
set (WAMR_BUILD_LIBC_WASI 0)
|
||||
set (WAMR_BUILD_APP_FRAMEWORK 0)
|
||||
set (WAMR_BUILD_THREAD_MGR 1)
|
||||
set (WAMR_BUILD_AOT 1)
|
||||
|
||||
include (../unit_common.cmake)
|
||||
|
||||
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")
|
||||
endif ()
|
||||
set (CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}")
|
||||
find_package(LLVM REQUIRED CONFIG)
|
||||
include_directories(${LLVM_INCLUDE_DIRS})
|
||||
add_definitions(${LLVM_DEFINITIONS})
|
||||
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
|
||||
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
|
||||
|
||||
include (${IWASM_DIR}/compilation/iwasm_compl.cmake)
|
||||
|
||||
include_directories (${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
|
||||
|
||||
set (UNIT_SOURCE ${source_all})
|
||||
|
||||
set (unit_test_sources
|
||||
${UNIT_SOURCE}
|
||||
${WAMR_RUNTIME_LIB_SOURCE}
|
||||
${UNCOMMON_SHARED_SOURCE}
|
||||
${SRC_LIST}
|
||||
${PLATFORM_SHARED_SOURCE}
|
||||
${UTILS_SHARED_SOURCE}
|
||||
${MEM_ALLOC_SHARED_SOURCE}
|
||||
${LIB_HOST_AGENT_SOURCE}
|
||||
${NATIVE_INTERFACE_SOURCE}
|
||||
${LIBC_BUILTIN_SOURCE}
|
||||
${IWASM_COMMON_SOURCE}
|
||||
${IWASM_INTERP_SOURCE}
|
||||
${IWASM_AOT_SOURCE}
|
||||
${IWASM_COMPL_SOURCE}
|
||||
${WASM_APP_LIB_SOURCE_ALL}
|
||||
)
|
||||
|
||||
# Now simply link against gtest or gtest_main as needed. Eg
|
||||
add_executable (compilation_test ${unit_test_sources})
|
||||
|
||||
target_link_libraries (compilation_test ${LLVM_AVAILABLE_LIBS} gtest_main )
|
||||
|
||||
add_custom_command(TARGET compilation_test POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_LIST_DIR}/wasm-apps/main.wasm
|
||||
${CMAKE_CURRENT_BINARY_DIR}
|
||||
COMMENT "Copy main.wasm to the directory: build/compilation."
|
||||
)
|
||||
|
||||
gtest_discover_tests(compilation_test)
|
||||
203
tests/unit/compilation/aot_compiler_test.cc
Normal file
203
tests/unit/compilation/aot_compiler_test.cc
Normal file
@ -0,0 +1,203 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "wasm_export.h"
|
||||
#include "aot_export.h"
|
||||
#include "bh_read_file.h"
|
||||
|
||||
static std::string CWD;
|
||||
static std::string MAIN_WASM = "/main.wasm";
|
||||
static char *WASM_FILE;
|
||||
|
||||
static std::string
|
||||
get_binary_path()
|
||||
{
|
||||
char cwd[1024];
|
||||
memset(cwd, 0, 1024);
|
||||
|
||||
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
|
||||
}
|
||||
|
||||
char *path_end = strrchr(cwd, '/');
|
||||
if (path_end != NULL) {
|
||||
*path_end = '\0';
|
||||
}
|
||||
|
||||
return std::string(cwd);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
char *
|
||||
aot_generate_tempfile_name(const char *prefix, const char *extension,
|
||||
char *buffer, uint32 len);
|
||||
}
|
||||
|
||||
class aot_compiler_test_suit : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// virtual void SetUp() will be called before each test is run. You
|
||||
// should define it if you need to initialize the varaibles.
|
||||
// Otherwise, this can be skipped.
|
||||
virtual void SetUp() {}
|
||||
|
||||
static void SetUpTestCase()
|
||||
{
|
||||
CWD = get_binary_path();
|
||||
WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
|
||||
}
|
||||
|
||||
// virtual void TearDown() will be called after each test is run.
|
||||
// You should define it if there is cleanup work to do. Otherwise,
|
||||
// you don't have to provide it.
|
||||
//
|
||||
virtual void TearDown() {}
|
||||
|
||||
static void TearDownTestCase() { free(WASM_FILE); }
|
||||
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
void
|
||||
test_aot_emit_object_file_with_option(AOTCompOption *option_ptr)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
aot_comp_data_t comp_data = nullptr;
|
||||
aot_comp_context_t comp_ctx = nullptr;
|
||||
char out_file_name[] = "test.aot";
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data(wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
|
||||
comp_ctx = aot_create_comp_context(comp_data, option_ptr);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_STREQ(aot_get_last_error(), "");
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
|
||||
EXPECT_TRUE(aot_emit_object_file(comp_ctx, out_file_name));
|
||||
}
|
||||
|
||||
TEST_F(aot_compiler_test_suit, aot_emit_object_file)
|
||||
{
|
||||
AOTCompOption option = { 0 };
|
||||
uint32_t i = 0;
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
// Test opt_level in range from 0 to 3.
|
||||
for (i = 0; i <= 3; i++) {
|
||||
option.opt_level = i;
|
||||
test_aot_emit_object_file_with_option(&option);
|
||||
}
|
||||
|
||||
// Test size_level in range from 0 to 3.
|
||||
option.opt_level = 3;
|
||||
for (i = 0; i <= 3; i++) {
|
||||
option.size_level = i;
|
||||
test_aot_emit_object_file_with_option(&option);
|
||||
}
|
||||
|
||||
// Test output_format in range from AOT_FORMAT_FILE to AOT_LLVMIR_OPT_FILE.
|
||||
option.size_level = 3;
|
||||
for (i = AOT_FORMAT_FILE; i <= AOT_LLVMIR_OPT_FILE; i++) {
|
||||
option.output_format = i;
|
||||
test_aot_emit_object_file_with_option(&option);
|
||||
}
|
||||
|
||||
// Test bounds_checks in range 0 to 2.
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
for (i = 0; i <= 2; i++) {
|
||||
option.bounds_checks = i;
|
||||
test_aot_emit_object_file_with_option(&option);
|
||||
}
|
||||
|
||||
// Test all enable option is false.
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = false;
|
||||
option.enable_aux_stack_check = false;
|
||||
option.enable_bulk_memory = false;
|
||||
option.enable_ref_types = false;
|
||||
test_aot_emit_object_file_with_option(&option);
|
||||
}
|
||||
|
||||
TEST_F(aot_compiler_test_suit, aot_emit_llvm_file)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
aot_comp_data_t comp_data = nullptr;
|
||||
aot_comp_context_t comp_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
char out_file_name[] = "out_file_name_test";
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data(wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_STREQ(aot_get_last_error(), "");
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
|
||||
EXPECT_EQ(true, aot_emit_llvm_file(comp_ctx, out_file_name));
|
||||
}
|
||||
|
||||
TEST_F(aot_compiler_test_suit, aot_generate_tempfile_name)
|
||||
{
|
||||
char obj_file_name[64];
|
||||
|
||||
// Test common case.
|
||||
aot_generate_tempfile_name("wamrc-obj", "o", obj_file_name,
|
||||
sizeof(obj_file_name));
|
||||
EXPECT_NE(nullptr, strstr(obj_file_name, ".o"));
|
||||
|
||||
// Test abnormal cases.
|
||||
EXPECT_EQ(nullptr,
|
||||
aot_generate_tempfile_name("wamrc-obj", "o", obj_file_name, 0));
|
||||
char obj_file_name_1[20];
|
||||
EXPECT_EQ(nullptr, aot_generate_tempfile_name(
|
||||
"wamrc-obj", "12345678901234567890", obj_file_name_1,
|
||||
sizeof(obj_file_name_1)));
|
||||
}
|
||||
104
tests/unit/compilation/aot_emit_aot_file_test.cc
Normal file
104
tests/unit/compilation/aot_emit_aot_file_test.cc
Normal file
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "wasm_export.h"
|
||||
#include "aot_export.h"
|
||||
#include "bh_read_file.h"
|
||||
|
||||
static std::string CWD;
|
||||
static std::string MAIN_WASM = "/main.wasm";
|
||||
static char *WASM_FILE;
|
||||
|
||||
static std::string
|
||||
get_binary_path()
|
||||
{
|
||||
char cwd[1024];
|
||||
memset(cwd, 0, 1024);
|
||||
|
||||
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
|
||||
}
|
||||
|
||||
char *path_end = strrchr(cwd, '/');
|
||||
if (path_end != NULL) {
|
||||
*path_end = '\0';
|
||||
}
|
||||
|
||||
return std::string(cwd);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
uint8 *
|
||||
aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
|
||||
uint32 *p_aot_file_size);
|
||||
}
|
||||
|
||||
class aot_emit_aot_file_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// virtual void SetUp() will be called before each test is run. You
|
||||
// should define it if you need to initialize the varaibles.
|
||||
// Otherwise, this can be skipped.
|
||||
virtual void SetUp() {}
|
||||
|
||||
static void SetUpTestCase()
|
||||
{
|
||||
CWD = get_binary_path();
|
||||
WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
|
||||
}
|
||||
|
||||
// virtual void TearDown() will be called after each test is run.
|
||||
// You should define it if there is cleanup work to do. Otherwise,
|
||||
// you don't have to provide it.
|
||||
//
|
||||
virtual void TearDown() {}
|
||||
|
||||
static void TearDownTestCase() { free(WASM_FILE); }
|
||||
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(aot_emit_aot_file_test_suite, aot_emit_aot_file)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
aot_comp_data_t comp_data = nullptr;
|
||||
aot_comp_context_t comp_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
char out_file_name[] = "test.aot";
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = false;
|
||||
option.enable_aux_stack_check = false;
|
||||
option.enable_bulk_memory = false;
|
||||
option.enable_ref_types = false;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data(wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
|
||||
EXPECT_EQ(false, aot_emit_aot_file(comp_ctx, comp_data, nullptr));
|
||||
}
|
||||
112
tests/unit/compilation/aot_emit_compare_test.cc
Normal file
112
tests/unit/compilation/aot_emit_compare_test.cc
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "aot_emit_compare.h"
|
||||
|
||||
class compilation_aot_emit_compare_test : public testing::Test
|
||||
{
|
||||
protected:
|
||||
virtual void SetUp() {}
|
||||
virtual void TearDown() {}
|
||||
|
||||
public:
|
||||
};
|
||||
|
||||
TEST_F(compilation_aot_emit_compare_test, aot_compile_op_i32_compare)
|
||||
{
|
||||
AOTCompContext comp_ctx = { 0 };
|
||||
AOTFuncContext func_ctx = { 0 };
|
||||
IntCond cond = INT_EQZ;
|
||||
IntCond cond1 = INT_EQZ;
|
||||
|
||||
/* false cond = 0 */
|
||||
EXPECT_FALSE(aot_compile_op_i32_compare(&comp_ctx, &func_ctx, cond));
|
||||
|
||||
/* false cond = -1 */
|
||||
EXPECT_FALSE(
|
||||
aot_compile_op_i32_compare(&comp_ctx, &func_ctx, (IntCond)(-1)));
|
||||
|
||||
/* false cond = [1:10] || [11:100] */
|
||||
for (int i = 0; i < 0xFFFF; i++) {
|
||||
/* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
|
||||
cond = (IntCond)(1 + (rand() % (INT_GE_U - 1 + 1)));
|
||||
cond1 = (IntCond)((INT_GE_U + 1) + (rand() % (100 - 1 + 1)));
|
||||
EXPECT_FALSE(aot_compile_op_i32_compare(&comp_ctx, &func_ctx, cond));
|
||||
EXPECT_FALSE(aot_compile_op_i32_compare(&comp_ctx, &func_ctx, cond1));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_compare_test, aot_compile_op_i64_compare)
|
||||
{
|
||||
AOTCompContext comp_ctx = { 0 };
|
||||
AOTFuncContext func_ctx = { 0 };
|
||||
IntCond cond = INT_EQZ;
|
||||
IntCond cond1 = INT_EQZ;
|
||||
|
||||
/* false cond = 0 */
|
||||
// EXPECT_FALSE(aot_compile_op_i64_compare(&comp_ctx, &func_ctx, cond));
|
||||
|
||||
/* false cond = -1 */
|
||||
EXPECT_FALSE(
|
||||
aot_compile_op_i64_compare(&comp_ctx, &func_ctx, (IntCond)(-1)));
|
||||
|
||||
/* false cond = [1:10] || [11:100] */
|
||||
for (int i = 0; i < 0xFFFF; i++) {
|
||||
/* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
|
||||
cond = (IntCond)(1 + (rand() % (INT_GE_U - 1 + 1)));
|
||||
cond1 = (IntCond)((INT_GE_U + 1) + (rand() % (100 - 1 + 1)));
|
||||
EXPECT_FALSE(aot_compile_op_i64_compare(&comp_ctx, &func_ctx, cond));
|
||||
EXPECT_FALSE(aot_compile_op_i64_compare(&comp_ctx, &func_ctx, cond1));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_compare_test, aot_compile_op_f32_compare)
|
||||
{
|
||||
AOTCompContext comp_ctx = { 0 };
|
||||
AOTFuncContext func_ctx = { 0 };
|
||||
FloatCond cond = FLOAT_EQ;
|
||||
FloatCond cond1 = FLOAT_EQ;
|
||||
|
||||
/* false cond = 0 */
|
||||
EXPECT_FALSE(aot_compile_op_f32_compare(&comp_ctx, &func_ctx, cond));
|
||||
|
||||
/* false cond = -1 */
|
||||
EXPECT_FALSE(
|
||||
aot_compile_op_f32_compare(&comp_ctx, &func_ctx, (FloatCond)(-1)));
|
||||
|
||||
/* false cond = [1:10] || [7:100] */
|
||||
for (int i = 0; i < 0xFFFF; i++) {
|
||||
/* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
|
||||
cond = (FloatCond)(1 + (rand() % (FLOAT_UNO - 1 + 1)));
|
||||
cond1 = (FloatCond)((FLOAT_UNO + 1) + (rand() % (100 - 1 + 1)));
|
||||
EXPECT_FALSE(aot_compile_op_f32_compare(&comp_ctx, &func_ctx, cond));
|
||||
EXPECT_FALSE(aot_compile_op_f32_compare(&comp_ctx, &func_ctx, cond1));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_compare_test, aot_compile_op_f64_compare)
|
||||
{
|
||||
AOTCompContext comp_ctx = { 0 };
|
||||
AOTFuncContext func_ctx = { 0 };
|
||||
FloatCond cond = FLOAT_EQ;
|
||||
FloatCond cond1 = FLOAT_EQ;
|
||||
|
||||
/* false cond = 0 */
|
||||
EXPECT_FALSE(aot_compile_op_f64_compare(&comp_ctx, &func_ctx, cond));
|
||||
|
||||
/* false cond = -1 */
|
||||
EXPECT_FALSE(
|
||||
aot_compile_op_f64_compare(&comp_ctx, &func_ctx, (FloatCond)(-1)));
|
||||
|
||||
/* false cond = [1:10] || [7:100] */
|
||||
for (int i = 0; i < 0xFFFF; i++) {
|
||||
/* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
|
||||
cond = (FloatCond)(1 + (rand() % (FLOAT_UNO - 1 + 1)));
|
||||
cond1 = (FloatCond)((FLOAT_UNO + 1) + (rand() % (100 - 1 + 1)));
|
||||
EXPECT_FALSE(aot_compile_op_f64_compare(&comp_ctx, &func_ctx, cond));
|
||||
EXPECT_FALSE(aot_compile_op_f64_compare(&comp_ctx, &func_ctx, cond1));
|
||||
}
|
||||
}
|
||||
196
tests/unit/compilation/aot_emit_control_test.cc
Normal file
196
tests/unit/compilation/aot_emit_control_test.cc
Normal file
@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "bh_read_file.h"
|
||||
#include "aot.h"
|
||||
#include "aot_llvm.h"
|
||||
#include "aot_emit_control.h"
|
||||
|
||||
static std::string CWD;
|
||||
static std::string MAIN_WASM = "/main.wasm";
|
||||
static char *WASM_FILE;
|
||||
|
||||
static std::string
|
||||
get_binary_path()
|
||||
{
|
||||
char cwd[1024];
|
||||
memset(cwd, 0, 1024);
|
||||
|
||||
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
|
||||
}
|
||||
|
||||
char *path_end = strrchr(cwd, '/');
|
||||
if (path_end != NULL) {
|
||||
*path_end = '\0';
|
||||
}
|
||||
|
||||
return std::string(cwd);
|
||||
}
|
||||
|
||||
class aot_emit_control_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// virtual void SetUp() will be called before each test is run. You
|
||||
// should define it if you need to initialize the varaibles.
|
||||
// Otherwise, this can be skipped.
|
||||
virtual void SetUp() {}
|
||||
|
||||
static void SetUpTestCase()
|
||||
{
|
||||
CWD = get_binary_path();
|
||||
WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
|
||||
}
|
||||
|
||||
// virtual void TearDown() will be called after each test is run.
|
||||
// You should define it if there is cleanup work to do. Otherwise,
|
||||
// you don't have to provide it.
|
||||
//
|
||||
virtual void TearDown() {}
|
||||
|
||||
static void TearDownTestCase() { free(WASM_FILE); }
|
||||
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(aot_emit_control_test_suite, check_suspend_flags)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
char out_file_name[] = "out_file_name_test";
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
|
||||
func_ctx = comp_ctx->func_ctxes[1];
|
||||
EXPECT_EQ(true, check_suspend_flags(comp_ctx, func_ctx, false));
|
||||
}
|
||||
|
||||
TEST_F(aot_emit_control_test_suite, aot_compile_op_block)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
char out_file_name[] = "out_file_name_test";
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
|
||||
func_ctx = comp_ctx->func_ctxes[1];
|
||||
func_ctx->block_stack.block_list_end = nullptr;
|
||||
EXPECT_EQ(false, aot_compile_op_block(comp_ctx, func_ctx, nullptr, nullptr,
|
||||
0, 0, nullptr, 0, nullptr));
|
||||
}
|
||||
|
||||
TEST_F(aot_emit_control_test_suite, aot_compile_op_else)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
char out_file_name[] = "out_file_name_test";
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
|
||||
func_ctx = comp_ctx->func_ctxes[1];
|
||||
func_ctx->block_stack.block_list_end = nullptr;
|
||||
EXPECT_EQ(false, aot_compile_op_else(comp_ctx, func_ctx, nullptr));
|
||||
|
||||
AOTBlock block_list_end_test;
|
||||
block_list_end_test.label_type = LABEL_TYPE_FUNCTION;
|
||||
func_ctx->block_stack.block_list_end = &block_list_end_test;
|
||||
EXPECT_EQ(false, aot_compile_op_else(comp_ctx, func_ctx, nullptr));
|
||||
|
||||
block_list_end_test.label_type = LABEL_TYPE_IF;
|
||||
block_list_end_test.llvm_else_block = nullptr;
|
||||
EXPECT_EQ(false, aot_compile_op_else(comp_ctx, func_ctx, nullptr));
|
||||
}
|
||||
101
tests/unit/compilation/aot_emit_function_test.cc
Normal file
101
tests/unit/compilation/aot_emit_function_test.cc
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "bh_read_file.h"
|
||||
#include "aot_emit_control.h"
|
||||
#include "aot_emit_function.h"
|
||||
|
||||
static std::string CWD;
|
||||
static std::string MAIN_WASM = "/main.wasm";
|
||||
static char *WASM_FILE;
|
||||
|
||||
static std::string
|
||||
get_binary_path()
|
||||
{
|
||||
char cwd[1024];
|
||||
memset(cwd, 0, 1024);
|
||||
|
||||
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
|
||||
}
|
||||
|
||||
char *path_end = strrchr(cwd, '/');
|
||||
if (path_end != NULL) {
|
||||
*path_end = '\0';
|
||||
}
|
||||
|
||||
return std::string(cwd);
|
||||
}
|
||||
|
||||
class aot_emit_function_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// virtual void SetUp() will be called before each test is run. You
|
||||
// should define it if you need to initialize the varaibles.
|
||||
// Otherwise, this can be skipped.
|
||||
virtual void SetUp() {}
|
||||
|
||||
static void SetUpTestCase()
|
||||
{
|
||||
CWD = get_binary_path();
|
||||
WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
|
||||
}
|
||||
|
||||
// virtual void TearDown() will be called after each test is run.
|
||||
// You should define it if there is cleanup work to do. Otherwise,
|
||||
// you don't have to provide it.
|
||||
//
|
||||
virtual void TearDown() {}
|
||||
|
||||
static void TearDownTestCase() { free(WASM_FILE); }
|
||||
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(aot_emit_function_test_suite, aot_compile_op_call)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
char out_file_name[] = "out_file_name_test";
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
|
||||
func_ctx = comp_ctx->func_ctxes[1];
|
||||
EXPECT_EQ(false, aot_compile_op_call(comp_ctx, func_ctx, 9999, true));
|
||||
}
|
||||
335
tests/unit/compilation/aot_emit_memory_test.cc
Normal file
335
tests/unit/compilation/aot_emit_memory_test.cc
Normal file
@ -0,0 +1,335 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "bh_platform.h"
|
||||
#include "bh_read_file.h"
|
||||
#include "aot_emit_memory.h"
|
||||
#include "test_helper.h"
|
||||
|
||||
#define DEFAULT_CYCLE_TIMES 0xFFFF
|
||||
#define DEFAULT_MAX_RAND_NUM 0xFFFFFFFF
|
||||
|
||||
static std::string CWD;
|
||||
static std::string MAIN_WASM = "/main.wasm";
|
||||
static char *WASM_FILE;
|
||||
|
||||
static std::string
|
||||
get_binary_path()
|
||||
{
|
||||
char cwd[1024];
|
||||
memset(cwd, 0, 1024);
|
||||
|
||||
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
|
||||
}
|
||||
|
||||
char *path_end = strrchr(cwd, '/');
|
||||
if (path_end != NULL) {
|
||||
*path_end = '\0';
|
||||
}
|
||||
|
||||
return std::string(cwd);
|
||||
}
|
||||
|
||||
class compilation_aot_emit_memory_test : public testing::Test
|
||||
{
|
||||
protected:
|
||||
void SetUp() override
|
||||
{
|
||||
CWD = get_binary_path();
|
||||
WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
|
||||
AOTCompOption option = { 0 };
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.stack_bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = reinterpret_cast<WASMModule *>(wasm_runtime_load(
|
||||
wasm_file_buf, wasm_file_size, error_buf, sizeof(error_buf)));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
comp_data = aot_create_comp_data(wasm_module, NULL, false);
|
||||
EXPECT_NE(comp_data, nullptr);
|
||||
|
||||
// properly init compilation and function context, to do that,
|
||||
// use as a dummy module(instead of compile the function in it, simply
|
||||
// test the APIs)
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
func_ctx = comp_ctx->func_ctxes[0];
|
||||
EXPECT_NE(func_ctx, nullptr);
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
aot_destroy_comp_context(comp_ctx);
|
||||
aot_destroy_comp_data(comp_data);
|
||||
wasm_runtime_unload(reinterpret_cast<WASMModuleCommon *>(wasm_module));
|
||||
}
|
||||
|
||||
public:
|
||||
WASMModule *wasm_module = nullptr;
|
||||
AOTCompData *comp_data = nullptr;
|
||||
AOTCompContext *comp_ctx = nullptr;
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_check_memory_overflow)
|
||||
{
|
||||
uint32 offset = 64;
|
||||
uint32 bytes = 4;
|
||||
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes, false);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i32_load)
|
||||
{
|
||||
uint32 align = 0;
|
||||
uint32 offset = 1024;
|
||||
uint32 bytes = 0;
|
||||
bool sign = false;
|
||||
bool atomic = false;
|
||||
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
bytes = (1 + (rand() % (4 - 1 + 1)));
|
||||
printf("---%d", aot_compile_op_i32_load(comp_ctx, func_ctx, align,
|
||||
offset, bytes, sign, atomic));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i64_load)
|
||||
{
|
||||
uint32 align = 0;
|
||||
uint32 offset = 1024;
|
||||
uint32 bytes = 0;
|
||||
bool sign = false;
|
||||
bool atomic = false;
|
||||
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
bytes = (1 + (rand() % (4 - 1 + 1)));
|
||||
sign = !sign;
|
||||
atomic = !atomic;
|
||||
aot_compile_op_i64_load(comp_ctx, func_ctx, align, offset, bytes, sign,
|
||||
atomic);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f32_load)
|
||||
{
|
||||
uint32 align = 10;
|
||||
uint32 offset = 10;
|
||||
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
aot_compile_op_f32_load(comp_ctx, func_ctx, align, offset);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f64_load)
|
||||
{
|
||||
uint32 align = 10;
|
||||
uint32 offset = 10;
|
||||
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
align = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
offset = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
aot_compile_op_f64_load(comp_ctx, func_ctx, align, offset);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i32_store)
|
||||
{
|
||||
uint32 align = 0;
|
||||
uint32 offset = 0;
|
||||
uint32 bytes = 0;
|
||||
bool atomic = false;
|
||||
|
||||
EXPECT_FALSE(aot_compile_op_i32_store(comp_ctx, func_ctx, align, offset,
|
||||
bytes, atomic));
|
||||
|
||||
/* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
bytes = (1 + (rand() % (4 - 1 + 1)));
|
||||
offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
|
||||
align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
|
||||
atomic = !atomic;
|
||||
|
||||
EXPECT_FALSE(aot_compile_op_i32_store(comp_ctx, func_ctx, align, offset,
|
||||
bytes, atomic));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_i64_store)
|
||||
{
|
||||
uint32 align = 0;
|
||||
uint32 offset = 0;
|
||||
uint32 bytes = 0;
|
||||
bool atomic = false;
|
||||
|
||||
EXPECT_FALSE(aot_compile_op_i64_store(comp_ctx, func_ctx, align, offset,
|
||||
bytes, atomic));
|
||||
|
||||
/* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
bytes = (1 + (rand() % (8 - 1 + 1)));
|
||||
offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
|
||||
align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
|
||||
atomic = !atomic;
|
||||
|
||||
EXPECT_FALSE(aot_compile_op_i64_store(comp_ctx, func_ctx, align, offset,
|
||||
bytes, atomic));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f32_store)
|
||||
{
|
||||
uint32 align = 0;
|
||||
uint32 offset = 0;
|
||||
|
||||
EXPECT_FALSE(aot_compile_op_f32_store(comp_ctx, func_ctx, align, offset));
|
||||
|
||||
/* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
|
||||
align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
|
||||
|
||||
EXPECT_FALSE(
|
||||
aot_compile_op_f32_store(comp_ctx, func_ctx, align, offset));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_f64_store)
|
||||
{
|
||||
uint32 align = 0;
|
||||
uint32 offset = 0;
|
||||
|
||||
EXPECT_FALSE(aot_compile_op_f64_store(comp_ctx, func_ctx, align, offset));
|
||||
|
||||
/* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
offset = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
|
||||
align = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
|
||||
|
||||
EXPECT_FALSE(
|
||||
aot_compile_op_f64_store(comp_ctx, func_ctx, align, offset));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_size)
|
||||
{
|
||||
aot_compile_op_memory_size(comp_ctx, func_ctx);
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_grow)
|
||||
{
|
||||
aot_compile_op_memory_grow(comp_ctx, func_ctx);
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_BULK_MEMORY != 0
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_init)
|
||||
{
|
||||
uint32 seg_index = 0;
|
||||
|
||||
/* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
seg_index = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
|
||||
aot_compile_op_memory_init(comp_ctx, func_ctx, seg_index);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_data_drop)
|
||||
{
|
||||
uint32 seg_index = 0;
|
||||
|
||||
/* Generate random number range:[m,n] int a=m+rand()%(n-m+1); */
|
||||
for (uint32 i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
seg_index = (1 + (rand() % (0xFFFFFFFF - 1 + 1)));
|
||||
aot_compile_op_data_drop(comp_ctx, func_ctx, seg_index);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_copy)
|
||||
{
|
||||
aot_compile_op_memory_copy(comp_ctx, func_ctx);
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_memory_fill)
|
||||
{
|
||||
aot_compile_op_memory_fill(comp_ctx, func_ctx);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_SHARED_MEMORY != 0
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_atomic_rmw)
|
||||
{
|
||||
uint8 atomic_op = LLVMAtomicRMWBinOpAdd;
|
||||
uint8 op_type = VALUE_TYPE_I32;
|
||||
uint32 align = 4;
|
||||
uint32 offset = 64;
|
||||
uint32 bytes = 4;
|
||||
|
||||
aot_compile_op_atomic_rmw(comp_ctx, func_ctx, atomic_op, op_type, align,
|
||||
offset, bytes);
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_atomic_cmpxchg)
|
||||
{
|
||||
|
||||
uint8 op_type = VALUE_TYPE_I32;
|
||||
uint32 align = 4;
|
||||
uint32 offset = 64;
|
||||
uint32 bytes = 4;
|
||||
|
||||
aot_compile_op_atomic_cmpxchg(comp_ctx, func_ctx, op_type, align, offset,
|
||||
bytes);
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compile_op_atomic_wait)
|
||||
{
|
||||
|
||||
uint8 op_type = VALUE_TYPE_I32;
|
||||
uint32 align = 4;
|
||||
uint32 offset = 64;
|
||||
uint32 bytes = 4;
|
||||
|
||||
aot_compile_op_atomic_wait(comp_ctx, func_ctx, op_type, align, offset,
|
||||
bytes);
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_memory_test, aot_compiler_op_atomic_notify)
|
||||
{
|
||||
|
||||
uint32 align = 4;
|
||||
uint32 offset = 64;
|
||||
uint32 bytes = 4;
|
||||
|
||||
aot_compiler_op_atomic_notify(comp_ctx, func_ctx, align, offset, bytes);
|
||||
}
|
||||
#endif
|
||||
119
tests/unit/compilation/aot_emit_numberic_test.cc
Normal file
119
tests/unit/compilation/aot_emit_numberic_test.cc
Normal file
@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "bh_read_file.h"
|
||||
#include "aot_llvm.h"
|
||||
#include "aot_emit_numberic.h"
|
||||
#include "aot_compiler.h"
|
||||
|
||||
static std::string CWD;
|
||||
static std::string MAIN_WASM = "/main.wasm";
|
||||
static char *WASM_FILE;
|
||||
|
||||
static std::string
|
||||
get_binary_path()
|
||||
{
|
||||
char cwd[1024];
|
||||
memset(cwd, 0, 1024);
|
||||
|
||||
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
|
||||
}
|
||||
|
||||
char *path_end = strrchr(cwd, '/');
|
||||
if (path_end != NULL) {
|
||||
*path_end = '\0';
|
||||
}
|
||||
|
||||
return std::string(cwd);
|
||||
}
|
||||
|
||||
class aot_emit_numberic_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// virtual void SetUp() will be called before each test is run. You
|
||||
// should define it if you need to initialize the varaibles.
|
||||
// Otherwise, this can be skipped.
|
||||
virtual void SetUp() {}
|
||||
|
||||
static void SetUpTestCase()
|
||||
{
|
||||
CWD = get_binary_path();
|
||||
WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
|
||||
}
|
||||
|
||||
// virtual void TearDown() will be called after each test is run.
|
||||
// You should define it if there is cleanup work to do. Otherwise,
|
||||
// you don't have to provide it.
|
||||
//
|
||||
virtual void TearDown() {}
|
||||
|
||||
static void TearDownTestCase() { free(WASM_FILE); }
|
||||
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(aot_emit_numberic_test_suite, aot_compile_op_functions)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
func_ctx = comp_ctx->func_ctxes[1];
|
||||
|
||||
EXPECT_EQ(false,
|
||||
aot_compile_op_f32_arithmetic(comp_ctx, func_ctx, FLOAT_SUB));
|
||||
EXPECT_EQ(false, aot_compile_op_f32_copysign(comp_ctx, func_ctx));
|
||||
EXPECT_EQ(false, aot_compile_op_f32_math(comp_ctx, func_ctx, FLOAT_NEG));
|
||||
EXPECT_EQ(false,
|
||||
aot_compile_op_f64_arithmetic(comp_ctx, func_ctx, FLOAT_SUB));
|
||||
EXPECT_EQ(false, aot_compile_op_f64_copysign(comp_ctx, func_ctx));
|
||||
EXPECT_EQ(false, aot_compile_op_f64_math(comp_ctx, func_ctx, FLOAT_NEG));
|
||||
EXPECT_EQ(false, aot_compile_op_i32_clz(comp_ctx, func_ctx));
|
||||
EXPECT_EQ(false, aot_compile_op_i32_ctz(comp_ctx, func_ctx));
|
||||
EXPECT_EQ(false, aot_compile_op_i32_popcnt(comp_ctx, func_ctx));
|
||||
EXPECT_EQ(false, aot_compile_op_i32_shift(comp_ctx, func_ctx, INT_SHR_S));
|
||||
EXPECT_EQ(false, aot_compile_op_i64_arithmetic(comp_ctx, func_ctx, INT_SUB,
|
||||
nullptr));
|
||||
EXPECT_EQ(false, aot_compile_op_i64_bitwise(comp_ctx, func_ctx, INT_OR));
|
||||
EXPECT_EQ(false, aot_compile_op_i64_clz(comp_ctx, func_ctx));
|
||||
EXPECT_EQ(false, aot_compile_op_i64_ctz(comp_ctx, func_ctx));
|
||||
EXPECT_EQ(false, aot_compile_op_i64_popcnt(comp_ctx, func_ctx));
|
||||
EXPECT_EQ(false, aot_compile_op_i64_shift(comp_ctx, func_ctx, INT_SHR_S));
|
||||
}
|
||||
143
tests/unit/compilation/aot_emit_parametric_test.cc
Normal file
143
tests/unit/compilation/aot_emit_parametric_test.cc
Normal file
@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "bh_read_file.h"
|
||||
#include "aot_llvm.h"
|
||||
#include "aot_emit_parametric.h"
|
||||
|
||||
static std::string CWD;
|
||||
static std::string MAIN_WASM = "/main.wasm";
|
||||
static char *WASM_FILE;
|
||||
|
||||
static std::string
|
||||
get_binary_path()
|
||||
{
|
||||
char cwd[1024];
|
||||
memset(cwd, 0, 1024);
|
||||
|
||||
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
|
||||
}
|
||||
|
||||
char *path_end = strrchr(cwd, '/');
|
||||
if (path_end != NULL) {
|
||||
*path_end = '\0';
|
||||
}
|
||||
|
||||
return std::string(cwd);
|
||||
}
|
||||
|
||||
class aot_emit_parametric_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// virtual void SetUp() will be called before each test is run. You
|
||||
// should define it if you need to initialize the varaibles.
|
||||
// Otherwise, this can be skipped.
|
||||
virtual void SetUp() {}
|
||||
|
||||
static void SetUpTestCase()
|
||||
{
|
||||
CWD = get_binary_path();
|
||||
WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
|
||||
}
|
||||
|
||||
// virtual void TearDown() will be called after each test is run.
|
||||
// You should define it if there is cleanup work to do. Otherwise,
|
||||
// you don't have to provide it.
|
||||
//
|
||||
virtual void TearDown() {}
|
||||
|
||||
static void TearDownTestCase() { free(WASM_FILE); }
|
||||
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(aot_emit_parametric_test_suite, aot_compile_op_select)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
func_ctx = comp_ctx->func_ctxes[1];
|
||||
|
||||
EXPECT_FALSE(aot_compile_op_select(comp_ctx, func_ctx, true));
|
||||
EXPECT_FALSE(aot_compile_op_select(comp_ctx, func_ctx, false));
|
||||
}
|
||||
|
||||
TEST_F(aot_emit_parametric_test_suite, aot_compile_op_drop)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
func_ctx = comp_ctx->func_ctxes[1];
|
||||
func_ctx->block_stack.block_list_end = nullptr;
|
||||
|
||||
EXPECT_FALSE(aot_compile_op_drop(comp_ctx, func_ctx, true));
|
||||
EXPECT_FALSE(aot_compile_op_drop(comp_ctx, func_ctx, false));
|
||||
}
|
||||
105
tests/unit/compilation/aot_emit_table_test.cc
Normal file
105
tests/unit/compilation/aot_emit_table_test.cc
Normal file
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "bh_read_file.h"
|
||||
#include "aot_llvm.h"
|
||||
#include "aot_emit_table.h"
|
||||
|
||||
static std::string CWD;
|
||||
static std::string MAIN_WASM = "/main.wasm";
|
||||
static char *WASM_FILE;
|
||||
|
||||
static std::string
|
||||
get_binary_path()
|
||||
{
|
||||
char cwd[1024];
|
||||
memset(cwd, 0, 1024);
|
||||
|
||||
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
|
||||
}
|
||||
|
||||
char *path_end = strrchr(cwd, '/');
|
||||
if (path_end != NULL) {
|
||||
*path_end = '\0';
|
||||
}
|
||||
|
||||
return std::string(cwd);
|
||||
}
|
||||
|
||||
class aot_emit_table_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// virtual void SetUp() will be called before each test is run. You
|
||||
// should define it if you need to initialize the varaibles.
|
||||
// Otherwise, this can be skipped.
|
||||
virtual void SetUp() {}
|
||||
|
||||
static void SetUpTestCase()
|
||||
{
|
||||
CWD = get_binary_path();
|
||||
WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
|
||||
}
|
||||
|
||||
// virtual void TearDown() will be called after each test is run.
|
||||
// You should define it if there is cleanup work to do. Otherwise,
|
||||
// you don't have to provide it.
|
||||
//
|
||||
virtual void TearDown() {}
|
||||
static void TearDownTestCase() { free(WASM_FILE); }
|
||||
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(aot_emit_table_test_suite, get_tbl_inst_offset)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
func_ctx = comp_ctx->func_ctxes[1];
|
||||
|
||||
EXPECT_NE(0, get_tbl_inst_offset(comp_ctx, func_ctx, 6));
|
||||
EXPECT_NE(0, get_tbl_inst_offset(comp_ctx, func_ctx, 1));
|
||||
EXPECT_NE(0, get_tbl_inst_offset(comp_ctx, func_ctx, 0));
|
||||
((AOTCompData *)comp_ctx->comp_data)->import_table_count = 1;
|
||||
AOTImportTable import_tables_test;
|
||||
((AOTCompData *)comp_ctx->comp_data)->import_tables = &import_tables_test;
|
||||
EXPECT_NE(0, get_tbl_inst_offset(comp_ctx, func_ctx, 6));
|
||||
}
|
||||
97
tests/unit/compilation/aot_emit_variable_test.cc
Normal file
97
tests/unit/compilation/aot_emit_variable_test.cc
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "aot_emit_variable.h"
|
||||
|
||||
#define DEFAULT_CYCLE_TIMES 0xFFFF
|
||||
#define DEFAULT_MAX_RAND_NUM 0xFFFFFFFF
|
||||
|
||||
class compilation_aot_emit_variable_test : public testing::Test
|
||||
{
|
||||
protected:
|
||||
virtual void SetUp() {}
|
||||
virtual void TearDown() {}
|
||||
|
||||
public:
|
||||
AOTCompContext comp_ctx = { 0 };
|
||||
AOTFuncContext func_ctx = { 0 };
|
||||
};
|
||||
|
||||
TEST_F(compilation_aot_emit_variable_test, aot_compile_op_get_local)
|
||||
{
|
||||
AOTCompContext *pcomp_ctx = &comp_ctx;
|
||||
AOTFuncContext *pfunc_ctx = &func_ctx;
|
||||
uint32 local_idx = 0;
|
||||
|
||||
// aot_compile_op_get_local(NULL, pfunc_ctx, local_idx);
|
||||
|
||||
// for (uint32_t i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
// local_idx = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
// aot_compile_op_get_local(pcomp_ctx, pfunc_ctx, local_idx);
|
||||
// }
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_variable_test, aot_compile_op_set_local)
|
||||
{
|
||||
|
||||
AOTCompContext *pcomp_ctx = &comp_ctx;
|
||||
AOTFuncContext *pfunc_ctx = &func_ctx;
|
||||
uint32 local_idx = 0;
|
||||
|
||||
// aot_compile_op_set_local(pcomp_ctx, pfunc_ctx, local_idx);
|
||||
|
||||
// for (uint32_t i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
// local_idx = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
// aot_compile_op_set_local(pcomp_ctx, pfunc_ctx, local_idx);
|
||||
// }
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_variable_test, aot_compile_op_tee_local)
|
||||
{
|
||||
|
||||
AOTCompContext *pcomp_ctx = &comp_ctx;
|
||||
AOTFuncContext *pfunc_ctx = &func_ctx;
|
||||
uint32 local_idx = 0;
|
||||
|
||||
// aot_compile_op_tee_local(pcomp_ctx, pfunc_ctx, local_idx);
|
||||
|
||||
// for (uint32_t i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
// local_idx = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
// aot_compile_op_tee_local(pcomp_ctx, pfunc_ctx, local_idx);
|
||||
// }
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_variable_test, aot_compile_op_get_global)
|
||||
{
|
||||
AOTCompContext *pcomp_ctx = &comp_ctx;
|
||||
AOTFuncContext *pfunc_ctx = &func_ctx;
|
||||
uint32 global_idx = 0;
|
||||
|
||||
// aot_compile_op_get_global(pcomp_ctx, pfunc_ctx, global_idx);
|
||||
|
||||
// for (uint32_t i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
// local_idx = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
// aot_compile_op_get_global(pcomp_ctx, pfunc_ctx, global_idx);
|
||||
// }
|
||||
}
|
||||
|
||||
TEST_F(compilation_aot_emit_variable_test, aot_compile_op_set_global)
|
||||
{
|
||||
AOTCompContext *pcomp_ctx = &comp_ctx;
|
||||
AOTFuncContext *pfunc_ctx = &func_ctx;
|
||||
uint32 global_idx = 0;
|
||||
bool is_aux_stack = false;
|
||||
|
||||
// aot_compile_op_set_global(pcomp_ctx, pfunc_ctx, global_idx,
|
||||
// is_aux_stack);
|
||||
|
||||
// for (uint32_t i = 0; i < DEFAULT_CYCLE_TIMES; i++) {
|
||||
// is_aux_stack = is_aux_stack ? false : ture;
|
||||
// local_idx = (1 + (rand() % (DEFAULT_MAX_RAND_NUM - 1 + 1)));
|
||||
// aot_compile_op_set_global(pcomp_ctx, pfunc_ctx,
|
||||
// global_idx,is_aux_stack);
|
||||
// }
|
||||
}
|
||||
305
tests/unit/compilation/aot_llvm_test.cc
Normal file
305
tests/unit/compilation/aot_llvm_test.cc
Normal file
@ -0,0 +1,305 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "bh_read_file.h"
|
||||
#include "aot_llvm.h"
|
||||
#include "aot_compiler.h"
|
||||
|
||||
static std::string CWD;
|
||||
static std::string MAIN_WASM = "/main.wasm";
|
||||
static char *WASM_FILE;
|
||||
|
||||
static std::string
|
||||
get_binary_path()
|
||||
{
|
||||
char cwd[1024];
|
||||
memset(cwd, 0, 1024);
|
||||
|
||||
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
|
||||
}
|
||||
|
||||
char *path_end = strrchr(cwd, '/');
|
||||
if (path_end != NULL) {
|
||||
*path_end = '\0';
|
||||
}
|
||||
|
||||
return std::string(cwd);
|
||||
}
|
||||
|
||||
class aot_llvm_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// virtual void SetUp() will be called before each test is run. You
|
||||
// should define it if you need to initialize the varaibles.
|
||||
// Otherwise, this can be skipped.
|
||||
virtual void SetUp() {}
|
||||
|
||||
static void SetUpTestCase()
|
||||
{
|
||||
CWD = get_binary_path();
|
||||
WASM_FILE = strdup((CWD + MAIN_WASM).c_str());
|
||||
}
|
||||
|
||||
// virtual void TearDown() will be called after each test is run.
|
||||
// You should define it if there is cleanup work to do. Otherwise,
|
||||
// you don't have to provide it.
|
||||
//
|
||||
virtual void TearDown() {}
|
||||
static void TearDownTestCase() { free(WASM_FILE); }
|
||||
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(aot_llvm_test_suite, aot_functions)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
WASMValue wasm_value;
|
||||
LLVMTypeRef param_types[1];
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
func_ctx = comp_ctx->func_ctxes[1];
|
||||
|
||||
param_types[0] = F64_TYPE;
|
||||
EXPECT_TRUE(aot_call_llvm_intrinsic(comp_ctx, func_ctx, "f32_demote_f64",
|
||||
F32_TYPE, param_types, 0));
|
||||
|
||||
/* Test function aot_get_native_symbol_index. */
|
||||
AOTNativeSymbol elem_insert_1;
|
||||
elem_insert_1.index = -1;
|
||||
bh_list_insert(&comp_ctx->native_symbols, &elem_insert_1);
|
||||
|
||||
AOTNativeSymbol elem_insert_2;
|
||||
strcpy(elem_insert_2.symbol, "f64#_test");
|
||||
elem_insert_2.index = -1;
|
||||
bh_list_insert(&comp_ctx->native_symbols, &elem_insert_2);
|
||||
comp_ctx->pointer_size = sizeof(uint32);
|
||||
strcpy(comp_ctx->target_arch, "i386");
|
||||
EXPECT_NE(-1, aot_get_native_symbol_index(comp_ctx, "f64#_test"));
|
||||
}
|
||||
|
||||
TEST_F(aot_llvm_test_suite, wasm_type_to_llvm_type) {}
|
||||
|
||||
TEST_F(aot_llvm_test_suite, aot_build_zero_function_ret)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
AOTFuncType func_type;
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = false;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
func_ctx = comp_ctx->func_ctxes[1];
|
||||
|
||||
func_type.result_count = 1;
|
||||
func_type.param_count = 0;
|
||||
func_type.types[func_type.param_count] = VALUE_TYPE_I32;
|
||||
EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
|
||||
func_type.types[func_type.param_count] = VALUE_TYPE_I64;
|
||||
EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
|
||||
func_type.types[func_type.param_count] = VALUE_TYPE_F32;
|
||||
EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
|
||||
func_type.types[func_type.param_count] = VALUE_TYPE_F64;
|
||||
EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
|
||||
func_type.types[func_type.param_count] = VALUE_TYPE_V128;
|
||||
EXPECT_NE(0, aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type));
|
||||
/* THe current optimization, if not actually use ref_types in wasm module,
|
||||
* it will set to false, so test false condition */
|
||||
func_type.types[func_type.param_count] = VALUE_TYPE_FUNCREF;
|
||||
EXPECT_DEATH(aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type),
|
||||
".*");
|
||||
func_type.types[func_type.param_count] = VALUE_TYPE_EXTERNREF;
|
||||
EXPECT_DEATH(aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type),
|
||||
".*");
|
||||
func_type.types[func_type.param_count] = 0xFF;
|
||||
EXPECT_DEATH(aot_build_zero_function_ret(comp_ctx, func_ctx, &func_type),
|
||||
".*");
|
||||
}
|
||||
|
||||
TEST_F(aot_llvm_test_suite, aot_destroy_comp_context)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
AOTFuncContext *func_ctx = nullptr;
|
||||
AOTFuncType func_type;
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
|
||||
AOTNativeSymbol elem_insert_1;
|
||||
elem_insert_1.index = -1;
|
||||
bh_list_insert(&comp_ctx->native_symbols, &elem_insert_1);
|
||||
aot_destroy_comp_context(comp_ctx);
|
||||
|
||||
aot_destroy_comp_context(nullptr);
|
||||
}
|
||||
|
||||
TEST_F(aot_llvm_test_suite, aot_create_comp_context)
|
||||
{
|
||||
const char *wasm_file = WASM_FILE;
|
||||
unsigned int wasm_file_size = 0;
|
||||
unsigned char *wasm_file_buf = nullptr;
|
||||
char error_buf[128] = { 0 };
|
||||
wasm_module_t wasm_module = nullptr;
|
||||
|
||||
struct AOTCompData *comp_data = nullptr;
|
||||
struct AOTCompContext *comp_ctx = nullptr;
|
||||
AOTCompOption option = { 0 };
|
||||
|
||||
option.opt_level = 3;
|
||||
option.size_level = 3;
|
||||
option.output_format = AOT_FORMAT_FILE;
|
||||
/* default value, enable or disable depends on the platform */
|
||||
option.bounds_checks = 2;
|
||||
option.enable_simd = true;
|
||||
option.enable_aux_stack_check = true;
|
||||
option.enable_bulk_memory = true;
|
||||
option.enable_ref_types = true;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
|
||||
EXPECT_NE(wasm_file_buf, nullptr);
|
||||
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
EXPECT_NE(wasm_module, nullptr);
|
||||
comp_data = aot_create_comp_data((WASMModule *)wasm_module, NULL, false);
|
||||
EXPECT_NE(nullptr, comp_data);
|
||||
|
||||
option.enable_thread_mgr = true;
|
||||
option.enable_tail_call = true;
|
||||
option.is_indirect_mode = true;
|
||||
option.disable_llvm_intrinsics = true;
|
||||
option.disable_llvm_lto = true;
|
||||
option.is_jit_mode = true;
|
||||
|
||||
option.target_arch = (char *)"arm";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
option.output_format = 100;
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
|
||||
// Test every target_arch.
|
||||
option.is_jit_mode = false;
|
||||
option.target_arch = (char *)"arm";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
option.target_arch = (char *)"armeb";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
option.target_arch = (char *)"thumb";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
option.target_arch = (char *)"thumbeb";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
option.target_arch = (char *)"aarch64";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
option.target_arch = (char *)"aarch64_be";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
option.target_arch = (char *)"help";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
|
||||
// Test every target_abi.
|
||||
option.target_arch = (char *)"arm";
|
||||
option.target_abi = (char *)"test";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
option.target_abi = (char *)"help";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
option.target_abi = (char *)"msvc";
|
||||
option.target_arch = (char *)"i386";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
|
||||
option.cpu_features = (char *)"test";
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
option.is_sgx_platform = true;
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
comp_data->func_count = 0;
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
}
|
||||
BIN
tests/unit/compilation/wasm-apps/main.wasm
Normal file
BIN
tests/unit/compilation/wasm-apps/main.wasm
Normal file
Binary file not shown.
Reference in New Issue
Block a user